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