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