v3d: Stop tracking num_inputs for VPM loads.
[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 nir_foreach_variable(var, &s->outputs) {
201 if (var->data.location == FRAG_RESULT_COLOR) {
202 key.cbufs |= 1 << 0;
203 } else if (var->data.location >= FRAG_RESULT_DATA0) {
204 key.cbufs |= 1 << (var->data.location -
205 FRAG_RESULT_DATA0);
206 }
207 }
208
209 v3d_setup_shared_precompile_key(so, &key.base);
210 v3d_get_compiled_shader(v3d, &key.base);
211 } else {
212 struct v3d_vs_key key = {
213 .base.shader_state = so,
214 };
215
216 v3d_setup_shared_precompile_key(so, &key.base);
217
218 /* Compile VS: All outputs */
219 nir_foreach_variable(var, &s->outputs) {
220 unsigned array_len = MAX2(glsl_get_length(var->type), 1);
221 assert(array_len == 1);
222 (void)array_len;
223
224 int slot = var->data.location;
225 for (int i = 0; i < glsl_get_components(var->type); i++) {
226 int swiz = var->data.location_frac + i;
227 key.fs_inputs[key.num_fs_inputs++] =
228 v3d_slot_from_slot_and_component(slot,
229 swiz);
230 }
231 }
232
233 v3d_get_compiled_shader(v3d, &key.base);
234
235 /* Compile VS bin shader: only position (XXX: include TF) */
236 key.is_coord = true;
237 key.num_fs_inputs = 0;
238 for (int i = 0; i < 4; i++) {
239 key.fs_inputs[key.num_fs_inputs++] =
240 v3d_slot_from_slot_and_component(VARYING_SLOT_POS,
241 i);
242 }
243 v3d_get_compiled_shader(v3d, &key.base);
244 }
245 }
246
247 static void *
248 v3d_shader_state_create(struct pipe_context *pctx,
249 const struct pipe_shader_state *cso)
250 {
251 struct v3d_context *v3d = v3d_context(pctx);
252 struct v3d_uncompiled_shader *so = CALLOC_STRUCT(v3d_uncompiled_shader);
253 if (!so)
254 return NULL;
255
256 so->program_id = v3d->next_uncompiled_program_id++;
257
258 nir_shader *s;
259
260 if (cso->type == PIPE_SHADER_IR_NIR) {
261 /* The backend takes ownership of the NIR shader on state
262 * creation.
263 */
264 s = cso->ir.nir;
265
266 NIR_PASS_V(s, nir_lower_io, nir_var_uniform,
267 uniforms_type_size,
268 (nir_lower_io_options)0);
269 } else {
270 assert(cso->type == PIPE_SHADER_IR_TGSI);
271
272 if (V3D_DEBUG & V3D_DEBUG_TGSI) {
273 fprintf(stderr, "prog %d TGSI:\n",
274 so->program_id);
275 tgsi_dump(cso->tokens, 0);
276 fprintf(stderr, "\n");
277 }
278 s = tgsi_to_nir(cso->tokens, &v3d_nir_options);
279 }
280
281 nir_variable_mode lower_mode = nir_var_all & ~nir_var_uniform;
282 if (s->info.stage == MESA_SHADER_VERTEX)
283 lower_mode &= ~(nir_var_shader_in | nir_var_shader_out);
284 NIR_PASS_V(s, nir_lower_io, lower_mode,
285 type_size,
286 (nir_lower_io_options)0);
287
288 NIR_PASS_V(s, nir_opt_global_to_local);
289 NIR_PASS_V(s, nir_lower_regs_to_ssa);
290 NIR_PASS_V(s, nir_normalize_cubemap_coords);
291
292 NIR_PASS_V(s, nir_lower_load_const_to_scalar);
293
294 v3d_optimize_nir(s);
295
296 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
297
298 /* Garbage collect dead instructions */
299 nir_sweep(s);
300
301 so->base.type = PIPE_SHADER_IR_NIR;
302 so->base.ir.nir = s;
303
304 v3d_set_transform_feedback_outputs(so, &cso->stream_output);
305
306 if (V3D_DEBUG & (V3D_DEBUG_NIR |
307 v3d_debug_flag_for_shader_stage(s->info.stage))) {
308 fprintf(stderr, "%s prog %d NIR:\n",
309 gl_shader_stage_name(s->info.stage),
310 so->program_id);
311 nir_print_shader(s, stderr);
312 fprintf(stderr, "\n");
313 }
314
315 if (V3D_DEBUG & V3D_DEBUG_PRECOMPILE)
316 v3d_shader_precompile(v3d, so);
317
318 return so;
319 }
320
321 static void
322 v3d_shader_debug_output(const char *message, void *data)
323 {
324 struct v3d_context *v3d = data;
325
326 pipe_debug_message(&v3d->debug, SHADER_INFO, "%s", message);
327 }
328
329 static struct v3d_compiled_shader *
330 v3d_get_compiled_shader(struct v3d_context *v3d, struct v3d_key *key)
331 {
332 struct v3d_uncompiled_shader *shader_state = key->shader_state;
333 nir_shader *s = shader_state->base.ir.nir;
334
335 struct hash_table *ht;
336 uint32_t key_size;
337 if (s->info.stage == MESA_SHADER_FRAGMENT) {
338 ht = v3d->fs_cache;
339 key_size = sizeof(struct v3d_fs_key);
340 } else {
341 ht = v3d->vs_cache;
342 key_size = sizeof(struct v3d_vs_key);
343 }
344
345 struct hash_entry *entry = _mesa_hash_table_search(ht, key);
346 if (entry)
347 return entry->data;
348
349 struct v3d_compiled_shader *shader =
350 rzalloc(NULL, struct v3d_compiled_shader);
351
352 int program_id = shader_state->program_id;
353 int variant_id =
354 p_atomic_inc_return(&shader_state->compiled_variant_count);
355 uint64_t *qpu_insts;
356 uint32_t shader_size;
357
358 qpu_insts = v3d_compile(v3d->screen->compiler, key,
359 &shader->prog_data.base, s,
360 v3d_shader_debug_output,
361 v3d,
362 program_id, variant_id, &shader_size);
363 ralloc_steal(shader, shader->prog_data.base);
364
365 v3d_set_shader_uniform_dirty_flags(shader);
366
367 if (shader_size) {
368 u_upload_data(v3d->state_uploader, 0, shader_size, 8,
369 qpu_insts, &shader->offset, &shader->resource);
370 }
371
372 free(qpu_insts);
373
374 struct v3d_key *dup_key;
375 dup_key = ralloc_size(shader, key_size);
376 memcpy(dup_key, key, key_size);
377 _mesa_hash_table_insert(ht, dup_key, shader);
378
379 if (shader->prog_data.base->spill_size >
380 v3d->prog.spill_size_per_thread) {
381 /* Max 4 QPUs per slice, 3 slices per core. We only do single
382 * core so far. This overallocates memory on smaller cores.
383 */
384 int total_spill_size =
385 4 * 3 * shader->prog_data.base->spill_size;
386
387 v3d_bo_unreference(&v3d->prog.spill_bo);
388 v3d->prog.spill_bo = v3d_bo_alloc(v3d->screen,
389 total_spill_size, "spill");
390 v3d->prog.spill_size_per_thread =
391 shader->prog_data.base->spill_size;
392 }
393
394 return shader;
395 }
396
397 static void
398 v3d_free_compiled_shader(struct v3d_compiled_shader *shader)
399 {
400 pipe_resource_reference(&shader->resource, NULL);
401 ralloc_free(shader);
402 }
403
404 static void
405 v3d_setup_shared_key(struct v3d_context *v3d, struct v3d_key *key,
406 struct v3d_texture_stateobj *texstate)
407 {
408 const struct v3d_device_info *devinfo = &v3d->screen->devinfo;
409
410 for (int i = 0; i < texstate->num_textures; i++) {
411 struct pipe_sampler_view *sampler = texstate->textures[i];
412 struct v3d_sampler_view *v3d_sampler = v3d_sampler_view(sampler);
413 struct pipe_sampler_state *sampler_state =
414 texstate->samplers[i];
415
416 if (!sampler)
417 continue;
418
419 key->tex[i].return_size =
420 v3d_get_tex_return_size(devinfo,
421 sampler->format,
422 sampler_state->compare_mode);
423
424 /* For 16-bit, we set up the sampler to always return 2
425 * channels (meaning no recompiles for most statechanges),
426 * while for 32 we actually scale the returns with channels.
427 */
428 if (key->tex[i].return_size == 16) {
429 key->tex[i].return_channels = 2;
430 } else if (devinfo->ver > 40) {
431 key->tex[i].return_channels = 4;
432 } else {
433 key->tex[i].return_channels =
434 v3d_get_tex_return_channels(devinfo,
435 sampler->format);
436 }
437
438 if (key->tex[i].return_size == 32 && devinfo->ver < 40) {
439 memcpy(key->tex[i].swizzle,
440 v3d_sampler->swizzle,
441 sizeof(v3d_sampler->swizzle));
442 } else {
443 /* For 16-bit returns, we let the sampler state handle
444 * the swizzle.
445 */
446 key->tex[i].swizzle[0] = PIPE_SWIZZLE_X;
447 key->tex[i].swizzle[1] = PIPE_SWIZZLE_Y;
448 key->tex[i].swizzle[2] = PIPE_SWIZZLE_Z;
449 key->tex[i].swizzle[3] = PIPE_SWIZZLE_W;
450 }
451
452 if (sampler) {
453 key->tex[i].clamp_s =
454 sampler_state->wrap_s == PIPE_TEX_WRAP_CLAMP;
455 key->tex[i].clamp_t =
456 sampler_state->wrap_t == PIPE_TEX_WRAP_CLAMP;
457 key->tex[i].clamp_r =
458 sampler_state->wrap_r == PIPE_TEX_WRAP_CLAMP;
459 }
460 }
461
462 key->ucp_enables = v3d->rasterizer->base.clip_plane_enable;
463 }
464
465 static void
466 v3d_setup_shared_precompile_key(struct v3d_uncompiled_shader *uncompiled,
467 struct v3d_key *key)
468 {
469 nir_shader *s = uncompiled->base.ir.nir;
470
471 for (int i = 0; i < s->info.num_textures; i++) {
472 key->tex[i].return_size = 16;
473 key->tex[i].return_channels = 2;
474
475 key->tex[i].swizzle[0] = PIPE_SWIZZLE_X;
476 key->tex[i].swizzle[1] = PIPE_SWIZZLE_Y;
477 key->tex[i].swizzle[2] = PIPE_SWIZZLE_Z;
478 key->tex[i].swizzle[3] = PIPE_SWIZZLE_W;
479 }
480 }
481
482 static void
483 v3d_update_compiled_fs(struct v3d_context *v3d, uint8_t prim_mode)
484 {
485 struct v3d_job *job = v3d->job;
486 struct v3d_fs_key local_key;
487 struct v3d_fs_key *key = &local_key;
488 nir_shader *s = v3d->prog.bind_fs->base.ir.nir;
489
490 if (!(v3d->dirty & (VC5_DIRTY_PRIM_MODE |
491 VC5_DIRTY_BLEND |
492 VC5_DIRTY_FRAMEBUFFER |
493 VC5_DIRTY_ZSA |
494 VC5_DIRTY_RASTERIZER |
495 VC5_DIRTY_SAMPLE_STATE |
496 VC5_DIRTY_FRAGTEX |
497 VC5_DIRTY_UNCOMPILED_FS))) {
498 return;
499 }
500
501 memset(key, 0, sizeof(*key));
502 v3d_setup_shared_key(v3d, &key->base, &v3d->tex[PIPE_SHADER_FRAGMENT]);
503 key->base.shader_state = v3d->prog.bind_fs;
504 key->is_points = (prim_mode == PIPE_PRIM_POINTS);
505 key->is_lines = (prim_mode >= PIPE_PRIM_LINES &&
506 prim_mode <= PIPE_PRIM_LINE_STRIP);
507 key->clamp_color = v3d->rasterizer->base.clamp_fragment_color;
508 if (v3d->blend->base.logicop_enable) {
509 key->logicop_func = v3d->blend->base.logicop_func;
510 } else {
511 key->logicop_func = PIPE_LOGICOP_COPY;
512 }
513 if (job->msaa) {
514 key->msaa = v3d->rasterizer->base.multisample;
515 key->sample_coverage = (v3d->rasterizer->base.multisample &&
516 v3d->sample_mask != (1 << V3D_MAX_SAMPLES) - 1);
517 key->sample_alpha_to_coverage = v3d->blend->base.alpha_to_coverage;
518 key->sample_alpha_to_one = v3d->blend->base.alpha_to_one;
519 }
520
521 key->depth_enabled = (v3d->zsa->base.depth.enabled ||
522 v3d->zsa->base.stencil[0].enabled);
523 if (v3d->zsa->base.alpha.enabled) {
524 key->alpha_test = true;
525 key->alpha_test_func = v3d->zsa->base.alpha.func;
526 }
527
528 key->swap_color_rb = v3d->swap_color_rb;
529
530 for (int i = 0; i < v3d->framebuffer.nr_cbufs; i++) {
531 struct pipe_surface *cbuf = v3d->framebuffer.cbufs[i];
532 if (!cbuf)
533 continue;
534
535 /* gl_FragColor's propagation to however many bound color
536 * buffers there are means that the shader compile needs to
537 * know what buffers are present.
538 */
539 key->cbufs |= 1 << i;
540
541 const struct util_format_description *desc =
542 util_format_description(cbuf->format);
543
544 if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT &&
545 desc->channel[0].size == 32) {
546 key->f32_color_rb |= 1 << i;
547 }
548
549 if (s->info.fs.untyped_color_outputs) {
550 if (util_format_is_pure_uint(cbuf->format))
551 key->uint_color_rb |= 1 << i;
552 else if (util_format_is_pure_sint(cbuf->format))
553 key->int_color_rb |= 1 << i;
554 }
555 }
556
557 if (key->is_points) {
558 key->point_sprite_mask =
559 v3d->rasterizer->base.sprite_coord_enable;
560 key->point_coord_upper_left =
561 (v3d->rasterizer->base.sprite_coord_mode ==
562 PIPE_SPRITE_COORD_UPPER_LEFT);
563 }
564
565 key->light_twoside = v3d->rasterizer->base.light_twoside;
566 key->shade_model_flat = v3d->rasterizer->base.flatshade;
567
568 struct v3d_compiled_shader *old_fs = v3d->prog.fs;
569 v3d->prog.fs = v3d_get_compiled_shader(v3d, &key->base);
570 if (v3d->prog.fs == old_fs)
571 return;
572
573 v3d->dirty |= VC5_DIRTY_COMPILED_FS;
574
575 if (old_fs) {
576 if (v3d->prog.fs->prog_data.fs->flat_shade_flags !=
577 old_fs->prog_data.fs->flat_shade_flags) {
578 v3d->dirty |= VC5_DIRTY_FLAT_SHADE_FLAGS;
579 }
580
581 if (v3d->prog.fs->prog_data.fs->noperspective_flags !=
582 old_fs->prog_data.fs->noperspective_flags) {
583 v3d->dirty |= VC5_DIRTY_NOPERSPECTIVE_FLAGS;
584 }
585
586 if (v3d->prog.fs->prog_data.fs->centroid_flags !=
587 old_fs->prog_data.fs->centroid_flags) {
588 v3d->dirty |= VC5_DIRTY_CENTROID_FLAGS;
589 }
590 }
591
592 if (old_fs && memcmp(v3d->prog.fs->prog_data.fs->input_slots,
593 old_fs->prog_data.fs->input_slots,
594 sizeof(v3d->prog.fs->prog_data.fs->input_slots))) {
595 v3d->dirty |= VC5_DIRTY_FS_INPUTS;
596 }
597 }
598
599 static void
600 v3d_update_compiled_vs(struct v3d_context *v3d, uint8_t prim_mode)
601 {
602 struct v3d_vs_key local_key;
603 struct v3d_vs_key *key = &local_key;
604
605 if (!(v3d->dirty & (VC5_DIRTY_PRIM_MODE |
606 VC5_DIRTY_RASTERIZER |
607 VC5_DIRTY_VERTTEX |
608 VC5_DIRTY_VTXSTATE |
609 VC5_DIRTY_UNCOMPILED_VS |
610 VC5_DIRTY_FS_INPUTS))) {
611 return;
612 }
613
614 memset(key, 0, sizeof(*key));
615 v3d_setup_shared_key(v3d, &key->base, &v3d->tex[PIPE_SHADER_VERTEX]);
616 key->base.shader_state = v3d->prog.bind_vs;
617 key->num_fs_inputs = v3d->prog.fs->prog_data.fs->num_inputs;
618 STATIC_ASSERT(sizeof(key->fs_inputs) ==
619 sizeof(v3d->prog.fs->prog_data.fs->input_slots));
620 memcpy(key->fs_inputs, v3d->prog.fs->prog_data.fs->input_slots,
621 sizeof(key->fs_inputs));
622 key->clamp_color = v3d->rasterizer->base.clamp_vertex_color;
623
624 key->per_vertex_point_size =
625 (prim_mode == PIPE_PRIM_POINTS &&
626 v3d->rasterizer->base.point_size_per_vertex);
627
628 struct v3d_compiled_shader *vs =
629 v3d_get_compiled_shader(v3d, &key->base);
630 if (vs != v3d->prog.vs) {
631 v3d->prog.vs = vs;
632 v3d->dirty |= VC5_DIRTY_COMPILED_VS;
633 }
634
635 key->is_coord = true;
636 /* Coord shaders only output varyings used by transform feedback. */
637 struct v3d_uncompiled_shader *shader_state = key->base.shader_state;
638 memcpy(key->fs_inputs, shader_state->tf_outputs,
639 sizeof(*key->fs_inputs) * shader_state->num_tf_outputs);
640 if (shader_state->num_tf_outputs < key->num_fs_inputs) {
641 memset(&key->fs_inputs[shader_state->num_tf_outputs],
642 0,
643 sizeof(*key->fs_inputs) * (key->num_fs_inputs -
644 shader_state->num_tf_outputs));
645 }
646 key->num_fs_inputs = shader_state->num_tf_outputs;
647
648 struct v3d_compiled_shader *cs =
649 v3d_get_compiled_shader(v3d, &key->base);
650 if (cs != v3d->prog.cs) {
651 v3d->prog.cs = cs;
652 v3d->dirty |= VC5_DIRTY_COMPILED_CS;
653 }
654 }
655
656 void
657 v3d_update_compiled_shaders(struct v3d_context *v3d, uint8_t prim_mode)
658 {
659 v3d_update_compiled_fs(v3d, prim_mode);
660 v3d_update_compiled_vs(v3d, prim_mode);
661 }
662
663 static uint32_t
664 fs_cache_hash(const void *key)
665 {
666 return _mesa_hash_data(key, sizeof(struct v3d_fs_key));
667 }
668
669 static uint32_t
670 vs_cache_hash(const void *key)
671 {
672 return _mesa_hash_data(key, sizeof(struct v3d_vs_key));
673 }
674
675 static bool
676 fs_cache_compare(const void *key1, const void *key2)
677 {
678 return memcmp(key1, key2, sizeof(struct v3d_fs_key)) == 0;
679 }
680
681 static bool
682 vs_cache_compare(const void *key1, const void *key2)
683 {
684 return memcmp(key1, key2, sizeof(struct v3d_vs_key)) == 0;
685 }
686
687 static void
688 delete_from_cache_if_matches(struct hash_table *ht,
689 struct v3d_compiled_shader **last_compile,
690 struct hash_entry *entry,
691 struct v3d_uncompiled_shader *so)
692 {
693 const struct v3d_key *key = entry->key;
694
695 if (key->shader_state == so) {
696 struct v3d_compiled_shader *shader = entry->data;
697 _mesa_hash_table_remove(ht, entry);
698
699 if (shader == *last_compile)
700 *last_compile = NULL;
701
702 v3d_free_compiled_shader(shader);
703 }
704 }
705
706 static void
707 v3d_shader_state_delete(struct pipe_context *pctx, void *hwcso)
708 {
709 struct v3d_context *v3d = v3d_context(pctx);
710 struct v3d_uncompiled_shader *so = hwcso;
711
712 hash_table_foreach(v3d->fs_cache, entry) {
713 delete_from_cache_if_matches(v3d->fs_cache, &v3d->prog.fs,
714 entry, so);
715 }
716 hash_table_foreach(v3d->vs_cache, entry) {
717 delete_from_cache_if_matches(v3d->vs_cache, &v3d->prog.vs,
718 entry, so);
719 }
720
721 ralloc_free(so->base.ir.nir);
722 free(so);
723 }
724
725 static void
726 v3d_fp_state_bind(struct pipe_context *pctx, void *hwcso)
727 {
728 struct v3d_context *v3d = v3d_context(pctx);
729 v3d->prog.bind_fs = hwcso;
730 v3d->dirty |= VC5_DIRTY_UNCOMPILED_FS;
731 }
732
733 static void
734 v3d_vp_state_bind(struct pipe_context *pctx, void *hwcso)
735 {
736 struct v3d_context *v3d = v3d_context(pctx);
737 v3d->prog.bind_vs = hwcso;
738 v3d->dirty |= VC5_DIRTY_UNCOMPILED_VS;
739 }
740
741 void
742 v3d_program_init(struct pipe_context *pctx)
743 {
744 struct v3d_context *v3d = v3d_context(pctx);
745
746 pctx->create_vs_state = v3d_shader_state_create;
747 pctx->delete_vs_state = v3d_shader_state_delete;
748
749 pctx->create_fs_state = v3d_shader_state_create;
750 pctx->delete_fs_state = v3d_shader_state_delete;
751
752 pctx->bind_fs_state = v3d_fp_state_bind;
753 pctx->bind_vs_state = v3d_vp_state_bind;
754
755 v3d->fs_cache = _mesa_hash_table_create(pctx, fs_cache_hash,
756 fs_cache_compare);
757 v3d->vs_cache = _mesa_hash_table_create(pctx, vs_cache_hash,
758 vs_cache_compare);
759 }
760
761 void
762 v3d_program_fini(struct pipe_context *pctx)
763 {
764 struct v3d_context *v3d = v3d_context(pctx);
765
766 hash_table_foreach(v3d->fs_cache, entry) {
767 struct v3d_compiled_shader *shader = entry->data;
768 v3d_free_compiled_shader(shader);
769 _mesa_hash_table_remove(v3d->fs_cache, entry);
770 }
771
772 hash_table_foreach(v3d->vs_cache, entry) {
773 struct v3d_compiled_shader *shader = entry->data;
774 v3d_free_compiled_shader(shader);
775 _mesa_hash_table_remove(v3d->vs_cache, entry);
776 }
777
778 v3d_bo_unreference(&v3d->prog.spill_bo);
779 }