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