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_store_output(nir_builder
*b
,
121 struct nir_instr
*instr
)
123 if (instr
->type
!= nir_instr_type_intrinsic
)
126 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
127 if (intr
->intrinsic
!= nir_intrinsic_store_output
)
130 if (nir_intrinsic_base(intr
) != VARYING_SLOT_POS
)
133 b
->cursor
= nir_before_instr(&intr
->instr
);
135 nir_ssa_def
*src
= nir_ssa_for_src(b
, intr
->src
[0], 4);
136 nir_ssa_def
*def
= nir_vec4(b
,
137 nir_channel(b
, src
, 0),
138 nir_channel(b
, src
, 1),
141 nir_channel(b
, src
, 2),
142 nir_channel(b
, src
, 3)),
143 nir_imm_float(b
, 0.5)),
144 nir_channel(b
, src
, 3));
145 nir_instr_rewrite_src(&intr
->instr
, &intr
->src
[0], nir_src_for_ssa(def
));
149 position_to_vulkan(nir_shader
*s
)
151 if (s
->info
.stage
!= MESA_SHADER_VERTEX
)
154 nir_foreach_function(function
, s
) {
155 if (function
->impl
) {
157 nir_builder_init(&b
, function
->impl
);
159 nir_foreach_block(block
, function
->impl
) {
160 nir_foreach_instr_safe(instr
, block
)
161 lower_store_output(&b
, instr
);
164 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
165 nir_metadata_dominance
);
171 lower_discard_if_instr(nir_intrinsic_instr
*instr
, nir_builder
*b
)
173 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
174 b
->cursor
= nir_before_instr(&instr
->instr
);
176 nir_if
*if_stmt
= nir_push_if(b
, nir_ssa_for_src(b
, instr
->src
[0], 1));
177 nir_intrinsic_instr
*discard
=
178 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_discard
);
179 nir_builder_instr_insert(b
, &discard
->instr
);
180 nir_pop_if(b
, if_stmt
);
181 nir_instr_remove(&instr
->instr
);
184 assert(instr
->intrinsic
!= nir_intrinsic_discard
||
185 nir_block_last_instr(instr
->instr
.block
) == &instr
->instr
);
191 lower_discard_if(nir_shader
*shader
)
193 bool progress
= false;
195 nir_foreach_function(function
, shader
) {
196 if (function
->impl
) {
198 nir_builder_init(&builder
, function
->impl
);
199 nir_foreach_block(block
, function
->impl
) {
200 nir_foreach_instr_safe(instr
, block
) {
201 if (instr
->type
== nir_instr_type_intrinsic
)
202 progress
|= lower_discard_if_instr(
203 nir_instr_as_intrinsic(instr
),
208 nir_metadata_preserve(function
->impl
, nir_metadata_dominance
);
215 static const struct nir_shader_compiler_options nir_options
= {
216 .lower_all_io_to_temps
= true,
218 .lower_flrp32
= true,
224 zink_get_compiler_options(struct pipe_screen
*screen
,
225 enum pipe_shader_ir ir
,
226 enum pipe_shader_type shader
)
228 assert(ir
== PIPE_SHADER_IR_NIR
);
233 zink_tgsi_to_nir(struct pipe_screen
*screen
, const struct tgsi_token
*tokens
)
235 if (zink_debug
& ZINK_DEBUG_TGSI
) {
236 fprintf(stderr
, "TGSI shader:\n---8<---\n");
237 tgsi_dump_to_file(tokens
, 0, stderr
);
238 fprintf(stderr
, "---8<---\n\n");
241 return tgsi_to_nir(tokens
, screen
);
245 optimize_nir(struct nir_shader
*s
)
250 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
251 NIR_PASS(progress
, s
, nir_copy_prop
);
252 NIR_PASS(progress
, s
, nir_opt_remove_phis
);
253 NIR_PASS(progress
, s
, nir_opt_dce
);
254 NIR_PASS(progress
, s
, nir_opt_dead_cf
);
255 NIR_PASS(progress
, s
, nir_opt_cse
);
256 NIR_PASS(progress
, s
, nir_opt_peephole_select
, 8, true, true);
257 NIR_PASS(progress
, s
, nir_opt_algebraic
);
258 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
259 NIR_PASS(progress
, s
, nir_opt_undef
);
264 glsl_type_size(const struct glsl_type
*type
, bool bindless
)
266 return glsl_count_attribute_slots(type
, false);
270 zink_binding(enum pipe_shader_type stage
, VkDescriptorType type
, int index
)
272 if (stage
== PIPE_SHADER_COMPUTE
) {
273 unreachable("not supported");
275 uint32_t stage_offset
= (uint32_t)stage
* (PIPE_MAX_CONSTANT_BUFFERS
+
276 PIPE_MAX_SHADER_SAMPLER_VIEWS
);
279 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
280 assert(index
< PIPE_MAX_CONSTANT_BUFFERS
);
281 return stage_offset
+ index
;
283 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
284 assert(index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
285 return stage_offset
+ PIPE_MAX_CONSTANT_BUFFERS
+ index
;
288 unreachable("unexpected type");
294 zink_compile_nir(struct zink_screen
*screen
, struct nir_shader
*nir
)
296 struct zink_shader
*ret
= CALLOC_STRUCT(zink_shader
);
298 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, glsl_type_size
, (nir_lower_io_options
)0);
299 NIR_PASS_V(nir
, lower_uniforms_to_ubo
);
300 NIR_PASS_V(nir
, position_to_vulkan
);
301 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
);
302 NIR_PASS_V(nir
, nir_lower_bool_to_float
);
304 NIR_PASS_V(nir
, nir_remove_dead_variables
, nir_var_function_temp
);
305 NIR_PASS_V(nir
, lower_discard_if
);
307 if (zink_debug
& ZINK_DEBUG_NIR
) {
308 fprintf(stderr
, "NIR shader:\n---8<---\n");
309 nir_print_shader(nir
, stderr
);
310 fprintf(stderr
, "---8<---\n");
313 enum pipe_shader_type stage
= pipe_shader_type_from_mesa(nir
->info
.stage
);
315 ret
->num_bindings
= 0;
316 nir_foreach_variable(var
, &nir
->uniforms
) {
317 if (glsl_type_is_sampler(var
->type
)) {
318 ret
->bindings
[ret
->num_bindings
].index
= var
->data
.driver_location
;
319 var
->data
.binding
= zink_binding(stage
, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
, var
->data
.driver_location
);
320 ret
->bindings
[ret
->num_bindings
].binding
= var
->data
.binding
;
321 ret
->bindings
[ret
->num_bindings
].type
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
;
323 } else if (var
->interface_type
) {
324 ret
->bindings
[ret
->num_bindings
].index
= var
->data
.binding
;
325 var
->data
.binding
= zink_binding(stage
, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
, var
->data
.binding
);
326 ret
->bindings
[ret
->num_bindings
].binding
= var
->data
.binding
;
327 ret
->bindings
[ret
->num_bindings
].type
= VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
;
332 struct spirv_shader
*spirv
= nir_to_spirv(nir
);
335 if (zink_debug
& ZINK_DEBUG_SPIRV
) {
338 snprintf(buf
, sizeof(buf
), "dump%02d.spv", i
++);
339 FILE *fp
= fopen(buf
, "wb");
340 fwrite(spirv
->words
, sizeof(uint32_t), spirv
->num_words
, fp
);
342 fprintf(stderr
, "wrote '%s'...\n", buf
);
345 VkShaderModuleCreateInfo smci
= {};
346 smci
.sType
= VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
;
347 smci
.codeSize
= spirv
->num_words
* sizeof(uint32_t);
348 smci
.pCode
= spirv
->words
;
350 if (vkCreateShaderModule(screen
->dev
, &smci
, NULL
, &ret
->shader_module
) != VK_SUCCESS
)
357 zink_shader_free(struct zink_screen
*screen
, struct zink_shader
*shader
)
359 vkDestroyShaderModule(screen
->dev
, shader
->shader_module
, NULL
);