zink: implement streamout and xfb handling in ntv
[mesa.git] / src / gallium / drivers / zink / zink_compiler.c
1 /*
2 * Copyright 2018 Collabora Ltd.
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 "zink_compiler.h"
25 #include "zink_program.h"
26 #include "zink_screen.h"
27 #include "nir_to_spirv/nir_to_spirv.h"
28
29 #include "pipe/p_state.h"
30
31 #include "nir.h"
32 #include "compiler/nir/nir_builder.h"
33
34 #include "nir/tgsi_to_nir.h"
35 #include "tgsi/tgsi_dump.h"
36 #include "tgsi/tgsi_from_mesa.h"
37
38 #include "util/u_memory.h"
39
40 static bool
41 lower_discard_if_instr(nir_intrinsic_instr *instr, nir_builder *b)
42 {
43 if (instr->intrinsic == nir_intrinsic_discard_if) {
44 b->cursor = nir_before_instr(&instr->instr);
45
46 nir_if *if_stmt = nir_push_if(b, nir_ssa_for_src(b, instr->src[0], 1));
47 nir_intrinsic_instr *discard =
48 nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard);
49 nir_builder_instr_insert(b, &discard->instr);
50 nir_pop_if(b, if_stmt);
51 nir_instr_remove(&instr->instr);
52 return true;
53 }
54 assert(instr->intrinsic != nir_intrinsic_discard ||
55 nir_block_last_instr(instr->instr.block) == &instr->instr);
56
57 return false;
58 }
59
60 static bool
61 lower_discard_if(nir_shader *shader)
62 {
63 bool progress = false;
64
65 nir_foreach_function(function, shader) {
66 if (function->impl) {
67 nir_builder builder;
68 nir_builder_init(&builder, function->impl);
69 nir_foreach_block(block, function->impl) {
70 nir_foreach_instr_safe(instr, block) {
71 if (instr->type == nir_instr_type_intrinsic)
72 progress |= lower_discard_if_instr(
73 nir_instr_as_intrinsic(instr),
74 &builder);
75 }
76 }
77
78 nir_metadata_preserve(function->impl, nir_metadata_dominance);
79 }
80 }
81
82 return progress;
83 }
84
85 static const struct nir_shader_compiler_options nir_options = {
86 .lower_all_io_to_temps = true,
87 .lower_ffma = true,
88 .lower_fdph = true,
89 .lower_flrp32 = true,
90 .lower_fpow = true,
91 .lower_fsat = true,
92 };
93
94 const void *
95 zink_get_compiler_options(struct pipe_screen *screen,
96 enum pipe_shader_ir ir,
97 enum pipe_shader_type shader)
98 {
99 assert(ir == PIPE_SHADER_IR_NIR);
100 return &nir_options;
101 }
102
103 struct nir_shader *
104 zink_tgsi_to_nir(struct pipe_screen *screen, const struct tgsi_token *tokens)
105 {
106 if (zink_debug & ZINK_DEBUG_TGSI) {
107 fprintf(stderr, "TGSI shader:\n---8<---\n");
108 tgsi_dump_to_file(tokens, 0, stderr);
109 fprintf(stderr, "---8<---\n\n");
110 }
111
112 return tgsi_to_nir(tokens, screen, false);
113 }
114
115 static void
116 optimize_nir(struct nir_shader *s)
117 {
118 bool progress;
119 do {
120 progress = false;
121 NIR_PASS_V(s, nir_lower_vars_to_ssa);
122 NIR_PASS(progress, s, nir_copy_prop);
123 NIR_PASS(progress, s, nir_opt_remove_phis);
124 NIR_PASS(progress, s, nir_opt_dce);
125 NIR_PASS(progress, s, nir_opt_dead_cf);
126 NIR_PASS(progress, s, nir_opt_cse);
127 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
128 NIR_PASS(progress, s, nir_opt_algebraic);
129 NIR_PASS(progress, s, nir_opt_constant_folding);
130 NIR_PASS(progress, s, nir_opt_undef);
131 NIR_PASS(progress, s, zink_nir_lower_b2b);
132 } while (progress);
133 }
134
135 /* check for a genuine gl_PointSize output vs one from nir_lower_point_size_mov */
136 static bool
137 check_psiz(struct nir_shader *s)
138 {
139 nir_foreach_variable(var, &s->outputs) {
140 if (var->data.location == VARYING_SLOT_PSIZ) {
141 /* genuine PSIZ outputs will have this set */
142 return !!var->data.explicit_location;
143 }
144 }
145 return false;
146 }
147
148 /* semi-copied from iris */
149 static void
150 update_so_info(struct pipe_stream_output_info *so_info,
151 uint64_t outputs_written, bool have_psiz)
152 {
153 uint8_t reverse_map[64] = {};
154 unsigned slot = 0;
155 while (outputs_written) {
156 int bit = u_bit_scan64(&outputs_written);
157 /* PSIZ from nir_lower_point_size_mov breaks stream output, so always skip it */
158 if (bit == VARYING_SLOT_PSIZ && !have_psiz)
159 continue;
160 reverse_map[slot++] = bit;
161 }
162
163 for (unsigned i = 0; i < so_info->num_outputs; i++) {
164 struct pipe_stream_output *output = &so_info->output[i];
165
166 /* Map Gallium's condensed "slots" back to real VARYING_SLOT_* enums */
167 output->register_index = reverse_map[output->register_index];
168 }
169 }
170
171 struct zink_shader *
172 zink_compile_nir(struct zink_screen *screen, struct nir_shader *nir,
173 const struct pipe_stream_output_info *so_info)
174 {
175 struct zink_shader *ret = CALLOC_STRUCT(zink_shader);
176 bool have_psiz = false;
177
178 ret->programs = _mesa_pointer_set_create(NULL);
179
180 NIR_PASS_V(nir, nir_lower_uniforms_to_ubo, 1);
181 NIR_PASS_V(nir, nir_lower_clip_halfz);
182 if (nir->info.stage == MESA_SHADER_VERTEX)
183 have_psiz = check_psiz(nir);
184 NIR_PASS_V(nir, nir_lower_regs_to_ssa);
185 optimize_nir(nir);
186 NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp, NULL);
187 NIR_PASS_V(nir, lower_discard_if);
188 NIR_PASS_V(nir, nir_convert_from_ssa, true);
189
190 if (zink_debug & ZINK_DEBUG_NIR) {
191 fprintf(stderr, "NIR shader:\n---8<---\n");
192 nir_print_shader(nir, stderr);
193 fprintf(stderr, "---8<---\n");
194 }
195
196 ret->num_bindings = 0;
197 nir_foreach_variable(var, &nir->uniforms) {
198 if (var->data.mode == nir_var_mem_ubo) {
199 int binding = zink_binding(nir->info.stage,
200 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
201 var->data.binding);
202 ret->bindings[ret->num_bindings].index = var->data.binding;
203 ret->bindings[ret->num_bindings].binding = binding;
204 ret->bindings[ret->num_bindings].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
205 ret->num_bindings++;
206 } else {
207 assert(var->data.mode == nir_var_uniform);
208 if (glsl_type_is_array(var->type) &&
209 glsl_type_is_sampler(glsl_get_array_element(var->type))) {
210 for (int i = 0; i < glsl_get_length(var->type); ++i) {
211 int binding = zink_binding(nir->info.stage,
212 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
213 var->data.binding + i);
214 ret->bindings[ret->num_bindings].index = var->data.binding + i;
215 ret->bindings[ret->num_bindings].binding = binding;
216 ret->bindings[ret->num_bindings].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
217 ret->num_bindings++;
218 }
219 } else if (glsl_type_is_sampler(var->type)) {
220 int binding = zink_binding(nir->info.stage,
221 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
222 var->data.binding);
223 ret->bindings[ret->num_bindings].index = var->data.binding;
224 ret->bindings[ret->num_bindings].binding = binding;
225 ret->bindings[ret->num_bindings].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
226 ret->num_bindings++;
227 }
228 }
229 }
230
231 ret->info = nir->info;
232 if (so_info) {
233 memcpy(&ret->stream_output, so_info, sizeof(ret->stream_output));
234 update_so_info(&ret->stream_output, nir->info.outputs_written, have_psiz);
235 }
236
237 struct spirv_shader *spirv = nir_to_spirv(nir, so_info, so_info ? &ret->stream_output : NULL);
238 assert(spirv);
239
240 if (zink_debug & ZINK_DEBUG_SPIRV) {
241 char buf[256];
242 static int i;
243 snprintf(buf, sizeof(buf), "dump%02d.spv", i++);
244 FILE *fp = fopen(buf, "wb");
245 if (fp) {
246 fwrite(spirv->words, sizeof(uint32_t), spirv->num_words, fp);
247 fclose(fp);
248 fprintf(stderr, "wrote '%s'...\n", buf);
249 }
250 }
251
252 VkShaderModuleCreateInfo smci = {};
253 smci.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
254 smci.codeSize = spirv->num_words * sizeof(uint32_t);
255 smci.pCode = spirv->words;
256
257 if (vkCreateShaderModule(screen->dev, &smci, NULL, &ret->shader_module) != VK_SUCCESS)
258 return NULL;
259
260 return ret;
261 }
262
263 void
264 zink_shader_free(struct zink_screen *screen, struct zink_shader *shader)
265 {
266 vkDestroyShaderModule(screen->dev, shader->shader_module, NULL);
267 set_foreach(shader->programs, entry) {
268 zink_gfx_program_remove_shader((void*)entry->key, shader);
269 }
270 _mesa_set_destroy(shader->programs, NULL);
271 FREE(shader);
272 }