2 * Copyright © 2019 Google LLC
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
21 * DEALINGS IN THE SOFTWARE.
24 #include "tu_private.h"
26 #include "spirv/nir_spirv.h"
27 #include "util/mesa-sha1.h"
28 #include "nir/nir_xfb_info.h"
29 #include "nir/nir_vulkan.h"
32 #include "ir3/ir3_nir.h"
35 tu_spirv_to_nir(struct ir3_compiler
*compiler
,
36 const uint32_t *words
,
38 gl_shader_stage stage
,
39 const char *entry_point_name
,
40 const VkSpecializationInfo
*spec_info
)
42 /* TODO these are made-up */
43 const struct spirv_to_nir_options spirv_options
= {
44 .frag_coord_is_sysval
= true,
45 .lower_ubo_ssbo_access_to_offsets
= true,
47 .transform_feedback
= compiler
->gpu_id
>= 600,
50 const nir_shader_compiler_options
*nir_options
=
51 ir3_get_compiler_options(compiler
);
53 /* convert VkSpecializationInfo */
54 struct nir_spirv_specialization
*spec
= NULL
;
55 uint32_t num_spec
= 0;
56 if (spec_info
&& spec_info
->mapEntryCount
) {
57 spec
= calloc(spec_info
->mapEntryCount
, sizeof(*spec
));
61 for (uint32_t i
= 0; i
< spec_info
->mapEntryCount
; i
++) {
62 const VkSpecializationMapEntry
*entry
= &spec_info
->pMapEntries
[i
];
63 const void *data
= spec_info
->pData
+ entry
->offset
;
64 assert(data
+ entry
->size
<= spec_info
->pData
+ spec_info
->dataSize
);
65 spec
[i
].id
= entry
->constantID
;
66 switch (entry
->size
) {
68 spec
[i
].value
.u64
= *(const uint64_t *)data
;
71 spec
[i
].value
.u32
= *(const uint32_t *)data
;
74 spec
[i
].value
.u16
= *(const uint16_t *)data
;
77 spec
[i
].value
.u8
= *(const uint8_t *)data
;
80 assert(!"Invalid spec constant size");
83 spec
[i
].defined_on_module
= false;
86 num_spec
= spec_info
->mapEntryCount
;
90 spirv_to_nir(words
, word_count
, spec
, num_spec
, stage
, entry_point_name
,
91 &spirv_options
, nir_options
);
95 assert(nir
->info
.stage
== stage
);
96 nir_validate_shader(nir
, "after spirv_to_nir");
102 lower_load_push_constant(nir_builder
*b
, nir_intrinsic_instr
*instr
,
103 struct tu_shader
*shader
)
105 nir_intrinsic_instr
*load
=
106 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_load_uniform
);
107 load
->num_components
= instr
->num_components
;
108 uint32_t base
= nir_intrinsic_base(instr
);
109 assert(base
% 4 == 0);
110 assert(base
>= shader
->push_consts
.lo
* 16);
111 base
-= shader
->push_consts
.lo
* 16;
112 nir_intrinsic_set_base(load
, base
/ 4);
114 nir_src_for_ssa(nir_ushr(b
, instr
->src
[0].ssa
, nir_imm_int(b
, 2)));
115 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
116 load
->num_components
, instr
->dest
.ssa
.bit_size
,
117 instr
->dest
.ssa
.name
);
118 nir_builder_instr_insert(b
, &load
->instr
);
119 nir_ssa_def_rewrite_uses(&instr
->dest
.ssa
, nir_src_for_ssa(&load
->dest
.ssa
));
121 nir_instr_remove(&instr
->instr
);
125 lower_vulkan_resource_index(nir_builder
*b
, nir_intrinsic_instr
*instr
,
126 struct tu_shader
*shader
,
127 const struct tu_pipeline_layout
*layout
)
129 nir_ssa_def
*vulkan_idx
= instr
->src
[0].ssa
;
131 unsigned set
= nir_intrinsic_desc_set(instr
);
132 unsigned binding
= nir_intrinsic_binding(instr
);
133 struct tu_descriptor_set_layout
*set_layout
= layout
->set
[set
].layout
;
134 struct tu_descriptor_set_binding_layout
*binding_layout
=
135 &set_layout
->binding
[binding
];
138 shader
->active_desc_sets
|= 1u << set
;
140 switch (binding_layout
->type
) {
141 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
142 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
143 base
= layout
->set
[set
].dynamic_offset_start
+
144 binding_layout
->dynamic_offset_offset
+
145 layout
->input_attachment_count
;
149 base
= binding_layout
->offset
/ (4 * A6XX_TEX_CONST_DWORDS
);
153 nir_intrinsic_instr
*bindless
=
154 nir_intrinsic_instr_create(b
->shader
,
155 nir_intrinsic_bindless_resource_ir3
);
156 bindless
->num_components
= 0;
157 nir_ssa_dest_init(&bindless
->instr
, &bindless
->dest
,
159 nir_intrinsic_set_desc_set(bindless
, set
);
160 bindless
->src
[0] = nir_src_for_ssa(nir_iadd(b
, nir_imm_int(b
, base
), vulkan_idx
));
161 nir_builder_instr_insert(b
, &bindless
->instr
);
163 nir_ssa_def_rewrite_uses(&instr
->dest
.ssa
,
164 nir_src_for_ssa(&bindless
->dest
.ssa
));
165 nir_instr_remove(&instr
->instr
);
169 build_bindless(nir_builder
*b
, nir_deref_instr
*deref
, bool is_sampler
,
170 struct tu_shader
*shader
,
171 const struct tu_pipeline_layout
*layout
)
173 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
175 unsigned set
= var
->data
.descriptor_set
;
176 unsigned binding
= var
->data
.binding
;
177 const struct tu_descriptor_set_binding_layout
*bind_layout
=
178 &layout
->set
[set
].layout
->binding
[binding
];
180 shader
->active_desc_sets
|= 1u << set
;
182 nir_ssa_def
*desc_offset
;
183 unsigned descriptor_stride
;
184 if (bind_layout
->type
== VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
) {
186 layout
->set
[set
].input_attachment_start
+
187 bind_layout
->input_attachment_offset
;
188 desc_offset
= nir_imm_int(b
, offset
);
190 descriptor_stride
= 1;
193 /* Samplers come second in combined image/sampler descriptors, see
194 * write_combined_image_sampler_descriptor().
196 if (is_sampler
&& bind_layout
->type
==
197 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
) {
201 nir_imm_int(b
, (bind_layout
->offset
/ (4 * A6XX_TEX_CONST_DWORDS
)) +
203 descriptor_stride
= bind_layout
->size
/ (4 * A6XX_TEX_CONST_DWORDS
);
206 if (deref
->deref_type
!= nir_deref_type_var
) {
207 assert(deref
->deref_type
== nir_deref_type_array
);
209 nir_ssa_def
*arr_index
= nir_ssa_for_src(b
, deref
->arr
.index
, 1);
210 desc_offset
= nir_iadd(b
, desc_offset
,
211 nir_imul_imm(b
, arr_index
, descriptor_stride
));
214 nir_intrinsic_instr
*bindless
=
215 nir_intrinsic_instr_create(b
->shader
,
216 nir_intrinsic_bindless_resource_ir3
);
217 bindless
->num_components
= 0;
218 nir_ssa_dest_init(&bindless
->instr
, &bindless
->dest
,
220 nir_intrinsic_set_desc_set(bindless
, set
);
221 bindless
->src
[0] = nir_src_for_ssa(desc_offset
);
222 nir_builder_instr_insert(b
, &bindless
->instr
);
224 return &bindless
->dest
.ssa
;
228 lower_image_deref(nir_builder
*b
,
229 nir_intrinsic_instr
*instr
, struct tu_shader
*shader
,
230 const struct tu_pipeline_layout
*layout
)
232 nir_deref_instr
*deref
= nir_src_as_deref(instr
->src
[0]);
233 nir_ssa_def
*bindless
= build_bindless(b
, deref
, false, shader
, layout
);
234 nir_rewrite_image_intrinsic(instr
, bindless
, true);
238 lower_intrinsic(nir_builder
*b
, nir_intrinsic_instr
*instr
,
239 struct tu_shader
*shader
,
240 const struct tu_pipeline_layout
*layout
)
242 switch (instr
->intrinsic
) {
243 case nir_intrinsic_load_layer_id
:
244 /* TODO: remove this when layered rendering is implemented */
245 nir_ssa_def_rewrite_uses(&instr
->dest
.ssa
,
246 nir_src_for_ssa(nir_imm_int(b
, 0)));
247 nir_instr_remove(&instr
->instr
);
250 case nir_intrinsic_load_push_constant
:
251 lower_load_push_constant(b
, instr
, shader
);
254 case nir_intrinsic_vulkan_resource_index
:
255 lower_vulkan_resource_index(b
, instr
, shader
, layout
);
258 case nir_intrinsic_image_deref_load
:
259 case nir_intrinsic_image_deref_store
:
260 case nir_intrinsic_image_deref_atomic_add
:
261 case nir_intrinsic_image_deref_atomic_imin
:
262 case nir_intrinsic_image_deref_atomic_umin
:
263 case nir_intrinsic_image_deref_atomic_imax
:
264 case nir_intrinsic_image_deref_atomic_umax
:
265 case nir_intrinsic_image_deref_atomic_and
:
266 case nir_intrinsic_image_deref_atomic_or
:
267 case nir_intrinsic_image_deref_atomic_xor
:
268 case nir_intrinsic_image_deref_atomic_exchange
:
269 case nir_intrinsic_image_deref_atomic_comp_swap
:
270 case nir_intrinsic_image_deref_size
:
271 case nir_intrinsic_image_deref_samples
:
272 lower_image_deref(b
, instr
, shader
, layout
);
281 lower_tex_ycbcr(const struct tu_pipeline_layout
*layout
,
282 nir_builder
*builder
,
285 int deref_src_idx
= nir_tex_instr_src_index(tex
, nir_tex_src_texture_deref
);
286 assert(deref_src_idx
>= 0);
287 nir_deref_instr
*deref
= nir_src_as_deref(tex
->src
[deref_src_idx
].src
);
289 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
290 const struct tu_descriptor_set_layout
*set_layout
=
291 layout
->set
[var
->data
.descriptor_set
].layout
;
292 const struct tu_descriptor_set_binding_layout
*binding
=
293 &set_layout
->binding
[var
->data
.binding
];
294 const struct tu_sampler_ycbcr_conversion
*ycbcr_samplers
=
295 tu_immutable_ycbcr_samplers(set_layout
, binding
);
300 /* For the following instructions, we don't apply any change */
301 if (tex
->op
== nir_texop_txs
||
302 tex
->op
== nir_texop_query_levels
||
303 tex
->op
== nir_texop_lod
)
306 assert(tex
->texture_index
== 0);
307 unsigned array_index
= 0;
308 if (deref
->deref_type
!= nir_deref_type_var
) {
309 assert(deref
->deref_type
== nir_deref_type_array
);
310 if (!nir_src_is_const(deref
->arr
.index
))
312 array_index
= nir_src_as_uint(deref
->arr
.index
);
313 array_index
= MIN2(array_index
, binding
->array_size
- 1);
315 const struct tu_sampler_ycbcr_conversion
*ycbcr_sampler
= ycbcr_samplers
+ array_index
;
317 if (ycbcr_sampler
->ycbcr_model
== VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY
)
320 builder
->cursor
= nir_after_instr(&tex
->instr
);
322 uint8_t bits
= vk_format_get_component_bits(ycbcr_sampler
->format
,
323 UTIL_FORMAT_COLORSPACE_RGB
,
325 uint32_t bpcs
[3] = {bits
, bits
, bits
}; /* TODO: use right bpc for each channel ? */
326 nir_ssa_def
*result
= nir_convert_ycbcr_to_rgb(builder
,
327 ycbcr_sampler
->ycbcr_model
,
328 ycbcr_sampler
->ycbcr_range
,
331 nir_ssa_def_rewrite_uses_after(&tex
->dest
.ssa
, nir_src_for_ssa(result
),
332 result
->parent_instr
);
334 builder
->cursor
= nir_before_instr(&tex
->instr
);
338 lower_tex(nir_builder
*b
, nir_tex_instr
*tex
,
339 struct tu_shader
*shader
, const struct tu_pipeline_layout
*layout
)
341 lower_tex_ycbcr(layout
, b
, tex
);
343 int sampler_src_idx
= nir_tex_instr_src_index(tex
, nir_tex_src_sampler_deref
);
344 if (sampler_src_idx
>= 0) {
345 nir_deref_instr
*deref
= nir_src_as_deref(tex
->src
[sampler_src_idx
].src
);
346 nir_ssa_def
*bindless
= build_bindless(b
, deref
, true, shader
, layout
);
347 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[sampler_src_idx
].src
,
348 nir_src_for_ssa(bindless
));
349 tex
->src
[sampler_src_idx
].src_type
= nir_tex_src_sampler_handle
;
352 int tex_src_idx
= nir_tex_instr_src_index(tex
, nir_tex_src_texture_deref
);
353 if (tex_src_idx
>= 0) {
354 nir_deref_instr
*deref
= nir_src_as_deref(tex
->src
[tex_src_idx
].src
);
355 nir_ssa_def
*bindless
= build_bindless(b
, deref
, false, shader
, layout
);
356 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex_src_idx
].src
,
357 nir_src_for_ssa(bindless
));
358 tex
->src
[tex_src_idx
].src_type
= nir_tex_src_texture_handle
;
365 lower_impl(nir_function_impl
*impl
, struct tu_shader
*shader
,
366 const struct tu_pipeline_layout
*layout
)
369 nir_builder_init(&b
, impl
);
370 bool progress
= false;
372 nir_foreach_block(block
, impl
) {
373 nir_foreach_instr_safe(instr
, block
) {
374 b
.cursor
= nir_before_instr(instr
);
375 switch (instr
->type
) {
376 case nir_instr_type_tex
:
377 progress
|= lower_tex(&b
, nir_instr_as_tex(instr
), shader
, layout
);
379 case nir_instr_type_intrinsic
:
380 progress
|= lower_intrinsic(&b
, nir_instr_as_intrinsic(instr
), shader
, layout
);
392 /* Figure out the range of push constants that we're actually going to push to
393 * the shader, and tell the backend to reserve this range when pushing UBO
398 gather_push_constants(nir_shader
*shader
, struct tu_shader
*tu_shader
)
400 uint32_t min
= UINT32_MAX
, max
= 0;
401 nir_foreach_function(function
, shader
) {
405 nir_foreach_block(block
, function
->impl
) {
406 nir_foreach_instr_safe(instr
, block
) {
407 if (instr
->type
!= nir_instr_type_intrinsic
)
410 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
411 if (intrin
->intrinsic
!= nir_intrinsic_load_push_constant
)
414 uint32_t base
= nir_intrinsic_base(intrin
);
415 uint32_t range
= nir_intrinsic_range(intrin
);
416 min
= MIN2(min
, base
);
417 max
= MAX2(max
, base
+ range
);
424 tu_shader
->push_consts
.lo
= 0;
425 tu_shader
->push_consts
.count
= 0;
429 /* CP_LOAD_STATE OFFSET and NUM_UNIT are in units of vec4 (4 dwords),
430 * however there's an alignment requirement of 4 on OFFSET. Expand the
431 * range and change units accordingly.
433 tu_shader
->push_consts
.lo
= (min
/ 16) / 4 * 4;
434 tu_shader
->push_consts
.count
=
435 align(max
, 16) / 16 - tu_shader
->push_consts
.lo
;
438 /* Gather the InputAttachmentIndex for each input attachment from the NIR
439 * shader and organize the info in a way so that draw-time patching is easy.
442 gather_input_attachments(nir_shader
*shader
, struct tu_shader
*tu_shader
,
443 const struct tu_pipeline_layout
*layout
)
445 nir_foreach_variable(var
, &shader
->uniforms
) {
446 const struct glsl_type
*glsl_type
= glsl_without_array(var
->type
);
448 if (!glsl_type_is_image(glsl_type
))
451 enum glsl_sampler_dim dim
= glsl_get_sampler_dim(glsl_type
);
453 const uint32_t set
= var
->data
.descriptor_set
;
454 const uint32_t binding
= var
->data
.binding
;
455 const struct tu_descriptor_set_binding_layout
*bind_layout
=
456 &layout
->set
[set
].layout
->binding
[binding
];
457 const uint32_t array_size
= bind_layout
->array_size
;
459 if (dim
== GLSL_SAMPLER_DIM_SUBPASS
||
460 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) {
462 layout
->set
[set
].input_attachment_start
+
463 bind_layout
->input_attachment_offset
;
464 for (unsigned i
= 0; i
< array_size
; i
++)
465 tu_shader
->attachment_idx
[offset
+ i
] = var
->data
.index
+ i
;
471 tu_lower_io(nir_shader
*shader
, struct tu_shader
*tu_shader
,
472 const struct tu_pipeline_layout
*layout
)
474 bool progress
= false;
476 gather_push_constants(shader
, tu_shader
);
477 gather_input_attachments(shader
, tu_shader
, layout
);
479 nir_foreach_function(function
, shader
) {
481 progress
|= lower_impl(function
->impl
, tu_shader
, layout
);
484 /* Remove now-unused variables so that when we gather the shader info later
485 * they won't be counted.
487 nir_remove_dead_variables(shader
,
488 nir_var_uniform
| nir_var_mem_ubo
| nir_var_mem_ssbo
,
495 tu_gather_xfb_info(nir_shader
*nir
, struct ir3_stream_output_info
*info
)
497 nir_xfb_info
*xfb
= nir_gather_xfb_info(nir
, NULL
);
502 /* creating a map from VARYING_SLOT_* enums to consecutive index */
503 uint8_t num_outputs
= 0;
504 uint64_t outputs_written
= 0;
505 for (int i
= 0; i
< xfb
->output_count
; i
++)
506 outputs_written
|= BITFIELD64_BIT(xfb
->outputs
[i
].location
);
508 uint8_t output_map
[VARYING_SLOT_TESS_MAX
];
509 memset(output_map
, 0, sizeof(output_map
));
511 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
512 if (outputs_written
& BITFIELD64_BIT(attr
))
513 output_map
[attr
] = num_outputs
++;
516 assert(xfb
->output_count
< IR3_MAX_SO_OUTPUTS
);
517 info
->num_outputs
= xfb
->output_count
;
519 for (int i
= 0; i
< IR3_MAX_SO_BUFFERS
; i
++)
520 info
->stride
[i
] = xfb
->buffers
[i
].stride
/ 4;
522 for (int i
= 0; i
< xfb
->output_count
; i
++) {
523 info
->output
[i
].register_index
= output_map
[xfb
->outputs
[i
].location
];
524 info
->output
[i
].start_component
= xfb
->outputs
[i
].component_offset
;
525 info
->output
[i
].num_components
=
526 util_bitcount(xfb
->outputs
[i
].component_mask
);
527 info
->output
[i
].output_buffer
= xfb
->outputs
[i
].buffer
;
528 info
->output
[i
].dst_offset
= xfb
->outputs
[i
].offset
/ 4;
529 info
->output
[i
].stream
= xfb
->buffer_to_stream
[xfb
->outputs
[i
].buffer
];
536 tu_shader_create(struct tu_device
*dev
,
537 gl_shader_stage stage
,
538 const VkPipelineShaderStageCreateInfo
*stage_info
,
539 struct tu_pipeline_layout
*layout
,
540 const VkAllocationCallbacks
*alloc
)
542 struct tu_shader
*shader
;
547 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
553 /* translate SPIR-V to NIR */
554 const struct tu_shader_module
*module
=
555 tu_shader_module_from_handle(stage_info
->module
);
556 assert(module
->code_size
% 4 == 0);
557 nir
= tu_spirv_to_nir(
558 dev
->compiler
, (const uint32_t *) module
->code
, module
->code_size
/ 4,
559 stage
, stage_info
->pName
, stage_info
->pSpecializationInfo
);
561 assert(stage
== MESA_SHADER_FRAGMENT
);
563 const nir_shader_compiler_options
*nir_options
=
564 ir3_get_compiler_options(dev
->compiler
);
565 nir_builder_init_simple_shader(&fs_b
, NULL
, MESA_SHADER_FRAGMENT
, nir_options
);
566 fs_b
.shader
->info
.name
= ralloc_strdup(fs_b
.shader
, "noop_fs");
571 vk_free2(&dev
->alloc
, alloc
, shader
);
575 if (unlikely(dev
->physical_device
->instance
->debug_flags
& TU_DEBUG_NIR
)) {
576 fprintf(stderr
, "translated nir:\n");
577 nir_print_shader(nir
, stderr
);
580 /* multi step inlining procedure */
581 NIR_PASS_V(nir
, nir_lower_variable_initializers
, nir_var_function_temp
);
582 NIR_PASS_V(nir
, nir_lower_returns
);
583 NIR_PASS_V(nir
, nir_inline_functions
);
584 NIR_PASS_V(nir
, nir_opt_deref
);
585 foreach_list_typed_safe(nir_function
, func
, node
, &nir
->functions
) {
586 if (!func
->is_entrypoint
)
587 exec_node_remove(&func
->node
);
589 assert(exec_list_length(&nir
->functions
) == 1);
590 NIR_PASS_V(nir
, nir_lower_variable_initializers
, ~nir_var_function_temp
);
592 /* Split member structs. We do this before lower_io_to_temporaries so that
593 * it doesn't lower system values to temporaries by accident.
595 NIR_PASS_V(nir
, nir_split_var_copies
);
596 NIR_PASS_V(nir
, nir_split_per_member_structs
);
598 NIR_PASS_V(nir
, nir_remove_dead_variables
,
599 nir_var_shader_in
| nir_var_shader_out
| nir_var_system_value
| nir_var_mem_shared
,
602 /* Gather information for transform feedback.
603 * This should be called after nir_split_per_member_structs.
604 * Also needs to be called after nir_remove_dead_variables with varyings,
605 * so that we could align stream outputs correctly.
607 struct ir3_stream_output_info so_info
= {};
608 if (nir
->info
.stage
== MESA_SHADER_VERTEX
||
609 nir
->info
.stage
== MESA_SHADER_TESS_EVAL
||
610 nir
->info
.stage
== MESA_SHADER_GEOMETRY
)
611 tu_gather_xfb_info(nir
, &so_info
);
613 NIR_PASS_V(nir
, nir_propagate_invariant
);
615 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
, nir_shader_get_entrypoint(nir
), true, true);
617 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
618 NIR_PASS_V(nir
, nir_split_var_copies
);
619 NIR_PASS_V(nir
, nir_lower_var_copies
);
621 NIR_PASS_V(nir
, nir_opt_copy_prop_vars
);
622 NIR_PASS_V(nir
, nir_opt_combine_stores
, nir_var_all
);
624 /* ir3 doesn't support indirect input/output */
625 NIR_PASS_V(nir
, nir_lower_indirect_derefs
, nir_var_shader_in
| nir_var_shader_out
);
627 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, false);
629 nir_assign_io_var_locations(&nir
->inputs
, &nir
->num_inputs
, stage
);
630 nir_assign_io_var_locations(&nir
->outputs
, &nir
->num_outputs
, stage
);
632 NIR_PASS_V(nir
, nir_lower_system_values
);
633 NIR_PASS_V(nir
, nir_lower_frexp
);
635 if (stage
== MESA_SHADER_FRAGMENT
)
636 NIR_PASS_V(nir
, nir_lower_input_attachments
, true);
638 NIR_PASS_V(nir
, tu_lower_io
, shader
, layout
);
640 nir_shader_gather_info(nir
, nir_shader_get_entrypoint(nir
));
643 ir3_shader_from_nir(dev
->compiler
, nir
,
644 align(shader
->push_consts
.count
, 4),
651 tu_shader_destroy(struct tu_device
*dev
,
652 struct tu_shader
*shader
,
653 const VkAllocationCallbacks
*alloc
)
655 ir3_shader_destroy(shader
->ir3_shader
);
657 vk_free2(&dev
->alloc
, alloc
, shader
);
661 tu_CreateShaderModule(VkDevice _device
,
662 const VkShaderModuleCreateInfo
*pCreateInfo
,
663 const VkAllocationCallbacks
*pAllocator
,
664 VkShaderModule
*pShaderModule
)
666 TU_FROM_HANDLE(tu_device
, device
, _device
);
667 struct tu_shader_module
*module
;
669 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
);
670 assert(pCreateInfo
->flags
== 0);
671 assert(pCreateInfo
->codeSize
% 4 == 0);
673 module
= vk_alloc2(&device
->alloc
, pAllocator
,
674 sizeof(*module
) + pCreateInfo
->codeSize
, 8,
675 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
677 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
679 module
->code_size
= pCreateInfo
->codeSize
;
680 memcpy(module
->code
, pCreateInfo
->pCode
, pCreateInfo
->codeSize
);
682 _mesa_sha1_compute(module
->code
, module
->code_size
, module
->sha1
);
684 *pShaderModule
= tu_shader_module_to_handle(module
);
690 tu_DestroyShaderModule(VkDevice _device
,
691 VkShaderModule _module
,
692 const VkAllocationCallbacks
*pAllocator
)
694 TU_FROM_HANDLE(tu_device
, device
, _device
);
695 TU_FROM_HANDLE(tu_shader_module
, module
, _module
);
700 vk_free2(&device
->alloc
, pAllocator
, module
);