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