nir/drawpixels: handle load_color0, load_input, load_interpolated_input
[mesa.git] / src / compiler / glsl / gl_nir_link_uniform_blocks.c
1 /*
2 * Copyright © 2019 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "nir.h"
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"
29
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.
33 *
34 * More details:
35 *
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.
41 *
42 * "19. How should the program interface query operations behave for program
43 * objects created from SPIR-V shaders?
44 *
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.
51 *
52 * Options:
53 *
54 * <skip>
55 *
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
65 * sufficient.
66 *
67 * RESOLVED. Pick (c), but also allow debug names to be returned if an
68 * implementation wants to."
69 *
70 * This implemention doesn't care for the names, as the main objective is
71 * functional, and not support optional debug features.
72 *
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.
75 *
76 * From ARB_gl_spirv spec:
77 * "Mapping of Storage Classes:
78 * <skip>
79 * uniform blockN { ... } ...; -> Uniform, with Block decoration
80 * <skip>
81 * buffer blockN { ... } ...; -> Uniform, with BufferBlock decoration"
82 *
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
87 * the SPIR-V spec:
88 *
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:
93 *
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."
99 *
100 * Additionally, the structure members are expected to be presented in
101 * increasing offset order:
102 *
103 * "a structure has lower-numbered members appearing at smaller offsets than
104 * higher-numbered members"
105 */
106
107 enum block_type {
108 BLOCK_UBO,
109 BLOCK_SSBO
110 };
111
112 /*
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.
116 */
117 static bool
118 link_blocks_are_compatible(const struct gl_uniform_block *a,
119 const struct gl_uniform_block *b)
120 {
121 /*
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
126 * Matching":
127 * "Uniform and shader storage block variables must also be decorated
128 * with a Binding"
129 */
130 if (a->Binding != b->Binding)
131 return false;
132
133 /* We are explicitly ignoring the names, so it would be good to check that
134 * this is happening.
135 */
136 assert(a->Name == NULL);
137 assert(b->Name == NULL);
138
139 if (a->NumUniforms != b->NumUniforms)
140 return false;
141
142 if (a->_Packing != b->_Packing)
143 return false;
144
145 if (a->_RowMajor != b->_RowMajor)
146 return false;
147
148 for (unsigned i = 0; i < a->NumUniforms; i++) {
149 if (a->Uniforms[i].Type != b->Uniforms[i].Type)
150 return false;
151
152 if (a->Uniforms[i].RowMajor != b->Uniforms[i].RowMajor)
153 return false;
154
155 if (a->Uniforms[i].Offset != b->Uniforms[i].Offset)
156 return false;
157
158 /* See comment on previous assert */
159 assert(a->Uniforms[i].Name == NULL);
160 assert(b->Uniforms[i].Name == NULL);
161 }
162
163 return true;
164 }
165
166 /**
167 * Merges a buffer block into an array of buffer blocks that may or may not
168 * already contain a copy of it.
169 *
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.
173 *
174 */
175 static int
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)
180 {
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];
184
185 if (old_block->Binding == new_block->Binding)
186 return link_blocks_are_compatible(old_block, new_block) ? i : -1;
187 }
188
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];
194
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);
199
200 memcpy(linked_block->Uniforms,
201 new_block->Uniforms,
202 sizeof(*linked_block->Uniforms) * linked_block->NumUniforms);
203
204 return linked_block_index;
205 }
206
207
208 /**
209 * Accumulates the array of buffer blocks and checks that all definitions of
210 * blocks agree on their contents.
211 */
212 static bool
213 nir_interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog,
214 enum block_type block_type)
215 {
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;
220
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;
227 } else {
228 max_num_buffer_blocks +=
229 prog->_LinkedShaders[i]->Program->info.num_ubos;
230 }
231 }
232 }
233
234 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
235 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
236
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;
240
241 if (sh == NULL)
242 continue;
243
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;
249 } else {
250 sh_num_blocks = prog->_LinkedShaders[i]->Program->info.num_ubos;
251 sh_blks = sh->Program->sh.UniformBlocks;
252 }
253
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]);
257
258 if (index == -1) {
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);
262
263 for (unsigned k = 0; k <= i; k++) {
264 free(interfaceBlockStageIndex[k]);
265 }
266
267 /* Reset the block count. This will help avoid various segfaults
268 * from api calls that assume the array exists due to the count
269 * being non-zero.
270 */
271 *num_blks = 0;
272 return false;
273 }
274
275 interfaceBlockStageIndex[i][index] = j;
276 }
277 }
278
279 /* Update per stage block pointers to point to the program list.
280 */
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];
284
285 if (stage_index != -1) {
286 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
287
288 struct gl_uniform_block **sh_blks = block_type == BLOCK_SSBO ?
289 sh->Program->sh.ShaderStorageBlocks :
290 sh->Program->sh.UniformBlocks;
291
292 blks[j].stageref |= sh_blks[stage_index]->stageref;
293 sh_blks[stage_index] = &blks[j];
294 }
295 }
296 }
297
298 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
299 free(interfaceBlockStageIndex[i]);
300 }
301
302 if (block_type == BLOCK_SSBO)
303 prog->data->ShaderStorageBlocks = blks;
304 else
305 prog->data->UniformBlocks = blks;
306
307 return true;
308 }
309
310 /*
311 * Iterates @type in order to compute how many individual leaf variables
312 * contains.
313 */
314 static void
315 iterate_type_count_variables(const struct glsl_type *type,
316 unsigned int *num_variables)
317 {
318 for (unsigned i = 0; i < glsl_get_length(type); i++) {
319 const struct glsl_type *field_type;
320
321 if (glsl_type_is_struct_or_ifc(type))
322 field_type = glsl_get_struct_field(type, i);
323 else
324 field_type = glsl_get_array_element(type);
325
326 if (glsl_type_is_leaf(field_type))
327 (*num_variables)++;
328 else
329 iterate_type_count_variables(field_type, num_variables);
330 }
331 }
332
333
334 static void
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)
341 {
342 /* ARB_gl_spirv: allowed to ignore names. Thus, we don't need to initialize
343 * the variable's Name or IndexName.
344 */
345 variables[*variable_index].Type = type;
346
347 if (glsl_type_is_matrix(type)) {
348 variables[*variable_index].RowMajor = glsl_matrix_type_is_row_major(type);
349 } else {
350 /* default value, better that potential meaningless garbage */
351 variables[*variable_index].RowMajor = false;
352 }
353
354 /**
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.
359 */
360 variables[*variable_index].Offset = *offset;
361 (*offset) += glsl_get_explicit_size(type, true);
362
363 (*variable_index)++;
364 }
365
366 static void
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)
373 {
374 unsigned int struct_base_offset;
375
376 for (unsigned i = 0; i < glsl_get_length(type); i++) {
377 const struct glsl_type *field_type;
378
379 if (glsl_type_is_struct_or_ifc(type)) {
380 field_type = glsl_get_struct_field(type, i);
381
382 if (i == 0) {
383 struct_base_offset = *offset;
384 }
385
386 *offset = struct_base_offset + glsl_get_struct_field_offset(type, i);
387 } else {
388 field_type = glsl_get_array_element(type);
389 }
390
391 if (glsl_type_is_leaf(field_type)) {
392 fill_individual_variable(field_type, variables, variable_index,
393 offset, prog, block);
394 } else {
395 iterate_type_fill_variables(field_type, variables, variable_index,
396 offset, prog, block);
397 }
398 }
399 }
400
401 /*
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
404 * we find.
405 */
406 static void
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)
413 {
414 *num_variables = 0;
415 *num_blocks = 0;
416
417 nir_foreach_variable(var, &shader->Program->nir->uniforms) {
418 if (block_type == BLOCK_UBO && !nir_variable_is_in_ubo(var))
419 continue;
420
421 if (block_type == BLOCK_SSBO && !nir_variable_is_in_ssbo(var))
422 continue;
423
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;
427
428 *num_blocks += buffer_count;
429
430 unsigned int block_variables = 0;
431 iterate_type_count_variables(type, &block_variables);
432
433 *num_variables += block_variables * buffer_count;
434 }
435
436 if (*num_blocks == 0) {
437 assert(*num_variables == 0);
438 return;
439 }
440
441 assert(*num_variables != 0);
442
443 struct gl_uniform_block *blocks =
444 rzalloc_array(mem_ctx, struct gl_uniform_block, *num_blocks);
445
446 struct gl_uniform_buffer_variable *variables =
447 rzalloc_array(blocks, struct gl_uniform_buffer_variable, *num_variables);
448
449 *out_blks = blocks;
450 *out_variables = variables;
451 }
452
453 static void
454 fill_block(struct gl_uniform_block *block,
455 nir_variable *var,
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)
461 {
462 const struct glsl_type *type = glsl_without_array(var->type);
463
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
469 * and OpenGL
470 */
471 block->Binding = var->data.binding + array_index;
472 block->Uniforms = &variables[*variable_index];
473 block->stageref = 1U << stage;
474
475 /* From SPIR-V 1.0 spec, 3.20, Decoration:
476 * "RowMajor
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."
481 *
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.
485 *
486 * Setting to the default, but it should be ignored.
487 */
488 block->_RowMajor = false;
489
490 /* From ARB_gl_spirv spec:
491 * "Mapping of layouts
492 *
493 * std140/std430 -> explicit *Offset*, *ArrayStride*, and
494 * *MatrixStride* Decoration on struct members
495 * shared/packed -> not allowed"
496 *
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.
499 */
500 block->_Packing = 0;
501 block->linearized_array_index = array_index;
502
503 unsigned old_variable_index = *variable_index;
504 unsigned offset = 0;
505 iterate_type_fill_variables(type, variables, variable_index, &offset, prog, block);
506 block->NumUniforms = *variable_index - old_variable_index;
507
508 block->UniformBufferSize = glsl_get_explicit_size(type, false);
509
510 /* From OpenGL 4.6 spec, section 7.6.2.3, "SPIR-V Uniform Offsets and
511 * strides"
512 *
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."
517 *
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:
521 *
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."
528 */
529 block->UniformBufferSize = glsl_align(block->UniformBufferSize, 16);
530 }
531
532 /*
533 * Link ubos/ssbos for a given linked_shader/stage.
534 */
535 static void
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)
543 {
544 struct gl_uniform_buffer_variable *variables = NULL;
545 unsigned num_variables = 0;
546
547 allocate_uniform_blocks(mem_ctx, shader,
548 blocks, num_blocks,
549 &variables, &num_variables,
550 block_type);
551
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;
556
557 nir_foreach_variable(var, &shader->Program->nir->uniforms) {
558 if (block_type == BLOCK_UBO && !nir_variable_is_in_ubo(var))
559 continue;
560
561 if (block_type == BLOCK_SSBO && !nir_variable_is_in_ssbo(var))
562 continue;
563
564 unsigned aoa_size = glsl_get_aoa_size(var->type);
565 unsigned buffer_count = aoa_size == 0 ? 1 : aoa_size;
566
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);
570 block_index++;
571 }
572 }
573
574 assert(block_index == *num_blocks);
575 assert(variable_index == num_variables);
576 }
577
578 bool
579 gl_nir_link_uniform_blocks(struct gl_context *ctx,
580 struct gl_shader_program *prog)
581 {
582 void *mem_ctx = ralloc_context(NULL);
583
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;
590
591 if (!linked)
592 continue;
593
594 link_linked_shader_uniform_blocks(mem_ctx, ctx, prog, linked,
595 &ubo_blocks, &num_ubo_blocks,
596 BLOCK_UBO);
597
598 link_linked_shader_uniform_blocks(mem_ctx, ctx, prog, linked,
599 &ssbo_blocks, &num_ssbo_blocks,
600 BLOCK_SSBO);
601
602 if (!prog->data->LinkStatus) {
603 return false;
604 }
605
606 prog->data->linked_stages |= 1 << stage;
607
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];
614 }
615
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.
619 */
620 linked->Program->nir->info.num_ubos = num_ubo_blocks;
621 linked->Program->info.num_ubos = num_ubo_blocks;
622
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];
629 }
630
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;
634 }
635
636 if (!nir_interstage_cross_validate_uniform_blocks(prog, BLOCK_UBO))
637 return false;
638
639 if (!nir_interstage_cross_validate_uniform_blocks(prog, BLOCK_SSBO))
640 return false;
641
642 return true;
643 }