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