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