radeonsi: move nir_shader_compiler_options into si_screen
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_shader.c
1 /*
2 * Copyright (c) 2012-2015 Etnaviv Project
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 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 * Authors:
24 * Wladimir J. van der Laan <laanwj@gmail.com>
25 */
26
27 #include "etnaviv_shader.h"
28
29 #include "etnaviv_compiler.h"
30 #include "etnaviv_context.h"
31 #include "etnaviv_debug.h"
32 #include "etnaviv_screen.h"
33 #include "etnaviv_util.h"
34
35 #include "tgsi/tgsi_parse.h"
36 #include "nir/tgsi_to_nir.h"
37 #include "util/u_atomic.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40
41 /* Upload shader code to bo, if not already done */
42 static bool etna_icache_upload_shader(struct etna_context *ctx, struct etna_shader_variant *v)
43 {
44 if (v->bo)
45 return true;
46 v->bo = etna_bo_new(ctx->screen->dev, v->code_size*4, DRM_ETNA_GEM_CACHE_WC);
47 if (!v->bo)
48 return false;
49
50 void *buf = etna_bo_map(v->bo);
51 etna_bo_cpu_prep(v->bo, DRM_ETNA_PREP_WRITE);
52 memcpy(buf, v->code, v->code_size*4);
53 etna_bo_cpu_fini(v->bo);
54 DBG("Uploaded %s of %u words to bo %p", v->stage == MESA_SHADER_FRAGMENT ? "fs":"vs", v->code_size, v->bo);
55 return true;
56 }
57
58 /* Link vs and fs together: fill in shader_state from vs and fs
59 * as this function is called every time a new fs or vs is bound, the goal is to
60 * do little processing as possible here, and to precompute as much as possible in
61 * the vs/fs shader_object.
62 *
63 * XXX we could cache the link result for a certain set of VS/PS; usually a pair
64 * of VS and PS will be used together anyway.
65 */
66 static bool
67 etna_link_shaders(struct etna_context *ctx, struct compiled_shader_state *cs,
68 struct etna_shader_variant *vs, struct etna_shader_variant *fs)
69 {
70 struct etna_shader_link_info link = { };
71 bool failed;
72
73 assert(vs->stage == MESA_SHADER_VERTEX);
74 assert(fs->stage == MESA_SHADER_FRAGMENT);
75
76 #ifdef DEBUG
77 if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS)) {
78 if (DBG_ENABLED(ETNA_DBG_NIR)) {
79 etna_dump_shader_nir(vs);
80 etna_dump_shader_nir(fs);
81 } else {
82 etna_dump_shader(vs);
83 etna_dump_shader(fs);
84 }
85 }
86 #endif
87
88 if (DBG_ENABLED(ETNA_DBG_NIR))
89 failed = etna_link_shader_nir(&link, vs, fs);
90 else
91 failed = etna_link_shader(&link, vs, fs);
92
93 if (failed) {
94 /* linking failed: some fs inputs do not have corresponding
95 * vs outputs */
96 assert(0);
97
98 return false;
99 }
100
101 if (DBG_ENABLED(ETNA_DBG_LINKER_MSGS)) {
102 debug_printf("link result:\n");
103 debug_printf(" vs -> fs comps use pa_attr\n");
104
105 for (int idx = 0; idx < link.num_varyings; ++idx)
106 debug_printf(" t%-2u -> t%-2u %-5.*s %u,%u,%u,%u 0x%08x\n",
107 link.varyings[idx].reg, idx + 1,
108 link.varyings[idx].num_components, "xyzw",
109 link.varyings[idx].use[0], link.varyings[idx].use[1],
110 link.varyings[idx].use[2], link.varyings[idx].use[3],
111 link.varyings[idx].pa_attributes);
112 }
113
114 /* set last_varying_2x flag if the last varying has 1 or 2 components */
115 bool last_varying_2x = false;
116 if (link.num_varyings > 0 && link.varyings[link.num_varyings - 1].num_components <= 2)
117 last_varying_2x = true;
118
119 cs->RA_CONTROL = VIVS_RA_CONTROL_UNK0 |
120 COND(last_varying_2x, VIVS_RA_CONTROL_LAST_VARYING_2X);
121
122 cs->PA_ATTRIBUTE_ELEMENT_COUNT = VIVS_PA_ATTRIBUTE_ELEMENT_COUNT_COUNT(link.num_varyings);
123 for (int idx = 0; idx < link.num_varyings; ++idx)
124 cs->PA_SHADER_ATTRIBUTES[idx] = link.varyings[idx].pa_attributes;
125
126 cs->VS_END_PC = vs->code_size / 4;
127 cs->VS_OUTPUT_COUNT = 1 + link.num_varyings; /* position + varyings */
128
129 /* vs outputs (varyings) */
130 DEFINE_ETNA_BITARRAY(vs_output, 16, 8) = {0};
131 int varid = 0;
132 etna_bitarray_set(vs_output, 8, varid++, vs->vs_pos_out_reg);
133 for (int idx = 0; idx < link.num_varyings; ++idx)
134 etna_bitarray_set(vs_output, 8, varid++, link.varyings[idx].reg);
135 if (vs->vs_pointsize_out_reg >= 0)
136 etna_bitarray_set(vs_output, 8, varid++, vs->vs_pointsize_out_reg); /* pointsize is last */
137
138 for (int idx = 0; idx < ARRAY_SIZE(cs->VS_OUTPUT); ++idx)
139 cs->VS_OUTPUT[idx] = vs_output[idx];
140
141 if (vs->vs_pointsize_out_reg != -1) {
142 /* vertex shader outputs point coordinate, provide extra output and make
143 * sure PA config is
144 * not masked */
145 cs->PA_CONFIG = ~0;
146 cs->VS_OUTPUT_COUNT_PSIZE = cs->VS_OUTPUT_COUNT + 1;
147 } else {
148 /* vertex shader does not output point coordinate, make sure thate
149 * POINT_SIZE_ENABLE is masked
150 * and no extra output is given */
151 cs->PA_CONFIG = ~VIVS_PA_CONFIG_POINT_SIZE_ENABLE;
152 cs->VS_OUTPUT_COUNT_PSIZE = cs->VS_OUTPUT_COUNT;
153 }
154
155 /* if fragment shader doesn't read pointcoord, disable it */
156 if (link.pcoord_varying_comp_ofs == -1)
157 cs->PA_CONFIG &= ~VIVS_PA_CONFIG_POINT_SPRITE_ENABLE;
158
159 cs->VS_LOAD_BALANCING = vs->vs_load_balancing;
160 cs->VS_START_PC = 0;
161
162 cs->PS_END_PC = fs->code_size / 4;
163 cs->PS_OUTPUT_REG = fs->ps_color_out_reg;
164 cs->PS_INPUT_COUNT =
165 VIVS_PS_INPUT_COUNT_COUNT(link.num_varyings + 1) | /* Number of inputs plus position */
166 VIVS_PS_INPUT_COUNT_UNK8(fs->input_count_unk8);
167 cs->PS_TEMP_REGISTER_CONTROL =
168 VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs->num_temps, link.num_varyings + 1));
169 cs->PS_START_PC = 0;
170
171 /* Precompute PS_INPUT_COUNT and TEMP_REGISTER_CONTROL in the case of MSAA
172 * mode, avoids some fumbling in sync_context. */
173 cs->PS_INPUT_COUNT_MSAA =
174 VIVS_PS_INPUT_COUNT_COUNT(link.num_varyings + 2) | /* MSAA adds another input */
175 VIVS_PS_INPUT_COUNT_UNK8(fs->input_count_unk8);
176 cs->PS_TEMP_REGISTER_CONTROL_MSAA =
177 VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs->num_temps, link.num_varyings + 2));
178
179 uint32_t total_components = 0;
180 DEFINE_ETNA_BITARRAY(num_components, ETNA_NUM_VARYINGS, 4) = {0};
181 DEFINE_ETNA_BITARRAY(component_use, 4 * ETNA_NUM_VARYINGS, 2) = {0};
182 for (int idx = 0; idx < link.num_varyings; ++idx) {
183 const struct etna_varying *varying = &link.varyings[idx];
184
185 etna_bitarray_set(num_components, 4, idx, varying->num_components);
186 for (int comp = 0; comp < varying->num_components; ++comp) {
187 etna_bitarray_set(component_use, 2, total_components, varying->use[comp]);
188 total_components += 1;
189 }
190 }
191
192 cs->GL_VARYING_TOTAL_COMPONENTS =
193 VIVS_GL_VARYING_TOTAL_COMPONENTS_NUM(align(total_components, 2));
194 cs->GL_VARYING_NUM_COMPONENTS[0] = num_components[0];
195 cs->GL_VARYING_NUM_COMPONENTS[1] = num_components[1];
196 cs->GL_VARYING_COMPONENT_USE[0] = component_use[0];
197 cs->GL_VARYING_COMPONENT_USE[1] = component_use[1];
198
199 cs->GL_HALTI5_SH_SPECIALS =
200 0x7f7f0000 | /* unknown bits, probably other PS inputs */
201 /* pointsize is last (see above) */
202 VIVS_GL_HALTI5_SH_SPECIALS_VS_PSIZE_OUT((vs->vs_pointsize_out_reg != -1) ?
203 cs->VS_OUTPUT_COUNT * 4 : 0x00) |
204 VIVS_GL_HALTI5_SH_SPECIALS_PS_PCOORD_IN((link.pcoord_varying_comp_ofs != -1) ?
205 link.pcoord_varying_comp_ofs : 0x7f);
206
207 /* mask out early Z bit when frag depth is written */
208 cs->PE_DEPTH_CONFIG = ~COND(fs->ps_depth_out_reg >= 0, VIVS_PE_DEPTH_CONFIG_EARLY_Z);
209
210 /* reference instruction memory */
211 cs->vs_inst_mem_size = vs->code_size;
212 cs->VS_INST_MEM = vs->code;
213
214 cs->ps_inst_mem_size = fs->code_size;
215 cs->PS_INST_MEM = fs->code;
216
217 if (vs->needs_icache || fs->needs_icache) {
218 /* If either of the shaders needs ICACHE, we use it for both. It is
219 * either switched on or off for the entire shader processor.
220 */
221 if (!etna_icache_upload_shader(ctx, vs) ||
222 !etna_icache_upload_shader(ctx, fs)) {
223 assert(0);
224 return false;
225 }
226
227 cs->VS_INST_ADDR.bo = vs->bo;
228 cs->VS_INST_ADDR.offset = 0;
229 cs->VS_INST_ADDR.flags = ETNA_RELOC_READ;
230 cs->PS_INST_ADDR.bo = fs->bo;
231 cs->PS_INST_ADDR.offset = 0;
232 cs->PS_INST_ADDR.flags = ETNA_RELOC_READ;
233 } else {
234 /* clear relocs */
235 memset(&cs->VS_INST_ADDR, 0, sizeof(cs->VS_INST_ADDR));
236 memset(&cs->PS_INST_ADDR, 0, sizeof(cs->PS_INST_ADDR));
237 }
238
239 return true;
240 }
241
242 bool
243 etna_shader_link(struct etna_context *ctx)
244 {
245 if (!ctx->shader.vs || !ctx->shader.fs)
246 return false;
247
248 /* re-link vs and fs if needed */
249 return etna_link_shaders(ctx, &ctx->shader_state, ctx->shader.vs, ctx->shader.fs);
250 }
251
252 static bool
253 etna_shader_update_vs_inputs(struct compiled_shader_state *cs,
254 const struct etna_shader_variant *vs,
255 const struct compiled_vertex_elements_state *ves)
256 {
257 unsigned num_temps, cur_temp, num_vs_inputs;
258
259 if (!vs)
260 return false;
261
262 /* Number of vertex elements determines number of VS inputs. Otherwise,
263 * the GPU crashes. Allocate any unused vertex elements to VS temporary
264 * registers. */
265 num_vs_inputs = MAX2(ves->num_elements, vs->infile.num_reg);
266 if (num_vs_inputs != ves->num_elements) {
267 BUG("Number of elements %u does not match the number of VS inputs %zu",
268 ves->num_elements, vs->infile.num_reg);
269 return false;
270 }
271
272 cur_temp = vs->num_temps;
273 num_temps = num_vs_inputs - vs->infile.num_reg + cur_temp;
274
275 cs->VS_INPUT_COUNT = VIVS_VS_INPUT_COUNT_COUNT(num_vs_inputs) |
276 VIVS_VS_INPUT_COUNT_UNK8(vs->input_count_unk8);
277 cs->VS_TEMP_REGISTER_CONTROL =
278 VIVS_VS_TEMP_REGISTER_CONTROL_NUM_TEMPS(num_temps);
279
280 /* vs inputs (attributes) */
281 DEFINE_ETNA_BITARRAY(vs_input, 16, 8) = {0};
282 for (int idx = 0; idx < num_vs_inputs; ++idx) {
283 if (idx < vs->infile.num_reg)
284 etna_bitarray_set(vs_input, 8, idx, vs->infile.reg[idx].reg);
285 else
286 etna_bitarray_set(vs_input, 8, idx, cur_temp++);
287 }
288
289 if (vs->vs_id_in_reg >= 0) {
290 cs->VS_INPUT_COUNT = VIVS_VS_INPUT_COUNT_COUNT(num_vs_inputs + 1) |
291 VIVS_VS_INPUT_COUNT_UNK8(vs->input_count_unk8) |
292 VIVS_VS_INPUT_COUNT_ID_ENABLE;
293
294 etna_bitarray_set(vs_input, 8, num_vs_inputs, vs->vs_id_in_reg);
295
296 cs->FE_HALTI5_ID_CONFIG =
297 VIVS_FE_HALTI5_ID_CONFIG_VERTEX_ID_ENABLE |
298 VIVS_FE_HALTI5_ID_CONFIG_INSTANCE_ID_ENABLE |
299 VIVS_FE_HALTI5_ID_CONFIG_VERTEX_ID_REG(vs->vs_id_in_reg * 4) |
300 VIVS_FE_HALTI5_ID_CONFIG_INSTANCE_ID_REG(vs->vs_id_in_reg * 4 + 1);
301 }
302
303 for (int idx = 0; idx < ARRAY_SIZE(cs->VS_INPUT); ++idx)
304 cs->VS_INPUT[idx] = vs_input[idx];
305
306 return true;
307 }
308
309 static inline const char *
310 etna_shader_stage(struct etna_shader_variant *shader)
311 {
312 switch (shader->stage) {
313 case MESA_SHADER_VERTEX: return "VERT";
314 case MESA_SHADER_FRAGMENT: return "FRAG";
315 case MESA_SHADER_COMPUTE: return "CL";
316 default:
317 unreachable("invalid type");
318 return NULL;
319 }
320 }
321
322 static void
323 dump_shader_info(struct etna_shader_variant *v, struct pipe_debug_callback *debug)
324 {
325 if (!unlikely(etna_mesa_debug & ETNA_DBG_SHADERDB))
326 return;
327
328 pipe_debug_message(debug, SHADER_INFO,
329 "%s shader: %u instructions, %u temps, "
330 "%u immediates, %u loops",
331 etna_shader_stage(v),
332 v->code_size,
333 v->num_temps,
334 v->uniforms.imm_count,
335 v->num_loops);
336 }
337
338 bool
339 etna_shader_update_vertex(struct etna_context *ctx)
340 {
341 return etna_shader_update_vs_inputs(&ctx->shader_state, ctx->shader.vs,
342 ctx->vertex_elements);
343 }
344
345 static struct etna_shader_variant *
346 create_variant(struct etna_shader *shader, struct etna_shader_key key)
347 {
348 struct etna_shader_variant *v = CALLOC_STRUCT(etna_shader_variant);
349 int ret;
350
351 if (!v)
352 return NULL;
353
354 v->shader = shader;
355 v->key = key;
356
357 ret = etna_compile_shader(v);
358 if (!ret) {
359 debug_error("compile failed!");
360 goto fail;
361 }
362
363 v->id = ++shader->variant_count;
364
365 return v;
366
367 fail:
368 FREE(v);
369 return NULL;
370 }
371
372 struct etna_shader_variant *
373 etna_shader_variant(struct etna_shader *shader, struct etna_shader_key key,
374 struct pipe_debug_callback *debug)
375 {
376 struct etna_shader_variant *v;
377
378 for (v = shader->variants; v; v = v->next)
379 if (etna_shader_key_equal(&key, &v->key))
380 return v;
381
382 /* compile new variant if it doesn't exist already */
383 v = create_variant(shader, key);
384 if (v) {
385 v->next = shader->variants;
386 shader->variants = v;
387 dump_shader_info(v, debug);
388 }
389
390 return v;
391 }
392
393 static void *
394 etna_create_shader_state(struct pipe_context *pctx,
395 const struct pipe_shader_state *pss)
396 {
397 struct etna_context *ctx = etna_context(pctx);
398 struct etna_screen *screen = ctx->screen;
399 struct etna_compiler *compiler = screen->compiler;
400 struct etna_shader *shader = CALLOC_STRUCT(etna_shader);
401
402 if (!shader)
403 return NULL;
404
405 shader->id = p_atomic_inc_return(&compiler->shader_count);
406 shader->specs = &screen->specs;
407 shader->compiler = screen->compiler;
408
409 if (DBG_ENABLED(ETNA_DBG_NIR))
410 shader->nir = (pss->type == PIPE_SHADER_IR_NIR) ? pss->ir.nir :
411 tgsi_to_nir(pss->tokens, pctx->screen, false);
412 else
413 shader->tokens = tgsi_dup_tokens(pss->tokens);
414
415
416
417 if (etna_mesa_debug & ETNA_DBG_SHADERDB) {
418 /* if shader-db run, create a standard variant immediately
419 * (as otherwise nothing will trigger the shader to be
420 * actually compiled).
421 */
422 struct etna_shader_key key = {};
423 etna_shader_variant(shader, key, &ctx->debug);
424 }
425
426 return shader;
427 }
428
429 static void
430 etna_delete_shader_state(struct pipe_context *pctx, void *ss)
431 {
432 struct etna_shader *shader = ss;
433 struct etna_shader_variant *v, *t;
434
435 v = shader->variants;
436 while (v) {
437 t = v;
438 v = v->next;
439 if (t->bo)
440 etna_bo_del(t->bo);
441
442 if (DBG_ENABLED(ETNA_DBG_NIR))
443 etna_destroy_shader_nir(t);
444 else
445 etna_destroy_shader(t);
446 }
447
448 ralloc_free(shader->nir);
449 FREE(shader);
450 }
451
452 static void
453 etna_bind_fs_state(struct pipe_context *pctx, void *hwcso)
454 {
455 struct etna_context *ctx = etna_context(pctx);
456
457 ctx->shader.bind_fs = hwcso;
458 ctx->dirty |= ETNA_DIRTY_SHADER;
459 }
460
461 static void
462 etna_bind_vs_state(struct pipe_context *pctx, void *hwcso)
463 {
464 struct etna_context *ctx = etna_context(pctx);
465
466 ctx->shader.bind_vs = hwcso;
467 ctx->dirty |= ETNA_DIRTY_SHADER;
468 }
469
470 void
471 etna_shader_init(struct pipe_context *pctx)
472 {
473 pctx->create_fs_state = etna_create_shader_state;
474 pctx->bind_fs_state = etna_bind_fs_state;
475 pctx->delete_fs_state = etna_delete_shader_state;
476 pctx->create_vs_state = etna_create_shader_state;
477 pctx->bind_vs_state = etna_bind_vs_state;
478 pctx->delete_vs_state = etna_delete_shader_state;
479 }