From 53c30ffae282122b44b155c6755d9a4cdb82a76a Mon Sep 17 00:00:00 2001
From: Alexander Gauggel <agauggel@uni-koblenz.de>
Date: Thu, 24 Jun 2021 19:21:21 +0200
Subject: [PATCH] [#82] Add alpha test

---
 .../resources/shaders/depthPrepass.frag       |  9 +++-
 .../resources/shaders/depthPrepass.vert       |  4 ++
 projects/voxelization/src/main.cpp            | 41 +++++++++++--------
 3 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/projects/voxelization/resources/shaders/depthPrepass.frag b/projects/voxelization/resources/shaders/depthPrepass.frag
index 65592d2c..b2401639 100644
--- a/projects/voxelization/resources/shaders/depthPrepass.frag
+++ b/projects/voxelization/resources/shaders/depthPrepass.frag
@@ -2,6 +2,13 @@
 #extension GL_ARB_separate_shader_objects : enable
 #extension GL_GOOGLE_include_directive : enable
 
-void main()	{
+#include "perMeshResources.inc"
+
+layout(location = 0) in vec2 passUV;
 
+void main()	{
+    float alpha = texture(sampler2D(albedoTexture, textureSampler), passUV).a;
+    if(alpha < 0.5){
+        discard;
+    }
 }
\ No newline at end of file
diff --git a/projects/voxelization/resources/shaders/depthPrepass.vert b/projects/voxelization/resources/shaders/depthPrepass.vert
index d800c547..4bb3500e 100644
--- a/projects/voxelization/resources/shaders/depthPrepass.vert
+++ b/projects/voxelization/resources/shaders/depthPrepass.vert
@@ -4,6 +4,9 @@
 #extension GL_GOOGLE_include_directive : enable
 
 layout(location = 0) in vec3 inPosition;
+layout(location = 2) in vec2 inUV;
+
+layout(location = 0) out vec2 passUV;
 
 layout( push_constant ) uniform constants{
     mat4 mvp;
@@ -11,4 +14,5 @@ layout( push_constant ) uniform constants{
 
 void main()	{
 	gl_Position = mvp * vec4(inPosition, 1.0);
+    passUV = inUV;
 }
\ No newline at end of file
diff --git a/projects/voxelization/src/main.cpp b/projects/voxelization/src/main.cpp
index daf1211f..c3ca1c7f 100644
--- a/projects/voxelization/src/main.cpp
+++ b/projects/voxelization/src/main.cpp
@@ -177,20 +177,6 @@ int main(int argc, const char** argv) {
 	vkcv::PassConfig prepassPassDefinition({ prepassAttachment }, msaa);
 	vkcv::PassHandle prepassPass = core.createPass(prepassPassDefinition);
 
-	vkcv::PipelineConfig prepassPipelineConfig{
-		depthPrepassShader,
-		windowWidth,
-		windowHeight,
-		prepassPass,
-		vertexLayout,
-		{},
-		true};
-	prepassPipelineConfig.m_culling         = vkcv::CullMode::Back;
-	prepassPipelineConfig.m_multisampling   = msaa;
-	prepassPipelineConfig.m_depthTest       = vkcv::DepthTest::LessEqual;
-
-	vkcv::PipelineHandle prepassPipeline = core.createGraphicsPipeline(prepassPipelineConfig);
-
 	// create descriptor sets
 	vkcv::SamplerHandle colorSampler = core.createSampler(
 		vkcv::SamplerFilterType::LINEAR,
@@ -264,13 +250,34 @@ int main(int argc, const char** argv) {
 		perMeshDescriptorSets.push_back(materialDescriptorSets[vertexGroup.materialIndex]);
 	}
 
+	// prepass pipeline
+	vkcv::DescriptorSetHandle prepassDescriptorSet = core.createDescriptorSet(std::vector<vkcv::DescriptorBinding>());
+
+	vkcv::PipelineConfig prepassPipelineConfig{
+		depthPrepassShader,
+		windowWidth,
+		windowHeight,
+		prepassPass,
+		vertexLayout,
+		{ 
+			core.getDescriptorSet(prepassDescriptorSet).layout,
+			core.getDescriptorSet(perMeshDescriptorSets[0]).layout },
+		true };
+	prepassPipelineConfig.m_culling = vkcv::CullMode::Back;
+	prepassPipelineConfig.m_multisampling = msaa;
+	prepassPipelineConfig.m_depthTest = vkcv::DepthTest::LessEqual;
+
+	vkcv::PipelineHandle prepassPipeline = core.createGraphicsPipeline(prepassPipelineConfig);
+
+	// forward pipeline
 	vkcv::PipelineConfig forwardPipelineConfig {
 		forwardProgram,
 		windowWidth,
 		windowHeight,
 		forwardPass,
 		vertexLayout,
-		{	core.getDescriptorSet(forwardShadingDescriptorSet).layout, 
+		{	
+			core.getDescriptorSet(forwardShadingDescriptorSet).layout, 
 			core.getDescriptorSet(perMeshDescriptorSets[0]).layout },
 		true
 	};
@@ -408,7 +415,9 @@ int main(int argc, const char** argv) {
 		drawcalls.push_back(vkcv::DrawcallInfo(meshes[i], { 
 			vkcv::DescriptorSetUsage(0, core.getDescriptorSet(forwardShadingDescriptorSet).vulkanHandle),
 			vkcv::DescriptorSetUsage(1, core.getDescriptorSet(perMeshDescriptorSets[i]).vulkanHandle) }));
-		prepassDrawcalls.push_back(vkcv::DrawcallInfo(meshes[i], {}));
+		prepassDrawcalls.push_back(vkcv::DrawcallInfo(meshes[i], {
+			vkcv::DescriptorSetUsage(0, core.getDescriptorSet(prepassDescriptorSet).vulkanHandle),
+			vkcv::DescriptorSetUsage(1, core.getDescriptorSet(perMeshDescriptorSets[i]).vulkanHandle) }));
 	}
 
 	vkcv::SamplerHandle voxelSampler = core.createSampler(
-- 
GitLab