2 * Copyright © 2018 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
26 #include "gl_nir_linker.h"
27 #include "linker_util.h"
28 #include "main/mtypes.h"
29 #include "main/shaderobj.h"
30 #include "ir_uniform.h" /* for gl_uniform_storage */
33 * This file included general link methods, using NIR, instead of IR as
34 * the counter-part glsl/linker.cpp
38 * Built-in / reserved GL variables names start with "gl_"
41 is_gl_identifier(const char *s
)
43 return s
&& s
[0] == 'g' && s
[1] == 'l' && s
[2] == '_';
47 inout_has_same_location(const nir_variable
*var
, unsigned stage
)
49 if (!var
->data
.patch
&&
50 ((var
->data
.mode
== nir_var_shader_out
&&
51 stage
== MESA_SHADER_TESS_CTRL
) ||
52 (var
->data
.mode
== nir_var_shader_in
&&
53 (stage
== MESA_SHADER_TESS_CTRL
|| stage
== MESA_SHADER_TESS_EVAL
||
54 stage
== MESA_SHADER_GEOMETRY
))))
61 * Create gl_shader_variable from nir_variable.
63 static struct gl_shader_variable
*
64 create_shader_variable(struct gl_shader_program
*shProg
,
65 const nir_variable
*in
,
66 const char *name
, const struct glsl_type
*type
,
67 const struct glsl_type
*interface_type
,
68 bool use_implicit_location
, int location
,
69 const struct glsl_type
*outermost_struct_type
)
71 /* Allocate zero-initialized memory to ensure that bitfield padding
74 struct gl_shader_variable
*out
= rzalloc(shProg
,
75 struct gl_shader_variable
);
79 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, but applications
80 * expect to see gl_VertexID in the program resource list. Pretend.
82 if (in
->data
.mode
== nir_var_system_value
&&
83 in
->data
.location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
) {
84 out
->name
= ralloc_strdup(shProg
, "gl_VertexID");
85 } else if ((in
->data
.mode
== nir_var_shader_out
&&
86 in
->data
.location
== VARYING_SLOT_TESS_LEVEL_OUTER
) ||
87 (in
->data
.mode
== nir_var_system_value
&&
88 in
->data
.location
== SYSTEM_VALUE_TESS_LEVEL_OUTER
)) {
89 out
->name
= ralloc_strdup(shProg
, "gl_TessLevelOuter");
90 type
= glsl_array_type(glsl_float_type(), 4, 0);
91 } else if ((in
->data
.mode
== nir_var_shader_out
&&
92 in
->data
.location
== VARYING_SLOT_TESS_LEVEL_INNER
) ||
93 (in
->data
.mode
== nir_var_system_value
&&
94 in
->data
.location
== SYSTEM_VALUE_TESS_LEVEL_INNER
)) {
95 out
->name
= ralloc_strdup(shProg
, "gl_TessLevelInner");
96 type
= glsl_array_type(glsl_float_type(), 2, 0);
98 out
->name
= ralloc_strdup(shProg
, name
);
104 /* The ARB_program_interface_query spec says:
106 * "Not all active variables are assigned valid locations; the
107 * following variables will have an effective location of -1:
109 * * uniforms declared as atomic counters;
111 * * members of a uniform block;
113 * * built-in inputs, outputs, and uniforms (starting with "gl_"); and
115 * * inputs or outputs not declared with a "location" layout
116 * qualifier, except for vertex shader inputs and fragment shader
119 if (glsl_get_base_type(in
->type
) == GLSL_TYPE_ATOMIC_UINT
||
120 is_gl_identifier(in
->name
) ||
121 !(in
->data
.explicit_location
|| use_implicit_location
)) {
124 out
->location
= location
;
128 out
->outermost_struct_type
= outermost_struct_type
;
129 out
->interface_type
= interface_type
;
130 out
->component
= in
->data
.location_frac
;
131 out
->index
= in
->data
.index
;
132 out
->patch
= in
->data
.patch
;
133 out
->mode
= in
->data
.mode
;
134 out
->interpolation
= in
->data
.interpolation
;
135 out
->precision
= in
->data
.precision
;
136 out
->explicit_location
= in
->data
.explicit_location
;
142 add_shader_variable(const struct gl_context
*ctx
,
143 struct gl_shader_program
*shProg
,
144 struct set
*resource_set
,
146 GLenum programInterface
, nir_variable
*var
,
147 const char *name
, const struct glsl_type
*type
,
148 bool use_implicit_location
, int location
,
149 bool inouts_share_location
,
150 const struct glsl_type
*outermost_struct_type
)
152 const struct glsl_type
*interface_type
= var
->interface_type
;
154 if (outermost_struct_type
== NULL
) {
155 if (var
->data
.from_named_ifc_block
) {
156 const char *interface_name
= glsl_get_type_name(interface_type
);
158 if (glsl_type_is_array(interface_type
)) {
159 /* Issue #16 of the ARB_program_interface_query spec says:
161 * "* If a variable is a member of an interface block without an
162 * instance name, it is enumerated using just the variable name.
164 * * If a variable is a member of an interface block with an
165 * instance name, it is enumerated as "BlockName.Member", where
166 * "BlockName" is the name of the interface block (not the
167 * instance name) and "Member" is the name of the variable."
169 * In particular, it indicates that it should be "BlockName",
170 * not "BlockName[array length]". The conformance suite and
171 * dEQP both require this behavior.
173 * Here, we unwrap the extra array level added by named interface
174 * block array lowering so we have the correct variable type. We
175 * also unwrap the interface type when constructing the name.
177 * We leave interface_type the same so that ES 3.x SSO pipeline
178 * validation can enforce the rules requiring array length to
179 * match on interface blocks.
181 type
= glsl_get_array_element(type
);
184 glsl_get_type_name(glsl_get_array_element(interface_type
));
187 name
= ralloc_asprintf(shProg
, "%s.%s", interface_name
, name
);
191 switch (glsl_get_base_type(type
)) {
192 case GLSL_TYPE_STRUCT
: {
193 /* The ARB_program_interface_query spec says:
195 * "For an active variable declared as a structure, a separate entry
196 * will be generated for each active structure member. The name of
197 * each entry is formed by concatenating the name of the structure,
198 * the "." character, and the name of the structure member. If a
199 * structure member to enumerate is itself a structure or array,
200 * these enumeration rules are applied recursively."
202 if (outermost_struct_type
== NULL
)
203 outermost_struct_type
= type
;
205 unsigned field_location
= location
;
206 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
207 const struct glsl_type
*field_type
= glsl_get_struct_field(type
, i
);
208 const struct glsl_struct_field
*field
=
209 glsl_get_struct_field_data(type
, i
);
211 char *field_name
= ralloc_asprintf(shProg
, "%s.%s", name
, field
->name
);
212 if (!add_shader_variable(ctx
, shProg
, resource_set
,
213 stage_mask
, programInterface
,
214 var
, field_name
, field_type
,
215 use_implicit_location
, field_location
,
216 false, outermost_struct_type
))
219 field_location
+= glsl_count_attribute_slots(field_type
, false);
224 case GLSL_TYPE_ARRAY
: {
225 /* The ARB_program_interface_query spec says:
227 * "For an active variable declared as an array of basic types, a
228 * single entry will be generated, with its name string formed by
229 * concatenating the name of the array and the string "[0]"."
231 * "For an active variable declared as an array of an aggregate data
232 * type (structures or arrays), a separate entry will be generated
233 * for each active array element, unless noted immediately below.
234 * The name of each entry is formed by concatenating the name of
235 * the array, the "[" character, an integer identifying the element
236 * number, and the "]" character. These enumeration rules are
237 * applied recursively, treating each enumerated array element as a
238 * separate active variable."
240 const struct glsl_type
*array_type
= glsl_get_array_element(type
);
241 if (glsl_get_base_type(array_type
) == GLSL_TYPE_STRUCT
||
242 glsl_get_base_type(array_type
) == GLSL_TYPE_ARRAY
) {
243 unsigned elem_location
= location
;
244 unsigned stride
= inouts_share_location
? 0 :
245 glsl_count_attribute_slots(array_type
, false);
246 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
247 char *elem
= ralloc_asprintf(shProg
, "%s[%d]", name
, i
);
248 if (!add_shader_variable(ctx
, shProg
, resource_set
,
249 stage_mask
, programInterface
,
250 var
, elem
, array_type
,
251 use_implicit_location
, elem_location
,
252 false, outermost_struct_type
))
254 elem_location
+= stride
;
262 /* The ARB_program_interface_query spec says:
264 * "For an active variable declared as a single instance of a basic
265 * type, a single entry will be generated, using the variable name
266 * from the shader source."
268 struct gl_shader_variable
*sha_v
=
269 create_shader_variable(shProg
, var
, name
, type
, interface_type
,
270 use_implicit_location
, location
,
271 outermost_struct_type
);
275 return link_util_add_program_resource(shProg
, resource_set
,
276 programInterface
, sha_v
, stage_mask
);
282 add_vars_from_list(const struct gl_context
*ctx
,
283 struct gl_shader_program
*prog
, struct set
*resource_set
,
284 const struct exec_list
*var_list
, unsigned stage
,
285 GLenum programInterface
)
287 nir_foreach_variable(var
, var_list
) {
288 if (var
->data
.how_declared
== nir_var_hidden
)
292 switch(var
->data
.mode
) {
293 case nir_var_system_value
:
294 case nir_var_shader_in
:
295 if (programInterface
!= GL_PROGRAM_INPUT
)
297 loc_bias
= (stage
== MESA_SHADER_VERTEX
) ? VERT_ATTRIB_GENERIC0
300 case nir_var_shader_out
:
301 if (programInterface
!= GL_PROGRAM_OUTPUT
)
303 loc_bias
= (stage
== MESA_SHADER_FRAGMENT
) ? FRAG_RESULT_DATA0
311 loc_bias
= VARYING_SLOT_PATCH0
;
313 if (prog
->data
->spirv
) {
314 struct gl_shader_variable
*sh_var
=
315 rzalloc(prog
, struct gl_shader_variable
);
317 /* In the ARB_gl_spirv spec, names are considered optional debug info, so
318 * the linker needs to work without them. Returning them is optional.
319 * For simplicity, we ignore names.
322 sh_var
->type
= var
->type
;
323 sh_var
->location
= var
->data
.location
- loc_bias
;
324 sh_var
->index
= var
->data
.index
;
326 if (!link_util_add_program_resource(prog
, resource_set
,
328 sh_var
, 1 << stage
)) {
332 /* Skip packed varyings, packed varyings are handled separately
333 * by add_packed_varyings in the GLSL IR
334 * build_program_resource_list() call.
335 * TODO: handle packed varyings here instead. We likely want a NIR
336 * based packing pass first.
338 if (strncmp(var
->name
, "packed:", 7) == 0)
341 const bool vs_input_or_fs_output
=
342 (stage
== MESA_SHADER_VERTEX
&&
343 var
->data
.mode
== nir_var_shader_in
) ||
344 (stage
== MESA_SHADER_FRAGMENT
&&
345 var
->data
.mode
== nir_var_shader_out
);
347 if (!add_shader_variable(ctx
, prog
, resource_set
,
348 1 << stage
, programInterface
,
349 var
, var
->name
, var
->type
,
350 vs_input_or_fs_output
,
351 var
->data
.location
- loc_bias
,
352 inout_has_same_location(var
, stage
),
362 add_interface_variables(const struct gl_context
*ctx
,
363 struct gl_shader_program
*prog
,
364 struct set
*resource_set
,
365 unsigned stage
, GLenum programInterface
)
367 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[stage
];
371 nir_shader
*nir
= sh
->Program
->nir
;
374 switch (programInterface
) {
375 case GL_PROGRAM_INPUT
: {
376 bool result
= add_vars_from_list(ctx
, prog
, resource_set
,
377 &nir
->inputs
, stage
, programInterface
);
378 result
&= add_vars_from_list(ctx
, prog
, resource_set
, &nir
->system_values
,
379 stage
, programInterface
);
382 case GL_PROGRAM_OUTPUT
:
383 return add_vars_from_list(ctx
, prog
, resource_set
, &nir
->outputs
, stage
,
386 assert("!Should not get here");
393 /* TODO: as we keep adding features, this method is becoming more and more
394 * similar to its GLSL counterpart at linker.cpp. Eventually it would be good
395 * to check if they could be refactored, and reduce code duplication somehow
398 nir_build_program_resource_list(struct gl_context
*ctx
,
399 struct gl_shader_program
*prog
,
400 bool rebuild_resourse_list
)
402 /* Rebuild resource list. */
403 if (prog
->data
->ProgramResourceList
&& rebuild_resourse_list
) {
404 ralloc_free(prog
->data
->ProgramResourceList
);
405 prog
->data
->ProgramResourceList
= NULL
;
406 prog
->data
->NumProgramResourceList
= 0;
409 int input_stage
= MESA_SHADER_STAGES
, output_stage
= 0;
411 /* Determine first input and final output stage. These are used to
412 * detect which variables should be enumerated in the resource list
413 * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT.
415 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
416 if (!prog
->_LinkedShaders
[i
])
418 if (input_stage
== MESA_SHADER_STAGES
)
423 /* Empty shader, no resources. */
424 if (input_stage
== MESA_SHADER_STAGES
&& output_stage
== 0)
427 struct set
*resource_set
= _mesa_pointer_set_create(NULL
);
429 /* Add inputs and outputs to the resource list. */
430 if (!add_interface_variables(ctx
, prog
, resource_set
, input_stage
,
435 if (!add_interface_variables(ctx
, prog
, resource_set
, output_stage
,
436 GL_PROGRAM_OUTPUT
)) {
440 /* Add transform feedback varyings and buffers. */
441 if (prog
->last_vert_prog
) {
442 struct gl_transform_feedback_info
*linked_xfb
=
443 prog
->last_vert_prog
->sh
.LinkedTransformFeedback
;
446 if (linked_xfb
->NumVarying
> 0) {
447 for (int i
= 0; i
< linked_xfb
->NumVarying
; i
++) {
448 if (!link_util_add_program_resource(prog
, resource_set
,
449 GL_TRANSFORM_FEEDBACK_VARYING
,
450 &linked_xfb
->Varyings
[i
], 0))
456 for (unsigned i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
457 if ((linked_xfb
->ActiveBuffers
>> i
) & 1) {
458 linked_xfb
->Buffers
[i
].Binding
= i
;
459 if (!link_util_add_program_resource(prog
, resource_set
,
460 GL_TRANSFORM_FEEDBACK_BUFFER
,
461 &linked_xfb
->Buffers
[i
], 0))
469 * Here, it is expected that nir_link_uniforms() has already been
470 * called, so that UniformStorage table is already available.
472 int top_level_array_base_offset
= -1;
473 int top_level_array_size_in_bytes
= -1;
474 int second_element_offset
= -1;
475 int block_index
= -1;
476 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
477 struct gl_uniform_storage
*uniform
= &prog
->data
->UniformStorage
[i
];
479 if (uniform
->hidden
) {
480 for (int j
= MESA_SHADER_VERTEX
; j
< MESA_SHADER_STAGES
; j
++) {
481 if (!uniform
->opaque
[j
].active
||
482 glsl_get_base_type(uniform
->type
) != GLSL_TYPE_SUBROUTINE
)
486 _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage
)j
);
487 /* add shader subroutines */
488 if (!link_util_add_program_resource(prog
, resource_set
,
496 if (!link_util_should_add_buffer_variable(prog
, uniform
,
497 top_level_array_base_offset
,
498 top_level_array_size_in_bytes
,
499 second_element_offset
, block_index
))
503 if (prog
->data
->UniformStorage
[i
].offset
>= second_element_offset
) {
504 top_level_array_base_offset
=
505 prog
->data
->UniformStorage
[i
].offset
;
507 top_level_array_size_in_bytes
=
508 prog
->data
->UniformStorage
[i
].top_level_array_size
*
509 prog
->data
->UniformStorage
[i
].top_level_array_stride
;
511 /* Set or reset the second element offset. For non arrays this
514 second_element_offset
= top_level_array_size_in_bytes
?
515 top_level_array_base_offset
+
516 prog
->data
->UniformStorage
[i
].top_level_array_stride
: -1;
518 block_index
= uniform
->block_index
;
521 GLenum interface
= uniform
->is_shader_storage
? GL_BUFFER_VARIABLE
: GL_UNIFORM
;
522 if (!link_util_add_program_resource(prog
, resource_set
, interface
, uniform
,
523 uniform
->active_shader_mask
)) {
529 for (unsigned i
= 0; i
< prog
->data
->NumUniformBlocks
; i
++) {
530 if (!link_util_add_program_resource(prog
, resource_set
, GL_UNIFORM_BLOCK
,
531 &prog
->data
->UniformBlocks
[i
],
532 prog
->data
->UniformBlocks
[i
].stageref
))
536 for (unsigned i
= 0; i
< prog
->data
->NumShaderStorageBlocks
; i
++) {
537 if (!link_util_add_program_resource(prog
, resource_set
, GL_SHADER_STORAGE_BLOCK
,
538 &prog
->data
->ShaderStorageBlocks
[i
],
539 prog
->data
->ShaderStorageBlocks
[i
].stageref
))
543 /* Add atomic counter buffers. */
544 for (unsigned i
= 0; i
< prog
->data
->NumAtomicBuffers
; i
++) {
545 if (!link_util_add_program_resource(prog
, resource_set
, GL_ATOMIC_COUNTER_BUFFER
,
546 &prog
->data
->AtomicBuffers
[i
], 0))
550 unsigned mask
= prog
->data
->linked_stages
;
552 const int i
= u_bit_scan(&mask
);
553 struct gl_program
*p
= prog
->_LinkedShaders
[i
]->Program
;
555 GLuint type
= _mesa_shader_stage_to_subroutine((gl_shader_stage
)i
);
556 for (unsigned j
= 0; j
< p
->sh
.NumSubroutineFunctions
; j
++) {
557 if (!link_util_add_program_resource(prog
, resource_set
,
559 &p
->sh
.SubroutineFunctions
[j
],
565 _mesa_set_destroy(resource_set
, NULL
);
569 gl_nir_link_spirv(struct gl_context
*ctx
, struct gl_shader_program
*prog
,
570 const struct gl_nir_linker_options
*options
)
572 if (!gl_nir_link_uniform_blocks(ctx
, prog
))
575 if (!gl_nir_link_uniforms(ctx
, prog
, options
->fill_parameters
))
578 gl_nir_link_assign_atomic_counter_resources(ctx
, prog
);
579 gl_nir_link_assign_xfb_resources(ctx
, prog
);
585 * Validate shader image resources.
588 check_image_resources(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
590 unsigned total_image_units
= 0;
591 unsigned fragment_outputs
= 0;
592 unsigned total_shader_storage_blocks
= 0;
594 if (!ctx
->Extensions
.ARB_shader_image_load_store
)
597 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
598 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
602 total_image_units
+= sh
->Program
->info
.num_images
;
603 total_shader_storage_blocks
+= sh
->Program
->info
.num_ssbos
;
606 if (total_image_units
> ctx
->Const
.MaxCombinedImageUniforms
)
607 linker_error(prog
, "Too many combined image uniforms\n");
609 struct gl_linked_shader
*frag_sh
=
610 prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
612 uint64_t frag_outputs_written
= frag_sh
->Program
->info
.outputs_written
;
613 fragment_outputs
= util_bitcount64(frag_outputs_written
);
616 if (total_image_units
+ fragment_outputs
+ total_shader_storage_blocks
>
617 ctx
->Const
.MaxCombinedShaderOutputResources
)
618 linker_error(prog
, "Too many combined image uniforms, shader storage "
619 " buffers and fragment outputs\n");
623 gl_nir_link_glsl(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
625 if (!gl_nir_link_uniforms(ctx
, prog
, true))
628 link_util_calculate_subroutine_compat(prog
);
629 link_util_check_uniform_resources(ctx
, prog
);
630 link_util_check_subroutine_resources(prog
);
631 check_image_resources(ctx
, prog
);
632 gl_nir_link_assign_atomic_counter_resources(ctx
, prog
);
633 gl_nir_link_check_atomic_counter_resources(ctx
, prog
);
635 if (prog
->data
->LinkStatus
== LINKING_FAILURE
)