Archived
1
Fork 0

Add sky pass

This commit is contained in:
Joshua Goins 2018-11-07 07:20:52 -05:00
parent 9ca2e3073c
commit af1b3b9845
8 changed files with 187 additions and 25 deletions

View file

@ -50,7 +50,8 @@ add_executable(Graph
src/worldpass.cpp src/worldpass.cpp
src/postpass.cpp src/postpass.cpp
src/dofpass.cpp src/dofpass.cpp
src/imguipass.cpp) src/imguipass.cpp
src/skypass.cpp)
target_compile_options(Graph target_compile_options(Graph
PUBLIC PUBLIC
-fno-exceptions -fno-exceptions
@ -78,7 +79,9 @@ add_shaders(Graph
shaders/dof.vert shaders/dof.vert
shaders/dof.frag shaders/dof.frag
shaders/imgui.vert shaders/imgui.vert
shaders/imgui.frag) shaders/imgui.frag
shaders/sky.vert
shaders/sky.frag)
add_data(Graph add_data(Graph
data/suzanne.obj data/suzanne.obj

View file

@ -6,6 +6,7 @@
#include "postpass.h" #include "postpass.h"
#include "dofpass.h" #include "dofpass.h"
#include "imguipass.h" #include "imguipass.h"
#include "skypass.h"
constexpr int numFrameResources = 2; constexpr int numFrameResources = 2;
@ -123,6 +124,10 @@ public:
return materialSetLayout_; return materialSetLayout_;
} }
WorldPass& getWorldPass() const {
return *worldPass_;
}
private: private:
void createInstance(); void createInstance();
#ifdef DEBUG #ifdef DEBUG
@ -168,4 +173,5 @@ private:
PostPass* postPass_ = nullptr; PostPass* postPass_ = nullptr;
DoFPass* dofPass_ = nullptr; DoFPass* dofPass_ = nullptr;
ImGuiPass* imguiPass_ = nullptr; ImGuiPass* imguiPass_ = nullptr;
SkyPass* skyPass_ = nullptr;
}; };

21
include/skypass.h Normal file
View file

@ -0,0 +1,21 @@
#pragma once
#include <vulkan/vulkan.h>
class Renderer;
class SkyPass {
public:
SkyPass(Renderer& renderer);
~SkyPass();
void render(VkCommandBuffer commandBuffer);
private:
void createPipeline();
VkPipeline pipeline_ = nullptr;
VkPipelineLayout pipelineLayout_ = nullptr;
Renderer& renderer_;
};

10
shaders/sky.frag Normal file
View file

@ -0,0 +1,10 @@
#version 460 core
layout(location = 0) in vec2 inUV;
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4(0.1, 0.1, 0.8, 1.0);
}

10
shaders/sky.vert Normal file
View file

@ -0,0 +1,10 @@
#version 460 core
layout(location = 0) out vec2 outUV;
void main() {
outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
gl_Position = vec4(outUV * 2.0 + -1.0, 1.0, 1.0);
}

View file

