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