nir: move to compiler/
[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 "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 int idx = var->data.location + var->data.index + offset + i;
98 bool is_patch_generic = var->data.patch &&
99 idx != VARYING_SLOT_TESS_LEVEL_INNER &&
100 idx != VARYING_SLOT_TESS_LEVEL_OUTER;
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->PatchInputsRead |= bitfield;
115 else
116 prog->InputsRead |= 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_double())
121 prog->DoubleInputsRead |= bitfield;
122
123 if (stage == MESA_SHADER_FRAGMENT) {
124 gl_fragment_program *fprog = (gl_fragment_program *) prog;
125 fprog->InterpQualifier[idx] =
126 (glsl_interp_qualifier) var->data.interpolation;
127 if (var->data.centroid)
128 fprog->IsCentroid |= bitfield;
129 if (var->data.sample)
130 fprog->IsSample |= bitfield;
131 }
132 } else if (var->data.mode == ir_var_system_value) {
133 prog->SystemValuesRead |= bitfield;
134 } else {
135 assert(var->data.mode == ir_var_shader_out);
136 if (is_patch_generic)
137 prog->PatchOutputsWritten |= bitfield;
138 else
139 prog->OutputsWritten |= 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 bool vertex_input = false;
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 if (this->shader_stage == MESA_SHADER_VERTEX &&
177 var->data.mode == ir_var_shader_in)
178 vertex_input = true;
179
180 mark(this->prog, var, 0, type->count_attribute_slots(vertex_input),
181 this->shader_stage);
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 /* TODO: implement proper arrays of arrays support
246 * for now let the caller mark whole variable as used.
247 */
248 if (type->is_array() && type->fields.array->is_array())
249 return false;
250
251 /* The code below only handles:
252 *
253 * - Indexing into matrices
254 * - Indexing into arrays of (matrices, vectors, or scalars)
255 *
256 * All other possibilities are either prohibited by GLSL (vertex inputs and
257 * fragment outputs can't be structs) or should have been eliminated by
258 * lowering passes (do_vec_index_to_swizzle() gets rid of indexing into
259 * vectors, and lower_packed_varyings() gets rid of structs that occur in
260 * varyings).
261 */
262 if (!(type->is_matrix() ||
263 (type->is_array() &&
264 (type->fields.array->is_numeric() ||
265 type->fields.array->is_boolean())))) {
266 assert(!"Unexpected indexing in ir_set_program_inouts");
267
268 /* For safety in release builds, in case we ever encounter unexpected
269 * indexing, give up and let the caller mark the whole variable as used.
270 */
271 return false;
272 }
273
274 ir_constant *index_as_constant = index->as_constant();
275 if (!index_as_constant)
276 return false;
277
278 unsigned elem_width;
279 unsigned num_elems;
280 if (type->is_array()) {
281 num_elems = type->length;
282 if (type->fields.array->is_matrix())
283 elem_width = type->fields.array->matrix_columns;
284 else
285 elem_width = 1;
286 } else {
287 num_elems = type->matrix_columns;
288 elem_width = 1;
289 }
290
291 if (index_as_constant->value.u[0] >= num_elems) {
292 /* Constant index outside the bounds of the matrix/array. This could
293 * arise as a result of constant folding of a legal GLSL program.
294 *
295 * Even though the spec says that indexing outside the bounds of a
296 * matrix/array results in undefined behaviour, we don't want to pass
297 * out-of-range values to mark() (since this could result in slots that
298 * don't exist being marked as used), so just let the caller mark the
299 * whole variable as used.
300 */
301 return false;
302 }
303
304 /* double element width for double types that takes two slots */
305 if (this->shader_stage != MESA_SHADER_VERTEX ||
306 var->data.mode != ir_var_shader_in) {
307 if (type->without_array()->is_dual_slot_double())
308 elem_width *= 2;
309 }
310
311 mark(this->prog, var, index_as_constant->value.u[0] * elem_width,
312 elem_width, this->shader_stage);
313 return true;
314 }
315
316 static bool
317 is_multiple_vertices(gl_shader_stage stage, ir_variable *var)
318 {
319 if (var->data.patch)
320 return false;
321
322 if (var->data.mode == ir_var_shader_in)
323 return stage == MESA_SHADER_GEOMETRY ||
324 stage == MESA_SHADER_TESS_CTRL ||
325 stage == MESA_SHADER_TESS_EVAL;
326 if (var->data.mode == ir_var_shader_out)
327 return stage == MESA_SHADER_TESS_CTRL;
328
329 return false;
330 }
331
332 ir_visitor_status
333 ir_set_program_inouts_visitor::visit_enter(ir_dereference_array *ir)
334 {
335 /* Note: for geometry shader inputs, lower_named_interface_blocks may
336 * create 2D arrays, so we need to be able to handle those. 2D arrays
337 * shouldn't be able to crop up for any other reason.
338 */
339 if (ir_dereference_array * const inner_array =
340 ir->array->as_dereference_array()) {
341 /* ir => foo[i][j]
342 * inner_array => foo[i]
343 */
344 if (ir_dereference_variable * const deref_var =
345 inner_array->array->as_dereference_variable()) {
346 if (is_multiple_vertices(this->shader_stage, deref_var->var)) {
347 /* foo is a geometry or tessellation shader input, so i is
348 * the vertex, and j the part of the input we're accessing.
349 */
350 if (try_mark_partial_variable(deref_var->var, ir->array_index))
351 {
352 /* We've now taken care of foo and j, but i might contain a
353 * subexpression that accesses shader inputs. So manually
354 * visit i and then continue with the parent.
355 */
356 inner_array->array_index->accept(this);
357 return visit_continue_with_parent;
358 }
359 }
360 }
361 } else if (ir_dereference_variable * const deref_var =
362 ir->array->as_dereference_variable()) {
363 /* ir => foo[i], where foo is a 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 we're accessing the entire input.
367 */
368 mark_whole_variable(deref_var->var);
369 /* We've now taken care of foo, but i might contain a subexpression
370 * that accesses shader inputs. So manually visit i and then
371 * continue with the parent.
372 */
373 ir->array_index->accept(this);
374 return visit_continue_with_parent;
375 } else if (is_shader_inout(deref_var->var)) {
376 /* foo is a shader input/output, but not a geometry shader input,
377 * so i is the part of the input we're accessing.
378 */
379 if (try_mark_partial_variable(deref_var->var, ir->array_index))
380 return visit_continue_with_parent;
381 }
382 }
383
384 /* The expression is something we don't recognize. Just visit its
385 * subexpressions.
386 */
387 return visit_continue;
388 }
389
390 ir_visitor_status
391 ir_set_program_inouts_visitor::visit_enter(ir_function_signature *ir)
392 {
393 /* We don't want to descend into the function parameters and
394 * consider them as shader inputs or outputs.
395 */
396 visit_list_elements(this, &ir->body);
397 return visit_continue_with_parent;
398 }
399
400 ir_visitor_status
401 ir_set_program_inouts_visitor::visit_enter(ir_expression *ir)
402 {
403 if (this->shader_stage == MESA_SHADER_FRAGMENT &&
404 (ir->operation == ir_unop_dFdy ||
405 ir->operation == ir_unop_dFdy_coarse ||
406 ir->operation == ir_unop_dFdy_fine)) {
407 gl_fragment_program *fprog = (gl_fragment_program *) prog;
408 fprog->UsesDFdy = true;
409 }
410 return visit_continue;
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->PatchInputsRead = 0;
442 prog->PatchOutputsWritten = 0;
443 prog->SystemValuesRead = 0;
444 if (shader_stage == MESA_SHADER_FRAGMENT) {
445 gl_fragment_program *fprog = (gl_fragment_program *) prog;
446 memset(fprog->InterpQualifier, 0, sizeof(fprog->InterpQualifier));
447 fprog->IsCentroid = 0;
448 fprog->IsSample = 0;
449 fprog->UsesDFdy = false;
450 fprog->UsesKill = false;
451 }
452 visit_list_elements(&v, instructions);
453 }