Formalize builtin shader resource contracts

This commit is contained in:
2026-04-08 04:44:36 +08:00
parent 6113ed92b0
commit 2c1c815072
26 changed files with 358 additions and 368 deletions

View File

@@ -10,8 +10,8 @@ Shader "Builtin Color Scale Post Process"
float4 gColorScale;
};
Texture2D gSourceColorTexture;
SamplerState gLinearClampSampler;
Texture2D SourceColorTexture;
SamplerState LinearClampSampler;
struct VSOutput
{
@@ -44,7 +44,7 @@ Shader "Builtin Color Scale Post Process"
float4 MainPS(VSOutput input) : SV_TARGET
{
return gSourceColorTexture.Sample(gLinearClampSampler, input.texcoord) * gColorScale;
return SourceColorTexture.Sample(LinearClampSampler, input.texcoord) * gColorScale;
}
ENDHLSL
SubShader

View File

@@ -14,8 +14,8 @@ Shader "Builtin Final Color"
float4 gFinalColorParams;
};
Texture2D gSourceColorTexture;
SamplerState gLinearClampSampler;
Texture2D SourceColorTexture;
SamplerState LinearClampSampler;
struct VSOutput
{
@@ -92,7 +92,7 @@ Shader "Builtin Final Color"
float4 MainPS(VSOutput input) : SV_TARGET
{
float4 color = gSourceColorTexture.Sample(gLinearClampSampler, input.texcoord);
float4 color = SourceColorTexture.Sample(LinearClampSampler, input.texcoord);
color.rgb *= max(gFinalColorParams.x, 0.0f);
color *= gColorScale;
color.rgb = ApplyToneMapping(color.rgb, gFinalColorParams.z);

View File

@@ -0,0 +1,133 @@
Shader "Builtin Object Id Outline"
{
HLSLINCLUDE
// XC_BUILTIN_OBJECT_ID_OUTLINE_D3D12_SHARED
cbuffer OutlineConstants
{
float4 gViewportSizeAndTexelSize;
float4 gOutlineColor;
float4 gSelectedInfo;
float4 gSelectedObjectColors[256];
};
Texture2D gObjectIdTexture;
struct VSOutput
{
float4 position : SV_POSITION;
};
VSOutput MainVS(uint vertexId : SV_VertexID)
{
// XC_BUILTIN_OBJECT_ID_OUTLINE_D3D12_VS
static const float2 positions[3] = {
float2(-1.0, -1.0),
float2(-1.0, 3.0),
float2( 3.0, -1.0)
};
VSOutput output;
output.position = float4(positions[vertexId], 0.0, 1.0);
return output;
}
int2 ClampPixelCoord(int2 pixelCoord)
{
const int2 maxCoord = int2(
max((int)gViewportSizeAndTexelSize.x - 1, 0),
max((int)gViewportSizeAndTexelSize.y - 1, 0));
return clamp(pixelCoord, int2(0, 0), maxCoord);
}
float4 LoadObjectId(int2 pixelCoord)
{
return gObjectIdTexture.Load(int3(ClampPixelCoord(pixelCoord), 0));
}
bool IsSelectedObject(float4 objectIdColor)
{
if (objectIdColor.a <= 0.0) {
return false;
}
const int selectedCount = min((int)gSelectedInfo.x, 256);
[loop]
for (int i = 0; i < selectedCount; ++i) {
const float4 selectedColor = gSelectedObjectColors[i];
if (all(abs(objectIdColor - selectedColor) <= float4(
0.0025,
0.0025,
0.0025,
0.0025))) {
return true;
}
}
return false;
}
float4 MainPS(VSOutput input) : SV_TARGET
{
// XC_BUILTIN_OBJECT_ID_OUTLINE_D3D12_PS
const int2 pixelCoord = int2(input.position.xy);
const bool debugSelectionMask = gSelectedInfo.y > 0.5;
const bool centerSelected = IsSelectedObject(LoadObjectId(pixelCoord));
if (debugSelectionMask) {
return centerSelected ? float4(1.0, 1.0, 1.0, 1.0) : float4(0.0, 0.0, 0.0, 1.0);
}
if (centerSelected) {
discard;
}
const int outlineWidth = max((int)gSelectedInfo.z, 1);
float outline = 0.0;
[loop]
for (int y = -2; y <= 2; ++y) {
[loop]
for (int x = -2; x <= 2; ++x) {
if (x == 0 && y == 0) {
continue;
}
const float distancePixels = length(float2((float)x, (float)y));
if (distancePixels > outlineWidth) {
continue;
}
if (!IsSelectedObject(LoadObjectId(pixelCoord + int2(x, y)))) {
continue;
}
const float weight = saturate(
1.0 - ((distancePixels - 1.0) / max((float)outlineWidth, 1.0)));
outline = max(outline, weight);
}
}
if (outline <= 0.001) {
discard;
}
return float4(gOutlineColor.rgb, gOutlineColor.a * outline);
}
ENDHLSL
SubShader
{
Pass
{
Name "ObjectIdOutline"
Tags { "LightMode" = "ObjectIdOutline" }
Cull Off
ZWrite Off
ZTest Always
Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
HLSLPROGRAM
#pragma target 4.5
#pragma vertex MainVS
#pragma fragment MainPS
ENDHLSL
}
}
}

View File

@@ -1,14 +0,0 @@
// XC_BUILTIN_OBJECT_ID_OPENGL_PS
#version 430
layout(std140, binding = 0) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
vec4 gObjectIdColor;
};
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = gObjectIdColor;
}

