2 * Copyright © 2019 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 DEALINGS
25 #include "gl_nir_linker.h"
26 #include "ir_uniform.h" /* for gl_uniform_storage */
27 #include "linker_util.h"
28 #include "main/mtypes.h"
30 /* Summary: This file contains code to do a nir-based linking for uniform
31 * blocks. This includes ubos and ssbos. Note that it is tailored to
32 * ARB_gl_spirv needs and particularities.
36 * 1. Linking doesn't use names: GLSL linking use names as core concept. But
37 * on SPIR-V, uniform block name, fields names, and other names are
38 * considered optional debug infor so could not be present. So the linking
39 * should work without it, and it is optional to not handle them at
40 * all. From ARB_gl_spirv spec.
42 * "19. How should the program interface query operations behave for program
43 * objects created from SPIR-V shaders?
45 * DISCUSSION: we previously said we didn't need reflection to work for
46 * SPIR-V shaders (at least for the first version), however we are left
47 * with specifying how it should "not work". The primary issue is that
48 * SPIR-V binaries are not required to have names associated with
49 * variables. They can be associated in debug information, but there is no
50 * requirement for that to be present, and it should not be relied upon.
56 * C) Allow as much as possible to work "naturally". You can query for the
57 * number of active resources, and for details about them. Anything that
58 * doesn't query by name will work as expected. Queries for maximum length
59 * of names return one. Queries for anything "by name" return INVALID_INDEX
60 * (or -1). Querying the name property of a resource returns an empty
61 * string. This may allow many queries to work, but it's not clear how
62 * useful it would be if you can't actually know which specific variable
63 * you are retrieving information on. If everything is specified a-priori
64 * by location/binding/offset/index/component in the shader, this may be
67 * RESOLVED. Pick (c), but also allow debug names to be returned if an
68 * implementation wants to."
70 * This implemention doesn't care for the names, as the main objective is
71 * functional, and not support optional debug features.
73 * 2. Terminology: this file handles both UBO and SSBO, including both as
74 * "uniform blocks" analogously to what is done in the GLSL (IR) path.
76 * From ARB_gl_spirv spec:
77 * "Mapping of Storage Classes:
79 * uniform blockN { ... } ...; -> Uniform, with Block decoration
81 * buffer blockN { ... } ...; -> Uniform, with BufferBlock decoration"
83 * 3. Explicit data: The code assumes that all structure members have an
84 * Offset decoration, all arrays have an ArrayStride and all matrices have
85 * a MatrixStride, even for nested structures. That way we don’t have to
86 * worry about the different layout modes. This is explicitly required in
89 * "Composite objects in the UniformConstant, Uniform, and PushConstant
90 * Storage Classes must be explicitly laid out. The following apply to all
91 * the aggregate and matrix types describing such an object, recursively
92 * through their nested types:
94 * – Each structure-type member must have an Offset Decoration.
95 * – Each array type must have an ArrayStride Decoration.
96 * – Each structure-type member that is a matrix or array-of-matrices must
97 * have be decorated with a MatrixStride Decoration, and one of the
98 * RowMajor or ColMajor Decorations."
100 * Additionally, the structure members are expected to be presented in
101 * increasing offset order:
103 * "a structure has lower-numbered members appearing at smaller offsets than
104 * higher-numbered members"
113 * It is worth to note that ARB_gl_spirv spec doesn't require us to do this
114 * validation, but at the same time, it allow us to do it. The following
115 * validation is easy and a nice-to-have.
118 link_blocks_are_compatible(const struct gl_uniform_block
*a
,
119 const struct gl_uniform_block
*b
)
122 * Names on ARB_gl_spirv are optional, so we are ignoring them. So
123 * meanwhile on the equivalent GLSL method the matching is done using the
124 * name, here we use the binding, that for SPIR-V binaries is explicit, and
125 * mandatory, from OpenGL 4.6 spec, section "7.4.2. SPIR-V Shader Interface
127 * "Uniform and shader storage block variables must also be decorated
130 if (a
->Binding
!= b
->Binding
)
133 /* We are explicitly ignoring the names, so it would be good to check that
136 assert(a
->Name
== NULL
);
137 assert(b
->Name
== NULL
);
139 if (a
->NumUniforms
!= b
->NumUniforms
)
142 if (a
->_Packing
!= b
->_Packing
)
145 if (a
->_RowMajor
!= b
->_RowMajor
)
148 for (unsigned i
= 0; i
< a
->NumUniforms
; i
++) {
149 if (a
->Uniforms
[i
].Type
!= b
->Uniforms
[i
].Type
)
152 if (a
->Uniforms
[i
].RowMajor
!= b
->Uniforms
[i
].RowMajor
)
155 if (a
->Uniforms
[i
].Offset
!= b
->Uniforms
[i
].Offset
)
158 /* See comment on previous assert */
159 assert(a
->Uniforms
[i
].Name
== NULL
);
160 assert(b
->Uniforms
[i
].Name
== NULL
);
167 * Merges a buffer block into an array of buffer blocks that may or may not
168 * already contain a copy of it.
170 * Returns the index of the block in the array (new if it was needed, or the
171 * index of the copy of it). -1 if there are two incompatible block
172 * definitions with the same binding.
176 link_cross_validate_uniform_block(void *mem_ctx
,
177 struct gl_uniform_block
**linked_blocks
,
178 unsigned int *num_linked_blocks
,
179 struct gl_uniform_block
*new_block
)
181 /* We first check if new_block was already linked */
182 for (unsigned int i
= 0; i
< *num_linked_blocks
; i
++) {
183 struct gl_uniform_block
*old_block
= &(*linked_blocks
)[i
];
185 if (old_block
->Binding
== new_block
->Binding
)
186 return link_blocks_are_compatible(old_block
, new_block
) ? i
: -1;
189 *linked_blocks
= reralloc(mem_ctx
, *linked_blocks
,
190 struct gl_uniform_block
,
191 *num_linked_blocks
+ 1);
192 int linked_block_index
= (*num_linked_blocks
)++;
193 struct gl_uniform_block
*linked_block
= &(*linked_blocks
)[linked_block_index
];
195 memcpy(linked_block
, new_block
, sizeof(*new_block
));
196 linked_block
->Uniforms
= ralloc_array(*linked_blocks
,
197 struct gl_uniform_buffer_variable
,
198 linked_block
->NumUniforms
);
200 memcpy(linked_block
->Uniforms
,
202 sizeof(*linked_block
->Uniforms
) * linked_block
->NumUniforms
);
204 return linked_block_index
;
209 * Accumulates the array of buffer blocks and checks that all definitions of
210 * blocks agree on their contents.
213 nir_interstage_cross_validate_uniform_blocks(struct gl_shader_program
*prog
,
214 enum block_type block_type
)
216 int *interfaceBlockStageIndex
[MESA_SHADER_STAGES
];
217 struct gl_uniform_block
*blks
= NULL
;
218 unsigned *num_blks
= block_type
== BLOCK_SSBO
? &prog
->data
->NumShaderStorageBlocks
:
219 &prog
->data
->NumUniformBlocks
;
221 unsigned max_num_buffer_blocks
= 0;
222 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
223 if (prog
->_LinkedShaders
[i
]) {
224 if (block_type
== BLOCK_SSBO
) {
225 max_num_buffer_blocks
+=
226 prog
->_LinkedShaders
[i
]->Program
->info
.num_ssbos
;
228 max_num_buffer_blocks
+=
229 prog
->_LinkedShaders
[i
]->Program
->info
.num_ubos
;
234 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
235 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
237 interfaceBlockStageIndex
[i
] = malloc(max_num_buffer_blocks
* sizeof(int));
238 for (unsigned int j
= 0; j
< max_num_buffer_blocks
; j
++)
239 interfaceBlockStageIndex
[i
][j
] = -1;
244 unsigned sh_num_blocks
;
245 struct gl_uniform_block
**sh_blks
;
246 if (block_type
== BLOCK_SSBO
) {
247 sh_num_blocks
= prog
->_LinkedShaders
[i
]->Program
->info
.num_ssbos
;
248 sh_blks
= sh
->Program
->sh
.ShaderStorageBlocks
;
250 sh_num_blocks
= prog
->_LinkedShaders
[i
]->Program
->info
.num_ubos
;
251 sh_blks
= sh
->Program
->sh
.UniformBlocks
;
254 for (unsigned int j
= 0; j
< sh_num_blocks
; j
++) {
255 int index
= link_cross_validate_uniform_block(prog
->data
, &blks
,
256 num_blks
, sh_blks
[j
]);
259 /* We use the binding as we are ignoring the names */
260 linker_error(prog
, "buffer block with binding `%i' has mismatching "
261 "definitions\n", sh_blks
[j
]->Binding
);
263 for (unsigned k
= 0; k
<= i
; k
++) {
264 free(interfaceBlockStageIndex
[k
]);
267 /* Reset the block count. This will help avoid various segfaults
268 * from api calls that assume the array exists due to the count
275 interfaceBlockStageIndex
[i
][index
] = j
;
279 /* Update per stage block pointers to point to the program list.
281 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
282 for (unsigned j
= 0; j
< *num_blks
; j
++) {
283 int stage_index
= interfaceBlockStageIndex
[i
][j
];
285 if (stage_index
!= -1) {
286 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
288 struct gl_uniform_block
**sh_blks
= block_type
== BLOCK_SSBO
?
289 sh
->Program
->sh
.ShaderStorageBlocks
:
290 sh
->Program
->sh
.UniformBlocks
;
292 blks
[j
].stageref
|= sh_blks
[stage_index
]->stageref
;
293 sh_blks
[stage_index
] = &blks
[j
];
298 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
299 free(interfaceBlockStageIndex
[i
]);
302 if (block_type
== BLOCK_SSBO
)
303 prog
->data
->ShaderStorageBlocks
= blks
;
305 prog
->data
->UniformBlocks
= blks
;
311 * Iterates @type in order to compute how many individual leaf variables
315 iterate_type_count_variables(const struct glsl_type
*type
,
316 unsigned int *num_variables
)
318 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
319 const struct glsl_type
*field_type
;
321 if (glsl_type_is_struct_or_ifc(type
))
322 field_type
= glsl_get_struct_field(type
, i
);
324 field_type
= glsl_get_array_element(type
);
326 if (glsl_type_is_leaf(field_type
))
329 iterate_type_count_variables(field_type
, num_variables
);
335 fill_individual_variable(const struct glsl_type
*type
,
336 struct gl_uniform_buffer_variable
*variables
,
337 unsigned int *variable_index
,
338 unsigned int *offset
,
339 struct gl_shader_program
*prog
,
340 struct gl_uniform_block
*block
)
342 /* ARB_gl_spirv: allowed to ignore names. Thus, we don't need to initialize
343 * the variable's Name or IndexName.
345 variables
[*variable_index
].Type
= type
;
347 if (glsl_type_is_matrix(type
)) {
348 variables
[*variable_index
].RowMajor
= glsl_matrix_type_is_row_major(type
);
350 /* default value, better that potential meaningless garbage */
351 variables
[*variable_index
].RowMajor
= false;
355 * Although ARB_gl_spirv points that the offsets need to be included (see
356 * "Mappings of layouts"), in the end those are only valid for
357 * root-variables, and we would need to recompute offsets when we iterate
358 * over non-trivial types, like aoa. So we compute the offset always.
360 variables
[*variable_index
].Offset
= *offset
;
361 (*offset
) += glsl_get_explicit_size(type
, true);
367 iterate_type_fill_variables(const struct glsl_type
*type
,
368 struct gl_uniform_buffer_variable
*variables
,
369 unsigned int *variable_index
,
370 unsigned int *offset
,
371 struct gl_shader_program
*prog
,
372 struct gl_uniform_block
*block
)
374 unsigned int struct_base_offset
;
376 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
377 const struct glsl_type
*field_type
;
379 if (glsl_type_is_struct_or_ifc(type
)) {
380 field_type
= glsl_get_struct_field(type
, i
);
383 struct_base_offset
= *offset
;
386 *offset
= struct_base_offset
+ glsl_get_struct_field_offset(type
, i
);
388 field_type
= glsl_get_array_element(type
);
391 if (glsl_type_is_leaf(field_type
)) {
392 fill_individual_variable(field_type
, variables
, variable_index
,
393 offset
, prog
, block
);
395 iterate_type_fill_variables(field_type
, variables
, variable_index
,
396 offset
, prog
, block
);
402 * In opposite to the equivalent glsl one, this one only allocates the needed
403 * space. We do a initial count here, just to avoid re-allocating for each one
407 allocate_uniform_blocks(void *mem_ctx
,
408 struct gl_linked_shader
*shader
,
409 struct gl_uniform_block
**out_blks
, unsigned *num_blocks
,
410 struct gl_uniform_buffer_variable
**out_variables
,
411 unsigned *num_variables
,
412 enum block_type block_type
)
417 nir_foreach_variable(var
, &shader
->Program
->nir
->uniforms
) {
418 if (block_type
== BLOCK_UBO
&& !nir_variable_is_in_ubo(var
))
421 if (block_type
== BLOCK_SSBO
&& !nir_variable_is_in_ssbo(var
))
424 const struct glsl_type
*type
= glsl_without_array(var
->type
);
425 unsigned aoa_size
= glsl_get_aoa_size(var
->type
);
426 unsigned buffer_count
= aoa_size
== 0 ? 1 : aoa_size
;
428 *num_blocks
+= buffer_count
;
430 unsigned int block_variables
= 0;
431 iterate_type_count_variables(type
, &block_variables
);
433 *num_variables
+= block_variables
* buffer_count
;
436 if (*num_blocks
== 0) {
437 assert(*num_variables
== 0);
441 assert(*num_variables
!= 0);
443 struct gl_uniform_block
*blocks
=
444 rzalloc_array(mem_ctx
, struct gl_uniform_block
, *num_blocks
);
446 struct gl_uniform_buffer_variable
*variables
=
447 rzalloc_array(blocks
, struct gl_uniform_buffer_variable
, *num_variables
);
450 *out_variables
= variables
;
454 fill_block(struct gl_uniform_block
*block
,
456 struct gl_uniform_buffer_variable
*variables
,
457 unsigned *variable_index
,
458 unsigned array_index
,
459 struct gl_shader_program
*prog
,
460 const gl_shader_stage stage
)
462 const struct glsl_type
*type
= glsl_without_array(var
->type
);
464 block
->Name
= NULL
; /* ARB_gl_spirv: allowed to ignore names */
465 /* From ARB_gl_spirv spec:
466 * "Vulkan uses only one binding point for a resource array,
467 * while OpenGL still uses multiple binding points, so binding
468 * numbers are counted differently for SPIR-V used in Vulkan
471 block
->Binding
= var
->data
.binding
+ array_index
;
472 block
->Uniforms
= &variables
[*variable_index
];
473 block
->stageref
= 1U << stage
;
475 /* From SPIR-V 1.0 spec, 3.20, Decoration:
477 * Applies only to a member of a structure type.
478 * Only valid on a matrix or array whose most basic
479 * element is a matrix. Indicates that components
480 * within a row are contiguous in memory."
482 * So the SPIR-V binary doesn't report if the block was defined as RowMajor
483 * or not. In any case, for the components it is mandatory to set it, so it
484 * is not needed a default RowMajor value to know it.
486 * Setting to the default, but it should be ignored.
488 block
->_RowMajor
= false;
490 /* From ARB_gl_spirv spec:
491 * "Mapping of layouts
493 * std140/std430 -> explicit *Offset*, *ArrayStride*, and
494 * *MatrixStride* Decoration on struct members
495 * shared/packed -> not allowed"
497 * So we would not have a value for _Packing, and in fact it would be
498 * useless so far. Using a default value. It should be ignored.
501 block
->linearized_array_index
= array_index
;
503 unsigned old_variable_index
= *variable_index
;
505 iterate_type_fill_variables(type
, variables
, variable_index
, &offset
, prog
, block
);
506 block
->NumUniforms
= *variable_index
- old_variable_index
;
508 block
->UniformBufferSize
= glsl_get_explicit_size(type
, false);
510 /* From OpenGL 4.6 spec, section 7.6.2.3, "SPIR-V Uniform Offsets and
513 * "If the variable is decorated as a BufferBlock , its offsets and
514 * strides must not contradict std430 alignment and minimum offset
515 * requirements. Otherwise, its offsets and strides must not contradict
516 * std140 alignment and minimum offset requirements."
518 * So although we are computing the size based on the offsets and
519 * array/matrix strides, at the end we need to ensure that the alignment is
520 * the same that with std140. From ARB_uniform_buffer_object spec:
522 * "For uniform blocks laid out according to [std140] rules, the minimum
523 * buffer object size returned by the UNIFORM_BLOCK_DATA_SIZE query is
524 * derived by taking the offset of the last basic machine unit consumed
525 * by the last uniform of the uniform block (including any end-of-array
526 * or end-of-structure padding), adding one, and rounding up to the next
527 * multiple of the base alignment required for a vec4."
529 block
->UniformBufferSize
= glsl_align(block
->UniformBufferSize
, 16);
533 * Link ubos/ssbos for a given linked_shader/stage.
536 link_linked_shader_uniform_blocks(void *mem_ctx
,
537 struct gl_context
*ctx
,
538 struct gl_shader_program
*prog
,
539 struct gl_linked_shader
*shader
,
540 struct gl_uniform_block
**blocks
,
541 unsigned *num_blocks
,
542 enum block_type block_type
)
544 struct gl_uniform_buffer_variable
*variables
= NULL
;
545 unsigned num_variables
= 0;
547 allocate_uniform_blocks(mem_ctx
, shader
,
549 &variables
, &num_variables
,
552 /* Fill the content of uniforms and variables */
553 unsigned block_index
= 0;
554 unsigned variable_index
= 0;
555 struct gl_uniform_block
*blks
= *blocks
;
557 nir_foreach_variable(var
, &shader
->Program
->nir
->uniforms
) {
558 if (block_type
== BLOCK_UBO
&& !nir_variable_is_in_ubo(var
))
561 if (block_type
== BLOCK_SSBO
&& !nir_variable_is_in_ssbo(var
))
564 unsigned aoa_size
= glsl_get_aoa_size(var
->type
);
565 unsigned buffer_count
= aoa_size
== 0 ? 1 : aoa_size
;
567 for (unsigned array_index
= 0; array_index
< buffer_count
; array_index
++) {
568 fill_block(&blks
[block_index
], var
, variables
, &variable_index
,
569 array_index
, prog
, shader
->Stage
);
574 assert(block_index
== *num_blocks
);
575 assert(variable_index
== num_variables
);
579 gl_nir_link_uniform_blocks(struct gl_context
*ctx
,
580 struct gl_shader_program
*prog
)
582 void *mem_ctx
= ralloc_context(NULL
);
584 for (int stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
585 struct gl_linked_shader
*const linked
= prog
->_LinkedShaders
[stage
];
586 struct gl_uniform_block
*ubo_blocks
= NULL
;
587 unsigned num_ubo_blocks
= 0;
588 struct gl_uniform_block
*ssbo_blocks
= NULL
;
589 unsigned num_ssbo_blocks
= 0;
594 link_linked_shader_uniform_blocks(mem_ctx
, ctx
, prog
, linked
,
595 &ubo_blocks
, &num_ubo_blocks
,
598 link_linked_shader_uniform_blocks(mem_ctx
, ctx
, prog
, linked
,
599 &ssbo_blocks
, &num_ssbo_blocks
,
602 if (!prog
->data
->LinkStatus
) {
606 prog
->data
->linked_stages
|= 1 << stage
;
608 /* Copy ubo blocks to linked shader list */
609 linked
->Program
->sh
.UniformBlocks
=
610 ralloc_array(linked
, struct gl_uniform_block
*, num_ubo_blocks
);
611 ralloc_steal(linked
, ubo_blocks
);
612 for (unsigned i
= 0; i
< num_ubo_blocks
; i
++) {
613 linked
->Program
->sh
.UniformBlocks
[i
] = &ubo_blocks
[i
];
616 /* We need to set it twice to avoid the value being overwritten by the
617 * one from nir in brw_shader_gather_info. TODO: get a way to set the
618 * info once, and being able to gather properly the info.
620 linked
->Program
->nir
->info
.num_ubos
= num_ubo_blocks
;
621 linked
->Program
->info
.num_ubos
= num_ubo_blocks
;
623 /* Copy ssbo blocks to linked shader list */
624 linked
->Program
->sh
.ShaderStorageBlocks
=
625 ralloc_array(linked
, struct gl_uniform_block
*, num_ssbo_blocks
);
626 ralloc_steal(linked
, ssbo_blocks
);
627 for (unsigned i
= 0; i
< num_ssbo_blocks
; i
++) {
628 linked
->Program
->sh
.ShaderStorageBlocks
[i
] = &ssbo_blocks
[i
];
631 /* See previous comment on num_ubo_blocks */
632 linked
->Program
->nir
->info
.num_ssbos
= num_ssbo_blocks
;
633 linked
->Program
->info
.num_ssbos
= num_ssbo_blocks
;
636 if (!nir_interstage_cross_validate_uniform_blocks(prog
, BLOCK_UBO
))
639 if (!nir_interstage_cross_validate_uniform_blocks(prog
, BLOCK_SSBO
))