struct vs_input { float4 pos : POSITION; float4 color_bg0 : COLOR_BG_A; float4 color_bg1 : COLOR_BG_B; float4 color_line : COLOR_LINE; float4 color_x : COLOR_X; float4 color_y : COLOR_Y; float line_thickness : THICKNESS; float line_spacing : SPACING; float2 offset : OFFSET; }; struct ps_input { float4 screen_pos : SV_POSITION; float4 color_bg0 : COLOR_BG_A; float4 color_bg1 : COLOR_BG_B; float4 color_line : COLOR_LINE; float4 color_x : COLOR_X; float4 color_y : COLOR_Y; float line_thickness : THICKNESS; float line_spacing : SPACING; float2 offset : OFFSET; }; cbuffer vs_constants : register(b0) { float4x4 projection; }; ps_input vs_main(vs_input input) { ps_input output; output.screen_pos = mul(projection, float4(input.pos.xy, 0.f, 1.f)); output.color_bg0 = input.color_bg0; output.color_bg1 = input.color_bg1; output.color_line = input.color_line; output.color_x = input.color_x; output.color_y = input.color_y; output.line_thickness = input.line_thickness; output.line_spacing = input.line_spacing; output.offset = input.offset; return output; } float4 ps_main(ps_input input) : SV_TARGET { float2 grid_pos = input.screen_pos.xy + input.offset; float half_thickness = input.line_thickness / 2; float spacing = input.line_spacing; float4 color = input.color_bg0; float2 v = abs(round(grid_pos / spacing) * spacing - grid_pos); float dist = min(v.x, v.y); if (grid_pos.y <= half_thickness && grid_pos.y >= -half_thickness) { color = input.color_x; } else if (grid_pos.x <= half_thickness && grid_pos.x >= -half_thickness) { color = input.color_y; } else if (dist < half_thickness) { color = input.color_line; } else { bool checker = false; uint cell_x = (uint)(abs(grid_pos.x) / spacing) + (grid_pos.x < 0); uint cell_y = (uint)(abs(grid_pos.y) / spacing) + (grid_pos.y < 0); if (cell_x % 2 == 0) { checker = cell_y % 2 == 0; } else { checker = cell_y % 2 == 1; } if (checker) { color = input.color_bg1; } } return color; }