glsl: Use link_calculate_matrix_stride in lower_buffer_access and friends
[mesa.git] / src / compiler / glsl / lower_buffer_access.cpp
1 /*
2 * Copyright (c) 2015 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_buffer_access.cpp
26 *
27 * Helper for IR lowering pass to replace dereferences of buffer object based
28 * shader variables with intrinsic function calls.
29 *
30 * This helper is used by lowering passes for UBOs, SSBOs and compute shader
31 * shared variables.
32 */
33
34 #include "lower_buffer_access.h"
35 #include "ir_builder.h"
36 #include "main/macros.h"
37 #include "util/list.h"
38 #include "glsl_parser_extras.h"
39 #include "linker.h"
40
41 using namespace ir_builder;
42
43 namespace lower_buffer_access {
44
45 static inline int
46 writemask_for_size(unsigned n)
47 {
48 return ((1 << n) - 1);
49 }
50
51 /**
52 * Takes a deref and recursively calls itself to break the deref down to the
53 * point that the reads or writes generated are contiguous scalars or vectors.
54 */
55 void
56 lower_buffer_access::emit_access(void *mem_ctx,
57 bool is_write,
58 ir_dereference *deref,
59 ir_variable *base_offset,
60 unsigned int deref_offset,
61 bool row_major,
62 const glsl_type *matrix_type,
63 enum glsl_interface_packing packing,
64 unsigned int write_mask)
65 {
66 if (deref->type->is_record()) {
67 unsigned int field_offset = 0;
68
69 for (unsigned i = 0; i < deref->type->length; i++) {
70 const struct glsl_struct_field *field =
71 &deref->type->fields.structure[i];
72 ir_dereference *field_deref =
73 new(mem_ctx) ir_dereference_record(deref->clone(mem_ctx, NULL),
74 field->name);
75
76 field_offset =
77 glsl_align(field_offset,
78 field->type->std140_base_alignment(row_major));
79
80 emit_access(mem_ctx, is_write, field_deref, base_offset,
81 deref_offset + field_offset,
82 row_major, NULL, packing,
83 writemask_for_size(field_deref->type->vector_elements));
84
85 field_offset += field->type->std140_size(row_major);
86 }
87 return;
88 }
89
90 if (deref->type->is_array()) {
91 unsigned array_stride = packing == GLSL_INTERFACE_PACKING_STD430 ?
92 deref->type->fields.array->std430_array_stride(row_major) :
93 glsl_align(deref->type->fields.array->std140_size(row_major), 16);
94
95 for (unsigned i = 0; i < deref->type->length; i++) {
96 ir_constant *element = new(mem_ctx) ir_constant(i);
97 ir_dereference *element_deref =
98 new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL),
99 element);
100 emit_access(mem_ctx, is_write, element_deref, base_offset,
101 deref_offset + i * array_stride,
102 row_major, NULL, packing,
103 writemask_for_size(element_deref->type->vector_elements));
104 }
105 return;
106 }
107
108 if (deref->type->is_matrix()) {
109 for (unsigned i = 0; i < deref->type->matrix_columns; i++) {
110 ir_constant *col = new(mem_ctx) ir_constant(i);
111 ir_dereference *col_deref =
112 new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL), col);
113
114 if (row_major) {
115 /* For a row-major matrix, the next column starts at the next
116 * element.
117 */
118 int size_mul = deref->type->is_64bit() ? 8 : 4;
119 emit_access(mem_ctx, is_write, col_deref, base_offset,
120 deref_offset + i * size_mul,
121 row_major, deref->type, packing,
122 writemask_for_size(col_deref->type->vector_elements));
123 } else {
124 int size_mul;
125
126 /* std430 doesn't round up vec2 size to a vec4 size */
127 if (packing == GLSL_INTERFACE_PACKING_STD430 &&
128 deref->type->vector_elements == 2 &&
129 !deref->type->is_64bit()) {
130 size_mul = 8;
131 } else {
132 /* std140 always rounds the stride of arrays (and matrices) to a
133 * vec4, so matrices are always 16 between columns/rows. With
134 * doubles, they will be 32 apart when there are more than 2 rows.
135 *
136 * For both std140 and std430, if the member is a
137 * three-'component vector with components consuming N basic
138 * machine units, the base alignment is 4N. For vec4, base
139 * alignment is 4N.
140 */
141 size_mul = (deref->type->is_64bit() &&
142 deref->type->vector_elements > 2) ? 32 : 16;
143 }
144
145 emit_access(mem_ctx, is_write, col_deref, base_offset,
146 deref_offset + i * size_mul,
147 row_major, deref->type, packing,
148 writemask_for_size(col_deref->type->vector_elements));
149 }
150 }
151 return;
152 }
153
154 assert(deref->type->is_scalar() || deref->type->is_vector());
155
156 if (!row_major) {
157 ir_rvalue *offset =
158 add(base_offset, new(mem_ctx) ir_constant(deref_offset));
159 unsigned mask =
160 is_write ? write_mask : (1 << deref->type->vector_elements) - 1;
161 insert_buffer_access(mem_ctx, deref, deref->type, offset, mask, -1);
162 } else {
163 /* We're dereffing a column out of a row-major matrix, so we
164 * gather the vector from each stored row.
165 */
166 assert(deref->type->is_float() || deref->type->is_double());
167 assert(matrix_type != NULL);
168
169 const unsigned matrix_stride =
170 link_calculate_matrix_stride(matrix_type, row_major, packing);
171
172 const glsl_type *deref_type = deref->type->is_float() ?
173 glsl_type::float_type : glsl_type::double_type;
174
175 for (unsigned i = 0; i < deref->type->vector_elements; i++) {
176 ir_rvalue *chan_offset =
177 add(base_offset,
178 new(mem_ctx) ir_constant(deref_offset + i * matrix_stride));
179 if (!is_write || ((1U << i) & write_mask))
180 insert_buffer_access(mem_ctx, deref, deref_type, chan_offset,
181 (1U << i), i);
182 }
183 }
184 }
185
186 /**
187 * Determine if a thing being dereferenced is row-major
188 *
189 * There is some trickery here.
190 *
191 * If the thing being dereferenced is a member of uniform block \b without an
192 * instance name, then the name of the \c ir_variable is the field name of an
193 * interface type. If this field is row-major, then the thing referenced is
194 * row-major.
195 *
196 * If the thing being dereferenced is a member of uniform block \b with an
197 * instance name, then the last dereference in the tree will be an
198 * \c ir_dereference_record. If that record field is row-major, then the
199 * thing referenced is row-major.
200 */
201 bool
202 lower_buffer_access::is_dereferenced_thing_row_major(const ir_rvalue *deref)
203 {
204 bool matrix = false;
205 const ir_rvalue *ir = deref;
206
207 while (true) {
208 matrix = matrix || ir->type->without_array()->is_matrix();
209
210 switch (ir->ir_type) {
211 case ir_type_dereference_array: {
212 const ir_dereference_array *const array_deref =
213 (const ir_dereference_array *) ir;
214
215 ir = array_deref->array;
216 break;
217 }
218
219 case ir_type_dereference_record: {
220 const ir_dereference_record *const record_deref =
221 (const ir_dereference_record *) ir;
222
223 ir = record_deref->record;
224
225 const int idx = record_deref->field_idx;
226 assert(idx >= 0);
227
228 const enum glsl_matrix_layout matrix_layout =
229 glsl_matrix_layout(ir->type->fields.structure[idx].matrix_layout);
230
231 switch (matrix_layout) {
232 case GLSL_MATRIX_LAYOUT_INHERITED:
233 break;
234 case GLSL_MATRIX_LAYOUT_COLUMN_MAJOR:
235 return false;
236 case GLSL_MATRIX_LAYOUT_ROW_MAJOR:
237 return matrix || deref->type->without_array()->is_record();
238 }
239
240 break;
241 }
242
243 case ir_type_dereference_variable: {
244 const ir_dereference_variable *const var_deref =
245 (const ir_dereference_variable *) ir;
246
247 const enum glsl_matrix_layout matrix_layout =
248 glsl_matrix_layout(var_deref->var->data.matrix_layout);
249
250 switch (matrix_layout) {
251 case GLSL_MATRIX_LAYOUT_INHERITED: {
252 /* For interface block matrix variables we handle inherited
253 * layouts at HIR generation time, but we don't do that for shared
254 * variables, which are always column-major
255 */
256 MAYBE_UNUSED ir_variable *var = deref->variable_referenced();
257 assert((var->is_in_buffer_block() && !matrix) ||
258 var->data.mode == ir_var_shader_shared);
259 return false;
260 }
261 case GLSL_MATRIX_LAYOUT_COLUMN_MAJOR:
262 return false;
263 case GLSL_MATRIX_LAYOUT_ROW_MAJOR:
264 return matrix || deref->type->without_array()->is_record();
265 }
266
267 unreachable("invalid matrix layout");
268 break;
269 }
270
271 default:
272 return false;
273 }
274 }
275
276 /* The tree must have ended with a dereference that wasn't an
277 * ir_dereference_variable. That is invalid, and it should be impossible.
278 */
279 unreachable("invalid dereference tree");
280 return false;
281 }
282
283 /**
284 * This function initializes various values that will be used later by
285 * emit_access when actually emitting loads or stores.
286 *
287 * Note: const_offset is an input as well as an output, clients must
288 * initialize it to the offset of the variable in the underlying block, and
289 * this function will adjust it by adding the constant offset of the member
290 * being accessed into that variable.
291 */
292 void
293 lower_buffer_access::setup_buffer_access(void *mem_ctx,
294 ir_rvalue *deref,
295 ir_rvalue **offset,
296 unsigned *const_offset,
297 bool *row_major,
298 const glsl_type **matrix_type,
299 const glsl_struct_field **struct_field,
300 enum glsl_interface_packing packing)
301 {
302 *offset = new(mem_ctx) ir_constant(0u);
303 *row_major = is_dereferenced_thing_row_major(deref);
304 *matrix_type = NULL;
305
306 /* Calculate the offset to the start of the region of the UBO
307 * dereferenced by *rvalue. This may be a variable offset if an
308 * array dereference has a variable index.
309 */
310 while (deref) {
311 switch (deref->ir_type) {
312 case ir_type_dereference_variable: {
313 deref = NULL;
314 break;
315 }
316
317 case ir_type_dereference_array: {
318 ir_dereference_array *deref_array = (ir_dereference_array *) deref;
319 unsigned array_stride;
320 if (deref_array->array->type->is_vector()) {
321 /* We get this when storing or loading a component out of a vector
322 * with a non-constant index. This happens for v[i] = f where v is
323 * a vector (or m[i][j] = f where m is a matrix). If we don't
324 * lower that here, it gets turned into v = vector_insert(v, i,
325 * f), which loads the entire vector, modifies one component and
326 * then write the entire thing back. That breaks if another
327 * thread or SIMD channel is modifying the same vector.
328 */
329 array_stride = 4;
330 if (deref_array->array->type->is_64bit())
331 array_stride *= 2;
332 } else if (deref_array->array->type->is_matrix() && *row_major) {
333 /* When loading a vector out of a row major matrix, the
334 * step between the columns (vectors) is the size of a
335 * float, while the step between the rows (elements of a
336 * vector) is handled below in emit_ubo_loads.
337 */
338 array_stride = 4;
339 if (deref_array->array->type->is_64bit())
340 array_stride *= 2;
341 *matrix_type = deref_array->array->type;
342 } else if (deref_array->type->without_array()->is_interface()) {
343 /* We're processing an array dereference of an interface instance
344 * array. The thing being dereferenced *must* be a variable
345 * dereference because interfaces cannot be embedded in other
346 * types. In terms of calculating the offsets for the lowering
347 * pass, we don't care about the array index. All elements of an
348 * interface instance array will have the same offsets relative to
349 * the base of the block that backs them.
350 */
351 deref = deref_array->array->as_dereference();
352 break;
353 } else {
354 /* Whether or not the field is row-major (because it might be a
355 * bvec2 or something) does not affect the array itself. We need
356 * to know whether an array element in its entirety is row-major.
357 */
358 const bool array_row_major =
359 is_dereferenced_thing_row_major(deref_array);
360
361 /* The array type will give the correct interface packing
362 * information
363 */
364 if (packing == GLSL_INTERFACE_PACKING_STD430) {
365 array_stride = deref_array->type->std430_array_stride(array_row_major);
366 } else {
367 array_stride = deref_array->type->std140_size(array_row_major);
368 array_stride = glsl_align(array_stride, 16);
369 }
370 }
371
372 ir_rvalue *array_index = deref_array->array_index;
373 if (array_index->type->base_type == GLSL_TYPE_INT)
374 array_index = i2u(array_index);
375
376 ir_constant *const_index =
377 array_index->constant_expression_value(mem_ctx, NULL);
378 if (const_index) {
379 *const_offset += array_stride * const_index->value.u[0];
380 } else {
381 *offset = add(*offset,
382 mul(array_index,
383 new(mem_ctx) ir_constant(array_stride)));
384 }
385 deref = deref_array->array->as_dereference();
386 break;
387 }
388
389 case ir_type_dereference_record: {
390 ir_dereference_record *deref_record = (ir_dereference_record *) deref;
391 const glsl_type *struct_type = deref_record->record->type;
392 unsigned intra_struct_offset = 0;
393
394 for (unsigned int i = 0; i < struct_type->length; i++) {
395 const glsl_type *type = struct_type->fields.structure[i].type;
396
397 ir_dereference_record *field_deref = new(mem_ctx)
398 ir_dereference_record(deref_record->record,
399 struct_type->fields.structure[i].name);
400 const bool field_row_major =
401 is_dereferenced_thing_row_major(field_deref);
402
403 ralloc_free(field_deref);
404
405 unsigned field_align = 0;
406
407 if (packing == GLSL_INTERFACE_PACKING_STD430)
408 field_align = type->std430_base_alignment(field_row_major);
409 else
410 field_align = type->std140_base_alignment(field_row_major);
411
412 if (struct_type->fields.structure[i].offset != -1) {
413 intra_struct_offset = struct_type->fields.structure[i].offset;
414 }
415
416 intra_struct_offset = glsl_align(intra_struct_offset, field_align);
417
418 assert(deref_record->field_idx >= 0);
419 if (i == (unsigned) deref_record->field_idx) {
420 if (struct_field)
421 *struct_field = &struct_type->fields.structure[i];
422 break;
423 }
424
425 if (packing == GLSL_INTERFACE_PACKING_STD430)
426 intra_struct_offset += type->std430_size(field_row_major);
427 else
428 intra_struct_offset += type->std140_size(field_row_major);
429
430 /* If the field just examined was itself a structure, apply rule
431 * #9:
432 *
433 * "The structure may have padding at the end; the base offset
434 * of the member following the sub-structure is rounded up to
435 * the next multiple of the base alignment of the structure."
436 */
437 if (type->without_array()->is_record()) {
438 intra_struct_offset = glsl_align(intra_struct_offset,
439 field_align);
440
441 }
442 }
443
444 *const_offset += intra_struct_offset;
445 deref = deref_record->record->as_dereference();
446 break;
447 }
448
449 case ir_type_swizzle: {
450 ir_swizzle *deref_swizzle = (ir_swizzle *) deref;
451
452 assert(deref_swizzle->mask.num_components == 1);
453
454 *const_offset += deref_swizzle->mask.x * sizeof(int);
455 deref = deref_swizzle->val->as_dereference();
456 break;
457 }
458
459 default:
460 assert(!"not reached");
461 deref = NULL;
462 break;
463 }
464 }
465 }
466
467 } /* namespace lower_buffer_access */