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