1 /*
2 * Copyright 2019 Advanced Micro Devices, Inc.
3 * Copyright 2021 Valve Corporation
4 *
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "ac_nir.h"
9 #include "nir_builder.h"
10
11 /* This code is adapted from ac_llvm_cull.c, hence the copyright to AMD. */
12
13 typedef struct
14 {
15 nir_def *w_reflection;
16 nir_def *all_w_negative;
17 nir_def *any_w_negative;
18 } position_w_info;
19
20 static void
analyze_position_w(nir_builder * b,nir_def * pos[][4],unsigned num_vertices,position_w_info * w_info)21 analyze_position_w(nir_builder *b, nir_def *pos[][4], unsigned num_vertices,
22 position_w_info *w_info)
23 {
24 w_info->all_w_negative = nir_imm_true(b);
25 w_info->w_reflection = nir_imm_false(b);
26 w_info->any_w_negative = nir_imm_false(b);
27
28 for (unsigned i = 0; i < num_vertices; ++i) {
29 nir_def *neg_w = nir_flt_imm(b, pos[i][3], 0.0f);
30 w_info->w_reflection = nir_ixor(b, neg_w, w_info->w_reflection);
31 w_info->any_w_negative = nir_ior(b, neg_w, w_info->any_w_negative);
32 w_info->all_w_negative = nir_iand(b, neg_w, w_info->all_w_negative);
33 }
34 }
35
36 static nir_def *
cull_face_triangle(nir_builder * b,nir_def * pos[3][4],const position_w_info * w_info)37 cull_face_triangle(nir_builder *b, nir_def *pos[3][4], const position_w_info *w_info)
38 {
39 nir_def *det_t0 = nir_fsub(b, pos[2][0], pos[0][0]);
40 nir_def *det_t1 = nir_fsub(b, pos[1][1], pos[0][1]);
41 nir_def *det_t2 = nir_fsub(b, pos[0][0], pos[1][0]);
42 nir_def *det_t3 = nir_fsub(b, pos[0][1], pos[2][1]);
43 nir_def *det_p0 = nir_fmul(b, det_t0, det_t1);
44 nir_def *det_p1 = nir_fmul(b, det_t2, det_t3);
45 nir_def *det = nir_fsub(b, det_p0, det_p1);
46
47 det = nir_bcsel(b, w_info->w_reflection, nir_fneg(b, det), det);
48
49 nir_def *front_facing_ccw = nir_fgt_imm(b, det, 0.0f);
50 nir_def *zero_area = nir_feq_imm(b, det, 0.0f);
51 nir_def *ccw = nir_load_cull_ccw_amd(b);
52 nir_def *front_facing = nir_ieq(b, front_facing_ccw, ccw);
53 nir_def *cull_front = nir_load_cull_front_face_enabled_amd(b);
54 nir_def *cull_back = nir_load_cull_back_face_enabled_amd(b);
55
56 nir_def *face_culled = nir_bcsel(b, front_facing, cull_front, cull_back);
57 face_culled = nir_ior(b, face_culled, zero_area);
58
59 /* Don't reject NaN and +/-infinity, these are tricky.
60 * Just trust fixed-function HW to handle these cases correctly.
61 */
62 return nir_iand(b, face_culled, nir_fisfinite(b, det));
63 }
64
65 static void
calc_bbox_triangle(nir_builder * b,nir_def * pos[3][4],nir_def * bbox_min[2],nir_def * bbox_max[2])66 calc_bbox_triangle(nir_builder *b, nir_def *pos[3][4], nir_def *bbox_min[2], nir_def *bbox_max[2])
67 {
68 for (unsigned chan = 0; chan < 2; ++chan) {
69 bbox_min[chan] = nir_fmin(b, pos[0][chan], nir_fmin(b, pos[1][chan], pos[2][chan]));
70 bbox_max[chan] = nir_fmax(b, pos[0][chan], nir_fmax(b, pos[1][chan], pos[2][chan]));
71 }
72 }
73
74 static nir_def *
cull_frustrum(nir_builder * b,nir_def * bbox_min[2],nir_def * bbox_max[2])75 cull_frustrum(nir_builder *b, nir_def *bbox_min[2], nir_def *bbox_max[2])
76 {
77 nir_def *prim_outside_view = nir_imm_false(b);
78
79 for (unsigned chan = 0; chan < 2; ++chan) {
80 prim_outside_view = nir_ior(b, prim_outside_view, nir_flt_imm(b, bbox_max[chan], -1.0f));
81 prim_outside_view = nir_ior(b, prim_outside_view, nir_fgt_imm(b, bbox_min[chan], 1.0f));
82 }
83
84 return prim_outside_view;
85 }
86
87 static nir_def *
cull_small_primitive_triangle(nir_builder * b,nir_def * bbox_min[2],nir_def * bbox_max[2],nir_def * prim_is_small_else)88 cull_small_primitive_triangle(nir_builder *b, nir_def *bbox_min[2], nir_def *bbox_max[2],
89 nir_def *prim_is_small_else)
90 {
91 nir_def *prim_is_small = NULL;
92
93 nir_if *if_cull_small_prims = nir_push_if(b, nir_load_cull_small_primitives_enabled_amd(b));
94 {
95 nir_def *vp = nir_load_viewport_xy_scale_and_offset(b);
96 nir_def *small_prim_precision = nir_load_cull_small_prim_precision_amd(b);
97 prim_is_small = prim_is_small_else;
98
99 for (unsigned chan = 0; chan < 2; ++chan) {
100 nir_def *vp_scale = nir_channel(b, vp, chan);
101 nir_def *vp_translate = nir_channel(b, vp, 2 + chan);
102
103 /* Convert the position to screen-space coordinates. */
104 nir_def *min = nir_ffma(b, bbox_min[chan], vp_scale, vp_translate);
105 nir_def *max = nir_ffma(b, bbox_max[chan], vp_scale, vp_translate);
106
107 /* Scale the bounding box according to precision. */
108 min = nir_fsub(b, min, small_prim_precision);
109 max = nir_fadd(b, max, small_prim_precision);
110
111 /* Determine if the bbox intersects the sample point, by checking if the min and max round to the same int. */
112 min = nir_fround_even(b, min);
113 max = nir_fround_even(b, max);
114
115 nir_def *rounded_to_eq = nir_feq(b, min, max);
116 prim_is_small = nir_ior(b, prim_is_small, rounded_to_eq);
117 }
118 }
119 nir_pop_if(b, if_cull_small_prims);
120
121 return nir_if_phi(b, prim_is_small, prim_is_small_else);
122 }
123
124 static nir_def *
ac_nir_cull_triangle(nir_builder * b,nir_def * initially_accepted,nir_def * pos[3][4],position_w_info * w_info,ac_nir_cull_accepted accept_func,void * state)125 ac_nir_cull_triangle(nir_builder *b,
126 nir_def *initially_accepted,
127 nir_def *pos[3][4],
128 position_w_info *w_info,
129 ac_nir_cull_accepted accept_func,
130 void *state)
131 {
132 nir_def *accepted = initially_accepted;
133 accepted = nir_iand(b, accepted, nir_inot(b, w_info->all_w_negative));
134 accepted = nir_iand(b, accepted, nir_inot(b, cull_face_triangle(b, pos, w_info)));
135
136 nir_def *bbox_accepted = NULL;
137
138 nir_if *if_accepted = nir_push_if(b, accepted);
139 {
140 nir_def *bbox_min[2] = {0}, *bbox_max[2] = {0};
141 calc_bbox_triangle(b, pos, bbox_min, bbox_max);
142
143 nir_def *prim_outside_view = cull_frustrum(b, bbox_min, bbox_max);
144 nir_def *prim_invisible =
145 cull_small_primitive_triangle(b, bbox_min, bbox_max, prim_outside_view);
146
147 bbox_accepted = nir_ior(b, nir_inot(b, prim_invisible), w_info->any_w_negative);
148
149 /* for caller which need to react when primitive is accepted */
150 if (accept_func) {
151 nir_if *if_still_accepted = nir_push_if(b, bbox_accepted);
152 if_still_accepted->control = nir_selection_control_divergent_always_taken;
153 {
154 accept_func(b, state);
155 }
156 nir_pop_if(b, if_still_accepted);
157 }
158 }
159 nir_pop_if(b, if_accepted);
160
161 return nir_if_phi(b, bbox_accepted, accepted);
162 }
163
164 static void
rotate_45degrees(nir_builder * b,nir_def * v[2])165 rotate_45degrees(nir_builder *b, nir_def *v[2])
166 {
167 /* Rotating a triangle by 45 degrees:
168 *
169 * x2 = x*cos(45) - y*sin(45)
170 * y2 = x*sin(45) + y*cos(45)
171 *
172 * Since sin(45) == cos(45), we can write:
173 *
174 * x2 = x*cos(45) - y*cos(45) = (x - y) * cos(45)
175 * y2 = x*cos(45) + y*cos(45) = (x + y) * cos(45)
176 *
177 * The width of each square (rotated diamond) is sqrt(0.5), so we have to scale it to 1
178 * by multiplying by 1/sqrt(0.5) = sqrt(2) because we want round() to give us the position
179 * of the closest center of the square (rotated diamond). After scaling, we get:
180 *
181 * x2 = (x - y) * cos(45) * sqrt(2)
182 * y2 = (x + y) * cos(45) * sqrt(2)
183 *
184 * Since cos(45) * sqrt(2) = 1, we get:
185 *
186 * x2 = x - y
187 * y2 = x + y
188 */
189 nir_def *result[2];
190 result[0] = nir_fsub(b, v[0], v[1]);
191 result[1] = nir_fadd(b, v[0], v[1]);
192
193 memcpy(v, result, sizeof(result));
194 }
195
196 static void
calc_bbox_line(nir_builder * b,nir_def * pos[3][4],nir_def * bbox_min[2],nir_def * bbox_max[2])197 calc_bbox_line(nir_builder *b, nir_def *pos[3][4], nir_def *bbox_min[2], nir_def *bbox_max[2])
198 {
199 nir_def *clip_half_line_width = nir_load_clip_half_line_width_amd(b);
200
201 for (unsigned chan = 0; chan < 2; ++chan) {
202 bbox_min[chan] = nir_fmin(b, pos[0][chan], pos[1][chan]);
203 bbox_max[chan] = nir_fmax(b, pos[0][chan], pos[1][chan]);
204
205 nir_def *width = nir_channel(b, clip_half_line_width, chan);
206 bbox_min[chan] = nir_fsub(b, bbox_min[chan], width);
207 bbox_max[chan] = nir_fadd(b, bbox_max[chan], width);
208 }
209 }
210
211 static nir_def *
cull_small_primitive_line(nir_builder * b,nir_def * pos[3][4],nir_def * bbox_min[2],nir_def * bbox_max[2],nir_def * prim_is_small_else)212 cull_small_primitive_line(nir_builder *b, nir_def *pos[3][4],
213 nir_def *bbox_min[2], nir_def *bbox_max[2],
214 nir_def *prim_is_small_else)
215 {
216 nir_def *prim_is_small = NULL;
217
218 /* Small primitive filter - eliminate lines that are too small to affect a sample. */
219 nir_if *if_cull_small_prims = nir_push_if(b, nir_load_cull_small_primitives_enabled_amd(b));
220 {
221 /* This only works with lines without perpendicular end caps (lines with perpendicular
222 * end caps are rasterized as quads and thus can't be culled as small prims in 99% of
223 * cases because line_width >= 1).
224 *
225 * This takes advantage of the diamond exit rule, which says that every pixel
226 * has a diamond inside it touching the pixel boundary and only if a line exits
227 * the diamond, that pixel is filled. If a line enters the diamond or stays
228 * outside the diamond, the pixel isn't filled.
229 *
230 * This algorithm is a little simpler than that. The space outside all diamonds also
231 * has the same diamond shape, which we'll call corner diamonds.
232 *
233 * The idea is to cull all lines that are entirely inside a diamond, including
234 * corner diamonds. If a line is entirely inside a diamond, it can be culled because
235 * it doesn't exit it. If a line is entirely inside a corner diamond, it can be culled
236 * because it doesn't enter any diamond and thus can't exit any diamond.
237 *
238 * The viewport is rotated by 45 degrees to turn diamonds into squares, and a bounding
239 * box test is used to determine whether a line is entirely inside any square (diamond).
240 *
241 * The line width doesn't matter. Wide lines only duplicate filled pixels in either X or
242 * Y direction from the filled pixels. MSAA also doesn't matter. MSAA should ideally use
243 * perpendicular end caps that enable quad rasterization for lines. Thus, this should
244 * always use non-MSAA viewport transformation and non-MSAA small prim precision.
245 *
246 * A good test is piglit/lineloop because it draws 10k subpixel lines in a circle.
247 * It should contain no holes if this matches hw behavior.
248 */
249 nir_def *v0[2], *v1[2];
250 nir_def *vp = nir_load_viewport_xy_scale_and_offset(b);
251
252 /* Get vertex positions in pixels. */
253 for (unsigned chan = 0; chan < 2; chan++) {
254 nir_def *vp_scale = nir_channel(b, vp, chan);
255 nir_def *vp_translate = nir_channel(b, vp, 2 + chan);
256
257 v0[chan] = nir_ffma(b, pos[0][chan], vp_scale, vp_translate);
258 v1[chan] = nir_ffma(b, pos[1][chan], vp_scale, vp_translate);
259 }
260
261 /* Rotate the viewport by 45 degrees, so that diamonds become squares. */
262 rotate_45degrees(b, v0);
263 rotate_45degrees(b, v1);
264
265 nir_def *small_prim_precision = nir_load_cull_small_prim_precision_amd(b);
266
267 nir_def *rounded_to_eq[2];
268 for (unsigned chan = 0; chan < 2; chan++) {
269 /* Compute the bounding box around both vertices. We do this because we must
270 * enlarge the line area by the precision of the rasterizer.
271 */
272 nir_def *min = nir_fmin(b, v0[chan], v1[chan]);
273 nir_def *max = nir_fmax(b, v0[chan], v1[chan]);
274
275 /* Enlarge the bounding box by the precision of the rasterizer. */
276 min = nir_fsub(b, min, small_prim_precision);
277 max = nir_fadd(b, max, small_prim_precision);
278
279 /* Round the bounding box corners. If both rounded corners are equal,
280 * the bounding box is entirely inside a square (diamond).
281 */
282 min = nir_fround_even(b, min);
283 max = nir_fround_even(b, max);
284
285 rounded_to_eq[chan] = nir_feq(b, min, max);
286 }
287
288 prim_is_small = nir_iand(b, rounded_to_eq[0], rounded_to_eq[1]);
289 prim_is_small = nir_ior(b, prim_is_small, prim_is_small_else);
290 }
291 nir_pop_if(b, if_cull_small_prims);
292
293 return nir_if_phi(b, prim_is_small, prim_is_small_else);
294 }
295
296 static nir_def *
ac_nir_cull_line(nir_builder * b,nir_def * initially_accepted,nir_def * pos[3][4],position_w_info * w_info,ac_nir_cull_accepted accept_func,void * state)297 ac_nir_cull_line(nir_builder *b,
298 nir_def *initially_accepted,
299 nir_def *pos[3][4],
300 position_w_info *w_info,
301 ac_nir_cull_accepted accept_func,
302 void *state)
303 {
304 nir_def *accepted = initially_accepted;
305 accepted = nir_iand(b, accepted, nir_inot(b, w_info->all_w_negative));
306
307 nir_def *bbox_accepted = NULL;
308
309 nir_if *if_accepted = nir_push_if(b, accepted);
310 {
311 nir_def *bbox_min[2] = {0}, *bbox_max[2] = {0};
312 calc_bbox_line(b, pos, bbox_min, bbox_max);
313
314 /* Frustrum culling - eliminate lines that are fully outside the view. */
315 nir_def *prim_outside_view = cull_frustrum(b, bbox_min, bbox_max);
316 nir_def *prim_invisible =
317 cull_small_primitive_line(b, pos, bbox_min, bbox_max, prim_outside_view);
318
319 bbox_accepted = nir_ior(b, nir_inot(b, prim_invisible), w_info->any_w_negative);
320
321 /* for caller which need to react when primitive is accepted */
322 if (accept_func) {
323 nir_if *if_still_accepted = nir_push_if(b, bbox_accepted);
324 {
325 accept_func(b, state);
326 }
327 nir_pop_if(b, if_still_accepted);
328 }
329 }
330 nir_pop_if(b, if_accepted);
331
332 return nir_if_phi(b, bbox_accepted, accepted);
333 }
334
335 nir_def *
ac_nir_cull_primitive(nir_builder * b,nir_def * initially_accepted,nir_def * pos[3][4],unsigned num_vertices,ac_nir_cull_accepted accept_func,void * state)336 ac_nir_cull_primitive(nir_builder *b,
337 nir_def *initially_accepted,
338 nir_def *pos[3][4],
339 unsigned num_vertices,
340 ac_nir_cull_accepted accept_func,
341 void *state)
342 {
343 position_w_info w_info = {0};
344 analyze_position_w(b, pos, num_vertices, &w_info);
345
346 if (num_vertices == 3)
347 return ac_nir_cull_triangle(b, initially_accepted, pos, &w_info, accept_func, state);
348 else if (num_vertices == 2)
349 return ac_nir_cull_line(b, initially_accepted, pos, &w_info, accept_func, state);
350 else
351 unreachable("point culling not implemented");
352
353 return NULL;
354 }
355