forked from SaschaWillems/Vulkan
-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
shadowmapping.cpp
421 lines (349 loc) · 16.9 KB
/
shadowmapping.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
* Vulkan Example - Offscreen rendering using a separate framebuffer
*
* p - Toggle light source animation
* l - Toggle between scene and light's POV
* s - Toggle shadowmap display
*
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#include <vulkanOffscreenExampleBase.hpp>
// Texture properties
// Vertex layout for this example
vks::model::VertexLayout vertexLayout{ {
vks::model::Component::VERTEX_COMPONENT_POSITION,
vks::model::Component::VERTEX_COMPONENT_UV,
vks::model::Component::VERTEX_COMPONENT_COLOR,
vks::model::Component::VERTEX_COMPONENT_NORMAL,
} };
class VulkanExample : public vkx::OffscreenExampleBase {
using Parent = OffscreenExampleBase;
public:
bool displayShadowMap = false;
bool lightPOV = false;
// Keep depth range as small as possible
// for better shadow map precision
float zNear = 1.0f;
float zFar = 96.0f;
// Constant depth bias factor (always applied)
float depthBiasConstant = 1.25f;
// Slope depth bias factor, applied depending on polygon's slope
float depthBiasSlope = 1.75f;
glm::vec3 lightPos = glm::vec3();
float lightFOV = 45.0f;
struct {
vks::model::Model scene;
vks::model::Model quad;
} meshes;
vks::Buffer uniformDataVS, uniformDataOffscreenVS;
struct {
vks::Buffer scene;
} uniformData;
struct {
glm::mat4 projection;
glm::mat4 model;
} uboVSquad;
struct {
glm::mat4 projection;
glm::mat4 view;
glm::mat4 model;
glm::mat4 depthBiasMVP;
glm::vec3 lightPos;
} uboVSscene;
struct {
glm::mat4 depthMVP;
} uboOffscreenVS;
struct {
vk::Pipeline quad;
vk::Pipeline offscreen;
vk::Pipeline scene;
} pipelines;
struct {
vk::PipelineLayout quad;
vk::PipelineLayout offscreen;
} pipelineLayouts;
struct {
vk::DescriptorSet offscreen;
vk::DescriptorSet scene;
} descriptorSets;
vk::DescriptorSet descriptorSet;
vk::DescriptorSetLayout descriptorSetLayout;
VulkanExample() {
enableVsync = true;
camera.type = Camera::lookat;
camera.setRotation({ -15.0f, -390.0f, 0.0f });
camera.dolly(-10.0f);
title = "Vulkan Example - Projected shadow mapping";
timerSpeed *= 0.5f;
}
~VulkanExample() {
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
device.destroyPipeline(pipelines.quad);
device.destroyPipeline(pipelines.offscreen);
device.destroyPipeline(pipelines.scene);
device.destroyPipelineLayout(pipelineLayouts.quad);
device.destroyPipelineLayout(pipelineLayouts.offscreen);
device.destroyDescriptorSetLayout(descriptorSetLayout);
// Meshes
meshes.scene.destroy();
meshes.quad.destroy();
// Uniform buffers
uniformDataVS.destroy();
uniformDataOffscreenVS.destroy();
}
void buildOffscreenCommandBuffer() override {
// Create separate command buffer for offscreen
// rendering
if (offscreen.cmdBuffer) {
std::vector<vk::CommandBuffer> buffers{ { offscreen.cmdBuffer } };
context.trashCommandBuffers(context.getCommandPool(), buffers);
}
offscreen.cmdBuffer = device.allocateCommandBuffers({ cmdPool, vk::CommandBufferLevel::ePrimary, 1 })[0];
vk::CommandBufferBeginInfo cmdBufInfo;
cmdBufInfo.flags = vk::CommandBufferUsageFlagBits::eSimultaneousUse;
offscreen.cmdBuffer.begin(cmdBufInfo);
vk::ClearValue clearValues[2];
clearValues[0].color = vks::util::clearColor();
clearValues[1].depthStencil = vk::ClearDepthStencilValue{ 1.0f, 0 };
vk::RenderPassBeginInfo renderPassBeginInfo;
renderPassBeginInfo.renderPass = offscreen.renderPass;
renderPassBeginInfo.framebuffer = offscreen.framebuffers[0].framebuffer;
renderPassBeginInfo.renderArea.extent.width = offscreen.size.x;
renderPassBeginInfo.renderArea.extent.height = offscreen.size.y;
renderPassBeginInfo.clearValueCount = 2;
renderPassBeginInfo.pClearValues = clearValues;
offscreen.cmdBuffer.setViewport(0, vks::util::viewport(offscreen.size));
offscreen.cmdBuffer.setScissor(0, vks::util::rect2D(offscreen.size));
// Set depth bias (aka "Polygon offset")
offscreen.cmdBuffer.setDepthBias(depthBiasConstant, 0.0f, depthBiasSlope);
offscreen.cmdBuffer.beginRenderPass(renderPassBeginInfo, vk::SubpassContents::eInline);
offscreen.cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipelines.offscreen);
offscreen.cmdBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayouts.offscreen, 0, descriptorSets.offscreen, nullptr);
offscreen.cmdBuffer.bindVertexBuffers(0, meshes.scene.vertices.buffer, { 0 });
offscreen.cmdBuffer.bindIndexBuffer(meshes.scene.indices.buffer, 0, vk::IndexType::eUint32);
offscreen.cmdBuffer.drawIndexed(meshes.scene.indexCount, 1, 0, 0, 0);
offscreen.cmdBuffer.endRenderPass();
offscreen.cmdBuffer.end();
}
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, pipelineLayouts.quad, 0, descriptorSet, nullptr);
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipelines.quad);
// Visualize shadow map
if (displayShadowMap) {
cmdBuffer.bindVertexBuffers(0, meshes.quad.vertices.buffer, { 0 });
cmdBuffer.bindIndexBuffer(meshes.quad.indices.buffer, 0, vk::IndexType::eUint32);
cmdBuffer.drawIndexed(meshes.quad.indexCount, 1, 0, 0, 0);
}
// 3D scene
cmdBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayouts.quad, 0, descriptorSets.scene, nullptr);
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipelines.scene);
cmdBuffer.bindVertexBuffers(0, meshes.scene.vertices.buffer, { 0 });
cmdBuffer.bindIndexBuffer(meshes.scene.indices.buffer, 0, vk::IndexType::eUint32);
cmdBuffer.drawIndexed(meshes.scene.indexCount, 1, 0, 0, 0);
}
void loadAssets() override { meshes.scene.loadFromFile(context, getAssetPath() + "models/vulkanscene_shadow.dae", vertexLayout, 4.0f); }
void generateQuad() {
// Setup vertices for a single uv-mapped quad
struct Vertex {
float pos[3];
float uv[2];
float col[3];
float normal[3];
};
#define QUAD_COLOR_NORMAL { 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f }
std::vector<Vertex> vertexBuffer = { { { 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f }, QUAD_COLOR_NORMAL },
{ { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f }, QUAD_COLOR_NORMAL },
{ { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f }, QUAD_COLOR_NORMAL },
{ { 1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f }, QUAD_COLOR_NORMAL } };
#undef QUAD_COLOR_NORMAL
meshes.quad.vertices = context.stageToDeviceBuffer(vk::BufferUsageFlagBits::eVertexBuffer, vertexBuffer);
// Setup indices
std::vector<uint32_t> indexBuffer = { 0, 1, 2, 2, 3, 0 };
meshes.quad.indexCount = (uint32_t)indexBuffer.size();
meshes.quad.indices = context.stageToDeviceBuffer(vk::BufferUsageFlagBits::eIndexBuffer, indexBuffer);
}
void setupDescriptorPool() {
// Example uses three ubos and two image samplers
std::vector<vk::DescriptorPoolSize> poolSizes = {
{ vk::DescriptorType::eUniformBuffer, 6 },
{ vk::DescriptorType::eCombinedImageSampler, 4 },
};
descriptorPool = device.createDescriptorPool({ {}, 3, (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 image sampler
{ 1, vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment },
};
descriptorSetLayout = device.createDescriptorSetLayout({ {}, (uint32_t)setLayoutBindings.size(), setLayoutBindings.data() });
// Textured quad pipeline layout
pipelineLayouts.quad = device.createPipelineLayout({ {}, 1, &descriptorSetLayout });
// Offscreen pipeline layout
pipelineLayouts.offscreen = device.createPipelineLayout({ {}, 1, &descriptorSetLayout });
}
void setupDescriptorSets() {
// Textured quad descriptor set
vk::DescriptorSetAllocateInfo allocInfo{
descriptorPool,
1,
&descriptorSetLayout,
};
descriptorSet = device.allocateDescriptorSets(allocInfo)[0];
// vk::Image descriptor for the shadow map texture
vk::DescriptorImageInfo texDescriptor{ offscreen.framebuffers[0].depth.sampler, offscreen.framebuffers[0].depth.view, vk::ImageLayout::eGeneral };
std::vector<vk::WriteDescriptorSet> writeDescriptorSets = {
// Binding 0 : Vertex shader uniform buffer
{ descriptorSet, 0, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &uniformDataVS.descriptor },
// Binding 1 : Fragment shader texture sampler
{ descriptorSet, 1, 0, 1, vk::DescriptorType::eCombinedImageSampler, &texDescriptor },
};
device.updateDescriptorSets(writeDescriptorSets, nullptr);
// Offscreen
descriptorSets.offscreen = device.allocateDescriptorSets(allocInfo)[0];
std::vector<vk::WriteDescriptorSet> offscreenWriteDescriptorSets = {
// Binding 0 : Vertex shader uniform buffer
{ descriptorSets.offscreen, 0, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &uniformDataOffscreenVS.descriptor },
};
device.updateDescriptorSets(offscreenWriteDescriptorSets, nullptr);
// 3D scene
descriptorSets.scene = device.allocateDescriptorSets(allocInfo)[0];
// vk::Image descriptor for the shadow map texture
texDescriptor.sampler = offscreen.framebuffers[0].depth.sampler;
texDescriptor.imageView = offscreen.framebuffers[0].depth.view;
std::vector<vk::WriteDescriptorSet> sceneDescriptorSets{
// Binding 0 : Vertex shader uniform buffer
{ descriptorSets.scene, 0, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &uniformData.scene.descriptor },
// Binding 1 : Fragment shader shadow sampler
{ descriptorSets.scene, 1, 0, 1, vk::DescriptorType::eCombinedImageSampler, &texDescriptor },
};
device.updateDescriptorSets(sceneDescriptorSets, nullptr);
}
void preparePipelines() {
// Solid rendering pipeline
vks::pipelines::GraphicsPipelineBuilder pipelineCreator{ device, pipelineLayouts.quad, renderPass };
pipelineCreator.rasterizationState.frontFace = vk::FrontFace::eClockwise;
pipelineCreator.rasterizationState.cullMode = vk::CullModeFlagBits::eNone;
pipelineCreator.vertexInputState.appendVertexLayout(vertexLayout);
pipelineCreator.loadShader(getAssetPath() + "shaders/shadowmapping/quad.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineCreator.loadShader(getAssetPath() + "shaders/shadowmapping/quad.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelines.quad = pipelineCreator.create(context.pipelineCache);
pipelineCreator.destroyShaderModules();
// 3D scene
pipelineCreator.loadShader(getAssetPath() + "shaders/shadowmapping/scene.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineCreator.loadShader(getAssetPath() + "shaders/shadowmapping/scene.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelines.scene = pipelineCreator.create(context.pipelineCache);
pipelineCreator.destroyShaderModules();
// Offscreen pipeline
pipelineCreator.loadShader(getAssetPath() + "shaders/shadowmapping/offscreen.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineCreator.loadShader(getAssetPath() + "shaders/shadowmapping/offscreen.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelineCreator.layout = pipelineLayouts.offscreen;
pipelineCreator.renderPass = offscreen.renderPass;
pipelineCreator.depthStencilState.depthCompareOp = vk::CompareOp::eLessOrEqual;
pipelineCreator.rasterizationState.depthBiasEnable = VK_TRUE;
// Add depth bias to dynamic state, so we can change it at runtime
pipelineCreator.dynamicState.dynamicStateEnables.push_back(vk::DynamicState::eDepthBias);
pipelines.offscreen = pipelineCreator.create(context.pipelineCache);
}
// Prepare and initialize uniform buffer containing shader uniforms
void prepareUniformBuffers() {
// Debug quad vertex shader uniform buffer block
uniformDataVS = context.createUniformBuffer(uboVSscene);
// Offsvreen vertex shader uniform buffer block
uniformDataOffscreenVS = context.createUniformBuffer(uboOffscreenVS);
// Scene vertex shader uniform buffer block
uniformData.scene = context.createUniformBuffer(uboVSscene);
updateLight();
updateUniformBufferOffscreen();
updateUniformBuffers();
}
void updateLight() {
// Animate the light source
lightPos.x = cos(glm::radians(timer * 360.0f)) * 40.0f;
lightPos.y = -50.0f + sin(glm::radians(timer * 360.0f)) * 20.0f;
lightPos.z = 25.0f + sin(glm::radians(timer * 360.0f)) * 5.0f;
}
void updateUniformBuffers() {
// Shadow map debug quad
float AR = (float)size.height / (float)size.width;
uboVSquad.projection = glm::ortho(2.5f / AR, 0.0f, 0.0f, 2.5f, -1.0f, 1.0f);
uboVSquad.model = glm::mat4();
uniformDataVS.copy(uboVSquad);
// 3D scene
uboVSscene.projection = glm::perspective(glm::radians(45.0f), (float)size.width / (float)size.height, zNear, zFar);
uboVSscene.view = camera.matrices.view;
uboVSscene.model = glm::mat4();
uboVSscene.lightPos = lightPos;
// Render scene from light's point of view
if (lightPOV) {
uboVSscene.projection = glm::perspective(glm::radians(lightFOV), (float)size.width / (float)size.height, zNear, zFar);
uboVSscene.view = glm::lookAt(lightPos, glm::vec3(0.0f), glm::vec3(0, 1, 0));
}
uboVSscene.depthBiasMVP = uboOffscreenVS.depthMVP;
uniformData.scene.copy(uboVSscene);
}
void updateUniformBufferOffscreen() {
// Matrix from light's point of view
glm::mat4 depthProjectionMatrix = glm::perspective(glm::radians(lightFOV), 1.0f, zNear, zFar);
glm::mat4 depthViewMatrix = glm::lookAt(lightPos, glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
glm::mat4 depthModelMatrix = glm::mat4();
uboOffscreenVS.depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix;
uniformDataOffscreenVS.copy(uboOffscreenVS);
}
#define TEX_FILTER vk::Filter::eLinear
void prepare() override {
offscreen.size = glm::uvec2(2048);
offscreen.depthFormat = vk::Format::eD16Unorm;
offscreen.depthFinalLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
offscreen.colorFinalLayout = vk::ImageLayout::eColorAttachmentOptimal;
offscreen.attachmentUsage = vk::ImageUsageFlags();
offscreen.depthAttachmentUsage = vk::ImageUsageFlagBits::eSampled;
OffscreenExampleBase::prepare();
generateQuad();
prepareUniformBuffers();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSets();
buildCommandBuffers();
buildOffscreenCommandBuffer();
prepared = true;
}
void update(float deltaTime) override {
Parent::update(deltaTime);
if (!paused) {
updateLight();
updateUniformBufferOffscreen();
updateUniformBuffers();
}
}
void viewChanged() override {
updateUniformBufferOffscreen();
updateUniformBuffers();
}
void toggleShadowMapDisplay() {
displayShadowMap = !displayShadowMap;
buildCommandBuffers();
}
void toogleLightPOV() {
lightPOV = !lightPOV;
viewChanged();
}
void keyPressed(uint32_t key) override {
switch (key) {
case KEY_S:
toggleShadowMapDisplay();
break;
case KEY_L:
toogleLightPOV();
break;
}
}
};
RUN_EXAMPLE(VulkanExample)