Code cleanuo

This commit is contained in:
Sascha Willems 2024-01-19 12:00:08 +01:00
parent 24591c6570
commit cd84777ada

View file

@ -1,7 +1,13 @@
/* /*
* Vulkan Example - Deferred shading with multiple render targets (aka G-Buffer) example * Vulkan Example - Deferred shading with multiple render targets (aka G-Buffer) example
* *
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de * This samples shows how to do deferred rendering. Unlike forward rendering, different components like
* albedo, normals, world positions are rendered to offscreen images which are then put together and lit
* in a composition pass
* Use the dropdown in the ui to switch between the final composition pass or the separate components
*
*
* Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de
* *
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/ */
@ -9,14 +15,6 @@
#include "vulkanexamplebase.h" #include "vulkanexamplebase.h"
#include "VulkanglTFModel.h" #include "VulkanglTFModel.h"
// Texture properties
#define TEX_DIM 2048
#define TEX_FILTER VK_FILTER_LINEAR
// Offscreen frame buffer properties
#define FB_DIM TEX_DIM
class VulkanExample : public VulkanExampleBase class VulkanExample : public VulkanExampleBase
{ {
public: public:
@ -58,25 +56,25 @@ public:
} uboComposition; } uboComposition;
struct { struct {
vks::Buffer offscreen; vks::Buffer offscreen{ VK_NULL_HANDLE };
vks::Buffer composition; vks::Buffer composition{ VK_NULL_HANDLE };
} uniformBuffers; } uniformBuffers;
struct { struct {
VkPipeline offscreen; VkPipeline offscreen{ VK_NULL_HANDLE };
VkPipeline composition; VkPipeline composition{ VK_NULL_HANDLE };
} pipelines; } pipelines;
VkPipelineLayout pipelineLayout; VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
struct { struct {
VkDescriptorSet model; VkDescriptorSet model{ VK_NULL_HANDLE };
VkDescriptorSet floor; VkDescriptorSet floor{ VK_NULL_HANDLE };
VkDescriptorSet composition{ VK_NULL_HANDLE };
} descriptorSets; } descriptorSets;
VkDescriptorSet descriptorSet; VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE };
VkDescriptorSetLayout descriptorSetLayout;
// Framebuffer for offscreen rendering // Framebuffers holding the deferred attachments
struct FrameBufferAttachment { struct FrameBufferAttachment {
VkImage image; VkImage image;
VkDeviceMemory mem; VkDeviceMemory mem;
@ -86,18 +84,19 @@ public:
struct FrameBuffer { struct FrameBuffer {
int32_t width, height; int32_t width, height;
VkFramebuffer frameBuffer; VkFramebuffer frameBuffer;
// One attachment for every component required for a deferred rendering setup
FrameBufferAttachment position, normal, albedo; FrameBufferAttachment position, normal, albedo;
FrameBufferAttachment depth; FrameBufferAttachment depth;
VkRenderPass renderPass; VkRenderPass renderPass;
} offScreenFrameBuf; } offScreenFrameBuf{};
// One sampler for the frame buffer color attachments // One sampler for the frame buffer color attachments
VkSampler colorSampler; VkSampler colorSampler{ VK_NULL_HANDLE };
VkCommandBuffer offScreenCmdBuffer = VK_NULL_HANDLE; VkCommandBuffer offScreenCmdBuffer{ VK_NULL_HANDLE };
// Semaphore used to synchronize between offscreen and final scene rendering // Semaphore used to synchronize between offscreen and final scene rendering
VkSemaphore offscreenSemaphore = VK_NULL_HANDLE; VkSemaphore offscreenSemaphore{ VK_NULL_HANDLE };
VulkanExample() : VulkanExampleBase() VulkanExample() : VulkanExampleBase()
{ {
@ -114,9 +113,7 @@ public:
~VulkanExample() ~VulkanExample()
{ {
// Clean up used Vulkan resources if (device) {
// Note : Inherited destructor cleans up resources stored in base class
vkDestroySampler(device, colorSampler, nullptr); vkDestroySampler(device, colorSampler, nullptr);
// Frame buffer // Frame buffer
@ -161,6 +158,7 @@ public:
vkDestroySemaphore(device, offscreenSemaphore, nullptr); vkDestroySemaphore(device, offscreenSemaphore, nullptr);
} }
}
// Enable physical device features required for this example // Enable physical device features required for this example
virtual void getEnabledFeatures() virtual void getEnabledFeatures()
@ -235,8 +233,9 @@ public:
// Prepare a new framebuffer and attachments for offscreen rendering (G-Buffer) // Prepare a new framebuffer and attachments for offscreen rendering (G-Buffer)
void prepareOffscreenFramebuffer() void prepareOffscreenFramebuffer()
{ {
offScreenFrameBuf.width = FB_DIM; // Note: Instead of using fixed sizes, one could also match the window size and recreate the attachments on resize
offScreenFrameBuf.height = FB_DIM; offScreenFrameBuf.width = 2048;
offScreenFrameBuf.height = 2048;
// Color attachments // Color attachments
@ -380,8 +379,7 @@ public:
// Build command buffer for rendering the scene to the offscreen frame buffer attachments // Build command buffer for rendering the scene to the offscreen frame buffer attachments
void buildDeferredCommandBuffer() void buildDeferredCommandBuffer()
{ {
if (offScreenCmdBuffer == VK_NULL_HANDLE) if (offScreenCmdBuffer == VK_NULL_HANDLE) {
{
offScreenCmdBuffer = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, false); offScreenCmdBuffer = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, false);
} }
@ -418,11 +416,11 @@ public:
vkCmdBindPipeline(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.offscreen); vkCmdBindPipeline(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.offscreen);
// Background // Floor
vkCmdBindDescriptorSets(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.floor, 0, nullptr); vkCmdBindDescriptorSets(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.floor, 0, nullptr);
models.floor.draw(offScreenCmdBuffer); models.floor.draw(offScreenCmdBuffer);
// Instanced object // We render multiple instances of a model
vkCmdBindDescriptorSets(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.model, 0, nullptr); vkCmdBindDescriptorSets(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.model, 0, nullptr);
models.model.bindBuffers(offScreenCmdBuffer); models.model.bindBuffers(offScreenCmdBuffer);
vkCmdDrawIndexed(offScreenCmdBuffer, models.model.indices.count, 3, 0, 0, 0); vkCmdDrawIndexed(offScreenCmdBuffer, models.model.indices.count, 3, 0, 0, 0);
@ -474,10 +472,13 @@ public:
VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0); VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0);
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.composition, 0, nullptr);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.composition); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.composition);
// Final composition as full screen quad
// Final composition
// This is done by simply drawing a full screen quad
// The fragment shader then combines the deferred attachments into the final image
// Note: Also used for debug display if debugDisplayTarget > 0 // Note: Also used for debug display if debugDisplayTarget > 0
vkCmdDraw(drawCmdBuffers[i], 3, 1, 0, 0); vkCmdDraw(drawCmdBuffers[i], 3, 1, 0, 0);
@ -489,20 +490,17 @@ public:
} }
} }
void setupDescriptorPool() void setupDescriptors()
{ {
// Pool
std::vector<VkDescriptorPoolSize> poolSizes = { std::vector<VkDescriptorPoolSize> poolSizes = {
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 8), vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 8),
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 9) vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 9)
}; };
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 3); VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 3);
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool)); VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
}
void setupDescriptorSetLayout() // Layouts
{
// Deferred shading layout
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = { std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
// Binding 0 : Vertex shader uniform buffer // Binding 0 : Vertex shader uniform buffer
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0), vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0),
@ -515,17 +513,10 @@ public:
// Binding 4 : Fragment shader uniform buffer // Binding 4 : Fragment shader uniform buffer
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_FRAGMENT_BIT, 4), vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_FRAGMENT_BIT, 4),
}; };
VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings); VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout)); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
// Shared pipeline layout used by all pipelines // Sets
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
}
void setupDescriptorSet()
{
std::vector<VkWriteDescriptorSet> writeDescriptorSets; std::vector<VkWriteDescriptorSet> writeDescriptorSets;
VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1); VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
@ -549,16 +540,16 @@ public:
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
// Deferred composition // Deferred composition
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet)); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.composition));
writeDescriptorSets = { writeDescriptorSets = {
// Binding 1 : Position texture target // Binding 1 : Position texture target
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &texDescriptorPosition), vks::initializers::writeDescriptorSet(descriptorSets.composition, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &texDescriptorPosition),
// Binding 2 : Normals texture target // Binding 2 : Normals texture target
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2, &texDescriptorNormal), vks::initializers::writeDescriptorSet(descriptorSets.composition, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2, &texDescriptorNormal),
// Binding 3 : Albedo texture target // Binding 3 : Albedo texture target
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 3, &texDescriptorAlbedo), vks::initializers::writeDescriptorSet(descriptorSets.composition, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 3, &texDescriptorAlbedo),
// Binding 4 : Fragment shader uniform buffer // Binding 4 : Fragment shader uniform buffer
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 4, &uniformBuffers.composition.descriptor), vks::initializers::writeDescriptorSet(descriptorSets.composition, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 4, &uniformBuffers.composition.descriptor),
}; };
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, nullptr); vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, nullptr);
@ -591,6 +582,11 @@ public:
void preparePipelines() void preparePipelines()
{ {
// Pipeline layout
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
// Pipelines
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE); VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0); VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);
VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE); VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
@ -652,18 +648,10 @@ public:
void prepareUniformBuffers() void prepareUniformBuffers()
{ {
// Offscreen vertex shader // Offscreen vertex shader
VK_CHECK_RESULT(vulkanDevice->createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffers.offscreen, sizeof(uboOffscreenVS)));
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffers.offscreen,
sizeof(uboOffscreenVS)));
// Deferred fragment shader // Deferred fragment shader
VK_CHECK_RESULT(vulkanDevice->createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffers.composition, sizeof(uboComposition)));
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffers.composition,
sizeof(uboComposition)));
// Map persistent // Map persistent
VK_CHECK_RESULT(uniformBuffers.offscreen.map()); VK_CHECK_RESULT(uniformBuffers.offscreen.map());
@ -716,6 +704,8 @@ public:
uboComposition.lights[5].color = glm::vec3(1.0f, 0.7f, 0.3f); uboComposition.lights[5].color = glm::vec3(1.0f, 0.7f, 0.3f);
uboComposition.lights[5].radius = 25.0f; uboComposition.lights[5].radius = 25.0f;
// Animate the lights
if (!paused) {
uboComposition.lights[0].position.x = sin(glm::radians(360.0f * timer)) * 5.0f; uboComposition.lights[0].position.x = sin(glm::radians(360.0f * timer)) * 5.0f;
uboComposition.lights[0].position.z = cos(glm::radians(360.0f * timer)) * 5.0f; uboComposition.lights[0].position.z = cos(glm::radians(360.0f * timer)) * 5.0f;
@ -730,6 +720,7 @@ public:
uboComposition.lights[5].position.x = 0.0f + sin(glm::radians(-360.0f * timer + 135.0f)) * 10.0f; uboComposition.lights[5].position.x = 0.0f + sin(glm::radians(-360.0f * timer + 135.0f)) * 10.0f;
uboComposition.lights[5].position.z = 0.0f - cos(glm::radians(-360.0f * timer - 45.0f)) * 10.0f; uboComposition.lights[5].position.z = 0.0f - cos(glm::radians(-360.0f * timer - 45.0f)) * 10.0f;
}
// Current view position // Current view position
uboComposition.viewPos = glm::vec4(camera.position, 0.0f) * glm::vec4(-1.0f, 1.0f, -1.0f, 1.0f); uboComposition.viewPos = glm::vec4(camera.position, 0.0f) * glm::vec4(-1.0f, 1.0f, -1.0f, 1.0f);
@ -739,6 +730,19 @@ public:
memcpy(uniformBuffers.composition.mapped, &uboComposition, sizeof(uboComposition)); memcpy(uniformBuffers.composition.mapped, &uboComposition, sizeof(uboComposition));
} }
void prepare()
{
VulkanExampleBase::prepare();
loadAssets();
prepareOffscreenFramebuffer();
prepareUniformBuffers();
setupDescriptors();
preparePipelines();
buildCommandBuffers();
buildDeferredCommandBuffer();
prepared = true;
}
void draw() void draw()
{ {
VulkanExampleBase::prepareFrame(); VulkanExampleBase::prepareFrame();
@ -780,38 +784,12 @@ public:
VulkanExampleBase::submitFrame(); VulkanExampleBase::submitFrame();
} }
void prepare()
{
VulkanExampleBase::prepare();
loadAssets();
prepareOffscreenFramebuffer();
prepareUniformBuffers();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
buildCommandBuffers();
buildDeferredCommandBuffer();
prepared = true;
}
virtual void render() virtual void render()
{ {
if (!prepared) if (!prepared)
return; return;
draw(); draw();
if (!paused)
{
updateUniformBufferComposition(); updateUniformBufferComposition();
}
if (camera.updated)
{
updateUniformBufferOffscreen();
}
}
virtual void viewChanged()
{
updateUniformBufferOffscreen(); updateUniformBufferOffscreen();
} }