Skip to content

Commit dd187c9

Browse files
committed
add vulkan allocator and model demo
1 parent 826c038 commit dd187c9

File tree

10 files changed

+132
-23
lines changed

10 files changed

+132
-23
lines changed

CMakeLists.txt

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
cmake_minimum_required(VERSION 3.5)
22

33
project(vulkan-demo)
4-
5-
set(GLFW_ROOT "D:\\glfw-3.3.5.bin.WIN64")
6-
include_directories("C:\\VulkanSDK\\1.2.189.2\\Include")
7-
LINK_DIRECTORIES("C:\\VulkanSDK\\1.2.189.2\\Lib")
8-
LINK_DIRECTORIES("D:\\glfw-3.3.5.bin.WIN64\\lib-mingw-w64")
9-
104
set(CMAKE_CXX_STANDARD 17)
115
set(CMAKE_CXX_STANDARD_REQUIRED ON)
126

@@ -53,6 +47,7 @@ add_library(
5347
source/VK_ValidationLayer.cpp
5448
source/VK_ShaderSet.cpp
5549
source/VK_Util.cpp
50+
source/VK_Allocator.cpp
5651
source/VK_ObjLoader.cpp
5752
source/VK_ObjLoader.h
5853
source/tiny_obj_loader.cc
@@ -71,6 +66,7 @@ set(DEMOS
7166
uniform-buffer2
7267
sampler
7368
texture
69+
model
7470
model-mesh
7571
model-mesh-geom
7672
model-mesh-tess

demo/main-clear-color.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ VK_Context* context = nullptr;
1313

1414
void onMouseButtonCallback(int button, int action, int mods)
1515
{
16+
(void)button;
17+
(void)mods;
1618
if(action)
1719
context->setClearColor(0, 1, 0, 1);
1820
else

demo/main-model.cpp

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include <iostream>
2+
#include <cstring>
3+
#include <chrono>
4+
#include <glm/mat4x4.hpp>
5+
#include <glm/gtx/transform.hpp>
6+
#include "VK_UniformBuffer.h"
7+
#include "VK_Context.h"
8+
#include "VK_Image.h"
9+
#include "VK_Texture.h"
10+
11+
using namespace std;
12+
13+
VK_Context *context = nullptr;
14+
15+
uint32_t updateUniformBufferData(char *&data, uint32_t size)
16+
{
17+
static auto startTime = std::chrono::high_resolution_clock::now();
18+
auto currentTime = std::chrono::high_resolution_clock::now();
19+
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
20+
glm::mat4 model = glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
21+
model *= glm::rotate(glm::mat4(1.0f), time * glm::radians(30.0f), glm::vec3(0.0f, 1.0f, 0.0f));
22+
auto view = glm::lookAt(glm::vec3(0.0f, 4.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
23+
auto proj = glm::perspective(glm::radians(45.0f),
24+
context->getSwapChainExtent().width / (float)context->getSwapChainExtent().height, 0.1f, 10.0f);
25+
proj[1][1] *= -1;
26+
27+
model = proj * view * model;
28+
memcpy(data, &model[0][0], size);
29+
time = sin(time);
30+
memcpy(data + sizeof(float) * 16, (void *)&time, sizeof(float));
31+
return 17 * sizeof(float);
32+
}
33+
34+
void onFrameSizeChanged(int width, int height)
35+
{
36+
auto vp = VK_Viewports::createViewport(width, height);
37+
VK_Viewports vps;
38+
vps.addViewport(vp);
39+
context->setViewports(vps);
40+
}
41+
42+
int main()
43+
{
44+
VK_ContextConfig config;
45+
config.debug = false;
46+
config.name = "Model";
47+
48+
context = createVkContext(config);
49+
context->createWindow(480, 480, true);
50+
context->setOnFrameSizeChanged(onFrameSizeChanged);
51+
52+
VK_Context::VK_Config vkConfig;
53+
context->initVulkanDevice(vkConfig);
54+
55+
auto shaderSet = context->createShaderSet();
56+
shaderSet->addShader("../shader/model/vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
57+
shaderSet->addShader("../shader/model/frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
58+
59+
shaderSet->appendAttributeDescription(0, sizeof (float) * 3);
60+
shaderSet->appendAttributeDescription(1, sizeof (float) * 2);
61+
shaderSet->appendAttributeDescription(2, sizeof (float) * 3);
62+
63+
VkDescriptorSetLayoutBinding uniformBinding = VK_ShaderSet::createDescriptorSetLayoutBinding(0,
64+
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT);
65+
shaderSet->addDescriptorSetLayoutBinding(uniformBinding);
66+
67+
auto samplerBinding = VK_ShaderSet::createDescriptorSetLayoutBinding(1,
68+
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT);
69+
shaderSet->addDescriptorSetLayoutBinding(uniformBinding);
70+
auto samplerCreateInfo = VK_Sampler::createSamplerCreateInfo();
71+
auto samplerPtr = context->createSampler(samplerCreateInfo);
72+
VkSampler sampler = samplerPtr->getSampler();
73+
samplerBinding.pImmutableSamplers = &sampler;
74+
75+
shaderSet->addDescriptorSetLayoutBinding(samplerBinding);
76+
77+
if (!shaderSet->isValid()) {
78+
std::cerr << "invalid shaderSet" << std::endl;
79+
shaderSet->release();
80+
context->release();
81+
return -1;
82+
}
83+
84+
auto ubo = context->createUniformBuffer(0, sizeof(float) * 17);
85+
ubo->setWriteDataCallback(updateUniformBufferData);
86+
context->addUniformBuffer(ubo);
87+
88+
auto buffer = context->createVertexBuffer("../model/pug.obj", true);
89+
context->addBuffer(buffer);
90+
91+
auto image = context->createImage("../model/PUG_TAN.tga");
92+
93+
auto imageViewCreateInfo = VK_ImageView::createImageViewCreateInfo(image->getImage(),
94+
VK_FORMAT_R8G8B8A8_SRGB);
95+
auto imageView = context->createImageView(imageViewCreateInfo);
96+
context->addImageView(imageView);
97+
98+
context->initVulkanContext(shaderSet);
99+
100+
auto rasterCreateInfo = context->getPipelineRasterizationStateCreateInfo();
101+
rasterCreateInfo.cullMode = VK_CULL_MODE_NONE;
102+
context->setPipelineRasterizationStateCreateInfo(rasterCreateInfo);
103+
104+
context->initPipeline();
105+
context->createCommandBuffers();
106+
107+
context->run();
108+
context->release();
109+
110+
return 0;
111+
}
112+

model/PUG_TAN.png

67.3 KB
Loading

screenshots/model.png

59.8 KB
Loading

shader/model/frag.spv

276 Bytes
Binary file not shown.

shader/model/gl.frag

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 450
2+
layout(binding = 1) uniform sampler2D texSampler;
3+
4+
layout(location = 0) in vec3 fragColor;
5+
layout(location = 1) in vec2 fragTexCoord;
6+
7+
layout(location = 0) out vec4 outColor;
8+
9+
void main() {
10+
outColor = texture(texSampler, fragTexCoord);
11+
}

shader/model/vertexbuffer.vert renamed to shader/model/gl.vert

+5-10
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22
layout(location = 0) in vec3 inPosition;
33
layout(location = 1) in vec2 coord;
44
layout(location = 2) in vec3 inColor;
5-
layout(location = 0) out vec4 fragColor;
65
layout(binding = 0) uniform UniformBufferObject {
76
mat4 model;
87
float value;
98
} mvp;
109

10+
layout(location = 0) out vec3 fragColor;
11+
layout(location = 1) out vec2 fragTexCoord;
12+
1113
void main() {
1214
gl_Position = mvp.model*vec4(inPosition, 1.0);
13-
14-
if(inPosition.y > mvp.value)
15-
{
16-
fragColor = vec4(1.0,0.3,0.3,1.0);
17-
}
18-
else
19-
{
20-
fragColor = vec4(0.3,0.3,1.0,1.0);
21-
}
15+
fragColor = inColor;
16+
fragTexCoord = coord;
2217
}

shader/model/vert.spv

-164 Bytes
Binary file not shown.

shader/model/vertexbuffer.frag

-7
This file was deleted.

0 commit comments

Comments
 (0)