1584 lines
No EOL
57 KiB
C++
1584 lines
No EOL
57 KiB
C++
/*
|
|
* Vulkan Example - Deferred shading with shadows from multiple light sources using geometry shader instancing
|
|
*
|
|
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
|
|
*
|
|
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include <vector>
|
|
|
|
#define GLM_FORCE_RADIANS
|
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtx/rotate_vector.hpp>
|
|
|
|
#include <vulkan/vulkan.h>
|
|
#include "vulkanexamplebase.h"
|
|
|
|
#define VERTEX_BUFFER_BIND_ID 0
|
|
#define ENABLE_VALIDATION false
|
|
|
|
// Shadowmap properties
|
|
#define SHADOWMAP_DIM 2048
|
|
#define SHADOWMAP_FILTER VK_FILTER_LINEAR
|
|
// 16 bits of depth is enough for such a small scene
|
|
#define SHADOWMAP_FORMAT VK_FORMAT_D32_SFLOAT_S8_UINT
|
|
|
|
#define FB_DIM 2048
|
|
|
|
// Must match the LIGHT_COUNT define in the shadow and deferred shaders
|
|
#define LIGHT_COUNT 3
|
|
|
|
// Vertex layout for this example
|
|
// todo: create class for vertex layout
|
|
std::vector<vkMeshLoader::VertexLayout> vertexLayout =
|
|
{
|
|
vkMeshLoader::VERTEX_LAYOUT_POSITION,
|
|
vkMeshLoader::VERTEX_LAYOUT_UV,
|
|
vkMeshLoader::VERTEX_LAYOUT_COLOR,
|
|
vkMeshLoader::VERTEX_LAYOUT_NORMAL,
|
|
vkMeshLoader::VERTEX_LAYOUT_TANGENT
|
|
};
|
|
|
|
class VulkanExample : public VulkanExampleBase
|
|
{
|
|
public:
|
|
bool debugDisplay = false;
|
|
|
|
// Keep depth range as small as possible
|
|
// for better shadow map precision
|
|
float zNear = 0.1f;
|
|
float zFar = 64.0f;
|
|
float lightFOV = 75.0f;
|
|
|
|
// Depth bias (and slope) are used to avoid shadowing artefacts
|
|
float depthBiasConstant = 1.25f;
|
|
float depthBiasSlope = 1.75f;
|
|
|
|
struct {
|
|
struct {
|
|
vkTools::VulkanTexture colorMap;
|
|
vkTools::VulkanTexture normalMap;
|
|
} model;
|
|
struct {
|
|
vkTools::VulkanTexture colorMap;
|
|
vkTools::VulkanTexture normalMap;
|
|
} background;
|
|
} textures;
|
|
|
|
struct {
|
|
vkMeshLoader::MeshBuffer model;
|
|
vkMeshLoader::MeshBuffer background;
|
|
vkMeshLoader::MeshBuffer quad;
|
|
} meshes;
|
|
|
|
struct {
|
|
VkPipelineVertexInputStateCreateInfo inputState;
|
|
std::vector<VkVertexInputBindingDescription> bindingDescriptions;
|
|
std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
|
|
} vertices;
|
|
|
|
struct {
|
|
glm::mat4 projection;
|
|
glm::mat4 model;
|
|
glm::mat4 view;
|
|
glm::vec4 instancePos[3];
|
|
int layer;
|
|
} uboVS, uboOffscreenVS;
|
|
|
|
// This UBO stores the shadow matrices for all of the light sources
|
|
// The matrices are indexed using geometry shader instancing
|
|
// The instancePos is used to place the models using instanced draws
|
|
struct {
|
|
glm::mat4 mvp[LIGHT_COUNT];
|
|
glm::vec4 instancePos[3];
|
|
} uboShadowGS;
|
|
|
|
struct Light {
|
|
glm::vec4 position;
|
|
glm::vec4 target;
|
|
glm::vec4 color;
|
|
glm::mat4 viewMatrix;
|
|
};
|
|
|
|
struct {
|
|
glm::vec4 viewPos;
|
|
Light lights[LIGHT_COUNT];
|
|
} uboFragmentLights;
|
|
|
|
struct {
|
|
vkTools::UniformData vsFullScreen;
|
|
vkTools::UniformData vsOffscreen;
|
|
vkTools::UniformData fsLights;
|
|
vkTools::UniformData uboShadowGS;
|
|
} uniformData;
|
|
|
|
struct {
|
|
VkPipeline deferred;
|
|
VkPipeline offscreen;
|
|
VkPipeline debug;
|
|
VkPipeline shadowpass;
|
|
} pipelines;
|
|
|
|
struct {
|
|
//todo: rename, shared with deferred and shadow pass
|
|
VkPipelineLayout deferred;
|
|
VkPipelineLayout offscreen;
|
|
} pipelineLayouts;
|
|
|
|
struct {
|
|
VkDescriptorSet model;
|
|
VkDescriptorSet background;
|
|
VkDescriptorSet shadow;
|
|
} descriptorSets;
|
|
|
|
VkDescriptorSet descriptorSet;
|
|
VkDescriptorSetLayout descriptorSetLayout;
|
|
|
|
// todo : move to vktools (or separate unit)
|
|
struct FrameBufferAttachment
|
|
{
|
|
VkImage image;
|
|
VkDeviceMemory mem;
|
|
VkImageView view;
|
|
VkFormat format;
|
|
bool isDepth = false;
|
|
};
|
|
|
|
// todo : move to vktools (or separate unit) and turn into class
|
|
struct FrameBuffer
|
|
{
|
|
uint32_t width, height;
|
|
VkFramebuffer frameBuffer;
|
|
std::vector<FrameBufferAttachment> attachments;
|
|
VkRenderPass renderPass;
|
|
VkSampler sampler;
|
|
void FreeResources(VkDevice device)
|
|
{
|
|
for (auto attachment : attachments)
|
|
{
|
|
vkDestroyImage(device, attachment.image, nullptr);
|
|
vkDestroyImageView(device, attachment.view, nullptr);
|
|
vkFreeMemory(device, attachment.mem, nullptr);
|
|
}
|
|
vkDestroySampler(device, sampler, nullptr);
|
|
vkDestroyRenderPass(device, renderPass, nullptr);
|
|
vkDestroyFramebuffer(device, frameBuffer, nullptr);
|
|
}
|
|
};
|
|
|
|
struct
|
|
{
|
|
// Framebuffer resources for the deferred pass
|
|
FrameBuffer deferred;
|
|
// Framebuffer resources for the shadow pass
|
|
FrameBuffer shadow;
|
|
} frameBuffers;
|
|
|
|
struct {
|
|
VkCommandBuffer deferred = VK_NULL_HANDLE;
|
|
} commandBuffers;
|
|
|
|
// Semaphore used to synchronize between offscreen and final scene rendering
|
|
VkSemaphore offscreenSemaphore = VK_NULL_HANDLE;
|
|
|
|
// Device features to be enabled for this example
|
|
static VkPhysicalDeviceFeatures getEnabledFeatures()
|
|
{
|
|
VkPhysicalDeviceFeatures enabledFeatures = {};
|
|
enabledFeatures.geometryShader = VK_TRUE;
|
|
enabledFeatures.shaderClipDistance = VK_TRUE;
|
|
enabledFeatures.shaderCullDistance = VK_TRUE;
|
|
enabledFeatures.shaderTessellationAndGeometryPointSize = VK_TRUE;
|
|
return enabledFeatures;
|
|
}
|
|
|
|
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION, getEnabledFeatures)
|
|
{
|
|
enableTextOverlay = true;
|
|
title = "Vulkan Example - Deferred shading with shadow mapping";
|
|
camera.type = Camera::CameraType::firstperson;
|
|
camera.movementSpeed = 5.0f;
|
|
camera.rotationSpeed = 0.25f;
|
|
camera.position = { 2.15f, 0.3f, -8.75f };
|
|
camera.setRotation(glm::vec3(-0.75f, 12.5f, 0.0f));
|
|
camera.setPerspective(60.0f, (float)width / (float)height, zNear, zFar);
|
|
}
|
|
|
|
~VulkanExample()
|
|
{
|
|
// Frame buffers
|
|
frameBuffers.shadow.FreeResources(device);
|
|
frameBuffers.deferred.FreeResources(device);
|
|
|
|
vkDestroyPipeline(device, pipelines.deferred, nullptr);
|
|
vkDestroyPipeline(device, pipelines.offscreen, nullptr);
|
|
vkDestroyPipeline(device, pipelines.shadowpass, nullptr);
|
|
vkDestroyPipeline(device, pipelines.debug, nullptr);
|
|
|
|
vkDestroyPipelineLayout(device, pipelineLayouts.deferred, nullptr);
|
|
vkDestroyPipelineLayout(device, pipelineLayouts.offscreen, nullptr);
|
|
|
|
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
|
|
|
// Meshes
|
|
vkMeshLoader::freeMeshBufferResources(device, &meshes.model);
|
|
vkMeshLoader::freeMeshBufferResources(device, &meshes.background);
|
|
vkMeshLoader::freeMeshBufferResources(device, &meshes.quad);
|
|
|
|
// Uniform buffers
|
|
vkTools::destroyUniformData(device, &uniformData.vsOffscreen);
|
|
vkTools::destroyUniformData(device, &uniformData.vsFullScreen);
|
|
vkTools::destroyUniformData(device, &uniformData.fsLights);
|
|
vkTools::destroyUniformData(device, &uniformData.uboShadowGS);
|
|
|
|
vkFreeCommandBuffers(device, cmdPool, 1, &commandBuffers.deferred);
|
|
|
|
// Textures
|
|
textureLoader->destroyTexture(textures.model.colorMap);
|
|
textureLoader->destroyTexture(textures.model.normalMap);
|
|
textureLoader->destroyTexture(textures.background.colorMap);
|
|
textureLoader->destroyTexture(textures.background.normalMap);
|
|
|
|
vkDestroySemaphore(device, offscreenSemaphore, nullptr);
|
|
}
|
|
|
|
// Create a frame buffer attachment
|
|
// todo : move into frame buffer class
|
|
void createAttachment(VkFormat format, VkImageUsageFlagBits usage, FrameBufferAttachment *attachment, VkCommandBuffer layoutCmd, bool depthSample = false)
|
|
{
|
|
VkImageAspectFlags aspectMask = 0;
|
|
VkImageLayout imageLayout;
|
|
|
|
attachment->format = format;
|
|
|
|
if (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
|
|
{
|
|
aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
|
}
|
|
if (usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)
|
|
{
|
|
aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
|
|
imageLayout = depthSample ? VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
attachment->isDepth = true;
|
|
}
|
|
|
|
assert(aspectMask > 0);
|
|
|
|
VkImageCreateInfo image = vkTools::initializers::imageCreateInfo();
|
|
image.imageType = VK_IMAGE_TYPE_2D;
|
|
image.format = format;
|
|
image.extent.width = frameBuffers.deferred.width;
|
|
image.extent.height = frameBuffers.deferred.height;
|
|
image.extent.depth = 1;
|
|
image.mipLevels = 1;
|
|
image.arrayLayers = 1;
|
|
image.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
image.tiling = VK_IMAGE_TILING_OPTIMAL;
|
|
image.usage = usage | VK_IMAGE_USAGE_SAMPLED_BIT;
|
|
|
|
VkMemoryAllocateInfo memAlloc = vkTools::initializers::memoryAllocateInfo();
|
|
VkMemoryRequirements memReqs;
|
|
|
|
VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &attachment->image));
|
|
vkGetImageMemoryRequirements(device, attachment->image, &memReqs);
|
|
memAlloc.allocationSize = memReqs.size;
|
|
memAlloc.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &attachment->mem));
|
|
VK_CHECK_RESULT(vkBindImageMemory(device, attachment->image, attachment->mem, 0));
|
|
|
|
if (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
|
|
{
|
|
// Set the initial layout to shader read instead of attachment
|
|
// This is done as the render loop does the actualy image layout transitions
|
|
vkTools::setImageLayout(
|
|
layoutCmd,
|
|
attachment->image,
|
|
aspectMask,
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
|
}
|
|
else
|
|
{
|
|
vkTools::setImageLayout(
|
|
layoutCmd,
|
|
attachment->image,
|
|
aspectMask,
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
|
imageLayout);
|
|
}
|
|
|
|
VkImageViewCreateInfo imageView = vkTools::initializers::imageViewCreateInfo();
|
|
imageView.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
imageView.format = format;
|
|
imageView.subresourceRange = {};
|
|
imageView.subresourceRange.aspectMask = aspectMask;
|
|
imageView.subresourceRange.baseMipLevel = 0;
|
|
imageView.subresourceRange.levelCount = 1;
|
|
imageView.subresourceRange.baseArrayLayer = 0;
|
|
imageView.subresourceRange.layerCount = 1;
|
|
imageView.image = attachment->image;
|
|
VK_CHECK_RESULT(vkCreateImageView(device, &imageView, nullptr, &attachment->view));
|
|
}
|
|
|
|
// Create a layered attachment
|
|
// todo: not used yet, move into framebuffer class
|
|
void createLayeredAttachment(VkFormat format, VkImageUsageFlagBits usage, FrameBufferAttachment *attachment, uint32_t layerCount, VkCommandBuffer layoutCmd, bool depthSample = false)
|
|
{
|
|
VkImageAspectFlags aspectMask = 0;
|
|
VkImageLayout imageLayout;
|
|
|
|
attachment->format = format;
|
|
|
|
if (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
|
|
{
|
|
aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
|
}
|
|
if (usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)
|
|
{
|
|
aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
|
|
imageLayout = depthSample ? VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
}
|
|
|
|
assert(aspectMask > 0);
|
|
|
|
VkImageCreateInfo image = vkTools::initializers::imageCreateInfo();
|
|
image.imageType = VK_IMAGE_TYPE_2D;
|
|
image.format = format;
|
|
image.extent.width = frameBuffers.deferred.width;
|
|
image.extent.height = frameBuffers.deferred.height;
|
|
image.extent.depth = 1;
|
|
image.mipLevels = 1;
|
|
image.arrayLayers = layerCount;
|
|
image.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
image.tiling = VK_IMAGE_TILING_OPTIMAL;
|
|
image.usage = usage | VK_IMAGE_USAGE_SAMPLED_BIT;
|
|
|
|
VkMemoryAllocateInfo memAlloc = vkTools::initializers::memoryAllocateInfo();
|
|
VkMemoryRequirements memReqs;
|
|
|
|
VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &attachment->image));
|
|
vkGetImageMemoryRequirements(device, attachment->image, &memReqs);
|
|
memAlloc.allocationSize = memReqs.size;
|
|
memAlloc.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &attachment->mem));
|
|
VK_CHECK_RESULT(vkBindImageMemory(device, attachment->image, attachment->mem, 0));
|
|
|
|
VkImageSubresourceRange subresourceRange = {};
|
|
subresourceRange.aspectMask = aspectMask;
|
|
subresourceRange.baseMipLevel = 0;
|
|
subresourceRange.levelCount = 1;
|
|
subresourceRange.layerCount = layerCount;
|
|
|
|
if (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
|
|
{
|
|
// Set the initial layout to shader read instead of attachment
|
|
// This is done as the render loop does the actualy image layout transitions
|
|
vkTools::setImageLayout(
|
|
layoutCmd,
|
|
attachment->image,
|
|
aspectMask,
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
subresourceRange);
|
|
}
|
|
else
|
|
{
|
|
vkTools::setImageLayout(
|
|
layoutCmd,
|
|
attachment->image,
|
|
aspectMask,
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
|
imageLayout,
|
|
subresourceRange);
|
|
}
|
|
|
|
VkImageViewCreateInfo imageView = vkTools::initializers::imageViewCreateInfo();
|
|
imageView.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
|
|
imageView.format = format;
|
|
imageView.subresourceRange = subresourceRange;
|
|
imageView.image = attachment->image;
|
|
VK_CHECK_RESULT(vkCreateImageView(device, &imageView, nullptr, &attachment->view));
|
|
}
|
|
|
|
|
|
// Prepare a layered shadow map with each layer containing depth from a light's point of view
|
|
// The shadow mapping pass uses geometry shader instancing to output the scene from the different
|
|
// light sources' point of view to the layers of the depth attachment in one single pass
|
|
void shadowSetup()
|
|
{
|
|
VkCommandBuffer layoutCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
|
|
|
frameBuffers.shadow.width = SHADOWMAP_DIM;
|
|
frameBuffers.shadow.height = SHADOWMAP_DIM;
|
|
// One layered (depth) attachment
|
|
frameBuffers.shadow.attachments.resize(1);
|
|
|
|
// Color attachment
|
|
VkImageCreateInfo image = vkTools::initializers::imageCreateInfo();
|
|
image.imageType = VK_IMAGE_TYPE_2D;
|
|
image.format = SHADOWMAP_FORMAT;
|
|
image.extent.width = frameBuffers.shadow.width;
|
|
image.extent.height = frameBuffers.shadow.height;
|
|
image.extent.depth = 1;
|
|
image.mipLevels = 1;
|
|
// Use a layererd attachment with one layer per light
|
|
image.arrayLayers = LIGHT_COUNT;
|
|
image.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
image.tiling = VK_IMAGE_TILING_OPTIMAL;
|
|
// Sample directly from the depth attachment for the shadow mapping
|
|
image.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
|
|
|
VkImageViewCreateInfo depthStencilView = vkTools::initializers::imageViewCreateInfo();
|
|
depthStencilView.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
|
|
depthStencilView.format = SHADOWMAP_FORMAT;
|
|
depthStencilView.subresourceRange = {};
|
|
depthStencilView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
|
|
depthStencilView.subresourceRange.baseMipLevel = 0;
|
|
depthStencilView.subresourceRange.levelCount = 1;
|
|
depthStencilView.subresourceRange.baseArrayLayer = 0;
|
|
depthStencilView.subresourceRange.layerCount = LIGHT_COUNT;
|
|
VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &frameBuffers.shadow.attachments[0].image));
|
|
|
|
VkMemoryAllocateInfo memAlloc = vkTools::initializers::memoryAllocateInfo();
|
|
VkMemoryRequirements memReqs;
|
|
vkGetImageMemoryRequirements(device, frameBuffers.shadow.attachments[0].image, &memReqs);
|
|
memAlloc.allocationSize = memReqs.size;
|
|
memAlloc.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &frameBuffers.shadow.attachments[0].mem));
|
|
VK_CHECK_RESULT(vkBindImageMemory(device, frameBuffers.shadow.attachments[0].image, frameBuffers.shadow.attachments[0].mem, 0));
|
|
|
|
// Set the initial layout to shader read instead of attachment
|
|
// This is done as the render loop does the actualy image layout transitions
|
|
|
|
VkImageSubresourceRange subresourceRange = {};
|
|
subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
|
|
subresourceRange.baseMipLevel = 0;
|
|
subresourceRange.levelCount = 1;
|
|
subresourceRange.layerCount = LIGHT_COUNT;
|
|
|
|
vkTools::setImageLayout(
|
|
layoutCmd,
|
|
frameBuffers.shadow.attachments[0].image,
|
|
VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT,
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
subresourceRange);
|
|
|
|
VulkanExampleBase::flushCommandBuffer(layoutCmd, queue, true);
|
|
|
|
depthStencilView.image = frameBuffers.shadow.attachments[0].image;
|
|
VK_CHECK_RESULT(vkCreateImageView(device, &depthStencilView, nullptr, &frameBuffers.shadow.attachments[0].view));
|
|
|
|
// Create sampler to sample from to depth attachment
|
|
// Used to sample in the fragment shader for shadowed rendering
|
|
VkSamplerCreateInfo sampler = vkTools::initializers::samplerCreateInfo();
|
|
sampler.magFilter = SHADOWMAP_FILTER;
|
|
sampler.minFilter = SHADOWMAP_FILTER;
|
|
sampler.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
|
sampler.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
|
sampler.addressModeV = sampler.addressModeU;
|
|
sampler.addressModeW = sampler.addressModeU;
|
|
sampler.mipLodBias = 0.0f;
|
|
sampler.maxAnisotropy = 0;
|
|
sampler.minLod = 0.0f;
|
|
sampler.maxLod = 1.0f;
|
|
sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
|
|
VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &frameBuffers.shadow.sampler));
|
|
|
|
VkAttachmentDescription attachmentDescription = {};
|
|
attachmentDescription.format = SHADOWMAP_FORMAT;
|
|
attachmentDescription.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
attachmentDescription.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
|
attachmentDescription.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
|
attachmentDescription.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
attachmentDescription.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
attachmentDescription.initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
attachmentDescription.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
|
|
VkAttachmentReference attachmentReference = {};
|
|
attachmentReference.attachment = 0;
|
|
attachmentReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
|
|
VkSubpassDescription subpass = {};
|
|
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
|
subpass.colorAttachmentCount = 0;
|
|
subpass.pColorAttachments = nullptr;
|
|
subpass.pDepthStencilAttachment = &attachmentReference;
|
|
|
|
VkRenderPassCreateInfo renderPassCreateInfo = vkTools::initializers::renderPassCreateInfo();
|
|
renderPassCreateInfo.attachmentCount = 1;
|
|
renderPassCreateInfo.pAttachments = &attachmentDescription;
|
|
renderPassCreateInfo.subpassCount = 1;
|
|
renderPassCreateInfo.pSubpasses = &subpass;
|
|
|
|
VK_CHECK_RESULT(vkCreateRenderPass(device, &renderPassCreateInfo, nullptr, &frameBuffers.shadow.renderPass));
|
|
|
|
// Create frame buffer
|
|
VkFramebufferCreateInfo fbufCreateInfo = vkTools::initializers::framebufferCreateInfo();
|
|
fbufCreateInfo.renderPass = frameBuffers.shadow.renderPass;
|
|
// Only one (layered depth) attachment
|
|
fbufCreateInfo.attachmentCount = 1;
|
|
fbufCreateInfo.pAttachments = &frameBuffers.shadow.attachments[0].view;
|
|
fbufCreateInfo.width = frameBuffers.shadow.width;
|
|
fbufCreateInfo.height = frameBuffers.shadow.height;
|
|
fbufCreateInfo.layers = LIGHT_COUNT;
|
|
VK_CHECK_RESULT(vkCreateFramebuffer(device, &fbufCreateInfo, nullptr, &frameBuffers.shadow.frameBuffer));
|
|
}
|
|
|
|
// Prepare the framebuffer for offscreen rendering with multiple attachments used as render targets inside the fragment shaders
|
|
void deferredSetup()
|
|
{
|
|
VkCommandBuffer layoutCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
|
|
|
frameBuffers.deferred.width = FB_DIM;
|
|
frameBuffers.deferred.height = FB_DIM;
|
|
|
|
// Four attachments (3 color, 1 depth)
|
|
frameBuffers.deferred.attachments.resize(4);
|
|
|
|
// Color attachments
|
|
// Attachment 0: (World space) Positions
|
|
createAttachment(
|
|
VK_FORMAT_R16G16B16A16_SFLOAT,
|
|
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
|
&frameBuffers.deferred.attachments[0],
|
|
layoutCmd);
|
|
|
|
// Attachment 1: (World space) Normals
|
|
createAttachment(
|
|
VK_FORMAT_R16G16B16A16_SFLOAT,
|
|
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
|
&frameBuffers.deferred.attachments[1],
|
|
layoutCmd);
|
|
|
|
// Attachment 1: Albedo (color)
|
|
createAttachment(
|
|
VK_FORMAT_R8G8B8A8_UNORM,
|
|
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
|
&frameBuffers.deferred.attachments[2],
|
|
layoutCmd);
|
|
|
|
// Depth attachment
|
|
// Find a suitable depth format
|
|
VkFormat attDepthFormat;
|
|
VkBool32 validDepthFormat = vkTools::getSupportedDepthFormat(physicalDevice, &attDepthFormat);
|
|
assert(validDepthFormat);
|
|
|
|
createAttachment(
|
|
attDepthFormat,
|
|
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
|
|
&frameBuffers.deferred.attachments[3],
|
|
layoutCmd);
|
|
|
|
VulkanExampleBase::flushCommandBuffer(layoutCmd, queue, true);
|
|
|
|
// Set up separate renderpass with references
|
|
// to the color and depth attachments
|
|
|
|
std::array<VkAttachmentDescription, 4> attachmentDescs = {};
|
|
|
|
// Init attachment properties
|
|
for (uint32_t i = 0; i < 4; ++i)
|
|
{
|
|
attachmentDescs[i].samples = VK_SAMPLE_COUNT_1_BIT;
|
|
attachmentDescs[i].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
|
attachmentDescs[i].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
|
attachmentDescs[i].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
attachmentDescs[i].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
attachmentDescs[i].format = frameBuffers.deferred.attachments[i].format;
|
|
if (i == 3)
|
|
{
|
|
attachmentDescs[i].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
attachmentDescs[i].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
}
|
|
else
|
|
{
|
|
attachmentDescs[i].initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
|
attachmentDescs[i].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
|
}
|
|
}
|
|
|
|
std::vector<VkAttachmentReference> colorReferences;
|
|
colorReferences.push_back({ 0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL });
|
|
colorReferences.push_back({ 1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL });
|
|
colorReferences.push_back({ 2, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL });
|
|
|
|
VkAttachmentReference depthReference = {};
|
|
depthReference.attachment = 3;
|
|
depthReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
|
|
VkSubpassDescription subpass = {};
|
|
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
|
subpass.pColorAttachments = colorReferences.data();
|
|
subpass.colorAttachmentCount = static_cast<uint32_t>(colorReferences.size());
|
|
subpass.pDepthStencilAttachment = &depthReference;
|
|
|
|
VkRenderPassCreateInfo renderPassInfo = {};
|
|
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
|
renderPassInfo.pAttachments = attachmentDescs.data();
|
|
renderPassInfo.attachmentCount = static_cast<uint32_t>(attachmentDescs.size());
|
|
renderPassInfo.subpassCount = 1;
|
|
renderPassInfo.pSubpasses = &subpass;
|
|
VK_CHECK_RESULT(vkCreateRenderPass(device, &renderPassInfo, nullptr, &frameBuffers.deferred.renderPass));
|
|
|
|
std::vector<VkImageView> attachments;
|
|
for (auto attachment : frameBuffers.deferred.attachments)
|
|
{
|
|
attachments.push_back(attachment.view);
|
|
}
|
|
|
|
VkFramebufferCreateInfo fbufCreateInfo = {};
|
|
fbufCreateInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
|
fbufCreateInfo.pNext = NULL;
|
|
fbufCreateInfo.renderPass = frameBuffers.deferred.renderPass;
|
|
fbufCreateInfo.pAttachments = attachments.data();
|
|
fbufCreateInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
|
|
fbufCreateInfo.width = frameBuffers.deferred.width;
|
|
fbufCreateInfo.height = frameBuffers.deferred.height;
|
|
fbufCreateInfo.layers = LIGHT_COUNT;
|
|
|
|
VK_CHECK_RESULT(vkCreateFramebuffer(device, &fbufCreateInfo, nullptr, &frameBuffers.deferred.frameBuffer));
|
|
// Create sampler to sample from the color attachments
|
|
VkSamplerCreateInfo sampler = vkTools::initializers::samplerCreateInfo();
|
|
sampler.magFilter = VK_FILTER_LINEAR;
|
|
sampler.minFilter = VK_FILTER_LINEAR;
|
|
sampler.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
|
sampler.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
|
sampler.addressModeV = sampler.addressModeU;
|
|
sampler.addressModeW = sampler.addressModeU;
|
|
sampler.mipLodBias = 0.0f;
|
|
sampler.maxAnisotropy = 0;
|
|
sampler.minLod = 0.0f;
|
|
sampler.maxLod = 1.0f;
|
|
sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
|
|
VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &frameBuffers.deferred.sampler));
|
|
}
|
|
|
|
// Put render commands for the scene into the given command buffer
|
|
void renderScene(VkCommandBuffer cmdBuffer, bool shadow)
|
|
{
|
|
VkDeviceSize offsets[1] = { 0 };
|
|
|
|
// Background
|
|
vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.offscreen, 0, 1, shadow ? &descriptorSets.shadow : &descriptorSets.background, 0, NULL);
|
|
vkCmdBindVertexBuffers(cmdBuffer, VERTEX_BUFFER_BIND_ID, 1, &meshes.background.vertices.buf, offsets);
|
|
vkCmdBindIndexBuffer(cmdBuffer, meshes.background.indices.buf, 0, VK_INDEX_TYPE_UINT32);
|
|
vkCmdDrawIndexed(cmdBuffer, meshes.background.indexCount, 1, 0, 0, 0);
|
|
|
|
// Objects
|
|
vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.offscreen, 0, 1, shadow ? &descriptorSets.shadow : &descriptorSets.model, 0, NULL);
|
|
vkCmdBindVertexBuffers(cmdBuffer, VERTEX_BUFFER_BIND_ID, 1, &meshes.model.vertices.buf, offsets);
|
|
vkCmdBindIndexBuffer(cmdBuffer, meshes.model.indices.buf, 0, VK_INDEX_TYPE_UINT32);
|
|
vkCmdDrawIndexed(cmdBuffer, meshes.model.indexCount, 3, 0, 0, 0);
|
|
}
|
|
|
|
// Build a secondary command buffer for rendering the scene values to the offscreen frame buffer attachments
|
|
void buildDeferredCommandBuffer()
|
|
{
|
|
if (commandBuffers.deferred == VK_NULL_HANDLE)
|
|
{
|
|
commandBuffers.deferred = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, false);
|
|
}
|
|
|
|
// Create a semaphore used to synchronize offscreen rendering and usage
|
|
VkSemaphoreCreateInfo semaphoreCreateInfo = vkTools::initializers::semaphoreCreateInfo();
|
|
VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &offscreenSemaphore));
|
|
|
|
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
|
|
|
|
VkRenderPassBeginInfo renderPassBeginInfo = vkTools::initializers::renderPassBeginInfo();
|
|
std::array<VkClearValue, 4> clearValues = {};
|
|
VkViewport viewport;
|
|
VkRect2D scissor;
|
|
|
|
// Shadow map generation pass first
|
|
|
|
clearValues[0].depthStencil = { 1.0f, 0 };
|
|
|
|
renderPassBeginInfo.renderPass = frameBuffers.shadow.renderPass;
|
|
renderPassBeginInfo.framebuffer = frameBuffers.shadow.frameBuffer;
|
|
renderPassBeginInfo.renderArea.extent.width = frameBuffers.shadow.width;
|
|
renderPassBeginInfo.renderArea.extent.height = frameBuffers.shadow.height;
|
|
renderPassBeginInfo.clearValueCount = 1;
|
|
renderPassBeginInfo.pClearValues = clearValues.data();
|
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(commandBuffers.deferred, &cmdBufInfo));
|
|
|
|
// Change back layout of the depth attachment after sampling in the fragment shader
|
|
// todo: replace with subpass dependency
|
|
VkImageSubresourceRange subresourceRange = {};
|
|
subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
|
|
subresourceRange.baseMipLevel = 0;
|
|
subresourceRange.levelCount = 1;
|
|
subresourceRange.layerCount = LIGHT_COUNT;
|
|
|
|
vkTools::setImageLayout(
|
|
commandBuffers.deferred,
|
|
frameBuffers.shadow.attachments[0].image,
|
|
VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT,
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
|
subresourceRange);
|
|
|
|
viewport = vkTools::initializers::viewport((float)frameBuffers.shadow.width, (float)frameBuffers.shadow.height, 0.0f, 1.0f);
|
|
vkCmdSetViewport(commandBuffers.deferred, 0, 1, &viewport);
|
|
|
|
scissor = vkTools::initializers::rect2D(frameBuffers.shadow.width, frameBuffers.shadow.height, 0, 0);
|
|
vkCmdSetScissor(commandBuffers.deferred, 0, 1, &scissor);
|
|
|
|
// Set depth bias (aka "Polygon offset")
|
|
vkCmdSetDepthBias(
|
|
commandBuffers.deferred,
|
|
depthBiasConstant,
|
|
0.0f,
|
|
depthBiasSlope);
|
|
|
|
vkCmdBeginRenderPass(commandBuffers.deferred, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
vkCmdBindPipeline(commandBuffers.deferred, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.shadowpass);
|
|
renderScene(commandBuffers.deferred, true);
|
|
vkCmdEndRenderPass(commandBuffers.deferred);
|
|
|
|
// Change layout of the depth attachment for sampling in the fragment shader
|
|
// todo: replace with subpass dependency
|
|
vkTools::setImageLayout(
|
|
commandBuffers.deferred,
|
|
frameBuffers.shadow.attachments[0].image,
|
|
VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT,
|
|
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
subresourceRange);
|
|
|
|
// Deferred pass second
|
|
// -------------------------------------------------------------------------------------------------------
|
|
|
|
// Change back layout of the color attachments after sampling in the fragment shader
|
|
// todo: replace with subpass dependency
|
|
for (auto attachment : frameBuffers.deferred.attachments)
|
|
{
|
|
if (!attachment.isDepth)
|
|
{
|
|
vkTools::setImageLayout(
|
|
commandBuffers.deferred,
|
|
attachment.image,
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
|
}
|
|
}
|
|
|
|
// Clear values for all attachments written in the fragment sahder
|
|
clearValues[0].color = { { 0.0f, 0.0f, 0.0f, 0.0f } };
|
|
clearValues[1].color = { { 0.0f, 0.0f, 0.0f, 0.0f } };
|
|
clearValues[2].color = { { 0.0f, 0.0f, 0.0f, 0.0f } };
|
|
clearValues[3].depthStencil = { 1.0f, 0 };
|
|
|
|
renderPassBeginInfo.renderPass = frameBuffers.deferred.renderPass;
|
|
renderPassBeginInfo.framebuffer = frameBuffers.deferred.frameBuffer;
|
|
renderPassBeginInfo.renderArea.extent.width = frameBuffers.deferred.width;
|
|
renderPassBeginInfo.renderArea.extent.height = frameBuffers.deferred.height;
|
|
renderPassBeginInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
|
|
renderPassBeginInfo.pClearValues = clearValues.data();
|
|
|
|
vkCmdBeginRenderPass(commandBuffers.deferred, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
viewport = vkTools::initializers::viewport((float)frameBuffers.deferred.width, (float)frameBuffers.deferred.height, 0.0f, 1.0f);
|
|
vkCmdSetViewport(commandBuffers.deferred, 0, 1, &viewport);
|
|
|
|
scissor = vkTools::initializers::rect2D(frameBuffers.deferred.width, frameBuffers.deferred.height, 0, 0);
|
|
vkCmdSetScissor(commandBuffers.deferred, 0, 1, &scissor);
|
|
|
|
vkCmdBindPipeline(commandBuffers.deferred, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.offscreen);
|
|
renderScene(commandBuffers.deferred, false);
|
|
vkCmdEndRenderPass(commandBuffers.deferred);
|
|
|
|
// Change back layout of the color attachments after sampling in the fragment shader
|
|
// todo: replace with subpass dependency
|
|
for (auto attachment : frameBuffers.deferred.attachments)
|
|
{
|
|
if (!attachment.isDepth)
|
|
{
|
|
vkTools::setImageLayout(
|
|
commandBuffers.deferred,
|
|
attachment.image,
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
|
}
|
|
}
|
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(commandBuffers.deferred));
|
|
}
|
|
|
|
void loadTextures()
|
|
{
|
|
textureLoader->loadTexture(getAssetPath() + "models/armor/colormap.ktx", VK_FORMAT_BC3_UNORM_BLOCK, &textures.model.colorMap);
|
|
textureLoader->loadTexture(getAssetPath() + "models/armor/normalmap.ktx", VK_FORMAT_BC3_UNORM_BLOCK, &textures.model.normalMap);
|
|
textureLoader->loadTexture(getAssetPath() + "textures/pattern57_bc3.ktx", VK_FORMAT_BC3_UNORM_BLOCK, &textures.background.colorMap);
|
|
textureLoader->loadTexture(getAssetPath() + "textures/pattern57_normal_bc3.ktx", VK_FORMAT_BC3_UNORM_BLOCK, &textures.background.normalMap);
|
|
}
|
|
|
|
void reBuildCommandBuffers()
|
|
{
|
|
if (!checkCommandBuffers())
|
|
{
|
|
destroyCommandBuffers();
|
|
createCommandBuffers();
|
|
}
|
|
buildCommandBuffers();
|
|
}
|
|
|
|
void buildCommandBuffers()
|
|
{
|
|
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
|
|
|
|
VkClearValue clearValues[2];
|
|
clearValues[0].color = { { 0.0f, 0.0f, 0.2f, 0.0f } };
|
|
clearValues[1].depthStencil = { 1.0f, 0 };
|
|
|
|
VkRenderPassBeginInfo renderPassBeginInfo = vkTools::initializers::renderPassBeginInfo();
|
|
renderPassBeginInfo.renderPass = renderPass;
|
|
renderPassBeginInfo.renderArea.offset.x = 0;
|
|
renderPassBeginInfo.renderArea.offset.y = 0;
|
|
renderPassBeginInfo.renderArea.extent.width = width;
|
|
renderPassBeginInfo.renderArea.extent.height = height;
|
|
renderPassBeginInfo.clearValueCount = 2;
|
|
renderPassBeginInfo.pClearValues = clearValues;
|
|
|
|
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
|
|
{
|
|
// Set target frame buffer
|
|
renderPassBeginInfo.framebuffer = VulkanExampleBase::frameBuffers[i];
|
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
|
|
|
|
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
VkViewport viewport = vkTools::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
|
|
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
|
|
|
|
VkRect2D scissor = vkTools::initializers::rect2D(width, height, 0, 0);
|
|
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
|
|
|
|
VkDeviceSize offsets[1] = { 0 };
|
|
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.deferred, 0, 1, &descriptorSet, 0, NULL);
|
|
|
|
if (debugDisplay)
|
|
{
|
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.debug);
|
|
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.quad.vertices.buf, offsets);
|
|
vkCmdBindIndexBuffer(drawCmdBuffers[i], meshes.quad.indices.buf, 0, VK_INDEX_TYPE_UINT32);
|
|
vkCmdDrawIndexed(drawCmdBuffers[i], meshes.quad.indexCount, 1, 0, 0, 1);
|
|
// Move viewport to display final composition in lower right corner
|
|
viewport.x = viewport.width * 0.5f;
|
|
viewport.y = viewport.height * 0.5f;
|
|
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
|
|
}
|
|
|
|
// Final composition as full screen quad
|
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.deferred);
|
|
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.quad.vertices.buf, offsets);
|
|
vkCmdBindIndexBuffer(drawCmdBuffers[i], meshes.quad.indices.buf, 0, VK_INDEX_TYPE_UINT32);
|
|
vkCmdDrawIndexed(drawCmdBuffers[i], 6, 1, 0, 0, 1);
|
|
|
|
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
|
}
|
|
}
|
|
|
|
void loadMeshes()
|
|
{
|
|
loadMesh(getAssetPath() + "models/armor/armor.dae", &meshes.model, vertexLayout, 1.0f);
|
|
|
|
vkMeshLoader::MeshCreateInfo meshCreateInfo;
|
|
meshCreateInfo.scale = glm::vec3(15.0f);
|
|
meshCreateInfo.uvscale = glm::vec2(2.0f);
|
|
meshCreateInfo.center = glm::vec3(0.0f, 2.3f, 0.0f);
|
|
loadMesh(getAssetPath() + "models/openbox.dae", &meshes.background, vertexLayout, &meshCreateInfo);
|
|
}
|
|
|
|
void generateQuads()
|
|
{
|
|
// Setup vertices for multiple screen aligned quads
|
|
// Used for displaying final result and debug
|
|
struct Vertex {
|
|
float pos[3];
|
|
float uv[2];
|
|
float col[3];
|
|
float normal[3];
|
|
float tangent[3];
|
|
};
|
|
|
|
std::vector<Vertex> vertexBuffer;
|
|
|
|
float x = 0.0f;
|
|
float y = 0.0f;
|
|
for (uint32_t i = 0; i < 3; i++)
|
|
{
|
|
// Last component of normal is used for debug display sampler index
|
|
vertexBuffer.push_back({ { x + 1.0f, y + 1.0f, 0.0f },{ 1.0f, 1.0f },{ 1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f, (float)i } });
|
|
vertexBuffer.push_back({ { x, y + 1.0f, 0.0f },{ 0.0f, 1.0f },{ 1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f, (float)i } });
|
|
vertexBuffer.push_back({ { x, y, 0.0f },{ 0.0f, 0.0f },{ 1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f, (float)i } });
|
|
vertexBuffer.push_back({ { x + 1.0f, y, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f, (float)i } });
|
|
x += 1.0f;
|
|
if (x > 1.0f)
|
|
{
|
|
x = 0.0f;
|
|
y += 1.0f;
|
|
}
|
|
}
|
|
|
|
createBuffer(
|
|
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
|
vertexBuffer.size() * sizeof(Vertex),
|
|
vertexBuffer.data(),
|
|
&meshes.quad.vertices.buf,
|
|
&meshes.quad.vertices.mem);
|
|
|
|
// Setup indices
|
|
std::vector<uint32_t> indexBuffer = { 0,1,2, 2,3,0 };
|
|
for (uint32_t i = 0; i < 3; ++i)
|
|
{
|
|
uint32_t indices[6] = { 0,1,2, 2,3,0 };
|
|
for (auto index : indices)
|
|
{
|
|
indexBuffer.push_back(i * 4 + index);
|
|
}
|
|
}
|
|
meshes.quad.indexCount = static_cast<uint32_t>(indexBuffer.size());
|
|
|
|
createBuffer(
|
|
VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
|
indexBuffer.size() * sizeof(uint32_t),
|
|
indexBuffer.data(),
|
|
&meshes.quad.indices.buf,
|
|
&meshes.quad.indices.mem);
|
|
}
|
|
|
|
void setupVertexDescriptions()
|
|
{
|
|
// Binding description
|
|
vertices.bindingDescriptions.resize(1);
|
|
vertices.bindingDescriptions[0] =
|
|
vkTools::initializers::vertexInputBindingDescription(
|
|
VERTEX_BUFFER_BIND_ID,
|
|
vkMeshLoader::vertexSize(vertexLayout),
|
|
VK_VERTEX_INPUT_RATE_VERTEX);
|
|
|
|
// Attribute descriptions
|
|
vertices.attributeDescriptions.clear();
|
|
vkMeshLoader::getVertexInputAttributeDescriptions(
|
|
vertexLayout,
|
|
vertices.attributeDescriptions,
|
|
VERTEX_BUFFER_BIND_ID);
|
|
|
|
vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo();
|
|
vertices.inputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertices.bindingDescriptions.size());
|
|
vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data();
|
|
vertices.inputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertices.attributeDescriptions.size());
|
|
vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data();
|
|
}
|
|
|
|
void setupDescriptorPool()
|
|
{
|
|
std::vector<VkDescriptorPoolSize> poolSizes =
|
|
{
|
|
vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 12), //todo: separate set layouts
|
|
vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 16)
|
|
};
|
|
|
|
VkDescriptorPoolCreateInfo descriptorPoolInfo =
|
|
vkTools::initializers::descriptorPoolCreateInfo(
|
|
static_cast<uint32_t>(poolSizes.size()),
|
|
poolSizes.data(),
|
|
4);
|
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
|
}
|
|
|
|
void setupDescriptorSetLayout()
|
|
{
|
|
// todo: split for clarity, esp. with GS instancing
|
|
// Deferred shading layout (Shared with debug display)
|
|
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings =
|
|
{
|
|
// Binding 0: Vertex shader uniform buffer
|
|
vkTools::initializers::descriptorSetLayoutBinding(
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT,
|
|
0),
|
|
// Binding 1: Position texture
|
|
vkTools::initializers::descriptorSetLayoutBinding(
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
1),
|
|
// Binding 2: Normals texture
|
|
vkTools::initializers::descriptorSetLayoutBinding(
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
2),
|
|
// Binding 3: Albedo texture
|
|
vkTools::initializers::descriptorSetLayoutBinding(
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
3),
|
|
// Binding 4: Fragment shader uniform buffer
|
|
vkTools::initializers::descriptorSetLayoutBinding(
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
4),
|
|
// Binding 5: Shadow map
|
|
vkTools::initializers::descriptorSetLayoutBinding(
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
5),
|
|
};
|
|
|
|
VkDescriptorSetLayoutCreateInfo descriptorLayout =
|
|
vkTools::initializers::descriptorSetLayoutCreateInfo(
|
|
setLayoutBindings.data(),
|
|
static_cast<uint32_t>(setLayoutBindings.size()));
|
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
|
|
|
|
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
|
vkTools::initializers::pipelineLayoutCreateInfo(
|
|
&descriptorSetLayout,
|
|
1);
|
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.deferred));
|
|
|
|
// Offscreen (scene) rendering pipeline layout
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.offscreen));
|
|
}
|
|
|
|
void setupDescriptorSet()
|
|
{
|
|
std::vector<VkWriteDescriptorSet> writeDescriptorSets;
|
|
|
|
// Textured quad descriptor set
|
|
VkDescriptorSetAllocateInfo allocInfo =
|
|
vkTools::initializers::descriptorSetAllocateInfo(
|
|
descriptorPool,
|
|
&descriptorSetLayout,
|
|
1);
|
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
|
|
|
|
// Image descriptors for the offscreen color attachments
|
|
VkDescriptorImageInfo texDescriptorPosition =
|
|
vkTools::initializers::descriptorImageInfo(
|
|
frameBuffers.deferred.sampler,
|
|
frameBuffers.deferred.attachments[0].view,
|
|
VK_IMAGE_LAYOUT_GENERAL);
|
|
|
|
VkDescriptorImageInfo texDescriptorNormal =
|
|
vkTools::initializers::descriptorImageInfo(
|
|
frameBuffers.deferred.sampler,
|
|
frameBuffers.deferred.attachments[1].view,
|
|
VK_IMAGE_LAYOUT_GENERAL);
|
|
|
|
VkDescriptorImageInfo texDescriptorAlbedo =
|
|
vkTools::initializers::descriptorImageInfo(
|
|
frameBuffers.deferred.sampler,
|
|
frameBuffers.deferred.attachments[2].view,
|
|
VK_IMAGE_LAYOUT_GENERAL);
|
|
|
|
VkDescriptorImageInfo texDescriptorShadowMap =
|
|
vkTools::initializers::descriptorImageInfo(
|
|
frameBuffers.shadow.sampler,
|
|
frameBuffers.shadow.attachments[0].view,
|
|
VK_IMAGE_LAYOUT_GENERAL);
|
|
|
|
writeDescriptorSets = {
|
|
// Binding 0: Vertex shader uniform buffer
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSet,
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
0,
|
|
&uniformData.vsFullScreen.descriptor),
|
|
// Binding 1: World space position texture
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSet,
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
1,
|
|
&texDescriptorPosition),
|
|
// Binding 2: World space normals texture
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSet,
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
2,
|
|
&texDescriptorNormal),
|
|
// Binding 3: Albedo texture
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSet,
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
3,
|
|
&texDescriptorAlbedo),
|
|
// Binding 4: Fragment shader uniform buffer
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSet,
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
4,
|
|
&uniformData.fsLights.descriptor),
|
|
// Binding 5: Shadow map
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSet,
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
5,
|
|
&texDescriptorShadowMap),
|
|
};
|
|
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
|
|
|
|
// Offscreen (scene)
|
|
|
|
// Model
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.model));
|
|
writeDescriptorSets =
|
|
{
|
|
// Binding 0: Vertex shader uniform buffer
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSets.model,
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
0,
|
|
&uniformData.vsOffscreen.descriptor),
|
|
// Binding 1: Color map
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSets.model,
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
1,
|
|
&textures.model.colorMap.descriptor),
|
|
// Binding 2: Normal map
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSets.model,
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
2,
|
|
&textures.model.normalMap.descriptor)
|
|
};
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
|
|
|
|
// Background
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.background));
|
|
writeDescriptorSets =
|
|
{
|
|
// Binding 0: Vertex shader uniform buffer
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSets.background,
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
0,
|
|
&uniformData.vsOffscreen.descriptor),
|
|
// Binding 1: Color map
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSets.background,
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
1,
|
|
&textures.background.colorMap.descriptor),
|
|
// Binding 2: Normal map
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSets.background,
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
2,
|
|
&textures.background.normalMap.descriptor)
|
|
};
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
|
|
|
|
// Shadow mapping
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.shadow));
|
|
writeDescriptorSets =
|
|
{
|
|
// Binding 0: Vertex shader uniform buffer
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSets.shadow,
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
0,
|
|
&uniformData.uboShadowGS.descriptor),
|
|
};
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
|
|
}
|
|
|
|
void preparePipelines()
|
|
{
|
|
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState =
|
|
vkTools::initializers::pipelineInputAssemblyStateCreateInfo(
|
|
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
|
|
0,
|
|
VK_FALSE);
|
|
|
|
VkPipelineRasterizationStateCreateInfo rasterizationState =
|
|
vkTools::initializers::pipelineRasterizationStateCreateInfo(
|
|
VK_POLYGON_MODE_FILL,
|
|
VK_CULL_MODE_BACK_BIT,
|
|
VK_FRONT_FACE_CLOCKWISE,
|
|
0);
|
|
|
|
VkPipelineColorBlendAttachmentState blendAttachmentState =
|
|
vkTools::initializers::pipelineColorBlendAttachmentState(
|
|
0xf,
|
|
VK_FALSE);
|
|
|
|
VkPipelineColorBlendStateCreateInfo colorBlendState =
|
|
vkTools::initializers::pipelineColorBlendStateCreateInfo(
|
|
1,
|
|
&blendAttachmentState);
|
|
|
|
VkPipelineDepthStencilStateCreateInfo depthStencilState =
|
|
vkTools::initializers::pipelineDepthStencilStateCreateInfo(
|
|
VK_TRUE,
|
|
VK_TRUE,
|
|
VK_COMPARE_OP_LESS_OR_EQUAL);
|
|
|
|
VkPipelineViewportStateCreateInfo viewportState =
|
|
vkTools::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
|
|
|
|
VkPipelineMultisampleStateCreateInfo multisampleState =
|
|
vkTools::initializers::pipelineMultisampleStateCreateInfo(
|
|
VK_SAMPLE_COUNT_1_BIT,
|
|
0);
|
|
|
|
std::vector<VkDynamicState> dynamicStateEnables = {
|
|
VK_DYNAMIC_STATE_VIEWPORT,
|
|
VK_DYNAMIC_STATE_SCISSOR
|
|
};
|
|
VkPipelineDynamicStateCreateInfo dynamicState =
|
|
vkTools::initializers::pipelineDynamicStateCreateInfo(
|
|
dynamicStateEnables.data(),
|
|
static_cast<uint32_t>(dynamicStateEnables.size()),
|
|
0);
|
|
|
|
// Final fullscreen pass pipeline
|
|
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
|
|
|
|
shaderStages[0] = loadShader(getAssetPath() + "shaders/deferredshadows/deferred.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
shaderStages[1] = loadShader(getAssetPath() + "shaders/deferredshadows/deferred.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
|
|
|
VkGraphicsPipelineCreateInfo pipelineCreateInfo =
|
|
vkTools::initializers::pipelineCreateInfo(
|
|
pipelineLayouts.deferred,
|
|
renderPass,
|
|
0);
|
|
|
|
pipelineCreateInfo.pVertexInputState = &vertices.inputState;
|
|
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
|
|
pipelineCreateInfo.pRasterizationState = &rasterizationState;
|
|
pipelineCreateInfo.pColorBlendState = &colorBlendState;
|
|
pipelineCreateInfo.pMultisampleState = &multisampleState;
|
|
pipelineCreateInfo.pViewportState = &viewportState;
|
|
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
|
|
pipelineCreateInfo.pDynamicState = &dynamicState;
|
|
pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
|
|
pipelineCreateInfo.pStages = shaderStages.data();
|
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.deferred));
|
|
|
|
// Debug display pipeline
|
|
shaderStages[0] = loadShader(getAssetPath() + "shaders/deferredshadows/debug.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
shaderStages[1] = loadShader(getAssetPath() + "shaders/deferredshadows/debug.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.debug));
|
|
|
|
// Offscreen pipeline
|
|
shaderStages[0] = loadShader(getAssetPath() + "shaders/deferredshadows/mrt.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
shaderStages[1] = loadShader(getAssetPath() + "shaders/deferredshadows/mrt.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
|
|
|
// Separate render pass
|
|
pipelineCreateInfo.renderPass = frameBuffers.deferred.renderPass;
|
|
|
|
// Separate layout
|
|
pipelineCreateInfo.layout = pipelineLayouts.offscreen;
|
|
|
|
// Blend attachment states required for all color attachments
|
|
// This is important, as color write mask will otherwise be 0x0 and you
|
|
// won't see anything rendered to the attachment
|
|
std::array<VkPipelineColorBlendAttachmentState, 3> blendAttachmentStates =
|
|
{
|
|
vkTools::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE),
|
|
vkTools::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE),
|
|
vkTools::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE)
|
|
};
|
|
|
|
colorBlendState.attachmentCount = static_cast<uint32_t>(blendAttachmentStates.size());
|
|
colorBlendState.pAttachments = blendAttachmentStates.data();
|
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.offscreen));
|
|
|
|
// Shadow mapping pipeline
|
|
// The shadow mapping pipeline uses geometry shader instancing (invoctations layout modifier) to output
|
|
// shadow maps for multiple lights sources into the different shadiw map layers in one single render pass
|
|
std::array<VkPipelineShaderStageCreateInfo, 3> shadowStages;
|
|
shadowStages[0] = loadShader(getAssetPath() + "shaders/deferredshadows/shadow.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
shadowStages[1] = loadShader(getAssetPath() + "shaders/deferredshadows/shadow.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
|
shadowStages[2] = loadShader(getAssetPath() + "shaders/deferredshadows/shadow.geom.spv", VK_SHADER_STAGE_GEOMETRY_BIT);
|
|
|
|
pipelineCreateInfo.pStages = shadowStages.data();
|
|
pipelineCreateInfo.stageCount = static_cast<uint32_t>(shadowStages.size());
|
|
|
|
// Shadow pass doesn't use a color attachment
|
|
colorBlendState.attachmentCount = 0;
|
|
colorBlendState.pAttachments = nullptr;
|
|
// Cull front faces
|
|
rasterizationState.cullMode = VK_CULL_MODE_FRONT_BIT;
|
|
depthStencilState.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
|
|
// Enable depth bias
|
|
rasterizationState.depthBiasEnable = VK_TRUE;
|
|
// Add depth bias to dynamic state, so we can change it at runtime
|
|
dynamicStateEnables.push_back(VK_DYNAMIC_STATE_DEPTH_BIAS);
|
|
dynamicState =
|
|
vkTools::initializers::pipelineDynamicStateCreateInfo(
|
|
dynamicStateEnables.data(),
|
|
dynamicStateEnables.size(),
|
|
0);
|
|
// Reset blend attachment state
|
|
colorBlendState = vkTools::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
|
|
pipelineCreateInfo.renderPass = frameBuffers.shadow.renderPass;
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.shadowpass));
|
|
}
|
|
|
|
// Prepare and initialize uniform buffer containing shader uniforms
|
|
void prepareUniformBuffers()
|
|
{
|
|
// Fullscreen vertex shader
|
|
createBuffer(
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
|
sizeof(uboVS),
|
|
nullptr,
|
|
&uniformData.vsFullScreen.buffer,
|
|
&uniformData.vsFullScreen.memory,
|
|
&uniformData.vsFullScreen.descriptor);
|
|
|
|
// Deferred vertex shader
|
|
createBuffer(
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
|
sizeof(uboOffscreenVS),
|
|
nullptr,
|
|
&uniformData.vsOffscreen.buffer,
|
|
&uniformData.vsOffscreen.memory,
|
|
&uniformData.vsOffscreen.descriptor);
|
|
|
|
// Deferred fragment shader
|
|
createBuffer(
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
|
sizeof(uboFragmentLights),
|
|
nullptr,
|
|
&uniformData.fsLights.buffer,
|
|
&uniformData.fsLights.memory,
|
|
&uniformData.fsLights.descriptor);
|
|
|
|
// Shadow map vertex shader (matrices from shadow's pov)
|
|
createBuffer(
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
|
sizeof(uboShadowGS),
|
|
nullptr,
|
|
&uniformData.uboShadowGS.buffer,
|
|
&uniformData.uboShadowGS.memory,
|
|
&uniformData.uboShadowGS.descriptor);
|
|
|
|
// Init some values
|
|
uboOffscreenVS.instancePos[0] = glm::vec4(0.0f);
|
|
uboOffscreenVS.instancePos[1] = glm::vec4(-4.0f, 0.0, -4.0f, 0.0f);
|
|
uboOffscreenVS.instancePos[2] = glm::vec4(4.0f, 0.0, -4.0f, 0.0f);
|
|
|
|
// Update
|
|
updateUniformBuffersScreen();
|
|
updateUniformBufferDeferredMatrices();
|
|
updateUniformBufferDeferredLights();
|
|
}
|
|
|
|
void updateUniformBuffersScreen()
|
|
{
|
|
if (debugDisplay)
|
|
{
|
|
uboVS.projection = glm::ortho(0.0f, 2.0f, 0.0f, 2.0f, -1.0f, 1.0f);
|
|
}
|
|
else
|
|
{
|
|
uboVS.projection = glm::ortho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
|
|
}
|
|
uboVS.model = glm::mat4();
|
|
|
|
uint8_t *pData;
|
|
VK_CHECK_RESULT(vkMapMemory(device, uniformData.vsFullScreen.memory, 0, sizeof(uboVS), 0, (void **)&pData));
|
|
memcpy(pData, &uboVS, sizeof(uboVS));
|
|
vkUnmapMemory(device, uniformData.vsFullScreen.memory);
|
|
}
|
|
|
|
void updateUniformBufferDeferredMatrices()
|
|
{
|
|
uboOffscreenVS.projection = camera.matrices.perspective;
|
|
uboOffscreenVS.view = camera.matrices.view;
|
|
uboOffscreenVS.model = glm::mat4();
|
|
|
|
uint8_t *pData;
|
|
VK_CHECK_RESULT(vkMapMemory(device, uniformData.vsOffscreen.memory, 0, sizeof(uboOffscreenVS), 0, (void **)&pData));
|
|
memcpy(pData, &uboOffscreenVS, sizeof(uboOffscreenVS));
|
|
vkUnmapMemory(device, uniformData.vsOffscreen.memory);
|
|
}
|
|
|
|
// Update fragment shader light position uniform block
|
|
void updateUniformBufferDeferredLights()
|
|
{
|
|
std::vector<glm::vec4> lightPositions =
|
|
{
|
|
glm::vec4(-14.0f, -0.0f, 15.0f, 0.0f),
|
|
glm::vec4(14.0f, -4.0f, 12.0f, 0.0f),
|
|
glm::vec4(0.0f, -10.0f, 4.0f, 0.0f)
|
|
};
|
|
std::vector<glm::vec4> lightColors =
|
|
{
|
|
glm::vec4(1.0f, 0.0f, 0.0f, 0.0f),
|
|
glm::vec4(0.0f, 0.0f, 1.0f, 0.0f),
|
|
glm::vec4(1.0f, 1.0f, 1.0f, 0.0f),
|
|
};
|
|
std::vector<glm::vec4> lightTargets =
|
|
{
|
|
glm::vec4(-2.0f, 0.0f, 0.0f, 0.0f),
|
|
glm::vec4(2.0f, 0.0f, 0.0f, 0.0f),
|
|
glm::vec4(0.0f, 0.0f, 0.0f, 0.0f),
|
|
};
|
|
|
|
for (uint32_t i = 0; i < static_cast<uint32_t>(lightPositions.size()); i++)
|
|
{
|
|
Light *light = &uboFragmentLights.lights[i];
|
|
|
|
light->position = lightPositions[i];
|
|
light->color = lightColors[i];
|
|
light->target = lightTargets[i];
|
|
|
|
// mvp from light's pov (for shadows)
|
|
glm::mat4 shadowProj = glm::perspective(glm::radians(lightFOV), 1.0f, zNear, zFar);
|
|
glm::mat4 shadowView = glm::lookAt(glm::vec3(light->position), glm::vec3(light->target), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
glm::mat4 shadowModel = glm::mat4();
|
|
|
|
uboShadowGS.mvp[i] = shadowProj * shadowView * shadowModel;
|
|
light->viewMatrix = uboShadowGS.mvp[i];
|
|
}
|
|
|
|
uint8_t *pData;
|
|
|
|
memcpy(uboShadowGS.instancePos, uboOffscreenVS.instancePos, sizeof(uboOffscreenVS.instancePos));
|
|
|
|
VK_CHECK_RESULT(vkMapMemory(device, uniformData.uboShadowGS.memory, 0, sizeof(uboShadowGS), 0, (void **)&pData));
|
|
memcpy(pData, &uboShadowGS, sizeof(uboShadowGS));
|
|
vkUnmapMemory(device, uniformData.uboShadowGS.memory);
|
|
|
|
uboFragmentLights.viewPos = glm::vec4(uboOffscreenVS.view[3]);
|
|
|
|
VK_CHECK_RESULT(vkMapMemory(device, uniformData.fsLights.memory, 0, sizeof(uboFragmentLights), 0, (void **)&pData));
|
|
memcpy(pData, &uboFragmentLights, sizeof(uboFragmentLights));
|
|
vkUnmapMemory(device, uniformData.fsLights.memory);
|
|
}
|
|
|
|
void draw()
|
|
{
|
|
VulkanExampleBase::prepareFrame();
|
|
|
|
// Offscreen rendering
|
|
|
|
// Wait for swap chain presentation to finish
|
|
submitInfo.pWaitSemaphores = &semaphores.presentComplete;
|
|
// Signal ready with offscreen semaphore
|
|
submitInfo.pSignalSemaphores = &offscreenSemaphore;
|
|
|
|
// Submit work
|
|
|
|
// Shadow map pass
|
|
submitInfo.commandBufferCount = 1;
|
|
submitInfo.pCommandBuffers = &commandBuffers.deferred;
|
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
|
|
// Scene rendering
|
|
|
|
// Wait for offscreen semaphore
|
|
submitInfo.pWaitSemaphores = &offscreenSemaphore;
|
|
// Signal ready with render complete semaphpre
|
|
submitInfo.pSignalSemaphores = &semaphores.renderComplete;
|
|
|
|
// Submit work
|
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
|
|
VulkanExampleBase::submitFrame();
|
|
}
|
|
|
|
void prepare()
|
|
{
|
|
VulkanExampleBase::prepare();
|
|
loadTextures();
|
|
generateQuads();
|
|
loadMeshes();
|
|
setupVertexDescriptions();
|
|
deferredSetup();
|
|
shadowSetup();
|
|
prepareUniformBuffers();
|
|
setupDescriptorSetLayout();
|
|
preparePipelines();
|
|
setupDescriptorPool();
|
|
setupDescriptorSet();
|
|
buildCommandBuffers();
|
|
buildDeferredCommandBuffer();
|
|
prepared = true;
|
|
}
|
|
|
|
virtual void render()
|
|
{
|
|
if (!prepared)
|
|
return;
|
|
draw();
|
|
//updateUniformBufferDeferredLights();
|
|
}
|
|
|
|
virtual void viewChanged()
|
|
{
|
|
updateUniformBufferDeferredMatrices();
|
|
}
|
|
|
|
void toggleDebugDisplay()
|
|
{
|
|
debugDisplay = !debugDisplay;
|
|
reBuildCommandBuffers();
|
|
updateUniformBuffersScreen();
|
|
}
|
|
|
|
virtual void keyPressed(uint32_t keyCode)
|
|
{
|
|
switch (keyCode)
|
|
{
|
|
case 0x70:
|
|
case GAMEPAD_BUTTON_A:
|
|
toggleDebugDisplay();
|
|
updateTextOverlay();
|
|
break;
|
|
}
|
|
}
|
|
|
|
virtual void getOverlayText(VulkanTextOverlay *textOverlay)
|
|
{
|
|
#if defined(__ANDROID__)
|
|
textOverlay->addText("Press \"Button A\" to toggle debug display", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
|
|
#else
|
|
textOverlay->addText("Press \"F1\" to toggle debug display", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
|
|
#endif
|
|
// Render targets
|
|
if (debugDisplay)
|
|
{
|
|
textOverlay->addText("World space position", (float)width * 0.25f, (float)height * 0.5f - 25.0f, VulkanTextOverlay::alignCenter);
|
|
textOverlay->addText("World space normals", (float)width * 0.75f, (float)height * 0.5f - 25.0f, VulkanTextOverlay::alignCenter);
|
|
textOverlay->addText("Albedo", (float)width * 0.25f, (float)height - 25.0f, VulkanTextOverlay::alignCenter);
|
|
textOverlay->addText("Final image", (float)width * 0.75f, (float)height - 25.0f, VulkanTextOverlay::alignCenter);
|
|
}
|
|
}
|
|
};
|
|
|
|
VULKAN_EXAMPLE_MAIN() |