1 |
6 |
Orka |
/*
|
2 |
|
|
ORSoC GFX accelerator core
|
3 |
|
|
Copyright 2012, ORSoC, Per Lenander, Anton Fosselius.
|
4 |
|
|
|
5 |
|
|
CLIPPING/SCISSORING MODULE
|
6 |
|
|
|
7 |
|
|
This file is part of orgfx.
|
8 |
|
|
|
9 |
|
|
orgfx is free software: you can redistribute it and/or modify
|
10 |
|
|
it under the terms of the GNU Lesser General Public License as published by
|
11 |
|
|
the Free Software Foundation, either version 3 of the License, or
|
12 |
|
|
(at your option) any later version.
|
13 |
|
|
|
14 |
|
|
orgfx is distributed in the hope that it will be useful,
|
15 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17 |
|
|
GNU Lesser General Public License for more details.
|
18 |
|
|
|
19 |
|
|
You should have received a copy of the GNU Lesser General Public License
|
20 |
|
|
along with orgfx. If not, see <http://www.gnu.org/licenses/>.
|
21 |
|
|
|
22 |
|
|
*/
|
23 |
|
|
|
24 |
|
|
/*
|
25 |
|
|
This module performs clipping by applying the current cliprect and the target size.
|
26 |
|
|
|
27 |
|
|
This module also performs z-buffer culling (requires reading from memory)
|
28 |
|
|
*/
|
29 |
|
|
module gfx_clip(clk_i, rst_i,
|
30 |
|
|
clipping_enable_i, zbuffer_enable_i,
|
31 |
|
|
zbuffer_base_i, target_size_x_i, target_size_y_i,
|
32 |
|
|
clip_pixel0_x_i, clip_pixel0_y_i, clip_pixel1_x_i, clip_pixel1_y_i, //clip pixel 0 and pixel 1
|
33 |
|
|
raster_pixel_x_i, raster_pixel_y_i, raster_u_i, raster_v_i, flat_color_i, raster_write_i, ack_o, // from raster
|
34 |
|
|
cuvz_pixel_x_i, cuvz_pixel_y_i, cuvz_pixel_z_i, cuvz_u_i, cuvz_v_i, cuvz_color_i, cuvz_write_i, // from cuvz
|
35 |
|
|
cuvz_a_i, // from cuvz
|
36 |
|
|
z_ack_i, z_addr_o, z_data_i, z_sel_o, z_request_o, wbm_busy_i, // from/to wbm reader
|
37 |
|
|
pixel_x_o, pixel_y_o, pixel_z_o, u_o, v_o, a_o, // to fragment
|
38 |
|
|
bezier_factor0_i, bezier_factor1_i, bezier_factor0_o, bezier_factor1_o, // bezier calculations
|
39 |
|
|
color_o, write_o, ack_i // from/to fragment
|
40 |
|
|
);
|
41 |
|
|
|
42 |
|
|
parameter point_width = 16;
|
43 |
|
|
|
44 |
|
|
input clk_i;
|
45 |
|
|
input rst_i;
|
46 |
|
|
|
47 |
|
|
input clipping_enable_i;
|
48 |
|
|
input zbuffer_enable_i;
|
49 |
|
|
input [31:2] zbuffer_base_i;
|
50 |
|
|
input [point_width-1:0] target_size_x_i;
|
51 |
|
|
input [point_width-1:0] target_size_y_i;
|
52 |
|
|
//clip pixels
|
53 |
|
|
input [point_width-1:0] clip_pixel0_x_i;
|
54 |
|
|
input [point_width-1:0] clip_pixel0_y_i;
|
55 |
|
|
input [point_width-1:0] clip_pixel1_x_i;
|
56 |
|
|
input [point_width-1:0] clip_pixel1_y_i;
|
57 |
|
|
|
58 |
|
|
// From cuvz
|
59 |
|
|
input [point_width-1:0] cuvz_pixel_x_i;
|
60 |
|
|
input [point_width-1:0] cuvz_pixel_y_i;
|
61 |
|
|
input signed [point_width-1:0] cuvz_pixel_z_i;
|
62 |
|
|
input [point_width-1:0] cuvz_u_i;
|
63 |
|
|
input [point_width-1:0] cuvz_v_i;
|
64 |
|
|
input [7:0] cuvz_a_i;
|
65 |
|
|
input [31:0] cuvz_color_i;
|
66 |
|
|
input cuvz_write_i;
|
67 |
|
|
// From raster
|
68 |
|
|
input [point_width-1:0] raster_pixel_x_i;
|
69 |
|
|
input [point_width-1:0] raster_pixel_y_i;
|
70 |
|
|
input [point_width-1:0] raster_u_i;
|
71 |
|
|
input [point_width-1:0] raster_v_i;
|
72 |
|
|
input [31:0] flat_color_i;
|
73 |
|
|
input raster_write_i;
|
74 |
|
|
output reg ack_o;
|
75 |
|
|
|
76 |
|
|
// Interface against wishbone master (reader)
|
77 |
|
|
input z_ack_i;
|
78 |
|
|
output [31:2] z_addr_o;
|
79 |
|
|
input [31:0] z_data_i;
|
80 |
|
|
output reg [3:0] z_sel_o;
|
81 |
|
|
output reg z_request_o;
|
82 |
|
|
input wbm_busy_i;
|
83 |
|
|
|
84 |
|
|
// To fragment
|
85 |
|
|
output reg [point_width-1:0] pixel_x_o;
|
86 |
|
|
output reg [point_width-1:0] pixel_y_o;
|
87 |
|
|
output reg [point_width-1:0] pixel_z_o;
|
88 |
|
|
output reg [point_width-1:0] u_o;
|
89 |
|
|
output reg [point_width-1:0] v_o;
|
90 |
|
|
output reg [7:0] a_o;
|
91 |
|
|
input [point_width-1:0] bezier_factor0_i;
|
92 |
|
|
input [point_width-1:0] bezier_factor1_i;
|
93 |
|
|
output reg [point_width-1:0] bezier_factor0_o;
|
94 |
|
|
output reg [point_width-1:0] bezier_factor1_o;
|
95 |
|
|
output reg [31:0] color_o;
|
96 |
|
|
output reg write_o;
|
97 |
|
|
input ack_i;
|
98 |
|
|
|
99 |
|
|
//
|
100 |
|
|
|
101 |
|
|
// State machine
|
102 |
|
|
reg [1:0] state;
|
103 |
|
|
parameter wait_state = 2'b00, z_read_state = 2'b01, write_pixel_state = 2'b10;
|
104 |
|
|
|
105 |
|
|
// Calculate address of target pixel
|
106 |
|
|
// Addr[31:2] = Base + (Y*width + X) * ppb
|
107 |
|
|
wire [31:0] pixel_offset = (target_size_x_i*cuvz_pixel_y_i + {16'h0, cuvz_pixel_x_i}) << 1; // 16 bit
|
108 |
|
|
assign z_addr_o = zbuffer_base_i + pixel_offset[31:2];
|
109 |
|
|
|
110 |
|
|
wire [31:0] z_value_at_target32;
|
111 |
|
|
wire signed [point_width-1:0] z_value_at_target = z_value_at_target32[point_width-1:0];
|
112 |
|
|
|
113 |
|
|
// Memory to color converter
|
114 |
|
|
memory_to_color memory_proc(
|
115 |
|
|
.color_depth_i (2'b01),
|
116 |
|
|
.mem_i (z_data_i),
|
117 |
|
|
.mem_lsb_i (cuvz_pixel_x_i[1:0]),
|
118 |
|
|
.color_o (z_value_at_target32),
|
119 |
|
|
.sel_o ()
|
120 |
|
|
);
|
121 |
|
|
|
122 |
|
|
// Forward texture coordinates, color, alpha etc
|
123 |
|
|
always @(posedge clk_i or posedge rst_i)
|
124 |
|
|
if(rst_i)
|
125 |
|
|
begin
|
126 |
|
|
u_o <= 1'b0;
|
127 |
|
|
v_o <= 1'b0;
|
128 |
|
|
bezier_factor0_o <= 1'b0;
|
129 |
|
|
bezier_factor1_o <= 1'b0;
|
130 |
|
|
pixel_x_o <= 1'b0;
|
131 |
|
|
pixel_y_o <= 1'b0;
|
132 |
|
|
pixel_z_o <= 1'b0;
|
133 |
|
|
color_o <= 1'b0;
|
134 |
|
|
a_o <= 1'b0;
|
135 |
|
|
z_sel_o <= 4'b1111;
|
136 |
|
|
end
|
137 |
|
|
else
|
138 |
|
|
begin
|
139 |
|
|
// Implement a MUX, send data from either the raster or from the cuvz module to the fragment processor
|
140 |
|
|
if(raster_write_i)
|
141 |
|
|
begin
|
142 |
|
|
u_o <= raster_u_i;
|
143 |
|
|
v_o <= raster_v_i;
|
144 |
|
|
a_o <= 8'hff;
|
145 |
|
|
color_o <= flat_color_i;
|
146 |
|
|
pixel_x_o <= raster_pixel_x_i;
|
147 |
|
|
pixel_y_o <= raster_pixel_y_i;
|
148 |
|
|
pixel_z_o <= 1'b0;
|
149 |
|
|
end
|
150 |
|
|
// If the cuvz module is being used, more parameters needs to be forwarded
|
151 |
|
|
else if(cuvz_write_i)
|
152 |
|
|
begin
|
153 |
|
|
u_o <= cuvz_u_i;
|
154 |
|
|
v_o <= cuvz_v_i;
|
155 |
|
|
a_o <= cuvz_a_i;
|
156 |
|
|
color_o <= cuvz_color_i;
|
157 |
|
|
pixel_x_o <= cuvz_pixel_x_i;
|
158 |
|
|
pixel_y_o <= cuvz_pixel_y_i;
|
159 |
|
|
pixel_z_o <= cuvz_pixel_z_i;
|
160 |
|
|
bezier_factor0_o <= bezier_factor0_i;
|
161 |
|
|
bezier_factor1_o <= bezier_factor1_i;
|
162 |
|
|
end
|
163 |
|
|
end
|
164 |
|
|
|
165 |
|
|
// Check if we should discard this pixel due to failing depth check
|
166 |
|
|
wire fail_z_check = z_value_at_target > cuvz_pixel_z_i;
|
167 |
|
|
|
168 |
|
|
// Check if we should discard this pixel due to falling outside render target/clip rect
|
169 |
|
|
wire outside_target = raster_write_i ? (raster_pixel_x_i >= target_size_x_i) | (raster_pixel_y_i >= target_size_y_i) :
|
170 |
|
|
(cuvz_pixel_x_i >= target_size_x_i) | (cuvz_pixel_y_i >= target_size_y_i);
|
171 |
|
|
wire outside_clip = raster_write_i ? (raster_pixel_x_i < clip_pixel0_x_i) | (raster_pixel_y_i < clip_pixel0_y_i) | (raster_pixel_x_i >= clip_pixel1_x_i) | (raster_pixel_y_i >= clip_pixel1_y_i) :
|
172 |
|
|
(cuvz_pixel_x_i < clip_pixel0_x_i) | (cuvz_pixel_y_i < clip_pixel0_y_i) | (cuvz_pixel_x_i >= clip_pixel1_x_i) | (cuvz_pixel_y_i >= clip_pixel1_y_i);
|
173 |
|
|
wire discard_pixel = outside_target | (clipping_enable_i & outside_clip);
|
174 |
|
|
|
175 |
|
|
// Check if there is a write signal
|
176 |
|
|
wire write_i = raster_write_i | cuvz_write_i;
|
177 |
|
|
|
178 |
|
|
// Acknowledge when a command has completed
|
179 |
|
|
always @(posedge clk_i or posedge rst_i)
|
180 |
|
|
// reset, init component
|
181 |
|
|
if(rst_i)
|
182 |
|
|
begin
|
183 |
|
|
ack_o <= 1'b0;
|
184 |
|
|
write_o <= 1'b0;
|
185 |
|
|
z_request_o <= 1'b0;
|
186 |
|
|
z_sel_o <= 4'b1111;
|
187 |
|
|
end
|
188 |
|
|
// Else, set outputs for next cycle
|
189 |
|
|
else
|
190 |
|
|
begin
|
191 |
|
|
case (state)
|
192 |
|
|
|
193 |
|
|
wait_state:
|
194 |
|
|
begin
|
195 |
|
|
if(write_i)
|
196 |
|
|
ack_o <= discard_pixel;
|
197 |
|
|
else
|
198 |
|
|
ack_o <= 1'b0;
|
199 |
|
|
|
200 |
|
|
if(write_i & zbuffer_enable_i)
|
201 |
|
|
z_request_o <= ~discard_pixel;
|
202 |
|
|
else if(write_i)
|
203 |
|
|
write_o <= ~discard_pixel;
|
204 |
|
|
|
205 |
|
|
end
|
206 |
|
|
|
207 |
|
|
// Do a depth check. If it fails, discard pixel, ack and go back to wait. If it succeeds, go to write state
|
208 |
|
|
z_read_state:
|
209 |
|
|
if(z_ack_i)
|
210 |
|
|
begin
|
211 |
|
|
write_o <= ~fail_z_check;
|
212 |
|
|
ack_o <= fail_z_check;
|
213 |
|
|
z_request_o <= 1'b0;
|
214 |
|
|
end
|
215 |
|
|
else
|
216 |
|
|
z_request_o <= ~wbm_busy_i | z_request_o;
|
217 |
|
|
|
218 |
|
|
// ack, then go back to wait state when the next module is ready
|
219 |
|
|
write_pixel_state:
|
220 |
|
|
begin
|
221 |
|
|
write_o <= 1'b0;
|
222 |
|
|
if(ack_i)
|
223 |
|
|
ack_o <= 1'b1;
|
224 |
|
|
end
|
225 |
|
|
|
226 |
|
|
endcase
|
227 |
|
|
|
228 |
|
|
end
|
229 |
|
|
|
230 |
|
|
// State machine
|
231 |
|
|
always @(posedge clk_i or posedge rst_i)
|
232 |
|
|
// reset, init component
|
233 |
|
|
if(rst_i)
|
234 |
|
|
state <= wait_state;
|
235 |
|
|
// Move in statemachine
|
236 |
|
|
else
|
237 |
|
|
case (state)
|
238 |
|
|
|
239 |
|
|
wait_state:
|
240 |
|
|
if(write_i & ~discard_pixel)
|
241 |
|
|
begin
|
242 |
|
|
if(zbuffer_enable_i)
|
243 |
|
|
state <= z_read_state;
|
244 |
|
|
else
|
245 |
|
|
state <= write_pixel_state;
|
246 |
|
|
end
|
247 |
|
|
|
248 |
|
|
z_read_state:
|
249 |
|
|
if(z_ack_i)
|
250 |
|
|
state <= fail_z_check ? wait_state : write_pixel_state;
|
251 |
|
|
|
252 |
|
|
write_pixel_state:
|
253 |
|
|
if(ack_i)
|
254 |
|
|
state <= wait_state;
|
255 |
|
|
|
256 |
|
|
endcase
|
257 |
|
|
|
258 |
|
|
endmodule
|
259 |
|
|
|