freedreno: Stop scattered remapping of SSBOs/images to IBOs.
[mesa.git] / src / freedreno / vulkan / tu_shader.c
1 /*
2 * Copyright © 2019 Google LLC
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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "tu_private.h"
25
26 #include "spirv/nir_spirv.h"
27 #include "util/mesa-sha1.h"
28
29 #include "ir3/ir3_nir.h"
30
31 static nir_shader *
32 tu_spirv_to_nir(struct ir3_compiler *compiler,
33 const uint32_t *words,
34 size_t word_count,
35 gl_shader_stage stage,
36 const char *entry_point_name,
37 const VkSpecializationInfo *spec_info)
38 {
39 /* TODO these are made-up */
40 const struct spirv_to_nir_options spirv_options = {
41 .frag_coord_is_sysval = true,
42 .lower_ubo_ssbo_access_to_offsets = true,
43 .caps = { false },
44 };
45 const nir_shader_compiler_options *nir_options =
46 ir3_get_compiler_options(compiler);
47
48 /* convert VkSpecializationInfo */
49 struct nir_spirv_specialization *spec = NULL;
50 uint32_t num_spec = 0;
51 if (spec_info && spec_info->mapEntryCount) {
52 spec = malloc(sizeof(*spec) * spec_info->mapEntryCount);
53 if (!spec)
54 return NULL;
55
56 for (uint32_t i = 0; i < spec_info->mapEntryCount; i++) {
57 const VkSpecializationMapEntry *entry = &spec_info->pMapEntries[i];
58 const void *data = spec_info->pData + entry->offset;
59 assert(data + entry->size <= spec_info->pData + spec_info->dataSize);
60 spec[i].id = entry->constantID;
61 if (entry->size == 8)
62 spec[i].data64 = *(const uint64_t *) data;
63 else
64 spec[i].data32 = *(const uint32_t *) data;
65 spec[i].defined_on_module = false;
66 }
67
68 num_spec = spec_info->mapEntryCount;
69 }
70
71 nir_shader *nir =
72 spirv_to_nir(words, word_count, spec, num_spec, stage, entry_point_name,
73 &spirv_options, nir_options);
74
75 free(spec);
76
77 assert(nir->info.stage == stage);
78 nir_validate_shader(nir, "after spirv_to_nir");
79
80 return nir;
81 }
82
83 static void
84 tu_sort_variables_by_location(struct exec_list *variables)
85 {
86 struct exec_list sorted;
87 exec_list_make_empty(&sorted);
88
89 nir_foreach_variable_safe(var, variables)
90 {
91 exec_node_remove(&var->node);
92
93 /* insert the variable into the sorted list */
94 nir_variable *next = NULL;
95 nir_foreach_variable(tmp, &sorted)
96 {
97 if (var->data.location < tmp->data.location) {
98 next = tmp;
99 break;
100 }
101 }
102 if (next)
103 exec_node_insert_node_before(&next->node, &var->node);
104 else
105 exec_list_push_tail(&sorted, &var->node);
106 }
107
108 exec_list_move_nodes_to(&sorted, variables);
109 }
110
111 static unsigned
112 map_add(struct tu_descriptor_map *map, int set, int binding, int value,
113 int array_size)
114 {
115 unsigned index = 0;
116 for (unsigned i = 0; i < map->num; i++) {
117 if (set == map->set[i] && binding == map->binding[i]) {
118 assert(value == map->value[i]);
119 assert(array_size == map->array_size[i]);
120 return index;
121 }
122 index += map->array_size[i];
123 }
124
125 assert(index == map->num_desc);
126
127 map->set[map->num] = set;
128 map->binding[map->num] = binding;
129 map->value[map->num] = value;
130 map->array_size[map->num] = array_size;
131 map->num++;
132 map->num_desc += array_size;
133
134 return index;
135 }
136
137 static void
138 lower_tex_src_to_offset(nir_builder *b, nir_tex_instr *instr, unsigned src_idx,
139 struct tu_shader *shader,
140 const struct tu_pipeline_layout *layout)
141 {
142 nir_ssa_def *index = NULL;
143 unsigned base_index = 0;
144 unsigned array_elements = 1;
145 nir_tex_src *src = &instr->src[src_idx];
146 bool is_sampler = src->src_type == nir_tex_src_sampler_deref;
147
148 /* We compute first the offsets */
149 nir_deref_instr *deref = nir_instr_as_deref(src->src.ssa->parent_instr);
150 while (deref->deref_type != nir_deref_type_var) {
151 assert(deref->parent.is_ssa);
152 nir_deref_instr *parent =
153 nir_instr_as_deref(deref->parent.ssa->parent_instr);
154
155 assert(deref->deref_type == nir_deref_type_array);
156
157 if (nir_src_is_const(deref->arr.index) && index == NULL) {
158 /* We're still building a direct index */
159 base_index += nir_src_as_uint(deref->arr.index) * array_elements;
160 } else {
161 if (index == NULL) {
162 /* We used to be direct but not anymore */
163 index = nir_imm_int(b, base_index);
164 base_index = 0;
165 }
166
167 index = nir_iadd(b, index,
168 nir_imul(b, nir_imm_int(b, array_elements),
169 nir_ssa_for_src(b, deref->arr.index, 1)));
170 }
171
172 array_elements *= glsl_get_length(parent->type);
173
174 deref = parent;
175 }
176
177 if (index)
178 index = nir_umin(b, index, nir_imm_int(b, array_elements - 1));
179
180 /* We have the offsets, we apply them, rewriting the source or removing
181 * instr if needed
182 */
183 if (index) {
184 nir_instr_rewrite_src(&instr->instr, &src->src,
185 nir_src_for_ssa(index));
186
187 src->src_type = is_sampler ?
188 nir_tex_src_sampler_offset :
189 nir_tex_src_texture_offset;
190
191 instr->texture_array_size = array_elements;
192 } else {
193 nir_tex_instr_remove_src(instr, src_idx);
194 }
195
196 uint32_t set = deref->var->data.descriptor_set;
197 uint32_t binding = deref->var->data.binding;
198 struct tu_descriptor_set_layout *set_layout = layout->set[set].layout;
199 struct tu_descriptor_set_binding_layout *binding_layout =
200 &set_layout->binding[binding];
201
202 int desc_index = map_add(is_sampler ?
203 &shader->sampler_map : &shader->texture_map,
204 deref->var->data.descriptor_set,
205 deref->var->data.binding,
206 deref->var->data.index,
207 binding_layout->array_size) + base_index;
208 if (is_sampler)
209 instr->sampler_index = desc_index;
210 else
211 instr->texture_index = desc_index;
212 }
213
214 static bool
215 lower_sampler(nir_builder *b, nir_tex_instr *instr, struct tu_shader *shader,
216 const struct tu_pipeline_layout *layout)
217 {
218 int texture_idx =
219 nir_tex_instr_src_index(instr, nir_tex_src_texture_deref);
220
221 if (texture_idx >= 0)
222 lower_tex_src_to_offset(b, instr, texture_idx, shader, layout);
223
224 int sampler_idx =
225 nir_tex_instr_src_index(instr, nir_tex_src_sampler_deref);
226
227 if (sampler_idx >= 0)
228 lower_tex_src_to_offset(b, instr, sampler_idx, shader, layout);
229
230 if (texture_idx < 0 && sampler_idx < 0)
231 return false;
232
233 return true;
234 }
235
236 static bool
237 lower_intrinsic(nir_builder *b, nir_intrinsic_instr *instr,
238 struct tu_shader *shader,
239 const struct tu_pipeline_layout *layout)
240 {
241 /* TODO: remove this when layered rendering is implemented */
242 if (instr->intrinsic == nir_intrinsic_load_layer_id) {
243 nir_ssa_def_rewrite_uses(&instr->dest.ssa,
244 nir_src_for_ssa(nir_imm_int(b, 0)));
245 nir_instr_remove(&instr->instr);
246 return true;
247 }
248
249 if (instr->intrinsic == nir_intrinsic_load_push_constant) {
250 /* note: ir3 wants load_ubo, not load_uniform */
251 assert(nir_intrinsic_base(instr) == 0);
252
253 nir_intrinsic_instr *load =
254 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_ubo);
255 load->num_components = instr->num_components;
256 load->src[0] = nir_src_for_ssa(nir_imm_int(b, 0));
257 load->src[1] = instr->src[0];
258 nir_ssa_dest_init(&load->instr, &load->dest,
259 load->num_components, instr->dest.ssa.bit_size,
260 instr->dest.ssa.name);
261 nir_builder_instr_insert(b, &load->instr);
262 nir_ssa_def_rewrite_uses(&instr->dest.ssa, nir_src_for_ssa(&load->dest.ssa));
263
264 nir_instr_remove(&instr->instr);
265
266 return true;
267 }
268
269 if (instr->intrinsic != nir_intrinsic_vulkan_resource_index)
270 return false;
271
272 nir_const_value *const_val = nir_src_as_const_value(instr->src[0]);
273
274
275 unsigned set = nir_intrinsic_desc_set(instr);
276 unsigned binding = nir_intrinsic_binding(instr);
277 struct tu_descriptor_set_layout *set_layout = layout->set[set].layout;
278 struct tu_descriptor_set_binding_layout *binding_layout =
279 &set_layout->binding[binding];
280 unsigned index = 0;
281
282 switch (nir_intrinsic_desc_type(instr)) {
283 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
284 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
285 if (!const_val || const_val->u32 != 0)
286 tu_finishme("non-zero vulkan_resource_index array index");
287 /* skip index 0 which is used for push constants */
288 index = map_add(&shader->ubo_map, set, binding, 0,
289 binding_layout->array_size) + 1;
290 break;
291 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
292 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
293 if (!const_val)
294 tu_finishme("non-constant vulkan_resource_index array index");
295 index = map_add(&shader->ssbo_map, set, binding, 0,
296 binding_layout->array_size);
297 index += const_val->u32;
298 break;
299 default:
300 tu_finishme("unsupported desc_type for vulkan_resource_index");
301 break;
302 }
303
304 nir_ssa_def_rewrite_uses(&instr->dest.ssa,
305 nir_src_for_ssa(nir_imm_int(b, index)));
306 nir_instr_remove(&instr->instr);
307
308 return true;
309 }
310
311 static bool
312 lower_impl(nir_function_impl *impl, struct tu_shader *shader,
313 const struct tu_pipeline_layout *layout)
314 {
315 nir_builder b;
316 nir_builder_init(&b, impl);
317 bool progress = false;
318
319 nir_foreach_block(block, impl) {
320 nir_foreach_instr_safe(instr, block) {
321 b.cursor = nir_before_instr(instr);
322 switch (instr->type) {
323 case nir_instr_type_tex:
324 progress |= lower_sampler(&b, nir_instr_as_tex(instr), shader, layout);
325 break;
326 case nir_instr_type_intrinsic:
327 progress |= lower_intrinsic(&b, nir_instr_as_intrinsic(instr), shader, layout);
328 break;
329 default:
330 break;
331 }
332 }
333 }
334
335 return progress;
336 }
337
338 static bool
339 tu_lower_io(nir_shader *shader, struct tu_shader *tu_shader,
340 const struct tu_pipeline_layout *layout)
341 {
342 bool progress = false;
343
344 nir_foreach_function(function, shader) {
345 if (function->impl)
346 progress |= lower_impl(function->impl, tu_shader, layout);
347 }
348
349 /* spirv_to_nir produces num_ssbos equal to the number of SSBO-containing
350 * variables, while ir3 wants the number of descriptors (like the gallium
351 * path).
352 */
353 shader->info.num_ssbos = tu_shader->ssbo_map.num_desc;
354
355 return progress;
356 }
357
358 struct tu_shader *
359 tu_shader_create(struct tu_device *dev,
360 gl_shader_stage stage,
361 const VkPipelineShaderStageCreateInfo *stage_info,
362 struct tu_pipeline_layout *layout,
363 const VkAllocationCallbacks *alloc)
364 {
365 const struct tu_shader_module *module =
366 tu_shader_module_from_handle(stage_info->module);
367 struct tu_shader *shader;
368
369 const uint32_t max_variant_count = (stage == MESA_SHADER_VERTEX) ? 2 : 1;
370 shader = vk_zalloc2(
371 &dev->alloc, alloc,
372 sizeof(*shader) + sizeof(struct ir3_shader_variant) * max_variant_count,
373 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
374 if (!shader)
375 return NULL;
376
377 /* translate SPIR-V to NIR */
378 assert(module->code_size % 4 == 0);
379 nir_shader *nir = tu_spirv_to_nir(
380 dev->compiler, (const uint32_t *) module->code, module->code_size / 4,
381 stage, stage_info->pName, stage_info->pSpecializationInfo);
382 if (!nir) {
383 vk_free2(&dev->alloc, alloc, shader);
384 return NULL;
385 }
386
387 if (unlikely(dev->physical_device->instance->debug_flags & TU_DEBUG_NIR)) {
388 fprintf(stderr, "translated nir:\n");
389 nir_print_shader(nir, stderr);
390 }
391
392 /* multi step inlining procedure */
393 NIR_PASS_V(nir, nir_lower_constant_initializers, nir_var_function_temp);
394 NIR_PASS_V(nir, nir_lower_returns);
395 NIR_PASS_V(nir, nir_inline_functions);
396 NIR_PASS_V(nir, nir_opt_deref);
397 foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
398 if (!func->is_entrypoint)
399 exec_node_remove(&func->node);
400 }
401 assert(exec_list_length(&nir->functions) == 1);
402 NIR_PASS_V(nir, nir_lower_constant_initializers, ~nir_var_function_temp);
403
404 /* Split member structs. We do this before lower_io_to_temporaries so that
405 * it doesn't lower system values to temporaries by accident.
406 */
407 NIR_PASS_V(nir, nir_split_var_copies);
408 NIR_PASS_V(nir, nir_split_per_member_structs);
409
410 NIR_PASS_V(nir, nir_remove_dead_variables,
411 nir_var_shader_in | nir_var_shader_out | nir_var_system_value | nir_var_mem_shared);
412
413 NIR_PASS_V(nir, nir_propagate_invariant);
414
415 NIR_PASS_V(nir, nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir), true, true);
416
417 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
418 NIR_PASS_V(nir, nir_split_var_copies);
419 NIR_PASS_V(nir, nir_lower_var_copies);
420
421 NIR_PASS_V(nir, nir_opt_copy_prop_vars);
422 NIR_PASS_V(nir, nir_opt_combine_stores, nir_var_all);
423
424 /* ir3 doesn't support indirect input/output */
425 NIR_PASS_V(nir, nir_lower_indirect_derefs, nir_var_shader_in | nir_var_shader_out);
426
427 switch (stage) {
428 case MESA_SHADER_VERTEX:
429 tu_sort_variables_by_location(&nir->outputs);
430 break;
431 case MESA_SHADER_TESS_CTRL:
432 case MESA_SHADER_TESS_EVAL:
433 case MESA_SHADER_GEOMETRY:
434 tu_sort_variables_by_location(&nir->inputs);
435 tu_sort_variables_by_location(&nir->outputs);
436 break;
437 case MESA_SHADER_FRAGMENT:
438 tu_sort_variables_by_location(&nir->inputs);
439 break;
440 case MESA_SHADER_COMPUTE:
441 break;
442 default:
443 unreachable("invalid gl_shader_stage");
444 break;
445 }
446
447 nir_assign_io_var_locations(&nir->inputs, &nir->num_inputs, stage);
448 nir_assign_io_var_locations(&nir->outputs, &nir->num_outputs, stage);
449
450 NIR_PASS_V(nir, nir_lower_system_values);
451 NIR_PASS_V(nir, nir_lower_frexp);
452
453 if (stage == MESA_SHADER_FRAGMENT)
454 NIR_PASS_V(nir, nir_lower_input_attachments, true);
455
456 NIR_PASS_V(nir, tu_lower_io, shader, layout);
457
458 NIR_PASS_V(nir, nir_lower_io, nir_var_all, ir3_glsl_type_size, 0);
459
460 if (stage == MESA_SHADER_FRAGMENT) {
461 /* NOTE: lower load_barycentric_at_sample first, since it
462 * produces load_barycentric_at_offset:
463 */
464 NIR_PASS_V(nir, ir3_nir_lower_load_barycentric_at_sample);
465 NIR_PASS_V(nir, ir3_nir_lower_load_barycentric_at_offset);
466
467 NIR_PASS_V(nir, ir3_nir_move_varying_inputs);
468 }
469
470 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, false);
471
472 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
473
474 /* num_uniforms only used by ir3 for size of ubo 0 (push constants) */
475 nir->num_uniforms = MAX_PUSH_CONSTANTS_SIZE / 16;
476
477 shader->ir3_shader.compiler = dev->compiler;
478 shader->ir3_shader.type = stage;
479 shader->ir3_shader.nir = nir;
480
481 return shader;
482 }
483
484 void
485 tu_shader_destroy(struct tu_device *dev,
486 struct tu_shader *shader,
487 const VkAllocationCallbacks *alloc)
488 {
489 if (shader->ir3_shader.nir)
490 ralloc_free(shader->ir3_shader.nir);
491
492 for (uint32_t i = 0; i < 1 + shader->has_binning_pass; i++) {
493 if (shader->variants[i].ir)
494 ir3_destroy(shader->variants[i].ir);
495 }
496
497 if (shader->ir3_shader.const_state.immediates)
498 free(shader->ir3_shader.const_state.immediates);
499 if (shader->binary)
500 free(shader->binary);
501 if (shader->binning_binary)
502 free(shader->binning_binary);
503
504 vk_free2(&dev->alloc, alloc, shader);
505 }
506
507 void
508 tu_shader_compile_options_init(
509 struct tu_shader_compile_options *options,
510 const VkGraphicsPipelineCreateInfo *pipeline_info)
511 {
512 *options = (struct tu_shader_compile_options) {
513 /* TODO ir3_key */
514
515 /* TODO: VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT
516 * some optimizations need to happen otherwise shader might not compile
517 */
518 .optimize = true,
519 .include_binning_pass = true,
520 };
521 }
522
523 static uint32_t *
524 tu_compile_shader_variant(struct ir3_shader *shader,
525 const struct ir3_shader_key *key,
526 struct ir3_shader_variant *nonbinning,
527 struct ir3_shader_variant *variant)
528 {
529 variant->shader = shader;
530 variant->type = shader->type;
531 variant->key = *key;
532 variant->binning_pass = !!nonbinning;
533 variant->nonbinning = nonbinning;
534
535 int ret = ir3_compile_shader_nir(shader->compiler, variant);
536 if (ret)
537 return NULL;
538
539 /* when assemble fails, we rely on tu_shader_destroy to clean up the
540 * variant
541 */
542 return ir3_shader_assemble(variant, shader->compiler->gpu_id);
543 }
544
545 VkResult
546 tu_shader_compile(struct tu_device *dev,
547 struct tu_shader *shader,
548 const struct tu_shader *next_stage,
549 const struct tu_shader_compile_options *options,
550 const VkAllocationCallbacks *alloc)
551 {
552 if (options->optimize) {
553 /* ignore the key for the first pass of optimization */
554 ir3_optimize_nir(&shader->ir3_shader, shader->ir3_shader.nir, NULL);
555
556 if (unlikely(dev->physical_device->instance->debug_flags &
557 TU_DEBUG_NIR)) {
558 fprintf(stderr, "optimized nir:\n");
559 nir_print_shader(shader->ir3_shader.nir, stderr);
560 }
561 }
562
563 shader->binary = tu_compile_shader_variant(
564 &shader->ir3_shader, &options->key, NULL, &shader->variants[0]);
565 if (!shader->binary)
566 return VK_ERROR_OUT_OF_HOST_MEMORY;
567
568 /* compile another variant for the binning pass */
569 if (options->include_binning_pass &&
570 shader->ir3_shader.type == MESA_SHADER_VERTEX) {
571 shader->binning_binary = tu_compile_shader_variant(
572 &shader->ir3_shader, &options->key, &shader->variants[0],
573 &shader->variants[1]);
574 if (!shader->binning_binary)
575 return VK_ERROR_OUT_OF_HOST_MEMORY;
576
577 shader->has_binning_pass = true;
578 }
579
580 if (unlikely(dev->physical_device->instance->debug_flags & TU_DEBUG_IR3)) {
581 fprintf(stderr, "disassembled ir3:\n");
582 fprintf(stderr, "shader: %s\n",
583 gl_shader_stage_name(shader->ir3_shader.type));
584 ir3_shader_disasm(&shader->variants[0], shader->binary, stderr);
585
586 if (shader->has_binning_pass) {
587 fprintf(stderr, "disassembled ir3:\n");
588 fprintf(stderr, "shader: %s (binning)\n",
589 gl_shader_stage_name(shader->ir3_shader.type));
590 ir3_shader_disasm(&shader->variants[1], shader->binning_binary,
591 stderr);
592 }
593 }
594
595 return VK_SUCCESS;
596 }
597
598 VkResult
599 tu_CreateShaderModule(VkDevice _device,
600 const VkShaderModuleCreateInfo *pCreateInfo,
601 const VkAllocationCallbacks *pAllocator,
602 VkShaderModule *pShaderModule)
603 {
604 TU_FROM_HANDLE(tu_device, device, _device);
605 struct tu_shader_module *module;
606
607 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
608 assert(pCreateInfo->flags == 0);
609 assert(pCreateInfo->codeSize % 4 == 0);
610
611 module = vk_alloc2(&device->alloc, pAllocator,
612 sizeof(*module) + pCreateInfo->codeSize, 8,
613 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
614 if (module == NULL)
615 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
616
617 module->code_size = pCreateInfo->codeSize;
618 memcpy(module->code, pCreateInfo->pCode, pCreateInfo->codeSize);
619
620 _mesa_sha1_compute(module->code, module->code_size, module->sha1);
621
622 *pShaderModule = tu_shader_module_to_handle(module);
623
624 return VK_SUCCESS;
625 }
626
627 void
628 tu_DestroyShaderModule(VkDevice _device,
629 VkShaderModule _module,
630 const VkAllocationCallbacks *pAllocator)
631 {
632 TU_FROM_HANDLE(tu_device, device, _device);
633 TU_FROM_HANDLE(tu_shader_module, module, _module);
634
635 if (!module)
636 return;
637
638 vk_free2(&device->alloc, pAllocator, module);
639 }