1 /*
2 * Copyright 2022 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "main/enums.h"
25 #include "main/context.h"
26
27 #include "st_context.h"
28 #include "st_nir.h"
29 #include "st_draw.h"
30
31 #include "nir.h"
32 #include "nir_builtin_builder.h"
33
34 #include "util/u_memory.h"
35
36 union state_key {
37 struct {
38 unsigned num_user_clip_planes:4;
39 unsigned face_culling_enabled:1;
40 unsigned result_offset_from_attribute:1;
41 unsigned primitive:4;
42 };
43 uint32_t u32;
44 };
45
46 enum primitive_state {
47 HW_SELECT_PRIM_NONE,
48 HW_SELECT_PRIM_POINTS,
49 HW_SELECT_PRIM_LINES,
50 HW_SELECT_PRIM_TRIANGLES,
51 HW_SELECT_PRIM_QUADS,
52 };
53
54 struct geometry_constant {
55 float depth_scale;
56 float depth_transport;
57 uint32_t culling_config;
58 uint32_t result_offset;
59 float clip_planes[MAX_CLIP_PLANES][4];
60 };
61
62 #define set_uniform_location(var, field, packed) \
63 do { \
64 unsigned offset = offsetof(struct geometry_constant, field); \
65 var->data.driver_location = offset >> (packed ? 2 : 4); \
66 var->data.location_frac = (offset >> 2) & 0x3; \
67 } while (0)
68
69 static nir_def *
has_nan_or_inf(nir_builder * b,nir_def * v)70 has_nan_or_inf(nir_builder *b, nir_def *v)
71 {
72 nir_def *nan = nir_bany_fnequal4(b, v, v);
73
74 nir_def *inf = nir_bany(b, nir_feq_imm(b, nir_fabs(b, v), INFINITY));
75
76 return nir_ior(b, nan, inf);
77 }
78
79 static void
return_if_true(nir_builder * b,nir_def * cond)80 return_if_true(nir_builder *b, nir_def *cond)
81 {
82 nir_if *if_cond = nir_push_if(b, cond);
83 nir_jump(b, nir_jump_return);
84 nir_pop_if(b, if_cond);
85 }
86
87 static void
get_input_vertices(nir_builder * b,nir_def ** v)88 get_input_vertices(nir_builder *b, nir_def **v)
89 {
90 const int num_in_vert = b->shader->info.gs.vertices_in;
91
92 nir_variable *in_pos = nir_variable_create(
93 b->shader, nir_var_shader_in, glsl_array_type(glsl_vec4_type(), num_in_vert, 0),
94 "gl_Position");
95 in_pos->data.location = VARYING_SLOT_POS;
96
97 nir_def *is_nan_or_inf = NULL;
98 for (int i = 0; i < num_in_vert; i++) {
99 v[i] = nir_load_array_var_imm(b, in_pos, i);
100 nir_def *r = has_nan_or_inf(b, v[i]);
101 is_nan_or_inf = i ? nir_ior(b, is_nan_or_inf, r) : r;
102 }
103 return_if_true(b, is_nan_or_inf);
104 }
105
106 static void
face_culling(nir_builder * b,nir_def ** v,bool packed)107 face_culling(nir_builder *b, nir_def **v, bool packed)
108 {
109 /* use the z value of the face normal to determine if the face points to us:
110 * Nz = (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0)
111 *
112 * it should be in NDC (Normalized Device Coordinate), but now we are in clip
113 * space (Vd = Vc / Vc.w), so multiply Nz with w0*w1*w2 to get the clip space
114 * value:
115 * det = x0 * (y1 * w2 - y2 * w1) +
116 * x1 * (y2 * w0 - y0 * w2) +
117 * x2 * (y0 * w1 - y1 * w0)
118 *
119 * we only care about the sign of the det, but also need to count the sign of
120 * w0/w1/w2 as a negtive w would change the direction of Nz < 0
121 */
122 nir_def *y1w2 = nir_fmul(b, nir_channel(b, v[1], 1), nir_channel(b, v[2], 3));
123 nir_def *y2w1 = nir_fmul(b, nir_channel(b, v[2], 1), nir_channel(b, v[1], 3));
124 nir_def *y2w0 = nir_fmul(b, nir_channel(b, v[2], 1), nir_channel(b, v[0], 3));
125 nir_def *y0w2 = nir_fmul(b, nir_channel(b, v[0], 1), nir_channel(b, v[2], 3));
126 nir_def *y0w1 = nir_fmul(b, nir_channel(b, v[0], 1), nir_channel(b, v[1], 3));
127 nir_def *y1w0 = nir_fmul(b, nir_channel(b, v[1], 1), nir_channel(b, v[0], 3));
128 nir_def *t0 = nir_fmul(b, nir_channel(b, v[0], 0), nir_fsub(b, y1w2, y2w1));
129 nir_def *t1 = nir_fmul(b, nir_channel(b, v[1], 0), nir_fsub(b, y2w0, y0w2));
130 nir_def *t2 = nir_fmul(b, nir_channel(b, v[2], 0), nir_fsub(b, y0w1, y1w0));
131 nir_def *det = nir_fadd(b, nir_fadd(b, t0, t1), t2);
132
133 /* invert det sign once any vertex w < 0 */
134 nir_def *n0 = nir_flt_imm(b, nir_channel(b, v[0], 3), 0);
135 nir_def *n1 = nir_flt_imm(b, nir_channel(b, v[1], 3), 0);
136 nir_def *n2 = nir_flt_imm(b, nir_channel(b, v[2], 3), 0);
137 nir_def *cond = nir_ixor(b, nir_ixor(b, n0, n1), n2);
138 det = nir_bcsel(b, cond, nir_fneg(b, det), det);
139
140 nir_variable *culling_config = nir_variable_create(
141 b->shader, nir_var_uniform, glsl_uint_type(), "culling_config");
142 set_uniform_location(culling_config, culling_config, packed);
143 nir_def *config = nir_i2b(b, nir_load_var(b, culling_config));
144
145 /* det < 0 then z points to camera */
146 nir_def *zero = nir_imm_zero(b, 1, det->bit_size);
147 nir_def *is_zero = nir_feq(b, det, zero);
148 nir_def *is_neg = nir_flt(b, det, zero);
149 nir_def *cull = nir_ixor(b, is_neg, config);
150 return_if_true(b, nir_ior(b, is_zero, cull));
151 }
152
153 static void
fast_frustum_culling(nir_builder * b,nir_def ** v)154 fast_frustum_culling(nir_builder *b, nir_def **v)
155 {
156 nir_def *cull = NULL;
157
158 /* there are six culling planes for the visible volume:
159 * 1. x + w = 0
160 * 2. -x + w = 0
161 * 3. y + w = 0
162 * 4. -y + w = 0
163 * 5. z + w = 0
164 * 6. -z + w = 0
165 *
166 * if all vertices of the primitive are outside (plane equation <0) of
167 * any plane, the primitive must be invisible.
168 */
169 for (int i = 0; i < 6; i++) {
170 nir_def *outside = NULL;
171
172 for (int j = 0; j < b->shader->info.gs.vertices_in; j++) {
173 nir_def *c = nir_channel(b, v[j], i >> 1);
174 if (i & 1)
175 c = nir_fneg(b, c);
176
177 nir_def *r = nir_flt(b, nir_channel(b, v[j], 3), c);
178 outside = j ? nir_iand(b, outside, r) : r;
179 }
180
181 cull = i ? nir_ior(b, cull, outside) : outside;
182 }
183
184 return_if_true(b, cull);
185 }
186
187 static nir_def *
get_intersection(nir_builder * b,nir_def * v1,nir_def * v2,nir_def * d1,nir_def * d2)188 get_intersection(nir_builder *b, nir_def *v1, nir_def *v2,
189 nir_def *d1, nir_def *d2)
190 {
191 nir_def *factor = nir_fdiv(b, d1, nir_fsub(b, d1, d2));
192 return nir_fmad(b, nir_fsub(b, v2, v1), factor, v1);
193 }
194
195 #define begin_for_loop(name, max) \
196 nir_variable *name##_index = \
197 nir_local_variable_create(b->impl, glsl_int_type(), #name "_i"); \
198 nir_store_var(b, name##_index, nir_imm_int(b, 0), 1); \
199 \
200 nir_loop *name = nir_push_loop(b); \
201 { \
202 nir_def *idx = nir_load_var(b, name##_index); \
203 nir_if *if_in_loop = nir_push_if(b, nir_ilt(b, idx, max));
204
205 #define end_for_loop(name) \
206 nir_store_var(b, name##_index, nir_iadd_imm(b, idx, 1), 1); \
207 nir_push_else(b, if_in_loop); \
208 nir_jump(b, nir_jump_break); \
209 nir_pop_if(b, if_in_loop); \
210 } \
211 nir_pop_loop(b, name);
212
213 static void
clip_with_plane(nir_builder * b,nir_variable * vert,nir_variable * num_vert,int max_vert,nir_def * plane)214 clip_with_plane(nir_builder *b, nir_variable *vert, nir_variable *num_vert,
215 int max_vert, nir_def *plane)
216 {
217 nir_variable *all_clipped = nir_local_variable_create(
218 b->impl, glsl_bool_type(), "all_clipped");
219 nir_store_var(b, all_clipped, nir_imm_true(b), 1);
220
221 nir_variable *dist = nir_local_variable_create(
222 b->impl, glsl_array_type(glsl_float_type(), max_vert, 0), "dist");
223
224 nir_def *num = nir_load_var(b, num_vert);
225 begin_for_loop(dist_loop, num)
226 {
227 nir_def *v = nir_load_array_var(b, vert, idx);
228 nir_def *d = nir_fdot(b, v, plane);
229 nir_store_array_var(b, dist, idx, d, 1);
230
231 nir_def *clipped = nir_flt_imm(b, d, 0);
232 nir_store_var(b, all_clipped,
233 nir_iand(b, nir_load_var(b, all_clipped), clipped), 1);
234 }
235 end_for_loop(dist_loop)
236
237 return_if_true(b, nir_load_var(b, all_clipped));
238
239 /* Use +/0/- to denote the dist[i] sign, which means:
240 * +: inside plane
241 * -: outside plane
242 * 0: just on the plane
243 *
244 * Some example:
245 * ++++: all vertex not clipped
246 * ----: all vertex clipped
247 * +-++: one vertex clipped, need to insert two vertex at '-', array grow
248 * +--+: two vertex clipped, need to insert two vertex at '--', array same
249 * +---: three vertex clipped, need to insert two vertex at '---', array trim
250 * +-0+: one vertex clipped, need to insert one vertex at '-', array same
251 *
252 * Plane clip only produce convex polygon, so '-' must be contigous, there's
253 * no '+-+-', so one clip plane can only grow array by 1.
254 */
255
256 /* when array grow or '-' has been replaced with inserted vertex, save the
257 * original vert to be used by following calculation.
258 */
259 nir_variable *saved =
260 nir_local_variable_create(b->impl, glsl_vec4_type(), "saved");
261
262 nir_variable *vert_index =
263 nir_local_variable_create(b->impl, glsl_int_type(), "vert_index");
264 nir_store_var(b, vert_index, nir_imm_int(b, 0), 1);
265
266 begin_for_loop(vert_loop, num)
267 {
268 nir_def *di = nir_load_array_var(b, dist, idx);
269 nir_if *if_clipped = nir_push_if(b, nir_flt_imm(b, di, 0));
270 {
271 /* - case, we need to take care of sign change and insert vertex */
272
273 nir_def *prev = nir_bcsel(b, nir_ieq_imm(b, idx, 0),
274 nir_iadd_imm(b, num, -1),
275 nir_iadd_imm(b, idx, -1));
276 nir_def *dp = nir_load_array_var(b, dist, prev);
277 nir_if *prev_if = nir_push_if(b, nir_fgt_imm(b, dp, 0));
278 {
279 /* +- case, replace - with inserted vertex
280 * assert(vert_index <= idx), array is sure to not grow here
281 * but need to save vert[idx] when vert_index==idx
282 */
283
284 nir_def *vi = nir_load_array_var(b, vert, idx);
285 nir_store_var(b, saved, vi, 0xf);
286
287 nir_def *vp = nir_load_array_var(b, vert, prev);
288 nir_def *iv = get_intersection(b, vp, vi, dp, di);
289 nir_def *index = nir_load_var(b, vert_index);
290 nir_store_array_var(b, vert, index, iv, 0xf);
291
292 nir_store_var(b, vert_index, nir_iadd_imm(b, index, 1), 1);
293 }
294 nir_pop_if(b, prev_if);
295
296 nir_def *next = nir_bcsel(b, nir_ieq(b, idx, nir_iadd_imm(b, num, -1)),
297 nir_imm_int(b, 0), nir_iadd_imm(b, idx, 1));
298 nir_def *dn = nir_load_array_var(b, dist, next);
299 nir_if *next_if = nir_push_if(b, nir_fgt_imm(b, dn, 0));
300 {
301 /* -+ case, may grow array:
302 * vert_index > idx: +-+ case, grow array, current vertex in 'saved',
303 * save next + to 'saved', will replace it with inserted vertex.
304 * vert_index <= idx: --+ case, will replace last - with inserted vertex,
305 * no need to save last -, because + case won't use - value.
306 */
307
308 nir_def *index = nir_load_var(b, vert_index);
309 nir_def *vi = nir_bcsel(b, nir_flt(b, idx, index),
310 nir_load_var(b, saved),
311 nir_load_array_var(b, vert, idx));
312 nir_def *vn = nir_load_array_var(b, vert, next);
313 nir_def *iv = get_intersection(b, vn, vi, dn, di);
314
315 nir_store_var(b, saved, nir_load_array_var(b, vert, index), 0xf);
316 nir_store_array_var(b, vert, index, iv, 0xf);
317
318 nir_store_var(b, vert_index, nir_iadd_imm(b, index, 1), 1);
319 }
320 nir_pop_if(b, next_if);
321 }
322 nir_push_else(b, if_clipped);
323 {
324 /* +/0 case, just keep the vert
325 * vert_index > idx: array grew case, vert[idx] is inserted vertex or prev
326 * +/0 vertex, current vertex is in 'saved', need to save next vertex
327 * vert_index < idx: array trim case
328 */
329
330 nir_def *index = nir_load_var(b, vert_index);
331 nir_def *vi = nir_bcsel(b, nir_flt(b, idx, index),
332 nir_load_var(b, saved),
333 nir_load_array_var(b, vert, idx));
334
335 nir_store_var(b, saved, nir_load_array_var(b, vert, index), 0xf);
336 nir_store_array_var(b, vert, index, vi, 0xf);
337
338 nir_store_var(b, vert_index, nir_iadd_imm(b, index, 1), 1);
339 }
340 nir_pop_if(b, if_clipped);
341 }
342 end_for_loop(vert_loop);
343
344 nir_copy_var(b, num_vert, vert_index);
345 }
346
347 static nir_def *
get_user_clip_plane(nir_builder * b,int index,bool packed)348 get_user_clip_plane(nir_builder *b, int index, bool packed)
349 {
350 char name[16];
351 snprintf(name, sizeof(name), "gl_ClipPlane%d", index);
352 nir_variable *plane = nir_variable_create(
353 b->shader, nir_var_uniform, glsl_vec4_type(), name);
354
355 set_uniform_location(plane, clip_planes[index][0], packed);
356
357 return nir_load_var(b, plane);
358 }
359
360 static void
get_depth_range_transform(nir_builder * b,bool packed,nir_def ** trans)361 get_depth_range_transform(nir_builder *b, bool packed, nir_def **trans)
362 {
363 nir_variable *depth_scale = nir_variable_create(
364 b->shader, nir_var_uniform, glsl_float_type(), "depth_scale");
365 set_uniform_location(depth_scale, depth_scale, packed);
366
367 nir_variable *depth_transport = nir_variable_create(
368 b->shader, nir_var_uniform, glsl_float_type(), "depth_transport");
369 set_uniform_location(depth_transport, depth_transport, packed);
370
371 trans[0] = nir_load_var(b, depth_scale);
372 trans[1] = nir_load_var(b, depth_transport);
373 }
374
375 static nir_def *
get_window_space_depth(nir_builder * b,nir_def * v,nir_def ** trans)376 get_window_space_depth(nir_builder *b, nir_def *v, nir_def **trans)
377 {
378 nir_def *z = nir_channel(b, v, 2);
379 nir_def *w = nir_channel(b, v, 3);
380
381 /* do perspective division, if w==0, xyz must be 0 too (otherwise can't pass
382 * the clip test), 0/0=NaN, but we want it to be the nearest point.
383 */
384 nir_def *c = nir_feq_imm(b, w, 0);
385 nir_def *d = nir_bcsel(b, c, nir_imm_float(b, -1), nir_fdiv(b, z, w));
386
387 /* map [-1, 1] to [near, far] set by glDepthRange(near, far) */
388 return nir_fmad(b, trans[0], d, trans[1]);
389 }
390
391 static void
update_result_buffer(nir_builder * b,nir_def * dmin,nir_def * dmax,bool offset_from_attribute,bool packed)392 update_result_buffer(nir_builder *b, nir_def *dmin, nir_def *dmax,
393 bool offset_from_attribute, bool packed)
394 {
395 nir_def *offset;
396 if (offset_from_attribute) {
397 nir_variable *in_offset = nir_variable_create(
398 b->shader, nir_var_shader_in,
399 glsl_array_type(glsl_uint_type(), b->shader->info.gs.vertices_in, 0),
400 "result_offset");
401 in_offset->data.location = VARYING_SLOT_VAR0;
402 offset = nir_load_array_var_imm(b, in_offset, 0);
403 } else {
404 nir_variable *uni_offset = nir_variable_create(
405 b->shader, nir_var_uniform, glsl_uint_type(), "result_offset");
406 set_uniform_location(uni_offset, result_offset, packed);
407 offset = nir_load_var(b, uni_offset);
408 }
409
410 nir_variable_create(b->shader, nir_var_mem_ssbo,
411 glsl_array_type(glsl_uint_type(), 0, 0), "result");
412 /* driver_location = 0 (slot 0) */
413
414 nir_def *ssbo = nir_imm_int(b, 0);
415 nir_ssbo_atomic(b, 32, ssbo, offset, nir_imm_int(b, 1),
416 .atomic_op = nir_atomic_op_xchg);
417 nir_ssbo_atomic(b, 32, ssbo, nir_iadd_imm(b, offset, 4), dmin,
418 .atomic_op = nir_atomic_op_umin);
419 nir_ssbo_atomic(b, 32, ssbo, nir_iadd_imm(b, offset, 8), dmax,
420 .atomic_op = nir_atomic_op_umax);
421 }
422
423 static void
build_point_nir_shader(nir_builder * b,union state_key state,bool packed)424 build_point_nir_shader(nir_builder *b, union state_key state, bool packed)
425 {
426 assert(b->shader->info.gs.vertices_in == 1);
427
428 nir_def *v;
429 get_input_vertices(b, &v);
430
431 fast_frustum_culling(b, &v);
432
433 nir_def *outside = NULL;
434 for (int i = 0; i < state.num_user_clip_planes; i++) {
435 nir_def *p = get_user_clip_plane(b, i, packed);
436 nir_def *d = nir_fdot(b, v, p);
437 nir_def *r = nir_flt_imm(b, d, 0);
438 outside = i ? nir_ior(b, outside, r) : r;
439 }
440 if (outside)
441 return_if_true(b, outside);
442
443 nir_def *trans[2];
444 get_depth_range_transform(b, packed, trans);
445
446 nir_def *depth = get_window_space_depth(b, v, trans);
447 nir_def *fdepth = nir_fmul_imm(b, depth, 4294967295.0);
448 nir_def *idepth = nir_f2uN(b, fdepth, 32);
449
450 update_result_buffer(b, idepth, idepth, state.result_offset_from_attribute, packed);
451 }
452
453 static nir_variable *
create_clip_planes(nir_builder * b,int num_clip_planes,bool packed)454 create_clip_planes(nir_builder *b, int num_clip_planes, bool packed)
455 {
456 nir_variable *clip_planes = nir_local_variable_create(
457 b->impl, glsl_array_type(glsl_vec4_type(), num_clip_planes, 0), "clip_planes");
458
459 nir_def *unit_clip_planes[6] = {
460 nir_imm_vec4(b, 1, 0, 0, 1),
461 nir_imm_vec4(b, -1, 0, 0, 1),
462 nir_imm_vec4(b, 0, 1, 0, 1),
463 nir_imm_vec4(b, 0, -1, 0, 1),
464 nir_imm_vec4(b, 0, 0, 1, 1),
465 nir_imm_vec4(b, 0, 0, -1, 1),
466 };
467 for (int i = 0; i < 6; i++)
468 nir_store_array_var_imm(b, clip_planes, i, unit_clip_planes[i], 0xf);
469
470 for (int i = 6; i < num_clip_planes; i++) {
471 nir_def *p = get_user_clip_plane(b, i - 6, packed);
472 nir_store_array_var_imm(b, clip_planes, i, p, 0xf);
473 }
474
475 return clip_planes;
476 }
477
478 static void
build_line_nir_shader(nir_builder * b,union state_key state,bool packed)479 build_line_nir_shader(nir_builder *b, union state_key state, bool packed)
480 {
481 assert(b->shader->info.gs.vertices_in == 2);
482
483 nir_def *v[2];
484 get_input_vertices(b, v);
485
486 fast_frustum_culling(b, v);
487
488 nir_variable *vert0 = nir_local_variable_create(b->impl, glsl_vec4_type(), "vert0");
489 nir_store_var(b, vert0, v[0], 0xf);
490
491 nir_variable *vert1 = nir_local_variable_create(b->impl, glsl_vec4_type(), "vert1");
492 nir_store_var(b, vert1, v[1], 0xf);
493
494 const int num_clip_planes = 6 + state.num_user_clip_planes;
495 nir_variable *clip_planes = create_clip_planes(b, num_clip_planes, packed);
496
497 begin_for_loop(clip_loop, nir_imm_int(b, num_clip_planes))
498 {
499 nir_def *plane = nir_load_array_var(b, clip_planes, idx);
500 nir_def *v0 = nir_load_var(b, vert0);
501 nir_def *v1 = nir_load_var(b, vert1);
502 nir_def *d0 = nir_fdot(b, v0, plane);
503 nir_def *d1 = nir_fdot(b, v1, plane);
504 nir_def *n0 = nir_flt_imm(b, d0, 0);
505 nir_def *n1 = nir_flt_imm(b, d1, 0);
506
507 return_if_true(b, nir_iand(b, n0, n1));
508
509 nir_if *clip_if = nir_push_if(b, nir_ior(b, n0, n1));
510 {
511 nir_def *iv = get_intersection(b, v0, v1, d0, d1);
512 nir_store_var(b, vert0, nir_bcsel(b, n0, iv, v0), 0xf);
513 nir_store_var(b, vert1, nir_bcsel(b, n1, iv, v1), 0xf);
514 }
515 nir_pop_if(b, clip_if);
516 }
517 end_for_loop(clip_loop)
518
519 nir_def *trans[2];
520 get_depth_range_transform(b, packed, trans);
521
522 nir_def *d0 = get_window_space_depth(b, nir_load_var(b, vert0), trans);
523 nir_def *d1 = get_window_space_depth(b, nir_load_var(b, vert1), trans);
524
525 nir_def *dmin = nir_fmin(b, d0, d1);
526 nir_def *dmax = nir_fmax(b, d0, d1);
527
528 nir_def *fdmin = nir_fmul_imm(b, dmin, 4294967295.0);
529 nir_def *idmin = nir_f2uN(b, fdmin, 32);
530
531 nir_def *fdmax = nir_fmul_imm(b, dmax, 4294967295.0);
532 nir_def *idmax = nir_f2uN(b, fdmax, 32);
533
534 update_result_buffer(b, idmin, idmax, state.result_offset_from_attribute, packed);
535 }
536
537 static void
build_planar_primitive_nir_shader(nir_builder * b,union state_key state,bool packed)538 build_planar_primitive_nir_shader(nir_builder *b, union state_key state, bool packed)
539 {
540 const int num_in_vert = b->shader->info.gs.vertices_in;
541 assert(num_in_vert == 3 || num_in_vert == 4);
542
543 nir_def *v[4];
544 get_input_vertices(b, v);
545
546 if (state.face_culling_enabled)
547 face_culling(b, v, packed);
548
549 /* fast frustum culling, this should filter out most primitives */
550 fast_frustum_culling(b, v);
551
552 const int num_clip_planes = 6 + state.num_user_clip_planes;
553 const int max_vert = num_in_vert + num_clip_planes;
554
555 /* TODO: could use shared memory (ie. AMD GPU LDS) for this array
556 * to reduce register usage.
557 */
558 nir_variable *vert = nir_local_variable_create(
559 b->impl, glsl_array_type(glsl_vec4_type(), max_vert, 0), "vert");
560 for (int i = 0; i < num_in_vert; i++)
561 nir_store_array_var_imm(b, vert, i, v[i], 0xf);
562
563 nir_variable *num_vert =
564 nir_local_variable_create(b->impl, glsl_int_type(), "num_vert");
565 nir_store_var(b, num_vert, nir_imm_int(b, num_in_vert), 1);
566
567 nir_variable *clip_planes = create_clip_planes(b, num_clip_planes, packed);
568
569 /* accurate clipping with all clip planes */
570 begin_for_loop(clip_loop, nir_imm_int(b, num_clip_planes))
571 {
572 nir_def *plane = nir_load_array_var(b, clip_planes, idx);
573 clip_with_plane(b, vert, num_vert, max_vert, plane);
574 }
575 end_for_loop(clip_loop)
576
577 nir_def *trans[2];
578 get_depth_range_transform(b, packed, trans);
579
580 nir_variable *dmin =
581 nir_local_variable_create(b->impl, glsl_float_type(), "dmin");
582 nir_store_var(b, dmin, nir_imm_float(b, 1), 1);
583
584 nir_variable *dmax =
585 nir_local_variable_create(b->impl, glsl_float_type(), "dmax");
586 nir_store_var(b, dmax, nir_imm_float(b, 0), 1);
587
588 begin_for_loop(depth_loop, nir_load_var(b, num_vert))
589 {
590 nir_def *vtx = nir_load_array_var(b, vert, idx);
591 nir_def *depth = get_window_space_depth(b, vtx, trans);
592 nir_store_var(b, dmin, nir_fmin(b, nir_load_var(b, dmin), depth), 1);
593 nir_store_var(b, dmax, nir_fmax(b, nir_load_var(b, dmax), depth), 1);
594 }
595 end_for_loop(depth_loop)
596
597 nir_def *fdmin = nir_fmul_imm(b, nir_load_var(b, dmin), 4294967295.0);
598 nir_def *idmin = nir_f2uN(b, fdmin, 32);
599
600 nir_def *fdmax = nir_fmul_imm(b, nir_load_var(b, dmax), 4294967295.0);
601 nir_def *idmax = nir_f2uN(b, fdmax, 32);
602
603 update_result_buffer(b, idmin, idmax, state.result_offset_from_attribute, packed);
604 }
605
606 static void *
hw_select_create_gs(struct st_context * st,union state_key state)607 hw_select_create_gs(struct st_context *st, union state_key state)
608 {
609 const nir_shader_compiler_options *options =
610 st_get_nir_compiler_options(st, MESA_SHADER_GEOMETRY);
611
612 nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_GEOMETRY, options,
613 "hw select GS");
614
615 nir_shader *nir = b.shader;
616 nir->info.inputs_read = VARYING_BIT_POS;
617 nir->num_uniforms = DIV_ROUND_UP(sizeof(struct geometry_constant), (4 * sizeof(float)));
618 nir->info.num_ssbos = 1;
619 nir->info.gs.output_primitive = MESA_PRIM_POINTS;
620 nir->info.gs.vertices_out = 1;
621 nir->info.gs.invocations = 1;
622 nir->info.gs.active_stream_mask = 1;
623
624 if (state.result_offset_from_attribute)
625 nir->info.inputs_read |= VARYING_BIT_VAR(0);
626
627 bool packed = st->ctx->Const.PackedDriverUniformStorage;
628
629 switch (state.primitive) {
630 case HW_SELECT_PRIM_POINTS:
631 nir->info.gs.input_primitive = MESA_PRIM_POINTS;
632 nir->info.gs.vertices_in = 1;
633 build_point_nir_shader(&b, state, packed);
634 break;
635 case HW_SELECT_PRIM_LINES:
636 nir->info.gs.input_primitive = MESA_PRIM_LINES;
637 nir->info.gs.vertices_in = 2;
638 build_line_nir_shader(&b, state, packed);
639 break;
640 case HW_SELECT_PRIM_TRIANGLES:
641 nir->info.gs.input_primitive = MESA_PRIM_TRIANGLES;
642 nir->info.gs.vertices_in = 3;
643 build_planar_primitive_nir_shader(&b, state, packed);
644 break;
645 case HW_SELECT_PRIM_QUADS:
646 /* geometry shader has no quad primitive, use lines_adjacency instead */
647 nir->info.gs.input_primitive = MESA_PRIM_LINES_ADJACENCY;
648 nir->info.gs.vertices_in = 4;
649 build_planar_primitive_nir_shader(&b, state, packed);
650 break;
651 default:
652 unreachable("unexpected primitive");
653 }
654
655 nir_lower_returns(nir);
656
657 return st_nir_finish_builtin_shader(st, nir);
658 }
659
660 bool
st_draw_hw_select_prepare_common(struct gl_context * ctx)661 st_draw_hw_select_prepare_common(struct gl_context *ctx)
662 {
663 struct st_context *st = st_context(ctx);
664 if (ctx->GeometryProgram._Current ||
665 ctx->TessCtrlProgram._Current ||
666 ctx->TessEvalProgram._Current) {
667 fprintf(stderr, "HW GL_SELECT does not support user geometry/tessellation shader\n");
668 return false;
669 }
670
671 struct geometry_constant consts;
672
673 float n = ctx->ViewportArray[0].Near;
674 float f = ctx->ViewportArray[0].Far;
675 consts.depth_scale = (f - n) / 2;
676 consts.depth_transport = (f + n) / 2;
677
678 /* this field is not used when face culling disabled */
679 consts.culling_config =
680 (ctx->Polygon.CullFaceMode == GL_BACK) ^
681 (ctx->Polygon.FrontFace == GL_CCW);
682
683 /* this field is not used when passing result offset by attribute */
684 consts.result_offset = st->ctx->Select.ResultOffset;
685
686 int num_planes = 0;
687 u_foreach_bit(i, ctx->Transform.ClipPlanesEnabled) {
688 COPY_4V(consts.clip_planes[num_planes], ctx->Transform._ClipUserPlane[i]);
689 num_planes++;
690 }
691
692 struct pipe_constant_buffer cb;
693 cb.buffer = NULL;
694 cb.user_buffer = &consts;
695 cb.buffer_offset = 0;
696 cb.buffer_size = sizeof(consts) - (MAX_CLIP_PLANES - num_planes) * 4 * sizeof(float);
697
698 struct pipe_context *pipe = st->pipe;
699 pipe->set_constant_buffer(pipe, PIPE_SHADER_GEOMETRY, 0, false, &cb);
700
701 struct pipe_shader_buffer buffer;
702 memset(&buffer, 0, sizeof(buffer));
703 buffer.buffer = ctx->Select.Result->buffer;
704 buffer.buffer_size = MAX_NAME_STACK_RESULT_NUM * 3 * sizeof(int);
705
706 pipe->set_shader_buffers(pipe, PIPE_SHADER_GEOMETRY, 0, 1, &buffer, 0x1);
707
708 return true;
709 }
710
711 static union state_key
make_state_key(struct gl_context * ctx,int mode)712 make_state_key(struct gl_context *ctx, int mode)
713 {
714 union state_key state = {0};
715
716 switch (mode) {
717 case GL_POINTS:
718 state.primitive = HW_SELECT_PRIM_POINTS;
719 break;
720 case GL_LINES:
721 case GL_LINE_STRIP:
722 case GL_LINE_LOOP:
723 state.primitive = HW_SELECT_PRIM_LINES;
724 break;
725 case GL_QUADS:
726 state.primitive = HW_SELECT_PRIM_QUADS;
727 break;
728 case GL_TRIANGLES:
729 case GL_TRIANGLE_STRIP:
730 case GL_TRIANGLE_FAN:
731 /* These will be broken into triangles. */
732 case GL_QUAD_STRIP:
733 case GL_POLYGON:
734 state.primitive = HW_SELECT_PRIM_TRIANGLES;
735 break;
736 default:
737 fprintf(stderr, "HW GL_SELECT does not support draw mode %s\n",
738 _mesa_enum_to_string(mode));
739 return (union state_key){0};
740 }
741
742 /* TODO: support gl_ClipDistance/gl_CullDistance, but it costs more regs */
743 struct gl_program *vp = ctx->VertexProgram._Current;
744 if (vp->info.clip_distance_array_size || vp->info.cull_distance_array_size) {
745 fprintf(stderr, "HW GL_SELECT does not support gl_ClipDistance/gl_CullDistance\n");
746 return (union state_key){0};
747 }
748
749 state.num_user_clip_planes = util_bitcount(ctx->Transform.ClipPlanesEnabled);
750
751 /* face culling only apply to 2D primitives */
752 if (state.primitive == HW_SELECT_PRIM_QUADS ||
753 state.primitive == HW_SELECT_PRIM_TRIANGLES)
754 state.face_culling_enabled = ctx->Polygon.CullFlag;
755
756 state.result_offset_from_attribute =
757 ctx->VertexProgram._VPMode == VP_MODE_FF &&
758 (ctx->VertexProgram._VaryingInputs & VERT_BIT_SELECT_RESULT_OFFSET);
759
760 return state;
761 }
762
763 bool
st_draw_hw_select_prepare_mode(struct gl_context * ctx,struct pipe_draw_info * info)764 st_draw_hw_select_prepare_mode(struct gl_context *ctx, struct pipe_draw_info *info)
765 {
766 union state_key key = make_state_key(ctx, info->mode);
767 if (!key.u32)
768 return false;
769
770 struct st_context *st = st_context(ctx);
771 if (!st->hw_select_shaders)
772 st->hw_select_shaders = _mesa_hash_table_create_u32_keys(NULL);
773
774 struct hash_entry *he = _mesa_hash_table_search(st->hw_select_shaders,
775 (void*)(uintptr_t)key.u32);
776 void *gs;
777 if (!he) {
778 gs = hw_select_create_gs(st, key);
779 if (!gs)
780 return false;
781
782 _mesa_hash_table_insert(st->hw_select_shaders, (void*)(uintptr_t)key.u32, gs);
783 } else
784 gs = he->data;
785
786 struct cso_context *cso = st->cso_context;
787 cso_set_geometry_shader_handle(cso, gs);
788
789 /* Replace draw mode with equivalent one which geometry shader support.
790 *
791 * New mode consume same vertex buffer structure and produce primitive with
792 * same vertices (no need to be same type of primitive, because geometry shader
793 * operate on vertives and emit nothing).
794 *
795 * We can break QUAD and POLYGON to triangles with same shape. But we can't futher
796 * break them into single line or point because new primitive need to contain >=3
797 * vertices so that it's still handled in 2D (planar) way instead of 1D (line) or
798 * 0D (point) way which have different algorithm.
799 */
800 switch (info->mode) {
801 case GL_QUADS:
802 info->mode = GL_LINES_ADJACENCY;
803 break;
804 case GL_QUAD_STRIP:
805 info->mode = GL_TRIANGLE_STRIP;
806 break;
807 case GL_POLYGON:
808 info->mode = GL_TRIANGLE_FAN;
809 break;
810 default:
811 break;
812 }
813
814 /* Only normal glBegin/End draws pass result offset by attribute to avoid flush
815 * vertices when change name stack, so multiple glBegin/End sections before/after
816 * name stack calls can be merged to a single draw call. To achieve this We mark
817 * name stack result buffer used in glEnd instead of the last draw call.
818 *
819 * Other case like glDrawArrays and display list replay won't merge draws cross
820 * name stack calls, so we just mark name stack result buffer used here.
821 */
822 if (!key.result_offset_from_attribute)
823 ctx->Select.ResultUsed = GL_TRUE;
824
825 return true;
826 }
827