1367 lines
No EOL
44 KiB
C++
1367 lines
No EOL
44 KiB
C++
/*
|
|
* Vulkan Example - Deferred shading multiple render targets (aka G-Buffer) example
|
|
*
|
|
* 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 <vulkan/vulkan.h>
|
|
#include "vulkanexamplebase.h"
|
|
|
|
#define VERTEX_BUFFER_BIND_ID 0
|
|
#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
|
|
|
|
// Vertex layout for this example
|
|
std::vector<vkMeshLoader::VertexLayout> vertexLayout =
|
|
{
|
|
vkMeshLoader::VERTEX_LAYOUT_POSITION,
|
|
vkMeshLoader::VERTEX_LAYOUT_UV,
|
|
vkMeshLoader::VERTEX_LAYOUT_COLOR,
|
|
vkMeshLoader::VERTEX_LAYOUT_NORMAL
|
|
};
|
|
|
|
class VulkanExample : public VulkanExampleBase
|
|
{
|
|
public:
|
|
bool debugDisplay = true;
|
|
|
|
struct {
|
|
vkTools::VulkanTexture colorMap;
|
|
} textures;
|
|
|
|
struct {
|
|
vkMeshLoader::MeshBuffer example;
|
|
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;
|
|
} uboVS, uboOffscreenVS;
|
|
|
|
struct Light {
|
|
glm::vec4 position;
|
|
glm::vec4 color;
|
|
float radius;
|
|
float quadraticFalloff;
|
|
float linearFalloff;
|
|
float _pad;
|
|
};
|
|
|
|
struct {
|
|
Light lights[5];
|
|
glm::vec4 viewPos;
|
|
} uboFragmentLights;
|
|
|
|
struct {
|
|
vkTools::UniformData vsFullScreen;
|
|
vkTools::UniformData vsOffscreen;
|
|
vkTools::UniformData fsLights;
|
|
} uniformData;
|
|
|
|
struct {
|
|
VkPipeline deferred;
|
|
VkPipeline offscreen;
|
|
VkPipeline debug;
|
|
} pipelines;
|
|
|
|
struct {
|
|
VkPipelineLayout deferred;
|
|
VkPipelineLayout offscreen;
|
|
} pipelineLayouts;
|
|
|
|
struct {
|
|
VkDescriptorSet offscreen;
|
|
} descriptorSets;
|
|
|
|
VkDescriptorSet descriptorSet;
|
|
VkDescriptorSetLayout descriptorSetLayout;
|
|
|
|
// Framebuffer for offscreen rendering
|
|
struct FrameBufferAttachment {
|
|
VkImage image;
|
|
VkDeviceMemory mem;
|
|
VkImageView view;
|
|
VkFormat format;
|
|
};
|
|
struct FrameBuffer {
|
|
int32_t width, height;
|
|
VkFramebuffer frameBuffer;
|
|
FrameBufferAttachment position, normal, albedo;
|
|
FrameBufferAttachment depth;
|
|
VkRenderPass renderPass;
|
|
} offScreenFrameBuf;
|
|
|
|
// Texture targets
|
|
struct {
|
|
vkTools::VulkanTexture position;
|
|
vkTools::VulkanTexture normal;
|
|
vkTools::VulkanTexture albedo;
|
|
} textureTargets;
|
|
|
|
VkCommandBuffer offScreenCmdBuffer = VK_NULL_HANDLE;
|
|
|
|
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
|
{
|
|
zoom = -8.0f;
|
|
rotation = { 0.0f, 0.0f, 0.0f };
|
|
width = 1024;
|
|
height = 1024;
|
|
title = "Vulkan Example - Deferred shading";
|
|
}
|
|
|
|
~VulkanExample()
|
|
{
|
|
// Clean up used Vulkan resources
|
|
// Note : Inherited destructor cleans up resources stored in base class
|
|
|
|
// Texture targets
|
|
textureLoader->destroyTexture(textureTargets.position);
|
|
textureLoader->destroyTexture(textureTargets.normal);
|
|
textureLoader->destroyTexture(textureTargets.albedo);
|
|
|
|
// Frame buffer
|
|
|
|
// Color attachments
|
|
vkDestroyImageView(device, offScreenFrameBuf.position.view, nullptr);
|
|
vkDestroyImage(device, offScreenFrameBuf.position.image, nullptr);
|
|
vkFreeMemory(device, offScreenFrameBuf.position.mem, nullptr);
|
|
|
|
vkDestroyImageView(device, offScreenFrameBuf.normal.view, nullptr);
|
|
vkDestroyImage(device, offScreenFrameBuf.normal.image, nullptr);
|
|
vkFreeMemory(device, offScreenFrameBuf.normal.mem, nullptr);
|
|
|
|
vkDestroyImageView(device, offScreenFrameBuf.albedo.view, nullptr);
|
|
vkDestroyImage(device, offScreenFrameBuf.albedo.image, nullptr);
|
|
vkFreeMemory(device, offScreenFrameBuf.albedo.mem, nullptr);
|
|
|
|
// Depth attachment
|
|
vkDestroyImageView(device, offScreenFrameBuf.depth.view, nullptr);
|
|
vkDestroyImage(device, offScreenFrameBuf.depth.image, nullptr);
|
|
vkFreeMemory(device, offScreenFrameBuf.depth.mem, nullptr);
|
|
|
|
vkDestroyFramebuffer(device, offScreenFrameBuf.frameBuffer, nullptr);
|
|
|
|
vkDestroyPipeline(device, pipelines.deferred, nullptr);
|
|
vkDestroyPipeline(device, pipelines.offscreen, 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.example);
|
|
vkMeshLoader::freeMeshBufferResources(device, &meshes.quad);
|
|
|
|
// Uniform buffers
|
|
vkTools::destroyUniformData(device, &uniformData.vsOffscreen);
|
|
vkTools::destroyUniformData(device, &uniformData.vsFullScreen);
|
|
vkTools::destroyUniformData(device, &uniformData.fsLights);
|
|
|
|
vkFreeCommandBuffers(device, cmdPool, 1, &offScreenCmdBuffer);
|
|
|
|
vkDestroyRenderPass(device, offScreenFrameBuf.renderPass, nullptr);
|
|
|
|
textureLoader->destroyTexture(textures.colorMap);
|
|
}
|
|
|
|
// Preapre an empty texture as the blit target from
|
|
// the offscreen framebuffer
|
|
void prepareTextureTarget(vkTools::VulkanTexture *target, VkFormat format)
|
|
{
|
|
VkFormatProperties formatProperties;
|
|
VkResult err;
|
|
|
|
uint32_t width = TEX_DIM;
|
|
uint32_t height = TEX_DIM;
|
|
|
|
// Prepare blit target texture
|
|
target->width = width;
|
|
target->height = height;
|
|
|
|
VkImageCreateInfo imageCreateInfo = vkTools::initializers::imageCreateInfo();
|
|
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
|
|
imageCreateInfo.format = format;
|
|
imageCreateInfo.extent = { width, height, 1 };
|
|
imageCreateInfo.mipLevels = 1;
|
|
imageCreateInfo.arrayLayers = 1;
|
|
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
|
// Texture will be sampled in a shader and is also the blit destination
|
|
imageCreateInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
|
imageCreateInfo.flags = 0;
|
|
|
|
VkMemoryAllocateInfo memAllocInfo = vkTools::initializers::memoryAllocateInfo();
|
|
VkMemoryRequirements memReqs;
|
|
|
|
err = vkCreateImage(device, &imageCreateInfo, nullptr, &target->image);
|
|
assert(!err);
|
|
vkGetImageMemoryRequirements(device, target->image, &memReqs);
|
|
memAllocInfo.allocationSize = memReqs.size;
|
|
getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &memAllocInfo.memoryTypeIndex);
|
|
err = vkAllocateMemory(device, &memAllocInfo, nullptr, &target->deviceMemory);
|
|
assert(!err);
|
|
err = vkBindImageMemory(device, target->image, target->deviceMemory, 0);
|
|
assert(!err);
|
|
|
|
// Image memory barrier
|
|
// Set initial layout for the offscreen texture to shader read
|
|
// Will be transformed while updating the texture
|
|
textureTargets.position.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
vkTools::setImageLayout(
|
|
setupCmdBuffer,
|
|
target->image,
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
|
textureTargets.position.imageLayout);
|
|
|
|
// Create sampler
|
|
VkSamplerCreateInfo sampler = vkTools::initializers::samplerCreateInfo();
|
|
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.addressModeV;
|
|
sampler.addressModeW = sampler.addressModeV;
|
|
sampler.mipLodBias = 0.0f;
|
|
sampler.maxAnisotropy = 0;
|
|
sampler.compareOp = VK_COMPARE_OP_NEVER;
|
|
sampler.minLod = 0.0f;
|
|
sampler.maxLod = 0.0f;
|
|
sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
|
|
err = vkCreateSampler(device, &sampler, nullptr, &target->sampler);
|
|
assert(!err);
|
|
|
|
// Create image view
|
|
VkImageViewCreateInfo view = {};
|
|
view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
|
view.pNext = NULL;
|
|
view.image = VK_NULL_HANDLE;
|
|
view.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
view.format = format;
|
|
view.components = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A };
|
|
view.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
|
|
view.image = target->image;
|
|
err = vkCreateImageView(device, &view, nullptr, &target->view);
|
|
assert(!err);
|
|
}
|
|
|
|
void prepareTextureTargets()
|
|
{
|
|
createSetupCommandBuffer();
|
|
|
|
prepareTextureTarget(&textureTargets.position, VK_FORMAT_R16G16B16A16_SFLOAT);
|
|
prepareTextureTarget(&textureTargets.normal, VK_FORMAT_R16G16B16A16_SFLOAT);
|
|
prepareTextureTarget(&textureTargets.albedo, VK_FORMAT_R8G8B8A8_UNORM);
|
|
|
|
flushSetupCommandBuffer();
|
|
}
|
|
|
|
// Create a frame buffer attachment
|
|
void createAttachment(
|
|
VkFormat format,
|
|
VkImageUsageFlagBits usage,
|
|
FrameBufferAttachment *attachment)
|
|
{
|
|
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 = 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 = offScreenFrameBuf.width;
|
|
image.extent.height = offScreenFrameBuf.height;
|
|
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_TRANSFER_SRC_BIT;
|
|
|
|
VkMemoryAllocateInfo memAlloc = vkTools::initializers::memoryAllocateInfo();
|
|
|
|
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;
|
|
|
|
VkMemoryRequirements memReqs;
|
|
|
|
VkResult err = vkCreateImage(device, &image, nullptr, &attachment->image);
|
|
assert(!err);
|
|
vkGetImageMemoryRequirements(device, attachment->image, &memReqs);
|
|
memAlloc.allocationSize = memReqs.size;
|
|
getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &memAlloc.memoryTypeIndex);
|
|
err = vkAllocateMemory(device, &memAlloc, nullptr, &attachment->mem);
|
|
assert(!err);
|
|
|
|
err = vkBindImageMemory(device, attachment->image, attachment->mem, 0);
|
|
assert(!err);
|
|
|
|
vkTools::setImageLayout(
|
|
setupCmdBuffer,
|
|
attachment->image,
|
|
aspectMask,
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
|
imageLayout);
|
|
|
|
imageView.image = attachment->image;
|
|
err = vkCreateImageView(device, &imageView, nullptr, &attachment->view);
|
|
assert(!err);
|
|
}
|
|
|
|
// Prepare a new framebuffer for offscreen rendering
|
|
// The contents of this framebuffer are then
|
|
// blitted to our render target
|
|
void prepareOffscreenFramebuffer()
|
|
{
|
|
offScreenFrameBuf.width = FB_DIM;
|
|
offScreenFrameBuf.height = FB_DIM;
|
|
|
|
VkResult err;
|
|
|
|
// Color attachments
|
|
|
|
// (World space) Positions
|
|
createAttachment(
|
|
VK_FORMAT_R16G16B16A16_SFLOAT,
|
|
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
|
&offScreenFrameBuf.position);
|
|
|
|
// (World space) Normals
|
|
createAttachment(
|
|
VK_FORMAT_R16G16B16A16_SFLOAT,
|
|
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
|
&offScreenFrameBuf.normal);
|
|
|
|
// Albedo (color)
|
|
createAttachment(
|
|
VK_FORMAT_R8G8B8A8_UNORM,
|
|
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
|
&offScreenFrameBuf.albedo);
|
|
|
|
// 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,
|
|
&offScreenFrameBuf.depth);
|
|
|
|
// 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;
|
|
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;
|
|
}
|
|
}
|
|
|
|
// Formats
|
|
attachmentDescs[0].format = offScreenFrameBuf.position.format;
|
|
attachmentDescs[1].format = offScreenFrameBuf.normal.format;
|
|
attachmentDescs[2].format = offScreenFrameBuf.albedo.format;
|
|
attachmentDescs[3].format = offScreenFrameBuf.depth.format;
|
|
|
|
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 = colorReferences.size();
|
|
subpass.pDepthStencilAttachment = &depthReference;
|
|
|
|
VkRenderPassCreateInfo renderPassInfo = {};
|
|
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
|
renderPassInfo.pAttachments = attachmentDescs.data();
|
|
renderPassInfo.attachmentCount = attachmentDescs.size();
|
|
renderPassInfo.subpassCount = 1;
|
|
renderPassInfo.pSubpasses = &subpass;
|
|
|
|
err = vkCreateRenderPass(device, &renderPassInfo, nullptr, &offScreenFrameBuf.renderPass);
|
|
assert(!err);
|
|
|
|
std::array<VkImageView,4> attachments;
|
|
attachments[0] = offScreenFrameBuf.position.view;
|
|
attachments[1] = offScreenFrameBuf.normal.view;
|
|
attachments[2] = offScreenFrameBuf.albedo.view;
|
|
// depth
|
|
attachments[3] = offScreenFrameBuf.depth.view;
|
|
|
|
VkFramebufferCreateInfo fbufCreateInfo = {};
|
|
fbufCreateInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
|
fbufCreateInfo.pNext = NULL;
|
|
fbufCreateInfo.renderPass = offScreenFrameBuf.renderPass;
|
|
fbufCreateInfo.pAttachments = attachments.data();
|
|
fbufCreateInfo.attachmentCount = attachments.size();
|
|
fbufCreateInfo.width = offScreenFrameBuf.width;
|
|
fbufCreateInfo.height = offScreenFrameBuf.height;
|
|
fbufCreateInfo.layers = 1;
|
|
|
|
err = vkCreateFramebuffer(device, &fbufCreateInfo, nullptr, &offScreenFrameBuf.frameBuffer);
|
|
assert(!err);
|
|
|
|
flushSetupCommandBuffer();
|
|
createSetupCommandBuffer();
|
|
}
|
|
|
|
// Blit frame buffer attachment to texture target
|
|
void blit(VkImage source, VkImage dest)
|
|
{
|
|
// Image memory barrier
|
|
// Transform frame buffer color attachment to transfer source layout
|
|
// Makes sure that writes to the color attachment are finished before
|
|
// using it as source for the blit
|
|
vkTools::setImageLayout(
|
|
offScreenCmdBuffer,
|
|
source,
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
|
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
|
|
|
|
// Image memory barrier
|
|
// Transform texture from shader read (initial layout) to transfer destination layout
|
|
// Makes sure that reads from texture are finished before
|
|
// using it as a transfer destination for the blit
|
|
vkTools::setImageLayout(
|
|
offScreenCmdBuffer,
|
|
dest,
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
|
|
|
|
// Blit offscreen color buffer to our texture target
|
|
VkImageBlit imgBlit;
|
|
|
|
imgBlit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
imgBlit.srcSubresource.mipLevel = 0;
|
|
imgBlit.srcSubresource.baseArrayLayer = 0;
|
|
imgBlit.srcSubresource.layerCount = 1;
|
|
|
|
imgBlit.srcOffsets[0] = { 0, 0, 0 };
|
|
imgBlit.srcOffsets[1].x = offScreenFrameBuf.width;
|
|
imgBlit.srcOffsets[1].y = offScreenFrameBuf.height;
|
|
imgBlit.srcOffsets[1].z = 1;
|
|
|
|
imgBlit.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
imgBlit.dstSubresource.mipLevel = 0;
|
|
imgBlit.dstSubresource.baseArrayLayer = 0;
|
|
imgBlit.dstSubresource.layerCount = 1;
|
|
|
|
imgBlit.dstOffsets[0] = { 0, 0, 0 };
|
|
imgBlit.dstOffsets[1].x = textureTargets.position.width;
|
|
imgBlit.dstOffsets[1].y = textureTargets.position.height;
|
|
imgBlit.dstOffsets[1].z = 1;
|
|
|
|
// Blit from framebuffer image to texture image
|
|
// vkCmdBlitImage does scaling and (if necessary and possible) also does format conversions
|
|
vkCmdBlitImage(
|
|
offScreenCmdBuffer,
|
|
source,
|
|
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
|
dest,
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
1,
|
|
&imgBlit,
|
|
VK_FILTER_LINEAR
|
|
);
|
|
|
|
// Image memory barrier
|
|
// Transform texture from transfer destination to shader read
|
|
// Makes sure that writes to the texture are finished before
|
|
// using it as the source for a sampler in the shader
|
|
vkTools::setImageLayout(
|
|
offScreenCmdBuffer,
|
|
dest,
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
|
|
|
// Image memory barrier
|
|
// Transform the framebuffer color attachment back
|
|
vkTools::setImageLayout(
|
|
offScreenCmdBuffer,
|
|
source,
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
|
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
|
}
|
|
|
|
// Build command buffer for rendering the scene to the offscreen frame buffer
|
|
// and blitting it to the different texture targets
|
|
void buildDeferredCommandBuffer()
|
|
{
|
|
VkResult err;
|
|
|
|
// Create separate command buffer for offscreen
|
|
// rendering
|
|
if (offScreenCmdBuffer == VK_NULL_HANDLE)
|
|
{
|
|
VkCommandBufferAllocateInfo cmd = vkTools::initializers::commandBufferAllocateInfo(
|
|
cmdPool,
|
|
VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
|
1);
|
|
VkResult vkRes = vkAllocateCommandBuffers(device, &cmd, &offScreenCmdBuffer);
|
|
assert(!vkRes);
|
|
}
|
|
|
|
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
|
|
|
|
// Clear values for all attachments written in the fragment sahder
|
|
std::array<VkClearValue,4> clearValues;
|
|
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 };
|
|
|
|
VkRenderPassBeginInfo renderPassBeginInfo = vkTools::initializers::renderPassBeginInfo();
|
|
renderPassBeginInfo.renderPass = offScreenFrameBuf.renderPass;
|
|
renderPassBeginInfo.framebuffer = offScreenFrameBuf.frameBuffer;
|
|
renderPassBeginInfo.renderArea.extent.width = offScreenFrameBuf.width;
|
|
renderPassBeginInfo.renderArea.extent.height = offScreenFrameBuf.height;
|
|
renderPassBeginInfo.clearValueCount = clearValues.size();
|
|
renderPassBeginInfo.pClearValues = clearValues.data();
|
|
|
|
err = vkBeginCommandBuffer(offScreenCmdBuffer, &cmdBufInfo);
|
|
assert(!err);
|
|
|
|
vkCmdBeginRenderPass(offScreenCmdBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
VkViewport viewport = vkTools::initializers::viewport(
|
|
(float)offScreenFrameBuf.width,
|
|
(float)offScreenFrameBuf.height,
|
|
0.0f,
|
|
1.0f);
|
|
vkCmdSetViewport(offScreenCmdBuffer, 0, 1, &viewport);
|
|
|
|
VkRect2D scissor = vkTools::initializers::rect2D(
|
|
offScreenFrameBuf.width,
|
|
offScreenFrameBuf.height,
|
|
0,
|
|
0);
|
|
vkCmdSetScissor(offScreenCmdBuffer, 0, 1, &scissor);
|
|
|
|
vkCmdBindDescriptorSets(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.offscreen, 0, 1, &descriptorSets.offscreen, 0, NULL);
|
|
vkCmdBindPipeline(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.offscreen);
|
|
|
|
VkDeviceSize offsets[1] = { 0 };
|
|
vkCmdBindVertexBuffers(offScreenCmdBuffer, VERTEX_BUFFER_BIND_ID, 1, &meshes.example.vertices.buf, offsets);
|
|
vkCmdBindIndexBuffer(offScreenCmdBuffer, meshes.example.indices.buf, 0, VK_INDEX_TYPE_UINT32);
|
|
vkCmdDrawIndexed(offScreenCmdBuffer, meshes.example.indexCount, 1, 0, 0, 0);
|
|
|
|
vkCmdEndRenderPass(offScreenCmdBuffer);
|
|
|
|
blit(offScreenFrameBuf.position.image, textureTargets.position.image);
|
|
blit(offScreenFrameBuf.normal.image, textureTargets.normal.image);
|
|
blit(offScreenFrameBuf.albedo.image, textureTargets.albedo.image);
|
|
|
|
err = vkEndCommandBuffer(offScreenCmdBuffer);
|
|
assert(!err);
|
|
}
|
|
|
|
void loadTextures()
|
|
{
|
|
textureLoader->loadTexture(
|
|
getAssetPath() + "models/armor/colormap.ktx",
|
|
VK_FORMAT_BC3_UNORM_BLOCK,
|
|
&textures.colorMap);
|
|
}
|
|
|
|
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;
|
|
|
|
VkResult err;
|
|
|
|
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
|
|
{
|
|
// Set target frame buffer
|
|
renderPassBeginInfo.framebuffer = frameBuffers[i];
|
|
|
|
err = vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo);
|
|
assert(!err);
|
|
|
|
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]);
|
|
|
|
err = vkEndCommandBuffer(drawCmdBuffers[i]);
|
|
assert(!err);
|
|
}
|
|
}
|
|
|
|
void draw()
|
|
{
|
|
VkResult err;
|
|
|
|
// Get next image in the swap chain (back/front buffer)
|
|
err = swapChain.acquireNextImage(semaphores.presentComplete, ¤tBuffer);
|
|
assert(!err);
|
|
|
|
submitPostPresentBarrier(swapChain.buffers[currentBuffer].image);
|
|
|
|
// Gather command buffers to be sumitted to the queue
|
|
std::vector<VkCommandBuffer> submitCmdBuffers = {
|
|
offScreenCmdBuffer,
|
|
drawCmdBuffers[currentBuffer],
|
|
};
|
|
submitInfo.commandBufferCount = submitCmdBuffers.size();
|
|
submitInfo.pCommandBuffers = submitCmdBuffers.data();
|
|
|
|
// Submit to queue
|
|
err = vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE);
|
|
assert(!err);
|
|
|
|
submitPrePresentBarrier(swapChain.buffers[currentBuffer].image);
|
|
|
|
err = swapChain.queuePresent(queue, currentBuffer, semaphores.renderComplete);
|
|
assert(!err);
|
|
|
|
err = vkQueueWaitIdle(queue);
|
|
assert(!err);
|
|
}
|
|
|
|
void loadMeshes()
|
|
{
|
|
loadMesh(getAssetPath() + "models/armor/armor.dae", &meshes.example, vertexLayout, 1.0f);
|
|
}
|
|
|
|
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];
|
|
};
|
|
|
|
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 = 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.resize(4);
|
|
// Location 0 : Position
|
|
vertices.attributeDescriptions[0] =
|
|
vkTools::initializers::vertexInputAttributeDescription(
|
|
VERTEX_BUFFER_BIND_ID,
|
|
0,
|
|
VK_FORMAT_R32G32B32_SFLOAT,
|
|
0);
|
|
// Location 1 : Texture coordinates
|
|
vertices.attributeDescriptions[1] =
|
|
vkTools::initializers::vertexInputAttributeDescription(
|
|
VERTEX_BUFFER_BIND_ID,
|
|
1,
|
|
VK_FORMAT_R32G32_SFLOAT,
|
|
sizeof(float) * 3);
|
|
// Location 2 : Color
|
|
vertices.attributeDescriptions[2] =
|
|
vkTools::initializers::vertexInputAttributeDescription(
|
|
VERTEX_BUFFER_BIND_ID,
|
|
2,
|
|
VK_FORMAT_R32G32B32_SFLOAT,
|
|
sizeof(float) * 5);
|
|
// Location 3 : Normal
|
|
vertices.attributeDescriptions[3] =
|
|
vkTools::initializers::vertexInputAttributeDescription(
|
|
VERTEX_BUFFER_BIND_ID,
|
|
3,
|
|
VK_FORMAT_R32G32B32_SFLOAT,
|
|
sizeof(float) * 8);
|
|
|
|
vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo();
|
|
vertices.inputState.vertexBindingDescriptionCount = vertices.bindingDescriptions.size();
|
|
vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data();
|
|
vertices.inputState.vertexAttributeDescriptionCount = vertices.attributeDescriptions.size();
|
|
vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data();
|
|
}
|
|
|
|
void setupDescriptorPool()
|
|
{
|
|
std::vector<VkDescriptorPoolSize> poolSizes =
|
|
{
|
|
vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 8),
|
|
vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 8)
|
|
};
|
|
|
|
VkDescriptorPoolCreateInfo descriptorPoolInfo =
|
|
vkTools::initializers::descriptorPoolCreateInfo(
|
|
poolSizes.size(),
|
|
poolSizes.data(),
|
|
2);
|
|
|
|
VkResult vkRes = vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool);
|
|
assert(!vkRes);
|
|
}
|
|
|
|
void setupDescriptorSetLayout()
|
|
{
|
|
// Deferred shading layout
|
|
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings =
|
|
{
|
|
// Binding 0 : Vertex shader uniform buffer
|
|
vkTools::initializers::descriptorSetLayoutBinding(
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
VK_SHADER_STAGE_VERTEX_BIT,
|
|
0),
|
|
// Binding 1 : Position texture target / Scene colormap
|
|
vkTools::initializers::descriptorSetLayoutBinding(
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
1),
|
|
// Binding 2 : Normals texture target
|
|
vkTools::initializers::descriptorSetLayoutBinding(
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
2),
|
|
// Binding 3 : Albedo texture target
|
|
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),
|
|
};
|
|
|
|
VkDescriptorSetLayoutCreateInfo descriptorLayout =
|
|
vkTools::initializers::descriptorSetLayoutCreateInfo(
|
|
setLayoutBindings.data(),
|
|
setLayoutBindings.size());
|
|
|
|
VkResult err = vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout);
|
|
assert(!err);
|
|
|
|
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
|
vkTools::initializers::pipelineLayoutCreateInfo(
|
|
&descriptorSetLayout,
|
|
1);
|
|
|
|
err = vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.deferred);
|
|
assert(!err);
|
|
|
|
// Offscreen (scene) rendering pipeline layout
|
|
err = vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.offscreen);
|
|
assert(!err);
|
|
}
|
|
|
|
void setupDescriptorSet()
|
|
{
|
|
// Textured quad descriptor set
|
|
VkDescriptorSetAllocateInfo allocInfo =
|
|
vkTools::initializers::descriptorSetAllocateInfo(
|
|
descriptorPool,
|
|
&descriptorSetLayout,
|
|
1);
|
|
|
|
VkResult vkRes = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet);
|
|
assert(!vkRes);
|
|
|
|
// Image descriptor for the offscreen texture targets
|
|
VkDescriptorImageInfo texDescriptorPosition =
|
|
vkTools::initializers::descriptorImageInfo(
|
|
textureTargets.position.sampler,
|
|
textureTargets.position.view,
|
|
VK_IMAGE_LAYOUT_GENERAL);
|
|
|
|
VkDescriptorImageInfo texDescriptorNormal =
|
|
vkTools::initializers::descriptorImageInfo(
|
|
textureTargets.normal.sampler,
|
|
textureTargets.normal.view,
|
|
VK_IMAGE_LAYOUT_GENERAL);
|
|
|
|
VkDescriptorImageInfo texDescriptorAlbedo =
|
|
vkTools::initializers::descriptorImageInfo(
|
|
textureTargets.albedo.sampler,
|
|
textureTargets.albedo.view,
|
|
VK_IMAGE_LAYOUT_GENERAL);
|
|
|
|
std::vector<VkWriteDescriptorSet> writeDescriptorSets =
|
|
{
|
|
// Binding 0 : Vertex shader uniform buffer
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSet,
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
0,
|
|
&uniformData.vsFullScreen.descriptor),
|
|
// Binding 1 : Position texture target
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSet,
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
1,
|
|
&texDescriptorPosition),
|
|
// Binding 2 : Normals texture target
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSet,
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
2,
|
|
&texDescriptorNormal),
|
|
// Binding 3 : Albedo texture target
|
|
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),
|
|
};
|
|
|
|
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL);
|
|
|
|
// Offscreen (scene)
|
|
vkRes = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.offscreen);
|
|
assert(!vkRes);
|
|
|
|
VkDescriptorImageInfo texDescriptorSceneColormap =
|
|
vkTools::initializers::descriptorImageInfo(
|
|
textures.colorMap.sampler,
|
|
textures.colorMap.view,
|
|
VK_IMAGE_LAYOUT_GENERAL);
|
|
|
|
std::vector<VkWriteDescriptorSet> offScreenWriteDescriptorSets =
|
|
{
|
|
// Binding 0 : Vertex shader uniform buffer
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSets.offscreen,
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
0,
|
|
&uniformData.vsOffscreen.descriptor),
|
|
// Binding 1 : Scene color map
|
|
vkTools::initializers::writeDescriptorSet(
|
|
descriptorSets.offscreen,
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
1,
|
|
&texDescriptorSceneColormap)
|
|
};
|
|
vkUpdateDescriptorSets(device, offScreenWriteDescriptorSets.size(), offScreenWriteDescriptorSets.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_NONE,
|
|
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(),
|
|
dynamicStateEnables.size(),
|
|
0);
|
|
|
|
// Final fullscreen pass pipeline
|
|
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
|
|
|
|
shaderStages[0] = loadShader(getAssetPath() + "shaders/deferred/deferred.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
shaderStages[1] = loadShader(getAssetPath() + "shaders/deferred/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 = shaderStages.size();
|
|
pipelineCreateInfo.pStages = shaderStages.data();
|
|
|
|
VkResult err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.deferred);
|
|
assert(!err);
|
|
|
|
// Debug display pipeline
|
|
shaderStages[0] = loadShader(getAssetPath() + "shaders/deferred/debug.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
shaderStages[1] = loadShader(getAssetPath() + "shaders/deferred/debug.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
|
err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.debug);
|
|
assert(!err);
|
|
|
|
// Offscreen pipeline
|
|
shaderStages[0] = loadShader(getAssetPath() + "shaders/deferred/mrt.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
shaderStages[1] = loadShader(getAssetPath() + "shaders/deferred/mrt.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
|
|
|
// Separate render pass
|
|
pipelineCreateInfo.renderPass = offScreenFrameBuf.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 = blendAttachmentStates.size();
|
|
colorBlendState.pAttachments = blendAttachmentStates.data();
|
|
|
|
err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.offscreen);
|
|
assert(!err);
|
|
|
|
}
|
|
|
|
// Prepare and initialize uniform buffer containing shader uniforms
|
|
void prepareUniformBuffers()
|
|
{
|
|
// Fullscreen vertex shader
|
|
createBuffer(
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
|
sizeof(uboVS),
|
|
&uboVS,
|
|
&uniformData.vsFullScreen.buffer,
|
|
&uniformData.vsFullScreen.memory,
|
|
&uniformData.vsFullScreen.descriptor);
|
|
|
|
// Deferred vertex shader
|
|
createBuffer(
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
|
sizeof(uboOffscreenVS),
|
|
&uboOffscreenVS,
|
|
&uniformData.vsOffscreen.buffer,
|
|
&uniformData.vsOffscreen.memory,
|
|
&uniformData.vsOffscreen.descriptor);
|
|
|
|
// Deferred fragment shader
|
|
createBuffer(
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
|
sizeof(uboFragmentLights),
|
|
&uboFragmentLights,
|
|
&uniformData.fsLights.buffer,
|
|
&uniformData.fsLights.memory,
|
|
&uniformData.fsLights.descriptor);
|
|
|
|
// 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;
|
|
VkResult err = vkMapMemory(device, uniformData.vsFullScreen.memory, 0, sizeof(uboVS), 0, (void **)&pData);
|
|
assert(!err);
|
|
memcpy(pData, &uboVS, sizeof(uboVS));
|
|
vkUnmapMemory(device, uniformData.vsFullScreen.memory);
|
|
}
|
|
|
|
void updateUniformBufferDeferredMatrices()
|
|
{
|
|
uboOffscreenVS.projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 256.0f);
|
|
uboOffscreenVS.view = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom));
|
|
uboOffscreenVS.view = glm::rotate(uboOffscreenVS.view, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
|
|
uboOffscreenVS.view = glm::rotate(uboOffscreenVS.view, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
uboOffscreenVS.view = glm::rotate(uboOffscreenVS.view, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
|
|
|
|
uboOffscreenVS.model = glm::mat4();
|
|
uboOffscreenVS.model = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.25f, 0.0f));
|
|
|
|
uint8_t *pData;
|
|
VkResult err = vkMapMemory(device, uniformData.vsOffscreen.memory, 0, sizeof(uboOffscreenVS), 0, (void **)&pData);
|
|
assert(!err);
|
|
memcpy(pData, &uboOffscreenVS, sizeof(uboOffscreenVS));
|
|
vkUnmapMemory(device, uniformData.vsOffscreen.memory);
|
|
}
|
|
|
|
// Update fragment shader light position uniform block
|
|
void updateUniformBufferDeferredLights()
|
|
{
|
|
// White light from above
|
|
uboFragmentLights.lights[0].position = glm::vec4(0.0f, 3.0f, 1.0f, 0.0f);
|
|
uboFragmentLights.lights[0].color = glm::vec4(1.5f);
|
|
uboFragmentLights.lights[0].radius = 15.0f;
|
|
uboFragmentLights.lights[0].linearFalloff = 0.3f;
|
|
uboFragmentLights.lights[0].quadraticFalloff = 0.4f;
|
|
// Red light
|
|
uboFragmentLights.lights[1].position = glm::vec4(-2.0f, 0.0f, 0.0f, 0.0f);
|
|
uboFragmentLights.lights[1].color = glm::vec4(1.5f, 0.0f, 0.0f, 0.0f);
|
|
uboFragmentLights.lights[1].radius = 15.0f;
|
|
uboFragmentLights.lights[1].linearFalloff = 0.4f;
|
|
uboFragmentLights.lights[1].quadraticFalloff = 0.3f;
|
|
// Blue light
|
|
uboFragmentLights.lights[2].position = glm::vec4(2.0f, 1.0f, 0.0f, 0.0f);
|
|
uboFragmentLights.lights[2].color = glm::vec4(0.0f, 0.0f, 2.5f, 0.0f);
|
|
uboFragmentLights.lights[2].radius = 10.0f;
|
|
uboFragmentLights.lights[2].linearFalloff = 0.45f;
|
|
uboFragmentLights.lights[2].quadraticFalloff = 0.35f;
|
|
// Belt glow
|
|
uboFragmentLights.lights[3].position = glm::vec4(0.0f, 0.7f, 0.5f, 0.0f);
|
|
uboFragmentLights.lights[3].color = glm::vec4(2.5f, 2.5f, 0.0f, 0.0f);
|
|
uboFragmentLights.lights[3].radius = 5.0f;
|
|
uboFragmentLights.lights[3].linearFalloff = 8.0f;
|
|
uboFragmentLights.lights[3].quadraticFalloff = 6.0f;
|
|
// Green light
|
|
uboFragmentLights.lights[4].position = glm::vec4(3.0f, 2.0f, 1.0f, 0.0f);
|
|
uboFragmentLights.lights[4].color = glm::vec4(0.0f, 1.5f, 0.0f, 0.0f);
|
|
uboFragmentLights.lights[4].radius = 10.0f;
|
|
uboFragmentLights.lights[4].linearFalloff = 0.8f;
|
|
uboFragmentLights.lights[4].quadraticFalloff = 0.6f;
|
|
|
|
// Current view position
|
|
uboFragmentLights.viewPos = glm::vec4(0.0f, 0.0f, -zoom, 0.0f);
|
|
|
|
uint8_t *pData;
|
|
VkResult err = vkMapMemory(device, uniformData.fsLights.memory, 0, sizeof(uboFragmentLights), 0, (void **)&pData);
|
|
assert(!err);
|
|
memcpy(pData, &uboFragmentLights, sizeof(uboFragmentLights));
|
|
vkUnmapMemory(device, uniformData.fsLights.memory);
|
|
}
|
|
|
|
|
|
void prepare()
|
|
{
|
|
VulkanExampleBase::prepare();
|
|
loadTextures();
|
|
generateQuads();
|
|
loadMeshes();
|
|
setupVertexDescriptions();
|
|
prepareOffscreenFramebuffer();
|
|
prepareUniformBuffers();
|
|
prepareTextureTargets();
|
|
setupDescriptorSetLayout();
|
|
preparePipelines();
|
|
setupDescriptorPool();
|
|
setupDescriptorSet();
|
|
buildCommandBuffers();
|
|
buildDeferredCommandBuffer();
|
|
prepared = true;
|
|
}
|
|
|
|
virtual void render()
|
|
{
|
|
if (!prepared)
|
|
return;
|
|
vkDeviceWaitIdle(device);
|
|
draw();
|
|
vkDeviceWaitIdle(device);
|
|
}
|
|
|
|
virtual void viewChanged()
|
|
{
|
|
updateUniformBufferDeferredMatrices();
|
|
}
|
|
|
|
void toggleDebugDisplay()
|
|
{
|
|
debugDisplay = !debugDisplay;
|
|
reBuildCommandBuffers();
|
|
updateUniformBuffersScreen();
|
|
}
|
|
};
|
|
|
|
VulkanExample *vulkanExample;
|
|
|
|
#if defined(_WIN32)
|
|
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
{
|
|
if (vulkanExample != NULL)
|
|
{
|
|
vulkanExample->handleMessages(hWnd, uMsg, wParam, lParam);
|
|
if (uMsg == WM_KEYDOWN)
|
|
{
|
|
switch (wParam)
|
|
{
|
|
case 0x44:
|
|
vulkanExample->toggleDebugDisplay();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
|
|
}
|
|
#elif defined(__linux__) && !defined(__ANDROID__)
|
|
static void handleEvent(const xcb_generic_event_t *event)
|
|
{
|
|
if (vulkanExample != NULL)
|
|
{
|
|
vulkanExample->handleEvent(event);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
// Main entry point
|
|
#if defined(_WIN32)
|
|
// Windows entry point
|
|
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow)
|
|
#elif defined(__ANDROID__)
|
|
// Android entry point
|
|
void android_main(android_app* state)
|
|
#elif defined(__linux__)
|
|
// Linux entry point
|
|
int main(const int argc, const char *argv[])
|
|
#endif
|
|
{
|
|
#if defined(__ANDROID__)
|
|
// Removing this may cause the compiler to omit the main entry point
|
|
// which would make the application crash at start
|
|
app_dummy();
|
|
#endif
|
|
vulkanExample = new VulkanExample();
|
|
#if defined(_WIN32)
|
|
vulkanExample->setupWindow(hInstance, WndProc);
|
|
#elif defined(__ANDROID__)
|
|
// Attach vulkan example to global android application state
|
|
state->userData = vulkanExample;
|
|
state->onAppCmd = VulkanExample::handleAppCommand;
|
|
state->onInputEvent = VulkanExample::handleAppInput;
|
|
vulkanExample->androidApp = state;
|
|
#elif defined(__linux__)
|
|
vulkanExample->setupWindow();
|
|
#endif
|
|
#if !defined(__ANDROID__)
|
|
vulkanExample->initSwapchain();
|
|
vulkanExample->prepare();
|
|
#endif
|
|
vulkanExample->renderLoop();
|
|
delete(vulkanExample);
|
|
#if !defined(__ANDROID__)
|
|
return 0;
|
|
#endif
|
|
} |