From b57f74012fcdff0e03b48ce143b83bbb1b96bbba Mon Sep 17 00:00:00 2001 From: saschawillems Date: Wed, 11 Jan 2017 21:25:53 +0100 Subject: [PATCH] Added transparent forward pass as third subpass, demonstrate preserve attachment --- data/shaders/subpasses/gbuffer.frag | 14 +- data/shaders/subpasses/gbuffer.frag.spv | Bin 1576 -> 2300 bytes data/shaders/subpasses/transparent.frag | 33 ++++ data/shaders/subpasses/transparent.frag.spv | Bin 0 -> 1848 bytes data/shaders/subpasses/transparent.vert | 27 ++++ data/shaders/subpasses/transparent.vert.spv | Bin 0 -> 1916 bytes subpasses/subpasses.cpp | 159 ++++++++++++++++---- subpasses/subpasses.vcxproj | 2 + subpasses/subpasses.vcxproj.filters | 6 + 9 files changed, 214 insertions(+), 27 deletions(-) create mode 100644 data/shaders/subpasses/transparent.frag create mode 100644 data/shaders/subpasses/transparent.frag.spv create mode 100644 data/shaders/subpasses/transparent.vert create mode 100644 data/shaders/subpasses/transparent.vert.spv diff --git a/data/shaders/subpasses/gbuffer.frag b/data/shaders/subpasses/gbuffer.frag index dee43e80..bf516ae2 100644 --- a/data/shaders/subpasses/gbuffer.frag +++ b/data/shaders/subpasses/gbuffer.frag @@ -15,6 +15,15 @@ layout (location = 1) out vec4 outPosition; layout (location = 2) out vec4 outNormal; layout (location = 3) out vec4 outAlbedo; +layout (constant_id = 0) const float NEAR_PLANE = 0.1f; +layout (constant_id = 1) const float FAR_PLANE = 256.0f; + +float linearDepth(float depth) +{ + float z = depth * 2.0f - 1.0f; + return (2.0f * NEAR_PLANE * FAR_PLANE) / (FAR_PLANE + NEAR_PLANE - z * (FAR_PLANE - NEAR_PLANE)); +} + void main() { outPosition = vec4(inWorldPos, 1.0); @@ -23,7 +32,10 @@ void main() N.y = -N.y; outNormal = vec4(N, 1.0); - outAlbedo = vec4(inColor, 1.0); + outAlbedo.rgb = inColor; + + // Store linearized depth in alpha component + outPosition.a = linearDepth(gl_FragCoord.z); // Write color attachments to avoid undefined behaviour (validation error) outColor = vec4(0.0); diff --git a/data/shaders/subpasses/gbuffer.frag.spv b/data/shaders/subpasses/gbuffer.frag.spv index 3fad360abf08e872d8f12840348e6bbff42f53fb..0c8cf70009c9e5533fa1e211c97fa78fff1b5937 100644 GIT binary patch literal 2300 zcmY+ETT>H35QRrb5ClQ+u2s5-_X{H8714yKQA9zo%E#1d)))=h)F!_8qLyWqzsqO+ zKfY*rzR9$%Lp9y!bWhK8PiF@QX0|)GO@G7gnd`4fHz4fXs2lRSy1KbK+wIk77p}|^ zG42L^ATbkerz=P!TXD0EBa+NW&Pf&|HzkXbl4M1)OZ$n}!LkN*nseWjtl&mmb+uOB zSgv)GPMpQPq}F{M*ORQ4zIc~;T-k*nEm)LL=7@h)y8wT1b)PWpQl-? zF229#tm2=9J=EN6KS{H#xFyr?p$E$Imn0885m#=#Nb0GNEBGEY+m*DHW{Mc{{c5yo z_p`WBNz<&ZF!2RfmZm*h9v6L2m8-web3<-jx*KnGT1l4Ux4WnR`2Kw!#~o3ws=QOu zdIrD1djf_7N76?E2j-2zydRj_!7MO!=l#?SW_s{56JX0AIhfT*|Bp)38~DY5qreeQEyNQ;Jhg6;*>KW~J8ul%#pk%Yzl!51a??R?&l^9=KbhB=QnB%br`1l#TpE(Ol=FAEPyh_3`6 zHJQIEoBa461RKAd&rIOAem|1UIGgnIzW`9ky1il?O;&@lyl3ICNY9N*vYOF}#m6RmQ z66*_ngeSf%4ZacH_rB~M5_od(TX-mmBvlD}z{Fv%NHhDt)Pd^~W{&%igEcI&ooToO3h!&AX19jukg6-?;@>&*xtMbHANU2JO!C0~m|0 zk`v)9xrVE1#zuY^6SyR6$kt?cWcOsdvVGaIa^Y%Itt#5dxnC+&RaUR>bw9oHvS93| z{v`0Sul^uNz2x*;a5l;Q=O`4$Lof2<;kiEyyw2gl*gyO3YN}M$QWJunlJlzhS;P2! z5+!M#$GOFs0y(Gd4mD@~XdDG;37>P%{^8SoKKNrnu9x4CbGNnT-toC(eC`k*&G-y_ z^p@jj#ix73=?T8oLuWZ3joUH?eEgQo_-HN1@rk2nRsYPbyvtK;UF*8Arffs?cmw$K ztEvBa&1eIERrvG{4E|-!@bTdUt*Bk*b7%0^6u+*y zDPxe2zbPY5{teBh+4OP8=rNx=HXl8^GV(3X9TPWwkF{p(t1`9QGO)63>IFX$#(Ah! z9_DYUDjz32v_36JVC;=g{}|@eFUAYKQhG%n80h;gq^21z_r|w@zSr`(zbi8C`lDFr zS}HJ27ua%vq0x9N1;&0S#hI^aO+I|$^z4mP(0gLG=epMPtDzmyupy)Nk&GBQ=mhUd z7ya6jfze0mp!KE<{72#6Dlp688?YQ|;=?f=%_44RYRLy<7`Cl-Q-(gnb_%R~_PfH! zhfmzj&buOJd*0WYw`8>*Xbr|N?4j0R^^6q5uE@ diff --git a/data/shaders/subpasses/transparent.frag b/data/shaders/subpasses/transparent.frag new file mode 100644 index 00000000..95dbc5c6 --- /dev/null +++ b/data/shaders/subpasses/transparent.frag @@ -0,0 +1,33 @@ +#version 450 + +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable + +layout (input_attachment_index = 1, binding = 1) uniform subpassInput samplerPositionDepth; + +layout (location = 0) in vec3 inColor; +layout (location = 1) in vec2 inUV; + +layout (location = 0) out vec4 outColor; + +layout (constant_id = 0) const float NEAR_PLANE = 0.1f; +layout (constant_id = 1) const float FAR_PLANE = 256.0f; + +float linearDepth(float depth) +{ + float z = depth * 2.0f - 1.0f; + return (2.0f * NEAR_PLANE * FAR_PLANE) / (FAR_PLANE + NEAR_PLANE - z * (FAR_PLANE - NEAR_PLANE)); +} + +void main () +{ + // Sample depth from deferred depth buffer and discard if obscured + float depth = subpassLoad(samplerPositionDepth).a; + if (linearDepth(gl_FragCoord.z) > depth) + { + discard; + }; + + outColor.rgb = vec3(0.0, 0.25, 0.86); + outColor.a = 0.5; +} diff --git a/data/shaders/subpasses/transparent.frag.spv b/data/shaders/subpasses/transparent.frag.spv new file mode 100644 index 0000000000000000000000000000000000000000..38d392f12e6302107b6e0c0356d21ca488c1c2ba GIT binary patch literal 1848 zcmY+ETTfF#6om(9p@4Fcn`ojG6sjNsa#Mr?Me2pbTlC4KInZONmY$|9FFs+6iNDE5 zpYiXgF=|}jbS5d&?C!nRUbAP<%sHp6b)v(V7X1?DiD`^}(<*EXJ|*96+AYrRz1*9v z)$_B9OLHiDOdHPTdQGQ^DaMY1Vj2FHY)zJxJ(l%JPhJZuYEyM%zGg5at#a<|@I#dlOfs=Jn(1yGv`7 zisdk4Ve0Mtn$>lR&%?;a)yp~g4>pdOD`#>ElN7E6N0m}oeHPV<^`W)-lVBdd$svxtuCnWz+1y@sZc&&gFq)WDhteKE#V zW6VazYS$c2I&MgK!s;4(q6hFbmmd+nt{9G*GrD2$qzny)W=ftOVlrypkROvVs0qF) z;|-yw8Tl3&^XLi8UBG$$ZN;pKo)?T9YYd8Aa{N7cYWg~0?huZ??#nX=?9X>yF~j2x zi%vE?P7D7?y-Ck#Ci6FC%t1%a>oSTp()72ZfHhbPyem(?%x8b#tmEkGp<=Xte~(-a z9zB?6jT*~3b99_7gLQt1iOZwykms4id(68AURR!HWL%b%?Q7PQe9Cc?^3TQ53-1Ly z^*8iSO}H)PIsbVXcf`F=mpKbEc=W6ZzU%7UQQRei_xeS})cqsHY{|Iur+Od2+m73m=gc@KdiDE4+t9@Ay*&_y@Tu{>*1DjD&1OI?lIKMn8N*{MqyEB|U+ZF%#@8AZhmM@rT~bUva$j8i%Zgc-+&}Fb H%o+U$LlT1p literal 0 HcmV?d00001 diff --git a/data/shaders/subpasses/transparent.vert b/data/shaders/subpasses/transparent.vert new file mode 100644 index 00000000..97c36262 --- /dev/null +++ b/data/shaders/subpasses/transparent.vert @@ -0,0 +1,27 @@ +#version 450 + +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable + +layout (location = 0) in vec4 inPos; +layout (location = 1) in vec3 inColor; +layout (location = 2) in vec3 inNormal; +layout (location = 3) in vec2 inUV; + +layout (binding = 0) uniform UBO +{ + mat4 projection; + mat4 model; + mat4 view; +} ubo; + +layout (location = 0) out vec3 outColor; +layout (location = 1) out vec2 outUV; + +void main () +{ + outColor = inColor; + outUV = inUV; + + gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPos.xyz, 1.0); +} \ No newline at end of file diff --git a/data/shaders/subpasses/transparent.vert.spv b/data/shaders/subpasses/transparent.vert.spv new file mode 100644 index 0000000000000000000000000000000000000000..b5ab165dc2ade21deac0a5f57caa280a49f77398 GIT binary patch literal 1916 zcmZ9N*-jKu5QdMy3@8X9vZx3HxTA>3mKYbnWVj%L61eR&&>35s?#WD#8n1W-eI%dC z8x#NUOcxnEq^SD;T27s+CUker^*VP+KYec9wa>8Y5_YcNmA$U+?C&fz^OJ?;l|>>3 zUAGS;X2=bAyGOrI!#D+8mh?-8B*T)Mk|&ZiNw*xhfC!h}#y!TsS2OH4M*r_9CZ`-Cgr> z?L6-7%mb76R${f_{?Fs?&O9)AXA;YUvpjmHFF5AK{AVQ`!!vE@F@G5Kaf;}dyEM1X|t3LOF@mGZV3LE}qf!V3~ugJD{;s@JasCQMEdfY|5L1}W~i3jry zSUqM;J@bS4Cg7+yB27K^IjY|`q$?86e1S)06F(_DCQUrtlq~jdHSi(yH6cw8_~{GG z-w+)4+>)+HsLMUz+Y-)%@T4?#>1(R^mf%ddgS{{Z_Q@S!_Gz=J6gD|_$6Ub~ejrQ_ z)SEAC?xh}>{acT``KVo vertexLayout = vkMeshLoader::VERTEX_LAYOUT_POSITION, vkMeshLoader::VERTEX_LAYOUT_COLOR, vkMeshLoader::VERTEX_LAYOUT_NORMAL, + vkMeshLoader::VERTEX_LAYOUT_UV, }; class VulkanExample : public VulkanExampleBase @@ -39,6 +40,7 @@ class VulkanExample : public VulkanExampleBase public: struct { vkMeshLoader::MeshBuffer scene; + vkMeshLoader::MeshBuffer transparent; } meshes; struct { @@ -72,21 +74,25 @@ public: struct { VkPipeline offscreen; VkPipeline composition; + VkPipeline transparent; } pipelines; struct { VkPipelineLayout offscreen; VkPipelineLayout composition; + VkPipelineLayout transparent; } pipelineLayouts; struct { VkDescriptorSet scene; VkDescriptorSet composition; + VkDescriptorSet transparent; } descriptorSets; struct { VkDescriptorSetLayout scene; VkDescriptorSetLayout composition; + VkDescriptorSetLayout transparent; } descriptorSetLayouts; // G-Buffer framebuffer attachments @@ -133,15 +139,18 @@ public: vkDestroyPipeline(device, pipelines.offscreen, nullptr); vkDestroyPipeline(device, pipelines.composition, nullptr); - + vkDestroyPipeline(device, pipelines.transparent, nullptr); + vkDestroyPipelineLayout(device, pipelineLayouts.offscreen, nullptr); vkDestroyPipelineLayout(device, pipelineLayouts.composition, nullptr); + vkDestroyPipelineLayout(device, pipelineLayouts.transparent, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.scene, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.composition, nullptr); + vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.transparent, nullptr); - vkMeshLoader::freeMeshBufferResources(device, &meshes.scene); - + meshes.scene.destroy(); + meshes.transparent.destroy(); uniformBuffers.GBuffer.destroy(); uniformBuffers.lights.destroy(); } @@ -294,9 +303,11 @@ public: attachments[4].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; attachments[4].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; - std::array subpassDescriptions{}; + // Three subpasses + std::array subpassDescriptions{}; // First subpass: Fill G-Buffer components + // ---------------------------------------------------------------------------------------- VkAttachmentReference colorReferences[4]; colorReferences[0] = { 0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL }; @@ -311,6 +322,7 @@ public: subpassDescriptions[0].pDepthStencilAttachment = &depthReference; // Second subpass: Final composition (using G-Buffer components) + // ---------------------------------------------------------------------------------------- VkAttachmentReference colorReference = { 0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL }; @@ -319,15 +331,35 @@ public: inputReferences[1] = { 2, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL }; inputReferences[2] = { 3, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL }; + uint32_t preserveAttachmentIndex = 1; + subpassDescriptions[1].pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; subpassDescriptions[1].colorAttachmentCount = 1; subpassDescriptions[1].pColorAttachments = &colorReference; subpassDescriptions[1].pDepthStencilAttachment = &depthReference; + // Use the color attachments filled in the first pass as input attachments subpassDescriptions[1].inputAttachmentCount = 3; subpassDescriptions[1].pInputAttachments = inputReferences; + // Preserve attachment 1 (position + depth) for next subpass + subpassDescriptions[1].preserveAttachmentCount = 1; + subpassDescriptions[1].pPreserveAttachments = &preserveAttachmentIndex; + + // Third subpass: Forward transparency + // ---------------------------------------------------------------------------------------- + colorReference = { 0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL }; + + inputReferences[0] = { 1, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL }; + + subpassDescriptions[2].pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; + subpassDescriptions[2].colorAttachmentCount = 1; + subpassDescriptions[2].pColorAttachments = &colorReference; + subpassDescriptions[2].pDepthStencilAttachment = &depthReference; + // Use the color/depth attachments filled in the first pass as input attachments + subpassDescriptions[2].inputAttachmentCount = 1; + subpassDescriptions[2].pInputAttachments = inputReferences; // Subpass dependencies for layout transitions - std::array dependencies; + std::array dependencies; dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL; dependencies[0].dstSubpass = 0; @@ -346,14 +378,22 @@ public: dependencies[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT; dependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; - dependencies[2].srcSubpass = 0; - dependencies[2].dstSubpass = VK_SUBPASS_EXTERNAL; + dependencies[2].srcSubpass = 1; + dependencies[2].dstSubpass = 2; dependencies[2].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - dependencies[2].dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; - dependencies[2].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; - dependencies[2].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; + dependencies[2].dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; + dependencies[2].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; + dependencies[2].dstAccessMask = VK_ACCESS_SHADER_READ_BIT; dependencies[2].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; + dependencies[3].srcSubpass = 0; + dependencies[3].dstSubpass = VK_SUBPASS_EXTERNAL; + dependencies[3].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + dependencies[3].dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; + dependencies[3].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; + dependencies[3].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; + dependencies[3].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; + VkRenderPassCreateInfo renderPassInfo = {}; renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; renderPassInfo.attachmentCount = static_cast(attachments.size()); @@ -422,6 +462,16 @@ public: vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.composition, 0, 1, &descriptorSets.composition, 0, NULL); vkCmdDraw(drawCmdBuffers[i], 3, 1, 0, 0); + // Third subpass + // Render transparent geometry using a forward pass that compares against depth generted during G-Buffer fill + vkCmdNextSubpass(drawCmdBuffers[i], VK_SUBPASS_CONTENTS_INLINE); + + vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.transparent); + vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.transparent, 0, 1, &descriptorSets.transparent, 0, NULL); + vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.transparent.vertices.buf, offsets); + vkCmdBindIndexBuffer(drawCmdBuffers[i], meshes.transparent.indices.buf, 0, VK_INDEX_TYPE_UINT32); + vkCmdDrawIndexed(drawCmdBuffers[i], meshes.transparent.indexCount, 1, 0, 0, 0); + vkCmdEndRenderPass(drawCmdBuffers[i]); VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i])); @@ -431,41 +481,46 @@ public: void loadAssets() { loadMesh(getAssetPath() + "models/samplescene.dae", &meshes.scene, vertexLayout, 0.25f); + loadMesh(getAssetPath() + "models/cube.dae", &meshes.transparent, vertexLayout, 3.25f); } void setupVertexDescriptions() { // Binding description - vertices.bindingDescriptions.resize(1); - vertices.bindingDescriptions[0] = + vertices.bindingDescriptions = { vkTools::initializers::vertexInputBindingDescription( VERTEX_BUFFER_BIND_ID, vkMeshLoader::vertexSize(vertexLayout), - VK_VERTEX_INPUT_RATE_VERTEX); + VK_VERTEX_INPUT_RATE_VERTEX), + }; // Attribute descriptions - vertices.attributeDescriptions.resize(3); - // Location 0: Position - vertices.attributeDescriptions[0] = + vertices.attributeDescriptions = { + // Location 0: Position vkTools::initializers::vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 0, VK_FORMAT_R32G32B32_SFLOAT, - 0); - // Location 1: Color - vertices.attributeDescriptions[1] = + 0), + // Location 1: Color vkTools::initializers::vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 1, VK_FORMAT_R32G32B32_SFLOAT, - sizeof(float) * 3); - // Location 2: Normal - vertices.attributeDescriptions[2] = + sizeof(float) * 3), + // Location 2: Normal vkTools::initializers::vertexInputAttributeDescription( VERTEX_BUFFER_BIND_ID, 2, VK_FORMAT_R32G32B32_SFLOAT, - sizeof(float) * 6); + sizeof(float) * 6), + // Location 3: UV + vkTools::initializers::vertexInputAttributeDescription( + VERTEX_BUFFER_BIND_ID, + 3, + VK_FORMAT_R32G32_SFLOAT, + sizeof(float) * 9), + }; vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo(); vertices.inputState.vertexBindingDescriptionCount = static_cast(vertices.bindingDescriptions.size()); @@ -478,9 +533,9 @@ public: { std::vector poolSizes = { - vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 8), + vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 9), vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 9), - vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 3), + vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 4), }; VkDescriptorPoolCreateInfo descriptorPoolInfo = @@ -623,7 +678,7 @@ public: colorBlendState.attachmentCount = static_cast(blendAttachmentStates.size()); colorBlendState.pAttachments = blendAttachmentStates.data(); - // Offscreen pipeline + // Offscreen scene rendering pipeline shaderStages[0] = loadShader(getAssetPath() + "shaders/subpasses/gbuffer.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); shaderStages[1] = loadShader(getAssetPath() + "shaders/subpasses/gbuffer.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); @@ -791,7 +846,59 @@ public: // Index of the subpass that this pipeline will be used in pipelineCreateInfo.subpass = 1; + depthStencilState.depthWriteEnable = VK_FALSE; + VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.composition)); + + // Transparent (forward) pipeline + + // Descriptor set layout + setLayoutBindings = { + vkTools::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0), + vkTools::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, VK_SHADER_STAGE_FRAGMENT_BIT, 1), + }; + + descriptorLayout = vkTools::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), static_cast(setLayoutBindings.size())); + VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayouts.transparent)); + + // Pipeline layout + pPipelineLayoutCreateInfo = vkTools::initializers::pipelineLayoutCreateInfo(&descriptorSetLayouts.transparent, 1); + VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayouts.transparent)); + + // Descriptor sets + allocInfo = vkTools::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.transparent, 1); + VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.transparent)); + + writeDescriptorSets = { + vkTools::initializers::writeDescriptorSet(descriptorSets.transparent, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.GBuffer.descriptor), + vkTools::initializers::writeDescriptorSet(descriptorSets.transparent, VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1, &texDescriptorPosition), + }; + vkUpdateDescriptorSets(device, static_cast(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL); + + //depthStencilState.depthWriteEnable = VK_TRUE; + + // Enable blending + blendAttachmentState.blendEnable = VK_TRUE; + blendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD; + blendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_COLOR; + blendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR; + + blendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_ONE; + blendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; + blendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD; + blendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; + blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; + blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD; + blendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; + + pipelineCreateInfo.pVertexInputState = &vertices.inputState; + pipelineCreateInfo.layout = pipelineLayouts.transparent; + pipelineCreateInfo.subpass = 2; + + shaderStages[0] = loadShader(getAssetPath() + "shaders/subpasses/transparent.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); + shaderStages[1] = loadShader(getAssetPath() + "shaders/subpasses/transparent.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); + + VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.transparent)); } // Prepare and initialize uniform buffer containing shader uniforms diff --git a/subpasses/subpasses.vcxproj b/subpasses/subpasses.vcxproj index 833e73a8..5371af97 100644 --- a/subpasses/subpasses.vcxproj +++ b/subpasses/subpasses.vcxproj @@ -94,6 +94,8 @@ + + diff --git a/subpasses/subpasses.vcxproj.filters b/subpasses/subpasses.vcxproj.filters index bf62153e..92710c3f 100644 --- a/subpasses/subpasses.vcxproj.filters +++ b/subpasses/subpasses.vcxproj.filters @@ -55,5 +55,11 @@ Shaders + + Shaders + + + Shaders + \ No newline at end of file