Files

96 lines
3.8 KiB
Plaintext

import Common;
// 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
// transparency and shifted glyphs).
struct RenderElementStyle
{
float x;
float y;
float w;
float h;
float3 color;
float opacity;
float z;
uint textureIndex;
float2 uvOffset;
float2 uvSize;
};
struct UIParameter
{
SamplerState sampler;
StructuredBuffer<RenderElementStyle> elements;
Texture2D textures[]; // must be last (unsized)
};
ParameterBlock<UIParameter> pParams;
// 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.
struct VertexOutput
{
float4 position : SV_Position;
float2 texCoords : TEXCOORD;
uint instanceId : INSTANCENUM;
};
[shader("vertex")]
VertexOutput vertexMain(uint vertexId : SV_VertexID, uint instanceId : SV_InstanceID)
{
// Read the per-quad rect from the non-interpolated SSBO (exact values).
RenderElementStyle style = pParams.elements[instanceId];
float xMin = style.x;
float xMax = xMin + style.w;
float yMin = style.y;
float yMax = yMin + style.h;
float4 coordinates[] = {
float4(xMin, yMin, 0, 0),
float4(xMin, yMax, 0, 1),
float4(xMax, yMin, 1, 0),
float4(xMax, yMax, 1, 1)
};
VertexOutput output;
// 2D ortho screen->NDC (screen is top-left / y-down; NDC is bottom-left / y-up):
// ndcX = (x / W) * 2 - 1
// ndcY = 1 - (y / H) * 2
float2 ndc = float2(
coordinates[vertexId].x * pViewParams.invScreenDimensions.x * 2.0 - 1.0,
1.0 - coordinates[vertexId].y * pViewParams.invScreenDimensions.y * 2.0
);
// 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);
output.texCoords = coordinates[vertexId].zw; // quad-local uv, interpolated per-fragment
output.instanceId = instanceId; // uint varying: exact per-fragment (not interpolated)
return output;
}
[shader("fragment")]
float4 fragmentMain(
float2 texCoords : TEXCOORD,
uint instanceId : INSTANCENUM
) : SV_Target
{
// 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)
return float4(style.color, style.opacity);
}
// 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).
float2 uv = style.uvOffset + texCoords * style.uvSize;
float4 tex = pParams.textures[style.textureIndex].Sample(pParams.sampler, uv);
float coverage = tex.r;
return float4(style.color, coverage * style.opacity);
}