forked from SaschaWillems/Vulkan
-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathvulkanscene.cpp
243 lines (201 loc) · 9.54 KB
/
vulkanscene.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Vulkan Demo Scene
*
* Don't take this a an example, it's more of a personal playground
*
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
*
* Note : Different license than the other examples!
*
* This code is licensed under the Mozilla Public License Version 2.0 (http://opensource.org/licenses/MPL-2.0)
*/
#include <vulkanExampleBase.h>
static std::vector<std::string> names{ "logos", "background", "models", "skybox" };
class VulkanExample : public vkx::ExampleBase {
public:
using DemoMesh = std::pair<vks::model::Model, vk::Pipeline>;
struct DemoMeshes {
DemoMesh logos;
DemoMesh background;
DemoMesh models;
DemoMesh skybox;
} demoMeshes;
struct {
vks::Buffer meshVS;
} uniformData;
struct {
glm::mat4 projection;
glm::mat4 model;
glm::mat4 normal;
glm::mat4 view;
glm::vec4 lightPos;
} uboVS;
struct {
vks::texture::TextureCubeMap skybox;
} textures;
struct {
vk::Pipeline logos;
vk::Pipeline models;
vk::Pipeline skybox;
} pipelines;
vk::PipelineLayout pipelineLayout;
vk::DescriptorSet descriptorSet;
vk::DescriptorSetLayout descriptorSetLayout;
glm::vec4 lightPos = glm::vec4(1.0f, 2.0f, 0.0f, 0.0f);
VulkanExample() {
size.width = 1280;
size.height = 720;
rotationSpeed = 0.5f;
camera.setRotation({ 15.0f, 0.f, 0.0f });
title = "Vulkan Demo Scene - � 2016 by Sascha Willems";
}
~VulkanExample() {
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
device.destroyPipeline(pipelines.logos);
device.destroyPipeline(pipelines.models);
device.destroyPipeline(pipelines.skybox);
device.destroyPipelineLayout(pipelineLayout);
device.destroyDescriptorSetLayout(descriptorSetLayout);
uniformData.meshVS.destroy();
demoMeshes.logos.first.destroy();
demoMeshes.background.first.destroy();
demoMeshes.models.first.destroy();
demoMeshes.skybox.first.destroy();
textures.skybox.destroy();
}
void loadTextures() { textures.skybox.loadFromFile(context, getAssetPath() + "textures/cubemap_vulkan.ktx", vk::Format::eR8G8B8A8Unorm); }
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);
std::array<DemoMesh*, 4> meshes{ {
&demoMeshes.skybox,
&demoMeshes.background,
&demoMeshes.logos,
&demoMeshes.models,
} };
for (auto& meshPtr : meshes) {
const auto& pipeline = meshPtr->second;
const auto& mesh = meshPtr->first;
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline);
cmdBuffer.bindVertexBuffers(0, mesh.vertices.buffer, { 0 });
cmdBuffer.bindIndexBuffer(mesh.indices.buffer, 0, vk::IndexType::eUint32);
cmdBuffer.drawIndexed(mesh.indexCount, 1, 0, 0, 0);
}
}
vks::model::VertexLayout vertexLayout{ {
vks::model::VERTEX_COMPONENT_POSITION,
vks::model::VERTEX_COMPONENT_NORMAL,
vks::model::VERTEX_COMPONENT_UV,
vks::model::VERTEX_COMPONENT_COLOR,
} };
void prepareVertices() {
struct Vertex {
float pos[3];
float normal[3];
float uv[2];
float color[3];
};
// Load meshes for demos scene
demoMeshes.logos.first.loadFromFile(context, getAssetPath() + "models/vulkanscenelogos.dae", vertexLayout);
demoMeshes.background.first.loadFromFile(context, getAssetPath() + "models/vulkanscenebackground.dae", vertexLayout);
demoMeshes.models.first.loadFromFile(context, getAssetPath() + "models/vulkanscenemodels.dae", vertexLayout);
demoMeshes.skybox.first.loadFromFile(context, getAssetPath() + "models/cube.obj", vertexLayout);
}
void setupDescriptorPool() {
// Example uses one ubo and one image sampler
std::vector<vk::DescriptorPoolSize> poolSizes{
{ vk::DescriptorType::eUniformBuffer, 2 },
{ vk::DescriptorType::eCombinedImageSampler, 1 },
};
descriptorPool = device.createDescriptorPool({ {}, 2, (uint32_t)poolSizes.size(), poolSizes.data() });
}
void setupDescriptorSetLayout() {
std::vector<vk::DescriptorSetLayoutBinding> setLayoutBindings{ { 0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex },
{ 1, vk::DescriptorType::eCombinedImageSampler, 1,
vk::ShaderStageFlagBits::eFragment } };
descriptorSetLayout = device.createDescriptorSetLayout({ {}, (uint32_t)setLayoutBindings.size(), setLayoutBindings.data() });
pipelineLayout = device.createPipelineLayout({ {}, 1, &descriptorSetLayout });
}
void setupDescriptorSet() {
descriptorSet = device.allocateDescriptorSets({ descriptorPool, 1, &descriptorSetLayout })[0];
// Cube map image descriptor
vk::DescriptorImageInfo texDescriptorCubeMap{ textures.skybox.sampler, textures.skybox.view, vk::ImageLayout::eGeneral };
std::vector<vk::WriteDescriptorSet> writeDescriptorSets{
// Binding 0 : Vertex shader uniform buffer
{ descriptorSet, 0, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &uniformData.meshVS.descriptor },
// Binding 1 : Fragment shader image sampler
{ descriptorSet, 1, 0, 1, vk::DescriptorType::eCombinedImageSampler, &texDescriptorCubeMap },
};
device.updateDescriptorSets(writeDescriptorSets, nullptr);
}
void preparePipelines() {
vks::pipelines::GraphicsPipelineBuilder pipelineBuilder{ device, pipelineLayout, renderPass };
pipelineBuilder.rasterizationState.frontFace = vk::FrontFace::eClockwise;
// Binding description
pipelineBuilder.vertexInputState.bindingDescriptions = { { 0, vertexLayout.stride(), vk::VertexInputRate::eVertex } };
pipelineBuilder.vertexInputState.attributeDescriptions = {
// Location 0 : Position
{ 0, 0, vk::Format::eR32G32B32Sfloat, 0 },
// Location 1 : Normal
{ 1, 0, vk::Format::eR32G32B32Sfloat, sizeof(float) * 3 },
// Location 2 : Texture coordinates
{ 2, 0, vk::Format::eR32G32Sfloat, sizeof(float) * 6 },
// Location 3 : Color
{ 3, 0, vk::Format::eR32G32B32Sfloat, sizeof(float) * 8 },
};
// Attribute descriptions
// vk::Pipeline for the meshes (armadillo, bunny, etc.)
// Load shaders
pipelineBuilder.loadShader(getAssetPath() + "shaders/vulkanscene/mesh.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineBuilder.loadShader(getAssetPath() + "shaders/vulkanscene/mesh.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelines.models = pipelineBuilder.create(context.pipelineCache);
pipelineBuilder.destroyShaderModules();
// vk::Pipeline for the logos
pipelineBuilder.loadShader(getAssetPath() + "shaders/vulkanscene/logo.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineBuilder.loadShader(getAssetPath() + "shaders/vulkanscene/logo.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelines.logos = pipelineBuilder.create(context.pipelineCache);
pipelineBuilder.destroyShaderModules();
// vk::Pipeline for the sky sphere (todo)
pipelineBuilder.rasterizationState.cullMode = vk::CullModeFlagBits::eFront; // Inverted culling
pipelineBuilder.depthStencilState.depthWriteEnable = VK_FALSE; // No depth writes
pipelineBuilder.loadShader(getAssetPath() + "shaders/vulkanscene/skybox.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineBuilder.loadShader(getAssetPath() + "shaders/vulkanscene/skybox.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelines.skybox = pipelineBuilder.create(context.pipelineCache);
// Assign pipelines
demoMeshes.logos.second = pipelines.logos;
demoMeshes.models.second = pipelines.models;
demoMeshes.background.second = pipelines.models;
demoMeshes.skybox.second = pipelines.skybox;
}
// Prepare and initialize uniform buffer containing shader uniforms
void prepareUniformBuffers() {
// Vertex shader uniform buffer block
uniformData.meshVS = context.createUniformBuffer(uboVS);
updateUniformBuffers();
}
void updateUniformBuffers() {
uboVS.projection = getProjection();
uboVS.view = glm::translate(glm::mat4(), glm::vec3(0, 0, camera.position.z));
uboVS.model = camera.matrices.view;
uboVS.model[3] = vec4{ 0, 0, 0, 1 };
uboVS.normal = glm::inverseTranspose(uboVS.view * uboVS.model);
uboVS.lightPos = lightPos;
uniformData.meshVS.copy(uboVS);
}
void prepare() override {
ExampleBase::prepare();
loadTextures();
prepareVertices();
prepareUniformBuffers();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
buildCommandBuffers();
prepared = true;
}
void viewChanged() override { updateUniformBuffers(); }
};
RUN_EXAMPLE(VulkanExample)