2016-02-16 15:07:25 +01:00
|
|
|
/*
|
|
|
|
|
* Vulkan Example - Omni directional shadows using a dynamic cube map
|
|
|
|
|
*
|
2023-12-24 15:58:23 +01:00
|
|
|
* Copyright (C) 2016-2023by Sascha Willems - www.saschawillems.de
|
2016-02-16 15:07:25 +01:00
|
|
|
*
|
|
|
|
|
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "vulkanexamplebase.h"
|
2020-07-28 20:20:38 +02:00
|
|
|
#include "VulkanglTFModel.h"
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
#define ENABLE_VALIDATION false
|
|
|
|
|
|
|
|
|
|
// Texture properties
|
|
|
|
|
#define TEX_DIM 1024
|
|
|
|
|
#define TEX_FILTER VK_FILTER_LINEAR
|
|
|
|
|
|
|
|
|
|
// Offscreen frame buffer properties
|
|
|
|
|
#define FB_DIM TEX_DIM
|
2020-05-29 16:08:53 +01:00
|
|
|
#define FB_COLOR_FORMAT VK_FORMAT_R32_SFLOAT
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
class VulkanExample : public VulkanExampleBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
bool displayCubeMap = false;
|
|
|
|
|
|
|
|
|
|
float zNear = 0.1f;
|
|
|
|
|
float zFar = 1024.0f;
|
|
|
|
|
|
|
|
|
|
struct {
|
2020-07-28 20:20:38 +02:00
|
|
|
vkglTF::Model scene;
|
|
|
|
|
vkglTF::Model debugcube;
|
2017-02-11 14:18:24 +01:00
|
|
|
} models;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
struct {
|
2017-02-12 10:44:51 +01:00
|
|
|
vks::Buffer scene;
|
|
|
|
|
vks::Buffer offscreen;
|
2016-12-24 12:48:01 +01:00
|
|
|
} uniformBuffers;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
glm::mat4 projection;
|
|
|
|
|
glm::mat4 model;
|
|
|
|
|
} uboVSquad;
|
|
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
glm::vec4 lightPos = glm::vec4(0.0f, -2.5f, 0.0f, 1.0);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-12-24 12:48:01 +01:00
|
|
|
struct UBO {
|
2016-02-16 15:07:25 +01:00
|
|
|
glm::mat4 projection;
|
|
|
|
|
glm::mat4 view;
|
|
|
|
|
glm::mat4 model;
|
|
|
|
|
glm::vec4 lightPos;
|
2016-12-24 12:48:01 +01:00
|
|
|
};
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-12-24 12:48:01 +01:00
|
|
|
UBO uboVSscene, uboOffscreenVS;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
VkPipeline scene;
|
|
|
|
|
VkPipeline offscreen;
|
2019-04-15 21:30:57 +02:00
|
|
|
VkPipeline cubemapDisplay;
|
2016-02-16 15:07:25 +01:00
|
|
|
} pipelines;
|
|
|
|
|
|
|
|
|
|
struct {
|
2020-05-29 16:08:53 +01:00
|
|
|
VkPipelineLayout scene;
|
2016-02-16 15:07:25 +01:00
|
|
|
VkPipelineLayout offscreen;
|
|
|
|
|
} pipelineLayouts;
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
VkDescriptorSet scene;
|
|
|
|
|
VkDescriptorSet offscreen;
|
|
|
|
|
} descriptorSets;
|
|
|
|
|
|
|
|
|
|
VkDescriptorSetLayout descriptorSetLayout;
|
|
|
|
|
|
2017-02-09 21:55:35 +01:00
|
|
|
vks::Texture shadowCubeMap;
|
2020-11-28 11:01:11 +00:00
|
|
|
std::array<VkImageView, 6> shadowCubeMapFaceImageViews;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Framebuffer for offscreen rendering
|
|
|
|
|
struct FrameBufferAttachment {
|
|
|
|
|
VkImage image;
|
|
|
|
|
VkDeviceMemory mem;
|
|
|
|
|
VkImageView view;
|
|
|
|
|
};
|
2016-08-13 20:35:22 +02:00
|
|
|
struct OffscreenPass {
|
2016-02-16 15:07:25 +01:00
|
|
|
int32_t width, height;
|
2020-11-28 11:01:11 +00:00
|
|
|
std::array<VkFramebuffer, 6> frameBuffers;
|
|
|
|
|
FrameBufferAttachment depth;
|
2016-05-31 20:07:43 +02:00
|
|
|
VkRenderPass renderPass;
|
2016-08-13 20:35:22 +02:00
|
|
|
VkSampler sampler;
|
|
|
|
|
VkDescriptorImageInfo descriptor;
|
|
|
|
|
} offscreenPass;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-05-17 15:52:33 +01:00
|
|
|
VkFormat fbDepthFormat;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
|
|
|
|
{
|
2017-11-01 14:22:10 +01:00
|
|
|
title = "Point light shadows (cubemap)";
|
2019-04-14 19:38:30 +02:00
|
|
|
camera.type = Camera::CameraType::lookat;
|
|
|
|
|
camera.setPerspective(45.0f, (float)width / (float)height, zNear, zFar);
|
|
|
|
|
camera.setRotation(glm::vec3(-20.5f, -673.0f, 0.0f));
|
2020-07-28 20:20:38 +02:00
|
|
|
camera.setPosition(glm::vec3(0.0f, 0.5f, -15.0f));
|
|
|
|
|
timerSpeed *= 0.5f;
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~VulkanExample()
|
|
|
|
|
{
|
2020-05-29 16:08:53 +01:00
|
|
|
// Clean up used Vulkan resources
|
2016-02-16 15:07:25 +01:00
|
|
|
// Note : Inherited destructor cleans up resources stored in base class
|
|
|
|
|
|
|
|
|
|
// Cube map
|
2020-11-28 11:01:11 +00:00
|
|
|
for (uint32_t i = 0; i < 6; i++)
|
|
|
|
|
{
|
|
|
|
|
vkDestroyImageView(device, shadowCubeMapFaceImageViews[i], nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
vkDestroyImageView(device, shadowCubeMap.view, nullptr);
|
|
|
|
|
vkDestroyImage(device, shadowCubeMap.image, nullptr);
|
|
|
|
|
vkDestroySampler(device, shadowCubeMap.sampler, nullptr);
|
|
|
|
|
vkFreeMemory(device, shadowCubeMap.deviceMemory, nullptr);
|
|
|
|
|
|
|
|
|
|
// Depth attachment
|
2016-08-13 20:35:22 +02:00
|
|
|
vkDestroyImageView(device, offscreenPass.depth.view, nullptr);
|
|
|
|
|
vkDestroyImage(device, offscreenPass.depth.image, nullptr);
|
|
|
|
|
vkFreeMemory(device, offscreenPass.depth.mem, nullptr);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2020-11-28 11:01:11 +00:00
|
|
|
for (uint32_t i = 0; i < 6; i++)
|
|
|
|
|
{
|
|
|
|
|
vkDestroyFramebuffer(device, offscreenPass.frameBuffers[i], nullptr);
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-08-13 20:35:22 +02:00
|
|
|
vkDestroyRenderPass(device, offscreenPass.renderPass, nullptr);
|
2016-05-17 15:52:33 +01:00
|
|
|
|
2020-08-09 13:48:49 +02:00
|
|
|
// Pipelines
|
2016-02-16 15:07:25 +01:00
|
|
|
vkDestroyPipeline(device, pipelines.scene, nullptr);
|
|
|
|
|
vkDestroyPipeline(device, pipelines.offscreen, nullptr);
|
2019-04-15 21:30:57 +02:00
|
|
|
vkDestroyPipeline(device, pipelines.cubemapDisplay, nullptr);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
vkDestroyPipelineLayout(device, pipelineLayouts.scene, nullptr);
|
|
|
|
|
vkDestroyPipelineLayout(device, pipelineLayouts.offscreen, nullptr);
|
|
|
|
|
|
|
|
|
|
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
|
|
|
|
|
|
|
|
|
// Uniform buffers
|
2016-12-24 12:48:01 +01:00
|
|
|
uniformBuffers.offscreen.destroy();
|
|
|
|
|
uniformBuffers.scene.destroy();
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void prepareCubeMap()
|
|
|
|
|
{
|
|
|
|
|
shadowCubeMap.width = TEX_DIM;
|
|
|
|
|
shadowCubeMap.height = TEX_DIM;
|
2020-05-29 16:08:53 +01:00
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
// 32 bit float format for higher precision
|
|
|
|
|
VkFormat format = VK_FORMAT_R32_SFLOAT;
|
|
|
|
|
|
|
|
|
|
// Cube map image description
|
2017-02-12 11:12:42 +01:00
|
|
|
VkImageCreateInfo imageCreateInfo = vks::initializers::imageCreateInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
|
|
|
|
|
imageCreateInfo.format = format;
|
|
|
|
|
imageCreateInfo.extent = { shadowCubeMap.width, shadowCubeMap.height, 1 };
|
|
|
|
|
imageCreateInfo.mipLevels = 1;
|
|
|
|
|
imageCreateInfo.arrayLayers = 6;
|
|
|
|
|
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
|
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
2020-11-28 11:01:11 +00:00
|
|
|
imageCreateInfo.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
2016-02-16 15:07:25 +01:00
|
|
|
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
2016-06-22 20:30:14 +02:00
|
|
|
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
2016-02-16 15:07:25 +01:00
|
|
|
imageCreateInfo.flags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkMemoryAllocateInfo memAllocInfo = vks::initializers::memoryAllocateInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
VkMemoryRequirements memReqs;
|
|
|
|
|
|
2020-04-20 22:13:51 +02:00
|
|
|
VkCommandBuffer layoutCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Create cube map image
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateImage(device, &imageCreateInfo, nullptr, &shadowCubeMap.image));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
vkGetImageMemoryRequirements(device, shadowCubeMap.image, &memReqs);
|
|
|
|
|
|
|
|
|
|
memAllocInfo.allocationSize = memReqs.size;
|
2016-07-23 20:42:03 +02:00
|
|
|
memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &shadowCubeMap.deviceMemory));
|
|
|
|
|
VK_CHECK_RESULT(vkBindImageMemory(device, shadowCubeMap.image, shadowCubeMap.deviceMemory, 0));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Image barrier for optimal image (target)
|
2016-03-13 16:04:39 +01:00
|
|
|
VkImageSubresourceRange subresourceRange = {};
|
|
|
|
|
subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
|
subresourceRange.baseMipLevel = 0;
|
|
|
|
|
subresourceRange.levelCount = 1;
|
|
|
|
|
subresourceRange.layerCount = 6;
|
2017-02-12 13:10:05 +01:00
|
|
|
vks::tools::setImageLayout(
|
2016-05-31 20:07:43 +02:00
|
|
|
layoutCmd,
|
2016-02-16 15:07:25 +01:00
|
|
|
shadowCubeMap.image,
|
2016-06-22 20:30:14 +02:00
|
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
2016-03-13 16:04:39 +01:00
|
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
|
|
|
subresourceRange);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2020-04-20 22:13:51 +02:00
|
|
|
vulkanDevice->flushCommandBuffer(layoutCmd, queue, true);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Create sampler
|
2017-02-12 11:12:42 +01:00
|
|
|
VkSamplerCreateInfo sampler = vks::initializers::samplerCreateInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
sampler.magFilter = TEX_FILTER;
|
|
|
|
|
sampler.minFilter = TEX_FILTER;
|
|
|
|
|
sampler.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
|
|
|
|
sampler.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
|
|
|
|
|
sampler.addressModeV = sampler.addressModeU;
|
|
|
|
|
sampler.addressModeW = sampler.addressModeU;
|
|
|
|
|
sampler.mipLodBias = 0.0f;
|
2017-06-17 16:07:38 +02:00
|
|
|
sampler.maxAnisotropy = 1.0f;
|
2016-02-16 15:07:25 +01:00
|
|
|
sampler.compareOp = VK_COMPARE_OP_NEVER;
|
|
|
|
|
sampler.minLod = 0.0f;
|
2016-05-31 20:07:43 +02:00
|
|
|
sampler.maxLod = 1.0f;
|
2016-02-16 15:07:25 +01:00
|
|
|
sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &shadowCubeMap.sampler));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Create image view
|
2017-02-12 11:12:42 +01:00
|
|
|
VkImageViewCreateInfo view = vks::initializers::imageViewCreateInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
view.image = VK_NULL_HANDLE;
|
|
|
|
|
view.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
|
|
|
|
|
view.format = format;
|
|
|
|
|
view.components = { VK_COMPONENT_SWIZZLE_R };
|
|
|
|
|
view.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
|
|
|
|
|
view.subresourceRange.layerCount = 6;
|
|
|
|
|
view.image = shadowCubeMap.image;
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateImageView(device, &view, nullptr, &shadowCubeMap.view));
|
2020-11-28 11:01:11 +00:00
|
|
|
|
|
|
|
|
view.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
|
|
|
view.subresourceRange.layerCount = 1;
|
|
|
|
|
view.image = shadowCubeMap.image;
|
|
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < 6; i++)
|
|
|
|
|
{
|
|
|
|
|
view.subresourceRange.baseArrayLayer = i;
|
|
|
|
|
VK_CHECK_RESULT(vkCreateImageView(device, &view, nullptr, &shadowCubeMapFaceImageViews[i]));
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare a new framebuffer for offscreen rendering
|
|
|
|
|
// The contents of this framebuffer are then
|
|
|
|
|
// copied to the different cube map faces
|
|
|
|
|
void prepareOffscreenFramebuffer()
|
|
|
|
|
{
|
2016-08-13 20:35:22 +02:00
|
|
|
offscreenPass.width = FB_DIM;
|
|
|
|
|
offscreenPass.height = FB_DIM;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
VkFormat fbColorFormat = FB_COLOR_FORMAT;
|
|
|
|
|
|
|
|
|
|
// Color attachment
|
2017-02-12 11:12:42 +01:00
|
|
|
VkImageCreateInfo imageCreateInfo = vks::initializers::imageCreateInfo();
|
2016-06-22 20:30:14 +02:00
|
|
|
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
|
|
|
|
|
imageCreateInfo.format = fbColorFormat;
|
2016-08-13 20:35:22 +02:00
|
|
|
imageCreateInfo.extent.width = offscreenPass.width;
|
|
|
|
|
imageCreateInfo.extent.height = offscreenPass.height;
|
2016-06-22 20:30:14 +02:00
|
|
|
imageCreateInfo.extent.depth = 1;
|
|
|
|
|
imageCreateInfo.mipLevels = 1;
|
|
|
|
|
imageCreateInfo.arrayLayers = 1;
|
|
|
|
|
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
|
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
2016-02-16 15:07:25 +01:00
|
|
|
// Image of the framebuffer is blit source
|
2016-06-22 20:30:14 +02:00
|
|
|
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
|
|
|
imageCreateInfo.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
|
|
|
|
|
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkImageViewCreateInfo colorImageView = vks::initializers::imageViewCreateInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
colorImageView.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
|
|
|
colorImageView.format = fbColorFormat;
|
|
|
|
|
colorImageView.flags = 0;
|
|
|
|
|
colorImageView.subresourceRange = {};
|
|
|
|
|
colorImageView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
|
colorImageView.subresourceRange.baseMipLevel = 0;
|
|
|
|
|
colorImageView.subresourceRange.levelCount = 1;
|
|
|
|
|
colorImageView.subresourceRange.baseArrayLayer = 0;
|
|
|
|
|
colorImageView.subresourceRange.layerCount = 1;
|
|
|
|
|
|
2020-04-20 22:13:51 +02:00
|
|
|
VkCommandBuffer layoutCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Depth stencil attachment
|
2016-06-22 20:30:14 +02:00
|
|
|
imageCreateInfo.format = fbDepthFormat;
|
|
|
|
|
imageCreateInfo.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkImageViewCreateInfo depthStencilView = vks::initializers::imageViewCreateInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
depthStencilView.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
|
|
|
depthStencilView.format = fbDepthFormat;
|
|
|
|
|
depthStencilView.flags = 0;
|
|
|
|
|
depthStencilView.subresourceRange = {};
|
2022-06-30 09:38:25 +02:00
|
|
|
depthStencilView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
|
|
|
|
|
if (fbDepthFormat >= VK_FORMAT_D16_UNORM_S8_UINT)
|
|
|
|
|
depthStencilView.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
|
2016-02-16 15:07:25 +01:00
|
|
|
depthStencilView.subresourceRange.baseMipLevel = 0;
|
|
|
|
|
depthStencilView.subresourceRange.levelCount = 1;
|
|
|
|
|
depthStencilView.subresourceRange.baseArrayLayer = 0;
|
|
|
|
|
depthStencilView.subresourceRange.layerCount = 1;
|
|
|
|
|
|
2016-08-13 20:35:22 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateImage(device, &imageCreateInfo, nullptr, &offscreenPass.depth.image));
|
2020-11-28 11:01:11 +00:00
|
|
|
|
|
|
|
|
VkMemoryRequirements memReqs;
|
2016-08-13 20:35:22 +02:00
|
|
|
vkGetImageMemoryRequirements(device, offscreenPass.depth.image, &memReqs);
|
2020-11-28 11:01:11 +00:00
|
|
|
|
|
|
|
|
VkMemoryAllocateInfo memAlloc = vks::initializers::memoryAllocateInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
memAlloc.allocationSize = memReqs.size;
|
2016-07-23 20:42:03 +02:00
|
|
|
memAlloc.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
2016-08-13 20:35:22 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &offscreenPass.depth.mem));
|
|
|
|
|
VK_CHECK_RESULT(vkBindImageMemory(device, offscreenPass.depth.image, offscreenPass.depth.mem, 0));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2017-02-12 13:10:05 +01:00
|
|
|
vks::tools::setImageLayout(
|
2016-05-31 20:07:43 +02:00
|
|
|
layoutCmd,
|
2020-05-29 16:08:53 +01:00
|
|
|
offscreenPass.depth.image,
|
2016-03-13 16:04:39 +01:00
|
|
|
VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT,
|
2016-06-22 20:30:14 +02:00
|
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
|
|
|
|
|
|
2020-04-20 22:13:51 +02:00
|
|
|
vulkanDevice->flushCommandBuffer(layoutCmd, queue, true);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-08-13 20:35:22 +02:00
|
|
|
depthStencilView.image = offscreenPass.depth.image;
|
|
|
|
|
VK_CHECK_RESULT(vkCreateImageView(device, &depthStencilView, nullptr, &offscreenPass.depth.view));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
VkImageView attachments[2];
|
2016-08-13 20:35:22 +02:00
|
|
|
attachments[1] = offscreenPass.depth.view;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkFramebufferCreateInfo fbufCreateInfo = vks::initializers::framebufferCreateInfo();
|
2016-08-13 20:35:22 +02:00
|
|
|
fbufCreateInfo.renderPass = offscreenPass.renderPass;
|
2016-02-16 15:07:25 +01:00
|
|
|
fbufCreateInfo.attachmentCount = 2;
|
|
|
|
|
fbufCreateInfo.pAttachments = attachments;
|
2016-08-13 20:35:22 +02:00
|
|
|
fbufCreateInfo.width = offscreenPass.width;
|
|
|
|
|
fbufCreateInfo.height = offscreenPass.height;
|
2016-02-16 15:07:25 +01:00
|
|
|
fbufCreateInfo.layers = 1;
|
|
|
|
|
|
2020-11-28 11:01:11 +00:00
|
|
|
for (uint32_t i = 0; i < 6; i++)
|
|
|
|
|
{
|
|
|
|
|
attachments[0] = shadowCubeMapFaceImageViews[i];
|
|
|
|
|
VK_CHECK_RESULT(vkCreateFramebuffer(device, &fbufCreateInfo, nullptr, &offscreenPass.frameBuffers[i]));
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Updates a single cube map face
|
2020-11-28 11:01:11 +00:00
|
|
|
// Renders the scene with face's view directly to the cubemap layer `faceIndex`
|
2019-04-14 19:38:30 +02:00
|
|
|
// Uses push constants for quick update of view matrix for the current cube map face
|
|
|
|
|
void updateCubeFace(uint32_t faceIndex, VkCommandBuffer commandBuffer)
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
|
|
|
|
VkClearValue clearValues[2];
|
|
|
|
|
clearValues[0].color = { { 0.0f, 0.0f, 0.0f, 1.0f } };
|
|
|
|
|
clearValues[1].depthStencil = { 1.0f, 0 };
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkRenderPassBeginInfo renderPassBeginInfo = vks::initializers::renderPassBeginInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
// Reuse render pass from example pass
|
2016-08-13 20:35:22 +02:00
|
|
|
renderPassBeginInfo.renderPass = offscreenPass.renderPass;
|
2020-11-28 11:01:11 +00:00
|
|
|
renderPassBeginInfo.framebuffer = offscreenPass.frameBuffers[faceIndex];
|
2016-08-13 20:35:22 +02:00
|
|
|
renderPassBeginInfo.renderArea.extent.width = offscreenPass.width;
|
|
|
|
|
renderPassBeginInfo.renderArea.extent.height = offscreenPass.height;
|
2016-02-16 15:07:25 +01:00
|
|
|
renderPassBeginInfo.clearValueCount = 2;
|
|
|
|
|
renderPassBeginInfo.pClearValues = clearValues;
|
|
|
|
|
|
|
|
|
|
// Update view matrix via push constant
|
|
|
|
|
|
2017-09-24 18:17:07 +02:00
|
|
|
glm::mat4 viewMatrix = glm::mat4(1.0f);
|
2016-02-16 15:07:25 +01:00
|
|
|
switch (faceIndex)
|
|
|
|
|
{
|
|
|
|
|
case 0: // POSITIVE_X
|
2016-03-08 20:59:25 +01:00
|
|
|
viewMatrix = glm::rotate(viewMatrix, glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
|
viewMatrix = glm::rotate(viewMatrix, glm::radians(180.0f), glm::vec3(1.0f, 0.0f, 0.0f));
|
2016-02-16 15:07:25 +01:00
|
|
|
break;
|
|
|
|
|
case 1: // NEGATIVE_X
|
2016-03-08 20:59:25 +01:00
|
|
|
viewMatrix = glm::rotate(viewMatrix, glm::radians(-90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
|
viewMatrix = glm::rotate(viewMatrix, glm::radians(180.0f), glm::vec3(1.0f, 0.0f, 0.0f));
|
2016-02-16 15:07:25 +01:00
|
|
|
break;
|
|
|
|
|
case 2: // POSITIVE_Y
|
2016-03-08 20:59:25 +01:00
|
|
|
viewMatrix = glm::rotate(viewMatrix, glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
|
2016-02-16 15:07:25 +01:00
|
|
|
break;
|
|
|
|
|
case 3: // NEGATIVE_Y
|
2016-03-08 20:59:25 +01:00
|
|
|
viewMatrix = glm::rotate(viewMatrix, glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
|
2016-02-16 15:07:25 +01:00
|
|
|
break;
|
|
|
|
|
case 4: // POSITIVE_Z
|
2016-03-08 20:59:25 +01:00
|
|
|
viewMatrix = glm::rotate(viewMatrix, glm::radians(180.0f), glm::vec3(1.0f, 0.0f, 0.0f));
|
2016-02-16 15:07:25 +01:00
|
|
|
break;
|
|
|
|
|
case 5: // NEGATIVE_Z
|
2016-03-08 20:59:25 +01:00
|
|
|
viewMatrix = glm::rotate(viewMatrix, glm::radians(180.0f), glm::vec3(0.0f, 0.0f, 1.0f));
|
2016-02-16 15:07:25 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 19:58:06 +01:00
|
|
|
// Render scene from cube face's point of view
|
2019-04-14 19:38:30 +02:00
|
|
|
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
2016-03-02 19:58:06 +01:00
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
// Update shader push constant block
|
|
|
|
|
// Contains current face view matrix
|
|
|
|
|
vkCmdPushConstants(
|
2019-04-14 19:38:30 +02:00
|
|
|
commandBuffer,
|
2016-02-16 15:07:25 +01:00
|
|
|
pipelineLayouts.offscreen,
|
|
|
|
|
VK_SHADER_STAGE_VERTEX_BIT,
|
|
|
|
|
0,
|
|
|
|
|
sizeof(glm::mat4),
|
|
|
|
|
&viewMatrix);
|
|
|
|
|
|
2019-04-14 19:38:30 +02:00
|
|
|
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.offscreen);
|
|
|
|
|
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.offscreen, 0, 1, &descriptorSets.offscreen, 0, NULL);
|
2020-07-28 20:20:38 +02:00
|
|
|
models.scene.draw(commandBuffer);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2019-04-14 19:38:30 +02:00
|
|
|
vkCmdEndRenderPass(commandBuffer);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void buildCommandBuffers()
|
|
|
|
|
{
|
2017-02-12 11:12:42 +01:00
|
|
|
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
|
|
|
|
|
{
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2019-04-14 19:38:30 +02:00
|
|
|
/*
|
|
|
|
|
Generate shadow cube maps using one render pass per face
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
VkViewport viewport = vks::initializers::viewport((float)offscreenPass.width, (float)offscreenPass.height, 0.0f, 1.0f);
|
|
|
|
|
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2019-04-14 19:38:30 +02:00
|
|
|
VkRect2D scissor = vks::initializers::rect2D(offscreenPass.width, offscreenPass.height, 0, 0);
|
|
|
|
|
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2019-04-14 19:38:30 +02:00
|
|
|
for (uint32_t face = 0; face < 6; face++) {
|
|
|
|
|
updateCubeFace(face, drawCmdBuffers[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2019-04-14 19:38:30 +02:00
|
|
|
/*
|
|
|
|
|
Note: Explicit synchronization is not required between the render pass, as this is done implicit via sub pass dependencies
|
|
|
|
|
*/
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2019-04-14 19:38:30 +02:00
|
|
|
/*
|
|
|
|
|
Scene rendering with applied shadow map
|
|
|
|
|
*/
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
2019-04-14 19:38:30 +02:00
|
|
|
VkClearValue clearValues[2];
|
|
|
|
|
clearValues[0].color = defaultClearColor;
|
|
|
|
|
clearValues[1].depthStencil = { 1.0f, 0 };
|
|
|
|
|
|
|
|
|
|
VkRenderPassBeginInfo renderPassBeginInfo = vks::initializers::renderPassBeginInfo();
|
|
|
|
|
renderPassBeginInfo.renderPass = renderPass;
|
|
|
|
|
renderPassBeginInfo.framebuffer = frameBuffers[i];
|
|
|
|
|
renderPassBeginInfo.renderArea.extent.width = width;
|
|
|
|
|
renderPassBeginInfo.renderArea.extent.height = height;
|
|
|
|
|
renderPassBeginInfo.clearValueCount = 2;
|
|
|
|
|
renderPassBeginInfo.pClearValues = clearValues;
|
|
|
|
|
|
|
|
|
|
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
|
|
|
|
|
|
VkViewport viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
|
|
|
|
|
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
|
|
|
|
|
|
|
|
|
|
VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0);
|
|
|
|
|
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
|
|
|
|
|
|
|
|
|
|
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.scene, 0, 1, &descriptorSets.scene, 0, NULL);
|
|
|
|
|
|
|
|
|
|
if (displayCubeMap)
|
|
|
|
|
{
|
2019-04-15 21:30:57 +02:00
|
|
|
// Display all six sides of the shadow cube map
|
|
|
|
|
// Note: Visualization of the different faces is done in the fragment shader, see cubemapdisplay.frag
|
|
|
|
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.cubemapDisplay);
|
2020-07-28 20:20:38 +02:00
|
|
|
models.debugcube.draw(drawCmdBuffers[i]);
|
2019-04-14 19:38:30 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.scene);
|
2020-07-28 20:20:38 +02:00
|
|
|
models.scene.draw(drawCmdBuffers[i]);
|
2019-04-14 19:38:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
drawUI(drawCmdBuffers[i]);
|
|
|
|
|
|
|
|
|
|
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
2020-05-29 16:08:53 +01:00
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-08-13 20:35:22 +02:00
|
|
|
void loadAssets()
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
2020-07-28 20:20:38 +02:00
|
|
|
const uint32_t glTFLoadingFlags = vkglTF::FileLoadingFlags::PreTransformVertices | vkglTF::FileLoadingFlags::PreMultiplyVertexColors | vkglTF::FileLoadingFlags::FlipY;
|
|
|
|
|
models.debugcube.loadFromFile(getAssetPath() + "models/cube.gltf", vulkanDevice, queue, glTFLoadingFlags);
|
|
|
|
|
models.scene.loadFromFile(getAssetPath() + "models/shadowscene_fire.gltf", vulkanDevice, queue, glTFLoadingFlags);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorPool()
|
|
|
|
|
{
|
|
|
|
|
// Example uses three ubos and two image samplers
|
2019-04-14 19:38:30 +02:00
|
|
|
std::vector<VkDescriptorPoolSize> poolSizes = {
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 3),
|
|
|
|
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2)
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
2023-12-24 15:58:23 +01:00
|
|
|
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 3);
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorSetLayout()
|
|
|
|
|
{
|
|
|
|
|
// Shared pipeline layout
|
2019-04-14 19:38:30 +02:00
|
|
|
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
|
2016-02-16 15:07:25 +01:00
|
|
|
// Binding 0 : Vertex shader uniform buffer
|
2019-04-14 19:38:30 +02:00
|
|
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0),
|
2016-02-16 15:07:25 +01:00
|
|
|
// Binding 1 : Fragment shader image sampler (cube map)
|
2019-04-14 19:38:30 +02:00
|
|
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 1)
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
|
|
|
|
|
2023-12-24 15:58:23 +01:00
|
|
|
VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// 3D scene pipeline layout
|
2019-04-14 19:38:30 +02:00
|
|
|
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.scene));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Offscreen pipeline layout
|
|
|
|
|
// Push constants for cube map face view matrices
|
2019-04-14 19:38:30 +02:00
|
|
|
VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::mat4), 0);
|
2016-02-16 15:07:25 +01:00
|
|
|
// Push constant ranges are part of the pipeline layout
|
|
|
|
|
pPipelineLayoutCreateInfo.pushConstantRangeCount = 1;
|
|
|
|
|
pPipelineLayoutCreateInfo.pPushConstantRanges = &pushConstantRange;
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.offscreen));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorSets()
|
|
|
|
|
{
|
2019-04-14 19:38:30 +02:00
|
|
|
VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// 3D scene
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.scene));
|
2020-05-29 16:08:53 +01:00
|
|
|
// Image descriptor for the cube map
|
2016-02-16 15:07:25 +01:00
|
|
|
VkDescriptorImageInfo texDescriptor =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorImageInfo(
|
2016-02-16 15:07:25 +01:00
|
|
|
shadowCubeMap.sampler,
|
|
|
|
|
shadowCubeMap.view,
|
2017-07-20 14:20:43 +02:00
|
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2019-04-14 19:38:30 +02:00
|
|
|
std::vector<VkWriteDescriptorSet> sceneDescriptorSets = {
|
2016-02-16 15:07:25 +01:00
|
|
|
// Binding 0 : Vertex shader uniform buffer
|
2019-04-14 19:38:30 +02:00
|
|
|
vks::initializers::writeDescriptorSet(descriptorSets.scene, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.scene.descriptor),
|
2016-02-16 15:07:25 +01:00
|
|
|
// Binding 1 : Fragment shader shadow sampler
|
2019-04-14 19:38:30 +02:00
|
|
|
vks::initializers::writeDescriptorSet(descriptorSets.scene, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &texDescriptor)
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
2023-12-24 15:58:23 +01:00
|
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(sceneDescriptorSets.size()), sceneDescriptorSets.data(), 0, nullptr);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Offscreen
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.offscreen));
|
2019-04-14 19:38:30 +02:00
|
|
|
std::vector<VkWriteDescriptorSet> offScreenWriteDescriptorSets = {
|
2016-02-16 15:07:25 +01:00
|
|
|
// Binding 0 : Vertex shader uniform buffer
|
2019-04-14 19:38:30 +02:00
|
|
|
vks::initializers::writeDescriptorSet(descriptorSets.offscreen, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.offscreen.descriptor),
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
2023-12-24 15:58:23 +01:00
|
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(offScreenWriteDescriptorSets.size()), offScreenWriteDescriptorSets.data(), 0, nullptr);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
2016-05-31 20:07:43 +02:00
|
|
|
// Set up a separate render pass for the offscreen frame buffer
|
|
|
|
|
// This is necessary as the offscreen frame buffer attachments
|
|
|
|
|
// use formats different to the ones from the visible frame buffer
|
|
|
|
|
// and at least the depth one may not be compatible
|
2016-05-17 15:52:33 +01:00
|
|
|
void prepareOffscreenRenderpass()
|
|
|
|
|
{
|
|
|
|
|
VkAttachmentDescription osAttachments[2] = {};
|
|
|
|
|
|
|
|
|
|
// Find a suitable depth format
|
2017-02-12 13:10:05 +01:00
|
|
|
VkBool32 validDepthFormat = vks::tools::getSupportedDepthFormat(physicalDevice, &fbDepthFormat);
|
2016-05-17 15:52:33 +01:00
|
|
|
assert(validDepthFormat);
|
|
|
|
|
|
|
|
|
|
osAttachments[0].format = FB_COLOR_FORMAT;
|
|
|
|
|
osAttachments[0].samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
|
osAttachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
|
|
|
|
osAttachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
|
|
|
|
osAttachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
|
|
|
osAttachments[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
2020-11-28 11:01:11 +00:00
|
|
|
osAttachments[0].initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
|
|
|
osAttachments[0].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
2016-05-17 15:52:33 +01:00
|
|
|
|
|
|
|
|
// Depth attachment
|
|
|
|
|
osAttachments[1].format = fbDepthFormat;
|
|
|
|
|
osAttachments[1].samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
|
osAttachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
|
|
|
|
osAttachments[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
|
|
|
|
osAttachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
|
|
|
osAttachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
|
|
|
osAttachments[1].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
|
|
|
osAttachments[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
|
|
|
|
|
|
|
|
VkAttachmentReference colorReference = {};
|
|
|
|
|
colorReference.attachment = 0;
|
|
|
|
|
colorReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
|
|
|
|
|
|
|
|
|
VkAttachmentReference depthReference = {};
|
|
|
|
|
depthReference.attachment = 1;
|
|
|
|
|
depthReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
|
|
|
|
2016-05-31 20:07:43 +02:00
|
|
|
VkSubpassDescription subpass = {};
|
|
|
|
|
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
|
|
|
|
subpass.colorAttachmentCount = 1;
|
|
|
|
|
subpass.pColorAttachments = &colorReference;
|
|
|
|
|
subpass.pDepthStencilAttachment = &depthReference;
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkRenderPassCreateInfo renderPassCreateInfo = vks::initializers::renderPassCreateInfo();
|
2016-05-31 20:07:43 +02:00
|
|
|
renderPassCreateInfo.attachmentCount = 2;
|
|
|
|
|
renderPassCreateInfo.pAttachments = osAttachments;
|
|
|
|
|
renderPassCreateInfo.subpassCount = 1;
|
|
|
|
|
renderPassCreateInfo.pSubpasses = &subpass;
|
|
|
|
|
|
2016-08-13 20:35:22 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateRenderPass(device, &renderPassCreateInfo, nullptr, &offscreenPass.renderPass));
|
2016-05-17 15:52:33 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
void preparePipelines()
|
|
|
|
|
{
|
2019-04-14 19:38:30 +02:00
|
|
|
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
|
2020-07-28 20:20:38 +02:00
|
|
|
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);
|
2019-04-14 19:38:30 +02:00
|
|
|
VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
|
|
|
|
|
VkPipelineColorBlendStateCreateInfo colorBlendState = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
|
|
|
|
|
VkPipelineDepthStencilStateCreateInfo depthStencilState = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);
|
|
|
|
|
VkPipelineViewportStateCreateInfo viewportState = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
|
|
|
|
|
VkPipelineMultisampleStateCreateInfo multisampleState = vks::initializers::pipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT, 0);
|
|
|
|
|
std::vector<VkDynamicState> dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
|
|
|
|
|
VkPipelineDynamicStateCreateInfo dynamicState = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables.data(), dynamicStateEnables.size(), 0);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// 3D scene pipeline
|
|
|
|
|
// Load shaders
|
|
|
|
|
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
|
|
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
shaderStages[0] = loadShader(getShadersPath() + "shadowmappingomni/scene.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
|
|
|
shaderStages[1] = loadShader(getShadersPath() + "shadowmappingomni/scene.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
|
|
|
|
|
|
|
|
|
VkGraphicsPipelineCreateInfo pipelineCI = vks::initializers::pipelineCreateInfo(pipelineLayouts.scene, renderPass, 0);
|
|
|
|
|
pipelineCI.pInputAssemblyState = &inputAssemblyState;
|
|
|
|
|
pipelineCI.pRasterizationState = &rasterizationState;
|
|
|
|
|
pipelineCI.pColorBlendState = &colorBlendState;
|
|
|
|
|
pipelineCI.pMultisampleState = &multisampleState;
|
|
|
|
|
pipelineCI.pViewportState = &viewportState;
|
|
|
|
|
pipelineCI.pDepthStencilState = &depthStencilState;
|
|
|
|
|
pipelineCI.pDynamicState = &dynamicState;
|
2023-12-20 20:14:36 +01:00
|
|
|
pipelineCI.stageCount = static_cast<uint32_t>(shaderStages.size());
|
2020-07-28 20:20:38 +02:00
|
|
|
pipelineCI.pStages = shaderStages.data();
|
|
|
|
|
pipelineCI.pVertexInputState = vkglTF::Vertex::getPipelineVertexInputState({vkglTF::VertexComponent::Position, vkglTF::VertexComponent::Color, vkglTF::VertexComponent::Normal});
|
|
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.scene));
|
|
|
|
|
|
|
|
|
|
// Offscreen pipeline
|
|
|
|
|
shaderStages[0] = loadShader(getShadersPath() + "shadowmappingomni/offscreen.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
|
|
|
shaderStages[1] = loadShader(getShadersPath() + "shadowmappingomni/offscreen.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
|
|
|
|
pipelineCI.layout = pipelineLayouts.offscreen;
|
|
|
|
|
pipelineCI.renderPass = offscreenPass.renderPass;
|
|
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.offscreen));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Cube map display pipeline
|
2020-07-28 20:20:38 +02:00
|
|
|
shaderStages[0] = loadShader(getShadersPath() + "shadowmappingomni/cubemapdisplay.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
|
|
|
shaderStages[1] = loadShader(getShadersPath() + "shadowmappingomni/cubemapdisplay.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
2019-04-15 21:30:57 +02:00
|
|
|
VkPipelineVertexInputStateCreateInfo emptyInputState = vks::initializers::pipelineVertexInputStateCreateInfo();
|
2020-07-28 20:20:38 +02:00
|
|
|
pipelineCI.pVertexInputState = &emptyInputState;
|
|
|
|
|
pipelineCI.layout = pipelineLayouts.scene;
|
|
|
|
|
pipelineCI.renderPass = renderPass;
|
|
|
|
|
rasterizationState.cullMode = VK_CULL_MODE_NONE;
|
|
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.cubemapDisplay));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare and initialize uniform buffer containing shader uniforms
|
|
|
|
|
void prepareUniformBuffers()
|
|
|
|
|
{
|
2020-05-29 16:08:53 +01:00
|
|
|
// Offscreen vertex shader uniform buffer
|
2016-12-24 12:48:01 +01:00
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
2016-12-24 12:48:01 +01:00
|
|
|
&uniformBuffers.offscreen,
|
|
|
|
|
sizeof(uboOffscreenVS)));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-12-24 12:48:01 +01:00
|
|
|
// Scene vertex shader uniform buffer
|
|
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
2016-12-24 12:48:01 +01:00
|
|
|
&uniformBuffers.scene,
|
|
|
|
|
sizeof(uboVSscene)));
|
|
|
|
|
|
|
|
|
|
// Map persistent
|
|
|
|
|
VK_CHECK_RESULT(uniformBuffers.offscreen.map());
|
|
|
|
|
VK_CHECK_RESULT(uniformBuffers.scene.map());
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
updateUniformBufferOffscreen();
|
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateUniformBuffers()
|
|
|
|
|
{
|
2019-04-14 19:38:30 +02:00
|
|
|
uboVSscene.projection = camera.matrices.perspective;
|
|
|
|
|
uboVSscene.view = camera.matrices.view;
|
2017-09-24 18:17:07 +02:00
|
|
|
uboVSscene.model = glm::mat4(1.0f);
|
2016-02-16 15:07:25 +01:00
|
|
|
uboVSscene.lightPos = lightPos;
|
2016-12-24 12:48:01 +01:00
|
|
|
memcpy(uniformBuffers.scene.mapped, &uboVSscene, sizeof(uboVSscene));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateUniformBufferOffscreen()
|
|
|
|
|
{
|
2020-07-28 20:20:38 +02:00
|
|
|
lightPos.x = sin(glm::radians(timer * 360.0f)) * 0.15f;
|
|
|
|
|
lightPos.z = cos(glm::radians(timer * 360.0f)) * 0.15f;
|
2016-02-16 15:07:25 +01:00
|
|
|
uboOffscreenVS.projection = glm::perspective((float)(M_PI / 2.0), 1.0f, zNear, zFar);
|
2017-09-24 18:17:07 +02:00
|
|
|
uboOffscreenVS.view = glm::mat4(1.0f);
|
|
|
|
|
uboOffscreenVS.model = glm::translate(glm::mat4(1.0f), glm::vec3(-lightPos.x, -lightPos.y, -lightPos.z));
|
2016-02-16 15:07:25 +01:00
|
|
|
uboOffscreenVS.lightPos = lightPos;
|
2016-12-24 12:48:01 +01:00
|
|
|
memcpy(uniformBuffers.offscreen.mapped, &uboOffscreenVS, sizeof(uboOffscreenVS));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
2016-05-31 20:07:43 +02:00
|
|
|
void draw()
|
|
|
|
|
{
|
|
|
|
|
VulkanExampleBase::prepareFrame();
|
2016-06-05 16:59:01 +02:00
|
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
2016-05-31 20:07:43 +02:00
|
|
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
|
|
|
VulkanExampleBase::submitFrame();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
void prepare()
|
|
|
|
|
{
|
|
|
|
|
VulkanExampleBase::prepare();
|
2016-08-13 20:35:22 +02:00
|
|
|
loadAssets();
|
2016-02-16 15:07:25 +01:00
|
|
|
prepareUniformBuffers();
|
|
|
|
|
prepareCubeMap();
|
|
|
|
|
setupDescriptorSetLayout();
|
2016-05-17 15:52:33 +01:00
|
|
|
prepareOffscreenRenderpass();
|
2016-02-16 15:07:25 +01:00
|
|
|
preparePipelines();
|
|
|
|
|
setupDescriptorPool();
|
|
|
|
|
setupDescriptorSets();
|
|
|
|
|
prepareOffscreenFramebuffer();
|
|
|
|
|
buildCommandBuffers();
|
|
|
|
|
prepared = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void render()
|
|
|
|
|
{
|
|
|
|
|
if (!prepared)
|
|
|
|
|
return;
|
|
|
|
|
draw();
|
2019-04-14 19:38:30 +02:00
|
|
|
if (!paused || camera.updated)
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
|
|
|
|
updateUniformBufferOffscreen();
|
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-01 14:22:10 +01:00
|
|
|
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
|
2016-05-31 20:07:43 +02:00
|
|
|
{
|
2017-11-01 14:22:10 +01:00
|
|
|
if (overlay->header("Settings")) {
|
|
|
|
|
if (overlay->checkBox("Display shadow cube render target", &displayCubeMap)) {
|
|
|
|
|
buildCommandBuffers();
|
|
|
|
|
}
|
2016-05-31 20:07:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
|
|
|
|
|
2016-12-13 19:25:56 +01:00
|
|
|
VULKAN_EXAMPLE_MAIN()
|