rendering: unify builtin forward and depth-style shaders

This commit is contained in:
2026-04-07 03:35:06 +08:00
parent 503ffbc4ff
commit 5f9f3386ab
27 changed files with 1135 additions and 1151 deletions

View File

@@ -1,20 +0,0 @@
// XC_BUILTIN_DEPTH_ONLY_OPENGL_PS
#version 430
layout(binding = 0) uniform sampler2D uBaseColorTexture;
layout(std140, binding = 1) uniform MaterialConstants {
vec4 gBaseColorFactor;
vec4 gAlphaCutoffParams;
};
in vec2 vTexCoord;
void main() {
#ifdef XC_ALPHA_TEST
vec4 baseColor = texture(uBaseColorTexture, vTexCoord) * gBaseColorFactor;
if (baseColor.a < gAlphaCutoffParams.x) {
discard;
}
#endif
}

View File

@@ -1,21 +0,0 @@
// XC_BUILTIN_DEPTH_ONLY_VULKAN_PS
#version 450
layout(set = 2, binding = 0) uniform texture2D uBaseColorTexture;
layout(set = 3, binding = 0) uniform sampler uLinearSampler;
layout(set = 1, binding = 0, std140) uniform MaterialConstants {
vec4 gBaseColorFactor;
vec4 gAlphaCutoffParams;
};
layout(location = 0) in vec2 vTexCoord;
void main() {
#ifdef XC_ALPHA_TEST
vec4 baseColor = texture(sampler2D(uBaseColorTexture, uLinearSampler), vTexCoord) * gBaseColorFactor;
if (baseColor.a < gAlphaCutoffParams.x) {
discard;
}
#endif
}

View File

@@ -1,20 +0,0 @@
// XC_BUILTIN_DEPTH_ONLY_D3D12_PS
Texture2D gBaseColorTexture : register(t0);
SamplerState gLinearSampler : register(s0);
cbuffer MaterialConstants : register(b1) {
float4 gBaseColorFactor;
float4 gAlphaCutoffParams;
};
struct PSInput {
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
void MainPS(PSInput input) {
#ifdef XC_ALPHA_TEST
float4 baseColor = gBaseColorTexture.Sample(gLinearSampler, input.texcoord) * gBaseColorFactor;
clip(baseColor.a - gAlphaCutoffParams.x);
#endif
}

View File

@@ -6,26 +6,67 @@ Shader "Builtin Depth Only"
_Cutoff ("Alpha Cutoff", Range) = 0.5 [Semantic(AlphaCutoff)]
_MainTex ("Base Map", 2D) = "white" [Semantic(BaseColorTexture)]
}
HLSLINCLUDE
cbuffer PerObjectConstants : register(b0)
{
float4x4 gProjectionMatrix;
float4x4 gViewMatrix;
float4x4 gModelMatrix;
};
cbuffer MaterialConstants : register(b1)
{
float4 gBaseColorFactor;
float4 gAlphaCutoffParams;
};
Texture2D BaseColorTexture : register(t0);
SamplerState LinearClampSampler : register(s0);
struct VSInput
{
float3 position : POSITION;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
struct PSInput
{
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
PSInput MainVS(VSInput input)
{
PSInput output;
const float4 positionWS = mul(gModelMatrix, float4(input.position, 1.0f));
const float4 positionVS = mul(gViewMatrix, positionWS);
output.position = mul(gProjectionMatrix, positionVS);
output.texcoord = input.texcoord;
return output;
}
float MainPS(PSInput input) : SV_Depth
{
#ifdef XC_ALPHA_TEST
const float4 baseColor =
BaseColorTexture.Sample(LinearClampSampler, input.texcoord) * gBaseColorFactor;
clip(baseColor.a - gAlphaCutoffParams.x);
#endif
return input.position.z;
}
ENDHLSL
SubShader
{
Pass
{
Name "DepthOnly"
Tags { "LightMode" = "DepthOnly" }
Resources
{
PerObjectConstants (ConstantBuffer, 0, 0) [Semantic(PerObject)]
MaterialConstants (ConstantBuffer, 1, 0) [Semantic(Material)]
BaseColorTexture (Texture2D, 2, 0) [Semantic(BaseColorTexture)]
LinearClampSampler (Sampler, 3, 0) [Semantic(LinearClampSampler)]
}
HLSLPROGRAM
#pragma target 4.5
#pragma vertex MainVS
#pragma fragment MainPS
#pragma shader_feature_local _ XC_ALPHA_TEST
#pragma backend D3D12 HLSL "depth-only.vs.hlsl" "depth-only.ps.hlsl" vs_5_0 ps_5_0
#pragma backend OpenGL GLSL "depth-only.vert.glsl" "depth-only.frag.glsl"
#pragma backend Vulkan GLSL "depth-only.vert.vk.glsl" "depth-only.frag.vk.glsl"
ENDHLSL
}
}

View File

@@ -1,19 +0,0 @@
// XC_BUILTIN_DEPTH_ONLY_OPENGL_VS
#version 430
layout(location = 0) in vec3 aPosition;
layout(location = 2) in vec2 aTexCoord;
layout(std140, binding = 0) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
};
out vec2 vTexCoord;
void main() {
vec4 positionWS = gModelMatrix * vec4(aPosition, 1.0);
vec4 positionVS = gViewMatrix * positionWS;
gl_Position = gProjectionMatrix * positionVS;
vTexCoord = aTexCoord;
}

View File

@@ -1,19 +0,0 @@
// XC_BUILTIN_DEPTH_ONLY_VULKAN_VS
#version 450
layout(location = 0) in vec3 aPosition;
layout(location = 2) in vec2 aTexCoord;
layout(set = 0, binding = 0, std140) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
};
layout(location = 0) out vec2 vTexCoord;
void main() {
vec4 positionWS = gModelMatrix * vec4(aPosition, 1.0);
vec4 positionVS = gViewMatrix * positionWS;
gl_Position = gProjectionMatrix * positionVS;
vTexCoord = aTexCoord;
}

View File

@@ -1,25 +0,0 @@
// XC_BUILTIN_DEPTH_ONLY_D3D12_VS
cbuffer PerObjectConstants : register(b0) {
float4x4 gProjectionMatrix;
float4x4 gViewMatrix;
float4x4 gModelMatrix;
};
struct VSInput {
float3 position : POSITION;
float2 texcoord : TEXCOORD0;
};
struct PSInput {
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
PSInput MainVS(VSInput input) {
PSInput output;
float4 positionWS = mul(gModelMatrix, float4(input.position, 1.0));
float4 positionVS = mul(gViewMatrix, positionWS);
output.position = mul(gProjectionMatrix, positionVS);
output.texcoord = input.texcoord;
return output;
}