anv,i965,radv,st,ir3: Call nir_lower_deref_instrs
[mesa.git] / src / gallium / drivers / v3d / v3d_program.c
1 /*
2 * Copyright © 2014-2017 Broadcom
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, sublicense,
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 next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * 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 NONINFRINGEMENT. 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <inttypes.h>
25 #include "util/u_format.h"
26 #include "util/u_math.h"
27 #include "util/u_memory.h"
28 #include "util/ralloc.h"
29 #include "util/hash_table.h"
30 #include "tgsi/tgsi_dump.h"
31 #include "tgsi/tgsi_parse.h"
32 #include "compiler/nir/nir.h"
33 #include "compiler/nir/nir_builder.h"
34 #include "nir/tgsi_to_nir.h"
35 #include "compiler/v3d_compiler.h"
36 #include "v3d_context.h"
37 #include "broadcom/cle/v3d_packet_v33_pack.h"
38 #include "mesa/state_tracker/st_glsl_types.h"
39
40 static gl_varying_slot
41 v3d_get_slot_for_driver_location(nir_shader *s, uint32_t driver_location)
42 {
43 nir_foreach_variable(var, &s->outputs) {
44 if (var->data.driver_location == driver_location) {
45 return var->data.location;
46 }
47 }
48
49 return -1;
50 }
51
52 /**
53 * Precomputes the TRANSFORM_FEEDBACK_OUTPUT_DATA_SPEC array for the shader.
54 *
55 * A shader can have 16 of these specs, and each one of them can write up to
56 * 16 dwords. Since we allow a total of 64 transform feedback output
57 * components (not 16 vectors), we have to group the writes of multiple
58 * varyings together in a single data spec.
59 */
60 static void
61 v3d_set_transform_feedback_outputs(struct v3d_uncompiled_shader *so,
62 const struct pipe_stream_output_info *stream_output)
63 {
64 if (!stream_output->num_outputs)
65 return;
66
67 struct v3d_varying_slot slots[PIPE_MAX_SO_OUTPUTS * 4];
68 int slot_count = 0;
69
70 for (int buffer = 0; buffer < PIPE_MAX_SO_BUFFERS; buffer++) {
71 uint32_t buffer_offset = 0;
72 uint32_t vpm_start = slot_count;
73
74 for (int i = 0; i < stream_output->num_outputs; i++) {
75 const struct pipe_stream_output *output =
76 &stream_output->output[i];
77
78 if (output->output_buffer != buffer)
79 continue;
80
81 /* We assume that the SO outputs appear in increasing
82 * order in the buffer.
83 */
84 assert(output->dst_offset >= buffer_offset);
85
86 /* Pad any undefined slots in the output */
87 for (int j = buffer_offset; j < output->dst_offset; j++) {
88 slots[slot_count] =
89 v3d_slot_from_slot_and_component(VARYING_SLOT_POS, 0);
90 slot_count++;
91 buffer_offset++;
92 }
93
94 /* Set the coordinate shader up to output the
95 * components of this varying.
96 */
97 for (int j = 0; j < output->num_components; j++) {
98 gl_varying_slot slot =
99 v3d_get_slot_for_driver_location(so->base.ir.nir, output->register_index);
100
101 slots[slot_count] =
102 v3d_slot_from_slot_and_component(slot,
103 output->start_component + j);
104 slot_count++;
105 buffer_offset++;
106 }
107 }
108
109 uint32_t vpm_size = slot_count - vpm_start;
110 if (!vpm_size)
111 continue;
112
113 uint32_t vpm_start_offset = vpm_start + 6;
114
115 while (vpm_size) {
116 uint32_t write_size = MIN2(vpm_size, 1 << 4);
117
118 struct V3D33_TRANSFORM_FEEDBACK_OUTPUT_DATA_SPEC unpacked = {
119 /* We need the offset from the coordinate shader's VPM
120 * output block, which has the [X, Y, Z, W, Xs, Ys]
121 * values at the start.
122 */
123 .first_shaded_vertex_value_to_output = vpm_start_offset,
124 .number_of_consecutive_vertex_values_to_output_as_32_bit_values_minus_1 = write_size - 1,
125 .output_buffer_to_write_to = buffer,
126 };
127
128 /* GFXH-1559 */
129 assert(unpacked.first_shaded_vertex_value_to_output != 8 ||
130 so->num_tf_specs != 0);
131
132 assert(so->num_tf_specs != ARRAY_SIZE(so->tf_specs));
133 V3D33_TRANSFORM_FEEDBACK_OUTPUT_DATA_SPEC_pack(NULL,
134 (void *)&so->tf_specs[so->num_tf_specs],
135 &unpacked);
136
137 /* If point size is being written by the shader, then
138 * all the VPM start offsets are shifted up by one.
139 * We won't know that until the variant is compiled,
140 * though.
141 */
142 unpacked.first_shaded_vertex_value_to_output++;
143
144 /* GFXH-1559 */
145 assert(unpacked.first_shaded_vertex_value_to_output != 8 ||
146 so->num_tf_specs != 0);
147
148 V3D33_TRANSFORM_FEEDBACK_OUTPUT_DATA_SPEC_pack(NULL,
149 (void *)&so->tf_specs_psiz[so->num_tf_specs],
150 &unpacked);
151 so->num_tf_specs++;
152 vpm_start_offset += write_size;
153 vpm_size -= write_size;
154 }
155 so->base.stream_output.stride[buffer] =
156 stream_output->stride[buffer];
157 }
158
159 so->num_tf_outputs = slot_count;
160 so->tf_outputs = ralloc_array(so->base.ir.nir, struct v3d_varying_slot,
161 slot_count);
162 memcpy(so->tf_outputs, slots, sizeof(*slots) * slot_count);
163 }
164
165 static int
166 type_size(const struct glsl_type *type)
167 {
168 return glsl_count_attribute_slots(type, false);
169 }
170
171 static int
172 uniforms_type_size(const struct glsl_type *type)
173 {
174 return st_glsl_storage_type_size(type, false);
175 }
176
177 static void *
178 v3d_shader_state_create(struct pipe_context *pctx,
179 const struct pipe_shader_state *cso)
180 {
181 struct v3d_context *v3d = v3d_context(pctx);
182 struct v3d_uncompiled_shader *so = CALLOC_STRUCT(v3d_uncompiled_shader);
183 if (!so)
184 return NULL;
185
186 so->program_id = v3d->next_uncompiled_program_id++;
187
188 nir_shader *s;
189
190 if (cso->type == PIPE_SHADER_IR_NIR) {
191 /* The backend takes ownership of the NIR shader on state
192 * creation.
193 */
194 s = cso->ir.nir;
195
196 NIR_PASS_V(s, nir_lower_io, nir_var_all & ~nir_var_uniform,
197 type_size,
198 (nir_lower_io_options)0);
199 NIR_PASS_V(s, nir_lower_io, nir_var_uniform,
200 uniforms_type_size,
201 (nir_lower_io_options)0);
202 } else {
203 assert(cso->type == PIPE_SHADER_IR_TGSI);
204
205 if (V3D_DEBUG & V3D_DEBUG_TGSI) {
206 fprintf(stderr, "prog %d TGSI:\n",
207 so->program_id);
208 tgsi_dump(cso->tokens, 0);
209 fprintf(stderr, "\n");
210 }
211 s = tgsi_to_nir(cso->tokens, &v3d_nir_options);
212 NIR_PASS_V(s, nir_lower_deref_instrs, ~0);
213
214 so->was_tgsi = true;
215 }
216
217 NIR_PASS_V(s, nir_opt_global_to_local);
218 NIR_PASS_V(s, nir_lower_regs_to_ssa);
219 NIR_PASS_V(s, nir_normalize_cubemap_coords);
220
221 NIR_PASS_V(s, nir_lower_load_const_to_scalar);
222
223 v3d_optimize_nir(s);
224
225 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_local);
226
227 /* Garbage collect dead instructions */
228 nir_sweep(s);
229
230 so->base.type = PIPE_SHADER_IR_NIR;
231 so->base.ir.nir = s;
232
233 v3d_set_transform_feedback_outputs(so, &cso->stream_output);
234
235 if (V3D_DEBUG & (V3D_DEBUG_NIR |
236 v3d_debug_flag_for_shader_stage(s->info.stage))) {
237 fprintf(stderr, "%s prog %d NIR:\n",
238 gl_shader_stage_name(s->info.stage),
239 so->program_id);
240 nir_print_shader(s, stderr);
241 fprintf(stderr, "\n");
242 }
243
244 return so;
245 }
246
247 static struct v3d_compiled_shader *
248 v3d_get_compiled_shader(struct v3d_context *v3d, struct v3d_key *key)
249 {
250 struct v3d_uncompiled_shader *shader_state = key->shader_state;
251 nir_shader *s = shader_state->base.ir.nir;
252
253 struct hash_table *ht;
254 uint32_t key_size;
255 if (s->info.stage == MESA_SHADER_FRAGMENT) {
256 ht = v3d->fs_cache;
257 key_size = sizeof(struct v3d_fs_key);
258 } else {
259 ht = v3d->vs_cache;
260 key_size = sizeof(struct v3d_vs_key);
261 }
262
263 struct hash_entry *entry = _mesa_hash_table_search(ht, key);
264 if (entry)
265 return entry->data;
266
267 struct v3d_compiled_shader *shader =
268 rzalloc(NULL, struct v3d_compiled_shader);
269
270 int program_id = shader_state->program_id;
271 int variant_id =
272 p_atomic_inc_return(&shader_state->compiled_variant_count);
273 uint64_t *qpu_insts;
274 uint32_t shader_size;
275
276 switch (s->info.stage) {
277 case MESA_SHADER_VERTEX:
278 shader->prog_data.vs = rzalloc(shader, struct v3d_vs_prog_data);
279
280 qpu_insts = v3d_compile_vs(v3d->screen->compiler,
281 (struct v3d_vs_key *)key,
282 shader->prog_data.vs, s,
283 program_id, variant_id,
284 &shader_size);
285 break;
286 case MESA_SHADER_FRAGMENT:
287 shader->prog_data.fs = rzalloc(shader, struct v3d_fs_prog_data);
288
289 qpu_insts = v3d_compile_fs(v3d->screen->compiler,
290 (struct v3d_fs_key *)key,
291 shader->prog_data.fs, s,
292 program_id, variant_id,
293 &shader_size);
294 break;
295 default:
296 unreachable("bad stage");
297 }
298
299 v3d_set_shader_uniform_dirty_flags(shader);
300
301 shader->bo = v3d_bo_alloc(v3d->screen, shader_size, "shader");
302 v3d_bo_map(shader->bo);
303 memcpy(shader->bo->map, qpu_insts, shader_size);
304
305 free(qpu_insts);
306
307 struct v3d_key *dup_key;
308 dup_key = ralloc_size(shader, key_size);
309 memcpy(dup_key, key, key_size);
310 _mesa_hash_table_insert(ht, dup_key, shader);
311
312 if (shader->prog_data.base->spill_size >
313 v3d->prog.spill_size_per_thread) {
314 /* Max 4 QPUs per slice, 3 slices per core. We only do single
315 * core so far. This overallocates memory on smaller cores.
316 */
317 int total_spill_size =
318 4 * 3 * shader->prog_data.base->spill_size;
319
320 v3d_bo_unreference(&v3d->prog.spill_bo);
321 v3d->prog.spill_bo = v3d_bo_alloc(v3d->screen,
322 total_spill_size, "spill");
323 v3d->prog.spill_size_per_thread =
324 shader->prog_data.base->spill_size;
325 }
326
327 return shader;
328 }
329
330 static void
331 v3d_setup_shared_key(struct v3d_context *v3d, struct v3d_key *key,
332 struct v3d_texture_stateobj *texstate)
333 {
334 const struct v3d_device_info *devinfo = &v3d->screen->devinfo;
335
336 for (int i = 0; i < texstate->num_textures; i++) {
337 struct pipe_sampler_view *sampler = texstate->textures[i];
338 struct v3d_sampler_view *v3d_sampler = v3d_sampler_view(sampler);
339 struct pipe_sampler_state *sampler_state =
340 texstate->samplers[i];
341
342 if (!sampler)
343 continue;
344
345 key->tex[i].return_size =
346 v3d_get_tex_return_size(devinfo,
347 sampler->format,
348 sampler_state->compare_mode);
349
350 /* For 16-bit, we set up the sampler to always return 2
351 * channels (meaning no recompiles for most statechanges),
352 * while for 32 we actually scale the returns with channels.
353 */
354 if (key->tex[i].return_size == 16) {
355 key->tex[i].return_channels = 2;
356 } else if (devinfo->ver > 40) {
357 key->tex[i].return_channels = 4;
358 } else {
359 key->tex[i].return_channels =
360 v3d_get_tex_return_channels(devinfo,
361 sampler->format);
362 }
363
364 if (key->tex[i].return_size == 32 && devinfo->ver < 40) {
365 memcpy(key->tex[i].swizzle,
366 v3d_sampler->swizzle,
367 sizeof(v3d_sampler->swizzle));
368 } else {
369 /* For 16-bit returns, we let the sampler state handle
370 * the swizzle.
371 */
372 key->tex[i].swizzle[0] = PIPE_SWIZZLE_X;
373 key->tex[i].swizzle[1] = PIPE_SWIZZLE_Y;
374 key->tex[i].swizzle[2] = PIPE_SWIZZLE_Z;
375 key->tex[i].swizzle[3] = PIPE_SWIZZLE_W;
376 }
377
378 if (sampler) {
379 key->tex[i].compare_mode = sampler_state->compare_mode;
380 key->tex[i].compare_func = sampler_state->compare_func;
381 key->tex[i].clamp_s =
382 sampler_state->wrap_s == PIPE_TEX_WRAP_CLAMP;
383 key->tex[i].clamp_t =
384 sampler_state->wrap_t == PIPE_TEX_WRAP_CLAMP;
385 key->tex[i].clamp_r =
386 sampler_state->wrap_r == PIPE_TEX_WRAP_CLAMP;
387 }
388 }
389
390 key->ucp_enables = v3d->rasterizer->base.clip_plane_enable;
391 }
392
393 static void
394 v3d_update_compiled_fs(struct v3d_context *v3d, uint8_t prim_mode)
395 {
396 struct v3d_job *job = v3d->job;
397 struct v3d_fs_key local_key;
398 struct v3d_fs_key *key = &local_key;
399
400 if (!(v3d->dirty & (VC5_DIRTY_PRIM_MODE |
401 VC5_DIRTY_BLEND |
402 VC5_DIRTY_FRAMEBUFFER |
403 VC5_DIRTY_ZSA |
404 VC5_DIRTY_RASTERIZER |
405 VC5_DIRTY_SAMPLE_STATE |
406 VC5_DIRTY_FRAGTEX |
407 VC5_DIRTY_UNCOMPILED_FS))) {
408 return;
409 }
410
411 memset(key, 0, sizeof(*key));
412 v3d_setup_shared_key(v3d, &key->base, &v3d->fragtex);
413 key->base.shader_state = v3d->prog.bind_fs;
414 key->is_points = (prim_mode == PIPE_PRIM_POINTS);
415 key->is_lines = (prim_mode >= PIPE_PRIM_LINES &&
416 prim_mode <= PIPE_PRIM_LINE_STRIP);
417 key->clamp_color = v3d->rasterizer->base.clamp_fragment_color;
418 if (v3d->blend->logicop_enable) {
419 key->logicop_func = v3d->blend->logicop_func;
420 } else {
421 key->logicop_func = PIPE_LOGICOP_COPY;
422 }
423 if (job->msaa) {
424 key->msaa = v3d->rasterizer->base.multisample;
425 key->sample_coverage = (v3d->rasterizer->base.multisample &&
426 v3d->sample_mask != (1 << VC5_MAX_SAMPLES) - 1);
427 key->sample_alpha_to_coverage = v3d->blend->alpha_to_coverage;
428 key->sample_alpha_to_one = v3d->blend->alpha_to_one;
429 }
430
431 key->depth_enabled = (v3d->zsa->base.depth.enabled ||
432 v3d->zsa->base.stencil[0].enabled);
433 if (v3d->zsa->base.alpha.enabled) {
434 key->alpha_test = true;
435 key->alpha_test_func = v3d->zsa->base.alpha.func;
436 }
437
438 /* gl_FragColor's propagation to however many bound color buffers
439 * there are means that the buffer count needs to be in the key.
440 */
441 key->nr_cbufs = v3d->framebuffer.nr_cbufs;
442 key->swap_color_rb = v3d->swap_color_rb;
443
444 for (int i = 0; i < key->nr_cbufs; i++) {
445 struct pipe_surface *cbuf = v3d->framebuffer.cbufs[i];
446 if (!cbuf)
447 continue;
448
449 const struct util_format_description *desc =
450 util_format_description(cbuf->format);
451
452 if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT &&
453 desc->channel[0].size == 32) {
454 key->f32_color_rb |= 1 << i;
455 }
456
457 if (v3d->prog.bind_fs->was_tgsi) {
458 if (util_format_is_pure_uint(cbuf->format))
459 key->uint_color_rb |= 1 << i;
460 else if (util_format_is_pure_sint(cbuf->format))
461 key->int_color_rb |= 1 << i;
462 }
463 }
464
465 if (key->is_points) {
466 key->point_sprite_mask =
467 v3d->rasterizer->base.sprite_coord_enable;
468 key->point_coord_upper_left =
469 (v3d->rasterizer->base.sprite_coord_mode ==
470 PIPE_SPRITE_COORD_UPPER_LEFT);
471 }
472
473 key->light_twoside = v3d->rasterizer->base.light_twoside;
474 key->shade_model_flat = v3d->rasterizer->base.flatshade;
475
476 struct v3d_compiled_shader *old_fs = v3d->prog.fs;
477 v3d->prog.fs = v3d_get_compiled_shader(v3d, &key->base);
478 if (v3d->prog.fs == old_fs)
479 return;
480
481 v3d->dirty |= VC5_DIRTY_COMPILED_FS;
482
483 if (old_fs) {
484 if (v3d->prog.fs->prog_data.fs->flat_shade_flags !=
485 old_fs->prog_data.fs->flat_shade_flags) {
486 v3d->dirty |= VC5_DIRTY_FLAT_SHADE_FLAGS;
487 }
488
489 if (v3d->prog.fs->prog_data.fs->centroid_flags !=
490 old_fs->prog_data.fs->centroid_flags) {
491 v3d->dirty |= VC5_DIRTY_CENTROID_FLAGS;
492 }
493 }
494
495 if (old_fs && memcmp(v3d->prog.fs->prog_data.fs->input_slots,
496 old_fs->prog_data.fs->input_slots,
497 sizeof(v3d->prog.fs->prog_data.fs->input_slots))) {
498 v3d->dirty |= VC5_DIRTY_FS_INPUTS;
499 }
500 }
501
502 static void
503 v3d_update_compiled_vs(struct v3d_context *v3d, uint8_t prim_mode)
504 {
505 struct v3d_vs_key local_key;
506 struct v3d_vs_key *key = &local_key;
507
508 if (!(v3d->dirty & (VC5_DIRTY_PRIM_MODE |
509 VC5_DIRTY_RASTERIZER |
510 VC5_DIRTY_VERTTEX |
511 VC5_DIRTY_VTXSTATE |
512 VC5_DIRTY_UNCOMPILED_VS |
513 VC5_DIRTY_FS_INPUTS))) {
514 return;
515 }
516
517 memset(key, 0, sizeof(*key));
518 v3d_setup_shared_key(v3d, &key->base, &v3d->verttex);
519 key->base.shader_state = v3d->prog.bind_vs;
520 key->num_fs_inputs = v3d->prog.fs->prog_data.fs->base.num_inputs;
521 STATIC_ASSERT(sizeof(key->fs_inputs) ==
522 sizeof(v3d->prog.fs->prog_data.fs->input_slots));
523 memcpy(key->fs_inputs, v3d->prog.fs->prog_data.fs->input_slots,
524 sizeof(key->fs_inputs));
525 key->clamp_color = v3d->rasterizer->base.clamp_vertex_color;
526
527 key->per_vertex_point_size =
528 (prim_mode == PIPE_PRIM_POINTS &&
529 v3d->rasterizer->base.point_size_per_vertex);
530
531 struct v3d_compiled_shader *vs =
532 v3d_get_compiled_shader(v3d, &key->base);
533 if (vs != v3d->prog.vs) {
534 v3d->prog.vs = vs;
535 v3d->dirty |= VC5_DIRTY_COMPILED_VS;
536 }
537
538 key->is_coord = true;
539 /* Coord shaders only output varyings used by transform feedback. */
540 struct v3d_uncompiled_shader *shader_state = key->base.shader_state;
541 memcpy(key->fs_inputs, shader_state->tf_outputs,
542 sizeof(*key->fs_inputs) * shader_state->num_tf_outputs);
543 if (shader_state->num_tf_outputs < key->num_fs_inputs) {
544 memset(&key->fs_inputs[shader_state->num_tf_outputs],
545 0,
546 sizeof(*key->fs_inputs) * (key->num_fs_inputs -
547 shader_state->num_tf_outputs));
548 }
549 key->num_fs_inputs = shader_state->num_tf_outputs;
550
551 struct v3d_compiled_shader *cs =
552 v3d_get_compiled_shader(v3d, &key->base);
553 if (cs != v3d->prog.cs) {
554 v3d->prog.cs = cs;
555 v3d->dirty |= VC5_DIRTY_COMPILED_CS;
556 }
557 }
558
559 void
560 v3d_update_compiled_shaders(struct v3d_context *v3d, uint8_t prim_mode)
561 {
562 v3d_update_compiled_fs(v3d, prim_mode);
563 v3d_update_compiled_vs(v3d, prim_mode);
564 }
565
566 static uint32_t
567 fs_cache_hash(const void *key)
568 {
569 return _mesa_hash_data(key, sizeof(struct v3d_fs_key));
570 }
571
572 static uint32_t
573 vs_cache_hash(const void *key)
574 {
575 return _mesa_hash_data(key, sizeof(struct v3d_vs_key));
576 }
577
578 static bool
579 fs_cache_compare(const void *key1, const void *key2)
580 {
581 return memcmp(key1, key2, sizeof(struct v3d_fs_key)) == 0;
582 }
583
584 static bool
585 vs_cache_compare(const void *key1, const void *key2)
586 {
587 return memcmp(key1, key2, sizeof(struct v3d_vs_key)) == 0;
588 }
589
590 static void
591 delete_from_cache_if_matches(struct hash_table *ht,
592 struct v3d_compiled_shader **last_compile,
593 struct hash_entry *entry,
594 struct v3d_uncompiled_shader *so)
595 {
596 const struct v3d_key *key = entry->key;
597
598 if (key->shader_state == so) {
599 struct v3d_compiled_shader *shader = entry->data;
600 _mesa_hash_table_remove(ht, entry);
601 v3d_bo_unreference(&shader->bo);
602
603 if (shader == *last_compile)
604 *last_compile = NULL;
605
606 ralloc_free(shader);
607 }
608 }
609
610 static void
611 v3d_shader_state_delete(struct pipe_context *pctx, void *hwcso)
612 {
613 struct v3d_context *v3d = v3d_context(pctx);
614 struct v3d_uncompiled_shader *so = hwcso;
615
616 struct hash_entry *entry;
617 hash_table_foreach(v3d->fs_cache, entry) {
618 delete_from_cache_if_matches(v3d->fs_cache, &v3d->prog.fs,
619 entry, so);
620 }
621 hash_table_foreach(v3d->vs_cache, entry) {
622 delete_from_cache_if_matches(v3d->vs_cache, &v3d->prog.vs,
623 entry, so);
624 }
625
626 ralloc_free(so->base.ir.nir);
627 free(so);
628 }
629
630 static void
631 v3d_fp_state_bind(struct pipe_context *pctx, void *hwcso)
632 {
633 struct v3d_context *v3d = v3d_context(pctx);
634 v3d->prog.bind_fs = hwcso;
635 v3d->dirty |= VC5_DIRTY_UNCOMPILED_FS;
636 }
637
638 static void
639 v3d_vp_state_bind(struct pipe_context *pctx, void *hwcso)
640 {
641 struct v3d_context *v3d = v3d_context(pctx);
642 v3d->prog.bind_vs = hwcso;
643 v3d->dirty |= VC5_DIRTY_UNCOMPILED_VS;
644 }
645
646 void
647 v3d_program_init(struct pipe_context *pctx)
648 {
649 struct v3d_context *v3d = v3d_context(pctx);
650
651 pctx->create_vs_state = v3d_shader_state_create;
652 pctx->delete_vs_state = v3d_shader_state_delete;
653
654 pctx->create_fs_state = v3d_shader_state_create;
655 pctx->delete_fs_state = v3d_shader_state_delete;
656
657 pctx->bind_fs_state = v3d_fp_state_bind;
658 pctx->bind_vs_state = v3d_vp_state_bind;
659
660 v3d->fs_cache = _mesa_hash_table_create(pctx, fs_cache_hash,
661 fs_cache_compare);
662 v3d->vs_cache = _mesa_hash_table_create(pctx, vs_cache_hash,
663 vs_cache_compare);
664 }
665
666 void
667 v3d_program_fini(struct pipe_context *pctx)
668 {
669 struct v3d_context *v3d = v3d_context(pctx);
670
671 struct hash_entry *entry;
672 hash_table_foreach(v3d->fs_cache, entry) {
673 struct v3d_compiled_shader *shader = entry->data;
674 v3d_bo_unreference(&shader->bo);
675 ralloc_free(shader);
676 _mesa_hash_table_remove(v3d->fs_cache, entry);
677 }
678
679 hash_table_foreach(v3d->vs_cache, entry) {
680 struct v3d_compiled_shader *shader = entry->data;
681 v3d_bo_unreference(&shader->bo);
682 ralloc_free(shader);
683 _mesa_hash_table_remove(v3d->vs_cache, entry);
684 }
685 }