glsl: Calculate bitset of secondary outputs written in ir_set_program_inouts.
[mesa.git] / src / compiler / glsl / ir_set_program_inouts.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * 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 OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file ir_set_program_inouts.cpp
26 *
27 * Sets the InputsRead and OutputsWritten of Mesa programs.
28 *
29 * Additionally, for fragment shaders, sets the InterpQualifier array, the
30 * IsCentroid and IsSample bitfields, and the UsesDFdy flag.
31 *
32 * Mesa programs (gl_program, not gl_shader_program) have a set of
33 * flags indicating which varyings are read and written. Computing
34 * which are actually read from some sort of backend code can be
35 * tricky when variable array indexing involved. So this pass
36 * provides support for setting InputsRead and OutputsWritten right
37 * from the GLSL IR.
38 */
39
40 #include "main/core.h" /* for struct gl_program */
41 #include "ir.h"
42 #include "ir_visitor.h"
43 #include "compiler/glsl_types.h"
44
45 namespace {
46
47 class ir_set_program_inouts_visitor : public ir_hierarchical_visitor {
48 public:
49 ir_set_program_inouts_visitor(struct gl_program *prog,
50 gl_shader_stage shader_stage)
51 {
52 this->prog = prog;
53 this->shader_stage = shader_stage;
54 }
55 ~ir_set_program_inouts_visitor()
56 {
57 }
58
59 virtual ir_visitor_status visit_enter(ir_dereference_array *);
60 virtual ir_visitor_status visit_enter(ir_function_signature *);
61 virtual ir_visitor_status visit_enter(ir_expression *);
62 virtual ir_visitor_status visit_enter(ir_discard *);
63 virtual ir_visitor_status visit_enter(ir_texture *);
64 virtual ir_visitor_status visit(ir_dereference_variable *);
65
66 private:
67 void mark_whole_variable(ir_variable *var);
68 bool try_mark_partial_variable(ir_variable *var, ir_rvalue *index);
69
70 struct gl_program *prog;
71 gl_shader_stage shader_stage;
72 };
73
74 } /* anonymous namespace */
75
76 static inline bool
77 is_shader_inout(ir_variable *var)
78 {
79 return var->data.mode == ir_var_shader_in ||
80 var->data.mode == ir_var_shader_out ||
81 var->data.mode == ir_var_system_value;
82 }
83
84 static void
85 mark(struct gl_program *prog, ir_variable *var, int offset, int len,
86 gl_shader_stage stage)
87 {
88 /* As of GLSL 1.20, varyings can only be floats, floating-point
89 * vectors or matrices, or arrays of them. For Mesa programs using
90 * InputsRead/OutputsWritten, everything but matrices uses one
91 * slot, while matrices use a slot per column. Presumably
92 * something doing a more clever packing would use something other
93 * than InputsRead/OutputsWritten.
94 */
95
96 for (int i = 0; i < len; i++) {
97 assert(var->data.location != -1);
98
99 int idx = var->data.location + var->data.index + offset + i;
100 bool is_patch_generic = var->data.patch &&
101 idx != VARYING_SLOT_TESS_LEVEL_INNER &&
102 idx != VARYING_SLOT_TESS_LEVEL_OUTER;
103 GLbitfield64 bitfield;
104
105 if (is_patch_generic) {
106 assert(idx >= VARYING_SLOT_PATCH0 && idx < VARYING_SLOT_TESS_MAX);
107 bitfield = BITFIELD64_BIT(idx - VARYING_SLOT_PATCH0);
108 }
109 else {
110 assert(idx < VARYING_SLOT_MAX);
111 bitfield = BITFIELD64_BIT(idx);
112 }
113
114 if (var->data.mode == ir_var_shader_in) {
115 if (is_patch_generic)
116 prog->PatchInputsRead |= bitfield;
117 else
118 prog->InputsRead |= bitfield;
119
120 /* double inputs read is only for vertex inputs */
121 if (stage == MESA_SHADER_VERTEX &&
122 var->type->without_array()->is_dual_slot())
123 prog->DoubleInputsRead |= bitfield;
124
125 if (stage == MESA_SHADER_FRAGMENT) {
126 gl_fragment_program *fprog = (gl_fragment_program *) prog;
127 fprog->InterpQualifier[idx] =
128 (glsl_interp_mode) var->data.interpolation;
129 if (var->data.centroid)
130 fprog->IsCentroid |= bitfield;
131 if (var->data.sample)
132 fprog->IsSample |= bitfield;
133 }
134 } else if (var->data.mode == ir_var_system_value) {
135 prog->SystemValuesRead |= bitfield;
136 } else {
137 assert(var->data.mode == ir_var_shader_out);
138 if (is_patch_generic) {
139 prog->PatchOutputsWritten |= bitfield;
140 } else if (!var->data.read_only) {
141 prog->OutputsWritten |= bitfield;
142 if (var->data.index > 0)
143 prog->SecondaryOutputsWritten |= bitfield;
144 }
145
146 if (var->data.fb_fetch_output)
147 prog->OutputsRead |= bitfield;
148 }
149 }
150 }
151
152 /**
153 * Mark an entire variable as used. Caller must ensure that the variable
154 * represents a shader input or output.
155 */
156 void
157 ir_set_program_inouts_visitor::mark_whole_variable(ir_variable *var)
158 {
159 const glsl_type *type = var->type;
160 bool is_vertex_input = false;
161 if (this->shader_stage == MESA_SHADER_GEOMETRY &&
162 var->data.mode == ir_var_shader_in && type->is_array()) {
163 type = type->fields.array;
164 }
165
166 if (this->shader_stage == MESA_SHADER_TESS_CTRL &&
167 var->data.mode == ir_var_shader_in) {
168 assert(type->is_array());
169 type = type->fields.array;
170 }
171
172 if (this->shader_stage == MESA_SHADER_TESS_CTRL &&
173 var->data.mode == ir_var_shader_out && !var->data.patch) {
174 assert(type->is_array());
175 type = type->fields.array;
176 }
177
178 if (this->shader_stage == MESA_SHADER_TESS_EVAL &&
179 var->data.mode == ir_var_shader_in && !var->data.patch) {
180 assert(type->is_array());
181 type = type->fields.array;
182 }
183
184 if (this->shader_stage == MESA_SHADER_VERTEX &&
185 var->data.mode == ir_var_shader_in)
186 is_vertex_input = true;
187
188 mark(this->prog, var, 0, type->count_attribute_slots(is_vertex_input),
189 this->shader_stage);
190 }
191
192 /* Default handler: Mark all the locations in the variable as used. */
193 ir_visitor_status
194 ir_set_program_inouts_visitor::visit(ir_dereference_variable *ir)
195 {
196 if (!is_shader_inout(ir->var))
197 return visit_continue;
198
199 mark_whole_variable(ir->var);
200
201 return visit_continue;
202 }
203
204 /**
205 * Try to mark a portion of the given variable as used. Caller must ensure
206 * that the variable represents a shader input or output which can be indexed
207 * into in array fashion (an array or matrix). For the purpose of geometry
208 * shader inputs (which are always arrays*), this means that the array element
209 * must be something that can be indexed into in array fashion.
210 *
211 * *Except gl_PrimitiveIDIn, as noted below.
212 *
213 * For tessellation control shaders all inputs and non-patch outputs are
214 * arrays. For tessellation evaluation shaders non-patch inputs are arrays.
215 *
216 * If the index can't be interpreted as a constant, or some other problem
217 * occurs, then nothing will be marked and false will be returned.
218 */
219 bool
220 ir_set_program_inouts_visitor::try_mark_partial_variable(ir_variable *var,
221 ir_rvalue *index)
222 {
223 const glsl_type *type = var->type;
224
225 if (this->shader_stage == MESA_SHADER_GEOMETRY &&
226 var->data.mode == ir_var_shader_in) {
227 /* The only geometry shader input that is not an array is
228 * gl_PrimitiveIDIn, and in that case, this code will never be reached,
229 * because gl_PrimitiveIDIn can't be indexed into in array fashion.
230 */
231 assert(type->is_array());
232 type = type->fields.array;
233 }
234
235 if (this->shader_stage == MESA_SHADER_TESS_CTRL &&
236 var->data.mode == ir_var_shader_in) {
237 assert(type->is_array());
238 type = type->fields.array;
239 }
240
241 if (this->shader_stage == MESA_SHADER_TESS_CTRL &&
242 var->data.mode == ir_var_shader_out && !var->data.patch) {
243 assert(type->is_array());
244 type = type->fields.array;
245 }
246
247 if (this->shader_stage == MESA_SHADER_TESS_EVAL &&
248 var->data.mode == ir_var_shader_in && !var->data.patch) {
249 assert(type->is_array());
250 type = type->fields.array;
251 }
252
253 /* TODO: implement proper arrays of arrays support
254 * for now let the caller mark whole variable as used.
255 */
256 if (type->is_array() && type->fields.array->is_array())
257 return false;
258
259 /* The code below only handles:
260 *
261 * - Indexing into matrices
262 * - Indexing into arrays of (matrices, vectors, or scalars)
263 *
264 * All other possibilities are either prohibited by GLSL (vertex inputs and
265 * fragment outputs can't be structs) or should have been eliminated by
266 * lowering passes (do_vec_index_to_swizzle() gets rid of indexing into
267 * vectors, and lower_packed_varyings() gets rid of structs that occur in
268 * varyings).
269 *
270 * However, we don't use varying packing in all cases - tessellation
271 * shaders bypass it. This means we'll see varying structs and arrays
272 * of structs here. For now, we just give up so the caller marks the
273 * entire variable as used.
274 */
275 if (!(type->is_matrix() ||
276 (type->is_array() &&
277 (type->fields.array->is_numeric() ||
278 type->fields.array->is_boolean())))) {
279
280 /* If we don't know how to handle this case, give up and let the
281 * caller mark the whole variable as used.
282 */
283 return false;
284 }
285
286 ir_constant *index_as_constant = index->as_constant();
287 if (!index_as_constant)
288 return false;
289
290 unsigned elem_width;
291 unsigned num_elems;
292 if (type->is_array()) {
293 num_elems = type->length;
294 if (type->fields.array->is_matrix())
295 elem_width = type->fields.array->matrix_columns;
296 else
297 elem_width = 1;
298 } else {
299 num_elems = type->matrix_columns;
300 elem_width = 1;
301 }
302
303 if (index_as_constant->value.u[0] >= num_elems) {
304 /* Constant index outside the bounds of the matrix/array. This could
305 * arise as a result of constant folding of a legal GLSL program.
306 *
307 * Even though the spec says that indexing outside the bounds of a
308 * matrix/array results in undefined behaviour, we don't want to pass
309 * out-of-range values to mark() (since this could result in slots that
310 * don't exist being marked as used), so just let the caller mark the
311 * whole variable as used.
312 */
313 return false;
314 }
315
316 /* double element width for double types that takes two slots */
317 if (this->shader_stage != MESA_SHADER_VERTEX ||
318 var->data.mode != ir_var_shader_in) {
319 if (type->without_array()->is_dual_slot())
320 elem_width *= 2;
321 }
322
323 mark(this->prog, var, index_as_constant->value.u[0] * elem_width,
324 elem_width, this->shader_stage);
325 return true;
326 }
327
328 static bool
329 is_multiple_vertices(gl_shader_stage stage, ir_variable *var)
330 {
331 if (var->data.patch)
332 return false;
333
334 if (var->data.mode == ir_var_shader_in)
335 return stage == MESA_SHADER_GEOMETRY ||
336 stage == MESA_SHADER_TESS_CTRL ||
337 stage == MESA_SHADER_TESS_EVAL;
338 if (var->data.mode == ir_var_shader_out)
339 return stage == MESA_SHADER_TESS_CTRL;
340
341 return false;
342 }
343
344 ir_visitor_status
345 ir_set_program_inouts_visitor::visit_enter(ir_dereference_array *ir)
346 {
347 /* Note: for geometry shader inputs, lower_named_interface_blocks may
348 * create 2D arrays, so we need to be able to handle those. 2D arrays
349 * shouldn't be able to crop up for any other reason.
350 */
351 if (ir_dereference_array * const inner_array =
352 ir->array->as_dereference_array()) {
353 /* ir => foo[i][j]
354 * inner_array => foo[i]
355 */
356 if (ir_dereference_variable * const deref_var =
357 inner_array->array->as_dereference_variable()) {
358 if (is_multiple_vertices(this->shader_stage, deref_var->var)) {
359 /* foo is a geometry or tessellation shader input, so i is
360 * the vertex, and j the part of the input we're accessing.
361 */
362 if (try_mark_partial_variable(deref_var->var, ir->array_index))
363 {
364 /* We've now taken care of foo and j, but i might contain a
365 * subexpression that accesses shader inputs. So manually
366 * visit i and then continue with the parent.
367 */
368 inner_array->array_index->accept(this);
369 return visit_continue_with_parent;
370 }
371 }
372 }
373 } else if (ir_dereference_variable * const deref_var =
374 ir->array->as_dereference_variable()) {
375 /* ir => foo[i], where foo is a variable. */
376 if (is_multiple_vertices(this->shader_stage, deref_var->var)) {
377 /* foo is a geometry or tessellation shader input, so i is
378 * the vertex, and we're accessing the entire input.
379 */
380 mark_whole_variable(deref_var->var);
381 /* We've now taken care of foo, but i might contain a subexpression
382 * that accesses shader inputs. So manually visit i and then
383 * continue with the parent.
384 */
385 ir->array_index->accept(this);
386 return visit_continue_with_parent;
387 } else if (is_shader_inout(deref_var->var)) {
388 /* foo is a shader input/output, but not a geometry shader input,
389 * so i is the part of the input we're accessing.
390 */
391 if (try_mark_partial_variable(deref_var->var, ir->array_index))
392 return visit_continue_with_parent;
393 }
394 }
395
396 /* The expression is something we don't recognize. Just visit its
397 * subexpressions.
398 */
399 return visit_continue;
400 }
401
402 ir_visitor_status
403 ir_set_program_inouts_visitor::visit_enter(ir_function_signature *ir)
404 {
405 /* We don't want to descend into the function parameters and
406 * consider them as shader inputs or outputs.
407 */
408 visit_list_elements(this, &ir->body);
409 return visit_continue_with_parent;
410 }
411
412 ir_visitor_status
413 ir_set_program_inouts_visitor::visit_enter(ir_expression *ir)
414 {
415 if (this->shader_stage == MESA_SHADER_FRAGMENT &&
416 (ir->operation == ir_unop_dFdy ||
417 ir->operation == ir_unop_dFdy_coarse ||
418 ir->operation == ir_unop_dFdy_fine)) {
419 gl_fragment_program *fprog = (gl_fragment_program *) prog;
420 fprog->UsesDFdy = true;
421 }
422 return visit_continue;
423 }
424
425 ir_visitor_status
426 ir_set_program_inouts_visitor::visit_enter(ir_discard *)
427 {
428 /* discards are only allowed in fragment shaders. */
429 assert(this->shader_stage == MESA_SHADER_FRAGMENT);
430
431 gl_fragment_program *fprog = (gl_fragment_program *) prog;
432 fprog->UsesKill = true;
433
434 return visit_continue;
435 }
436
437 ir_visitor_status
438 ir_set_program_inouts_visitor::visit_enter(ir_texture *ir)
439 {
440 if (ir->op == ir_tg4)
441 prog->UsesGather = true;
442 return visit_continue;
443 }
444
445 void
446 do_set_program_inouts(exec_list *instructions, struct gl_program *prog,
447 gl_shader_stage shader_stage)
448 {
449 ir_set_program_inouts_visitor v(prog, shader_stage);
450
451 prog->InputsRead = 0;
452 prog->OutputsWritten = 0;
453 prog->SecondaryOutputsWritten = 0;
454 prog->OutputsRead = 0;
455 prog->PatchInputsRead = 0;
456 prog->PatchOutputsWritten = 0;
457 prog->SystemValuesRead = 0;
458 if (shader_stage == MESA_SHADER_FRAGMENT) {
459 gl_fragment_program *fprog = (gl_fragment_program *) prog;
460 memset(fprog->InterpQualifier, 0, sizeof(fprog->InterpQualifier));
461 fprog->IsCentroid = 0;
462 fprog->IsSample = 0;
463 fprog->UsesDFdy = false;
464 fprog->UsesKill = false;
465 }
466 visit_list_elements(&v, instructions);
467 }