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