View File

@@ -1,14 +0,0 @@
// XC_BUILTIN_OBJECT_ID_VULKAN_PS
#version 450
layout(set = 0, binding = 0, std140) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
vec4 gObjectIdColor;
};
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = gObjectIdColor;
}

View File

@@ -1,15 +0,0 @@
// XC_BUILTIN_OBJECT_ID_D3D12_PS
cbuffer PerObjectConstants : register(b0) {
float4x4 gProjectionMatrix;
float4x4 gViewMatrix;
float4x4 gModelMatrix;
float4 gObjectIdColor;
};
struct PSInput {
float4 position : SV_POSITION;
};
float4 MainPS(PSInput input) : SV_TARGET {
return gObjectIdColor;
}

View File

@@ -1,16 +0,0 @@
// XC_BUILTIN_OBJECT_ID_OPENGL_VS
#version 430
layout(location = 0) in vec3 aPosition;
layout(std140, binding = 0) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
vec4 gObjectIdColor;
};
void main() {
vec4 positionWS = gModelMatrix * vec4(aPosition, 1.0);
vec4 positionVS = gViewMatrix * positionWS;
gl_Position = gProjectionMatrix * positionVS;
}

View File

@@ -1,16 +0,0 @@
// XC_BUILTIN_OBJECT_ID_VULKAN_VS
#version 450
layout(location = 0) in vec3 aPosition;
layout(set = 0, binding = 0, std140) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
vec4 gObjectIdColor;
};
void main() {
vec4 positionWS = gModelMatrix * vec4(aPosition, 1.0);
vec4 positionVS = gViewMatrix * positionWS;
gl_Position = gProjectionMatrix * positionVS;
}

View File

@@ -1,23 +0,0 @@
// XC_BUILTIN_OBJECT_ID_D3D12_VS
cbuffer PerObjectConstants : register(b0) {
float4x4 gProjectionMatrix;
float4x4 gViewMatrix;
float4x4 gModelMatrix;
float4 gObjectIdColor;
};
struct VSInput {
float3 position : POSITION;
};
struct PSInput {
float4 position : SV_POSITION;
};
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);
return output;
}

View File

@@ -1,22 +0,0 @@
// XC_BUILTIN_UNLIT_OPENGL_PS
#version 430
layout(binding = 0) uniform sampler2D uBaseColorTexture;
layout(std140, binding = 0) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
mat4 gNormalMatrix;
};
layout(std140, binding = 1) uniform MaterialConstants {
vec4 gBaseColorFactor;
};
in vec2 vTexCoord;
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = texture(uBaseColorTexture, vTexCoord) * gBaseColorFactor;
}

View File

@@ -1,23 +0,0 @@
// XC_BUILTIN_UNLIT_VULKAN_PS
#version 450
layout(set = 2, binding = 0) uniform texture2D uBaseColorTexture;
layout(set = 3, binding = 0) uniform sampler uLinearSampler;
layout(set = 0, binding = 0, std140) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
mat4 gNormalMatrix;
};
layout(set = 1, binding = 0, std140) uniform MaterialConstants {
vec4 gBaseColorFactor;
};
layout(location = 0) in vec2 vTexCoord;
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = texture(sampler2D(uBaseColorTexture, uLinearSampler), vTexCoord) * gBaseColorFactor;
}

View File

@@ -1,23 +0,0 @@
// XC_BUILTIN_UNLIT_D3D12_PS
Texture2D gBaseColorTexture : register(t0);
SamplerState gLinearSampler : register(s0);
cbuffer PerObjectConstants : register(b0) {
float4x4 gProjectionMatrix;
float4x4 gViewMatrix;
float4x4 gModelMatrix;
float4x4 gNormalMatrix;
};
cbuffer MaterialConstants : register(b1) {
float4 gBaseColorFactor;
};
struct PSInput {
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
float4 MainPS(PSInput input) : SV_TARGET {
return gBaseColorTexture.Sample(gLinearSampler, input.texcoord) * gBaseColorFactor;
}

View File

@@ -1,21 +0,0 @@
// XC_BUILTIN_UNLIT_OPENGL_VS
#version 430
layout(location = 0) in vec3 aPosition;
layout(location = 1) in vec3 aNormal;
layout(location = 2) in vec2 aTexCoord;
layout(std140, binding = 0) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
mat4 gNormalMatrix;
};
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,21 +0,0 @@
// XC_BUILTIN_UNLIT_VULKAN_VS
#version 450
layout(location = 0) in vec3 aPosition;
layout(location = 1) in vec3 aNormal;
layout(location = 2) in vec2 aTexCoord;
layout(set = 0, binding = 0, std140) uniform PerObjectConstants {
mat4 gProjectionMatrix;
mat4 gViewMatrix;
mat4 gModelMatrix;
mat4 gNormalMatrix;
};
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,27 +0,0 @@
// XC_BUILTIN_UNLIT_D3D12_VS
cbuffer PerObjectConstants : register(b0) {
float4x4 gProjectionMatrix;
float4x4 gViewMatrix;
float4x4 gModelMatrix;
float4x4 gNormalMatrix;
};
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;
float4 positionWS = mul(gModelMatrix, float4(input.position, 1.0f));
float4 positionVS = mul(gViewMatrix, positionWS);
output.position = mul(gProjectionMatrix, positionVS);
output.texcoord = input.texcoord;
return output;
}