2 * Copyright 2017 Advanced Micro Devices, Inc.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "shaderobj.h"
29 #include "compiler/nir/nir.h"
30 #include "compiler/spirv/nir_spirv.h"
32 #include "program/program.h"
34 #include "util/u_atomic.h"
37 _mesa_spirv_module_reference(struct gl_spirv_module
**dest
,
38 struct gl_spirv_module
*src
)
40 struct gl_spirv_module
*old
= *dest
;
42 if (old
&& p_atomic_dec_zero(&old
->RefCount
))
48 p_atomic_inc(&src
->RefCount
);
52 _mesa_shader_spirv_data_reference(struct gl_shader_spirv_data
**dest
,
53 struct gl_shader_spirv_data
*src
)
55 struct gl_shader_spirv_data
*old
= *dest
;
57 if (old
&& p_atomic_dec_zero(&old
->RefCount
)) {
58 _mesa_spirv_module_reference(&(*dest
)->SpirVModule
, NULL
);
65 p_atomic_inc(&src
->RefCount
);
69 _mesa_spirv_shader_binary(struct gl_context
*ctx
,
70 unsigned n
, struct gl_shader
**shaders
,
71 const void* binary
, size_t length
)
73 struct gl_spirv_module
*module
;
74 struct gl_shader_spirv_data
*spirv_data
;
76 module
= malloc(sizeof(*module
) + length
);
78 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glShaderBinary");
82 p_atomic_set(&module
->RefCount
, 0);
83 module
->Length
= length
;
84 memcpy(&module
->Binary
[0], binary
, length
);
86 for (int i
= 0; i
< n
; ++i
) {
87 struct gl_shader
*sh
= shaders
[i
];
89 spirv_data
= rzalloc(NULL
, struct gl_shader_spirv_data
);
90 _mesa_shader_spirv_data_reference(&sh
->spirv_data
, spirv_data
);
91 _mesa_spirv_module_reference(&spirv_data
->SpirVModule
, module
);
93 sh
->CompileStatus
= COMPILE_FAILURE
;
95 free((void *)sh
->Source
);
97 free((void *)sh
->FallbackSource
);
98 sh
->FallbackSource
= NULL
;
102 ralloc_free(sh
->symbols
);
108 * This is the equivalent to compiler/glsl/linker.cpp::link_shaders()
109 * but for SPIR-V programs.
111 * This method just creates the gl_linked_shader structs with a reference to
112 * the SPIR-V data collected during previous steps.
114 * The real linking happens later in the driver-specifc call LinkShader().
115 * This is so backends can implement different linking strategies for
119 _mesa_spirv_link_shaders(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
121 prog
->data
->LinkStatus
= LINKING_SUCCESS
;
122 prog
->data
->Validated
= false;
124 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
125 struct gl_shader
*shader
= prog
->Shaders
[i
];
126 gl_shader_stage shader_type
= shader
->Stage
;
128 /* We only support one shader per stage. The gl_spirv spec doesn't seem
129 * to prevent this, but the way the API is designed, requiring all shaders
130 * to be specialized with an entry point, makes supporting this quite
133 * TODO: Turn this into a proper error once the spec bug
134 * <https://gitlab.khronos.org/opengl/API/issues/58> is resolved.
136 if (prog
->_LinkedShaders
[shader_type
]) {
137 ralloc_strcat(&prog
->data
->InfoLog
,
138 "\nError trying to link more than one SPIR-V shader "
140 prog
->data
->LinkStatus
= LINKING_FAILURE
;
144 assert(shader
->spirv_data
);
146 struct gl_linked_shader
*linked
= rzalloc(NULL
, struct gl_linked_shader
);
147 linked
->Stage
= shader_type
;
149 /* Create program and attach it to the linked shader */
150 struct gl_program
*gl_prog
=
151 ctx
->Driver
.NewProgram(ctx
,
152 _mesa_shader_stage_to_program(shader_type
),
155 prog
->data
->LinkStatus
= LINKING_FAILURE
;
156 _mesa_delete_linked_shader(ctx
, linked
);
160 _mesa_reference_shader_program_data(ctx
,
164 /* Don't use _mesa_reference_program() just take ownership */
165 linked
->Program
= gl_prog
;
167 /* Reference the SPIR-V data from shader to the linked shader */
168 _mesa_shader_spirv_data_reference(&linked
->spirv_data
,
171 prog
->_LinkedShaders
[shader_type
] = linked
;
172 prog
->data
->linked_stages
|= 1 << shader_type
;
175 int last_vert_stage
=
176 util_last_bit(prog
->data
->linked_stages
&
177 ((1 << (MESA_SHADER_GEOMETRY
+ 1)) - 1));
180 prog
->last_vert_prog
= prog
->_LinkedShaders
[last_vert_stage
- 1]->Program
;
184 _mesa_spirv_to_nir(struct gl_context
*ctx
,
185 const struct gl_shader_program
*prog
,
186 gl_shader_stage stage
,
187 const nir_shader_compiler_options
*options
)
189 struct gl_linked_shader
*linked_shader
= prog
->_LinkedShaders
[stage
];
190 assert (linked_shader
);
192 struct gl_shader_spirv_data
*spirv_data
= linked_shader
->spirv_data
;
195 struct gl_spirv_module
*spirv_module
= spirv_data
->SpirVModule
;
196 assert (spirv_module
!= NULL
);
198 const char *entry_point_name
= spirv_data
->SpirVEntryPoint
;
199 assert(entry_point_name
);
201 struct nir_spirv_specialization
*spec_entries
=
202 calloc(sizeof(*spec_entries
),
203 spirv_data
->NumSpecializationConstants
);
205 for (unsigned i
= 0; i
< spirv_data
->NumSpecializationConstants
; ++i
) {
206 spec_entries
[i
].id
= spirv_data
->SpecializationConstantsIndex
[i
];
207 spec_entries
[i
].data32
= spirv_data
->SpecializationConstantsValue
[i
];
208 spec_entries
[i
].defined_on_module
= false;
211 const struct spirv_to_nir_options spirv_options
= {
212 .environment
= NIR_SPIRV_OPENGL
,
213 .lower_workgroup_access_to_offsets
= true,
214 .caps
= ctx
->Const
.SpirVCapabilities
,
215 .ubo_addr_format
= nir_address_format_32bit_index_offset
,
216 .ssbo_addr_format
= nir_address_format_32bit_index_offset
,
218 /* TODO: Consider changing this to an address format that has the NULL
219 * pointer equals to 0. That might be a better format to play nice
220 * with certain code / code generators.
222 .shared_addr_format
= nir_address_format_32bit_offset
,
227 spirv_to_nir((const uint32_t *) &spirv_module
->Binary
[0],
228 spirv_module
->Length
/ 4,
229 spec_entries
, spirv_data
->NumSpecializationConstants
,
230 stage
, entry_point_name
,
236 assert(nir
->info
.stage
== stage
);
238 nir
->options
= options
;
241 ralloc_asprintf(nir
, "SPIRV:%s:%d",
242 _mesa_shader_stage_to_abbrev(nir
->info
.stage
),
244 nir_validate_shader(nir
, "after spirv_to_nir");
246 nir
->info
.separate_shader
= linked_shader
->Program
->info
.separate_shader
;
248 /* We have to lower away local constant initializers right before we
249 * inline functions. That way they get properly initialized at the top
250 * of the function and not at the top of its caller.
252 NIR_PASS_V(nir
, nir_lower_constant_initializers
, nir_var_function_temp
);
253 NIR_PASS_V(nir
, nir_lower_returns
);
254 NIR_PASS_V(nir
, nir_inline_functions
);
255 NIR_PASS_V(nir
, nir_opt_deref
);
257 /* Pick off the single entrypoint that we want */
258 foreach_list_typed_safe(nir_function
, func
, node
, &nir
->functions
) {
259 if (!func
->is_entrypoint
)
260 exec_node_remove(&func
->node
);
262 assert(exec_list_length(&nir
->functions
) == 1);
264 /* Split member structs. We do this before lower_io_to_temporaries so that
265 * it doesn't lower system values to temporaries by accident.
267 NIR_PASS_V(nir
, nir_split_var_copies
);
268 NIR_PASS_V(nir
, nir_split_per_member_structs
);
270 if (nir
->info
.stage
== MESA_SHADER_VERTEX
)
271 nir_remap_dual_slot_attributes(nir
, &linked_shader
->Program
->DualSlotInputs
);
273 NIR_PASS_V(nir
, nir_lower_frexp
);
279 _mesa_SpecializeShaderARB(GLuint shader
,
280 const GLchar
*pEntryPoint
,
281 GLuint numSpecializationConstants
,
282 const GLuint
*pConstantIndex
,
283 const GLuint
*pConstantValue
)
285 GET_CURRENT_CONTEXT(ctx
);
286 struct gl_shader
*sh
;
287 bool has_entry_point
;
288 struct nir_spirv_specialization
*spec_entries
= NULL
;
290 if (!ctx
->Extensions
.ARB_gl_spirv
) {
291 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glSpecializeShaderARB");
295 sh
= _mesa_lookup_shader_err(ctx
, shader
, "glSpecializeShaderARB");
299 if (!sh
->spirv_data
) {
300 _mesa_error(ctx
, GL_INVALID_OPERATION
,
301 "glSpecializeShaderARB(not SPIR-V)");
305 if (sh
->CompileStatus
) {
306 _mesa_error(ctx
, GL_INVALID_OPERATION
,
307 "glSpecializeShaderARB(already specialized)");
311 struct gl_shader_spirv_data
*spirv_data
= sh
->spirv_data
;
313 /* From the GL_ARB_gl_spirv spec:
315 * "The OpenGL API expects the SPIR-V module to have already been
316 * validated, and can return an error if it discovers anything invalid
317 * in the module. An invalid SPIR-V module is allowed to result in
318 * undefined behavior."
320 * However, the following errors still need to be detected (from the same
323 * "INVALID_VALUE is generated if <pEntryPoint> does not name a valid
324 * entry point for <shader>.
326 * INVALID_VALUE is generated if any element of <pConstantIndex>
327 * refers to a specialization constant that does not exist in the
328 * shader module contained in <shader>."
330 * We cannot flag those errors a-priori because detecting them requires
331 * parsing the module. However, flagging them during specialization is okay,
332 * since it makes no difference in terms of application-visible state.
334 spec_entries
= calloc(sizeof(*spec_entries
), numSpecializationConstants
);
336 for (unsigned i
= 0; i
< numSpecializationConstants
; ++i
) {
337 spec_entries
[i
].id
= pConstantIndex
[i
];
338 spec_entries
[i
].data32
= pConstantValue
[i
];
339 spec_entries
[i
].defined_on_module
= false;
343 gl_spirv_validation((uint32_t *)&spirv_data
->SpirVModule
->Binary
[0],
344 spirv_data
->SpirVModule
->Length
/ 4,
345 spec_entries
, numSpecializationConstants
,
346 sh
->Stage
, pEntryPoint
);
348 /* See previous spec comment */
349 if (!has_entry_point
) {
350 _mesa_error(ctx
, GL_INVALID_VALUE
,
351 "glSpecializeShaderARB(\"%s\" is not a valid entry point"
352 " for shader)", pEntryPoint
);
356 for (unsigned i
= 0; i
< numSpecializationConstants
; ++i
) {
357 if (spec_entries
[i
].defined_on_module
== false) {
358 _mesa_error(ctx
, GL_INVALID_VALUE
,
359 "glSpecializeShaderARB(constant \"%i\" does not exist "
360 "in shader)", spec_entries
[i
].id
);
365 spirv_data
->SpirVEntryPoint
= ralloc_strdup(spirv_data
, pEntryPoint
);
367 /* Note that we didn't make a real compilation of the module (spirv_to_nir),
368 * but just checked some error conditions. Real "compilation" will be done
369 * later, upon linking.
371 sh
->CompileStatus
= COMPILE_SUCCESS
;
373 spirv_data
->NumSpecializationConstants
= numSpecializationConstants
;
374 spirv_data
->SpecializationConstantsIndex
=
375 rzalloc_array_size(spirv_data
, sizeof(GLuint
),
376 numSpecializationConstants
);
377 spirv_data
->SpecializationConstantsValue
=
378 rzalloc_array_size(spirv_data
, sizeof(GLuint
),
379 numSpecializationConstants
);
380 for (unsigned i
= 0; i
< numSpecializationConstants
; ++i
) {
381 spirv_data
->SpecializationConstantsIndex
[i
] = pConstantIndex
[i
];
382 spirv_data
->SpecializationConstantsValue
[i
] = pConstantValue
[i
];