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