Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
graph/src/postpass.cpp

248 lines
11 KiB
C++
Raw Normal View History

2018-10-17 10:06:38 -04:00
#include "postpass.h"
#include <array>
#include "renderer.h"
PostPass::PostPass(Renderer& renderer) : renderer_(renderer) {
createDescriptorSetLayout();
createPipeline();
createSampler();
}
PostPass::~PostPass() {
vkDestroySampler(renderer_.getDevice(), offscreenSampler_, nullptr);
vkDestroyPipelineLayout(renderer_.getDevice(), pipelineLayout_, nullptr);
vkDestroyPipeline(renderer_.getDevice(), pipeline_, nullptr);
vkDestroyDescriptorSetLayout(renderer_.getDevice(), setLayout_, nullptr);
}
2018-10-17 10:06:38 -04:00
void PostPass::render(VkCommandBuffer commandBuffer, RenderTarget* target) {
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_);
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout_, 0, 1, &target->postSets[target->currentImage], 0, nullptr);
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
}
void PostPass::createDescriptorSet(RenderTarget* target) {
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = renderer_.getDescriptorPool();
allocInfo.descriptorSetCount = target->numImages;
// FIXME: lol what
2018-10-26 20:56:06 -04:00
auto layouts = new VkDescriptorSetLayout[target->numImages];
2018-10-17 10:06:38 -04:00
for(uint32_t i = 0; i < target->numImages; i++)
layouts[i] = setLayout_;
allocInfo.pSetLayouts = layouts;
target->postSets = new VkDescriptorSet[target->numImages];
vkAllocateDescriptorSets(renderer_.getDevice(), &allocInfo, target->postSets);
delete[] layouts;
for(uint32_t i = 0; i < target->numImages; i++) {
2018-11-03 07:24:32 -04:00
VkDescriptorImageInfo sceneImageInfo = {};
sceneImageInfo.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
sceneImageInfo.imageView = target->offscreenColorImageViews[i];
sceneImageInfo.sampler = offscreenSampler_;
VkDescriptorImageInfo depthImageInfo = {};
depthImageInfo.imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;;
depthImageInfo.imageView = target->offscreenDepthImageViews[i];
depthImageInfo.sampler = offscreenSampler_;
VkDescriptorImageInfo nearFieldImageInfo = {};
nearFieldImageInfo.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
nearFieldImageInfo.imageView = target->nearFieldImageViews[i];
nearFieldImageInfo.sampler = offscreenSampler_;
VkDescriptorImageInfo farFieldImageInfo = {};
farFieldImageInfo.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
farFieldImageInfo.imageView = target->farFieldImageViews[i];
farFieldImageInfo.sampler = offscreenSampler_;
VkWriteDescriptorSet sceneDescriptorWrite = {};
sceneDescriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
sceneDescriptorWrite.descriptorCount = 1;
sceneDescriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
sceneDescriptorWrite.dstSet = target->postSets[i];
sceneDescriptorWrite.pImageInfo = &sceneImageInfo;
VkWriteDescriptorSet depthDescriptorWrite = {};
depthDescriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
depthDescriptorWrite.descriptorCount = 1;
depthDescriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
depthDescriptorWrite.dstBinding = 1;
depthDescriptorWrite.dstSet = target->postSets[i];
depthDescriptorWrite.pImageInfo = &depthImageInfo;
VkWriteDescriptorSet nearFieldDescriptorWrite = {};
nearFieldDescriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
nearFieldDescriptorWrite.descriptorCount = 1;
nearFieldDescriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
nearFieldDescriptorWrite.dstBinding = 2;
nearFieldDescriptorWrite.dstSet = target->postSets[i];
nearFieldDescriptorWrite.pImageInfo = &nearFieldImageInfo;
VkWriteDescriptorSet farFieldDescriptorWrite = {};
farFieldDescriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
farFieldDescriptorWrite.descriptorCount = 1;
farFieldDescriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
farFieldDescriptorWrite.dstBinding = 3;
farFieldDescriptorWrite.dstSet = target->postSets[i];
farFieldDescriptorWrite.pImageInfo = &farFieldImageInfo;
const std::array<VkWriteDescriptorSet, 4> descriptorWrites = {
sceneDescriptorWrite,
depthDescriptorWrite,
nearFieldDescriptorWrite,
farFieldDescriptorWrite
};
vkUpdateDescriptorSets(renderer_.getDevice(), descriptorWrites.size(), descriptorWrites.data(), 0, nullptr);
2018-10-17 10:06:38 -04:00
}
}
void PostPass::createDescriptorSetLayout() {
VkDescriptorSetLayoutBinding offscreenSamplerBinding = {};
offscreenSamplerBinding.descriptorCount = 1;
offscreenSamplerBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
offscreenSamplerBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
2018-11-03 07:24:32 -04:00
VkDescriptorSetLayoutBinding depthSamplerBinding = {};
depthSamplerBinding.binding = 1;
depthSamplerBinding.descriptorCount = 1;
depthSamplerBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
depthSamplerBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
VkDescriptorSetLayoutBinding nearFieldSamplerBinding = {};
nearFieldSamplerBinding.binding = 2;
nearFieldSamplerBinding.descriptorCount = 1;
nearFieldSamplerBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
nearFieldSamplerBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
VkDescriptorSetLayoutBinding farFieldSamplerBinding = {};
farFieldSamplerBinding.binding = 3;
farFieldSamplerBinding.descriptorCount = 1;
farFieldSamplerBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
farFieldSamplerBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
const std::array<VkDescriptorSetLayoutBinding, 4> bindings = {
offscreenSamplerBinding,
depthSamplerBinding,
nearFieldSamplerBinding,
farFieldSamplerBinding
};
2018-10-17 10:06:38 -04:00
VkDescriptorSetLayoutCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
2018-11-03 07:24:32 -04:00
createInfo.bindingCount = bindings.size();
createInfo.pBindings = bindings.data();
2018-10-17 10:06:38 -04:00
vkCreateDescriptorSetLayout(renderer_.getDevice(), &createInfo, nullptr, &setLayout_);
}
void PostPass::createPipeline() {
VkShaderModule vertShaderModule = renderer_.createShader("shaders/post.vert.spv");
VkShaderModule fragShaderModule = renderer_.createShader("shaders/post.frag.spv");
2018-10-17 10:06:38 -04:00
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.lineWidth = 1.0f;
rasterizer.cullMode = VK_CULL_MODE_FRONT_BIT;
rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
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;
colorBlendAttachment.blendEnable = VK_FALSE;
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 = {
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;
pipelineLayoutInfo.setLayoutCount = 1;
pipelineLayoutInfo.pSetLayouts = &setLayout_;
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.pDynamicState = &dynamicState;
pipelineInfo.layout = pipelineLayout_;
pipelineInfo.renderPass = renderer_.getRenderPass();
vkCreateGraphicsPipelines(renderer_.getDevice(), nullptr, 1, &pipelineInfo, nullptr, &pipeline_);
vkDestroyShaderModule(renderer_.getDevice(), fragShaderModule, nullptr);
vkDestroyShaderModule(renderer_.getDevice(), vertShaderModule, nullptr);
}
void PostPass::createSampler() {
VkSamplerCreateInfo samplerInfo = {};
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerInfo.magFilter = VK_FILTER_LINEAR;
samplerInfo.minFilter = VK_FILTER_LINEAR;
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
vkCreateSampler(renderer_.getDevice(), &samplerInfo, nullptr, &offscreenSampler_);
}