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