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