xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/nir/nir_draw_helpers.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2019 Red Hat.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  **************************************************************************/
25 
26 /*
27  * NIR lowering passes to handle the draw stages for
28  * - pstipple
29  * - aaline
30  * - aapoint.
31  *
32  * These are all ported from the equivalent TGSI transforms.
33  */
34 
35 #include "nir.h"
36 #include "tgsi/tgsi_from_mesa.h"
37 #include "nir_builder.h"
38 
39 #include "nir_draw_helpers.h"
40 
41 typedef struct {
42    nir_builder b;
43    nir_shader *shader;
44    bool fs_pos_is_sysval;
45    nir_variable *stip_tex;
46    nir_def *fragcoord;
47    nir_alu_type bool_type;
48 } lower_pstipple;
49 
50 static nir_def *
load_frag_coord(nir_builder * b)51 load_frag_coord(nir_builder *b)
52 {
53    nir_variable *pos = nir_get_variable_with_location(b->shader, nir_var_shader_in,
54                                                       VARYING_SLOT_POS, glsl_vec4_type());
55    pos->data.interpolation = INTERP_MODE_NOPERSPECTIVE;
56    return nir_load_var(b, pos);
57 }
58 
59 static void
nir_lower_pstipple_block(nir_block * block,lower_pstipple * state)60 nir_lower_pstipple_block(nir_block *block,
61                          lower_pstipple *state)
62 {
63    nir_builder *b = &state->b;
64    nir_def *texcoord;
65 
66    b->cursor = nir_before_block(block);
67 
68    nir_def *frag_coord = state->fs_pos_is_sysval ? nir_load_frag_coord(b) : load_frag_coord(b);
69 
70    texcoord = nir_fmul(b, nir_trim_vector(b, frag_coord, 2),
71                        nir_imm_vec2(b, 1.0/32.0, 1.0/32.0));
72 
73    nir_tex_instr *tex = nir_tex_instr_create(b->shader, 1);
74    tex->op = nir_texop_tex;
75    tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
76    tex->coord_components = 2;
77    tex->dest_type = nir_type_float32;
78    tex->texture_index = state->stip_tex->data.binding;
79    tex->sampler_index = state->stip_tex->data.binding;
80    tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_coord, texcoord);
81    nir_def_init(&tex->instr, &tex->def, 4, 32);
82 
83    nir_builder_instr_insert(b, &tex->instr);
84 
85    nir_def *condition;
86 
87    switch (state->bool_type) {
88    case nir_type_bool1:
89       condition = nir_fneu_imm(b, nir_channel(b, &tex->def, 3), 0.0);
90       break;
91    case nir_type_bool32:
92       condition = nir_fneu32(b, nir_channel(b, &tex->def, 3),
93                              nir_imm_floatN_t(b, 0.0, tex->def.bit_size));
94       break;
95    default:
96       unreachable("Invalid Boolean type.");
97    }
98 
99    nir_discard_if(b, condition);
100    b->shader->info.fs.uses_discard = true;
101 }
102 
103 static void
nir_lower_pstipple_impl(nir_function_impl * impl,lower_pstipple * state)104 nir_lower_pstipple_impl(nir_function_impl *impl,
105                         lower_pstipple *state)
106 {
107    state->b = nir_builder_create(impl);
108 
109    nir_block *start = nir_start_block(impl);
110    nir_lower_pstipple_block(start, state);
111 }
112 
113 void
nir_lower_pstipple_fs(struct nir_shader * shader,unsigned * samplerUnitOut,unsigned fixedUnit,bool fs_pos_is_sysval,nir_alu_type bool_type)114 nir_lower_pstipple_fs(struct nir_shader *shader,
115                       unsigned *samplerUnitOut,
116                       unsigned fixedUnit,
117                       bool fs_pos_is_sysval,
118                       nir_alu_type bool_type)
119 {
120    lower_pstipple state = {
121       .shader = shader,
122       .fs_pos_is_sysval = fs_pos_is_sysval,
123       .bool_type = bool_type,
124    };
125 
126    assert(bool_type == nir_type_bool1 ||
127           bool_type == nir_type_bool32);
128 
129    if (shader->info.stage != MESA_SHADER_FRAGMENT)
130       return;
131 
132    int binding = 0;
133    nir_foreach_uniform_variable(var, shader) {
134       if (glsl_type_is_sampler(var->type)) {
135          if (var->data.binding >= binding)
136             binding = var->data.binding + 1;
137       }
138    }
139    const struct glsl_type *sampler2D =
140       glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT);
141 
142    nir_variable *tex_var = nir_variable_create(shader, nir_var_uniform, sampler2D, "stipple_tex");
143    tex_var->data.binding = binding;
144    tex_var->data.explicit_binding = true;
145    tex_var->data.how_declared = nir_var_hidden;
146 
147    BITSET_SET(shader->info.textures_used, binding);
148    BITSET_SET(shader->info.samplers_used, binding);
149    state.stip_tex = tex_var;
150 
151    nir_foreach_function_impl(impl, shader) {
152       nir_lower_pstipple_impl(impl, &state);
153    }
154    *samplerUnitOut = binding;
155 }
156 
157 typedef struct {
158    nir_variable *line_width_input;
159    nir_variable *stipple_counter;
160    nir_variable *stipple_pattern;
161 } lower_aaline;
162 
163 static bool
lower_aaline_instr(nir_builder * b,nir_instr * instr,void * data)164 lower_aaline_instr(nir_builder *b, nir_instr *instr, void *data)
165 {
166    lower_aaline *state = data;
167 
168    if (instr->type != nir_instr_type_intrinsic)
169       return false;
170 
171    nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
172    if (intrin->intrinsic != nir_intrinsic_store_deref)
173       return false;
174 
175    nir_variable *var = nir_intrinsic_get_var(intrin, 0);
176    if (var->data.mode != nir_var_shader_out)
177       return false;
178    if (var->data.location < FRAG_RESULT_DATA0 && var->data.location != FRAG_RESULT_COLOR)
179       return false;
180    uint32_t mask = nir_intrinsic_write_mask(intrin) << var->data.location_frac;
181    if (!(mask & BITFIELD_BIT(3)))
182       return false;
183 
184    nir_def *out_input = intrin->src[1].ssa;
185    b->cursor = nir_before_instr(instr);
186    nir_def *lw = nir_load_var(b, state->line_width_input);
187    nir_def *len = nir_channel(b, lw, 3);
188    len = nir_fadd_imm(b, nir_fmul_imm(b, len, 2.0), -1.0);
189    nir_def *tmp = nir_fsat(b, nir_fadd(b, nir_channels(b, lw, 0xa),
190                                              nir_fneg(b, nir_fabs(b, nir_channels(b, lw, 0x5)))));
191 
192    nir_def *max = len;
193    if (state->stipple_counter) {
194       assert(state->stipple_pattern);
195 
196       nir_def *counter = nir_load_var(b, state->stipple_counter);
197       nir_def *pattern = nir_load_var(b, state->stipple_pattern);
198       nir_def *factor = nir_i2f32(b, nir_ishr_imm(b, pattern, 16));
199       pattern = nir_iand_imm(b, pattern, 0xffff);
200 
201       nir_def *stipple_pos = nir_vec2(b, nir_fadd_imm(b, counter, -0.5),
202                                              nir_fadd_imm(b, counter, 0.5));
203 
204       stipple_pos = nir_frem(b, nir_fdiv(b, stipple_pos, factor),
205                                  nir_imm_float(b, 16.0));
206 
207       nir_def *p = nir_f2i32(b, stipple_pos);
208       nir_def *one = nir_imm_float(b, 1.0);
209 
210       // float t = 1.0 - min((1.0 - fract(stipple_pos.x)) * factor, 1.0);
211       nir_def *t = nir_ffract(b, nir_channel(b, stipple_pos, 0));
212       t = nir_fsub(b, one,
213                      nir_fmin(b, nir_fmul(b, factor,
214                                           nir_fsub(b, one, t)), one));
215 
216       // vec2 a = vec2((uvec2(pattern) >> p) & uvec2(1u));
217       nir_def *a = nir_i2f32(b,
218          nir_iand(b, nir_ishr(b, nir_replicate(b, pattern, 2), p),
219                   nir_imm_ivec2(b, 1, 1)));
220 
221       // float cov = mix(a.x, a.y, t);
222       nir_def *cov = nir_flrp(b, nir_channel(b, a, 0), nir_channel(b, a, 1), t);
223 
224       max = nir_fmin(b, len, cov);
225    }
226 
227    tmp = nir_fmul(b, nir_channel(b, tmp, 0),
228                   nir_fmin(b, nir_channel(b, tmp, 1), max));
229    tmp = nir_fmul(b, nir_channel(b, out_input, out_input->num_components - 1), tmp);
230 
231    nir_def *out = nir_vector_insert_imm(b, out_input, tmp,
232                                         out_input->num_components - 1);
233    nir_src_rewrite(&intrin->src[1], out);
234    return true;
235 }
236 
237 void
nir_lower_aaline_fs(struct nir_shader * shader,int * varying,nir_variable * stipple_counter,nir_variable * stipple_pattern)238 nir_lower_aaline_fs(struct nir_shader *shader, int *varying,
239                     nir_variable *stipple_counter,
240                     nir_variable *stipple_pattern)
241 {
242    lower_aaline state = {
243       .stipple_counter = stipple_counter,
244       .stipple_pattern = stipple_pattern,
245    };
246    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
247 
248    int highest_location = -1, highest_drv_location = -1;
249    nir_foreach_shader_in_variable(var, shader) {
250      if ((int)var->data.location > highest_location)
251          highest_location = var->data.location;
252      if ((int)var->data.driver_location > highest_drv_location)
253          highest_drv_location = var->data.driver_location;
254    }
255 
256    nir_variable *line_width = nir_variable_create(shader, nir_var_shader_in,
257                                                   glsl_vec4_type(), "aaline");
258    if (highest_location == -1 || highest_location < VARYING_SLOT_VAR0) {
259      line_width->data.location = VARYING_SLOT_VAR0;
260      line_width->data.driver_location = highest_drv_location + 1;
261    } else {
262      line_width->data.location = highest_location + 1;
263      line_width->data.driver_location = highest_drv_location + 1;
264    }
265    shader->num_inputs++;
266    *varying = tgsi_get_generic_gl_varying_index(line_width->data.location, true);
267    state.line_width_input = line_width;
268 
269    nir_shader_instructions_pass(shader, lower_aaline_instr,
270                                 nir_metadata_dominance, &state);
271 }
272 
273 typedef struct {
274    nir_builder b;
275    nir_shader *shader;
276    nir_variable *input;
277 } lower_aapoint;
278 
279 static void
nir_lower_aapoint_block(nir_block * block,lower_aapoint * state,nir_def * sel)280 nir_lower_aapoint_block(nir_block *block,
281                         lower_aapoint *state, nir_def *sel)
282 {
283   nir_builder *b = &state->b;
284   nir_foreach_instr(instr, block) {
285       if (instr->type != nir_instr_type_intrinsic)
286          continue;
287 
288       nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
289       if (intrin->intrinsic != nir_intrinsic_store_deref)
290          continue;
291 
292       nir_variable *var = nir_intrinsic_get_var(intrin, 0);
293       if (var->data.mode != nir_var_shader_out)
294          continue;
295       if (var->data.location < FRAG_RESULT_DATA0 && var->data.location != FRAG_RESULT_COLOR)
296          continue;
297 
298       nir_def *out_input = intrin->src[1].ssa;
299       b->cursor = nir_before_instr(instr);
300 
301       nir_def *tmp = nir_fmul(b, nir_channel(b, out_input, 3), sel);
302       nir_def *out = nir_vec4(b, nir_channel(b, out_input, 0),
303                                   nir_channel(b, out_input, 1),
304                                   nir_channel(b, out_input, 2),
305                                   tmp);
306       nir_src_rewrite(&intrin->src[1], out);
307    }
308 
309 }
310 
311 static void
nir_lower_aapoint_impl(nir_function_impl * impl,lower_aapoint * state,nir_alu_type bool_type)312 nir_lower_aapoint_impl(nir_function_impl *impl, lower_aapoint *state,
313                        nir_alu_type bool_type)
314 {
315    nir_block *block = nir_start_block(impl);
316    state->b = nir_builder_at(nir_before_block(block));
317 
318    nir_builder *b = &state->b;
319    nir_def *aainput = nir_load_var(b, state->input);
320 
321    nir_def *dist = nir_fadd(b, nir_fmul(b, nir_channel(b, aainput, 0), nir_channel(b, aainput, 0)),
322                                 nir_fmul(b, nir_channel(b, aainput, 1), nir_channel(b, aainput, 1)));
323 
324    nir_def *k = nir_channel(b, aainput, 2);
325    nir_def *chan_val_one = nir_channel(b, aainput, 3);
326    nir_def *comp;
327 
328    switch (bool_type) {
329    case nir_type_bool1:
330       comp = nir_flt(b, chan_val_one, dist);
331       break;
332    case nir_type_bool32:
333       comp = nir_flt32(b, chan_val_one, dist);
334       break;
335    case nir_type_float32:
336       comp = nir_slt(b, chan_val_one, dist);
337       break;
338    default:
339       unreachable("Invalid Boolean type.");
340    }
341 
342    nir_discard_if(b, comp);
343    b->shader->info.fs.uses_discard = true;
344 
345    /* compute coverage factor = (1-d)/(1-k) */
346    /* 1 - k */
347    nir_def *tmp = nir_fadd(b, chan_val_one, nir_fneg(b, k));
348    /* 1.0 / (1 - k) */
349    tmp = nir_frcp(b, tmp);
350 
351    /* 1 - d */
352    nir_def *tmp2 = nir_fadd(b, chan_val_one, nir_fneg(b, dist));
353 
354    /* (1 - d) / (1 - k) */
355    nir_def *coverage = nir_fmul(b, tmp, tmp2);
356 
357    /* if (k >= distance)
358     *    sel = coverage;
359     * else
360     *    sel = 1.0;
361     */
362    nir_def *sel;
363 
364    switch (bool_type) {
365    case nir_type_bool1:
366       sel = nir_b32csel(b, nir_fge(b, k, dist), coverage, chan_val_one);
367       break;
368    case nir_type_bool32:
369       sel = nir_b32csel(b, nir_fge32(b, k, dist), coverage, chan_val_one);
370       break;
371    case nir_type_float32: {
372       /* On this path, don't assume that any "fancy" instructions are
373        * supported, but also try to emit something decent.
374        *
375        *    sel = (k >= distance) ? coverage : 1.0;
376        *    sel = (k >= distance) * coverage : (1 - (k >= distance)) * 1.0
377        *    sel = (k >= distance) * coverage : (1 - (k >= distance))
378        *
379        * Since (k >= distance) * coverage is zero when (1 - (k >= distance))
380        * is not zero,
381        *
382        *    sel = (k >= distance) * coverage + (1 - (k >= distance))
383        *
384        * If we assume that coverage == fsat(coverage), this could be further
385        * optimized to fsat(coverage + (1 - (k >= distance))), but I don't feel
386        * like verifying that right now.
387        */
388       nir_def *cmp_result = nir_sge(b, k, dist);
389       sel = nir_fadd(b,
390                      nir_fmul(b, coverage, cmp_result),
391                      nir_fadd(b, chan_val_one, nir_fneg(b, cmp_result)));
392       break;
393    }
394    default:
395       unreachable("Invalid Boolean type.");
396    }
397 
398    nir_foreach_block(block, impl) {
399      nir_lower_aapoint_block(block, state, sel);
400    }
401 }
402 
403 void
nir_lower_aapoint_fs(struct nir_shader * shader,int * varying,const nir_alu_type bool_type)404 nir_lower_aapoint_fs(struct nir_shader *shader, int *varying, const nir_alu_type bool_type)
405 {
406    assert(bool_type == nir_type_bool1 ||
407           bool_type == nir_type_bool32 ||
408           bool_type == nir_type_float32);
409 
410    lower_aapoint state = {
411       .shader = shader,
412    };
413    if (shader->info.stage != MESA_SHADER_FRAGMENT)
414       return;
415 
416    int highest_location = -1, highest_drv_location = -1;
417    nir_foreach_shader_in_variable(var, shader) {
418      if ((int)var->data.location > highest_location)
419          highest_location = var->data.location;
420      if ((int)var->data.driver_location > highest_drv_location)
421          highest_drv_location = var->data.driver_location;
422    }
423 
424    nir_variable *aapoint_input = nir_variable_create(shader, nir_var_shader_in,
425                                                      glsl_vec4_type(), "aapoint");
426    if (highest_location == -1 || highest_location < VARYING_SLOT_VAR0) {
427      aapoint_input->data.location = VARYING_SLOT_VAR0;
428    } else {
429      aapoint_input->data.location = highest_location + 1;
430    }
431    aapoint_input->data.driver_location = highest_drv_location + 1;
432 
433    shader->num_inputs++;
434    *varying = tgsi_get_generic_gl_varying_index(aapoint_input->data.location, true);
435    state.input = aapoint_input;
436 
437    nir_foreach_function_impl(impl, shader) {
438       nir_lower_aapoint_impl(impl, &state, bool_type);
439    }
440 }
441