@ -30,11 +30,13 @@ Renderer::Renderer() {
postPass_ = new PostPass(*this); postPass_ = new PostPass(*this);
dofPass_ = new DoFPass(*this); dofPass_ = new DoFPass(*this);
imguiPass_ = new ImGuiPass(*this); imguiPass_ = new ImGuiPass(*this);
skyPass_ = new SkyPass(*this);
} }
Renderer::~Renderer() { Renderer::~Renderer() {
vkDeviceWaitIdle(device_); vkDeviceWaitIdle(device_);
delete skyPass_;
delete imguiPass_; delete imguiPass_;
delete dofPass_; delete dofPass_;
delete postPass_; delete postPass_;
@ -84,22 +86,39 @@ void Renderer::render(World& world, Camera& camera, RenderTarget* target) {
vkCmdSetScissor(commandBuffer, 0, 1, &scissor); vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
std::array<VkClearValue, 2> clearColor = {};
clearColor[1].depthStencil.depth = 1.0f;
VkRenderPassBeginInfo renderPassBeginInfo = {};
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassBeginInfo.framebuffer = target->offscreenFramebuffers[target->currentResource];
renderPassBeginInfo.renderPass = worldPass_->getRenderPass();
renderPassBeginInfo.renderArea.extent = target->extent;
renderPassBeginInfo.clearValueCount = clearColor.size();
renderPassBeginInfo.pClearValues = clearColor.data();
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
worldPass_->render(commandBuffer, world, camera, target); worldPass_->render(commandBuffer, world, camera, target);
skyPass_->render(commandBuffer);
vkCmdEndRenderPass(commandBuffer);
dofPass_->render(commandBuffer, camera, target); dofPass_->render(commandBuffer, camera, target);
// reset after dof pass // reset after dof pass
vkCmdSetViewport(commandBuffer, 0, 1, &viewport); vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
vkCmdSetScissor(commandBuffer, 0, 1, &scissor); vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
VkClearValue clearColor = {}; clearColor = {};
VkRenderPassBeginInfo renderPassBeginInfo = {}; renderPassBeginInfo = {};
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassBeginInfo.framebuffer = target->swapchainFramebuffers[target->currentResource]; renderPassBeginInfo.framebuffer = target->swapchainFramebuffers[target->currentResource];
renderPassBeginInfo.renderPass = presentationRenderPass_; renderPassBeginInfo.renderPass = presentationRenderPass_;
renderPassBeginInfo.renderArea.extent = target->extent; renderPassBeginInfo.renderArea.extent = target->extent;
renderPassBeginInfo.clearValueCount = 1; renderPassBeginInfo.clearValueCount = 1;
renderPassBeginInfo.pClearValues = &clearColor; renderPassBeginInfo.pClearValues = &clearColor[0];
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);

108
src/skypass.cpp Normal file
View file

@ -0,0 +1,108 @@
#include "skypass.h"
#include "renderer.h"
SkyPass::SkyPass(Renderer& renderer) : renderer_(renderer) {
createPipeline();
}
SkyPass::~SkyPass() {
vkDestroyPipeline(renderer_.getDevice(), pipeline_, nullptr);
vkDestroyPipelineLayout(renderer_.getDevice(), pipelineLayout_, nullptr);
}
void SkyPass::render(VkCommandBuffer commandBuffer) {
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_);
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
}
void SkyPass::createPipeline() {
VkShaderModule vertShaderModule = renderer_.createShader("shaders/sky.vert.spv");
VkShaderModule fragShaderModule = renderer_.createShader("shaders/sky.frag.spv");
VkPipelineShaderStageCreateInfo vertShaderStageInfo = {};
vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
vertShaderStageInfo.module = vertShaderModule;
vertShaderStageInfo.pName = "main";
VkPipelineShaderStageCreateInfo fragShaderStageInfo = {};
fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
fragShaderStageInfo.module = fragShaderModule;
fragShaderStageInfo.pName = "main";
const std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages = {vertShaderStageInfo, fragShaderStageInfo};
VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
VkPipelineViewportStateCreateInfo viewportState = {};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = 1;
viewportState.scissorCount = 1;
VkPipelineRasterizationStateCreateInfo rasterizer = {};
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
rasterizer.cullMode = VK_CULL_MODE_NONE;
rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
rasterizer.lineWidth = 1.0f;
VkPipelineMultisampleStateCreateInfo multisampling = {};
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
VkPipelineColorBlendStateCreateInfo colorBlending = {};
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlending.attachmentCount = 1;
colorBlending.pAttachments = &colorBlendAttachment;
VkPipelineDepthStencilStateCreateInfo depthState = {};
depthState.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
depthState.depthTestEnable = VK_TRUE;
depthState.depthWriteEnable = VK_FALSE;
depthState.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
const std::array<VkDynamicState, 2> dynamicStates = {
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR
};
VkPipelineDynamicStateCreateInfo dynamicState = {};
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
dynamicState.dynamicStateCount = dynamicStates.size();
dynamicState.pDynamicStates = dynamicStates.data();
VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
vkCreatePipelineLayout(renderer_.getDevice(), &pipelineLayoutInfo, nullptr, &pipelineLayout_);
VkGraphicsPipelineCreateInfo pipelineInfo = {};
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.stageCount = shaderStages.size();
pipelineInfo.pStages = shaderStages.data();
pipelineInfo.pVertexInputState = &vertexInputInfo;
pipelineInfo.pInputAssemblyState = &inputAssembly;
pipelineInfo.pViewportState = &viewportState;
pipelineInfo.pRasterizationState = &rasterizer;
pipelineInfo.pMultisampleState = &multisampling;
pipelineInfo.pColorBlendState = &colorBlending;
pipelineInfo.pDepthStencilState = &depthState;
pipelineInfo.pDynamicState = &dynamicState;
pipelineInfo.layout = pipelineLayout_;
pipelineInfo.renderPass = renderer_.getWorldPass().getRenderPass();
vkCreateGraphicsPipelines(renderer_.getDevice(), nullptr, 1, &pipelineInfo, nullptr, &pipeline_);
vkDestroyShaderModule(renderer_.getDevice(), fragShaderModule, nullptr);
vkDestroyShaderModule(renderer_.getDevice(), vertShaderModule, nullptr);
}

View file

@ -49,19 +49,6 @@ void WorldPass::render(VkCommandBuffer commandBuffer, World& world, Camera& came
vkUnmapMemory(renderer_.getDevice(), lightMemory_); vkUnmapMemory(renderer_.getDevice(), lightMemory_);
std::array<VkClearValue, 2> clearColor = {};
clearColor[1].depthStencil.depth = 1.0f;
VkRenderPassBeginInfo renderPassBeginInfo = {};
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassBeginInfo.framebuffer = target->offscreenFramebuffers[target->currentResource];
renderPassBeginInfo.renderPass = renderPass_;
renderPassBeginInfo.renderArea.extent = target->extent;
renderPassBeginInfo.clearValueCount = clearColor.size();
renderPassBeginInfo.pClearValues = clearColor.data();
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_); vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_);
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout_, 0, 1, &descriptorSet_, 0, nullptr); vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout_, 0, 1, &descriptorSet_, 0, nullptr);
@ -81,8 +68,6 @@ void WorldPass::render(VkCommandBuffer commandBuffer, World& world, Camera& came
vkCmdDrawIndexed(commandBuffer, mesh->indices.size(), 1, 0, 0, 0); vkCmdDrawIndexed(commandBuffer, mesh->indices.size(), 1, 0, 0, 0);
} }
vkCmdEndRenderPass(commandBuffer);
} }
void WorldPass::createRenderPass() { void WorldPass::createRenderPass() {
@ -238,17 +223,17 @@ void WorldPass::createPipeline() {
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
colorBlendAttachment.blendEnable = VK_FALSE; colorBlendAttachment.blendEnable = VK_FALSE;
VkPipelineColorBlendStateCreateInfo colorBlending = {};
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlending.attachmentCount = 1;
colorBlending.pAttachments = &colorBlendAttachment;
VkPipelineDepthStencilStateCreateInfo depthState = {}; VkPipelineDepthStencilStateCreateInfo depthState = {};
depthState.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; depthState.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
depthState.depthTestEnable = VK_TRUE; depthState.depthTestEnable = VK_TRUE;
depthState.depthWriteEnable = VK_TRUE; depthState.depthWriteEnable = VK_TRUE;
depthState.depthCompareOp = VK_COMPARE_OP_LESS; depthState.depthCompareOp = VK_COMPARE_OP_LESS;
VkPipelineColorBlendStateCreateInfo colorBlending = {};
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlending.attachmentCount = 1;
colorBlending.pAttachments = &colorBlendAttachment;
const std::array<VkDynamicState, 2> dynamicStates = { const std::array<VkDynamicState, 2> dynamicStates = {
VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR VK_DYNAMIC_STATE_SCISSOR