2018-11-03 07:24:32 -04:00
|
|
|
#include "dofpass.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <glm/glm.hpp>
|
2018-11-05 10:26:54 -05:00
|
|
|
#include <stb_image.h>
|
2018-11-03 07:24:32 -04:00
|
|
|
|
|
|
|
#include "renderer.h"
|
|
|
|
|
|
|
|
DoFPass::DoFPass(Renderer& renderer) : renderer_(renderer) {
|
|
|
|
createRenderPass();
|
|
|
|
createDescriptorSetLayout();
|
|
|
|
createPipeline();
|
|
|
|
createBokehImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
DoFPass::~DoFPass() {
|
|
|
|
vkDestroySampler(renderer_.getDevice(), bokehSampler_, nullptr);
|
|
|
|
vkDestroyImageView(renderer_.getDevice(), bokehImageView_, nullptr);
|
|
|
|
vkFreeMemory(renderer_.getDevice(), bokehMemory_, nullptr);
|
|
|
|
vkDestroyImage(renderer_.getDevice(), bokehImage_, nullptr);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkDestroyPipeline(renderer_.getDevice(), pipeline_, nullptr);
|
|
|
|
vkDestroyPipelineLayout(renderer_.getDevice(), pipelineLayout_, nullptr);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkDestroyDescriptorSetLayout(renderer_.getDevice(), setLayout_, nullptr);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkDestroyRenderPass(renderer_.getDevice(), renderPass_, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoFPass::render(VkCommandBuffer commandBuffer, RenderTarget* target) {
|
|
|
|
VkViewport viewport = {};
|
|
|
|
viewport.width = target->extent.width / 2;
|
|
|
|
viewport.height = target->extent.height / 2;
|
|
|
|
viewport.maxDepth = 1.0f;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkRect2D scissor = {};
|
|
|
|
scissor.extent.width = target->extent.width / 2;
|
|
|
|
scissor.extent.height = target->extent.height / 2;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkClearValue clearColor = {};
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkRenderPassBeginInfo renderPassBeginInfo = {};
|
|
|
|
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
|
|
|
renderPassBeginInfo.framebuffer = target->farFieldFramebuffers[target->currentImage];
|
|
|
|
renderPassBeginInfo.renderPass = renderPass_;
|
|
|
|
renderPassBeginInfo.renderArea.extent.width = target->extent.width / 2;
|
|
|
|
renderPassBeginInfo.renderArea.extent.height = target->extent.height / 2;
|
|
|
|
renderPassBeginInfo.clearValueCount = 1;
|
|
|
|
renderPassBeginInfo.pClearValues = &clearColor;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
// far field
|
|
|
|
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
glm::vec4 dpack;
|
|
|
|
dpack[0] = 0;
|
|
|
|
dpack[1] = 0.9581;
|
|
|
|
dpack[2] = target->extent.width / 2;
|
|
|
|
dpack[3] = target->extent.height / 2;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_);
|
|
|
|
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout_, 0, 1, &target->dofSets[target->currentImage], 0, nullptr);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCmdPushConstants(commandBuffer, pipelineLayout_, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(glm::vec4), &dpack);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCmdDraw(commandBuffer, 3, (target->extent.width / 2) * (target->extent.height / 2), 0, 0);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCmdEndRenderPass(commandBuffer);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
//near field
|
|
|
|
renderPassBeginInfo.framebuffer = target->nearFieldFramebuffers[target->currentImage];
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_);
|
|
|
|
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout_, 0, 1, &target->dofSets[target->currentImage], 0, nullptr);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
dpack[0] = 1;
|
|
|
|
dpack[1] = 0.9581;
|
|
|
|
|
|
|
|
vkCmdPushConstants(commandBuffer, pipelineLayout_, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(glm::vec4), &dpack);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
//FIXME: near field is bugged
|
|
|
|
//vkCmdDraw(commandBuffer, 3, (target->extent.width / 2) * (target->extent.height / 2), 0, 0);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCmdEndRenderPass(commandBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoFPass::createDescriptorSet(RenderTarget* target) {
|
|
|
|
VkDescriptorSetAllocateInfo allocInfo = {};
|
|
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
|
|
|
allocInfo.descriptorPool = renderer_.getDescriptorPool();
|
|
|
|
allocInfo.descriptorSetCount = target->numImages;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
// FIXME: lol what
|
|
|
|
auto layouts = new VkDescriptorSetLayout[target->numImages];
|
|
|
|
for(uint32_t i = 0; i < target->numImages; i++)
|
|
|
|
layouts[i] = setLayout_;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
allocInfo.pSetLayouts = layouts;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
target->dofSets = new VkDescriptorSet[target->numImages];
|
|
|
|
vkAllocateDescriptorSets(renderer_.getDevice(), &allocInfo, target->dofSets);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
delete[] layouts;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
for(uint32_t i = 0; i < target->numImages; i++) {
|
|
|
|
VkDescriptorImageInfo bokehImageInfo = {};
|
|
|
|
bokehImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
|
|
bokehImageInfo.imageView = bokehImageView_;
|
|
|
|
bokehImageInfo.sampler = bokehSampler_;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
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 = bokehSampler_;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkDescriptorImageInfo depthImageInfo = {};
|
|
|
|
depthImageInfo.imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;;
|
|
|
|
depthImageInfo.imageView = target->offscreenDepthImageViews[i];
|
|
|
|
depthImageInfo.sampler = bokehSampler_;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkWriteDescriptorSet bokehDescriptorWrite = {};
|
|
|
|
bokehDescriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
|
|
bokehDescriptorWrite.descriptorCount = 1;
|
|
|
|
bokehDescriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
bokehDescriptorWrite.dstSet = target->dofSets[i];
|
|
|
|
bokehDescriptorWrite.pImageInfo = &bokehImageInfo;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkWriteDescriptorSet sceneDescriptorWrite = {};
|
|
|
|
sceneDescriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
|
|
sceneDescriptorWrite.descriptorCount = 1;
|
|
|
|
sceneDescriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
sceneDescriptorWrite.dstBinding = 1;
|
|
|
|
sceneDescriptorWrite.dstSet = target->dofSets[i];
|
|
|
|
sceneDescriptorWrite.pImageInfo = &sceneImageInfo;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkWriteDescriptorSet depthDescriptorWrite = {};
|
|
|
|
depthDescriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
|
|
depthDescriptorWrite.descriptorCount = 1;
|
|
|
|
depthDescriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
depthDescriptorWrite.dstBinding = 2;
|
|
|
|
depthDescriptorWrite.dstSet = target->dofSets[i];
|
|
|
|
depthDescriptorWrite.pImageInfo = &depthImageInfo;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
const std::array<VkWriteDescriptorSet, 3> descriptorWrites = {
|
|
|
|
bokehDescriptorWrite,
|
|
|
|
sceneDescriptorWrite,
|
|
|
|
depthDescriptorWrite
|
|
|
|
};
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkUpdateDescriptorSets(renderer_.getDevice(), descriptorWrites.size(), descriptorWrites.data(), 0, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoFPass::createRenderPass() {
|
|
|
|
VkAttachmentDescription colorAttachment = {};
|
|
|
|
colorAttachment.format = VK_FORMAT_R32G32B32A32_SFLOAT;
|
|
|
|
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
|
|
|
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
|
|
|
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
|
|
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
|
|
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
|
|
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkAttachmentReference colorAttachmentRef = {};
|
|
|
|
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkSubpassDescription subpass = {};
|
|
|
|
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
|
|
|
subpass.colorAttachmentCount = 1;
|
|
|
|
subpass.pColorAttachments = &colorAttachmentRef;
|
|
|
|
|
|
|
|
VkRenderPassCreateInfo renderPassInfo = {};
|
|
|
|
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
|
|
|
renderPassInfo.attachmentCount = 1;
|
|
|
|
renderPassInfo.pAttachments = &colorAttachment;
|
|
|
|
renderPassInfo.subpassCount = 1;
|
|
|
|
renderPassInfo.pSubpasses = &subpass;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCreateRenderPass(renderer_.getDevice(), &renderPassInfo, nullptr, &renderPass_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoFPass::createDescriptorSetLayout() {
|
|
|
|
VkDescriptorSetLayoutBinding bokehSamplerBinding = {};
|
|
|
|
bokehSamplerBinding.descriptorCount = 1;
|
|
|
|
bokehSamplerBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
bokehSamplerBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkDescriptorSetLayoutBinding sceneSamplerBinding = {};
|
|
|
|
sceneSamplerBinding.binding = 1;
|
|
|
|
sceneSamplerBinding.descriptorCount = 1;
|
|
|
|
sceneSamplerBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
sceneSamplerBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkDescriptorSetLayoutBinding depthSamplerBinding = {};
|
|
|
|
depthSamplerBinding.binding = 2;
|
|
|
|
depthSamplerBinding.descriptorCount = 1;
|
|
|
|
depthSamplerBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
depthSamplerBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
const std::array<VkDescriptorSetLayoutBinding, 3> bindings = {
|
|
|
|
bokehSamplerBinding,
|
|
|
|
sceneSamplerBinding,
|
|
|
|
depthSamplerBinding
|
|
|
|
};
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkDescriptorSetLayoutCreateInfo createInfo = {};
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
|
|
|
createInfo.bindingCount = bindings.size();
|
|
|
|
createInfo.pBindings = bindings.data();
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCreateDescriptorSetLayout(renderer_.getDevice(), &createInfo, nullptr, &setLayout_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoFPass::createPipeline() {
|
|
|
|
VkShaderModule vertShaderModule = renderer_.createShader("shaders/gfield.vert.spv");
|
|
|
|
VkShaderModule fragShaderModule = renderer_.createShader("shaders/gfield.frag.spv");
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -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";
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
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";
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
const std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages = {vertShaderStageInfo, fragShaderStageInfo};
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
|
|
|
|
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
|
|
|
|
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
|
|
|
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkPipelineViewportStateCreateInfo viewportState = {};
|
|
|
|
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
|
|
|
viewportState.viewportCount = 1;
|
|
|
|
viewportState.scissorCount = 1;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
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;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkPipelineMultisampleStateCreateInfo multisampling = {};
|
|
|
|
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
|
|
|
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
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_TRUE;
|
|
|
|
colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
|
|
|
|
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
|
|
|
|
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE;
|
|
|
|
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
|
|
|
|
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
|
|
|
|
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkPipelineColorBlendStateCreateInfo colorBlending = {};
|
|
|
|
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
|
|
|
colorBlending.attachmentCount = 1;
|
|
|
|
colorBlending.pAttachments = &colorBlendAttachment;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
const std::array<VkDynamicState, 2> dynamicStates = {
|
|
|
|
VK_DYNAMIC_STATE_VIEWPORT,
|
|
|
|
VK_DYNAMIC_STATE_SCISSOR
|
|
|
|
};
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkPipelineDynamicStateCreateInfo dynamicState = {};
|
|
|
|
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
|
|
|
|
dynamicState.dynamicStateCount = dynamicStates.size();
|
|
|
|
dynamicState.pDynamicStates = dynamicStates.data();
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkPushConstantRange pushConstant = {};
|
|
|
|
pushConstant.size = sizeof(glm::vec4);
|
|
|
|
pushConstant.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
|
|
|
|
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
|
|
|
pipelineLayoutInfo.setLayoutCount = 1;
|
|
|
|
pipelineLayoutInfo.pSetLayouts = &setLayout_;
|
|
|
|
pipelineLayoutInfo.pushConstantRangeCount = 1;
|
|
|
|
pipelineLayoutInfo.pPushConstantRanges = &pushConstant;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCreatePipelineLayout(renderer_.getDevice(), &pipelineLayoutInfo, nullptr, &pipelineLayout_);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
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 = renderPass_;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCreateGraphicsPipelines(renderer_.getDevice(), nullptr, 1, &pipelineInfo, nullptr, &pipeline_);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkDestroyShaderModule(renderer_.getDevice(), fragShaderModule, nullptr);
|
|
|
|
vkDestroyShaderModule(renderer_.getDevice(), vertShaderModule, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoFPass::createBokehImage() {
|
2018-11-03 20:36:05 -04:00
|
|
|
int width = 0, height = 0, channels = 0;
|
2018-11-03 07:24:32 -04:00
|
|
|
stbi_uc* pixels = stbi_load("data/bokeh.png", &width, &height, &channels, STBI_rgb_alpha);
|
2018-11-03 20:36:05 -04:00
|
|
|
if(pixels == nullptr)
|
|
|
|
return; // haha what
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkImageCreateInfo imageCreateInfo = {};
|
|
|
|
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
|
|
|
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
|
|
|
|
imageCreateInfo.extent.width = width;
|
|
|
|
imageCreateInfo.extent.height = height;
|
|
|
|
imageCreateInfo.extent.depth = 1;
|
|
|
|
imageCreateInfo.mipLevels = 1;
|
|
|
|
imageCreateInfo.arrayLayers = 1;
|
|
|
|
imageCreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
|
|
|
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
|
|
imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
|
|
|
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCreateImage(renderer_.getDevice(), &imageCreateInfo, nullptr, &bokehImage_);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkMemoryRequirements memRequirements;
|
|
|
|
vkGetImageMemoryRequirements(renderer_.getDevice(), bokehImage_, &memRequirements);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkMemoryAllocateInfo allocInfo = {};
|
|
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
|
|
allocInfo.allocationSize = memRequirements.size;
|
|
|
|
allocInfo.memoryTypeIndex = renderer_.findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkAllocateMemory(renderer_.getDevice(), &allocInfo, nullptr, &bokehMemory_);
|
|
|
|
vkBindImageMemory(renderer_.getDevice(), bokehImage_, bokehMemory_, 0);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkBuffer stagingBuffer;
|
|
|
|
VkDeviceMemory stagingMemory;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkBufferCreateInfo bufferInfo = {};
|
|
|
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
|
|
|
bufferInfo.size = width * height * 4;
|
|
|
|
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
|
|
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCreateBuffer(renderer_.getDevice(), &bufferInfo, nullptr, &stagingBuffer);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkGetBufferMemoryRequirements(renderer_.getDevice(), stagingBuffer, &memRequirements);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
allocInfo = {};
|
|
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
|
|
allocInfo.allocationSize = memRequirements.size;
|
|
|
|
allocInfo.memoryTypeIndex = renderer_.findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkAllocateMemory(renderer_.getDevice(), &allocInfo, nullptr, &stagingMemory);
|
|
|
|
vkBindBufferMemory(renderer_.getDevice(), stagingBuffer, stagingMemory, 0);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
void* data;
|
|
|
|
vkMapMemory(renderer_.getDevice(), stagingMemory, 0, width * height * 4, 0, &data);
|
|
|
|
memcpy(data, pixels, width * height * 4);
|
|
|
|
vkUnmapMemory(renderer_.getDevice(), stagingMemory);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
stbi_image_free(pixels);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkCommandBufferAllocateInfo bufferAllocateInfo = {};
|
|
|
|
bufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
|
|
|
bufferAllocateInfo.commandPool = renderer_.getCommandPool();
|
|
|
|
bufferAllocateInfo.commandBufferCount = 1;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkCommandBuffer commandBuffer = nullptr;
|
|
|
|
vkAllocateCommandBuffers(renderer_.getDevice(), &bufferAllocateInfo, &commandBuffer);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkCommandBufferBeginInfo beginInfo = {};
|
|
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
// change layout to transfer dst
|
|
|
|
{
|
|
|
|
VkImageMemoryBarrier imageMemoryBarrier = {};
|
|
|
|
imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
|
|
|
imageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
|
|
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
|
|
|
imageMemoryBarrier.image = bokehImage_;
|
|
|
|
imageMemoryBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
imageMemoryBarrier.subresourceRange.layerCount = 1;
|
|
|
|
imageMemoryBarrier.subresourceRange.levelCount = 1;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCmdPipelineBarrier(
|
|
|
|
commandBuffer,
|
|
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
0,
|
|
|
|
0, nullptr,
|
|
|
|
0, nullptr,
|
|
|
|
1, &imageMemoryBarrier);
|
|
|
|
}
|
2018-11-05 10:26:54 -05:00
|
|
|
|
|
|
|
VkBufferImageCopy region = {};
|
2018-11-03 07:24:32 -04:00
|
|
|
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
region.imageSubresource.layerCount = 1;
|
|
|
|
region.imageExtent.width = width;
|
|
|
|
region.imageExtent.height = height;
|
|
|
|
region.imageExtent.depth = 1;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCmdCopyBufferToImage(
|
|
|
|
commandBuffer,
|
|
|
|
stagingBuffer,
|
|
|
|
bokehImage_,
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
|
|
1,
|
|
|
|
®ion
|
|
|
|
);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
// change layout to shader read only
|
|
|
|
{
|
|
|
|
VkImageMemoryBarrier imageMemoryBarrier = {};
|
|
|
|
imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
|
|
|
imageMemoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
|
|
imageMemoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
|
|
imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
|
|
|
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
|
|
imageMemoryBarrier.image = bokehImage_;
|
|
|
|
imageMemoryBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
imageMemoryBarrier.subresourceRange.layerCount = 1;
|
|
|
|
imageMemoryBarrier.subresourceRange.levelCount = 1;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCmdPipelineBarrier(
|
|
|
|
commandBuffer,
|
|
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
|
|
|
|
0,
|
|
|
|
0, nullptr,
|
|
|
|
0, nullptr,
|
|
|
|
1, &imageMemoryBarrier);
|
|
|
|
}
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkEndCommandBuffer(commandBuffer);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkSubmitInfo submitInfo = {};
|
|
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
submitInfo.pCommandBuffers = &commandBuffer;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkFenceCreateInfo fenceCreateInfo = {};
|
|
|
|
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkFence fence = nullptr;
|
|
|
|
vkCreateFence(renderer_.getDevice(), &fenceCreateInfo, nullptr, &fence);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkQueueSubmit(renderer_.getGraphicsQueue(), 1, &submitInfo, fence);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkWaitForFences(renderer_.getDevice(), 1, &fence, VK_TRUE, -1);
|
|
|
|
vkDestroyFence(renderer_.getDevice(), fence, nullptr);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkFreeCommandBuffers(renderer_.getDevice(), renderer_.getCommandPool(), 1, &commandBuffer);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkFreeMemory(renderer_.getDevice(), stagingMemory, nullptr);
|
|
|
|
vkDestroyBuffer(renderer_.getDevice(), stagingBuffer, nullptr);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
VkImageViewCreateInfo createInfo = {};
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
|
|
|
createInfo.image = bokehImage_;
|
|
|
|
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
|
|
createInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
createInfo.subresourceRange.levelCount = 1;
|
|
|
|
createInfo.subresourceRange.layerCount = 1;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCreateImageView(renderer_.getDevice(), &createInfo, nullptr, &bokehImageView_);
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
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_CLAMP_TO_BORDER;
|
|
|
|
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
|
|
|
|
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
|
|
|
|
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
|
|
|
|
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
2018-11-05 10:26:54 -05:00
|
|
|
|
2018-11-03 07:24:32 -04:00
|
|
|
vkCreateSampler(renderer_.getDevice(), &samplerInfo, nullptr, &bokehSampler_);
|
|
|
|
}
|