1 /*
2 * Copyright (c) 2017-2019 Lima 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 */
24
25 #include "util/u_memory.h"
26 #include "util/ralloc.h"
27 #include "util/u_debug.h"
28
29 #include "tgsi/tgsi_dump.h"
30 #include "compiler/nir/nir.h"
31 #include "compiler/nir/nir_serialize.h"
32 #include "nir/tgsi_to_nir.h"
33
34 #include "pipe/p_state.h"
35
36 #include "lima_screen.h"
37 #include "lima_context.h"
38 #include "lima_job.h"
39 #include "lima_program.h"
40 #include "lima_bo.h"
41 #include "lima_disk_cache.h"
42
43 #include "ir/lima_ir.h"
44
45 static const nir_shader_compiler_options vs_nir_options = {
46 .lower_ffma16 = true,
47 .lower_ffma32 = true,
48 .lower_ffma64 = true,
49 .lower_fpow = true,
50 .lower_ffract = true,
51 .lower_fdiv = true,
52 .lower_fmod = true,
53 .lower_fsqrt = true,
54 .lower_flrp32 = true,
55 .lower_flrp64 = true,
56 /* could be implemented by clamp */
57 .lower_fsat = true,
58 .lower_bitops = true,
59 .lower_rotate = true,
60 .lower_sincos = true,
61 .lower_fceil = true,
62 .lower_insert_byte = true,
63 .lower_insert_word = true,
64 .force_indirect_unrolling = (nir_var_shader_in | nir_var_shader_out | nir_var_function_temp),
65 };
66
67 static const nir_shader_compiler_options fs_nir_options = {
68 .lower_ffma16 = true,
69 .lower_ffma32 = true,
70 .lower_ffma64 = true,
71 .lower_fpow = true,
72 .lower_fdiv = true,
73 .lower_fmod = true,
74 .lower_flrp32 = true,
75 .lower_flrp64 = true,
76 .lower_fsign = true,
77 .lower_rotate = true,
78 .lower_fdot = true,
79 .lower_fdph = true,
80 .lower_insert_byte = true,
81 .lower_insert_word = true,
82 .lower_bitops = true,
83 .lower_vector_cmp = true,
84 .force_indirect_unrolling = (nir_var_shader_in | nir_var_shader_out | nir_var_function_temp),
85 };
86
87 const void *
lima_program_get_compiler_options(enum pipe_shader_type shader)88 lima_program_get_compiler_options(enum pipe_shader_type shader)
89 {
90 switch (shader) {
91 case PIPE_SHADER_VERTEX:
92 return &vs_nir_options;
93 case PIPE_SHADER_FRAGMENT:
94 return &fs_nir_options;
95 default:
96 return NULL;
97 }
98 }
99
100 static int
type_size(const struct glsl_type * type,bool bindless)101 type_size(const struct glsl_type *type, bool bindless)
102 {
103 return glsl_count_attribute_slots(type, false);
104 }
105
106 void
lima_program_optimize_vs_nir(struct nir_shader * s)107 lima_program_optimize_vs_nir(struct nir_shader *s)
108 {
109 bool progress;
110
111 NIR_PASS_V(s, nir_lower_viewport_transform);
112 NIR_PASS_V(s, nir_lower_point_size, 1.0f, 100.0f);
113 NIR_PASS_V(s, nir_lower_io,
114 nir_var_shader_in | nir_var_shader_out, type_size, 0);
115 NIR_PASS_V(s, nir_lower_load_const_to_scalar);
116 NIR_PASS_V(s, lima_nir_lower_uniform_to_scalar);
117 NIR_PASS_V(s, nir_lower_io_to_scalar,
118 nir_var_shader_in|nir_var_shader_out);
119
120 do {
121 progress = false;
122
123 NIR_PASS_V(s, nir_lower_vars_to_ssa);
124 NIR_PASS(progress, s, nir_lower_alu_to_scalar, NULL, NULL);
125 NIR_PASS(progress, s, nir_lower_phis_to_scalar, false);
126 NIR_PASS(progress, s, nir_copy_prop);
127 NIR_PASS(progress, s, nir_opt_remove_phis);
128 NIR_PASS(progress, s, nir_opt_dce);
129 NIR_PASS(progress, s, nir_opt_dead_cf);
130 NIR_PASS(progress, s, nir_opt_cse);
131 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
132 NIR_PASS(progress, s, nir_opt_algebraic);
133 NIR_PASS(progress, s, lima_nir_lower_ftrunc);
134 NIR_PASS(progress, s, nir_opt_constant_folding);
135 NIR_PASS(progress, s, nir_opt_undef);
136 NIR_PASS(progress, s, nir_opt_loop_unroll);
137 } while (progress);
138
139 NIR_PASS_V(s, nir_lower_int_to_float);
140 /* int_to_float pass generates ftrunc, so lower it */
141 NIR_PASS(progress, s, lima_nir_lower_ftrunc);
142 NIR_PASS_V(s, nir_lower_bool_to_float);
143
144 NIR_PASS_V(s, nir_copy_prop);
145 NIR_PASS_V(s, nir_opt_dce);
146 NIR_PASS_V(s, lima_nir_split_loads);
147 NIR_PASS_V(s, nir_lower_locals_to_regs);
148 NIR_PASS_V(s, nir_convert_from_ssa, true);
149 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp, NULL);
150 nir_sweep(s);
151 }
152
153 static bool
lima_alu_to_scalar_filter_cb(const nir_instr * instr,const void * data)154 lima_alu_to_scalar_filter_cb(const nir_instr *instr, const void *data)
155 {
156 if (instr->type != nir_instr_type_alu)
157 return false;
158
159 nir_alu_instr *alu = nir_instr_as_alu(instr);
160 switch (alu->op) {
161 case nir_op_frcp:
162 case nir_op_frsq:
163 case nir_op_flog2:
164 case nir_op_fexp2:
165 case nir_op_fsqrt:
166 case nir_op_fsin:
167 case nir_op_fcos:
168 return true;
169 default:
170 break;
171 }
172
173 /* nir vec4 fcsel assumes that each component of the condition will be
174 * used to select the same component from the two options, but Utgard PP
175 * has only 1 component condition. If all condition components are not the
176 * same we need to lower it to scalar.
177 */
178 switch (alu->op) {
179 case nir_op_bcsel:
180 case nir_op_fcsel:
181 break;
182 default:
183 return false;
184 }
185
186 int num_components = nir_dest_num_components(alu->dest.dest);
187
188 uint8_t swizzle = alu->src[0].swizzle[0];
189
190 for (int i = 1; i < num_components; i++)
191 if (alu->src[0].swizzle[i] != swizzle)
192 return true;
193
194 return false;
195 }
196
197 static bool
lima_vec_to_movs_filter_cb(const nir_instr * instr,unsigned writemask,const void * data)198 lima_vec_to_movs_filter_cb(const nir_instr *instr, unsigned writemask,
199 const void *data)
200 {
201 assert(writemask > 0);
202 if (util_bitcount(writemask) == 1)
203 return true;
204
205 return !lima_alu_to_scalar_filter_cb(instr, data);
206 }
207
208 void
lima_program_optimize_fs_nir(struct nir_shader * s,struct nir_lower_tex_options * tex_options)209 lima_program_optimize_fs_nir(struct nir_shader *s,
210 struct nir_lower_tex_options *tex_options)
211 {
212 bool progress;
213
214 NIR_PASS_V(s, nir_lower_fragcoord_wtrans);
215 NIR_PASS_V(s, nir_lower_io,
216 nir_var_shader_in | nir_var_shader_out, type_size, 0);
217 NIR_PASS_V(s, nir_lower_regs_to_ssa);
218 NIR_PASS_V(s, nir_lower_tex, tex_options);
219
220 do {
221 progress = false;
222 NIR_PASS(progress, s, nir_opt_vectorize, NULL, NULL);
223 } while (progress);
224
225 do {
226 progress = false;
227
228 NIR_PASS_V(s, nir_lower_vars_to_ssa);
229 NIR_PASS(progress, s, nir_lower_alu_to_scalar, lima_alu_to_scalar_filter_cb, NULL);
230 NIR_PASS(progress, s, nir_copy_prop);
231 NIR_PASS(progress, s, nir_opt_remove_phis);
232 NIR_PASS(progress, s, nir_opt_dce);
233 NIR_PASS(progress, s, nir_opt_dead_cf);
234 NIR_PASS(progress, s, nir_opt_cse);
235 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
236 NIR_PASS(progress, s, nir_opt_algebraic);
237 NIR_PASS(progress, s, nir_opt_constant_folding);
238 NIR_PASS(progress, s, nir_opt_undef);
239 NIR_PASS(progress, s, nir_opt_loop_unroll);
240 NIR_PASS(progress, s, lima_nir_split_load_input);
241 } while (progress);
242
243 NIR_PASS_V(s, nir_lower_int_to_float);
244 NIR_PASS_V(s, nir_lower_bool_to_float);
245
246 /* Some ops must be lowered after being converted from int ops,
247 * so re-run nir_opt_algebraic after int lowering. */
248 do {
249 progress = false;
250 NIR_PASS(progress, s, nir_opt_algebraic);
251 } while (progress);
252
253 /* Must be run after optimization loop */
254 NIR_PASS_V(s, lima_nir_scale_trig);
255
256 /* Lower modifiers */
257 NIR_PASS_V(s, nir_lower_to_source_mods, nir_lower_all_source_mods);
258 NIR_PASS_V(s, nir_copy_prop);
259 NIR_PASS_V(s, nir_opt_dce);
260
261 NIR_PASS_V(s, nir_lower_locals_to_regs);
262 NIR_PASS_V(s, nir_convert_from_ssa, true);
263 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp, NULL);
264
265 NIR_PASS_V(s, nir_move_vec_src_uses_to_dest);
266 NIR_PASS_V(s, nir_lower_vec_to_movs, lima_vec_to_movs_filter_cb, NULL);
267 NIR_PASS_V(s, nir_opt_dce); /* clean up any new dead code from vec to movs */
268
269 NIR_PASS_V(s, lima_nir_duplicate_load_uniforms);
270 NIR_PASS_V(s, lima_nir_duplicate_load_inputs);
271 NIR_PASS_V(s, lima_nir_duplicate_load_consts);
272
273 nir_sweep(s);
274 }
275
276 static bool
lima_fs_compile_shader(struct lima_context * ctx,struct lima_fs_key * key,struct lima_fs_uncompiled_shader * ufs,struct lima_fs_compiled_shader * fs)277 lima_fs_compile_shader(struct lima_context *ctx,
278 struct lima_fs_key *key,
279 struct lima_fs_uncompiled_shader *ufs,
280 struct lima_fs_compiled_shader *fs)
281 {
282 struct lima_screen *screen = lima_screen(ctx->base.screen);
283 nir_shader *nir = nir_shader_clone(fs, ufs->base.ir.nir);
284
285 struct nir_lower_tex_options tex_options = {
286 .lower_txp = ~0u,
287 .swizzle_result = ~0u,
288 };
289
290 for (int i = 0; i < ARRAY_SIZE(key->tex); i++) {
291 for (int j = 0; j < 4; j++)
292 tex_options.swizzles[i][j] = key->tex[i].swizzle[j];
293 }
294
295 lima_program_optimize_fs_nir(nir, &tex_options);
296
297 if (lima_debug & LIMA_DEBUG_PP)
298 nir_print_shader(nir, stdout);
299
300 if (!ppir_compile_nir(fs, nir, screen->pp_ra, &ctx->debug)) {
301 ralloc_free(nir);
302 return false;
303 }
304
305 fs->state.uses_discard = nir->info.fs.uses_discard;
306 ralloc_free(nir);
307
308 return true;
309 }
310
311 static bool
lima_fs_upload_shader(struct lima_context * ctx,struct lima_fs_compiled_shader * fs)312 lima_fs_upload_shader(struct lima_context *ctx,
313 struct lima_fs_compiled_shader *fs)
314 {
315 struct lima_screen *screen = lima_screen(ctx->base.screen);
316
317 fs->bo = lima_bo_create(screen, fs->state.shader_size, 0);
318 if (!fs->bo) {
319 fprintf(stderr, "lima: create fs shader bo fail\n");
320 return false;
321 }
322
323 memcpy(lima_bo_map(fs->bo), fs->shader, fs->state.shader_size);
324
325 return true;
326 }
327
328 static struct lima_fs_compiled_shader *
lima_get_compiled_fs(struct lima_context * ctx,struct lima_fs_uncompiled_shader * ufs,struct lima_fs_key * key)329 lima_get_compiled_fs(struct lima_context *ctx,
330 struct lima_fs_uncompiled_shader *ufs,
331 struct lima_fs_key *key)
332 {
333 struct lima_screen *screen = lima_screen(ctx->base.screen);
334 struct hash_table *ht;
335 uint32_t key_size;
336
337 ht = ctx->fs_cache;
338 key_size = sizeof(struct lima_fs_key);
339
340 struct hash_entry *entry = _mesa_hash_table_search(ht, key);
341 if (entry)
342 return entry->data;
343
344 /* Not on memory cache, try disk cache */
345 struct lima_fs_compiled_shader *fs =
346 lima_fs_disk_cache_retrieve(screen->disk_cache, key);
347
348 if (!fs) {
349 /* Not on disk cache, compile and insert into disk cache*/
350 fs = rzalloc(NULL, struct lima_fs_compiled_shader);
351 if (!fs)
352 return NULL;
353
354 if (!lima_fs_compile_shader(ctx, key, ufs, fs))
355 goto err;
356
357 lima_fs_disk_cache_store(screen->disk_cache, key, fs);
358 }
359
360 if (!lima_fs_upload_shader(ctx, fs))
361 goto err;
362
363 ralloc_free(fs->shader);
364 fs->shader = NULL;
365
366 /* Insert into memory cache */
367 struct lima_key *dup_key;
368 dup_key = rzalloc_size(fs, key_size);
369 memcpy(dup_key, key, key_size);
370 _mesa_hash_table_insert(ht, dup_key, fs);
371
372 return fs;
373
374 err:
375 ralloc_free(fs);
376 return NULL;
377 }
378
379 static void *
lima_create_fs_state(struct pipe_context * pctx,const struct pipe_shader_state * cso)380 lima_create_fs_state(struct pipe_context *pctx,
381 const struct pipe_shader_state *cso)
382 {
383 struct lima_context *ctx = lima_context(pctx);
384 struct lima_fs_uncompiled_shader *so = rzalloc(NULL, struct lima_fs_uncompiled_shader);
385
386 if (!so)
387 return NULL;
388
389 nir_shader *nir;
390 if (cso->type == PIPE_SHADER_IR_NIR)
391 /* The backend takes ownership of the NIR shader on state
392 * creation. */
393 nir = cso->ir.nir;
394 else {
395 assert(cso->type == PIPE_SHADER_IR_TGSI);
396
397 nir = tgsi_to_nir(cso->tokens, pctx->screen, false);
398 }
399
400 so->base.type = PIPE_SHADER_IR_NIR;
401 so->base.ir.nir = nir;
402
403 /* Serialize the NIR to a binary blob that we can hash for the disk
404 * cache. Drop unnecessary information (like variable names)
405 * so the serialized NIR is smaller, and also to let us detect more
406 * isomorphic shaders when hashing, increasing cache hits.
407 */
408 struct blob blob;
409 blob_init(&blob);
410 nir_serialize(&blob, nir, true);
411 _mesa_sha1_compute(blob.data, blob.size, so->nir_sha1);
412 blob_finish(&blob);
413
414 if (lima_debug & LIMA_DEBUG_PRECOMPILE) {
415 /* Trigger initial compilation with default settings */
416 struct lima_fs_key key;
417 memset(&key, 0, sizeof(key));
418 memcpy(key.nir_sha1, so->nir_sha1, sizeof(so->nir_sha1));
419 for (int i = 0; i < ARRAY_SIZE(key.tex); i++) {
420 for (int j = 0; j < 4; j++)
421 key.tex[i].swizzle[j] = j;
422 }
423 lima_get_compiled_fs(ctx, so, &key);
424 }
425
426 return so;
427 }
428
429 static void
lima_bind_fs_state(struct pipe_context * pctx,void * hwcso)430 lima_bind_fs_state(struct pipe_context *pctx, void *hwcso)
431 {
432 struct lima_context *ctx = lima_context(pctx);
433
434 ctx->uncomp_fs = hwcso;
435 ctx->dirty |= LIMA_CONTEXT_DIRTY_UNCOMPILED_FS;
436 }
437
438 static void
lima_delete_fs_state(struct pipe_context * pctx,void * hwcso)439 lima_delete_fs_state(struct pipe_context *pctx, void *hwcso)
440 {
441 struct lima_context *ctx = lima_context(pctx);
442 struct lima_fs_uncompiled_shader *so = hwcso;
443
444 hash_table_foreach(ctx->fs_cache, entry) {
445 const struct lima_fs_key *key = entry->key;
446 if (!memcmp(key->nir_sha1, so->nir_sha1, sizeof(so->nir_sha1))) {
447 struct lima_fs_compiled_shader *fs = entry->data;
448 _mesa_hash_table_remove(ctx->fs_cache, entry);
449 if (fs->bo)
450 lima_bo_unreference(fs->bo);
451
452 if (fs == ctx->fs)
453 ctx->fs = NULL;
454
455 ralloc_free(fs);
456 }
457 }
458
459 ralloc_free(so->base.ir.nir);
460 ralloc_free(so);
461 }
462
463 static bool
lima_vs_compile_shader(struct lima_context * ctx,struct lima_vs_key * key,struct lima_vs_uncompiled_shader * uvs,struct lima_vs_compiled_shader * vs)464 lima_vs_compile_shader(struct lima_context *ctx,
465 struct lima_vs_key *key,
466 struct lima_vs_uncompiled_shader *uvs,
467 struct lima_vs_compiled_shader *vs)
468 {
469 nir_shader *nir = nir_shader_clone(vs, uvs->base.ir.nir);
470
471 lima_program_optimize_vs_nir(nir);
472
473 if (lima_debug & LIMA_DEBUG_GP)
474 nir_print_shader(nir, stdout);
475
476 if (!gpir_compile_nir(vs, nir, &ctx->debug)) {
477 ralloc_free(nir);
478 return false;
479 }
480
481 ralloc_free(nir);
482
483 return true;
484 }
485
486 static bool
lima_vs_upload_shader(struct lima_context * ctx,struct lima_vs_compiled_shader * vs)487 lima_vs_upload_shader(struct lima_context *ctx,
488 struct lima_vs_compiled_shader *vs)
489 {
490 struct lima_screen *screen = lima_screen(ctx->base.screen);
491 vs->bo = lima_bo_create(screen, vs->state.shader_size, 0);
492 if (!vs->bo) {
493 fprintf(stderr, "lima: create vs shader bo fail\n");
494 return false;
495 }
496
497 memcpy(lima_bo_map(vs->bo), vs->shader, vs->state.shader_size);
498
499 return true;
500 }
501
502 static struct lima_vs_compiled_shader *
lima_get_compiled_vs(struct lima_context * ctx,struct lima_vs_uncompiled_shader * uvs,struct lima_vs_key * key)503 lima_get_compiled_vs(struct lima_context *ctx,
504 struct lima_vs_uncompiled_shader *uvs,
505 struct lima_vs_key *key)
506 {
507 struct lima_screen *screen = lima_screen(ctx->base.screen);
508 struct hash_table *ht;
509 uint32_t key_size;
510
511 ht = ctx->vs_cache;
512 key_size = sizeof(struct lima_vs_key);
513
514 struct hash_entry *entry = _mesa_hash_table_search(ht, key);
515 if (entry)
516 return entry->data;
517
518 /* Not on memory cache, try disk cache */
519 struct lima_vs_compiled_shader *vs =
520 lima_vs_disk_cache_retrieve(screen->disk_cache, key);
521
522 if (!vs) {
523 /* Not on disk cache, compile and insert into disk cache */
524 vs = rzalloc(NULL, struct lima_vs_compiled_shader);
525 if (!vs)
526 return NULL;
527 if (!lima_vs_compile_shader(ctx, key, uvs, vs))
528 goto err;
529
530 lima_vs_disk_cache_store(screen->disk_cache, key, vs);
531 }
532
533 if (!lima_vs_upload_shader(ctx, vs))
534 goto err;
535
536 ralloc_free(vs->shader);
537 vs->shader = NULL;
538
539 struct lima_key *dup_key;
540 dup_key = rzalloc_size(vs, key_size);
541 memcpy(dup_key, key, key_size);
542 _mesa_hash_table_insert(ht, dup_key, vs);
543
544 return vs;
545
546 err:
547 ralloc_free(vs);
548 return NULL;
549 }
550
551 bool
lima_update_vs_state(struct lima_context * ctx)552 lima_update_vs_state(struct lima_context *ctx)
553 {
554 if (!(ctx->dirty & LIMA_CONTEXT_DIRTY_UNCOMPILED_VS)) {
555 return true;
556 }
557
558 struct lima_vs_key local_key;
559 struct lima_vs_key *key = &local_key;
560 memset(key, 0, sizeof(*key));
561 memcpy(key->nir_sha1, ctx->uncomp_vs->nir_sha1,
562 sizeof(ctx->uncomp_vs->nir_sha1));
563
564 struct lima_vs_compiled_shader *old_vs = ctx->vs;
565 struct lima_vs_compiled_shader *vs = lima_get_compiled_vs(ctx,
566 ctx->uncomp_vs,
567 key);
568 if (!vs)
569 return false;
570
571 ctx->vs = vs;
572
573 if (ctx->vs != old_vs)
574 ctx->dirty |= LIMA_CONTEXT_DIRTY_COMPILED_VS;
575
576 return true;
577 }
578
579 bool
lima_update_fs_state(struct lima_context * ctx)580 lima_update_fs_state(struct lima_context *ctx)
581 {
582 if (!(ctx->dirty & (LIMA_CONTEXT_DIRTY_UNCOMPILED_FS |
583 LIMA_CONTEXT_DIRTY_TEXTURES))) {
584 return true;
585 }
586
587 struct lima_texture_stateobj *lima_tex = &ctx->tex_stateobj;
588 struct lima_fs_key local_key;
589 struct lima_fs_key *key = &local_key;
590 memset(key, 0, sizeof(*key));
591 memcpy(key->nir_sha1, ctx->uncomp_fs->nir_sha1,
592 sizeof(ctx->uncomp_fs->nir_sha1));
593
594 for (int i = 0; i < lima_tex->num_textures; i++) {
595 struct lima_sampler_view *sampler = lima_sampler_view(lima_tex->textures[i]);
596 for (int j = 0; j < 4; j++)
597 key->tex[i].swizzle[j] = sampler->swizzle[j];
598 }
599
600 /* Fill rest with identity swizzle */
601 uint8_t identity[4] = { PIPE_SWIZZLE_X, PIPE_SWIZZLE_Y,
602 PIPE_SWIZZLE_Z, PIPE_SWIZZLE_W };
603 for (int i = lima_tex->num_textures; i < ARRAY_SIZE(key->tex); i++)
604 memcpy(key->tex[i].swizzle, identity, 4);
605
606 struct lima_fs_compiled_shader *old_fs = ctx->fs;
607
608 struct lima_fs_compiled_shader *fs = lima_get_compiled_fs(ctx,
609 ctx->uncomp_fs,
610 key);
611 if (!fs)
612 return false;
613
614 ctx->fs = fs;
615
616 if (ctx->fs != old_fs)
617 ctx->dirty |= LIMA_CONTEXT_DIRTY_COMPILED_FS;
618
619 return true;
620 }
621
622 static void *
lima_create_vs_state(struct pipe_context * pctx,const struct pipe_shader_state * cso)623 lima_create_vs_state(struct pipe_context *pctx,
624 const struct pipe_shader_state *cso)
625 {
626 struct lima_context *ctx = lima_context(pctx);
627 struct lima_vs_uncompiled_shader *so = rzalloc(NULL, struct lima_vs_uncompiled_shader);
628
629 if (!so)
630 return NULL;
631
632 nir_shader *nir;
633 if (cso->type == PIPE_SHADER_IR_NIR)
634 /* The backend takes ownership of the NIR shader on state
635 * creation. */
636 nir = cso->ir.nir;
637 else {
638 assert(cso->type == PIPE_SHADER_IR_TGSI);
639
640 nir = tgsi_to_nir(cso->tokens, pctx->screen, false);
641 }
642
643 so->base.type = PIPE_SHADER_IR_NIR;
644 so->base.ir.nir = nir;
645
646 /* Serialize the NIR to a binary blob that we can hash for the disk
647 * cache. Drop unnecessary information (like variable names)
648 * so the serialized NIR is smaller, and also to let us detect more
649 * isomorphic shaders when hashing, increasing cache hits.
650 */
651 struct blob blob;
652 blob_init(&blob);
653 nir_serialize(&blob, nir, true);
654 _mesa_sha1_compute(blob.data, blob.size, so->nir_sha1);
655 blob_finish(&blob);
656
657 if (lima_debug & LIMA_DEBUG_PRECOMPILE) {
658 /* Trigger initial compilation with default settings */
659 struct lima_vs_key key;
660 memset(&key, 0, sizeof(key));
661 memcpy(key.nir_sha1, so->nir_sha1, sizeof(so->nir_sha1));
662 lima_get_compiled_vs(ctx, so, &key);
663 }
664
665 return so;
666 }
667
668 static void
lima_bind_vs_state(struct pipe_context * pctx,void * hwcso)669 lima_bind_vs_state(struct pipe_context *pctx, void *hwcso)
670 {
671 struct lima_context *ctx = lima_context(pctx);
672
673 ctx->uncomp_vs = hwcso;
674 ctx->dirty |= LIMA_CONTEXT_DIRTY_UNCOMPILED_VS;
675 }
676
677 static void
lima_delete_vs_state(struct pipe_context * pctx,void * hwcso)678 lima_delete_vs_state(struct pipe_context *pctx, void *hwcso)
679 {
680 struct lima_context *ctx = lima_context(pctx);
681 struct lima_vs_uncompiled_shader *so = hwcso;
682
683 hash_table_foreach(ctx->vs_cache, entry) {
684 const struct lima_vs_key *key = entry->key;
685 if (!memcmp(key->nir_sha1, so->nir_sha1, sizeof(so->nir_sha1))) {
686 struct lima_vs_compiled_shader *vs = entry->data;
687 _mesa_hash_table_remove(ctx->vs_cache, entry);
688 if (vs->bo)
689 lima_bo_unreference(vs->bo);
690
691 if (vs == ctx->vs)
692 ctx->vs = NULL;
693
694 ralloc_free(vs);
695 }
696 }
697
698 ralloc_free(so->base.ir.nir);
699 ralloc_free(so);
700 }
701
702 static uint32_t
lima_fs_cache_hash(const void * key)703 lima_fs_cache_hash(const void *key)
704 {
705 return _mesa_hash_data(key, sizeof(struct lima_fs_key));
706 }
707
708 static uint32_t
lima_vs_cache_hash(const void * key)709 lima_vs_cache_hash(const void *key)
710 {
711 return _mesa_hash_data(key, sizeof(struct lima_vs_key));
712 }
713
714 static bool
lima_fs_cache_compare(const void * key1,const void * key2)715 lima_fs_cache_compare(const void *key1, const void *key2)
716 {
717 return memcmp(key1, key2, sizeof(struct lima_fs_key)) == 0;
718 }
719
720 static bool
lima_vs_cache_compare(const void * key1,const void * key2)721 lima_vs_cache_compare(const void *key1, const void *key2)
722 {
723 return memcmp(key1, key2, sizeof(struct lima_vs_key)) == 0;
724 }
725
726 void
lima_program_init(struct lima_context * ctx)727 lima_program_init(struct lima_context *ctx)
728 {
729 ctx->base.create_fs_state = lima_create_fs_state;
730 ctx->base.bind_fs_state = lima_bind_fs_state;
731 ctx->base.delete_fs_state = lima_delete_fs_state;
732
733 ctx->base.create_vs_state = lima_create_vs_state;
734 ctx->base.bind_vs_state = lima_bind_vs_state;
735 ctx->base.delete_vs_state = lima_delete_vs_state;
736
737 ctx->fs_cache = _mesa_hash_table_create(ctx, lima_fs_cache_hash,
738 lima_fs_cache_compare);
739 ctx->vs_cache = _mesa_hash_table_create(ctx, lima_vs_cache_hash,
740 lima_vs_cache_compare);
741 }
742
743 void
lima_program_fini(struct lima_context * ctx)744 lima_program_fini(struct lima_context *ctx)
745 {
746 hash_table_foreach(ctx->vs_cache, entry) {
747 struct lima_vs_compiled_shader *vs = entry->data;
748 if (vs->bo)
749 lima_bo_unreference(vs->bo);
750 ralloc_free(vs);
751 _mesa_hash_table_remove(ctx->vs_cache, entry);
752 }
753
754 hash_table_foreach(ctx->fs_cache, entry) {
755 struct lima_fs_compiled_shader *fs = entry->data;
756 if (fs->bo)
757 lima_bo_unreference(fs->bo);
758 ralloc_free(fs);
759 _mesa_hash_table_remove(ctx->fs_cache, entry);
760 }
761 }
762