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"
40 using namespace ir_builder
;
42 namespace lower_buffer_access
{
45 writemask_for_size(unsigned n
)
47 return ((1 << n
) - 1);
51 * Takes a deref and recursively calls itself to break the deref down to the
52 * point that the reads or writes generated are contiguous scalars or vectors.
55 lower_buffer_access::emit_access(void *mem_ctx
,
57 ir_dereference
*deref
,
58 ir_variable
*base_offset
,
59 unsigned int deref_offset
,
63 unsigned int write_mask
)
65 if (deref
->type
->is_record()) {
66 unsigned int field_offset
= 0;
68 for (unsigned i
= 0; i
< deref
->type
->length
; i
++) {
69 const struct glsl_struct_field
*field
=
70 &deref
->type
->fields
.structure
[i
];
71 ir_dereference
*field_deref
=
72 new(mem_ctx
) ir_dereference_record(deref
->clone(mem_ctx
, NULL
),
76 glsl_align(field_offset
,
77 field
->type
->std140_base_alignment(row_major
));
79 emit_access(mem_ctx
, is_write
, field_deref
, base_offset
,
80 deref_offset
+ field_offset
,
81 row_major
, 1, packing
,
82 writemask_for_size(field_deref
->type
->vector_elements
));
84 field_offset
+= field
->type
->std140_size(row_major
);
89 if (deref
->type
->is_array()) {
90 unsigned array_stride
= packing
== GLSL_INTERFACE_PACKING_STD430
?
91 deref
->type
->fields
.array
->std430_array_stride(row_major
) :
92 glsl_align(deref
->type
->fields
.array
->std140_size(row_major
), 16);
94 for (unsigned i
= 0; i
< deref
->type
->length
; i
++) {
95 ir_constant
*element
= new(mem_ctx
) ir_constant(i
);
96 ir_dereference
*element_deref
=
97 new(mem_ctx
) ir_dereference_array(deref
->clone(mem_ctx
, NULL
),
99 emit_access(mem_ctx
, is_write
, element_deref
, base_offset
,
100 deref_offset
+ i
* array_stride
,
101 row_major
, 1, packing
,
102 writemask_for_size(element_deref
->type
->vector_elements
));
107 if (deref
->type
->is_matrix()) {
108 for (unsigned i
= 0; i
< deref
->type
->matrix_columns
; i
++) {
109 ir_constant
*col
= new(mem_ctx
) ir_constant(i
);
110 ir_dereference
*col_deref
=
111 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
117 int size_mul
= deref
->type
->is_double() ? 8 : 4;
118 emit_access(mem_ctx
, is_write
, col_deref
, base_offset
,
119 deref_offset
+ i
* size_mul
,
120 row_major
, deref
->type
->matrix_columns
, packing
,
121 writemask_for_size(col_deref
->type
->vector_elements
));
125 /* std430 doesn't round up vec2 size to a vec4 size */
126 if (packing
== GLSL_INTERFACE_PACKING_STD430
&&
127 deref
->type
->vector_elements
== 2 &&
128 !deref
->type
->is_double()) {
131 /* std140 always rounds the stride of arrays (and matrices) to a
132 * vec4, so matrices are always 16 between columns/rows. With
133 * doubles, they will be 32 apart when there are more than 2 rows.
135 * For both std140 and std430, if the member is a
136 * three-'component vector with components consuming N basic
137 * machine units, the base alignment is 4N. For vec4, base
140 size_mul
= (deref
->type
->is_double() &&
141 deref
->type
->vector_elements
> 2) ? 32 : 16;
144 emit_access(mem_ctx
, is_write
, col_deref
, base_offset
,
145 deref_offset
+ i
* size_mul
,
146 row_major
, deref
->type
->matrix_columns
, packing
,
147 writemask_for_size(col_deref
->type
->vector_elements
));
153 assert(deref
->type
->is_scalar() || deref
->type
->is_vector());
157 add(base_offset
, new(mem_ctx
) ir_constant(deref_offset
));
159 is_write
? write_mask
: (1 << deref
->type
->vector_elements
) - 1;
160 insert_buffer_access(mem_ctx
, deref
, deref
->type
, offset
, mask
, -1);
162 unsigned N
= deref
->type
->is_double() ? 8 : 4;
164 /* We're dereffing a column out of a row-major matrix, so we
165 * gather the vector from each stored row.
167 assert(deref
->type
->base_type
== GLSL_TYPE_FLOAT
||
168 deref
->type
->base_type
== GLSL_TYPE_DOUBLE
);
169 /* Matrices, row_major or not, are stored as if they were
170 * arrays of vectors of the appropriate size in std140.
171 * Arrays have their strides rounded up to a vec4, so the
172 * matrix stride is always 16. However a double matrix may either be 16
173 * or 32 depending on the number of columns.
175 assert(matrix_columns
<= 4);
176 unsigned matrix_stride
= 0;
177 /* Matrix stride for std430 mat2xY matrices are not rounded up to
178 * vec4 size. From OpenGL 4.3 spec, section 7.6.2.2 "Standard Uniform
181 * "2. If the member is a two- or four-component vector with components
182 * consuming N basic machine units, the base alignment is 2N or 4N,
183 * respectively." [...]
184 * "4. If the member is an array of scalars or vectors, the base alignment
185 * and array stride are set to match the base alignment of a single array
186 * element, according to rules (1), (2), and (3), and rounded up to the
187 * base alignment of a vec4." [...]
188 * "7. If the member is a row-major matrix with C columns and R rows, the
189 * matrix is stored identically to an array of R row vectors with C
190 * components each, according to rule (4)." [...]
191 * "When using the std430 storage layout, shader storage blocks will be
192 * laid out in buffer storage identically to uniform and shader storage
193 * blocks using the std140 layout, except that the base alignment and
194 * stride of arrays of scalars and vectors in rule 4 and of structures in
195 * rule 9 are not rounded up a multiple of the base alignment of a vec4."
197 if (packing
== GLSL_INTERFACE_PACKING_STD430
&& matrix_columns
== 2)
198 matrix_stride
= 2 * N
;
200 matrix_stride
= glsl_align(matrix_columns
* N
, 16);
202 const glsl_type
*deref_type
= deref
->type
->base_type
== GLSL_TYPE_FLOAT
?
203 glsl_type::float_type
: glsl_type::double_type
;
205 for (unsigned i
= 0; i
< deref
->type
->vector_elements
; i
++) {
206 ir_rvalue
*chan_offset
=
208 new(mem_ctx
) ir_constant(deref_offset
+ i
* matrix_stride
));
209 if (!is_write
|| ((1U << i
) & write_mask
))
210 insert_buffer_access(mem_ctx
, deref
, deref_type
, chan_offset
,
217 * Determine if a thing being dereferenced is row-major
219 * There is some trickery here.
221 * If the thing being dereferenced is a member of uniform block \b without an
222 * instance name, then the name of the \c ir_variable is the field name of an
223 * interface type. If this field is row-major, then the thing referenced is
226 * If the thing being dereferenced is a member of uniform block \b with an
227 * instance name, then the last dereference in the tree will be an
228 * \c ir_dereference_record. If that record field is row-major, then the
229 * thing referenced is row-major.
232 lower_buffer_access::is_dereferenced_thing_row_major(const ir_rvalue
*deref
)
235 const ir_rvalue
*ir
= deref
;
238 matrix
= matrix
|| ir
->type
->without_array()->is_matrix();
240 switch (ir
->ir_type
) {
241 case ir_type_dereference_array
: {
242 const ir_dereference_array
*const array_deref
=
243 (const ir_dereference_array
*) ir
;
245 ir
= array_deref
->array
;
249 case ir_type_dereference_record
: {
250 const ir_dereference_record
*const record_deref
=
251 (const ir_dereference_record
*) ir
;
253 ir
= record_deref
->record
;
255 const int idx
= ir
->type
->field_index(record_deref
->field
);
258 const enum glsl_matrix_layout matrix_layout
=
259 glsl_matrix_layout(ir
->type
->fields
.structure
[idx
].matrix_layout
);
261 switch (matrix_layout
) {
262 case GLSL_MATRIX_LAYOUT_INHERITED
:
264 case GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
:
266 case GLSL_MATRIX_LAYOUT_ROW_MAJOR
:
267 return matrix
|| deref
->type
->without_array()->is_record();
273 case ir_type_dereference_variable
: {
274 const ir_dereference_variable
*const var_deref
=
275 (const ir_dereference_variable
*) ir
;
277 const enum glsl_matrix_layout matrix_layout
=
278 glsl_matrix_layout(var_deref
->var
->data
.matrix_layout
);
280 switch (matrix_layout
) {
281 case GLSL_MATRIX_LAYOUT_INHERITED
:
284 case GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
:
286 case GLSL_MATRIX_LAYOUT_ROW_MAJOR
:
287 return matrix
|| deref
->type
->without_array()->is_record();
290 unreachable("invalid matrix layout");
299 /* The tree must have ended with a dereference that wasn't an
300 * ir_dereference_variable. That is invalid, and it should be impossible.
302 unreachable("invalid dereference tree");
306 } /* namespace lower_buffer_access */