Files

95 lines
3.8 KiB
Plaintext
Raw Permalink Normal View History

2023-11-05 10:36:01 +01:00
import Common;
2026-10-22 21:53:33 +02:00
// Per-quad render data, read from the EXISTING pParams.elements storage buffer (bound on the
// CPU side by UIPass as SE_DESCRIPTOR_TYPE_STORAGE_BUFFER). We read it here as a plain SSBO so
// every value (x/y/w/h, color, opacity, uvOffset/uvSize, textureIndex) is EXACT — not barycentric
// interpolated the way the old code passed it as a STYLE vertex attribute (which corrupted the
2026-11-05 22:31:04 +01:00
// transparency and shifted glyphs).
2022-04-18 18:08:38 +02:00
struct RenderElementStyle
2021-09-23 10:10:39 +02:00
{
2025-01-12 11:26:52 +01:00
float x;
float y;
float w;
float h;
float3 color;
2022-04-18 18:08:38 +02:00
float opacity;
2025-01-12 11:26:52 +01:00
float z;
uint textureIndex;
2026-10-21 21:21:49 +02:00
float2 uvOffset;
float2 uvSize;
2021-09-23 10:10:39 +02:00
};
2023-11-05 10:36:01 +01:00
struct UIParameter
2022-04-18 18:08:38 +02:00
{
2025-01-12 11:26:52 +01:00
SamplerState sampler;
2026-10-22 21:53:33 +02:00
StructuredBuffer<RenderElementStyle> elements;
Texture2D textures[]; // must be last (unsized)
2025-01-12 11:26:52 +01:00
};
2026-10-22 21:53:33 +02:00
2023-11-08 23:27:21 +01:00
ParameterBlock<UIParameter> pParams;
2022-04-18 18:08:38 +02:00
2026-10-22 21:53:33 +02:00
// Varyings carry only per-fragment continuous data (the corner uv in quad-local space) + the
// per-quad instance id (uint, not interpolated — exact per fragment). Position is SV_Position.
// Everything else comes from the non-interpolated pParams SSBO.
2023-11-08 23:27:21 +01:00
struct VertexOutput
2022-04-18 18:08:38 +02:00
{
float4 position : SV_Position;
float2 texCoords : TEXCOORD;
2026-10-22 21:53:33 +02:00
uint instanceId : INSTANCENUM;
2022-04-18 18:08:38 +02:00
};
2021-09-23 10:10:39 +02:00
[shader("vertex")]
2026-10-22 21:53:33 +02:00
VertexOutput vertexMain(uint vertexId : SV_VertexID, uint instanceId : SV_InstanceID)
2021-09-23 10:10:39 +02:00
{
2026-10-22 21:53:33 +02:00
// Read the per-quad rect from the non-interpolated SSBO (exact values).
2025-01-12 11:26:52 +01:00
RenderElementStyle style = pParams.elements[instanceId];
float xMin = style.x;
float xMax = xMin + style.w;
float yMin = style.y;
float yMax = yMin + style.h;
2022-04-18 18:08:38 +02:00
float4 coordinates[] = {
float4(xMin, yMin, 0, 0),
float4(xMin, yMax, 0, 1),
float4(xMax, yMin, 1, 0),
float4(xMax, yMax, 1, 1)
2021-09-23 10:10:39 +02:00
};
2023-11-08 23:27:21 +01:00
VertexOutput output;
2026-10-22 21:53:33 +02:00
// 2D ortho screen->NDC (screen is top-left / y-down; NDC is bottom-left / y-up):
2026-10-20 22:01:51 +02:00
// ndcX = (x / W) * 2 - 1
2026-10-22 21:53:33 +02:00
// ndcY = 1 - (y / H) * 2
2026-10-20 22:01:51 +02:00
float2 ndc = float2(
coordinates[vertexId].x * pViewParams.invScreenDimensions.x * 2.0 - 1.0,
1.0 - coordinates[vertexId].y * pViewParams.invScreenDimensions.y * 2.0
);
2026-11-05 22:31:04 +01:00
// style.z is ALREADY the final depth: the CPU maps the tree depth (render.level) into [0,1]
// and REVERSES it for the depth buffer's SE_COMPARE_OP_GREATER (nearer = LARGER). Write it
// verbatim — do NOT negate it again (the old `1 - style.z` double-inverted the depth).
output.position = float4(ndc, style.z, 1.0);
2026-10-22 21:53:33 +02:00
output.texCoords = coordinates[vertexId].zw; // quad-local uv, interpolated per-fragment
output.instanceId = instanceId; // uint varying: exact per-fragment (not interpolated)
2021-09-23 10:10:39 +02:00
return output;
}
[shader("fragment")]
float4 fragmentMain(
2022-04-18 18:08:38 +02:00
float2 texCoords : TEXCOORD,
2026-10-22 21:53:33 +02:00
uint instanceId : INSTANCENUM
2021-09-23 10:10:39 +02:00
) : SV_Target
{
2026-10-22 21:53:33 +02:00
// Re-read the per-quad data from the non-interpolated SSBO: exact uvOffset/uvSize, exact
// color, exact opacity. (Interpolating these as vertex attributes corrupted transparency
// and shifted the sampling — the bug the old STYLE-attribute code had.)
RenderElementStyle style = pParams.elements[instanceId];
if(style.textureIndex == 4294967295u) {
// untextured quad (solid background)
2025-01-12 11:26:52 +01:00
return float4(style.color, style.opacity);
}
2026-10-22 21:53:33 +02:00
// Textured quad (glyph): the texture is the font atlas (R8_UNORM, coverage in R, A baked
// to 1.0). Sampled as float4 that gives (R=coverage, G=0, B=0, A=1) — the alpha we want is
// .r, not .a. Output pure text color + coverage as alpha; the pipeline's SRC_ALPHA blend
// applies the coverage exactly once (do NOT premultiply or it'd be squared).
2026-10-21 21:21:49 +02:00
float2 uv = style.uvOffset + texCoords * style.uvSize;
float4 tex = pParams.textures[style.textureIndex].Sample(pParams.sampler, uv);
2026-10-20 22:01:51 +02:00
float coverage = tex.r;
return float4(style.color, coverage * style.opacity);
}