1 /*
2  * Copyright © 2012 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 lower_ubo_reference.cpp
26  *
27  * IR lower pass to replace dereferences of variables in a uniform
28  * buffer object with usage of ir_binop_ubo_load expressions, each of
29  * which can read data up to the size of a vec4.
30  *
31  * This relieves drivers of the responsibility to deal with tricky UBO
32  * layout issues like std140 structures and row_major matrices on
33  * their own.
34  */
35 
36 #include "ir.h"
37 #include "ir_builder.h"
38 #include "ir_rvalue_visitor.h"
39 #include "main/macros.h"
40 
41 using namespace ir_builder;
42 
43 namespace {
44 class lower_ubo_reference_visitor : public ir_rvalue_enter_visitor {
45 public:
lower_ubo_reference_visitor(struct gl_shader * shader)46    lower_ubo_reference_visitor(struct gl_shader *shader)
47    : shader(shader)
48    {
49    }
50 
51    void handle_rvalue(ir_rvalue **rvalue);
52    void emit_ubo_loads(ir_dereference *deref, ir_variable *base_offset,
53 		       unsigned int deref_offset);
54    ir_expression *ubo_load(const struct glsl_type *type,
55 			   ir_rvalue *offset);
56 
57    void *mem_ctx;
58    struct gl_shader *shader;
59    struct gl_uniform_buffer_variable *ubo_var;
60    unsigned uniform_block;
61    bool progress;
62 };
63 
64 static inline unsigned int
align(unsigned int a,unsigned int align)65 align(unsigned int a, unsigned int align)
66 {
67    return (a + align - 1) / align * align;
68 }
69 
70 void
handle_rvalue(ir_rvalue ** rvalue)71 lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
72 {
73    if (!*rvalue)
74       return;
75 
76    ir_dereference *deref = (*rvalue)->as_dereference();
77    if (!deref)
78       return;
79 
80    ir_variable *var = deref->variable_referenced();
81    if (!var || var->uniform_block == -1)
82       return;
83 
84    mem_ctx = ralloc_parent(*rvalue);
85    uniform_block = var->uniform_block;
86    struct gl_uniform_block *block = &shader->UniformBlocks[uniform_block];
87    this->ubo_var = &block->Uniforms[var->location];
88    ir_rvalue *offset = new(mem_ctx) ir_constant(0u);
89    unsigned const_offset = 0;
90    bool row_major = ubo_var->RowMajor;
91 
92    /* Calculate the offset to the start of the region of the UBO
93     * dereferenced by *rvalue.  This may be a variable offset if an
94     * array dereference has a variable index.
95     */
96    while (deref) {
97       switch (deref->ir_type) {
98       case ir_type_dereference_variable: {
99 	 const_offset += ubo_var->Offset;
100 	 deref = NULL;
101 	 break;
102       }
103 
104       case ir_type_dereference_array: {
105 	 ir_dereference_array *deref_array = (ir_dereference_array *)deref;
106 	 unsigned array_stride;
107 	 if (deref_array->array->type->is_matrix() && row_major) {
108 	    /* When loading a vector out of a row major matrix, the
109 	     * step between the columns (vectors) is the size of a
110 	     * float, while the step between the rows (elements of a
111 	     * vector) is handled below in emit_ubo_loads.
112 	     */
113 	    array_stride = 4;
114 	 } else {
115 	    array_stride = deref_array->type->std140_size(row_major);
116 	    array_stride = align(array_stride, 16);
117 	 }
118 
119 	 ir_constant *const_index = deref_array->array_index->as_constant();
120 	 if (const_index) {
121 	    const_offset += array_stride * const_index->value.i[0];
122 	 } else {
123 	    offset = add(offset,
124 			 mul(deref_array->array_index,
125 			     new(mem_ctx) ir_constant(array_stride)));
126 	 }
127 	 deref = deref_array->array->as_dereference();
128 	 break;
129       }
130 
131       case ir_type_dereference_record: {
132 	 ir_dereference_record *deref_record = (ir_dereference_record *)deref;
133 	 const glsl_type *struct_type = deref_record->record->type;
134 	 unsigned intra_struct_offset = 0;
135 
136 	 unsigned max_field_align = 16;
137 	 for (unsigned int i = 0; i < struct_type->length; i++) {
138 	    const glsl_type *type = struct_type->fields.structure[i].type;
139 	    unsigned field_align = type->std140_base_alignment(row_major);
140 	    max_field_align = MAX2(field_align, max_field_align);
141 	    intra_struct_offset = align(intra_struct_offset, field_align);
142 
143 	    if (strcmp(struct_type->fields.structure[i].name,
144 		       deref_record->field) == 0)
145 	       break;
146 	    intra_struct_offset += type->std140_size(row_major);
147 	 }
148 
149 	 const_offset = align(const_offset, max_field_align);
150 	 const_offset += intra_struct_offset;
151 
152 	 deref = deref_record->record->as_dereference();
153 	 break;
154       }
155       default:
156 	 assert(!"not reached");
157 	 deref = NULL;
158 	 break;
159       }
160    }
161 
162    /* Now that we've calculated the offset to the start of the
163     * dereference, walk over the type and emit loads into a temporary.
164     */
165    const glsl_type *type = (*rvalue)->type;
166    ir_variable *load_var = new(mem_ctx) ir_variable(type,
167 						    "ubo_load_temp",
168 						    ir_var_temporary);
169    base_ir->insert_before(load_var);
170 
171    ir_variable *load_offset = new(mem_ctx) ir_variable(glsl_type::uint_type,
172 						       "ubo_load_temp_offset",
173 						       ir_var_temporary);
174    base_ir->insert_before(load_offset);
175    base_ir->insert_before(assign(load_offset, offset));
176 
177    deref = new(mem_ctx) ir_dereference_variable(load_var);
178    emit_ubo_loads(deref, load_offset, const_offset);
179    *rvalue = deref;
180 
181    progress = true;
182 }
183 
184 ir_expression *
ubo_load(const glsl_type * type,ir_rvalue * offset)185 lower_ubo_reference_visitor::ubo_load(const glsl_type *type,
186 				      ir_rvalue *offset)
187 {
188    return new(mem_ctx)
189       ir_expression(ir_binop_ubo_load,
190 		    type,
191 		    new(mem_ctx) ir_constant(this->uniform_block),
192 		    offset);
193 
194 }
195 
196 /**
197  * Takes LHS and emits a series of assignments into its components
198  * from the UBO variable at variable_offset + deref_offset.
199  *
200  * Recursively calls itself to break the deref down to the point that
201  * the ir_binop_ubo_load expressions generated are contiguous scalars
202  * or vectors.
203  */
204 void
emit_ubo_loads(ir_dereference * deref,ir_variable * base_offset,unsigned int deref_offset)205 lower_ubo_reference_visitor::emit_ubo_loads(ir_dereference *deref,
206 					    ir_variable *base_offset,
207 					    unsigned int deref_offset)
208 {
209    if (deref->type->is_record()) {
210       unsigned int field_offset = 0;
211 
212       for (unsigned i = 0; i < deref->type->length; i++) {
213 	 const struct glsl_struct_field *field =
214 	    &deref->type->fields.structure[i];
215 	 ir_dereference *field_deref =
216 	    new(mem_ctx) ir_dereference_record(deref->clone(mem_ctx, NULL),
217 					       field->name);
218 
219 	 field_offset =
220 	    align(field_offset,
221 		  field->type->std140_base_alignment(ubo_var->RowMajor));
222 
223 	 emit_ubo_loads(field_deref, base_offset, deref_offset + field_offset);
224 
225 	 field_offset += field->type->std140_size(ubo_var->RowMajor);
226       }
227       return;
228    }
229 
230    if (deref->type->is_array()) {
231       unsigned array_stride =
232 	 align(deref->type->fields.array->std140_size(ubo_var->RowMajor), 16);
233 
234       for (unsigned i = 0; i < deref->type->length; i++) {
235 	 ir_constant *element = new(mem_ctx) ir_constant(i);
236 	 ir_dereference *element_deref =
237 	    new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL),
238 					      element);
239 	 emit_ubo_loads(element_deref, base_offset,
240 			deref_offset + i * array_stride);
241       }
242       return;
243    }
244 
245    if (deref->type->is_matrix()) {
246       for (unsigned i = 0; i < deref->type->matrix_columns; i++) {
247 	 ir_constant *col = new(mem_ctx) ir_constant(i);
248 	 ir_dereference *col_deref =
249 	    new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL),
250 					      col);
251 
252 	 /* std140 always rounds the stride of arrays (and matrices)
253 	  * to a vec4, so matrices are always 16 between columns/rows.
254 	  */
255 	 emit_ubo_loads(col_deref, base_offset, deref_offset + i * 16);
256       }
257       return;
258    }
259 
260    assert(deref->type->is_scalar() ||
261 	  deref->type->is_vector());
262 
263    if (!ubo_var->RowMajor) {
264       ir_rvalue *offset = add(base_offset,
265 			      new(mem_ctx) ir_constant(deref_offset));
266       base_ir->insert_before(assign(deref->clone(mem_ctx, NULL),
267 				    ubo_load(deref->type, offset)));
268    } else {
269       /* We're dereffing a column out of a row-major matrix, so we
270        * gather the vector from each stored row.
271       */
272       assert(deref->type->base_type == GLSL_TYPE_FLOAT);
273       /* Matrices, row_major or not, are stored as if they were
274        * arrays of vectors of the appropriate size in std140.
275        * Arrays have their strides rounded up to a vec4, so the
276        * matrix stride is always 16.
277        */
278       unsigned matrix_stride = 16;
279 
280       for (unsigned i = 0; i < deref->type->vector_elements; i++) {
281 	 ir_rvalue *chan = new(mem_ctx) ir_constant((int)i);
282 	 ir_dereference *deref_chan =
283 	    new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL),
284 					      chan);
285 
286 	 ir_rvalue *chan_offset =
287 	    add(base_offset,
288 		new(mem_ctx) ir_constant(deref_offset + i * matrix_stride));
289 
290 	 base_ir->insert_before(assign(deref_chan,
291 				       ubo_load(glsl_type::float_type,
292 						chan_offset)));
293       }
294    }
295 }
296 
297 } /* unnamed namespace */
298 
299 void
lower_ubo_reference(struct gl_shader * shader,exec_list * instructions)300 lower_ubo_reference(struct gl_shader *shader, exec_list *instructions)
301 {
302    lower_ubo_reference_visitor v(shader);
303 
304    /* Loop over the instructions lowering references, because we take
305     * a deref of a UBO array using a UBO dereference as the index will
306     * produce a collection of instructions all of which have cloned
307     * UBO dereferences for that array index.
308     */
309    do {
310       v.progress = false;
311       visit_list_elements(&v, instructions);
312    } while (v.progress);
313 }
314