forked from Dynamitos/Seele
86 lines
2.8 KiB
Plaintext
86 lines
2.8 KiB
Plaintext
import Common;
|
|
|
|
struct RenderElementStyle
|
|
{
|
|
float x;
|
|
float y;
|
|
float w;
|
|
float h;
|
|
float3 color;
|
|
float opacity;
|
|
float z;
|
|
uint textureIndex;
|
|
// UV rect for textured quads (glyphs: their rect in the font atlas)
|
|
float2 uvOffset;
|
|
float2 uvSize;
|
|
float2 pad;
|
|
};
|
|
|
|
struct UIParameter
|
|
{
|
|
StructuredBuffer<RenderElementStyle> elements;
|
|
SamplerState sampler;
|
|
Texture2D textures[];
|
|
};
|
|
ParameterBlock<UIParameter> pParams;
|
|
|
|
struct VertexOutput
|
|
{
|
|
float4 position : SV_Position;
|
|
float2 texCoords : TEXCOORD;
|
|
RenderElementStyle style : STYLE;
|
|
};
|
|
|
|
[shader("vertex")]
|
|
VertexOutput vertexMain(uint vertexId : SV_VertexID, uint instanceId: SV_InstanceID)
|
|
{
|
|
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;
|
|
// The UI is a 2D overlay in *screen* pixel space (top-left origin, y down).
|
|
// The 3D scene camera's projectionMatrix is meaningless for these coordinates,
|
|
// so build a 2D ortho screen->NDC projection from the viewport size instead:
|
|
// ndcX = (x / W) * 2 - 1
|
|
// ndcY = 1 - (y / H) * 2 (flip y: screen is top-down, NDC is bottom-up)
|
|
// The UI must NOT be transformed by pViewParams.projectionMatrix (the 3D camera).
|
|
float2 ndc = float2(
|
|
coordinates[vertexId].x * pViewParams.invScreenDimensions.x * 2.0 - 1.0,
|
|
1.0 - coordinates[vertexId].y * pViewParams.invScreenDimensions.y * 2.0
|
|
);
|
|
output.position = float4(ndc, style.z, 1.0);
|
|
output.texCoords = coordinates[vertexId].zw;
|
|
output.style = style;
|
|
return output;
|
|
}
|
|
|
|
[shader("fragment")]
|
|
float4 fragmentMain(
|
|
float2 texCoords : TEXCOORD,
|
|
RenderElementStyle style : STYLE
|
|
) : SV_Target
|
|
{
|
|
if(style.textureIndex == -1) {
|
|
return float4(style.color, style.opacity);
|
|
}
|
|
// Glyph quads: the texture is the font's per-size atlas (R8_UNORM, coverage in the R
|
|
// channel, A baked to 1.0). Reading it as a Texture2D gives (R=coverage, G=0, B=0, A=1)
|
|
// — so the alpha we want is in .r, NOT .a (using .a would make every pixel opaque).
|
|
// Output the pure text color + coverage as alpha; the pipeline's SRC_ALPHA blend then
|
|
// applies the coverage exactly once (do NOT premultiply here or it'd be squared).
|
|
// The UV rect (style.uvOffset/uvSize) selects the glyph inside the atlas; plain
|
|
// textured UI quads use the full-rect default (0,0,1,1).
|
|
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);
|
|
}
|