summaryrefslogtreecommitdiff
path: root/rtl/gfx/gfx_frag_bary.sv
diff options
context:
space:
mode:
authorAlejandro Soto <alejandro@34project.org>2023-11-12 22:18:10 -0600
committerAlejandro Soto <alejandro@34project.org>2023-11-14 07:48:52 -0600
commit58d647a047e8761ad1f619173ee51dd4b65831ac (patch)
treee3a08d6321da9a031caf81d84a59ca4f05823fa9 /rtl/gfx/gfx_frag_bary.sv
parenta401d413ba766b01aa980a8e013c79500a490c2e (diff)
rtl/gfx: implement perspective-corrected barycentric coordinates
Diffstat (limited to 'rtl/gfx/gfx_frag_bary.sv')
-rw-r--r--rtl/gfx/gfx_frag_bary.sv78
1 files changed, 78 insertions, 0 deletions
diff --git a/rtl/gfx/gfx_frag_bary.sv b/rtl/gfx/gfx_frag_bary.sv
new file mode 100644
index 0000000..5004866
--- /dev/null
+++ b/rtl/gfx/gfx_frag_bary.sv
@@ -0,0 +1,78 @@
+`include "gfx/gfx_defs.sv"
+
+module gfx_frag_bary
+(
+ input logic clk,
+
+ input fixed_tri edges,
+ ws,
+ input logic stall,
+
+ output fixed b1,
+ b2
+);
+
+ fixed area, b0_w0, b1_w1, b2_w2, b1_w1_b2_w2, hold_b0_w0, hold_b1_w1, hold_b2_w2;
+ fixed_tri bs_ws, orthographic_bs;
+
+ assign b0_w0 = bs_ws[0];
+ assign b1_w1 = bs_ws[1];
+ assign b2_w2 = bs_ws[2];
+
+ assign orthographic_bs[0] = edges[`EDGE_P1_TO_P2];
+ assign orthographic_bs[1] = edges[`EDGE_P2_TO_P0];
+ assign orthographic_bs[2] = edges[`EDGE_P0_TO_P1];
+
+ genvar i;
+ generate
+ for (i = 0; i < 3; ++i) begin: vertices
+ gfx_fixed_div div_b_w
+ (
+ .z(orthographic_bs[i]),
+ .d(ws[i]),
+ .q(bs_ws[i]),
+ .*
+ );
+ end
+ endgenerate
+
+ localparam AREA_STAGES = 2;
+
+ gfx_pipes #(.WIDTH($bits(fixed)), .DEPTH(AREA_STAGES)) b1_w1_pipes
+ (
+ .in(b1_w1),
+ .out(hold_b1_w1),
+ .*
+ );
+
+ gfx_pipes #(.WIDTH($bits(fixed)), .DEPTH(AREA_STAGES)) b2_w2_pipes
+ (
+ .in(b2_w2),
+ .out(hold_b2_w2),
+ .*
+ );
+
+ gfx_fixed_div norm_b1
+ (
+ .z(hold_b1_w1),
+ .d(area),
+ .q(b1),
+ .*
+ );
+
+ gfx_fixed_div norm_b2
+ (
+ .z(hold_b2_w2),
+ .d(area),
+ .q(b2),
+ .*
+ );
+
+ always_ff @(posedge clk)
+ if (!stall) begin
+ area <= hold_b0_w0 + b1_w1_b2_w2;
+ hold_b0_w0 <= b0_w0;
+ b1_w1_b2_w2 <= b1_w1 + b2_w2;
+ end
+
+endmodule