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 "nir/tgsi_to_nir.h"
38 #include "util/u_atomic.h"
39 #include "util/u_cpu_detect.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 void
etna_dump_shader(const struct etna_shader_variant * shader)61 etna_dump_shader(const struct etna_shader_variant *shader)
62 {
63 if (shader->stage == MESA_SHADER_VERTEX)
64 printf("VERT\n");
65 else
66 printf("FRAG\n");
67
68 etna_disasm(shader->code, shader->code_size, PRINT_RAW);
69
70 printf("num loops: %i\n", shader->num_loops);
71 printf("num temps: %i\n", shader->num_temps);
72 printf("immediates:\n");
73 for (int idx = 0; idx < shader->uniforms.count; ++idx) {
74 printf(" [%i].%c = %f (0x%08x) (%d)\n",
75 idx / 4,
76 "xyzw"[idx % 4],
77 *((float *)&shader->uniforms.data[idx]),
78 shader->uniforms.data[idx],
79 shader->uniforms.contents[idx]);
80 }
81 printf("inputs:\n");
82 for (int idx = 0; idx < shader->infile.num_reg; ++idx) {
83 printf(" [%i] name=%s comps=%i\n", shader->infile.reg[idx].reg,
84 (shader->stage == MESA_SHADER_VERTEX) ?
85 gl_vert_attrib_name(shader->infile.reg[idx].slot) :
86 gl_varying_slot_name_for_stage(shader->infile.reg[idx].slot, shader->stage),
87 shader->infile.reg[idx].num_components);
88 }
89 printf("outputs:\n");
90 for (int idx = 0; idx < shader->outfile.num_reg; ++idx) {
91 printf(" [%i] name=%s comps=%i\n", shader->outfile.reg[idx].reg,
92 (shader->stage == MESA_SHADER_VERTEX) ?
93 gl_varying_slot_name_for_stage(shader->outfile.reg[idx].slot, shader->stage) :
94 gl_frag_result_name(shader->outfile.reg[idx].slot),
95 shader->outfile.reg[idx].num_components);
96 }
97 printf("special:\n");
98 if (shader->stage == MESA_SHADER_VERTEX) {
99 printf(" vs_pos_out_reg=%i\n", shader->vs_pos_out_reg);
100 printf(" vs_pointsize_out_reg=%i\n", shader->vs_pointsize_out_reg);
101 printf(" vs_load_balancing=0x%08x\n", shader->vs_load_balancing);
102 } else {
103 for (int idx = 0; idx < ARRAY_SIZE(shader->ps_color_out_reg); idx++)
104 printf(" ps_color_out_reg[%u]=%i\n", idx, shader->ps_color_out_reg[idx]);
105
106 printf(" ps_depth_out_reg=%i\n", shader->ps_depth_out_reg);
107 }
108 printf(" input_count_unk8=0x%08x\n", shader->input_count_unk8);
109 }
110
111 /* Link vs and fs together: fill in shader_state from vs and fs
112 * as this function is called every time a new fs or vs is bound, the goal is to
113 * do little processing as possible here, and to precompute as much as possible in
114 * the vs/fs shader_object.
115 *
116 * XXX we could cache the link result for a certain set of VS/PS; usually a pair
117 * of VS and PS will be used together anyway.
118 */
119 static bool
etna_link_shaders(struct etna_context * ctx,struct compiled_shader_state * cs,struct etna_shader_variant * vs,struct etna_shader_variant * fs)120 etna_link_shaders(struct etna_context *ctx, struct compiled_shader_state *cs,
121 struct etna_shader_variant *vs, struct etna_shader_variant *fs)
122 {
123 struct etna_shader_link_info link = { };
124
125 assert(vs->stage == MESA_SHADER_VERTEX);
126 assert(fs->stage == MESA_SHADER_FRAGMENT);
127
128 etna_link_shader(&link, vs, fs);
129
130 if (DBG_ENABLED(ETNA_DBG_LINKER_MSGS)) {
131 debug_printf("link result:\n");
132 debug_printf(" vs -> fs comps use pa_attr\n");
133
134 for (int idx = 0; idx < link.num_varyings; ++idx)
135 debug_printf(" t%-2u -> t%-2u %-5.*s %u,%u,%u,%u 0x%08x\n",
136 link.varyings[idx].reg, idx + 1,
137 link.varyings[idx].num_components, "xyzw",
138 link.varyings[idx].use[0], link.varyings[idx].use[1],
139 link.varyings[idx].use[2], link.varyings[idx].use[3],
140 link.varyings[idx].pa_attributes);
141 }
142
143 if (ctx->screen->specs.has_unified_uniforms) {
144 /* check if combined shader constants fit into unified const memory */
145 if ((vs->uniforms.count + fs->uniforms.count) / 4 >
146 ctx->screen->info->gpu.num_constants) {
147 DBG("Number of combined uniforms (%d) exceeds maximum %d",
148 (vs->uniforms.count + fs->uniforms.count) / 4,
149 ctx->screen->info->gpu.num_constants);
150 return false;
151 }
152 }
153
154 /* set last_varying_2x flag if the last varying has 1 or 2 components */
155 bool last_varying_2x = false;
156 if (link.num_varyings > 0 && link.varyings[link.num_varyings - 1].num_components <= 2)
157 last_varying_2x = true;
158
159 cs->RA_CONTROL = VIVS_RA_CONTROL_UNK0 |
160 COND(last_varying_2x, VIVS_RA_CONTROL_LAST_VARYING_2X);
161
162 cs->PA_ATTRIBUTE_ELEMENT_COUNT = VIVS_PA_ATTRIBUTE_ELEMENT_COUNT_COUNT(link.num_varyings);
163 STATIC_ASSERT(VIVS_PA_SHADER_ATTRIBUTES__LEN >= ETNA_NUM_VARYINGS);
164 for (int idx = 0; idx < link.num_varyings; ++idx)
165 cs->PA_SHADER_ATTRIBUTES[idx] = link.varyings[idx].pa_attributes;
166 cs->pa_shader_attributes_states = link.num_varyings;
167
168 cs->VS_END_PC = vs->code_size / 4;
169 cs->VS_OUTPUT_COUNT = 1 + link.num_varyings; /* position + varyings */
170
171 /* vs outputs (varyings) */
172 DEFINE_ETNA_BITARRAY(vs_output, ARRAY_SIZE(cs->VS_OUTPUT) * 4, 8) = {0};
173 int varid = 0;
174 etna_bitarray_set(vs_output, 8, varid++, vs->vs_pos_out_reg);
175 for (int idx = 0; idx < link.num_varyings; ++idx)
176 etna_bitarray_set(vs_output, 8, varid++, link.varyings[idx].reg);
177 if (vs->vs_pointsize_out_reg >= 0)
178 etna_bitarray_set(vs_output, 8, varid++, vs->vs_pointsize_out_reg); /* pointsize is last */
179
180 for (int idx = 0; idx < ARRAY_SIZE(cs->VS_OUTPUT); ++idx)
181 cs->VS_OUTPUT[idx] = vs_output[idx];
182
183 if (vs->vs_pointsize_out_reg != -1) {
184 /* vertex shader outputs point coordinate, provide extra output and make
185 * sure PA config is
186 * not masked */
187 cs->PA_CONFIG = ~0;
188 cs->VS_OUTPUT_COUNT_PSIZE = cs->VS_OUTPUT_COUNT + 1;
189 } else {
190 /* vertex shader does not output point coordinate, make sure thate
191 * POINT_SIZE_ENABLE is masked
192 * and no extra output is given */
193 cs->PA_CONFIG = ~VIVS_PA_CONFIG_POINT_SIZE_ENABLE;
194 cs->VS_OUTPUT_COUNT_PSIZE = cs->VS_OUTPUT_COUNT;
195 }
196
197 /* if fragment shader doesn't read pointcoord, disable it */
198 if (link.pcoord_varying_comp_ofs == -1)
199 cs->PA_CONFIG &= ~VIVS_PA_CONFIG_POINT_SPRITE_ENABLE;
200
201 cs->VS_LOAD_BALANCING = vs->vs_load_balancing;
202 cs->VS_START_PC = 0;
203
204 cs->PS_END_PC = fs->code_size / 4;
205
206 /* apply output remapping based on current framebuffer state */
207 int ps_color_out_reg[PIPE_MAX_COLOR_BUFS];
208
209 for (unsigned i = 0; i < ARRAY_SIZE(ctx->framebuffer.ps_output_remap); i++)
210 ps_color_out_reg[i] = fs->ps_color_out_reg[ctx->framebuffer.ps_output_remap[i]];
211
212 cs->PS_OUTPUT_REG[0] =
213 VIVS_PS_OUTPUT_REG_0(ps_color_out_reg[0]) |
214 VIVS_PS_OUTPUT_REG_1(ps_color_out_reg[1]) |
215 VIVS_PS_OUTPUT_REG_2(ps_color_out_reg[2]) |
216 VIVS_PS_OUTPUT_REG_3(ps_color_out_reg[3]);
217
218 cs->PS_OUTPUT_REG[1] =
219 VIVS_PS_OUTPUT_REG2_4(ps_color_out_reg[4]) |
220 VIVS_PS_OUTPUT_REG2_5(ps_color_out_reg[5]) |
221 VIVS_PS_OUTPUT_REG2_6(ps_color_out_reg[6]) |
222 VIVS_PS_OUTPUT_REG2_7(ps_color_out_reg[7]);
223
224 /* apply saturation information from current framebuffer state */
225 cs->PS_OUTPUT_REG[1] |= ctx->framebuffer.PS_OUTPUT_REG2;
226
227 cs->PS_INPUT_COUNT =
228 VIVS_PS_INPUT_COUNT_COUNT(link.num_varyings + 1) | /* Number of inputs plus position */
229 VIVS_PS_INPUT_COUNT_UNK8(fs->input_count_unk8);
230 cs->PS_TEMP_REGISTER_CONTROL =
231 VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs->num_temps, link.num_varyings + 1));
232 cs->PS_START_PC = 0;
233
234 /* Precompute PS_INPUT_COUNT and TEMP_REGISTER_CONTROL in the case of MSAA
235 * mode, avoids some fumbling in sync_context. */
236 /* MSAA adds another input */
237 cs->PS_INPUT_COUNT_MSAA =
238 VIVS_PS_INPUT_COUNT_COUNT(link.num_varyings + 2) |
239 VIVS_PS_INPUT_COUNT_UNK8(fs->input_count_unk8);
240 /* MSAA adds another temp */
241 cs->PS_TEMP_REGISTER_CONTROL_MSAA =
242 VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs->num_temps + 1, link.num_varyings + 2));
243
244 uint32_t total_components = 0;
245 DEFINE_ETNA_BITARRAY(num_components, ETNA_NUM_VARYINGS, 4) = {0};
246 DEFINE_ETNA_BITARRAY(component_use, 4 * ETNA_NUM_VARYINGS, 2) = {0};
247 DEFINE_ETNA_BITARRAY(halti5_varying_semantic, 4 * 32, 4) = {0};
248 for (int idx = 0; idx < link.num_varyings; ++idx) {
249 const struct etna_varying *varying = &link.varyings[idx];
250
251 etna_bitarray_set(num_components, 4, idx, varying->num_components);
252 for (int comp = 0; comp < varying->num_components; ++comp) {
253 if (ctx->screen->info->halti >= 5)
254 etna_bitarray_set(halti5_varying_semantic, 4, total_components, varying->semantic);
255 else
256 etna_bitarray_set(component_use, 2, total_components, varying->use[comp]);
257 total_components += 1;
258 }
259 }
260
261 cs->GL_VARYING_TOTAL_COMPONENTS =
262 VIVS_GL_VARYING_TOTAL_COMPONENTS_NUM(align(total_components, 2));
263 memcpy(cs->GL_VARYING_NUM_COMPONENTS, num_components, sizeof(uint32_t) * 2);
264 memcpy(cs->GL_VARYING_COMPONENT_USE, component_use, sizeof(uint32_t) * 4);
265 memcpy(cs->GL_HALTI5_SHADER_ATTRIBUTES, halti5_varying_semantic,
266 sizeof(uint32_t) * VIVS_GL_HALTI5_SHADER_ATTRIBUTES__LEN);
267 cs->halti5_shader_attributes_states = DIV_ROUND_UP(total_components, 8);
268
269 cs->GL_HALTI5_SH_SPECIALS =
270 0x7f7f0000 | /* unknown bits, probably other PS inputs */
271 /* pointsize is last (see above) */
272 VIVS_GL_HALTI5_SH_SPECIALS_VS_PSIZE_OUT((vs->vs_pointsize_out_reg != -1) ?
273 cs->VS_OUTPUT_COUNT * 4 : 0x00) |
274 VIVS_GL_HALTI5_SH_SPECIALS_PS_PCOORD_IN((link.pcoord_varying_comp_ofs != -1) ?
275 link.pcoord_varying_comp_ofs : 0x7f);
276
277 cs->writes_z = fs->ps_depth_out_reg >= 0;
278 cs->uses_discard = fs->uses_discard;
279
280 /* reference instruction memory */
281 cs->vs_inst_mem_size = vs->code_size;
282 cs->VS_INST_MEM = vs->code;
283
284 cs->ps_inst_mem_size = fs->code_size;
285 cs->PS_INST_MEM = fs->code;
286
287 if (vs->needs_icache || fs->needs_icache) {
288 /* If either of the shaders needs ICACHE, we use it for both. It is
289 * either switched on or off for the entire shader processor.
290 */
291 if (!etna_icache_upload_shader(ctx, vs) ||
292 !etna_icache_upload_shader(ctx, fs)) {
293 assert(0);
294 return false;
295 }
296
297 cs->VS_INST_ADDR.bo = vs->bo;
298 cs->VS_INST_ADDR.offset = 0;
299 cs->VS_INST_ADDR.flags = ETNA_RELOC_READ;
300 cs->PS_INST_ADDR.bo = fs->bo;
301 cs->PS_INST_ADDR.offset = 0;
302 cs->PS_INST_ADDR.flags = ETNA_RELOC_READ;
303 } else {
304 /* clear relocs */
305 memset(&cs->VS_INST_ADDR, 0, sizeof(cs->VS_INST_ADDR));
306 memset(&cs->PS_INST_ADDR, 0, sizeof(cs->PS_INST_ADDR));
307 }
308
309 return true;
310 }
311
312 bool
etna_shader_link(struct etna_context * ctx)313 etna_shader_link(struct etna_context *ctx)
314 {
315 if (!ctx->shader.vs || !ctx->shader.fs)
316 return false;
317
318 /* re-link vs and fs if needed */
319 return etna_link_shaders(ctx, &ctx->shader_state, ctx->shader.vs, ctx->shader.fs);
320 }
321
322 void
etna_destroy_shader(struct etna_shader_variant * shader)323 etna_destroy_shader(struct etna_shader_variant *shader)
324 {
325 assert(shader);
326
327 FREE(shader->code);
328 FREE(shader->uniforms.data);
329 FREE(shader->uniforms.contents);
330 FREE(shader);
331 }
332
333 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)334 etna_shader_update_vs_inputs(struct compiled_shader_state *cs,
335 const struct etna_shader_variant *vs,
336 const struct compiled_vertex_elements_state *ves)
337 {
338 unsigned num_temps, cur_temp, num_vs_inputs;
339
340 if (!vs)
341 return false;
342
343 /* Number of vertex elements determines number of VS inputs. Otherwise,
344 * the GPU crashes. Allocate any unused vertex elements to VS temporary
345 * registers. */
346 num_vs_inputs = MAX2(ves->num_elements, vs->infile.num_reg);
347 if (num_vs_inputs != ves->num_elements) {
348 BUG("Number of elements %u does not match the number of VS inputs %zu",
349 ves->num_elements, vs->infile.num_reg);
350 return false;
351 }
352
353 cur_temp = vs->num_temps;
354 num_temps = num_vs_inputs - vs->infile.num_reg + cur_temp;
355
356 cs->VS_INPUT_COUNT = VIVS_VS_INPUT_COUNT_COUNT(num_vs_inputs) |
357 VIVS_VS_INPUT_COUNT_UNK8(vs->input_count_unk8);
358 cs->VS_TEMP_REGISTER_CONTROL =
359 VIVS_VS_TEMP_REGISTER_CONTROL_NUM_TEMPS(num_temps);
360
361 /* vs inputs (attributes) */
362 DEFINE_ETNA_BITARRAY(vs_input, 16, 8) = {0};
363 for (int idx = 0; idx < num_vs_inputs; ++idx) {
364 if (idx < vs->infile.num_reg)
365 etna_bitarray_set(vs_input, 8, idx, vs->infile.reg[idx].reg);
366 else
367 etna_bitarray_set(vs_input, 8, idx, cur_temp++);
368 }
369
370 if (vs->vs_id_in_reg >= 0) {
371 cs->VS_INPUT_COUNT = VIVS_VS_INPUT_COUNT_COUNT(num_vs_inputs + 1) |
372 VIVS_VS_INPUT_COUNT_UNK8(vs->input_count_unk8) |
373 VIVS_VS_INPUT_COUNT_ID_ENABLE;
374
375 etna_bitarray_set(vs_input, 8, num_vs_inputs, vs->vs_id_in_reg);
376
377 cs->FE_HALTI5_ID_CONFIG =
378 VIVS_FE_HALTI5_ID_CONFIG_VERTEX_ID_ENABLE |
379 VIVS_FE_HALTI5_ID_CONFIG_INSTANCE_ID_ENABLE |
380 VIVS_FE_HALTI5_ID_CONFIG_VERTEX_ID_REG(vs->vs_id_in_reg * 4) |
381 VIVS_FE_HALTI5_ID_CONFIG_INSTANCE_ID_REG(vs->vs_id_in_reg * 4 + 1);
382 }
383
384 for (int idx = 0; idx < ARRAY_SIZE(cs->VS_INPUT); ++idx)
385 cs->VS_INPUT[idx] = vs_input[idx];
386
387 return true;
388 }
389
390 static inline const char *
etna_shader_stage(struct etna_shader * shader)391 etna_shader_stage(struct etna_shader *shader)
392 {
393 switch (shader->nir->info.stage) {
394 case MESA_SHADER_VERTEX: return "VERT";
395 case MESA_SHADER_FRAGMENT: return "FRAG";
396 case MESA_SHADER_COMPUTE: return "CL";
397 default:
398 unreachable("invalid type");
399 return NULL;
400 }
401 }
402
403 static void
dump_shader_info(struct etna_shader_variant * v,struct util_debug_callback * debug)404 dump_shader_info(struct etna_shader_variant *v, struct util_debug_callback *debug)
405 {
406 if (!DBG_ENABLED(ETNA_DBG_SHADERDB))
407 return;
408
409 util_debug_message(debug, SHADER_INFO,
410 "%s shader: %u instructions, %u temps, "
411 "%u immediates, %u loops",
412 etna_shader_stage(v->shader),
413 v->code_size / 4,
414 v->num_temps,
415 v->uniforms.count,
416 v->num_loops);
417 }
418
419 bool
etna_shader_update_vertex(struct etna_context * ctx)420 etna_shader_update_vertex(struct etna_context *ctx)
421 {
422 return etna_shader_update_vs_inputs(&ctx->shader_state, ctx->shader.vs,
423 ctx->vertex_elements);
424 }
425
426 static struct etna_shader_variant *
create_variant(struct etna_shader * shader,const struct etna_shader_key * const key)427 create_variant(struct etna_shader *shader,
428 const struct etna_shader_key* const key)
429 {
430 struct etna_shader_variant *v = CALLOC_STRUCT(etna_shader_variant);
431 int ret;
432
433 if (!v)
434 return NULL;
435
436 v->shader = shader;
437 v->key = *key;
438 v->id = ++shader->variant_count;
439
440 if (etna_disk_cache_retrieve(shader->compiler, v))
441 return v;
442
443 ret = etna_compile_shader(v);
444 if (!ret) {
445 debug_error("compile failed!");
446 goto fail;
447 }
448
449 etna_disk_cache_store(shader->compiler, v);
450
451 if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS))
452 etna_dump_shader(v);
453
454 return v;
455
456 fail:
457 FREE(v);
458 return NULL;
459 }
460
461 struct etna_shader_variant *
etna_shader_variant(struct etna_shader * shader,const struct etna_shader_key * const key,struct util_debug_callback * debug,bool called_from_draw)462 etna_shader_variant(struct etna_shader *shader,
463 const struct etna_shader_key* const key,
464 struct util_debug_callback *debug,
465 bool called_from_draw)
466 {
467 struct etna_shader_variant *v;
468
469 assert(shader->specs->fragment_sampler_count <= ARRAY_SIZE(key->tex_swizzle));
470
471 for (v = shader->variants; v; v = v->next)
472 if (etna_shader_key_equal(key, &v->key))
473 return v;
474
475 /* compile new variant if it doesn't exist already */
476 v = create_variant(shader, key);
477 if (v) {
478 v->next = shader->variants;
479 shader->variants = v;
480 dump_shader_info(v, debug);
481 }
482
483 if (called_from_draw) {
484 perf_debug_message(debug, SHADER_INFO,
485 "%s shader: recompiling at draw time: global "
486 "0x%08x\n",
487 etna_shader_stage(shader), key->global);
488 }
489
490 return v;
491 }
492
493 /**
494 * Should initial variants be compiled synchronously?
495 *
496 * The only case where pipe_debug_message() is used in the initial-variants
497 * path is with ETNA_MESA_DEBUG=shaderdb. So if either debug is disabled (ie.
498 * debug.debug_message==NULL), or shaderdb stats are not enabled, we can
499 * compile the initial shader variant asynchronously.
500 */
501 static inline bool
initial_variants_synchronous(struct etna_context * ctx)502 initial_variants_synchronous(struct etna_context *ctx)
503 {
504 return unlikely(ctx->base.debug.debug_message) ||
505 DBG_ENABLED(ETNA_DBG_SHADERDB) ||
506 DBG_ENABLED(ETNA_DBG_DUMP_SHADERS);
507 }
508
509 static void
create_initial_variants_async(void * job,void * gdata,int thread_index)510 create_initial_variants_async(void *job, void *gdata, int thread_index)
511 {
512 struct etna_shader *shader = job;
513 struct util_debug_callback debug = {};
514 static struct etna_shader_key key;
515
516 etna_shader_variant(shader, &key, &debug, false);
517 }
518
519 static void *
etna_create_shader_state(struct pipe_context * pctx,const struct pipe_shader_state * pss)520 etna_create_shader_state(struct pipe_context *pctx,
521 const struct pipe_shader_state *pss)
522 {
523 struct etna_context *ctx = etna_context(pctx);
524 struct etna_screen *screen = ctx->screen;
525 struct etna_compiler *compiler = screen->compiler;
526 struct etna_shader *shader = CALLOC_STRUCT(etna_shader);
527
528 if (!shader)
529 return NULL;
530
531 shader->id = p_atomic_inc_return(&compiler->shader_count);
532 shader->info = screen->info;
533 shader->specs = &screen->specs;
534 shader->compiler = screen->compiler;
535 util_queue_fence_init(&shader->ready);
536
537 shader->nir = (pss->type == PIPE_SHADER_IR_NIR) ? pss->ir.nir :
538 tgsi_to_nir(pss->tokens, pctx->screen, false);
539
540 etna_disk_cache_init_shader_key(compiler, shader);
541
542 if (initial_variants_synchronous(ctx)) {
543 struct etna_shader_key key = {};
544 etna_shader_variant(shader, &key, &ctx->base.debug, false);
545 } else {
546 struct etna_screen *screen = ctx->screen;
547 util_queue_add_job(&screen->shader_compiler_queue, shader, &shader->ready,
548 create_initial_variants_async, NULL, 0);
549 }
550
551 return shader;
552 }
553
554 static void
etna_delete_shader_state(struct pipe_context * pctx,void * ss)555 etna_delete_shader_state(struct pipe_context *pctx, void *ss)
556 {
557 struct etna_context *ctx = etna_context(pctx);
558 struct etna_screen *screen = ctx->screen;
559 struct etna_shader *shader = ss;
560 struct etna_shader_variant *v, *t;
561
562 util_queue_drop_job(&screen->shader_compiler_queue, &shader->ready);
563
564 v = shader->variants;
565 while (v) {
566 t = v;
567 v = v->next;
568 if (t->bo)
569 etna_bo_del(t->bo);
570
571 etna_destroy_shader(t);
572 }
573
574 ralloc_free(shader->nir);
575 util_queue_fence_destroy(&shader->ready);
576 FREE(shader);
577 }
578
579 static void
etna_bind_fs_state(struct pipe_context * pctx,void * hwcso)580 etna_bind_fs_state(struct pipe_context *pctx, void *hwcso)
581 {
582 struct etna_context *ctx = etna_context(pctx);
583
584 ctx->shader.bind_fs = hwcso;
585 ctx->dirty |= ETNA_DIRTY_SHADER;
586 }
587
588 static void
etna_bind_vs_state(struct pipe_context * pctx,void * hwcso)589 etna_bind_vs_state(struct pipe_context *pctx, void *hwcso)
590 {
591 struct etna_context *ctx = etna_context(pctx);
592
593 ctx->shader.bind_vs = hwcso;
594 ctx->dirty |= ETNA_DIRTY_SHADER;
595 }
596
597 static void
etna_set_max_shader_compiler_threads(struct pipe_screen * pscreen,unsigned max_threads)598 etna_set_max_shader_compiler_threads(struct pipe_screen *pscreen,
599 unsigned max_threads)
600 {
601 struct etna_screen *screen = etna_screen(pscreen);
602
603 util_queue_adjust_num_threads(&screen->shader_compiler_queue, max_threads,
604 false);
605 }
606
607 static bool
etna_is_parallel_shader_compilation_finished(struct pipe_screen * pscreen,void * hwcso,enum pipe_shader_type shader_type)608 etna_is_parallel_shader_compilation_finished(struct pipe_screen *pscreen,
609 void *hwcso,
610 enum pipe_shader_type shader_type)
611 {
612 struct etna_shader *shader = (struct etna_shader *)hwcso;
613
614 return util_queue_fence_is_signalled(&shader->ready);
615 }
616
617 void
etna_shader_init(struct pipe_context * pctx)618 etna_shader_init(struct pipe_context *pctx)
619 {
620 pctx->create_fs_state = etna_create_shader_state;
621 pctx->bind_fs_state = etna_bind_fs_state;
622 pctx->delete_fs_state = etna_delete_shader_state;
623 pctx->create_vs_state = etna_create_shader_state;
624 pctx->bind_vs_state = etna_bind_vs_state;
625 pctx->delete_vs_state = etna_delete_shader_state;
626 }
627
628 bool
etna_shader_screen_init(struct pipe_screen * pscreen)629 etna_shader_screen_init(struct pipe_screen *pscreen)
630 {
631 struct etna_screen *screen = etna_screen(pscreen);
632 unsigned num_threads = util_get_cpu_caps()->nr_cpus - 1;
633
634 /* Create at least one thread - even on single core CPU systems. */
635 num_threads = MAX2(1, num_threads);
636
637 screen->compiler = etna_compiler_create(pscreen->get_name(pscreen), screen->info);
638 if (!screen->compiler)
639 return false;
640
641 pscreen->set_max_shader_compiler_threads = etna_set_max_shader_compiler_threads;
642 pscreen->is_parallel_shader_compilation_finished = etna_is_parallel_shader_compilation_finished;
643
644 return util_queue_init(&screen->shader_compiler_queue, "sh", 64, num_threads,
645 UTIL_QUEUE_INIT_RESIZE_IF_FULL | UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY,
646 NULL);
647 }
648
649 void
etna_shader_screen_fini(struct pipe_screen * pscreen)650 etna_shader_screen_fini(struct pipe_screen *pscreen)
651 {
652 struct etna_screen *screen = etna_screen(pscreen);
653
654 util_queue_destroy(&screen->shader_compiler_queue);
655 etna_compiler_destroy(screen->compiler);
656 }
657