2016-02-16 15:07:25 +01:00
|
|
|
/*
|
2016-07-03 21:32:35 +02:00
|
|
|
* Vulkan Example - Deferred shading with multiple render targets (aka G-Buffer) example
|
2016-02-16 15:07:25 +01:00
|
|
|
*
|
|
|
|
|
* 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
|
2016-03-08 21:52:40 +01:00
|
|
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
2016-02-16 15:07:25 +01:00
|
|
|
#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
|
2016-07-03 21:32:35 +02:00
|
|
|
#define TEX_DIM 2048
|
2016-02-16 15:07:25 +01:00
|
|
|
#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,
|
2016-07-03 21:32:35 +02:00
|
|
|
vkMeshLoader::VERTEX_LAYOUT_NORMAL,
|
|
|
|
|
vkMeshLoader::VERTEX_LAYOUT_TANGENT
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class VulkanExample : public VulkanExampleBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
2016-07-03 21:32:35 +02:00
|
|
|
bool debugDisplay = false;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
struct {
|
2016-07-03 21:32:35 +02:00
|
|
|
struct {
|
|
|
|
|
vkTools::VulkanTexture colorMap;
|
|
|
|
|
vkTools::VulkanTexture normalMap;
|
|
|
|
|
} model;
|
|
|
|
|
struct {
|
|
|
|
|
vkTools::VulkanTexture colorMap;
|
|
|
|
|
vkTools::VulkanTexture normalMap;
|
|
|
|
|
} floor;
|
2016-02-16 15:07:25 +01:00
|
|
|
} textures;
|
|
|
|
|
|
|
|
|
|
struct {
|
2016-07-03 21:32:35 +02:00
|
|
|
vkMeshLoader::MeshBuffer model;
|
|
|
|
|
vkMeshLoader::MeshBuffer floor;
|
2016-02-16 15:07:25 +01:00
|
|
|
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;
|
2016-07-03 21:32:35 +02:00
|
|
|
glm::vec4 instancePos[3];
|
2016-02-16 15:07:25 +01:00
|
|
|
} uboVS, uboOffscreenVS;
|
|
|
|
|
|
|
|
|
|
struct Light {
|
|
|
|
|
glm::vec4 position;
|
2016-07-03 21:32:35 +02:00
|
|
|
glm::vec3 color;
|
2016-02-16 15:07:25 +01:00
|
|
|
float radius;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct {
|
2016-07-03 21:32:35 +02:00
|
|
|
Light lights[6];
|
2016-02-16 15:07:25 +01:00
|
|
|
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 {
|
2016-07-03 21:32:35 +02:00
|
|
|
VkDescriptorSet model;
|
|
|
|
|
VkDescriptorSet floor;
|
2016-02-16 15:07:25 +01:00
|
|
|
} 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;
|
|
|
|
|
|
2016-06-05 18:53:30 +02:00
|
|
|
// One sampler for the frame buffer color attachments
|
|
|
|
|
VkSampler colorSampler;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
VkCommandBuffer offScreenCmdBuffer = VK_NULL_HANDLE;
|
|
|
|
|
|
2016-06-05 18:29:29 +02:00
|
|
|
// Semaphore used to synchronize between offscreen and final scene rendering
|
|
|
|
|
VkSemaphore offscreenSemaphore = VK_NULL_HANDLE;
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
|
|
|
|
{
|
|
|
|
|
zoom = -8.0f;
|
|
|
|
|
rotation = { 0.0f, 0.0f, 0.0f };
|
2016-06-03 17:41:17 +02:00
|
|
|
enableTextOverlay = true;
|
2016-07-03 21:32:35 +02:00
|
|
|
title = "Vulkan Example - Deferred shading (2016 by Sascha Willems)";
|
|
|
|
|
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, 0.1f, 256.0f);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~VulkanExample()
|
|
|
|
|
{
|
|
|
|
|
// Clean up used Vulkan resources
|
|
|
|
|
// Note : Inherited destructor cleans up resources stored in base class
|
|
|
|
|
|
2016-06-05 18:53:30 +02:00
|
|
|
vkDestroySampler(device, colorSampler, nullptr);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// 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
|
2016-07-03 21:32:35 +02:00
|
|
|
vkMeshLoader::freeMeshBufferResources(device, &meshes.model);
|
|
|
|
|
vkMeshLoader::freeMeshBufferResources(device, &meshes.floor);
|
2016-02-16 15:07:25 +01:00
|
|
|
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);
|
|
|
|
|
|
2016-07-03 21:32:35 +02:00
|
|
|
textureLoader->destroyTexture(textures.model.colorMap);
|
|
|
|
|
textureLoader->destroyTexture(textures.model.normalMap);
|
|
|
|
|
textureLoader->destroyTexture(textures.floor.colorMap);
|
|
|
|
|
textureLoader->destroyTexture(textures.floor.normalMap);
|
|
|
|
|
|
2016-06-05 18:29:29 +02:00
|
|
|
vkDestroySemaphore(device, offscreenSemaphore, nullptr);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a frame buffer attachment
|
|
|
|
|
void createAttachment(
|
|
|
|
|
VkFormat format,
|
|
|
|
|
VkImageUsageFlagBits usage,
|
2016-06-05 18:53:30 +02:00
|
|
|
FrameBufferAttachment *attachment,
|
|
|
|
|
VkCommandBuffer layoutCmd)
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
|
|
|
|
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;
|
2016-06-03 17:41:17 +02:00
|
|
|
image.extent.depth = 1;
|
2016-02-16 15:07:25 +01:00
|
|
|
image.mipLevels = 1;
|
|
|
|
|
image.arrayLayers = 1;
|
|
|
|
|
image.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
|
image.tiling = VK_IMAGE_TILING_OPTIMAL;
|
2016-06-05 18:53:30 +02:00
|
|
|
image.usage = usage | VK_IMAGE_USAGE_SAMPLED_BIT;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
VkMemoryAllocateInfo memAlloc = vkTools::initializers::memoryAllocateInfo();
|
|
|
|
|
VkMemoryRequirements memReqs;
|
|
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &attachment->image));
|
2016-02-16 15:07:25 +01:00
|
|
|
vkGetImageMemoryRequirements(device, attachment->image, &memReqs);
|
|
|
|
|
memAlloc.allocationSize = memReqs.size;
|
2016-06-05 18:22:20 +02:00
|
|
|
memAlloc.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &attachment->mem));
|
|
|
|
|
VK_CHECK_RESULT(vkBindImageMemory(device, attachment->image, attachment->mem, 0));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-06-05 18:53:30 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
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;
|
2016-02-16 15:07:25 +01:00
|
|
|
imageView.image = attachment->image;
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateImageView(device, &imageView, nullptr, &attachment->view));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare a new framebuffer for offscreen rendering
|
|
|
|
|
// The contents of this framebuffer are then
|
|
|
|
|
// blitted to our render target
|
|
|
|
|
void prepareOffscreenFramebuffer()
|
|
|
|
|
{
|
2016-06-05 18:53:30 +02:00
|
|
|
VkCommandBuffer layoutCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
offScreenFrameBuf.width = FB_DIM;
|
|
|
|
|
offScreenFrameBuf.height = FB_DIM;
|
|
|
|
|
|
|
|
|
|
// Color attachments
|
|
|
|
|
|
|
|
|
|
// (World space) Positions
|
|
|
|
|
createAttachment(
|
|
|
|
|
VK_FORMAT_R16G16B16A16_SFLOAT,
|
|
|
|
|
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
2016-06-05 18:53:30 +02:00
|
|
|
&offScreenFrameBuf.position,
|
|
|
|
|
layoutCmd);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// (World space) Normals
|
|
|
|
|
createAttachment(
|
|
|
|
|
VK_FORMAT_R16G16B16A16_SFLOAT,
|
|
|
|
|
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
2016-06-05 18:53:30 +02:00
|
|
|
&offScreenFrameBuf.normal,
|
|
|
|
|
layoutCmd);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Albedo (color)
|
|
|
|
|
createAttachment(
|
|
|
|
|
VK_FORMAT_R8G8B8A8_UNORM,
|
|
|
|
|
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
2016-06-05 18:53:30 +02:00
|
|
|
&offScreenFrameBuf.albedo,
|
|
|
|
|
layoutCmd);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Depth attachment
|
|
|
|
|
|
2016-02-18 22:09:47 +01:00
|
|
|
// Find a suitable depth format
|
|
|
|
|
VkFormat attDepthFormat;
|
|
|
|
|
VkBool32 validDepthFormat = vkTools::getSupportedDepthFormat(physicalDevice, &attDepthFormat);
|
|
|
|
|
assert(validDepthFormat);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
createAttachment(
|
|
|
|
|
attDepthFormat,
|
|
|
|
|
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
|
2016-06-05 18:53:30 +02:00
|
|
|
&offScreenFrameBuf.depth,
|
|
|
|
|
layoutCmd);
|
|
|
|
|
|
|
|
|
|
VulkanExampleBase::flushCommandBuffer(layoutCmd, queue, true);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Set up separate renderpass with references
|
|
|
|
|
// to the color and depth attachments
|
|
|
|
|
|
2016-04-30 10:45:39 +02:00
|
|
|
std::array<VkAttachmentDescription, 4> attachmentDescs = {};
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// 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();
|
2016-07-03 21:32:35 +02:00
|
|
|
subpass.colorAttachmentCount = static_cast<uint32_t>(colorReferences.size());
|
2016-02-16 15:07:25 +01:00
|
|
|
subpass.pDepthStencilAttachment = &depthReference;
|
|
|
|
|
|
|
|
|
|
VkRenderPassCreateInfo renderPassInfo = {};
|
|
|
|
|
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
|
|
|
|
renderPassInfo.pAttachments = attachmentDescs.data();
|
2016-07-03 21:32:35 +02:00
|
|
|
renderPassInfo.attachmentCount = static_cast<uint32_t>(attachmentDescs.size());
|
2016-02-16 15:07:25 +01:00
|
|
|
renderPassInfo.subpassCount = 1;
|
|
|
|
|
renderPassInfo.pSubpasses = &subpass;
|
|
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateRenderPass(device, &renderPassInfo, nullptr, &offScreenFrameBuf.renderPass));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
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();
|
2016-07-03 21:32:35 +02:00
|
|
|
fbufCreateInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
|
2016-02-16 15:07:25 +01:00
|
|
|
fbufCreateInfo.width = offScreenFrameBuf.width;
|
|
|
|
|
fbufCreateInfo.height = offScreenFrameBuf.height;
|
|
|
|
|
fbufCreateInfo.layers = 1;
|
|
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateFramebuffer(device, &fbufCreateInfo, nullptr, &offScreenFrameBuf.frameBuffer));
|
2016-06-05 18:53:30 +02:00
|
|
|
// 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, &colorSampler));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
2016-06-05 18:29:29 +02:00
|
|
|
// Build command buffer for rendering the scene to the offscreen frame buffer attachments
|
2016-02-16 15:07:25 +01:00
|
|
|
void buildDeferredCommandBuffer()
|
|
|
|
|
{
|
|
|
|
|
if (offScreenCmdBuffer == VK_NULL_HANDLE)
|
|
|
|
|
{
|
2016-06-05 18:29:29 +02:00
|
|
|
offScreenCmdBuffer = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, false);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
2016-06-05 18:29:29 +02:00
|
|
|
// Create a semaphore used to synchronize offscreen rendering and usage
|
|
|
|
|
VkSemaphoreCreateInfo semaphoreCreateInfo = vkTools::initializers::semaphoreCreateInfo();
|
|
|
|
|
VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &offscreenSemaphore));
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
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;
|
2016-07-03 21:32:35 +02:00
|
|
|
renderPassBeginInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
|
2016-02-16 15:07:25 +01:00
|
|
|
renderPassBeginInfo.pClearValues = clearValues.data();
|
|
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(offScreenCmdBuffer, &cmdBufInfo));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-06-05 18:53:30 +02:00
|
|
|
std::vector<FrameBufferAttachment> attachments = { offScreenFrameBuf.position, offScreenFrameBuf.normal, offScreenFrameBuf.albedo };
|
|
|
|
|
|
|
|
|
|
// Change back layout of the color attachments after sampling in the fragment shader
|
|
|
|
|
for (auto attachment : attachments)
|
|
|
|
|
{
|
|
|
|
|
vkTools::setImageLayout(
|
|
|
|
|
offScreenCmdBuffer,
|
|
|
|
|
attachment.image,
|
|
|
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
|
|
|
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
vkCmdBeginRenderPass(offScreenCmdBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
|
|
2016-06-05 18:29:29 +02:00
|
|
|
VkViewport viewport = vkTools::initializers::viewport((float)offScreenFrameBuf.width, (float)offScreenFrameBuf.height, 0.0f, 1.0f);
|
2016-02-16 15:07:25 +01:00
|
|
|
vkCmdSetViewport(offScreenCmdBuffer, 0, 1, &viewport);
|
|
|
|
|
|
2016-06-05 18:29:29 +02:00
|
|
|
VkRect2D scissor = vkTools::initializers::rect2D(offScreenFrameBuf.width, offScreenFrameBuf.height, 0, 0);
|
2016-02-16 15:07:25 +01:00
|
|
|
vkCmdSetScissor(offScreenCmdBuffer, 0, 1, &scissor);
|
|
|
|
|
|
|
|
|
|
vkCmdBindPipeline(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.offscreen);
|
|
|
|
|
|
|
|
|
|
VkDeviceSize offsets[1] = { 0 };
|
2016-07-03 21:32:35 +02:00
|
|
|
|
|
|
|
|
// Background
|
|
|
|
|
vkCmdBindDescriptorSets(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.offscreen, 0, 1, &descriptorSets.floor, 0, NULL);
|
|
|
|
|
vkCmdBindVertexBuffers(offScreenCmdBuffer, VERTEX_BUFFER_BIND_ID, 1, &meshes.floor.vertices.buf, offsets);
|
|
|
|
|
vkCmdBindIndexBuffer(offScreenCmdBuffer, meshes.floor.indices.buf, 0, VK_INDEX_TYPE_UINT32);
|
|
|
|
|
vkCmdDrawIndexed(offScreenCmdBuffer, meshes.floor.indexCount, 1, 0, 0, 0);
|
|
|
|
|
|
|
|
|
|
// Object
|
|
|
|
|
vkCmdBindDescriptorSets(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.offscreen, 0, 1, &descriptorSets.model, 0, NULL);
|
|
|
|
|
vkCmdBindVertexBuffers(offScreenCmdBuffer, VERTEX_BUFFER_BIND_ID, 1, &meshes.model.vertices.buf, offsets);
|
|
|
|
|
vkCmdBindIndexBuffer(offScreenCmdBuffer, meshes.model.indices.buf, 0, VK_INDEX_TYPE_UINT32);
|
|
|
|
|
vkCmdDrawIndexed(offScreenCmdBuffer, meshes.model.indexCount, 3, 0, 0, 0);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
vkCmdEndRenderPass(offScreenCmdBuffer);
|
|
|
|
|
|
2016-06-05 18:53:30 +02:00
|
|
|
// Change back layout of the color attachments after sampling in the fragment shader
|
|
|
|
|
for (auto attachment : attachments)
|
|
|
|
|
{
|
|
|
|
|
vkTools::setImageLayout(
|
|
|
|
|
offScreenCmdBuffer,
|
|
|
|
|
attachment.image,
|
|
|
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
|
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
|
|
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(offScreenCmdBuffer));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loadTextures()
|
|
|
|
|
{
|
2016-07-03 21:32:35 +02:00
|
|
|
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/pattern_35_bc3.ktx", VK_FORMAT_BC3_UNORM_BLOCK, &textures.floor.colorMap);
|
|
|
|
|
textureLoader->loadTexture(getAssetPath() + "textures/pattern_35_normalmap_bc3.ktx", VK_FORMAT_BC3_UNORM_BLOCK, &textures.floor.normalMap);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 = frameBuffers[i];
|
|
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
|
|
2016-06-05 18:53:30 +02:00
|
|
|
VkViewport viewport = vkTools::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
|
2016-02-16 15:07:25 +01:00
|
|
|
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
|
|
|
|
|
|
2016-06-05 18:53:30 +02:00
|
|
|
VkRect2D scissor = vkTools::initializers::rect2D(width, height, 0, 0);
|
2016-02-16 15:07:25 +01:00
|
|
|
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]);
|
|
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loadMeshes()
|
|
|
|
|
{
|
2016-07-03 21:32:35 +02:00
|
|
|
loadMesh(getAssetPath() + "models/armor/armor.dae", &meshes.model, vertexLayout, 1.0f);
|
|
|
|
|
|
|
|
|
|
vkMeshLoader::MeshCreateInfo meshCreateInfo;
|
|
|
|
|
meshCreateInfo.scale = glm::vec3(2.0f);
|
|
|
|
|
meshCreateInfo.uvscale = glm::vec2(4.0f);
|
|
|
|
|
meshCreateInfo.center = glm::vec3(0.0f, 2.35f, 0.0f);
|
|
|
|
|
loadMesh(getAssetPath() + "models/plane.obj", &meshes.floor, vertexLayout, &meshCreateInfo);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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];
|
2016-07-03 21:32:35 +02:00
|
|
|
float tangent[3];
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-03 21:32:35 +02:00
|
|
|
meshes.quad.indexCount = static_cast<uint32_t>(indexBuffer.size());
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
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
|
2016-07-03 21:32:35 +02:00
|
|
|
vertices.attributeDescriptions.resize(5);
|
|
|
|
|
// Location 0: Position
|
2016-02-16 15:07:25 +01:00
|
|
|
vertices.attributeDescriptions[0] =
|
|
|
|
|
vkTools::initializers::vertexInputAttributeDescription(
|
|
|
|
|
VERTEX_BUFFER_BIND_ID,
|
|
|
|
|
0,
|
|
|
|
|
VK_FORMAT_R32G32B32_SFLOAT,
|
|
|
|
|
0);
|
2016-07-03 21:32:35 +02:00
|
|
|
// Location 1: Texture coordinates
|
2016-02-16 15:07:25 +01:00
|
|
|
vertices.attributeDescriptions[1] =
|
|
|
|
|
vkTools::initializers::vertexInputAttributeDescription(
|
|
|
|
|
VERTEX_BUFFER_BIND_ID,
|
|
|
|
|
1,
|
|
|
|
|
VK_FORMAT_R32G32_SFLOAT,
|
|
|
|
|
sizeof(float) * 3);
|
2016-07-03 21:32:35 +02:00
|
|
|
// Location 2: Color
|
2016-02-16 15:07:25 +01:00
|
|
|
vertices.attributeDescriptions[2] =
|
|
|
|
|
vkTools::initializers::vertexInputAttributeDescription(
|
|
|
|
|
VERTEX_BUFFER_BIND_ID,
|
|
|
|
|
2,
|
|
|
|
|
VK_FORMAT_R32G32B32_SFLOAT,
|
|
|
|
|
sizeof(float) * 5);
|
2016-07-03 21:32:35 +02:00
|
|
|
// Location 3: Normal
|
2016-02-16 15:07:25 +01:00
|
|
|
vertices.attributeDescriptions[3] =
|
|
|
|
|
vkTools::initializers::vertexInputAttributeDescription(
|
|
|
|
|
VERTEX_BUFFER_BIND_ID,
|
|
|
|
|
3,
|
|
|
|
|
VK_FORMAT_R32G32B32_SFLOAT,
|
|
|
|
|
sizeof(float) * 8);
|
2016-07-03 21:32:35 +02:00
|
|
|
// Location 4: Tangent
|
|
|
|
|
vertices.attributeDescriptions[4] =
|
|
|
|
|
vkTools::initializers::vertexInputAttributeDescription(
|
|
|
|
|
VERTEX_BUFFER_BIND_ID,
|
|
|
|
|
4,
|
|
|
|
|
VK_FORMAT_R32G32B32_SFLOAT,
|
|
|
|
|
sizeof(float) * 11);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo();
|
2016-07-03 21:32:35 +02:00
|
|
|
vertices.inputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertices.bindingDescriptions.size());
|
2016-02-16 15:07:25 +01:00
|
|
|
vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data();
|
2016-07-03 21:32:35 +02:00
|
|
|
vertices.inputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertices.attributeDescriptions.size());
|
2016-02-16 15:07:25 +01:00
|
|
|
vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorPool()
|
|
|
|
|
{
|
|
|
|
|
std::vector<VkDescriptorPoolSize> poolSizes =
|
|
|
|
|
{
|
|
|
|
|
vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 8),
|
2016-07-03 21:32:35 +02:00
|
|
|
vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 9)
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
VkDescriptorPoolCreateInfo descriptorPoolInfo =
|
|
|
|
|
vkTools::initializers::descriptorPoolCreateInfo(
|
2016-07-03 21:32:35 +02:00
|
|
|
static_cast<uint32_t>(poolSizes.size()),
|
2016-02-16 15:07:25 +01:00
|
|
|
poolSizes.data(),
|
2016-07-03 21:32:35 +02:00
|
|
|
3);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(),
|
2016-07-03 21:32:35 +02:00
|
|
|
static_cast<uint32_t>(setLayoutBindings.size()));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
|
|
|
|
vkTools::initializers::pipelineLayoutCreateInfo(
|
|
|
|
|
&descriptorSetLayout,
|
|
|
|
|
1);
|
|
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.deferred));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Offscreen (scene) rendering pipeline layout
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.offscreen));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorSet()
|
|
|
|
|
{
|
2016-07-03 21:32:35 +02:00
|
|
|
std::vector<VkWriteDescriptorSet> writeDescriptorSets;
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
// Textured quad descriptor set
|
|
|
|
|
VkDescriptorSetAllocateInfo allocInfo =
|
|
|
|
|
vkTools::initializers::descriptorSetAllocateInfo(
|
|
|
|
|
descriptorPool,
|
|
|
|
|
&descriptorSetLayout,
|
|
|
|
|
1);
|
|
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-06-05 18:53:30 +02:00
|
|
|
// Image descriptors for the offscreen color attachments
|
2016-02-16 15:07:25 +01:00
|
|
|
VkDescriptorImageInfo texDescriptorPosition =
|
|
|
|
|
vkTools::initializers::descriptorImageInfo(
|
2016-06-05 18:53:30 +02:00
|
|
|
colorSampler,
|
|
|
|
|
offScreenFrameBuf.position.view,
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_IMAGE_LAYOUT_GENERAL);
|
|
|
|
|
|
|
|
|
|
VkDescriptorImageInfo texDescriptorNormal =
|
|
|
|
|
vkTools::initializers::descriptorImageInfo(
|
2016-06-05 18:53:30 +02:00
|
|
|
colorSampler,
|
|
|
|
|
offScreenFrameBuf.normal.view,
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_IMAGE_LAYOUT_GENERAL);
|
|
|
|
|
|
|
|
|
|
VkDescriptorImageInfo texDescriptorAlbedo =
|
|
|
|
|
vkTools::initializers::descriptorImageInfo(
|
2016-06-05 18:53:30 +02:00
|
|
|
colorSampler,
|
|
|
|
|
offScreenFrameBuf.albedo.view,
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_IMAGE_LAYOUT_GENERAL);
|
|
|
|
|
|
2016-07-03 21:32:35 +02:00
|
|
|
writeDescriptorSets = {
|
2016-02-16 15:07:25 +01:00
|
|
|
// 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),
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-03 21:32:35 +02:00
|
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Offscreen (scene)
|
|
|
|
|
|
2016-07-03 21:32:35 +02:00
|
|
|
// 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);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-07-03 21:32:35 +02:00
|
|
|
// Backbround
|
|
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.floor));
|
|
|
|
|
writeDescriptorSets =
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
2016-07-03 21:32:35 +02:00
|
|
|
// Binding 0: Vertex shader uniform buffer
|
2016-02-16 15:07:25 +01:00
|
|
|
vkTools::initializers::writeDescriptorSet(
|
2016-07-03 21:32:35 +02:00
|
|
|
descriptorSets.floor,
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
|
|
|
0,
|
|
|
|
|
&uniformData.vsOffscreen.descriptor),
|
2016-07-03 21:32:35 +02:00
|
|
|
// Binding 1: Color map
|
2016-02-16 15:07:25 +01:00
|
|
|
vkTools::initializers::writeDescriptorSet(
|
2016-07-03 21:32:35 +02:00
|
|
|
descriptorSets.floor,
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
|
1,
|
2016-07-03 21:32:35 +02:00
|
|
|
&textures.floor.colorMap.descriptor),
|
|
|
|
|
// Binding 2: Normal map
|
|
|
|
|
vkTools::initializers::writeDescriptorSet(
|
|
|
|
|
descriptorSets.floor,
|
|
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
|
2,
|
|
|
|
|
&textures.floor.normalMap.descriptor)
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
2016-07-03 21:32:35 +02:00
|
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(),
|
2016-07-03 21:32:35 +02:00
|
|
|
static_cast<uint32_t>(dynamicStateEnables.size()),
|
2016-02-16 15:07:25 +01:00
|
|
|
0);
|
|
|
|
|
|
|
|
|
|
// Final fullscreen pass pipeline
|
|
|
|
|
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
|
|
|
|
|
|
2016-03-25 14:12:17 +01:00
|
|
|
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);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
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;
|
2016-07-03 21:32:35 +02:00
|
|
|
pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
|
2016-02-16 15:07:25 +01:00
|
|
|
pipelineCreateInfo.pStages = shaderStages.data();
|
|
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.deferred));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Debug display pipeline
|
2016-03-25 14:12:17 +01:00
|
|
|
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);
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.debug));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Offscreen pipeline
|
2016-03-25 14:12:17 +01:00
|
|
|
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);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-03 21:32:35 +02:00
|
|
|
colorBlendState.attachmentCount = static_cast<uint32_t>(blendAttachmentStates.size());
|
2016-02-16 15:07:25 +01:00
|
|
|
colorBlendState.pAttachments = blendAttachmentStates.data();
|
|
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.offscreen));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare and initialize uniform buffer containing shader uniforms
|
|
|
|
|
void prepareUniformBuffers()
|
|
|
|
|
{
|
|
|
|
|
// Fullscreen vertex shader
|
|
|
|
|
createBuffer(
|
|
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
2016-02-16 15:07:25 +01:00
|
|
|
sizeof(uboVS),
|
2016-06-03 17:41:17 +02:00
|
|
|
nullptr,
|
2016-02-16 15:07:25 +01:00
|
|
|
&uniformData.vsFullScreen.buffer,
|
|
|
|
|
&uniformData.vsFullScreen.memory,
|
|
|
|
|
&uniformData.vsFullScreen.descriptor);
|
|
|
|
|
|
|
|
|
|
// Deferred vertex shader
|
|
|
|
|
createBuffer(
|
|
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
2016-02-16 15:07:25 +01:00
|
|
|
sizeof(uboOffscreenVS),
|
2016-06-03 17:41:17 +02:00
|
|
|
nullptr,
|
2016-02-16 15:07:25 +01:00
|
|
|
&uniformData.vsOffscreen.buffer,
|
|
|
|
|
&uniformData.vsOffscreen.memory,
|
|
|
|
|
&uniformData.vsOffscreen.descriptor);
|
|
|
|
|
|
|
|
|
|
// Deferred fragment shader
|
|
|
|
|
createBuffer(
|
|
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
2016-02-16 15:07:25 +01:00
|
|
|
sizeof(uboFragmentLights),
|
2016-06-03 17:41:17 +02:00
|
|
|
nullptr,
|
2016-02-16 15:07:25 +01:00
|
|
|
&uniformData.fsLights.buffer,
|
|
|
|
|
&uniformData.fsLights.memory,
|
|
|
|
|
&uniformData.fsLights.descriptor);
|
|
|
|
|
|
2016-07-03 21:32:35 +02:00
|
|
|
// 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);
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
// 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;
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkMapMemory(device, uniformData.vsFullScreen.memory, 0, sizeof(uboVS), 0, (void **)&pData));
|
2016-02-16 15:07:25 +01:00
|
|
|
memcpy(pData, &uboVS, sizeof(uboVS));
|
|
|
|
|
vkUnmapMemory(device, uniformData.vsFullScreen.memory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateUniformBufferDeferredMatrices()
|
|
|
|
|
{
|
2016-03-08 20:59:25 +01:00
|
|
|
uboOffscreenVS.projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 256.0f);
|
2016-02-16 15:07:25 +01:00
|
|
|
uboOffscreenVS.view = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom));
|
|
|
|
|
|
|
|
|
|
uboOffscreenVS.model = glm::mat4();
|
2016-06-03 17:41:17 +02:00
|
|
|
uboOffscreenVS.model = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.25f, 0.0f) + cameraPos);
|
|
|
|
|
uboOffscreenVS.model = glm::rotate(uboOffscreenVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
|
|
|
|
|
uboOffscreenVS.model = glm::rotate(uboOffscreenVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
|
uboOffscreenVS.model = glm::rotate(uboOffscreenVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-07-03 21:32:35 +02:00
|
|
|
uboOffscreenVS.projection = camera.matrices.perspective;
|
|
|
|
|
uboOffscreenVS.view = camera.matrices.view;
|
|
|
|
|
uboOffscreenVS.model = glm::mat4();
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
uint8_t *pData;
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkMapMemory(device, uniformData.vsOffscreen.memory, 0, sizeof(uboOffscreenVS), 0, (void **)&pData));
|
2016-02-16 15:07:25 +01:00
|
|
|
memcpy(pData, &uboOffscreenVS, sizeof(uboOffscreenVS));
|
|
|
|
|
vkUnmapMemory(device, uniformData.vsOffscreen.memory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update fragment shader light position uniform block
|
|
|
|
|
void updateUniformBufferDeferredLights()
|
|
|
|
|
{
|
2016-07-03 21:32:35 +02:00
|
|
|
// White
|
|
|
|
|
uboFragmentLights.lights[0].position = glm::vec4(0.0f, 0.0f, 1.0f, 0.0f);
|
|
|
|
|
uboFragmentLights.lights[0].color = glm::vec3(1.5f);
|
|
|
|
|
uboFragmentLights.lights[0].radius = 15.0f * 0.25f;
|
|
|
|
|
// Red
|
2016-02-16 15:07:25 +01:00
|
|
|
uboFragmentLights.lights[1].position = glm::vec4(-2.0f, 0.0f, 0.0f, 0.0f);
|
2016-07-03 21:32:35 +02:00
|
|
|
uboFragmentLights.lights[1].color = glm::vec3(1.0f, 0.0f, 0.0f);
|
2016-02-16 15:07:25 +01:00
|
|
|
uboFragmentLights.lights[1].radius = 15.0f;
|
2016-07-03 21:32:35 +02:00
|
|
|
// Blue
|
2016-02-16 15:07:25 +01:00
|
|
|
uboFragmentLights.lights[2].position = glm::vec4(2.0f, 1.0f, 0.0f, 0.0f);
|
2016-07-03 21:32:35 +02:00
|
|
|
uboFragmentLights.lights[2].color = glm::vec3(0.0f, 0.0f, 2.5f);
|
|
|
|
|
uboFragmentLights.lights[2].radius = 5.0f;
|
|
|
|
|
// Yellow
|
|
|
|
|
uboFragmentLights.lights[3].position = glm::vec4(0.0f, 0.9f, 0.5f, 0.0f);
|
|
|
|
|
uboFragmentLights.lights[3].color = glm::vec3(1.0f, 1.0f, 0.0f);
|
|
|
|
|
uboFragmentLights.lights[3].radius = 2.0f;
|
|
|
|
|
// Green
|
|
|
|
|
uboFragmentLights.lights[4].position = glm::vec4(0.0f, 0.5f, 0.0f, 0.0f);
|
|
|
|
|
uboFragmentLights.lights[4].color = glm::vec3(0.0f, 1.0f, 0.2f);
|
|
|
|
|
uboFragmentLights.lights[4].radius = 5.0f;
|
|
|
|
|
// Yellow
|
|
|
|
|
uboFragmentLights.lights[5].position = glm::vec4(0.0f, 1.0f, 0.0f, 0.0f);
|
|
|
|
|
uboFragmentLights.lights[5].color = glm::vec3(1.0f, 0.7f, 0.3f);
|
|
|
|
|
uboFragmentLights.lights[5].radius = 25.0f;
|
|
|
|
|
|
|
|
|
|
uboFragmentLights.lights[0].position.x = sin(glm::radians(360.0f * timer)) * 5.0f;
|
|
|
|
|
uboFragmentLights.lights[0].position.z = cos(glm::radians(360.0f * timer)) * 5.0f;
|
|
|
|
|
|
|
|
|
|
uboFragmentLights.lights[1].position.x = -4.0f + sin(glm::radians(360.0f * timer) + 45.0f) * 2.0f;
|
|
|
|
|
uboFragmentLights.lights[1].position.z = 0.0f + cos(glm::radians(360.0f * timer) + 45.0f) * 2.0f;
|
|
|
|
|
|
|
|
|
|
uboFragmentLights.lights[2].position.x = 4.0f + sin(glm::radians(360.0f * timer)) * 2.0f;
|
|
|
|
|
uboFragmentLights.lights[2].position.z = 0.0f + cos(glm::radians(360.0f * timer)) * 2.0f;
|
|
|
|
|
|
|
|
|
|
uboFragmentLights.lights[4].position.x = 0.0f + sin(glm::radians(360.0f * timer + 90.0f)) * 5.0f;
|
|
|
|
|
uboFragmentLights.lights[4].position.z = 0.0f - cos(glm::radians(360.0f * timer + 45.0f)) * 5.0f;
|
|
|
|
|
|
|
|
|
|
uboFragmentLights.lights[5].position.x = 0.0f + sin(glm::radians(-360.0f * timer + 135.0f)) * 10.0f;
|
|
|
|
|
uboFragmentLights.lights[5].position.z = 0.0f - cos(glm::radians(-360.0f * timer - 45.0f)) * 10.0f;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Current view position
|
2016-07-03 21:32:35 +02:00
|
|
|
uboFragmentLights.viewPos = glm::vec4(camera.position, 0.0f) * glm::vec4(-1.0f, 1.0f, -1.0f, 1.0f);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
uint8_t *pData;
|
2016-06-03 17:41:17 +02:00
|
|
|
VK_CHECK_RESULT(vkMapMemory(device, uniformData.fsLights.memory, 0, sizeof(uboFragmentLights), 0, (void **)&pData));
|
2016-02-16 15:07:25 +01:00
|
|
|
memcpy(pData, &uboFragmentLights, sizeof(uboFragmentLights));
|
|
|
|
|
vkUnmapMemory(device, uniformData.fsLights.memory);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-03 17:41:17 +02:00
|
|
|
void draw()
|
|
|
|
|
{
|
|
|
|
|
VulkanExampleBase::prepareFrame();
|
|
|
|
|
|
2016-06-05 18:29:29 +02:00
|
|
|
// The scene render command buffer has to wait for the offscreen
|
|
|
|
|
// rendering to be finished before we can use the framebuffer
|
|
|
|
|
// color image for sampling during final rendering
|
|
|
|
|
// To ensure this we use a dedicated offscreen synchronization
|
|
|
|
|
// semaphore that will be signaled when offscreen renderin
|
|
|
|
|
// has been finished
|
|
|
|
|
// This is necessary as an implementation may start both
|
|
|
|
|
// command buffers at the same time, there is no guarantee
|
|
|
|
|
// that command buffers will be executed in the order they
|
|
|
|
|
// have been submitted by the application
|
|
|
|
|
|
|
|
|
|
// Offscreen rendering
|
|
|
|
|
|
|
|
|
|
// Wait for swap chain presentation to finish
|
|
|
|
|
submitInfo.pWaitSemaphores = &semaphores.presentComplete;
|
|
|
|
|
// Signal ready with offscreen semaphore
|
|
|
|
|
submitInfo.pSignalSemaphores = &offscreenSemaphore;
|
|
|
|
|
|
|
|
|
|
// Submit work
|
|
|
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
|
submitInfo.pCommandBuffers = &offScreenCmdBuffer;
|
|
|
|
|
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;
|
2016-06-03 17:41:17 +02:00
|
|
|
|
2016-06-05 18:29:29 +02:00
|
|
|
// Submit work
|
|
|
|
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
2016-06-03 17:41:17 +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();
|
|
|
|
|
loadTextures();
|
|
|
|
|
generateQuads();
|
|
|
|
|
loadMeshes();
|
|
|
|
|
setupVertexDescriptions();
|
|
|
|
|
prepareOffscreenFramebuffer();
|
|
|
|
|
prepareUniformBuffers();
|
|
|
|
|
setupDescriptorSetLayout();
|
|
|
|
|
preparePipelines();
|
|
|
|
|
setupDescriptorPool();
|
|
|
|
|
setupDescriptorSet();
|
|
|
|
|
buildCommandBuffers();
|
|
|
|
|
buildDeferredCommandBuffer();
|
|
|
|
|
prepared = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void render()
|
|
|
|
|
{
|
|
|
|
|
if (!prepared)
|
|
|
|
|
return;
|
|
|
|
|
draw();
|
2016-07-03 21:32:35 +02:00
|
|
|
updateUniformBufferDeferredLights();
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void viewChanged()
|
|
|
|
|
{
|
|
|
|
|
updateUniformBufferDeferredMatrices();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void toggleDebugDisplay()
|
|
|
|
|
{
|
|
|
|
|
debugDisplay = !debugDisplay;
|
|
|
|
|
reBuildCommandBuffers();
|
|
|
|
|
updateUniformBuffersScreen();
|
|
|
|
|
}
|
2016-06-03 17:41:17 +02:00
|
|
|
|
|
|
|
|
virtual void keyPressed(uint32_t keyCode)
|
|
|
|
|
{
|
|
|
|
|
switch (keyCode)
|
|
|
|
|
{
|
2016-07-03 21:32:35 +02:00
|
|
|
case 0x70:
|
2016-06-03 17:41:17 +02:00
|
|
|
case GAMEPAD_BUTTON_A:
|
|
|
|
|
toggleDebugDisplay();
|
|
|
|
|
updateTextOverlay();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void getOverlayText(VulkanTextOverlay *textOverlay)
|
|
|
|
|
{
|
|
|
|
|
#if defined(__ANDROID__)
|
2016-07-03 21:32:35 +02:00
|
|
|
textOverlay->addText("Press \"Button A\" to toggle debug display", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
|
2016-06-03 17:41:17 +02:00
|
|
|
#else
|
2016-07-03 21:32:35 +02:00
|
|
|
textOverlay->addText("Press \"F1\" to toggle debug display", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
|
2016-06-03 17:41:17 +02:00
|
|
|
#endif
|
|
|
|
|
// Render targets
|
|
|
|
|
if (debugDisplay)
|
|
|
|
|
{
|
2016-07-03 21:32:35 +02:00
|
|
|
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);
|
2016-06-03 17:41:17 +02:00
|
|
|
textOverlay->addText("Final image", (float)width * 0.75f, (float)height - 25.0f, VulkanTextOverlay::alignCenter);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
|
|
|
|
|
2016-07-03 21:32:35 +02:00
|
|
|
VULKAN_EXAMPLE_MAIN()
|