glsl: add gl_BoundingBox and associated varying slots
[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 + 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 idx != VARYING_SLOT_BOUNDING_BOX0 &&
104 idx != VARYING_SLOT_BOUNDING_BOX1;
105 GLbitfield64 bitfield;
106
107 if (is_patch_generic) {
108 assert(idx >= VARYING_SLOT_PATCH0 && idx < VARYING_SLOT_TESS_MAX);
109 bitfield = BITFIELD64_BIT(idx - VARYING_SLOT_PATCH0);
110 }
111 else {
112 assert(idx < VARYING_SLOT_MAX);
113 bitfield = BITFIELD64_BIT(idx);
114 }
115
116 if (var->data.mode == ir_var_shader_in) {
117 if (is_patch_generic)
118 prog->PatchInputsRead |= bitfield;
119 else
120 prog->InputsRead |= bitfield;
121
122 /* double inputs read is only for vertex inputs */
123 if (stage == MESA_SHADER_VERTEX &&
124 var->type->without_array()->is_dual_slot())
125 prog->DoubleInputsRead |= bitfield;
126
127 if (stage == MESA_SHADER_FRAGMENT) {
128 gl_fragment_program *fprog = (gl_fragment_program *) prog;
129 fprog->InterpQualifier[idx] =
130 (glsl_interp_mode) var->data.interpolation;
131 if (var->data.centroid)
132 fprog->IsCentroid |= bitfield;
133 if (var->data.sample)
134 fprog->IsSample |= bitfield;
135 }
136 } else if (var->data.mode == ir_var_system_value) {
137 prog->SystemValuesRead |= bitfield;
138 } else {
139 assert(var->data.mode == ir_var_shader_out);
140 if (is_patch_generic) {
141 prog->PatchOutputsWritten |= bitfield;
142 } else if (!var->data.read_only) {
143 prog->OutputsWritten |= bitfield;
144 if (var->data.index > 0)
145 prog->SecondaryOutputsWritten |= bitfield;
146 }
147
148 if (var->data.fb_fetch_output)
149 prog->OutputsRead |= bitfield;
150 }
151 }
152 }
153
154 /**
155 * Mark an entire variable as used. Caller must ensure that the variable
156 * represents a shader input or output.
157 */
158 void
159 ir_set_program_inouts_visitor::mark_whole_variable(ir_variable *var)
160 {
161 const glsl_type *type = var->type;
162 bool is_vertex_input = false;
163 if (this->shader_stage == MESA_SHADER_GEOMETRY &&
164 var->data.mode == ir_var_shader_in && type->is_array()) {
165 type = type->fields.array;
166 }
167
168 if (this->shader_stage == MESA_SHADER_TESS_CTRL &&
169 var->data.mode == ir_var_shader_in) {
170 assert(type->is_array());
171 type = type->fields.array;
172 }
173
174 if (this->shader_stage == MESA_SHADER_TESS_CTRL &&
175 var->data.mode == ir_var_shader_out && !var->data.patch) {
176 assert(type->is_array());
177 type = type->fields.array;
178 }
179
180 if (this->shader_stage == MESA_SHADER_TESS_EVAL &&
181 var->data.mode == ir_var_shader_in && !var->data.patch) {
182 assert(type->is_array());
183 type = type->fields.array;
184 }
185
186 if (this->shader_stage == MESA_SHADER_VERTEX &&
187 var->data.mode == ir_var_shader_in)
188 is_vertex_input = true;
189
190 mark(this->prog, var, 0, type->count_attribute_slots(is_vertex_input),
191 this->shader_stage);
192 }
193
194 /* Default handler: Mark all the locations in the variable as used. */
195 ir_visitor_status
196 ir_set_program_inouts_visitor::visit(ir_dereference_variable *ir)
197 {
198 if (!is_shader_inout(ir->var))
199 return visit_continue;
200
201 mark_whole_variable(ir->var);
202
203 return visit_continue;
204 }
205
206 /**
207 * Try to mark a portion of the given variable as used. Caller must ensure
208 * that the variable represents a shader input or output which can be indexed
209 * into in array fashion (an array or matrix). For the purpose of geometry
210 * shader inputs (which are always arrays*), this means that the array element
211 * must be something that can be indexed into in array fashion.
212 *
213 * *Except gl_PrimitiveIDIn, as noted below.
214 *
215 * For tessellation control shaders all inputs and non-patch outputs are
216 * arrays. For tessellation evaluation shaders non-patch inputs are arrays.
217 *
218 * If the index can't be interpreted as a constant, or some other problem
219 * occurs, then nothing will be marked and false will be returned.
220 */
221 bool
222 ir_set_program_inouts_visitor::try_mark_partial_variable(ir_variable *var,
223 ir_rvalue *index)
224 {
225 const glsl_type *type = var->type;
226
227 if (this->shader_stage == MESA_SHADER_GEOMETRY &&
228 var->data.mode == ir_var_shader_in) {
229 /* The only geometry shader input that is not an array is
230 * gl_PrimitiveIDIn, and in that case, this code will never be reached,
231 * because gl_PrimitiveIDIn can't be indexed into in array fashion.
232 */
233 assert(type->is_array());
234 type = type->fields.array;
235 }
236
237 if (this->shader_stage == MESA_SHADER_TESS_CTRL &&
238 var->data.mode == ir_var_shader_in) {
239 assert(type->is_array());
240 type = type->fields.array;
241 }
242
243 if (this->shader_stage == MESA_SHADER_TESS_CTRL &&
244 var->data.mode == ir_var_shader_out && !var->data.patch) {
245 assert(type->is_array());
246 type = type->fields.array;
247 }
248
249 if (this->shader_stage == MESA_SHADER_TESS_EVAL &&
250 var->data.mode == ir_var_shader_in && !var->data.patch) {
251 assert(type->is_array());
252 type = type->fields.array;
253 }
254
255 /* TODO: implement proper arrays of arrays support
256 * for now let the caller mark whole variable as used.
257 */
258 if (type->is_array() && type->fields.array->is_array())
259 return false;
260
261 /* The code below only handles:
262 *
263 * - Indexing into matrices
264 * - Indexing into arrays of (matrices, vectors, or scalars)
265 *
266 * All other possibilities are either prohibited by GLSL (vertex inputs and
267 * fragment outputs can't be structs) or should have been eliminated by
268 * lowering passes (do_vec_index_to_swizzle() gets rid of indexing into
269 * vectors, and lower_packed_varyings() gets rid of structs that occur in
270 * varyings).
271 *
272 * However, we don't use varying packing in all cases - tessellation
273 * shaders bypass it. This means we'll see varying structs and arrays
274 * of structs here. For now, we just give up so the caller marks the
275 * entire variable as used.
276 */
277 if (!(type->is_matrix() ||
278 (type->is_array() &&
279 (type->fields.array->is_numeric() ||
280 type->fields.array->is_boolean())))) {
281
282 /* If we don't know how to handle this case, give up and let the
283 * caller mark the whole variable as used.
284 */
285 return false;
286 }
287
288 ir_constant *index_as_constant = index->as_constant();
289 if (!index_as_constant)
290 return false;
291
292 unsigned elem_width;
293 unsigned num_elems;
294 if (type->is_array()) {
295 num_elems = type->length;
296 if (type->fields.array->is_matrix())
297 elem_width = type->fields.array->matrix_columns;
298 else
299 elem_width = 1;
300 } else {
301 num_elems = type->matrix_columns;
302 elem_width = 1;
303 }
304
305 if (index_as_constant->value.u[0] >= num_elems) {
306 /* Constant index outside the bounds of the matrix/array. This could
307 * arise as a result of constant folding of a legal GLSL program.
308 *
309 * Even though the spec says that indexing outside the bounds of a
310 * matrix/array results in undefined behaviour, we don't want to pass
311 * out-of-range values to mark() (since this could result in slots that
312 * don't exist being marked as used), so just let the caller mark the
313 * whole variable as used.
314 */
315 return false;
316 }
317
318 /* double element width for double types that takes two slots */
319 if (this->shader_stage != MESA_SHADER_VERTEX ||
320 var->data.mode != ir_var_shader_in) {
321 if (type->without_array()->is_dual_slot())
322 elem_width *= 2;
323 }
324
325 mark(this->prog, var, index_as_constant->value.u[0] * elem_width,
326 elem_width, this->shader_stage);
327 return true;
328 }
329
330 static bool
331 is_multiple_vertices(gl_shader_stage stage, ir_variable *var)
332 {
333 if (var->data.patch)
334 return false;
335
336 if (var->data.mode == ir_var_shader_in)
337 return stage == MESA_SHADER_GEOMETRY ||
338 stage == MESA_SHADER_TESS_CTRL ||
339 stage == MESA_SHADER_TESS_EVAL;
340 if (var->data.mode == ir_var_shader_out)
341 return stage == MESA_SHADER_TESS_CTRL;
342
343 return false;
344 }
345
346 ir_visitor_status
347 ir_set_program_inouts_visitor::visit_enter(ir_dereference_array *ir)
348 {
349 /* Note: for geometry shader inputs, lower_named_interface_blocks may
350 * create 2D arrays, so we need to be able to handle those. 2D arrays
351 * shouldn't be able to crop up for any other reason.
352 */
353 if (ir_dereference_array * const inner_array =
354 ir->array->as_dereference_array()) {
355 /* ir => foo[i][j]
356 * inner_array => foo[i]
357 */
358 if (ir_dereference_variable * const deref_var =
359 inner_array->array->as_dereference_variable()) {
360 if (is_multiple_vertices(this->shader_stage, deref_var->var)) {
361 /* foo is a geometry or tessellation shader input, so i is
362 * the vertex, and j the part of the input we're accessing.
363 */
364 if (try_mark_partial_variable(deref_var->var, ir->array_index))
365 {
366 /* We've now taken care of foo and j, but i might contain a
367 * subexpression that accesses shader inputs. So manually
368 * visit i and then continue with the parent.
369 */
370 inner_array->array_index->accept(this);
371 return visit_continue_with_parent;
372 }
373 }
374 }
375 } else if (ir_dereference_variable * const deref_var =
376 ir->array->as_dereference_variable()) {
377 /* ir => foo[i], where foo is a variable. */
378 if (is_multiple_vertices(this->shader_stage, deref_var->var)) {
379 /* foo is a geometry or tessellation shader input, so i is
380 * the vertex, and we're accessing the entire input.
381 */
382 mark_whole_variable(deref_var->var);
383 /* We've now taken care of foo, but i might contain a subexpression
384 * that accesses shader inputs. So manually visit i and then
385 * continue with the parent.
386 */
387 ir->array_index->accept(this);
388 return visit_continue_with_parent;
389 } else if (is_shader_inout(deref_var->var)) {
390 /* foo is a shader input/output, but not a geometry shader input,
391 * so i is the part of the input we're accessing.
392 */
393 if (try_mark_partial_variable(deref_var->var, ir->array_index))
394 return visit_continue_with_parent;
395 }
396 }
397
398 /* The expression is something we don't recognize. Just visit its
399 * subexpressions.
400 */
401 return visit_continue;
402 }
403
404 ir_visitor_status
405 ir_set_program_inouts_visitor::visit_enter(ir_function_signature *ir)
406 {
407 /* We don't want to descend into the function parameters and
408 * consider them as shader inputs or outputs.
409 */
410 visit_list_elements(this, &ir->body);
411 return visit_continue_with_parent;
412 }
413
414 ir_visitor_status
415 ir_set_program_inouts_visitor::visit_enter(ir_expression *ir)
416 {
417 if (this->shader_stage == MESA_SHADER_FRAGMENT &&
418 (ir->operation == ir_unop_dFdy ||
419 ir->operation == ir_unop_dFdy_coarse ||
420 ir->operation == ir_unop_dFdy_fine)) {
421 gl_fragment_program *fprog = (gl_fragment_program *) prog;
422 fprog->UsesDFdy = true;
423 }
424 return visit_continue;
425 }
426
427 ir_visitor_status
428 ir_set_program_inouts_visitor::visit_enter(ir_discard *)
429 {
430 /* discards are only allowed in fragment shaders. */
431 assert(this->shader_stage == MESA_SHADER_FRAGMENT);
432
433 gl_fragment_program *fprog = (gl_fragment_program *) prog;
434 fprog->UsesKill = true;
435
436 return visit_continue;
437 }
438
439 ir_visitor_status
440 ir_set_program_inouts_visitor::visit_enter(ir_texture *ir)
441 {
442 if (ir->op == ir_tg4)
443 prog->UsesGather = true;
444 return visit_continue;
445 }
446
447 void
448 do_set_program_inouts(exec_list *instructions, struct gl_program *prog,
449 gl_shader_stage shader_stage)
450 {
451 ir_set_program_inouts_visitor v(prog, shader_stage);
452
453 prog->InputsRead = 0;
454 prog->OutputsWritten = 0;
455 prog->SecondaryOutputsWritten = 0;
456 prog->OutputsRead = 0;
457 prog->PatchInputsRead = 0;
458 prog->PatchOutputsWritten = 0;
459 prog->SystemValuesRead = 0;
460 if (shader_stage == MESA_SHADER_FRAGMENT) {
461 gl_fragment_program *fprog = (gl_fragment_program *) prog;
462 memset(fprog->InterpQualifier, 0, sizeof(fprog->InterpQualifier));
463 fprog->IsCentroid = 0;
464 fprog->IsSample = 0;
465 fprog->UsesDFdy = false;
466 fprog->UsesKill = false;
467 }
468 visit_list_elements(&v, instructions);
469 }