rect shader improvements, moved calculations from pixel to vertex shader

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