rect shader improvements, moved calculations from pixel to vertex shader

This commit is contained in:
Martins Mozeiko
2024-01-23 08:38:53 -08:00
committed by Ryan Fleury
parent 9a2ae21b89
commit a77d457f51
2 changed files with 68 additions and 84 deletions
+34 -42
View File
@@ -62,18 +62,15 @@ struct CPU2Vertex
struct Vertex2Pixel
{
float4 position : SV_POSITION;
float2 rect_half_size_px : PSIZE;
float2 texcoord_pct : TEX;
float2 cornercoord_pct : COLC;
float4 color00 : COL0;
float4 color01 : COL1;
float4 color10 : COL2;
float4 color11 : COL3;
float corner_radius_px : CRAD;
float border_thickness_px : BTHC;
float softness_px : SFT;
float omit_texture : OTX;
float4 position : SV_POSITION;
nointerpolation float2 rect_half_size_px : PSIZE;
float2 texcoord_pct : TEX;
float2 sdf_sample_pos : SDF;
float4 tint : TINT;
float corner_radius_px : CRAD;
nointerpolation float border_thickness_px : BTHC;
nointerpolation float softness_px : SFT;
nointerpolation float omit_texture : OTX;
};
Texture2D main_t2d : register(t0);
@@ -104,24 +101,17 @@ vs_main(CPU2Vertex cpu2vertex)
//- rjf: prep per-vertex arrays to sample from (p: position, t: texcoord, c: colorcoord, r: cornerradius)
float2 dst_p_verts_px[] =
{
mul(xform, float3(dst_p0_px.x, dst_p1_px.y, 1)).xy * float2(1, -1) + float2(0, viewport_size_px.y),
mul(xform, float3(dst_p0_px.x, dst_p0_px.y, 1)).xy * float2(1, -1) + float2(0, viewport_size_px.y),
mul(xform, float3(dst_p1_px.x, dst_p1_px.y, 1)).xy * float2(1, -1) + float2(0, viewport_size_px.y),
mul(xform, float3(dst_p1_px.x, dst_p0_px.y, 1)).xy * float2(1, -1) + float2(0, viewport_size_px.y),
float2(dst_p0_px.x, viewport_size_px.y - dst_p1_px.y),
float2(dst_p0_px.x, viewport_size_px.y - dst_p0_px.y),
float2(dst_p1_px.x, viewport_size_px.y - dst_p1_px.y),
float2(dst_p1_px.x, viewport_size_px.y - dst_p0_px.y),
};
float2 src_p_verts_pct[] =
float2 src_p_verts_px[] =
{
float2(src_p0_px.x/texture_t2d_size_px.x, src_p1_px.y/texture_t2d_size_px.y),
float2(src_p0_px.x/texture_t2d_size_px.x, src_p0_px.y/texture_t2d_size_px.y),
float2(src_p1_px.x/texture_t2d_size_px.x, src_p1_px.y/texture_t2d_size_px.y),
float2(src_p1_px.x/texture_t2d_size_px.x, src_p0_px.y/texture_t2d_size_px.y),
};
float2 dst_c_verts_pct[] =
{
float2(0, 1),
float2(0, 0),
float2(1, 1),
float2(1, 0),
float2(src_p0_px.x, src_p1_px.y),
float2(src_p0_px.x, src_p0_px.y),
float2(src_p1_px.x, src_p1_px.y),
float2(src_p1_px.x, src_p0_px.y),
};
float dst_r_verts_px[] =
{
@@ -130,21 +120,26 @@ vs_main(CPU2Vertex cpu2vertex)
cpu2vertex.corner_radii_px.w,
cpu2vertex.corner_radii_px.z,
};
float4 src_color[] = {
cpu2vertex.color01,
cpu2vertex.color00,
cpu2vertex.color11,
cpu2vertex.color10,
};
float2 dst_verts_pct = float2(
(cpu2vertex.vertex_id >> 1) ? 1.f : 0.f,
(cpu2vertex.vertex_id & 1) ? 0.f : 1.f);
// rjf: fill vertex -> pixel data
Vertex2Pixel vertex2pixel;
{
vertex2pixel.position.x = 2 * dst_p_verts_px[cpu2vertex.vertex_id].x / viewport_size_px.x - 1.f;
vertex2pixel.position.y = 2 * dst_p_verts_px[cpu2vertex.vertex_id].y / viewport_size_px.y - 1.f;
vertex2pixel.position.xy = 2.f * mul(xform, float3(dst_p_verts_px[cpu2vertex.vertex_id], 1.f)).xy / viewport_size_px - 1.f;
vertex2pixel.position.z = 0.f;
vertex2pixel.position.w = 1.f;
vertex2pixel.rect_half_size_px = dst_size_px/2 * xform_scale;
vertex2pixel.texcoord_pct = src_p_verts_pct[cpu2vertex.vertex_id];
vertex2pixel.cornercoord_pct = dst_c_verts_pct[cpu2vertex.vertex_id];
vertex2pixel.color00 = cpu2vertex.color00;
vertex2pixel.color01 = cpu2vertex.color01;
vertex2pixel.color10 = cpu2vertex.color10;
vertex2pixel.color11 = cpu2vertex.color11;
vertex2pixel.rect_half_size_px = dst_size_px / 2.f * xform_scale;
vertex2pixel.texcoord_pct = src_p_verts_px[cpu2vertex.vertex_id] / texture_t2d_size_px;
vertex2pixel.sdf_sample_pos = (2.f * dst_verts_pct - 1.f) * vertex2pixel.rect_half_size_px;
vertex2pixel.tint = src_color[cpu2vertex.vertex_id];
vertex2pixel.corner_radius_px = dst_r_verts_px[cpu2vertex.vertex_id];
vertex2pixel.border_thickness_px = border_thickness_px;
vertex2pixel.softness_px = softness_px;
@@ -159,9 +154,7 @@ float4
ps_main(Vertex2Pixel vertex2pixel) : SV_TARGET
{
// rjf: blend corner colors to produce final tint
float4 top_color = (1-vertex2pixel.cornercoord_pct.x)*vertex2pixel.color00 + (vertex2pixel.cornercoord_pct.x)*vertex2pixel.color10;
float4 bot_color = (1-vertex2pixel.cornercoord_pct.x)*vertex2pixel.color01 + (vertex2pixel.cornercoord_pct.x)*vertex2pixel.color11;
float4 tint = (1-vertex2pixel.cornercoord_pct.y)*top_color + (vertex2pixel.cornercoord_pct.y)*bot_color;
float4 tint = vertex2pixel.tint;
// rjf: sample texture
float4 albedo_sample = float4(1, 1, 1, 1);
@@ -171,8 +164,7 @@ ps_main(Vertex2Pixel vertex2pixel) : SV_TARGET
}
// rjf: determine SDF sample position
float2 sdf_sample_pos = float2((2*vertex2pixel.cornercoord_pct.x-1)*vertex2pixel.rect_half_size_px.x,
(2*vertex2pixel.cornercoord_pct.y-1)*vertex2pixel.rect_half_size_px.y);
float2 sdf_sample_pos = vertex2pixel.sdf_sample_pos;
// rjf: sample for corners
float corner_sdf_s = rect_sdf(sdf_sample_pos,