2 * Copyright (c) 2015 Intel Corporation
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:
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
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.
25 * \file lower_buffer_access.cpp
27 * Helper for IR lowering pass to replace dereferences of buffer object based
28 * shader variables with intrinsic function calls.
30 * This helper is used by lowering passes for UBOs, SSBOs and compute shader
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"
41 using namespace ir_builder
;
43 namespace lower_buffer_access
{
46 writemask_for_size(unsigned n
)
48 return ((1 << n
) - 1);
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.
56 lower_buffer_access::emit_access(void *mem_ctx
,
58 ir_dereference
*deref
,
59 ir_variable
*base_offset
,
60 unsigned int deref_offset
,
62 const glsl_type
*matrix_type
,
63 enum glsl_interface_packing packing
,
64 unsigned int write_mask
)
66 if (deref
->type
->is_record()) {
67 unsigned int field_offset
= 0;
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
),
77 glsl_align(field_offset
,
78 field
->type
->std140_base_alignment(row_major
));
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
));
85 field_offset
+= field
->type
->std140_size(row_major
);
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);
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
),
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
));
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
);
114 /* For a row-major matrix, the next column starts at the next
115 * element. Otherwise it is offset by the matrix stride.
117 const unsigned size_mul
= row_major
118 ? (deref
->type
->is_double() ? 8 : 4)
119 : link_calculate_matrix_stride(deref
->type
, row_major
, packing
);
121 emit_access(mem_ctx
, is_write
, col_deref
, base_offset
,
122 deref_offset
+ i
* size_mul
,
123 row_major
, deref
->type
, packing
,
124 writemask_for_size(col_deref
->type
->vector_elements
));
129 assert(deref
->type
->is_scalar() || deref
->type
->is_vector());
133 add(base_offset
, new(mem_ctx
) ir_constant(deref_offset
));
135 is_write
? write_mask
: (1 << deref
->type
->vector_elements
) - 1;
136 insert_buffer_access(mem_ctx
, deref
, deref
->type
, offset
, mask
, -1);
138 /* We're dereffing a column out of a row-major matrix, so we
139 * gather the vector from each stored row.
141 assert(deref
->type
->is_float() || deref
->type
->is_double());
142 assert(matrix_type
!= NULL
);
144 const unsigned matrix_stride
=
145 link_calculate_matrix_stride(matrix_type
, row_major
, packing
);
147 const glsl_type
*deref_type
= deref
->type
->get_scalar_type();
149 for (unsigned i
= 0; i
< deref
->type
->vector_elements
; i
++) {
150 ir_rvalue
*chan_offset
=
152 new(mem_ctx
) ir_constant(deref_offset
+ i
* matrix_stride
));
153 if (!is_write
|| ((1U << i
) & write_mask
))
154 insert_buffer_access(mem_ctx
, deref
, deref_type
, chan_offset
,
161 * Determine if a thing being dereferenced is row-major
163 * There is some trickery here.
165 * If the thing being dereferenced is a member of uniform block \b without an
166 * instance name, then the name of the \c ir_variable is the field name of an
167 * interface type. If this field is row-major, then the thing referenced is
170 * If the thing being dereferenced is a member of uniform block \b with an
171 * instance name, then the last dereference in the tree will be an
172 * \c ir_dereference_record. If that record field is row-major, then the
173 * thing referenced is row-major.
176 lower_buffer_access::is_dereferenced_thing_row_major(const ir_rvalue
*deref
)
179 const ir_rvalue
*ir
= deref
;
182 matrix
= matrix
|| ir
->type
->without_array()->is_matrix();
184 switch (ir
->ir_type
) {
185 case ir_type_dereference_array
: {
186 const ir_dereference_array
*const array_deref
=
187 (const ir_dereference_array
*) ir
;
189 ir
= array_deref
->array
;
193 case ir_type_dereference_record
: {
194 const ir_dereference_record
*const record_deref
=
195 (const ir_dereference_record
*) ir
;
197 ir
= record_deref
->record
;
199 const int idx
= record_deref
->field_idx
;
202 const enum glsl_matrix_layout matrix_layout
=
203 glsl_matrix_layout(ir
->type
->fields
.structure
[idx
].matrix_layout
);
205 switch (matrix_layout
) {
206 case GLSL_MATRIX_LAYOUT_INHERITED
:
208 case GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
:
210 case GLSL_MATRIX_LAYOUT_ROW_MAJOR
:
211 return matrix
|| deref
->type
->without_array()->is_record();
217 case ir_type_dereference_variable
: {
218 const ir_dereference_variable
*const var_deref
=
219 (const ir_dereference_variable
*) ir
;
221 const enum glsl_matrix_layout matrix_layout
=
222 glsl_matrix_layout(var_deref
->var
->data
.matrix_layout
);
224 switch (matrix_layout
) {
225 case GLSL_MATRIX_LAYOUT_INHERITED
: {
226 /* For interface block matrix variables we handle inherited
227 * layouts at HIR generation time, but we don't do that for shared
228 * variables, which are always column-major
230 MAYBE_UNUSED ir_variable
*var
= deref
->variable_referenced();
231 assert((var
->is_in_buffer_block() && !matrix
) ||
232 var
->data
.mode
== ir_var_shader_shared
);
235 case GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
:
237 case GLSL_MATRIX_LAYOUT_ROW_MAJOR
:
238 return matrix
|| deref
->type
->without_array()->is_record();
241 unreachable("invalid matrix layout");
250 /* The tree must have ended with a dereference that wasn't an
251 * ir_dereference_variable. That is invalid, and it should be impossible.
253 unreachable("invalid dereference tree");
258 * This function initializes various values that will be used later by
259 * emit_access when actually emitting loads or stores.
261 * Note: const_offset is an input as well as an output, clients must
262 * initialize it to the offset of the variable in the underlying block, and
263 * this function will adjust it by adding the constant offset of the member
264 * being accessed into that variable.
267 lower_buffer_access::setup_buffer_access(void *mem_ctx
,
270 unsigned *const_offset
,
272 const glsl_type
**matrix_type
,
273 const glsl_struct_field
**struct_field
,
274 enum glsl_interface_packing packing
)
276 *offset
= new(mem_ctx
) ir_constant(0u);
277 *row_major
= is_dereferenced_thing_row_major(deref
);
280 /* Calculate the offset to the start of the region of the UBO
281 * dereferenced by *rvalue. This may be a variable offset if an
282 * array dereference has a variable index.
285 switch (deref
->ir_type
) {
286 case ir_type_dereference_variable
: {
291 case ir_type_dereference_array
: {
292 ir_dereference_array
*deref_array
= (ir_dereference_array
*) deref
;
293 unsigned array_stride
;
294 if (deref_array
->array
->type
->is_vector()) {
295 /* We get this when storing or loading a component out of a vector
296 * with a non-constant index. This happens for v[i] = f where v is
297 * a vector (or m[i][j] = f where m is a matrix). If we don't
298 * lower that here, it gets turned into v = vector_insert(v, i,
299 * f), which loads the entire vector, modifies one component and
300 * then write the entire thing back. That breaks if another
301 * thread or SIMD channel is modifying the same vector.
304 if (deref_array
->array
->type
->is_64bit())
306 } else if (deref_array
->array
->type
->is_matrix() && *row_major
) {
307 /* When loading a vector out of a row major matrix, the
308 * step between the columns (vectors) is the size of a
309 * float, while the step between the rows (elements of a
310 * vector) is handled below in emit_ubo_loads.
313 if (deref_array
->array
->type
->is_64bit())
315 *matrix_type
= deref_array
->array
->type
;
316 } else if (deref_array
->type
->without_array()->is_interface()) {
317 /* We're processing an array dereference of an interface instance
318 * array. The thing being dereferenced *must* be a variable
319 * dereference because interfaces cannot be embedded in other
320 * types. In terms of calculating the offsets for the lowering
321 * pass, we don't care about the array index. All elements of an
322 * interface instance array will have the same offsets relative to
323 * the base of the block that backs them.
325 deref
= deref_array
->array
->as_dereference();
328 /* Whether or not the field is row-major (because it might be a
329 * bvec2 or something) does not affect the array itself. We need
330 * to know whether an array element in its entirety is row-major.
332 const bool array_row_major
=
333 is_dereferenced_thing_row_major(deref_array
);
335 /* The array type will give the correct interface packing
338 if (packing
== GLSL_INTERFACE_PACKING_STD430
) {
339 array_stride
= deref_array
->type
->std430_array_stride(array_row_major
);
341 array_stride
= deref_array
->type
->std140_size(array_row_major
);
342 array_stride
= glsl_align(array_stride
, 16);
346 ir_rvalue
*array_index
= deref_array
->array_index
;
347 if (array_index
->type
->base_type
== GLSL_TYPE_INT
)
348 array_index
= i2u(array_index
);
350 ir_constant
*const_index
=
351 array_index
->constant_expression_value(mem_ctx
, NULL
);
353 *const_offset
+= array_stride
* const_index
->value
.u
[0];
355 *offset
= add(*offset
,
357 new(mem_ctx
) ir_constant(array_stride
)));
359 deref
= deref_array
->array
->as_dereference();
363 case ir_type_dereference_record
: {
364 ir_dereference_record
*deref_record
= (ir_dereference_record
*) deref
;
365 const glsl_type
*struct_type
= deref_record
->record
->type
;
366 unsigned intra_struct_offset
= 0;
368 for (unsigned int i
= 0; i
< struct_type
->length
; i
++) {
369 const glsl_type
*type
= struct_type
->fields
.structure
[i
].type
;
371 ir_dereference_record
*field_deref
= new(mem_ctx
)
372 ir_dereference_record(deref_record
->record
,
373 struct_type
->fields
.structure
[i
].name
);
374 const bool field_row_major
=
375 is_dereferenced_thing_row_major(field_deref
);
377 ralloc_free(field_deref
);
379 unsigned field_align
= 0;
381 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
382 field_align
= type
->std430_base_alignment(field_row_major
);
384 field_align
= type
->std140_base_alignment(field_row_major
);
386 if (struct_type
->fields
.structure
[i
].offset
!= -1) {
387 intra_struct_offset
= struct_type
->fields
.structure
[i
].offset
;
390 intra_struct_offset
= glsl_align(intra_struct_offset
, field_align
);
392 assert(deref_record
->field_idx
>= 0);
393 if (i
== (unsigned) deref_record
->field_idx
) {
395 *struct_field
= &struct_type
->fields
.structure
[i
];
399 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
400 intra_struct_offset
+= type
->std430_size(field_row_major
);
402 intra_struct_offset
+= type
->std140_size(field_row_major
);
404 /* If the field just examined was itself a structure, apply rule
407 * "The structure may have padding at the end; the base offset
408 * of the member following the sub-structure is rounded up to
409 * the next multiple of the base alignment of the structure."
411 if (type
->without_array()->is_record()) {
412 intra_struct_offset
= glsl_align(intra_struct_offset
,
418 *const_offset
+= intra_struct_offset
;
419 deref
= deref_record
->record
->as_dereference();
423 case ir_type_swizzle
: {
424 ir_swizzle
*deref_swizzle
= (ir_swizzle
*) deref
;
426 assert(deref_swizzle
->mask
.num_components
== 1);
428 *const_offset
+= deref_swizzle
->mask
.x
* sizeof(int);
429 deref
= deref_swizzle
->val
->as_dereference();
434 assert(!"not reached");
441 } /* namespace lower_buffer_access */