forked from SaschaWillems/Vulkan
-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathmesh.cpp
More file actions
196 lines (167 loc) · 7.24 KB
/
mesh.cpp
File metadata and controls
196 lines (167 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Vulkan Example - Mesh rendering and loading using ASSIMP
*
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#include <vulkanExampleBase.h>
using namespace vkx;
// Vertex layout used in this example
struct Vertex {
glm::vec3 pos;
glm::vec3 normal;
glm::vec2 uv;
glm::vec3 color;
};
class VulkanExample : public ExampleBase {
public:
bool wireframe = false;
struct {
vks::texture::Texture2D colorMap;
} textures;
// Contains all buffers and information
// necessary to represent a mesh for rendering purposes
// This is for demonstration and learning purposes,
// the other examples use a mesh loader class for easy access
struct Mesh {
vks::model::VertexLayout vertexLayout{ {
vks::model::VERTEX_COMPONENT_POSITION,
vks::model::VERTEX_COMPONENT_NORMAL,
vks::model::VERTEX_COMPONENT_UV,
vks::model::VERTEX_COMPONENT_COLOR,
} };
vks::model::Model model;
} meshes;
struct {
vks::Buffer vsScene;
} uniformData;
struct {
glm::mat4 projection;
glm::mat4 model;
glm::vec4 lightPos = glm::vec4(25.0f, 5.0f, 5.0f, 1.0f);
} uboVS;
struct {
vk::Pipeline solid;
vk::Pipeline wireframe;
} pipelines;
vk::PipelineLayout pipelineLayout;
vk::DescriptorSet descriptorSet;
vk::DescriptorSetLayout descriptorSetLayout;
VulkanExample() {
zoomSpeed = 2.5f;
rotationSpeed = 0.5f;
camera.setRotation({ -0.5f, -112.75f, 0.0f });
camera.setTranslation({ -0.1f, 1.1f, -5.5f });
title = "Vulkan Example - Mesh rendering";
}
~VulkanExample() {
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
device.destroyPipeline(pipelines.wireframe);
device.destroyPipeline(pipelines.solid);
device.destroyPipelineLayout(pipelineLayout);
device.destroyDescriptorSetLayout(descriptorSetLayout);
// Destroy and free mesh resources
meshes.model.destroy();
textures.colorMap.destroy();
uniformData.vsScene.destroy();
}
void updateDrawCommandBuffer(const vk::CommandBuffer& cmdBuffer) override {
cmdBuffer.setViewport(0, vks::util::viewport(size));
cmdBuffer.setScissor(0, vks::util::rect2D(size));
cmdBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, descriptorSet, nullptr);
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, wireframe ? pipelines.wireframe : pipelines.solid);
// Bind mesh vertex buffer
cmdBuffer.bindVertexBuffers(0, meshes.model.vertices.buffer, { 0 });
// Bind mesh index buffer
cmdBuffer.bindIndexBuffer(meshes.model.indices.buffer, 0, vk::IndexType::eUint32);
// Render mesh vertex buffer using it's indices
cmdBuffer.drawIndexed(meshes.model.indexCount, 1, 0, 0, 0);
}
// Load a mesh based on data read via assimp
// The other example will use the VulkanMesh loader which has some additional functionality for loading meshes
void loadAssets() override {
meshes.model.loadFromFile(context, getAssetPath() + "models/voyager/voyager.dae", meshes.vertexLayout);
textures.colorMap.loadFromFile(context, getAssetPath() + "models/voyager/voyager.ktx", vk::Format::eBc3UnormBlock);
}
void setupDescriptorPool() {
// Example uses one ubo and one combined image sampler
std::vector<vk::DescriptorPoolSize> poolSizes = {
vk::DescriptorPoolSize(vk::DescriptorType::eUniformBuffer, 1),
vk::DescriptorPoolSize(vk::DescriptorType::eCombinedImageSampler, 1),
};
descriptorPool = device.createDescriptorPool({ {}, 1, (uint32_t)poolSizes.size(), poolSizes.data() });
}
void setupDescriptorSetLayout() {
std::vector<vk::DescriptorSetLayoutBinding> setLayoutBindings{
// Binding 0 : Vertex shader uniform buffer
{ 0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex },
// Binding 1 : Fragment shader combined sampler
{ 1, vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment },
};
descriptorSetLayout = device.createDescriptorSetLayout({ {}, (uint32_t)setLayoutBindings.size(), setLayoutBindings.data() });
pipelineLayout = device.createPipelineLayout({ {}, 1, &descriptorSetLayout });
}
void setupDescriptorSet() {
vk::DescriptorSetAllocateInfo allocInfo{ descriptorPool, 1, &descriptorSetLayout };
descriptorSet = device.allocateDescriptorSets(allocInfo)[0];
vk::DescriptorImageInfo texDescriptor{ textures.colorMap.sampler, textures.colorMap.view, vk::ImageLayout::eGeneral };
std::vector<vk::WriteDescriptorSet> writeDescriptorSets{
// Binding 0 : Vertex shader uniform buffer
{ descriptorSet, 0, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &uniformData.vsScene.descriptor },
// Binding 1 : Color map
{ descriptorSet, 1, 0, 1, vk::DescriptorType::eCombinedImageSampler, &texDescriptor },
};
device.updateDescriptorSets(writeDescriptorSets, nullptr);
}
void preparePipelines() {
// Solid rendering pipeline
vks::pipelines::GraphicsPipelineBuilder pipelineBuilder{ device, pipelineLayout, renderPass };
pipelineBuilder.rasterizationState.frontFace = vk::FrontFace::eClockwise;
pipelineBuilder.vertexInputState.appendVertexLayout(meshes.vertexLayout);
pipelineBuilder.loadShader(getAssetPath() + "shaders/mesh/mesh.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineBuilder.loadShader(getAssetPath() + "shaders/mesh/mesh.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelines.solid = pipelineBuilder.create(context.pipelineCache);
// Wire frame rendering pipeline
pipelineBuilder.rasterizationState.polygonMode = vk::PolygonMode::eLine;
pipelines.wireframe = pipelineBuilder.create(context.pipelineCache);
}
// Prepare and initialize uniform buffer containing shader uniforms
void prepareUniformBuffers() {
// Vertex shader uniform buffer block
uniformData.vsScene = context.createUniformBuffer(uboVS);
updateUniformBuffers();
}
void updateUniformBuffers() {
uboVS.projection = getProjection();
uboVS.model = getView();
uniformData.vsScene.copy(uboVS);
}
void prepare() override {
ExampleBase::prepare();
prepareUniformBuffers();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
buildCommandBuffers();
prepared = true;
}
void render() override {
if (!prepared)
return;
draw();
}
void viewChanged() override { updateUniformBuffers(); }
void keyPressed(uint32_t keyCode) override {
switch (keyCode) {
case KEY_W:
case GAMEPAD_BUTTON_A:
wireframe = !wireframe;
buildCommandBuffers();
break;
}
}
};
RUN_EXAMPLE(VulkanExample)