From b6f94f710f545b3e376fd48a64e375d3b57c8970 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 19 Apr 2024 10:38:46 +0100 Subject: [PATCH] Added VSG_ALPHA_TEST #pragma(tic) shadow composition define to control whether the alpha test discard code path in fragment shaders is run. --- data/shaders/standard_flat_shaded.frag | 10 ++++------ data/shaders/standard_pbr.frag | 10 ++++------ data/shaders/standard_phong.frag | 10 ++++------ examples/nodes/vsgshadow/vsgshadow.cpp | 5 +++++ examples/utils/vsgshaderset/flat.cpp | 2 +- examples/utils/vsgshaderset/pbr.cpp | 2 +- examples/utils/vsgshaderset/phong.cpp | 2 +- 7 files changed, 20 insertions(+), 21 deletions(-) diff --git a/data/shaders/standard_flat_shaded.frag b/data/shaders/standard_flat_shaded.frag index cc83d412..39400ab8 100644 --- a/data/shaders/standard_flat_shaded.frag +++ b/data/shaders/standard_flat_shaded.frag @@ -1,6 +1,6 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -#pragma import_defines (VSG_POINT_SPRITE, VSG_DIFFUSE_MAP, VSG_GREYSCALE_DIFFUSE_MAP) +#pragma import_defines (VSG_POINT_SPRITE, VSG_DIFFUSE_MAP, VSG_GREYSCALE_DIFFUSE_MAP, VSG_ALPHA_TEST) #define VIEW_DESCRIPTOR_SET 0 #define MATERIAL_DESCRIPTOR_SET 1 @@ -44,11 +44,9 @@ void main() #endif #endif - if (material.alphaMask == 1.0f) - { - if (diffuseColor.a < material.alphaMaskCutoff) - discard; - } +#ifdef VSG_ALPHA_TEST + if (material.alphaMask == 1.0f && diffuseColor.a < material.alphaMaskCutoff) discard; +#endif outColor = diffuseColor; } diff --git a/data/shaders/standard_pbr.frag b/data/shaders/standard_pbr.frag index f5ac1d18..627e063d 100644 --- a/data/shaders/standard_pbr.frag +++ b/data/shaders/standard_pbr.frag @@ -1,6 +1,6 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -#pragma import_defines (VSG_DIFFUSE_MAP, VSG_GREYSCALE_DIFFUSE_MAP, VSG_EMISSIVE_MAP, VSG_LIGHTMAP_MAP, VSG_NORMAL_MAP, VSG_METALLROUGHNESS_MAP, VSG_SPECULAR_MAP, VSG_TWO_SIDED_LIGHTING, VSG_WORKFLOW_SPECGLOSS, VSG_SHADOWS_PCSS, VSG_SHADOWS_SOFT, VSG_SHADOWS_HARD, SHADOWMAP_DEBUG) +#pragma import_defines (VSG_DIFFUSE_MAP, VSG_GREYSCALE_DIFFUSE_MAP, VSG_EMISSIVE_MAP, VSG_LIGHTMAP_MAP, VSG_NORMAL_MAP, VSG_METALLROUGHNESS_MAP, VSG_SPECULAR_MAP, VSG_TWO_SIDED_LIGHTING, VSG_WORKFLOW_SPECGLOSS, VSG_SHADOWS_PCSS, VSG_SHADOWS_SOFT, VSG_SHADOWS_HARD, SHADOWMAP_DEBUG, VSG_ALPHA_TEST) // define by default for backwards compatibility #define VSG_SHADOWS_HARD @@ -334,11 +334,9 @@ void main() baseColor = vertexColor * pbr.baseColorFactor; #endif - if (pbr.alphaMask == 1.0f) - { - if (baseColor.a < pbr.alphaMaskCutoff) - discard; - } +#ifdef VSG_ALPHA_TEST + if (material.alphaMask == 1.0f && diffuseColor.a < material.alphaMaskCutoff) discard; +#endif #ifdef VSG_WORKFLOW_SPECGLOSS #ifdef VSG_DIFFUSE_MAP diff --git a/data/shaders/standard_phong.frag b/data/shaders/standard_phong.frag index 0fc5b978..15181e9e 100644 --- a/data/shaders/standard_phong.frag +++ b/data/shaders/standard_phong.frag @@ -1,6 +1,6 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -#pragma import_defines (VSG_POINT_SPRITE, VSG_DIFFUSE_MAP, VSG_GREYSCALE_DIFFUSE_MAP, VSG_EMISSIVE_MAP, VSG_LIGHTMAP_MAP, VSG_NORMAL_MAP, VSG_SPECULAR_MAP, VSG_TWO_SIDED_LIGHTING, VSG_SHADOWS_PCSS, VSG_SHADOWS_SOFT, VSG_SHADOWS_HARD, SHADOWMAP_DEBUG) +#pragma import_defines (VSG_POINT_SPRITE, VSG_DIFFUSE_MAP, VSG_GREYSCALE_DIFFUSE_MAP, VSG_EMISSIVE_MAP, VSG_LIGHTMAP_MAP, VSG_NORMAL_MAP, VSG_SPECULAR_MAP, VSG_TWO_SIDED_LIGHTING, VSG_SHADOWS_PCSS, VSG_SHADOWS_SOFT, VSG_SHADOWS_HARD, SHADOWMAP_DEBUG, VSG_ALPHA_TEST) // define by default for backwards compatibility #define VSG_SHADOWS_HARD @@ -136,11 +136,9 @@ void main() float shininess = material.shininess; float ambientOcclusion = 1.0; - if (material.alphaMask == 1.0f) - { - if (diffuseColor.a < material.alphaMaskCutoff) - discard; - } +#ifdef VSG_ALPHA_TEST + if (material.alphaMask == 1.0f && diffuseColor.a < material.alphaMaskCutoff) discard; +#endif #ifdef VSG_EMISSIVE_MAP emissiveColor *= texture(emissiveMap, texCoord0.st); diff --git a/examples/nodes/vsgshadow/vsgshadow.cpp b/examples/nodes/vsgshadow/vsgshadow.cpp index d9db7d8a..d78705d2 100644 --- a/examples/nodes/vsgshadow/vsgshadow.cpp +++ b/examples/nodes/vsgshadow/vsgshadow.cpp @@ -324,6 +324,11 @@ int main(int argc, char** argv) shadowSettings = vsg::HardShadows::create(numShadowMapsPerLight); } + if (arguments.read({"--alpha-test", "--at"})) + { + shaderHints->defines.insert("VSG_ALPHA_TEST"); + } + bool overrideShadowSettings = arguments.read("--override"); if (overrideShadowSettings) { diff --git a/examples/utils/vsgshaderset/flat.cpp b/examples/utils/vsgshaderset/flat.cpp index a04067d6..8d4dba21 100644 --- a/examples/utils/vsgshaderset/flat.cpp +++ b/examples/utils/vsgshaderset/flat.cpp @@ -65,7 +65,7 @@ vsg::ref_ptr flat_ShaderSet(vsg::ref_ptr opt shaderSet->addPushConstantRange("pc", "", VK_SHADER_STAGE_VERTEX_BIT, 0, 128); - shaderSet->optionalDefines = {"VSG_POINT_SPRITE", "VSG_GREYSCALE_DIFFUSE_MAP"}; + shaderSet->optionalDefines = {"VSG_POINT_SPRITE", "VSG_GREYSCALE_DIFFUSE_MAP", "VSG_ALPHA_TEST"}; shaderSet->definesArrayStates.push_back(vsg::DefinesArrayState{{"VSG_INSTANCE_POSITIONS", "VSG_DISPLACEMENT_MAP"}, vsg::PositionAndDisplacementMapArrayState::create()}); shaderSet->definesArrayStates.push_back(vsg::DefinesArrayState{{"VSG_INSTANCE_POSITIONS"}, vsg::PositionArrayState::create()}); diff --git a/examples/utils/vsgshaderset/pbr.cpp b/examples/utils/vsgshaderset/pbr.cpp index 6e8f804c..a57ab61b 100644 --- a/examples/utils/vsgshaderset/pbr.cpp +++ b/examples/utils/vsgshaderset/pbr.cpp @@ -65,7 +65,7 @@ vsg::ref_ptr pbr_ShaderSet(vsg::ref_ptr opti shaderSet->addDescriptorBinding("shadowMapShadowSampler", "", VIEW_DESCRIPTOR_SET, 4, VK_DESCRIPTOR_TYPE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr); // additional defines - shaderSet->optionalDefines = {"VSG_GREYSCALE_DIFFUSE_MAP", "VSG_TWO_SIDED_LIGHTING", "VSG_WORKFLOW_SPECGLOSS", "VSG_SHADOWS_PCSS", "VSG_SHADOWS_SOFT", "VSG_SHADOWS_HARD", "SHADOWMAP_DEBUG" }; + shaderSet->optionalDefines = {"VSG_GREYSCALE_DIFFUSE_MAP", "VSG_TWO_SIDED_LIGHTING", "VSG_WORKFLOW_SPECGLOSS", "VSG_SHADOWS_PCSS", "VSG_SHADOWS_SOFT", "VSG_SHADOWS_HARD", "SHADOWMAP_DEBUG", "VSG_ALPHA_TEST" }; shaderSet->addPushConstantRange("pc", "", VK_SHADER_STAGE_VERTEX_BIT, 0, 128); diff --git a/examples/utils/vsgshaderset/phong.cpp b/examples/utils/vsgshaderset/phong.cpp index 3a27d62b..35eb342c 100644 --- a/examples/utils/vsgshaderset/phong.cpp +++ b/examples/utils/vsgshaderset/phong.cpp @@ -63,7 +63,7 @@ vsg::ref_ptr phong_ShaderSet(vsg::ref_ptr op shaderSet->addPushConstantRange("pc", "", VK_SHADER_STAGE_VERTEX_BIT, 0, 128); - shaderSet->optionalDefines = {"VSG_GREYSCALE_DIFFUSE_MAP", "VSG_TWO_SIDED_LIGHTING", "VSG_POINT_SPRITE", "VSG_SHADOWS_PCSS", "VSG_SHADOWS_SOFT", "VSG_SHADOWS_HARD", "SHADOWMAP_DEBUG" }; + shaderSet->optionalDefines = {"VSG_GREYSCALE_DIFFUSE_MAP", "VSG_TWO_SIDED_LIGHTING", "VSG_POINT_SPRITE", "VSG_SHADOWS_PCSS", "VSG_SHADOWS_SOFT", "VSG_SHADOWS_HARD", "SHADOWMAP_DEBUG", "VSG_ALPHA_TEST" }; shaderSet->definesArrayStates.push_back(vsg::DefinesArrayState{{"VSG_INSTANCE_POSITIONS", "VSG_DISPLACEMENT_MAP"}, vsg::PositionAndDisplacementMapArrayState::create()}); shaderSet->definesArrayStates.push_back(vsg::DefinesArrayState{{"VSG_INSTANCE_POSITIONS"}, vsg::PositionArrayState::create()});