power_play/src/ui/ui_shaders.hlsl
2025-12-08 16:33:44 -06:00

205 lines
5.3 KiB
HLSL

////////////////////////////////////////////////////////////
//~ Rect
Struct(UI_DRectPSInput)
{
Semantic(Vec4, sv_position);
Semantic(nointerpolation u32, rect_idx);
Semantic(Vec4, background_lin);
Semantic(Vec4, border_lin);
Semantic(Vec4, tint_lin);
Semantic(Vec2, rect_uv);
Semantic(Vec2, tex_uv);
};
Struct(UI_DRectPSOutput)
{
Semantic(Vec4, sv_target0);
};
//////////////////////////////
//- Vertex shader
VertexShader(UI_DRectVS, UI_DRectPSInput)
{
UI_DParams params = StructuredBufferFromHandle<UI_DParams>(UI_ShaderConst_Params)[0];
StructuredBuffer<UI_DRect> rects = StructuredBufferFromHandle<UI_DRect>(params.rects);
UI_DRect rect = rects[SV_InstanceID];
Vec2 rect_uv = RectUvFromVertexId(SV_VertexID);
Vec2 tex_uv = lerp(rect.tex_uv0, rect.tex_uv1, rect_uv);
Vec2 target_pos = lerp(rect.p0, rect.p1, rect_uv);
UI_DRectPSInput result;
result.sv_position = Vec4(NdcFromPos(target_pos, Vec2(params.target_size).xy), 0, 1);
result.background_lin = rect.background_lin;
result.border_lin = rect.border_lin;
result.tint_lin = rect.tint_lin;
result.rect_idx = SV_InstanceID;
result.rect_uv = rect_uv;
result.tex_uv = tex_uv;
return result;
}
//////////////////////////////
//- Pixel shader
PixelShader(UI_DRectPS, UI_DRectPSOutput, UI_DRectPSInput input)
{
UI_DParams params = StructuredBufferFromHandle<UI_DParams>(UI_ShaderConst_Params)[0];
StructuredBuffer<UI_DRect> rects = StructuredBufferFromHandle<UI_DRect>(params.rects);
SamplerState sampler = SamplerStateFromHandle(params.sampler);
UI_DRect rect = rects[input.rect_idx];
Vec2 p = input.sv_position.xy;
Vec2 rect_uv = input.rect_uv;
Vec2 p0 = rect.p0;
Vec2 p1 = rect.p1;
/* Calculate rect sdf (negative means pixel is inside of rect) */
f32 rect_dist = min(min(p.x - p0.x, p1.x - p.x), min(p.y - p0.y, p1.y - p.y));
{
f32 tl_radius = rect.tl_rounding;
f32 tr_radius = rect.tr_rounding;
f32 br_radius = rect.br_rounding;
f32 bl_radius = rect.bl_rounding;
Vec2 tl = Vec2(p0.x + tl_radius, p0.y + tl_radius);
Vec2 tr = Vec2(p1.x - tr_radius, p0.y + tr_radius);
Vec2 br = Vec2(p1.x - br_radius, p1.y - br_radius);
Vec2 bl = Vec2(p0.x + bl_radius, p1.y - bl_radius);
if (p.x < tl.x && p.y < tl.y) { rect_dist = min(rect_dist, tl_radius - length(tl - p)); }
if (p.x > tr.x && p.y < tr.y) { rect_dist = min(rect_dist, tr_radius - length(tr - p)); }
if (p.x > br.x && p.y > br.y) { rect_dist = min(rect_dist, br_radius - length(br - p)); }
if (p.x < bl.x && p.y > bl.y) { rect_dist = min(rect_dist, bl_radius - length(bl - p)); }
}
rect_dist = -rect_dist;
/* Calculate border sdf (negative means pixel is inside of border) */
f32 border_width = 0;
f32 border_dist = 0;
Vec4 border_color = 0;
{
if (rect.border > 0)
{
border_width = rect.border;
border_color = input.border_lin;
}
else
{
border_width = 0;
border_color = input.background_lin;
}
border_dist = abs(rect_dist);
if (rect_dist <= 0)
{
border_dist -= border_width;
}
}
/* Background color */
Vec4 background_color = 0;
{
if (rect_dist <= 0)
{
if (border_dist <= 0)
{
background_color = border_color;
}
else if (!IsGpuHandleNil(rect.tex))
{
Texture2D<Vec4> tex = Texture2DFromHandle<Vec4>(rect.tex);
background_color = tex.Sample(sampler, input.tex_uv);
}
else
{
background_color = input.background_lin;
}
}
}
/* Final color */
Vec4 final_color = background_color;
final_color *= input.tint_lin;
/* Debug color */
if (UI_ShaderConst_DebugDraw)
{
final_color = rect.debug_lin;
}
UI_DRectPSOutput output;
output.sv_target0 = final_color;
return output;
}
////////////////////////////////////////////////////////////
//~ Blit
Struct(UI_BlitPSInput)
{
Semantic(Vec4, SV_Position);
Semantic(Vec2, src_uv);
};
Struct(UI_BlitPSOutput)
{
Semantic(Vec4, SV_Target0);
};
//////////////////////////////
//- Vertex shader
VertexShader(UI_BlitVS, UI_BlitPSInput)
{
Vec2 uv = RectUvFromVertexId(SV_VertexID);
UI_BlitPSInput result;
result.SV_Position = Vec4(NdcFromUv(uv).xy, 0, 1);
result.src_uv = uv;
return result;
}
//////////////////////////////
//- Pixel shader
PixelShader(UI_BlitPS, UI_BlitPSOutput, UI_BlitPSInput input)
{
UI_DParams params = StructuredBufferFromHandle<UI_DParams>(UI_ShaderConst_Params)[0];
SamplerState sampler = SamplerStateFromHandle(params.sampler);
Texture2D<Vec4> tex = Texture2DFromHandle<Vec4>(params.target_ro);
Vec2 uv = input.src_uv;
Vec4 result = tex.Sample(sampler, uv);
u32 ints[] = {
1,
2,
3,
4
};
u32 count = countof(ints);
if (count == 4)
{
result.g = 1;
}
else
{
// result.b = 1;
}
DebugPrint("Hello there");
UI_BlitPSOutput output;
output.SV_Target0 = result;
return output;
}