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