• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ssa_def *fragcoord;
47 } lower_pstipple;
48 
49 static nir_ssa_def *
load_frag_coord(nir_builder * b)50 load_frag_coord(nir_builder *b)
51 {
52    nir_foreach_shader_in_variable(var, b->shader) {
53       if (var->data.location == VARYING_SLOT_POS)
54          return nir_load_var(b, var);
55    }
56 
57    nir_variable *pos = nir_variable_create(b->shader, nir_var_shader_in,
58                                            glsl_vec4_type(), NULL);
59    pos->data.location = VARYING_SLOT_POS;
60    pos->data.interpolation = INTERP_MODE_NOPERSPECTIVE;
61    pos->data.driver_location = b->shader->num_inputs++;
62    return nir_load_var(b, pos);
63 }
64 
65 static void
nir_lower_pstipple_block(nir_block * block,lower_pstipple * state)66 nir_lower_pstipple_block(nir_block *block,
67                          lower_pstipple *state)
68 {
69    nir_builder *b = &state->b;
70    nir_ssa_def *texcoord;
71 
72    b->cursor = nir_before_block(block);
73 
74    nir_ssa_def *frag_coord = state->fs_pos_is_sysval ? nir_load_frag_coord(b) : load_frag_coord(b);
75 
76    texcoord = nir_fmul(b, nir_channels(b, frag_coord, 0x3),
77                        nir_imm_vec2(b, 1.0/32.0, 1.0/32.0));
78 
79    nir_tex_instr *tex = nir_tex_instr_create(b->shader, 1);
80    tex->op = nir_texop_tex;
81    tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
82    tex->coord_components = 2;
83    tex->dest_type = nir_type_float;
84    tex->texture_index = state->stip_tex->data.binding;
85    tex->sampler_index = state->stip_tex->data.binding;
86    tex->src[0].src_type = nir_tex_src_coord;
87    tex->src[0].src = nir_src_for_ssa(texcoord);
88    nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
89 
90    nir_builder_instr_insert(b, &tex->instr);
91 
92    nir_ssa_def *condition = nir_f2b32(b, nir_channel(b, &tex->dest.ssa, 3));
93    nir_intrinsic_instr *discard = nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard_if);
94    discard->src[0] = nir_src_for_ssa(condition);
95    nir_builder_instr_insert(b, &discard->instr);
96    b->shader->info.fs.uses_discard = true;
97 }
98 
99 static void
nir_lower_pstipple_impl(nir_function_impl * impl,lower_pstipple * state)100 nir_lower_pstipple_impl(nir_function_impl *impl,
101                         lower_pstipple *state)
102 {
103    nir_builder *b = &state->b;
104 
105    nir_builder_init(b, impl);
106 
107    nir_block *start = nir_start_block(impl);
108    nir_lower_pstipple_block(start, state);
109 }
110 
111 void
nir_lower_pstipple_fs(struct nir_shader * shader,unsigned * samplerUnitOut,unsigned fixedUnit,bool fs_pos_is_sysval)112 nir_lower_pstipple_fs(struct nir_shader *shader,
113                       unsigned *samplerUnitOut,
114                       unsigned fixedUnit,
115                       bool fs_pos_is_sysval)
116 {
117    lower_pstipple state = {
118       .shader = shader,
119       .fs_pos_is_sysval = fs_pos_is_sysval,
120    };
121    if (shader->info.stage != MESA_SHADER_FRAGMENT)
122       return;
123 
124    int binding = 0;
125    nir_foreach_uniform_variable(var, shader) {
126       if (glsl_type_is_sampler(var->type)) {
127          if (var->data.binding >= binding)
128             binding = var->data.binding + 1;
129       }
130    }
131    const struct glsl_type *sampler2D =
132       glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT);
133 
134    nir_variable *tex_var = nir_variable_create(shader, nir_var_uniform, sampler2D, "stipple_tex");
135    tex_var->data.binding = binding;
136    tex_var->data.explicit_binding = true;
137    tex_var->data.how_declared = nir_var_hidden;
138 
139    shader->info.textures_used |= (1 << binding);
140    state.stip_tex = tex_var;
141 
142    nir_foreach_function(function, shader) {
143       if (function->impl) {
144          nir_lower_pstipple_impl(function->impl, &state);
145       }
146    }
147    *samplerUnitOut = binding;
148 }
149 
150 typedef struct {
151    nir_builder b;
152    nir_shader *shader;
153    nir_variable *line_width_input;
154 } lower_aaline;
155 
156 static void
nir_lower_aaline_block(nir_block * block,lower_aaline * state)157 nir_lower_aaline_block(nir_block *block,
158                        lower_aaline *state)
159 {
160   nir_builder *b = &state->b;
161   nir_foreach_instr(instr, block) {
162       if (instr->type != nir_instr_type_intrinsic)
163          continue;
164 
165       nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
166       if (intrin->intrinsic != nir_intrinsic_store_deref)
167          continue;
168 
169       nir_variable *var = nir_intrinsic_get_var(intrin, 0);
170       if (var->data.mode != nir_var_shader_out)
171          continue;
172       if (var->data.location != FRAG_RESULT_COLOR)
173          continue;
174 
175       nir_ssa_def *out_input = intrin->src[1].ssa;
176       b->cursor = nir_before_instr(instr);
177       nir_ssa_def *lw = nir_load_var(b, state->line_width_input);
178       nir_ssa_def *tmp = nir_fsat(b, nir_fadd(b, nir_channel(b, lw, 1),
179                                               nir_fneg(b, nir_fabs(b, nir_channel(b, lw, 0)))));
180       nir_ssa_def *tmp1 = nir_fsat(b, nir_fadd(b, nir_channel(b, lw, 3),
181                                                nir_fneg(b, nir_fabs(b, nir_channel(b, lw, 2)))));
182 
183       tmp = nir_fmul(b, tmp, tmp1);
184       tmp = nir_fmul(b, nir_channel(b, out_input, 3), tmp);
185 
186       nir_ssa_def *out = nir_vec4(b, nir_channel(b, out_input, 0),
187                                   nir_channel(b, out_input, 1),
188                                   nir_channel(b, out_input, 2),
189                                   tmp);
190       nir_instr_rewrite_src(instr, &intrin->src[1], nir_src_for_ssa(out));
191    }
192 
193 }
194 
195 static void
nir_lower_aaline_impl(nir_function_impl * impl,lower_aaline * state)196 nir_lower_aaline_impl(nir_function_impl *impl,
197                       lower_aaline *state)
198 {
199    nir_builder *b = &state->b;
200 
201    nir_builder_init(b, impl);
202 
203    nir_foreach_block(block, impl) {
204       nir_lower_aaline_block(block, state);
205    }
206 }
207 
208 void
nir_lower_aaline_fs(struct nir_shader * shader,int * varying)209 nir_lower_aaline_fs(struct nir_shader *shader, int *varying)
210 {
211    lower_aaline state = {
212       .shader = shader,
213    };
214    if (shader->info.stage != MESA_SHADER_FRAGMENT)
215       return;
216 
217    int highest_location = -1, highest_drv_location = -1;
218    nir_foreach_shader_in_variable(var, shader) {
219      if ((int)var->data.location > highest_location)
220          highest_location = var->data.location;
221      if ((int)var->data.driver_location > highest_drv_location)
222          highest_drv_location = var->data.driver_location;
223    }
224 
225    nir_variable *line_width = nir_variable_create(shader, nir_var_shader_in,
226                                                   glsl_vec4_type(), "aaline");
227    if (highest_location == -1 || highest_location < VARYING_SLOT_VAR0) {
228      line_width->data.location = VARYING_SLOT_VAR0;
229      line_width->data.driver_location = highest_drv_location + 1;
230    } else {
231      line_width->data.location = highest_location + 1;
232      line_width->data.driver_location = highest_drv_location + 1;
233    }
234    shader->num_inputs++;
235    *varying = tgsi_get_generic_gl_varying_index(line_width->data.location, true);
236    state.line_width_input = line_width;
237 
238    nir_foreach_function(function, shader) {
239       if (function->impl) {
240          nir_lower_aaline_impl(function->impl, &state);
241       }
242    }
243 }
244 
245 typedef struct {
246    nir_builder b;
247    nir_shader *shader;
248    nir_variable *input;
249 } lower_aapoint;
250 
251 static void
nir_lower_aapoint_block(nir_block * block,lower_aapoint * state,nir_ssa_def * sel)252 nir_lower_aapoint_block(nir_block *block,
253                         lower_aapoint *state, nir_ssa_def *sel)
254 {
255   nir_builder *b = &state->b;
256   nir_foreach_instr(instr, block) {
257       if (instr->type != nir_instr_type_intrinsic)
258          continue;
259 
260       nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
261       if (intrin->intrinsic != nir_intrinsic_store_deref)
262          continue;
263 
264       nir_variable *var = nir_intrinsic_get_var(intrin, 0);
265       if (var->data.mode != nir_var_shader_out)
266          continue;
267       if (var->data.location != FRAG_RESULT_COLOR)
268          continue;
269 
270       nir_ssa_def *out_input = intrin->src[1].ssa;
271       b->cursor = nir_before_instr(instr);
272 
273       nir_ssa_def *tmp = nir_fmul(b, nir_channel(b, out_input, 3), sel);
274       nir_ssa_def *out = nir_vec4(b, nir_channel(b, out_input, 0),
275                                   nir_channel(b, out_input, 1),
276                                   nir_channel(b, out_input, 2),
277                                   tmp);
278       nir_instr_rewrite_src(instr, &intrin->src[1], nir_src_for_ssa(out));
279    }
280 
281 }
282 
283 static void
nir_lower_aapoint_impl(nir_function_impl * impl,lower_aapoint * state)284 nir_lower_aapoint_impl(nir_function_impl *impl,
285                       lower_aapoint *state)
286 {
287    nir_builder *b = &state->b;
288 
289    nir_builder_init(b, impl);
290 
291    nir_block *block = nir_start_block(impl);
292    b->cursor = nir_before_block(block);
293 
294    nir_ssa_def *aainput = nir_load_var(b, state->input);
295 
296    nir_ssa_def *dist = nir_fadd(b, nir_fmul(b, nir_channel(b, aainput, 0), nir_channel(b, aainput, 0)),
297                                 nir_fmul(b, nir_channel(b, aainput, 1), nir_channel(b, aainput, 1)));
298 
299    nir_ssa_def *k = nir_channel(b, aainput, 2);
300    nir_ssa_def *chan_val_one = nir_channel(b, aainput, 3);
301    nir_ssa_def *comp = nir_flt32(b, chan_val_one, dist);
302 
303    nir_intrinsic_instr *discard = nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard_if);
304    discard->src[0] = nir_src_for_ssa(comp);
305    nir_builder_instr_insert(b, &discard->instr);
306    b->shader->info.fs.uses_discard = true;
307 
308    /* compute coverage factor = (1-d)/(1-k) */
309    /* 1 - k */
310    nir_ssa_def *tmp = nir_fadd(b, chan_val_one, nir_fneg(b, k));
311    /* 1.0 / (1 - k) */
312    tmp = nir_frcp(b, tmp);
313 
314    /* 1 - d */
315    nir_ssa_def *tmp2 = nir_fadd(b, chan_val_one, nir_fneg(b, dist));
316 
317    /* (1 - d) / (1 - k) */
318    nir_ssa_def *coverage = nir_fmul(b, tmp, tmp2);
319 
320    /* if (k >= distance)
321     *    sel = coverage;
322     * else
323     *    sel = 1.0;
324     */
325    nir_ssa_def *sel = nir_b32csel(b, nir_fge32(b, k, dist), coverage, chan_val_one);
326 
327    nir_foreach_block(block, impl) {
328      nir_lower_aapoint_block(block, state, sel);
329    }
330 }
331 
332 void
nir_lower_aapoint_fs(struct nir_shader * shader,int * varying)333 nir_lower_aapoint_fs(struct nir_shader *shader, int *varying)
334 {
335    lower_aapoint state = {
336       .shader = shader,
337    };
338    if (shader->info.stage != MESA_SHADER_FRAGMENT)
339       return;
340 
341    int highest_location = -1, highest_drv_location = -1;
342    nir_foreach_shader_in_variable(var, shader) {
343      if ((int)var->data.location > highest_location)
344          highest_location = var->data.location;
345      if ((int)var->data.driver_location > highest_drv_location)
346          highest_drv_location = var->data.driver_location;
347    }
348 
349    nir_variable *aapoint_input = nir_variable_create(shader, nir_var_shader_in,
350                                                      glsl_vec4_type(), "aapoint");
351    if (highest_location == -1 || highest_location < VARYING_SLOT_VAR0) {
352      aapoint_input->data.location = VARYING_SLOT_VAR0;
353    } else {
354      aapoint_input->data.location = highest_location + 1;
355    }
356    aapoint_input->data.driver_location = highest_drv_location + 1;
357 
358    shader->num_inputs++;
359    *varying = tgsi_get_generic_gl_varying_index(aapoint_input->data.location, true);
360    state.input = aapoint_input;
361 
362    nir_foreach_function(function, shader) {
363       if (function->impl) {
364          nir_lower_aapoint_impl(function->impl, &state);
365       }
366    }
367 }
368