Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/lima/lima_program.c
4565 views
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
};
65
66
static const nir_shader_compiler_options fs_nir_options = {
67
.lower_ffma16 = true,
68
.lower_ffma32 = true,
69
.lower_ffma64 = true,
70
.lower_fpow = true,
71
.lower_fdiv = true,
72
.lower_fmod = true,
73
.lower_flrp32 = true,
74
.lower_flrp64 = true,
75
.lower_fsign = true,
76
.lower_rotate = true,
77
.lower_fdot = true,
78
.lower_fdph = true,
79
.lower_insert_byte = true,
80
.lower_insert_word = true,
81
.lower_bitops = true,
82
.lower_vector_cmp = true,
83
};
84
85
const void *
86
lima_program_get_compiler_options(enum pipe_shader_type shader)
87
{
88
switch (shader) {
89
case PIPE_SHADER_VERTEX:
90
return &vs_nir_options;
91
case PIPE_SHADER_FRAGMENT:
92
return &fs_nir_options;
93
default:
94
return NULL;
95
}
96
}
97
98
static int
99
type_size(const struct glsl_type *type, bool bindless)
100
{
101
return glsl_count_attribute_slots(type, false);
102
}
103
104
void
105
lima_program_optimize_vs_nir(struct nir_shader *s)
106
{
107
bool progress;
108
109
NIR_PASS_V(s, nir_lower_viewport_transform);
110
NIR_PASS_V(s, nir_lower_point_size, 1.0f, 100.0f);
111
NIR_PASS_V(s, nir_lower_io,
112
nir_var_shader_in | nir_var_shader_out, type_size, 0);
113
NIR_PASS_V(s, nir_lower_load_const_to_scalar);
114
NIR_PASS_V(s, lima_nir_lower_uniform_to_scalar);
115
NIR_PASS_V(s, nir_lower_io_to_scalar,
116
nir_var_shader_in|nir_var_shader_out);
117
118
do {
119
progress = false;
120
121
NIR_PASS_V(s, nir_lower_vars_to_ssa);
122
NIR_PASS(progress, s, nir_lower_alu_to_scalar, NULL, NULL);
123
NIR_PASS(progress, s, nir_lower_phis_to_scalar, false);
124
NIR_PASS(progress, s, nir_copy_prop);
125
NIR_PASS(progress, s, nir_opt_remove_phis);
126
NIR_PASS(progress, s, nir_opt_dce);
127
NIR_PASS(progress, s, nir_opt_dead_cf);
128
NIR_PASS(progress, s, nir_opt_cse);
129
NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
130
NIR_PASS(progress, s, nir_opt_algebraic);
131
NIR_PASS(progress, s, lima_nir_lower_ftrunc);
132
NIR_PASS(progress, s, nir_opt_constant_folding);
133
NIR_PASS(progress, s, nir_opt_undef);
134
NIR_PASS(progress, s, nir_opt_loop_unroll,
135
nir_var_shader_in |
136
nir_var_shader_out |
137
nir_var_function_temp);
138
} while (progress);
139
140
NIR_PASS_V(s, nir_lower_int_to_float);
141
/* int_to_float pass generates ftrunc, so lower it */
142
NIR_PASS(progress, s, lima_nir_lower_ftrunc);
143
NIR_PASS_V(s, nir_lower_bool_to_float);
144
145
NIR_PASS_V(s, nir_copy_prop);
146
NIR_PASS_V(s, nir_opt_dce);
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
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
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
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_var_shader_in |
241
nir_var_shader_out |
242
nir_var_function_temp);
243
NIR_PASS(progress, s, lima_nir_split_load_input);
244
} while (progress);
245
246
NIR_PASS_V(s, nir_lower_int_to_float);
247
NIR_PASS_V(s, nir_lower_bool_to_float);
248
249
/* Some ops must be lowered after being converted from int ops,
250
* so re-run nir_opt_algebraic after int lowering. */
251
do {
252
progress = false;
253
NIR_PASS(progress, s, nir_opt_algebraic);
254
} while (progress);
255
256
/* Must be run after optimization loop */
257
NIR_PASS_V(s, lima_nir_scale_trig);
258
259
/* Lower modifiers */
260
NIR_PASS_V(s, nir_lower_to_source_mods, nir_lower_all_source_mods);
261
NIR_PASS_V(s, nir_copy_prop);
262
NIR_PASS_V(s, nir_opt_dce);
263
264
NIR_PASS_V(s, nir_lower_locals_to_regs);
265
NIR_PASS_V(s, nir_convert_from_ssa, true);
266
NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp, NULL);
267
268
NIR_PASS_V(s, nir_move_vec_src_uses_to_dest);
269
NIR_PASS_V(s, nir_lower_vec_to_movs, lima_vec_to_movs_filter_cb, NULL);
270
NIR_PASS_V(s, nir_opt_dce); /* clean up any new dead code from vec to movs */
271
272
NIR_PASS_V(s, lima_nir_duplicate_load_uniforms);
273
NIR_PASS_V(s, lima_nir_duplicate_load_inputs);
274
NIR_PASS_V(s, lima_nir_duplicate_load_consts);
275
276
nir_sweep(s);
277
}
278
279
static bool
280
lima_fs_compile_shader(struct lima_context *ctx,
281
struct lima_fs_key *key,
282
struct lima_fs_uncompiled_shader *ufs,
283
struct lima_fs_compiled_shader *fs)
284
{
285
struct lima_screen *screen = lima_screen(ctx->base.screen);
286
nir_shader *nir = nir_shader_clone(fs, ufs->base.ir.nir);
287
288
struct nir_lower_tex_options tex_options = {
289
.lower_txp = ~0u,
290
.swizzle_result = ~0u,
291
};
292
293
for (int i = 0; i < ARRAY_SIZE(key->tex); i++) {
294
for (int j = 0; j < 4; j++)
295
tex_options.swizzles[i][j] = key->tex[i].swizzle[j];
296
}
297
298
lima_program_optimize_fs_nir(nir, &tex_options);
299
300
if (lima_debug & LIMA_DEBUG_PP)
301
nir_print_shader(nir, stdout);
302
303
if (!ppir_compile_nir(fs, nir, screen->pp_ra, &ctx->debug)) {
304
ralloc_free(nir);
305
return false;
306
}
307
308
fs->state.uses_discard = nir->info.fs.uses_discard;
309
ralloc_free(nir);
310
311
return true;
312
}
313
314
static bool
315
lima_fs_upload_shader(struct lima_context *ctx,
316
struct lima_fs_compiled_shader *fs)
317
{
318
struct lima_screen *screen = lima_screen(ctx->base.screen);
319
320
fs->bo = lima_bo_create(screen, fs->state.shader_size, 0);
321
if (!fs->bo) {
322
fprintf(stderr, "lima: create fs shader bo fail\n");
323
return false;
324
}
325
326
memcpy(lima_bo_map(fs->bo), fs->shader, fs->state.shader_size);
327
328
return true;
329
}
330
331
static struct lima_fs_compiled_shader *
332
lima_get_compiled_fs(struct lima_context *ctx,
333
struct lima_fs_uncompiled_shader *ufs,
334
struct lima_fs_key *key)
335
{
336
struct lima_screen *screen = lima_screen(ctx->base.screen);
337
struct hash_table *ht;
338
uint32_t key_size;
339
340
ht = ctx->fs_cache;
341
key_size = sizeof(struct lima_fs_key);
342
343
struct hash_entry *entry = _mesa_hash_table_search(ht, key);
344
if (entry)
345
return entry->data;
346
347
/* Not on memory cache, try disk cache */
348
struct lima_fs_compiled_shader *fs =
349
lima_fs_disk_cache_retrieve(screen->disk_cache, key);
350
351
if (!fs) {
352
/* Not on disk cache, compile and insert into disk cache*/
353
fs = rzalloc(NULL, struct lima_fs_compiled_shader);
354
if (!fs)
355
return NULL;
356
357
if (!lima_fs_compile_shader(ctx, key, ufs, fs))
358
goto err;
359
360
lima_fs_disk_cache_store(screen->disk_cache, key, fs);
361
}
362
363
if (!lima_fs_upload_shader(ctx, fs))
364
goto err;
365
366
ralloc_free(fs->shader);
367
fs->shader = NULL;
368
369
/* Insert into memory cache */
370
struct lima_key *dup_key;
371
dup_key = rzalloc_size(fs, key_size);
372
memcpy(dup_key, key, key_size);
373
_mesa_hash_table_insert(ht, dup_key, fs);
374
375
return fs;
376
377
err:
378
ralloc_free(fs);
379
return NULL;
380
}
381
382
static void *
383
lima_create_fs_state(struct pipe_context *pctx,
384
const struct pipe_shader_state *cso)
385
{
386
struct lima_context *ctx = lima_context(pctx);
387
struct lima_fs_uncompiled_shader *so = rzalloc(NULL, struct lima_fs_uncompiled_shader);
388
389
if (!so)
390
return NULL;
391
392
nir_shader *nir;
393
if (cso->type == PIPE_SHADER_IR_NIR)
394
/* The backend takes ownership of the NIR shader on state
395
* creation. */
396
nir = cso->ir.nir;
397
else {
398
assert(cso->type == PIPE_SHADER_IR_TGSI);
399
400
nir = tgsi_to_nir(cso->tokens, pctx->screen, false);
401
}
402
403
so->base.type = PIPE_SHADER_IR_NIR;
404
so->base.ir.nir = nir;
405
406
/* Serialize the NIR to a binary blob that we can hash for the disk
407
* cache. Drop unnecessary information (like variable names)
408
* so the serialized NIR is smaller, and also to let us detect more
409
* isomorphic shaders when hashing, increasing cache hits.
410
*/
411
struct blob blob;
412
blob_init(&blob);
413
nir_serialize(&blob, nir, true);
414
_mesa_sha1_compute(blob.data, blob.size, so->nir_sha1);
415
blob_finish(&blob);
416
417
if (lima_debug & LIMA_DEBUG_PRECOMPILE) {
418
/* Trigger initial compilation with default settings */
419
struct lima_fs_key key;
420
memset(&key, 0, sizeof(key));
421
memcpy(key.nir_sha1, so->nir_sha1, sizeof(so->nir_sha1));
422
for (int i = 0; i < ARRAY_SIZE(key.tex); i++) {
423
for (int j = 0; j < 4; j++)
424
key.tex[i].swizzle[j] = j;
425
}
426
lima_get_compiled_fs(ctx, so, &key);
427
}
428
429
return so;
430
}
431
432
static void
433
lima_bind_fs_state(struct pipe_context *pctx, void *hwcso)
434
{
435
struct lima_context *ctx = lima_context(pctx);
436
437
ctx->uncomp_fs = hwcso;
438
ctx->dirty |= LIMA_CONTEXT_DIRTY_UNCOMPILED_FS;
439
}
440
441
static void
442
lima_delete_fs_state(struct pipe_context *pctx, void *hwcso)
443
{
444
struct lima_context *ctx = lima_context(pctx);
445
struct lima_fs_uncompiled_shader *so = hwcso;
446
447
hash_table_foreach(ctx->fs_cache, entry) {
448
const struct lima_fs_key *key = entry->key;
449
if (!memcmp(key->nir_sha1, so->nir_sha1, sizeof(so->nir_sha1))) {
450
struct lima_fs_compiled_shader *fs = entry->data;
451
_mesa_hash_table_remove(ctx->fs_cache, entry);
452
if (fs->bo)
453
lima_bo_unreference(fs->bo);
454
455
if (fs == ctx->fs)
456
ctx->fs = NULL;
457
458
ralloc_free(fs);
459
}
460
}
461
462
ralloc_free(so->base.ir.nir);
463
ralloc_free(so);
464
}
465
466
static bool
467
lima_vs_compile_shader(struct lima_context *ctx,
468
struct lima_vs_key *key,
469
struct lima_vs_uncompiled_shader *uvs,
470
struct lima_vs_compiled_shader *vs)
471
{
472
nir_shader *nir = nir_shader_clone(vs, uvs->base.ir.nir);
473
474
lima_program_optimize_vs_nir(nir);
475
476
if (lima_debug & LIMA_DEBUG_GP)
477
nir_print_shader(nir, stdout);
478
479
if (!gpir_compile_nir(vs, nir, &ctx->debug)) {
480
ralloc_free(nir);
481
return false;
482
}
483
484
ralloc_free(nir);
485
486
return true;
487
}
488
489
static bool
490
lima_vs_upload_shader(struct lima_context *ctx,
491
struct lima_vs_compiled_shader *vs)
492
{
493
struct lima_screen *screen = lima_screen(ctx->base.screen);
494
vs->bo = lima_bo_create(screen, vs->state.shader_size, 0);
495
if (!vs->bo) {
496
fprintf(stderr, "lima: create vs shader bo fail\n");
497
return false;
498
}
499
500
memcpy(lima_bo_map(vs->bo), vs->shader, vs->state.shader_size);
501
502
return true;
503
}
504
505
static struct lima_vs_compiled_shader *
506
lima_get_compiled_vs(struct lima_context *ctx,
507
struct lima_vs_uncompiled_shader *uvs,
508
struct lima_vs_key *key)
509
{
510
struct lima_screen *screen = lima_screen(ctx->base.screen);
511
struct hash_table *ht;
512
uint32_t key_size;
513
514
ht = ctx->vs_cache;
515
key_size = sizeof(struct lima_vs_key);
516
517
struct hash_entry *entry = _mesa_hash_table_search(ht, key);
518
if (entry)
519
return entry->data;
520
521
/* Not on memory cache, try disk cache */
522
struct lima_vs_compiled_shader *vs =
523
lima_vs_disk_cache_retrieve(screen->disk_cache, key);
524
525
if (!vs) {
526
/* Not on disk cache, compile and insert into disk cache */
527
vs = rzalloc(NULL, struct lima_vs_compiled_shader);
528
if (!vs)
529
return NULL;
530
if (!lima_vs_compile_shader(ctx, key, uvs, vs))
531
goto err;
532
533
lima_vs_disk_cache_store(screen->disk_cache, key, vs);
534
}
535
536
if (!lima_vs_upload_shader(ctx, vs))
537
goto err;
538
539
ralloc_free(vs->shader);
540
vs->shader = NULL;
541
542
struct lima_key *dup_key;
543
dup_key = rzalloc_size(vs, key_size);
544
memcpy(dup_key, key, key_size);
545
_mesa_hash_table_insert(ht, dup_key, vs);
546
547
return vs;
548
549
err:
550
ralloc_free(vs);
551
return NULL;
552
}
553
554
bool
555
lima_update_vs_state(struct lima_context *ctx)
556
{
557
if (!(ctx->dirty & LIMA_CONTEXT_DIRTY_UNCOMPILED_VS)) {
558
return true;
559
}
560
561
struct lima_vs_key local_key;
562
struct lima_vs_key *key = &local_key;
563
memset(key, 0, sizeof(*key));
564
memcpy(key->nir_sha1, ctx->uncomp_vs->nir_sha1,
565
sizeof(ctx->uncomp_vs->nir_sha1));
566
567
struct lima_vs_compiled_shader *old_vs = ctx->vs;
568
struct lima_vs_compiled_shader *vs = lima_get_compiled_vs(ctx,
569
ctx->uncomp_vs,
570
key);
571
if (!vs)
572
return false;
573
574
ctx->vs = vs;
575
576
if (ctx->vs != old_vs)
577
ctx->dirty |= LIMA_CONTEXT_DIRTY_COMPILED_VS;
578
579
return true;
580
}
581
582
bool
583
lima_update_fs_state(struct lima_context *ctx)
584
{
585
if (!(ctx->dirty & (LIMA_CONTEXT_DIRTY_UNCOMPILED_FS |
586
LIMA_CONTEXT_DIRTY_TEXTURES))) {
587
return true;
588
}
589
590
struct lima_texture_stateobj *lima_tex = &ctx->tex_stateobj;
591
struct lima_fs_key local_key;
592
struct lima_fs_key *key = &local_key;
593
memset(key, 0, sizeof(*key));
594
memcpy(key->nir_sha1, ctx->uncomp_fs->nir_sha1,
595
sizeof(ctx->uncomp_fs->nir_sha1));
596
597
for (int i = 0; i < lima_tex->num_textures; i++) {
598
struct lima_sampler_view *sampler = lima_sampler_view(lima_tex->textures[i]);
599
for (int j = 0; j < 4; j++)
600
key->tex[i].swizzle[j] = sampler->swizzle[j];
601
}
602
603
/* Fill rest with identity swizzle */
604
uint8_t identity[4] = { PIPE_SWIZZLE_X, PIPE_SWIZZLE_Y,
605
PIPE_SWIZZLE_Z, PIPE_SWIZZLE_W };
606
for (int i = lima_tex->num_textures; i < ARRAY_SIZE(key->tex); i++)
607
memcpy(key->tex[i].swizzle, identity, 4);
608
609
struct lima_fs_compiled_shader *old_fs = ctx->fs;
610
611
struct lima_fs_compiled_shader *fs = lima_get_compiled_fs(ctx,
612
ctx->uncomp_fs,
613
key);
614
if (!fs)
615
return false;
616
617
ctx->fs = fs;
618
619
if (ctx->fs != old_fs)
620
ctx->dirty |= LIMA_CONTEXT_DIRTY_COMPILED_FS;
621
622
return true;
623
}
624
625
static void *
626
lima_create_vs_state(struct pipe_context *pctx,
627
const struct pipe_shader_state *cso)
628
{
629
struct lima_context *ctx = lima_context(pctx);
630
struct lima_vs_uncompiled_shader *so = rzalloc(NULL, struct lima_vs_uncompiled_shader);
631
632
if (!so)
633
return NULL;
634
635
nir_shader *nir;
636
if (cso->type == PIPE_SHADER_IR_NIR)
637
/* The backend takes ownership of the NIR shader on state
638
* creation. */
639
nir = cso->ir.nir;
640
else {
641
assert(cso->type == PIPE_SHADER_IR_TGSI);
642
643
nir = tgsi_to_nir(cso->tokens, pctx->screen, false);
644
}
645
646
so->base.type = PIPE_SHADER_IR_NIR;
647
so->base.ir.nir = nir;
648
649
/* Serialize the NIR to a binary blob that we can hash for the disk
650
* cache. Drop unnecessary information (like variable names)
651
* so the serialized NIR is smaller, and also to let us detect more
652
* isomorphic shaders when hashing, increasing cache hits.
653
*/
654
struct blob blob;
655
blob_init(&blob);
656
nir_serialize(&blob, nir, true);
657
_mesa_sha1_compute(blob.data, blob.size, so->nir_sha1);
658
blob_finish(&blob);
659
660
if (lima_debug & LIMA_DEBUG_PRECOMPILE) {
661
/* Trigger initial compilation with default settings */
662
struct lima_vs_key key;
663
memset(&key, 0, sizeof(key));
664
memcpy(key.nir_sha1, so->nir_sha1, sizeof(so->nir_sha1));
665
lima_get_compiled_vs(ctx, so, &key);
666
}
667
668
return so;
669
}
670
671
static void
672
lima_bind_vs_state(struct pipe_context *pctx, void *hwcso)
673
{
674
struct lima_context *ctx = lima_context(pctx);
675
676
ctx->uncomp_vs = hwcso;
677
ctx->dirty |= LIMA_CONTEXT_DIRTY_UNCOMPILED_VS;
678
}
679
680
static void
681
lima_delete_vs_state(struct pipe_context *pctx, void *hwcso)
682
{
683
struct lima_context *ctx = lima_context(pctx);
684
struct lima_vs_uncompiled_shader *so = hwcso;
685
686
hash_table_foreach(ctx->vs_cache, entry) {
687
const struct lima_vs_key *key = entry->key;
688
if (!memcmp(key->nir_sha1, so->nir_sha1, sizeof(so->nir_sha1))) {
689
struct lima_vs_compiled_shader *vs = entry->data;
690
_mesa_hash_table_remove(ctx->vs_cache, entry);
691
if (vs->bo)
692
lima_bo_unreference(vs->bo);
693
694
if (vs == ctx->vs)
695
ctx->vs = NULL;
696
697
ralloc_free(vs);
698
}
699
}
700
701
ralloc_free(so->base.ir.nir);
702
ralloc_free(so);
703
}
704
705
static uint32_t
706
lima_fs_cache_hash(const void *key)
707
{
708
return _mesa_hash_data(key, sizeof(struct lima_fs_key));
709
}
710
711
static uint32_t
712
lima_vs_cache_hash(const void *key)
713
{
714
return _mesa_hash_data(key, sizeof(struct lima_vs_key));
715
}
716
717
static bool
718
lima_fs_cache_compare(const void *key1, const void *key2)
719
{
720
return memcmp(key1, key2, sizeof(struct lima_fs_key)) == 0;
721
}
722
723
static bool
724
lima_vs_cache_compare(const void *key1, const void *key2)
725
{
726
return memcmp(key1, key2, sizeof(struct lima_vs_key)) == 0;
727
}
728
729
void
730
lima_program_init(struct lima_context *ctx)
731
{
732
ctx->base.create_fs_state = lima_create_fs_state;
733
ctx->base.bind_fs_state = lima_bind_fs_state;
734
ctx->base.delete_fs_state = lima_delete_fs_state;
735
736
ctx->base.create_vs_state = lima_create_vs_state;
737
ctx->base.bind_vs_state = lima_bind_vs_state;
738
ctx->base.delete_vs_state = lima_delete_vs_state;
739
740
ctx->fs_cache = _mesa_hash_table_create(ctx, lima_fs_cache_hash,
741
lima_fs_cache_compare);
742
ctx->vs_cache = _mesa_hash_table_create(ctx, lima_vs_cache_hash,
743
lima_vs_cache_compare);
744
}
745
746
void
747
lima_program_fini(struct lima_context *ctx)
748
{
749
hash_table_foreach(ctx->vs_cache, entry) {
750
struct lima_vs_compiled_shader *vs = entry->data;
751
if (vs->bo)
752
lima_bo_unreference(vs->bo);
753
ralloc_free(vs);
754
_mesa_hash_table_remove(ctx->vs_cache, entry);
755
}
756
757
hash_table_foreach(ctx->fs_cache, entry) {
758
struct lima_fs_compiled_shader *fs = entry->data;
759
if (fs->bo)
760
lima_bo_unreference(fs->bo);
761
ralloc_free(fs);
762
_mesa_hash_table_remove(ctx->fs_cache, entry);
763
}
764
}
765
766