2 * Copyright 2018 Collabora Ltd.
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.
24 #include "zink_compiler.h"
25 #include "zink_screen.h"
26 #include "nir_to_spirv/nir_to_spirv.h"
28 #include "pipe/p_state.h"
31 #include "compiler/nir/nir_builder.h"
33 #include "nir/tgsi_to_nir.h"
34 #include "tgsi/tgsi_dump.h"
35 #include "tgsi/tgsi_from_mesa.h"
37 #include "util/u_memory.h"
40 lower_instr(nir_intrinsic_instr
*instr
, nir_builder
*b
)
42 b
->cursor
= nir_before_instr(&instr
->instr
);
44 if (instr
->intrinsic
== nir_intrinsic_load_ubo
) {
45 nir_ssa_def
*old_idx
= nir_ssa_for_src(b
, instr
->src
[0], 1);
46 nir_ssa_def
*new_idx
= nir_iadd(b
, old_idx
, nir_imm_int(b
, 1));
47 nir_instr_rewrite_src(&instr
->instr
, &instr
->src
[0],
48 nir_src_for_ssa(new_idx
));
52 if (instr
->intrinsic
== nir_intrinsic_load_uniform
) {
53 nir_ssa_def
*ubo_idx
= nir_imm_int(b
, 0);
54 nir_ssa_def
*ubo_offset
=
55 nir_iadd(b
, nir_imm_int(b
, nir_intrinsic_base(instr
)),
56 nir_ssa_for_src(b
, instr
->src
[0], 1));
58 nir_intrinsic_instr
*load
=
59 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_load_ubo
);
60 load
->num_components
= instr
->num_components
;
61 load
->src
[0] = nir_src_for_ssa(ubo_idx
);
62 load
->src
[1] = nir_src_for_ssa(ubo_offset
);
63 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
64 load
->num_components
, instr
->dest
.ssa
.bit_size
,
65 instr
->dest
.ssa
.name
);
66 nir_builder_instr_insert(b
, &load
->instr
);
67 nir_ssa_def_rewrite_uses(&instr
->dest
.ssa
, nir_src_for_ssa(&load
->dest
.ssa
));
69 nir_instr_remove(&instr
->instr
);
77 lower_uniforms_to_ubo(nir_shader
*shader
)
79 bool progress
= false;
81 nir_foreach_function(function
, shader
) {
84 nir_builder_init(&builder
, function
->impl
);
85 nir_foreach_block(block
, function
->impl
) {
86 nir_foreach_instr_safe(instr
, block
) {
87 if (instr
->type
== nir_instr_type_intrinsic
)
88 progress
|= lower_instr(nir_instr_as_intrinsic(instr
),
93 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
94 nir_metadata_dominance
);
99 assert(shader
->num_uniforms
> 0);
100 const struct glsl_type
*type
= glsl_array_type(glsl_vec4_type(),
101 shader
->num_uniforms
, 0);
102 nir_variable
*ubo
= nir_variable_create(shader
, nir_var_mem_ubo
, type
,
104 ubo
->data
.binding
= 0;
106 struct glsl_struct_field field
= {
111 ubo
->interface_type
=
112 glsl_interface_type(&field
, 1, GLSL_INTERFACE_PACKING_STD430
,
113 false, "__ubo0_interface");
120 lower_discard_if_instr(nir_intrinsic_instr
*instr
, nir_builder
*b
)
122 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
123 b
->cursor
= nir_before_instr(&instr
->instr
);
125 nir_if
*if_stmt
= nir_push_if(b
, nir_ssa_for_src(b
, instr
->src
[0], 1));
126 nir_intrinsic_instr
*discard
=
127 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_discard
);
128 nir_builder_instr_insert(b
, &discard
->instr
);
129 nir_pop_if(b
, if_stmt
);
130 nir_instr_remove(&instr
->instr
);
133 assert(instr
->intrinsic
!= nir_intrinsic_discard
||
134 nir_block_last_instr(instr
->instr
.block
) == &instr
->instr
);
140 lower_discard_if(nir_shader
*shader
)
142 bool progress
= false;
144 nir_foreach_function(function
, shader
) {
145 if (function
->impl
) {
147 nir_builder_init(&builder
, function
->impl
);
148 nir_foreach_block(block
, function
->impl
) {
149 nir_foreach_instr_safe(instr
, block
) {
150 if (instr
->type
== nir_instr_type_intrinsic
)
151 progress
|= lower_discard_if_instr(
152 nir_instr_as_intrinsic(instr
),
157 nir_metadata_preserve(function
->impl
, nir_metadata_dominance
);
164 static const struct nir_shader_compiler_options nir_options
= {
165 .lower_all_io_to_temps
= true,
168 .lower_flrp32
= true,
174 zink_get_compiler_options(struct pipe_screen
*screen
,
175 enum pipe_shader_ir ir
,
176 enum pipe_shader_type shader
)
178 assert(ir
== PIPE_SHADER_IR_NIR
);
183 zink_tgsi_to_nir(struct pipe_screen
*screen
, const struct tgsi_token
*tokens
)
185 if (zink_debug
& ZINK_DEBUG_TGSI
) {
186 fprintf(stderr
, "TGSI shader:\n---8<---\n");
187 tgsi_dump_to_file(tokens
, 0, stderr
);
188 fprintf(stderr
, "---8<---\n\n");
191 return tgsi_to_nir(tokens
, screen
);
195 optimize_nir(struct nir_shader
*s
)
200 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
201 NIR_PASS(progress
, s
, nir_copy_prop
);
202 NIR_PASS(progress
, s
, nir_opt_remove_phis
);
203 NIR_PASS(progress
, s
, nir_opt_dce
);
204 NIR_PASS(progress
, s
, nir_opt_dead_cf
);
205 NIR_PASS(progress
, s
, nir_opt_cse
);
206 NIR_PASS(progress
, s
, nir_opt_peephole_select
, 8, true, true);
207 NIR_PASS(progress
, s
, nir_opt_algebraic
);
208 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
209 NIR_PASS(progress
, s
, nir_opt_undef
);
214 zink_binding(enum pipe_shader_type stage
, VkDescriptorType type
, int index
)
216 if (stage
== PIPE_SHADER_COMPUTE
) {
217 unreachable("not supported");
219 uint32_t stage_offset
= (uint32_t)stage
* (PIPE_MAX_CONSTANT_BUFFERS
+
220 PIPE_MAX_SHADER_SAMPLER_VIEWS
);
223 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
224 assert(index
< PIPE_MAX_CONSTANT_BUFFERS
);
225 return stage_offset
+ index
;
227 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
228 assert(index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
229 return stage_offset
+ PIPE_MAX_CONSTANT_BUFFERS
+ index
;
232 unreachable("unexpected type");
238 zink_compile_nir(struct zink_screen
*screen
, struct nir_shader
*nir
)
240 struct zink_shader
*ret
= CALLOC_STRUCT(zink_shader
);
242 NIR_PASS_V(nir
, lower_uniforms_to_ubo
);
243 NIR_PASS_V(nir
, nir_lower_clip_halfz
);
244 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
);
246 NIR_PASS_V(nir
, nir_remove_dead_variables
, nir_var_function_temp
);
247 NIR_PASS_V(nir
, lower_discard_if
);
248 NIR_PASS_V(nir
, nir_convert_from_ssa
, true);
250 if (zink_debug
& ZINK_DEBUG_NIR
) {
251 fprintf(stderr
, "NIR shader:\n---8<---\n");
252 nir_print_shader(nir
, stderr
);
253 fprintf(stderr
, "---8<---\n");
256 enum pipe_shader_type stage
= pipe_shader_type_from_mesa(nir
->info
.stage
);
258 ret
->num_bindings
= 0;
259 nir_foreach_variable(var
, &nir
->uniforms
) {
260 if (var
->data
.mode
== nir_var_mem_ubo
) {
261 ret
->bindings
[ret
->num_bindings
].index
= var
->data
.binding
;
262 var
->data
.binding
= zink_binding(stage
, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
, var
->data
.binding
);
263 ret
->bindings
[ret
->num_bindings
].binding
= var
->data
.binding
;
264 ret
->bindings
[ret
->num_bindings
].type
= VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
;
267 assert(var
->data
.mode
== nir_var_uniform
);
268 if (glsl_type_is_array(var
->type
) &&
269 glsl_type_is_sampler(glsl_get_array_element(var
->type
))) {
270 for (int i
= 0; i
< glsl_get_length(var
->type
); ++i
) {
271 ret
->bindings
[ret
->num_bindings
].index
= var
->data
.driver_location
+ i
;
272 var
->data
.binding
= zink_binding(stage
, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
, var
->data
.driver_location
+ i
);
273 ret
->bindings
[ret
->num_bindings
].binding
= var
->data
.binding
;
274 ret
->bindings
[ret
->num_bindings
].type
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
;
277 } else if (glsl_type_is_sampler(var
->type
)) {
278 ret
->bindings
[ret
->num_bindings
].index
= var
->data
.driver_location
;
279 var
->data
.binding
= zink_binding(stage
, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
, var
->data
.driver_location
);
280 ret
->bindings
[ret
->num_bindings
].binding
= var
->data
.binding
;
281 ret
->bindings
[ret
->num_bindings
].type
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
;
287 ret
->info
= nir
->info
;
289 struct spirv_shader
*spirv
= nir_to_spirv(nir
);
292 if (zink_debug
& ZINK_DEBUG_SPIRV
) {
295 snprintf(buf
, sizeof(buf
), "dump%02d.spv", i
++);
296 FILE *fp
= fopen(buf
, "wb");
297 fwrite(spirv
->words
, sizeof(uint32_t), spirv
->num_words
, fp
);
299 fprintf(stderr
, "wrote '%s'...\n", buf
);
302 VkShaderModuleCreateInfo smci
= {};
303 smci
.sType
= VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
;
304 smci
.codeSize
= spirv
->num_words
* sizeof(uint32_t);
305 smci
.pCode
= spirv
->words
;
307 if (vkCreateShaderModule(screen
->dev
, &smci
, NULL
, &ret
->shader_module
) != VK_SUCCESS
)
314 zink_shader_free(struct zink_screen
*screen
, struct zink_shader
*shader
)
316 vkDestroyShaderModule(screen
->dev
, shader
->shader_module
, NULL
);