arb_gl_spirv: add calls to several nir lowerings
[mesa.git] / src / mesa / main / glspirv.c
1 /*
2 * Copyright 2017 Advanced Micro Devices, Inc.
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 * 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:
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 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.
22 */
23
24 #include "glspirv.h"
25 #include "errors.h"
26 #include "shaderobj.h"
27 #include "mtypes.h"
28
29 #include "compiler/nir/nir.h"
30 #include "compiler/spirv/nir_spirv.h"
31
32 #include "program/program.h"
33
34 #include "util/u_atomic.h"
35
36 void
37 _mesa_spirv_module_reference(struct gl_spirv_module **dest,
38 struct gl_spirv_module *src)
39 {
40 struct gl_spirv_module *old = *dest;
41
42 if (old && p_atomic_dec_zero(&old->RefCount))
43 free(old);
44
45 *dest = src;
46
47 if (src)
48 p_atomic_inc(&src->RefCount);
49 }
50
51 void
52 _mesa_shader_spirv_data_reference(struct gl_shader_spirv_data **dest,
53 struct gl_shader_spirv_data *src)
54 {
55 struct gl_shader_spirv_data *old = *dest;
56
57 if (old && p_atomic_dec_zero(&old->RefCount)) {
58 _mesa_spirv_module_reference(&(*dest)->SpirVModule, NULL);
59 ralloc_free(old);
60 }
61
62 *dest = src;
63
64 if (src)
65 p_atomic_inc(&src->RefCount);
66 }
67
68 void
69 _mesa_spirv_shader_binary(struct gl_context *ctx,
70 unsigned n, struct gl_shader **shaders,
71 const void* binary, size_t length)
72 {
73 struct gl_spirv_module *module;
74 struct gl_shader_spirv_data *spirv_data;
75
76 assert(length >= 0);
77
78 module = malloc(sizeof(*module) + length);
79 if (!module) {
80 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderBinary");
81 return;
82 }
83
84 p_atomic_set(&module->RefCount, 0);
85 module->Length = length;
86 memcpy(&module->Binary[0], binary, length);
87
88 for (int i = 0; i < n; ++i) {
89 struct gl_shader *sh = shaders[i];
90
91 spirv_data = rzalloc(NULL, struct gl_shader_spirv_data);
92 _mesa_shader_spirv_data_reference(&sh->spirv_data, spirv_data);
93 _mesa_spirv_module_reference(&spirv_data->SpirVModule, module);
94
95 sh->CompileStatus = COMPILE_FAILURE;
96
97 free((void *)sh->Source);
98 sh->Source = NULL;
99 free((void *)sh->FallbackSource);
100 sh->FallbackSource = NULL;
101
102 ralloc_free(sh->ir);
103 sh->ir = NULL;
104 ralloc_free(sh->symbols);
105 sh->symbols = NULL;
106 }
107 }
108
109 /**
110 * This is the equivalent to compiler/glsl/linker.cpp::link_shaders()
111 * but for SPIR-V programs.
112 *
113 * This method just creates the gl_linked_shader structs with a reference to
114 * the SPIR-V data collected during previous steps.
115 *
116 * The real linking happens later in the driver-specifc call LinkShader().
117 * This is so backends can implement different linking strategies for
118 * SPIR-V programs.
119 */
120 void
121 _mesa_spirv_link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
122 {
123 prog->data->LinkStatus = LINKING_SUCCESS;
124 prog->data->Validated = false;
125
126 for (unsigned i = 0; i < prog->NumShaders; i++) {
127 struct gl_shader *shader = prog->Shaders[i];
128 gl_shader_stage shader_type = shader->Stage;
129
130 /* We only support one shader per stage. The gl_spirv spec doesn't seem
131 * to prevent this, but the way the API is designed, requiring all shaders
132 * to be specialized with an entry point, makes supporting this quite
133 * undefined.
134 *
135 * TODO: Turn this into a proper error once the spec bug
136 * <https://gitlab.khronos.org/opengl/API/issues/58> is resolved.
137 */
138 if (prog->_LinkedShaders[shader_type]) {
139 ralloc_strcat(&prog->data->InfoLog,
140 "\nError trying to link more than one SPIR-V shader "
141 "per stage.\n");
142 prog->data->LinkStatus = LINKING_FAILURE;
143 return;
144 }
145
146 assert(shader->spirv_data);
147
148 struct gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader);
149 linked->Stage = shader_type;
150
151 /* Create program and attach it to the linked shader */
152 struct gl_program *gl_prog =
153 ctx->Driver.NewProgram(ctx,
154 _mesa_shader_stage_to_program(shader_type),
155 prog->Name, false);
156 if (!gl_prog) {
157 prog->data->LinkStatus = LINKING_FAILURE;
158 _mesa_delete_linked_shader(ctx, linked);
159 return;
160 }
161
162 _mesa_reference_shader_program_data(ctx,
163 &gl_prog->sh.data,
164 prog->data);
165
166 /* Don't use _mesa_reference_program() just take ownership */
167 linked->Program = gl_prog;
168
169 /* Reference the SPIR-V data from shader to the linked shader */
170 _mesa_shader_spirv_data_reference(&linked->spirv_data,
171 shader->spirv_data);
172
173 prog->_LinkedShaders[shader_type] = linked;
174 prog->data->linked_stages |= 1 << shader_type;
175 }
176
177 int last_vert_stage =
178 util_last_bit(prog->data->linked_stages &
179 ((1 << (MESA_SHADER_GEOMETRY + 1)) - 1));
180
181 if (last_vert_stage)
182 prog->last_vert_prog = prog->_LinkedShaders[last_vert_stage - 1]->Program;
183 }
184
185 nir_shader *
186 _mesa_spirv_to_nir(struct gl_context *ctx,
187 const struct gl_shader_program *prog,
188 gl_shader_stage stage,
189 const nir_shader_compiler_options *options)
190 {
191 nir_shader *nir = NULL;
192
193 struct gl_linked_shader *linked_shader = prog->_LinkedShaders[stage];
194 assert (linked_shader);
195
196 struct gl_shader_spirv_data *spirv_data = linked_shader->spirv_data;
197 assert(spirv_data);
198
199 struct gl_spirv_module *spirv_module = spirv_data->SpirVModule;
200 assert (spirv_module != NULL);
201
202 const char *entry_point_name = spirv_data->SpirVEntryPoint;
203 assert(entry_point_name);
204
205 struct nir_spirv_specialization *spec_entries =
206 calloc(sizeof(*spec_entries),
207 spirv_data->NumSpecializationConstants);
208
209 for (unsigned i = 0; i < spirv_data->NumSpecializationConstants; ++i) {
210 spec_entries[i].id = spirv_data->SpecializationConstantsIndex[i];
211 spec_entries[i].data32 = spirv_data->SpecializationConstantsValue[i];
212 spec_entries[i].defined_on_module = false;
213 }
214
215 const struct spirv_to_nir_options spirv_options = {
216 .lower_workgroup_access_to_offsets = true,
217 .caps = ctx->Const.SpirVCapabilities
218 };
219
220 nir_function *entry_point =
221 spirv_to_nir((const uint32_t *) &spirv_module->Binary[0],
222 spirv_module->Length / 4,
223 spec_entries, spirv_data->NumSpecializationConstants,
224 stage, entry_point_name,
225 &spirv_options,
226 options);
227 free(spec_entries);
228
229 assert (entry_point);
230 nir = entry_point->shader;
231 assert(nir->info.stage == stage);
232
233 nir->options = options;
234
235 nir->info.name =
236 ralloc_asprintf(nir, "SPIRV:%s:%d",
237 _mesa_shader_stage_to_abbrev(nir->info.stage),
238 prog->Name);
239 nir_validate_shader(nir);
240
241 NIR_PASS_V(nir, nir_copy_prop);
242
243 /* Split member structs. We do this before lower_io_to_temporaries so that
244 * it doesn't lower system values to temporaries by accident.
245 */
246 NIR_PASS_V(nir, nir_split_var_copies);
247 NIR_PASS_V(nir, nir_split_per_member_structs);
248
249 return nir;
250 }
251
252 void GLAPIENTRY
253 _mesa_SpecializeShaderARB(GLuint shader,
254 const GLchar *pEntryPoint,
255 GLuint numSpecializationConstants,
256 const GLuint *pConstantIndex,
257 const GLuint *pConstantValue)
258 {
259 GET_CURRENT_CONTEXT(ctx);
260 struct gl_shader *sh;
261 bool has_entry_point;
262 struct nir_spirv_specialization *spec_entries = NULL;
263
264 if (!ctx->Extensions.ARB_gl_spirv) {
265 _mesa_error(ctx, GL_INVALID_OPERATION, "glSpecializeShaderARB");
266 return;
267 }
268
269 sh = _mesa_lookup_shader_err(ctx, shader, "glSpecializeShaderARB");
270 if (!sh)
271 return;
272
273 if (!sh->spirv_data) {
274 _mesa_error(ctx, GL_INVALID_OPERATION,
275 "glSpecializeShaderARB(not SPIR-V)");
276 return;
277 }
278
279 if (sh->CompileStatus) {
280 _mesa_error(ctx, GL_INVALID_OPERATION,
281 "glSpecializeShaderARB(already specialized)");
282 return;
283 }
284
285 struct gl_shader_spirv_data *spirv_data = sh->spirv_data;
286
287 /* From the GL_ARB_gl_spirv spec:
288 *
289 * "The OpenGL API expects the SPIR-V module to have already been
290 * validated, and can return an error if it discovers anything invalid
291 * in the module. An invalid SPIR-V module is allowed to result in
292 * undefined behavior."
293 *
294 * However, the following errors still need to be detected (from the same
295 * spec):
296 *
297 * "INVALID_VALUE is generated if <pEntryPoint> does not name a valid
298 * entry point for <shader>.
299 *
300 * INVALID_VALUE is generated if any element of <pConstantIndex>
301 * refers to a specialization constant that does not exist in the
302 * shader module contained in <shader>."
303 *
304 * We cannot flag those errors a-priori because detecting them requires
305 * parsing the module. However, flagging them during specialization is okay,
306 * since it makes no difference in terms of application-visible state.
307 */
308 spec_entries = calloc(sizeof(*spec_entries), numSpecializationConstants);
309
310 for (unsigned i = 0; i < numSpecializationConstants; ++i) {
311 spec_entries[i].id = pConstantIndex[i];
312 spec_entries[i].data32 = pConstantValue[i];
313 spec_entries[i].defined_on_module = false;
314 }
315
316 has_entry_point =
317 gl_spirv_validation((uint32_t *)&spirv_data->SpirVModule->Binary[0],
318 spirv_data->SpirVModule->Length / 4,
319 spec_entries, numSpecializationConstants,
320 sh->Stage, pEntryPoint);
321
322 /* See previous spec comment */
323 if (!has_entry_point) {
324 _mesa_error(ctx, GL_INVALID_VALUE,
325 "glSpecializeShaderARB(\"%s\" is not a valid entry point"
326 " for shader)", pEntryPoint);
327 goto end;
328 }
329
330 for (unsigned i = 0; i < numSpecializationConstants; ++i) {
331 if (spec_entries[i].defined_on_module == false) {
332 _mesa_error(ctx, GL_INVALID_VALUE,
333 "glSpecializeShaderARB(constant \"%i\" does not exist "
334 "in shader)", spec_entries[i].id);
335 goto end;
336 }
337 }
338
339 spirv_data->SpirVEntryPoint = ralloc_strdup(spirv_data, pEntryPoint);
340
341 /* Note that we didn't make a real compilation of the module (spirv_to_nir),
342 * but just checked some error conditions. Real "compilation" will be done
343 * later, upon linking.
344 */
345 sh->CompileStatus = COMPILE_SUCCESS;
346
347 spirv_data->NumSpecializationConstants = numSpecializationConstants;
348 spirv_data->SpecializationConstantsIndex =
349 rzalloc_array_size(spirv_data, sizeof(GLuint),
350 numSpecializationConstants);
351 spirv_data->SpecializationConstantsValue =
352 rzalloc_array_size(spirv_data, sizeof(GLuint),
353 numSpecializationConstants);
354 for (unsigned i = 0; i < numSpecializationConstants; ++i) {
355 spirv_data->SpecializationConstantsIndex[i] = pConstantIndex[i];
356 spirv_data->SpecializationConstantsValue[i] = pConstantValue[i];
357 }
358
359 end:
360 free(spec_entries);
361 }