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
181 nir_def *out_input = intrin->src[1].ssa;
182 b->cursor = nir_before_instr(instr);
183 nir_def *lw = nir_load_var(b, state->line_width_input);
184 nir_def *len = nir_channel(b, lw, 3);
185 len = nir_fadd_imm(b, nir_fmul_imm(b, len, 2.0), -1.0);
186 nir_def *tmp = nir_fsat(b, nir_fadd(b, nir_channels(b, lw, 0xa),
187 nir_fneg(b, nir_fabs(b, nir_channels(b, lw, 0x5)))));
188
189 nir_def *max = len;
190 if (state->stipple_counter) {
191 assert(state->stipple_pattern);
192
193 nir_def *counter = nir_load_var(b, state->stipple_counter);
194 nir_def *pattern = nir_load_var(b, state->stipple_pattern);
195 nir_def *factor = nir_i2f32(b, nir_ishr_imm(b, pattern, 16));
196 pattern = nir_iand_imm(b, pattern, 0xffff);
197
198 nir_def *stipple_pos = nir_vec2(b, nir_fadd_imm(b, counter, -0.5),
199 nir_fadd_imm(b, counter, 0.5));
200
201 stipple_pos = nir_frem(b, nir_fdiv(b, stipple_pos, factor),
202 nir_imm_float(b, 16.0));
203
204 nir_def *p = nir_f2i32(b, stipple_pos);
205 nir_def *one = nir_imm_float(b, 1.0);
206
207 // float t = 1.0 - min((1.0 - fract(stipple_pos.x)) * factor, 1.0);
208 nir_def *t = nir_ffract(b, nir_channel(b, stipple_pos, 0));
209 t = nir_fsub(b, one,
210 nir_fmin(b, nir_fmul(b, factor,
211 nir_fsub(b, one, t)), one));
212
213 // vec2 a = vec2((uvec2(pattern) >> p) & uvec2(1u));
214 nir_def *a = nir_i2f32(b,
215 nir_iand(b, nir_ishr(b, nir_replicate(b, pattern, 2), p),
216 nir_imm_ivec2(b, 1, 1)));
217
218 // float cov = mix(a.x, a.y, t);
219 nir_def *cov = nir_flrp(b, nir_channel(b, a, 0), nir_channel(b, a, 1), t);
220
221 max = nir_fmin(b, len, cov);
222 }
223
224 tmp = nir_fmul(b, nir_channel(b, tmp, 0),
225 nir_fmin(b, nir_channel(b, tmp, 1), max));
226 tmp = nir_fmul(b, nir_channel(b, out_input, 3), tmp);
227
228 nir_def *out = nir_vec4(b, nir_channel(b, out_input, 0),
229 nir_channel(b, out_input, 1),
230 nir_channel(b, out_input, 2),
231 tmp);
232 nir_src_rewrite(&intrin->src[1], out);
233 return true;
234 }
235
236 void
nir_lower_aaline_fs(struct nir_shader * shader,int * varying,nir_variable * stipple_counter,nir_variable * stipple_pattern)237 nir_lower_aaline_fs(struct nir_shader *shader, int *varying,
238 nir_variable *stipple_counter,
239 nir_variable *stipple_pattern)
240 {
241 lower_aaline state = {
242 .stipple_counter = stipple_counter,
243 .stipple_pattern = stipple_pattern,
244 };
245 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
246
247 int highest_location = -1, highest_drv_location = -1;
248 nir_foreach_shader_in_variable(var, shader) {
249 if ((int)var->data.location > highest_location)
250 highest_location = var->data.location;
251 if ((int)var->data.driver_location > highest_drv_location)
252 highest_drv_location = var->data.driver_location;
253 }
254
255 nir_variable *line_width = nir_variable_create(shader, nir_var_shader_in,
256 glsl_vec4_type(), "aaline");
257 if (highest_location == -1 || highest_location < VARYING_SLOT_VAR0) {
258 line_width->data.location = VARYING_SLOT_VAR0;
259 line_width->data.driver_location = highest_drv_location + 1;
260 } else {
261 line_width->data.location = highest_location + 1;
262 line_width->data.driver_location = highest_drv_location + 1;
263 }
264 shader->num_inputs++;
265 *varying = tgsi_get_generic_gl_varying_index(line_width->data.location, true);
266 state.line_width_input = line_width;
267
268 nir_shader_instructions_pass(shader, lower_aaline_instr,
269 nir_metadata_dominance, &state);
270 }
271
272 typedef struct {
273 nir_builder b;
274 nir_shader *shader;
275 nir_variable *input;
276 } lower_aapoint;
277
278 static void
nir_lower_aapoint_block(nir_block * block,lower_aapoint * state,nir_def * sel)279 nir_lower_aapoint_block(nir_block *block,
280 lower_aapoint *state, nir_def *sel)
281 {
282 nir_builder *b = &state->b;
283 nir_foreach_instr(instr, block) {
284 if (instr->type != nir_instr_type_intrinsic)
285 continue;
286
287 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
288 if (intrin->intrinsic != nir_intrinsic_store_deref)
289 continue;
290
291 nir_variable *var = nir_intrinsic_get_var(intrin, 0);
292 if (var->data.mode != nir_var_shader_out)
293 continue;
294 if (var->data.location < FRAG_RESULT_DATA0 && var->data.location != FRAG_RESULT_COLOR)
295 continue;
296
297 nir_def *out_input = intrin->src[1].ssa;
298 b->cursor = nir_before_instr(instr);
299
300 nir_def *tmp = nir_fmul(b, nir_channel(b, out_input, 3), sel);
301 nir_def *out = nir_vec4(b, nir_channel(b, out_input, 0),
302 nir_channel(b, out_input, 1),
303 nir_channel(b, out_input, 2),
304 tmp);
305 nir_src_rewrite(&intrin->src[1], out);
306 }
307
308 }
309
310 static void
nir_lower_aapoint_impl(nir_function_impl * impl,lower_aapoint * state,nir_alu_type bool_type)311 nir_lower_aapoint_impl(nir_function_impl *impl, lower_aapoint *state,
312 nir_alu_type bool_type)
313 {
314 nir_block *block = nir_start_block(impl);
315 state->b = nir_builder_at(nir_before_block(block));
316
317 nir_builder *b = &state->b;
318 nir_def *aainput = nir_load_var(b, state->input);
319
320 nir_def *dist = nir_fadd(b, nir_fmul(b, nir_channel(b, aainput, 0), nir_channel(b, aainput, 0)),
321 nir_fmul(b, nir_channel(b, aainput, 1), nir_channel(b, aainput, 1)));
322
323 nir_def *k = nir_channel(b, aainput, 2);
324 nir_def *chan_val_one = nir_channel(b, aainput, 3);
325 nir_def *comp;
326
327 switch (bool_type) {
328 case nir_type_bool1:
329 comp = nir_flt(b, chan_val_one, dist);
330 break;
331 case nir_type_bool32:
332 comp = nir_flt32(b, chan_val_one, dist);
333 break;
334 case nir_type_float32:
335 comp = nir_slt(b, chan_val_one, dist);
336 break;
337 default:
338 unreachable("Invalid Boolean type.");
339 }
340
341 nir_discard_if(b, comp);
342 b->shader->info.fs.uses_discard = true;
343
344 /* compute coverage factor = (1-d)/(1-k) */
345 /* 1 - k */
346 nir_def *tmp = nir_fadd(b, chan_val_one, nir_fneg(b, k));
347 /* 1.0 / (1 - k) */
348 tmp = nir_frcp(b, tmp);
349
350 /* 1 - d */
351 nir_def *tmp2 = nir_fadd(b, chan_val_one, nir_fneg(b, dist));
352
353 /* (1 - d) / (1 - k) */
354 nir_def *coverage = nir_fmul(b, tmp, tmp2);
355
356 /* if (k >= distance)
357 * sel = coverage;
358 * else
359 * sel = 1.0;
360 */
361 nir_def *sel;
362
363 switch (bool_type) {
364 case nir_type_bool1:
365 sel = nir_b32csel(b, nir_fge(b, k, dist), coverage, chan_val_one);
366 break;
367 case nir_type_bool32:
368 sel = nir_b32csel(b, nir_fge32(b, k, dist), coverage, chan_val_one);
369 break;
370 case nir_type_float32: {
371 /* On this path, don't assume that any "fancy" instructions are
372 * supported, but also try to emit something decent.
373 *
374 * sel = (k >= distance) ? coverage : 1.0;
375 * sel = (k >= distance) * coverage : (1 - (k >= distance)) * 1.0
376 * sel = (k >= distance) * coverage : (1 - (k >= distance))
377 *
378 * Since (k >= distance) * coverage is zero when (1 - (k >= distance))
379 * is not zero,
380 *
381 * sel = (k >= distance) * coverage + (1 - (k >= distance))
382 *
383 * If we assume that coverage == fsat(coverage), this could be further
384 * optimized to fsat(coverage + (1 - (k >= distance))), but I don't feel
385 * like verifying that right now.
386 */
387 nir_def *cmp_result = nir_sge(b, k, dist);
388 sel = nir_fadd(b,
389 nir_fmul(b, coverage, cmp_result),
390 nir_fadd(b, chan_val_one, nir_fneg(b, cmp_result)));
391 break;
392 }
393 default:
394 unreachable("Invalid Boolean type.");
395 }
396
397 nir_foreach_block(block, impl) {
398 nir_lower_aapoint_block(block, state, sel);
399 }
400 }
401
402 void
nir_lower_aapoint_fs(struct nir_shader * shader,int * varying,const nir_alu_type bool_type)403 nir_lower_aapoint_fs(struct nir_shader *shader, int *varying, const nir_alu_type bool_type)
404 {
405 assert(bool_type == nir_type_bool1 ||
406 bool_type == nir_type_bool32 ||
407 bool_type == nir_type_float32);
408
409 lower_aapoint state = {
410 .shader = shader,
411 };
412 if (shader->info.stage != MESA_SHADER_FRAGMENT)
413 return;
414
415 int highest_location = -1, highest_drv_location = -1;
416 nir_foreach_shader_in_variable(var, shader) {
417 if ((int)var->data.location > highest_location)
418 highest_location = var->data.location;
419 if ((int)var->data.driver_location > highest_drv_location)
420 highest_drv_location = var->data.driver_location;
421 }
422
423 nir_variable *aapoint_input = nir_variable_create(shader, nir_var_shader_in,
424 glsl_vec4_type(), "aapoint");
425 if (highest_location == -1 || highest_location < VARYING_SLOT_VAR0) {
426 aapoint_input->data.location = VARYING_SLOT_VAR0;
427 } else {
428 aapoint_input->data.location = highest_location + 1;
429 }
430 aapoint_input->data.driver_location = highest_drv_location + 1;
431
432 shader->num_inputs++;
433 *varying = tgsi_get_generic_gl_varying_index(aapoint_input->data.location, true);
434 state.input = aapoint_input;
435
436 nir_foreach_function_impl(impl, shader) {
437 nir_lower_aapoint_impl(impl, &state, bool_type);
438 }
439 }
440