v3d: do not automatically flush current job for SSBOs and shader images
[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
40 static struct v3d_compiled_shader *
41 v3d_get_compiled_shader(struct v3d_context *v3d,
42 struct v3d_key *key, size_t key_size);
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, bool bindless)
174 {
175 return glsl_count_attribute_slots(type, false);
176 }
177
178 /**
179 * Precompiles a shader variant at shader state creation time if
180 * V3D_DEBUG=precompile is set. Used for shader-db
181 * (https://gitlab.freedesktop.org/mesa/shader-db)
182 */
183 static void
184 v3d_shader_precompile(struct v3d_context *v3d,
185 struct v3d_uncompiled_shader *so)
186 {
187 nir_shader *s = so->base.ir.nir;
188
189 if (s->info.stage == MESA_SHADER_FRAGMENT) {
190 struct v3d_fs_key key = {
191 .base.shader_state = so,
192 };
193
194 nir_foreach_variable(var, &s->outputs) {
195 if (var->data.location == FRAG_RESULT_COLOR) {
196 key.cbufs |= 1 << 0;
197 } else if (var->data.location >= FRAG_RESULT_DATA0) {
198 key.cbufs |= 1 << (var->data.location -
199 FRAG_RESULT_DATA0);
200 }
201 }
202
203 key.logicop_func = PIPE_LOGICOP_COPY;
204
205 v3d_setup_shared_precompile_key(so, &key.base);
206 v3d_get_compiled_shader(v3d, &key.base, sizeof(key));
207 } else {
208 struct v3d_vs_key key = {
209 .base.shader_state = so,
210 };
211
212 v3d_setup_shared_precompile_key(so, &key.base);
213
214 /* Compile VS: All outputs */
215 nir_foreach_variable(var, &s->outputs) {
216 unsigned array_len = MAX2(glsl_get_length(var->type), 1);
217 assert(array_len == 1);
218 (void)array_len;
219
220 int slot = var->data.location;
221 for (int i = 0; i < glsl_get_components(var->type); i++) {
222 int swiz = var->data.location_frac + i;
223 key.fs_inputs[key.num_fs_inputs++] =
224 v3d_slot_from_slot_and_component(slot,
225 swiz);
226 }
227 }
228
229 v3d_get_compiled_shader(v3d, &key.base, sizeof(key));
230
231 /* Compile VS bin shader: only position (XXX: include TF) */
232 key.is_coord = true;
233 key.num_fs_inputs = 0;
234 for (int i = 0; i < 4; i++) {
235 key.fs_inputs[key.num_fs_inputs++] =
236 v3d_slot_from_slot_and_component(VARYING_SLOT_POS,
237 i);
238 }
239 v3d_get_compiled_shader(v3d, &key.base, sizeof(key));
240 }
241 }
242
243 static void *
244 v3d_uncompiled_shader_create(struct pipe_context *pctx,
245 enum pipe_shader_ir type, void *ir)
246 {
247 struct v3d_context *v3d = v3d_context(pctx);
248 struct v3d_uncompiled_shader *so = CALLOC_STRUCT(v3d_uncompiled_shader);
249 if (!so)
250 return NULL;
251
252 so->program_id = v3d->next_uncompiled_program_id++;
253
254 nir_shader *s;
255
256 if (type == PIPE_SHADER_IR_NIR) {
257 /* The backend takes ownership of the NIR shader on state
258 * creation.
259 */
260 s = ir;
261 } else {
262 assert(type == PIPE_SHADER_IR_TGSI);
263
264 if (V3D_DEBUG & V3D_DEBUG_TGSI) {
265 fprintf(stderr, "prog %d TGSI:\n",
266 so->program_id);
267 tgsi_dump(ir, 0);
268 fprintf(stderr, "\n");
269 }
270 s = tgsi_to_nir(ir, pctx->screen);
271 }
272
273 nir_variable_mode lower_mode = nir_var_all & ~nir_var_uniform;
274 if (s->info.stage == MESA_SHADER_VERTEX)
275 lower_mode &= ~(nir_var_shader_in | nir_var_shader_out);
276 NIR_PASS_V(s, nir_lower_io, lower_mode,
277 type_size,
278 (nir_lower_io_options)0);
279
280 NIR_PASS_V(s, nir_lower_regs_to_ssa);
281 NIR_PASS_V(s, nir_normalize_cubemap_coords);
282
283 NIR_PASS_V(s, nir_lower_load_const_to_scalar);
284
285 v3d_optimize_nir(s);
286
287 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
288
289 /* Garbage collect dead instructions */
290 nir_sweep(s);
291
292 so->base.type = PIPE_SHADER_IR_NIR;
293 so->base.ir.nir = s;
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 void *
319 v3d_shader_state_create(struct pipe_context *pctx,
320 const struct pipe_shader_state *cso)
321 {
322 struct v3d_uncompiled_shader *so =
323 v3d_uncompiled_shader_create(pctx,
324 cso->type,
325 (cso->type == PIPE_SHADER_IR_TGSI ?
326 (void *)cso->tokens :
327 cso->ir.nir));
328
329 v3d_set_transform_feedback_outputs(so, &cso->stream_output);
330
331 return so;
332 }
333
334 struct v3d_compiled_shader *
335 v3d_get_compiled_shader(struct v3d_context *v3d,
336 struct v3d_key *key,
337 size_t key_size)
338 {
339 struct v3d_uncompiled_shader *shader_state = key->shader_state;
340 nir_shader *s = shader_state->base.ir.nir;
341
342 struct hash_table *ht = v3d->prog.cache[s->info.stage];
343 struct hash_entry *entry = _mesa_hash_table_search(ht, key);
344 if (entry)
345 return entry->data;
346
347 struct v3d_compiled_shader *shader =
348 rzalloc(NULL, struct v3d_compiled_shader);
349
350 int program_id = shader_state->program_id;
351 int variant_id =
352 p_atomic_inc_return(&shader_state->compiled_variant_count);
353 uint64_t *qpu_insts;
354 uint32_t shader_size;
355
356 qpu_insts = v3d_compile(v3d->screen->compiler, key,
357 &shader->prog_data.base, s,
358 v3d_shader_debug_output,
359 v3d,
360 program_id, variant_id, &shader_size);
361 ralloc_steal(shader, shader->prog_data.base);
362
363 v3d_set_shader_uniform_dirty_flags(shader);
364
365 if (shader_size) {
366 u_upload_data(v3d->state_uploader, 0, shader_size, 8,
367 qpu_insts, &shader->offset, &shader->resource);
368 }
369
370 free(qpu_insts);
371
372 if (ht) {
373 struct v3d_key *dup_key;
374 dup_key = ralloc_size(shader, key_size);
375 memcpy(dup_key, key, key_size);
376 _mesa_hash_table_insert(ht, dup_key, shader);
377 }
378
379 if (shader->prog_data.base->spill_size >
380 v3d->prog.spill_size_per_thread) {
381 /* The TIDX register we use for choosing the area to access
382 * for scratch space is: (core << 6) | (qpu << 2) | thread.
383 * Even at minimum threadcount in a particular shader, that
384 * means we still multiply by qpus by 4.
385 */
386 int total_spill_size = (v3d->screen->devinfo.qpu_count * 4 *
387 shader->prog_data.base->spill_size);
388
389 v3d_bo_unreference(&v3d->prog.spill_bo);
390 v3d->prog.spill_bo = v3d_bo_alloc(v3d->screen,
391 total_spill_size, "spill");
392 v3d->prog.spill_size_per_thread =
393 shader->prog_data.base->spill_size;
394 }
395
396 return shader;
397 }
398
399 static void
400 v3d_free_compiled_shader(struct v3d_compiled_shader *shader)
401 {
402 pipe_resource_reference(&shader->resource, NULL);
403 ralloc_free(shader);
404 }
405
406 static void
407 v3d_setup_shared_key(struct v3d_context *v3d, struct v3d_key *key,
408 struct v3d_texture_stateobj *texstate)
409 {
410 const struct v3d_device_info *devinfo = &v3d->screen->devinfo;
411
412 for (int i = 0; i < texstate->num_textures; i++) {
413 struct pipe_sampler_view *sampler = texstate->textures[i];
414 struct v3d_sampler_view *v3d_sampler = v3d_sampler_view(sampler);
415 struct pipe_sampler_state *sampler_state =
416 texstate->samplers[i];
417
418 if (!sampler)
419 continue;
420
421 key->tex[i].return_size =
422 v3d_get_tex_return_size(devinfo,
423 sampler->format,
424 sampler_state->compare_mode);
425
426 /* For 16-bit, we set up the sampler to always return 2
427 * channels (meaning no recompiles for most statechanges),
428 * while for 32 we actually scale the returns with channels.
429 */
430 if (key->tex[i].return_size == 16) {
431 key->tex[i].return_channels = 2;
432 } else if (devinfo->ver > 40) {
433 key->tex[i].return_channels = 4;
434 } else {
435 key->tex[i].return_channels =
436 v3d_get_tex_return_channels(devinfo,
437 sampler->format);
438 }
439
440 if (key->tex[i].return_size == 32 && devinfo->ver < 40) {
441 memcpy(key->tex[i].swizzle,
442 v3d_sampler->swizzle,
443 sizeof(v3d_sampler->swizzle));
444 } else {
445 /* For 16-bit returns, we let the sampler state handle
446 * the swizzle.
447 */
448 key->tex[i].swizzle[0] = PIPE_SWIZZLE_X;
449 key->tex[i].swizzle[1] = PIPE_SWIZZLE_Y;
450 key->tex[i].swizzle[2] = PIPE_SWIZZLE_Z;
451 key->tex[i].swizzle[3] = PIPE_SWIZZLE_W;
452 }
453
454 if (sampler) {
455 key->tex[i].clamp_s =
456 sampler_state->wrap_s == PIPE_TEX_WRAP_CLAMP;
457 key->tex[i].clamp_t =
458 sampler_state->wrap_t == PIPE_TEX_WRAP_CLAMP;
459 key->tex[i].clamp_r =
460 sampler_state->wrap_r == PIPE_TEX_WRAP_CLAMP;
461 }
462 }
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->base.ucp_enables = v3d->rasterizer->base.clip_plane_enable;
505 key->is_points = (prim_mode == PIPE_PRIM_POINTS);
506 key->is_lines = (prim_mode >= PIPE_PRIM_LINES &&
507 prim_mode <= PIPE_PRIM_LINE_STRIP);
508 key->clamp_color = v3d->rasterizer->base.clamp_fragment_color;
509 if (v3d->blend->base.logicop_enable) {
510 key->logicop_func = v3d->blend->base.logicop_func;
511 } else {
512 key->logicop_func = PIPE_LOGICOP_COPY;
513 }
514 if (job->msaa) {
515 key->msaa = v3d->rasterizer->base.multisample;
516 key->sample_coverage = (v3d->rasterizer->base.multisample &&
517 v3d->sample_mask != (1 << V3D_MAX_SAMPLES) - 1);
518 key->sample_alpha_to_coverage = v3d->blend->base.alpha_to_coverage;
519 key->sample_alpha_to_one = v3d->blend->base.alpha_to_one;
520 }
521
522 key->depth_enabled = (v3d->zsa->base.depth.enabled ||
523 v3d->zsa->base.stencil[0].enabled);
524 if (v3d->zsa->base.alpha.enabled) {
525 key->alpha_test = true;
526 key->alpha_test_func = v3d->zsa->base.alpha.func;
527 }
528
529 key->swap_color_rb = v3d->swap_color_rb;
530
531 for (int i = 0; i < v3d->framebuffer.nr_cbufs; i++) {
532 struct pipe_surface *cbuf = v3d->framebuffer.cbufs[i];
533 if (!cbuf)
534 continue;
535
536 /* gl_FragColor's propagation to however many bound color
537 * buffers there are means that the shader compile needs to
538 * know what buffers are present.
539 */
540 key->cbufs |= 1 << i;
541
542 /* If logic operations are enabled then we might emit color
543 * reads and we need to know the color buffer format and
544 * swizzle for that.
545 */
546 if (key->logicop_func != PIPE_LOGICOP_COPY) {
547 key->color_fmt[i].format = cbuf->format;
548 key->color_fmt[i].swizzle =
549 v3d_get_format_swizzle(&v3d->screen->devinfo,
550 cbuf->format);
551 }
552
553 const struct util_format_description *desc =
554 util_format_description(cbuf->format);
555
556 if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT &&
557 desc->channel[0].size == 32) {
558 key->f32_color_rb |= 1 << i;
559 }
560
561 if (s->info.fs.untyped_color_outputs) {
562 if (util_format_is_pure_uint(cbuf->format))
563 key->uint_color_rb |= 1 << i;
564 else if (util_format_is_pure_sint(cbuf->format))
565 key->int_color_rb |= 1 << i;
566 }
567 }
568
569 if (key->is_points) {
570 key->point_sprite_mask =
571 v3d->rasterizer->base.sprite_coord_enable;
572 key->point_coord_upper_left =
573 (v3d->rasterizer->base.sprite_coord_mode ==
574 PIPE_SPRITE_COORD_UPPER_LEFT);
575 }
576
577 key->light_twoside = v3d->rasterizer->base.light_twoside;
578 key->shade_model_flat = v3d->rasterizer->base.flatshade;
579
580 struct v3d_compiled_shader *old_fs = v3d->prog.fs;
581 v3d->prog.fs = v3d_get_compiled_shader(v3d, &key->base, sizeof(*key));
582 if (v3d->prog.fs == old_fs)
583 return;
584
585 v3d->dirty |= VC5_DIRTY_COMPILED_FS;
586
587 if (old_fs) {
588 if (v3d->prog.fs->prog_data.fs->flat_shade_flags !=
589 old_fs->prog_data.fs->flat_shade_flags) {
590 v3d->dirty |= VC5_DIRTY_FLAT_SHADE_FLAGS;
591 }
592
593 if (v3d->prog.fs->prog_data.fs->noperspective_flags !=
594 old_fs->prog_data.fs->noperspective_flags) {
595 v3d->dirty |= VC5_DIRTY_NOPERSPECTIVE_FLAGS;
596 }
597
598 if (v3d->prog.fs->prog_data.fs->centroid_flags !=
599 old_fs->prog_data.fs->centroid_flags) {
600 v3d->dirty |= VC5_DIRTY_CENTROID_FLAGS;
601 }
602 }
603
604 if (old_fs && memcmp(v3d->prog.fs->prog_data.fs->input_slots,
605 old_fs->prog_data.fs->input_slots,
606 sizeof(v3d->prog.fs->prog_data.fs->input_slots))) {
607 v3d->dirty |= VC5_DIRTY_FS_INPUTS;
608 }
609 }
610
611 static void
612 v3d_update_compiled_vs(struct v3d_context *v3d, uint8_t prim_mode)
613 {
614 struct v3d_vs_key local_key;
615 struct v3d_vs_key *key = &local_key;
616
617 if (!(v3d->dirty & (VC5_DIRTY_PRIM_MODE |
618 VC5_DIRTY_RASTERIZER |
619 VC5_DIRTY_VERTTEX |
620 VC5_DIRTY_VTXSTATE |
621 VC5_DIRTY_UNCOMPILED_VS |
622 VC5_DIRTY_FS_INPUTS))) {
623 return;
624 }
625
626 memset(key, 0, sizeof(*key));
627 v3d_setup_shared_key(v3d, &key->base, &v3d->tex[PIPE_SHADER_VERTEX]);
628 key->base.shader_state = v3d->prog.bind_vs;
629 key->base.ucp_enables = v3d->rasterizer->base.clip_plane_enable;
630 key->num_fs_inputs = v3d->prog.fs->prog_data.fs->num_inputs;
631 STATIC_ASSERT(sizeof(key->fs_inputs) ==
632 sizeof(v3d->prog.fs->prog_data.fs->input_slots));
633 memcpy(key->fs_inputs, v3d->prog.fs->prog_data.fs->input_slots,
634 sizeof(key->fs_inputs));
635 key->clamp_color = v3d->rasterizer->base.clamp_vertex_color;
636
637 key->per_vertex_point_size =
638 (prim_mode == PIPE_PRIM_POINTS &&
639 v3d->rasterizer->base.point_size_per_vertex);
640
641 struct v3d_compiled_shader *vs =
642 v3d_get_compiled_shader(v3d, &key->base, sizeof(*key));
643 if (vs != v3d->prog.vs) {
644 v3d->prog.vs = vs;
645 v3d->dirty |= VC5_DIRTY_COMPILED_VS;
646 }
647
648 key->is_coord = true;
649 /* Coord shaders only output varyings used by transform feedback. */
650 struct v3d_uncompiled_shader *shader_state = key->base.shader_state;
651 memcpy(key->fs_inputs, shader_state->tf_outputs,
652 sizeof(*key->fs_inputs) * shader_state->num_tf_outputs);
653 if (shader_state->num_tf_outputs < key->num_fs_inputs) {
654 memset(&key->fs_inputs[shader_state->num_tf_outputs],
655 0,
656 sizeof(*key->fs_inputs) * (key->num_fs_inputs -
657 shader_state->num_tf_outputs));
658 }
659 key->num_fs_inputs = shader_state->num_tf_outputs;
660
661 struct v3d_compiled_shader *cs =
662 v3d_get_compiled_shader(v3d, &key->base, sizeof(*key));
663 if (cs != v3d->prog.cs) {
664 v3d->prog.cs = cs;
665 v3d->dirty |= VC5_DIRTY_COMPILED_CS;
666 }
667 }
668
669 void
670 v3d_update_compiled_shaders(struct v3d_context *v3d, uint8_t prim_mode)
671 {
672 v3d_update_compiled_fs(v3d, prim_mode);
673 v3d_update_compiled_vs(v3d, prim_mode);
674 }
675
676 void
677 v3d_update_compiled_cs(struct v3d_context *v3d)
678 {
679 struct v3d_key local_key;
680 struct v3d_key *key = &local_key;
681
682 if (!(v3d->dirty & (~0 | /* XXX */
683 VC5_DIRTY_VERTTEX |
684 VC5_DIRTY_UNCOMPILED_FS))) {
685 return;
686 }
687
688 memset(key, 0, sizeof(*key));
689 v3d_setup_shared_key(v3d, key, &v3d->tex[PIPE_SHADER_COMPUTE]);
690 key->shader_state = v3d->prog.bind_compute;
691
692 struct v3d_compiled_shader *cs =
693 v3d_get_compiled_shader(v3d, key, sizeof(*key));
694 if (cs != v3d->prog.compute) {
695 v3d->prog.compute = cs;
696 v3d->dirty |= VC5_DIRTY_COMPILED_CS; /* XXX */
697 }
698 }
699
700 static uint32_t
701 fs_cache_hash(const void *key)
702 {
703 return _mesa_hash_data(key, sizeof(struct v3d_fs_key));
704 }
705
706 static uint32_t
707 vs_cache_hash(const void *key)
708 {
709 return _mesa_hash_data(key, sizeof(struct v3d_vs_key));
710 }
711
712 static uint32_t
713 cs_cache_hash(const void *key)
714 {
715 return _mesa_hash_data(key, sizeof(struct v3d_key));
716 }
717
718 static bool
719 fs_cache_compare(const void *key1, const void *key2)
720 {
721 return memcmp(key1, key2, sizeof(struct v3d_fs_key)) == 0;
722 }
723
724 static bool
725 vs_cache_compare(const void *key1, const void *key2)
726 {
727 return memcmp(key1, key2, sizeof(struct v3d_vs_key)) == 0;
728 }
729
730 static bool
731 cs_cache_compare(const void *key1, const void *key2)
732 {
733 return memcmp(key1, key2, sizeof(struct v3d_key)) == 0;
734 }
735
736 static void
737 v3d_shader_state_delete(struct pipe_context *pctx, void *hwcso)
738 {
739 struct v3d_context *v3d = v3d_context(pctx);
740 struct v3d_uncompiled_shader *so = hwcso;
741 nir_shader *s = so->base.ir.nir;
742
743 hash_table_foreach(v3d->prog.cache[s->info.stage], entry) {
744 const struct v3d_key *key = entry->key;
745 struct v3d_compiled_shader *shader = entry->data;
746
747 if (key->shader_state != so)
748 continue;
749
750 if (v3d->prog.fs == shader)
751 v3d->prog.fs = NULL;
752 if (v3d->prog.vs == shader)
753 v3d->prog.vs = NULL;
754 if (v3d->prog.cs == shader)
755 v3d->prog.cs = NULL;
756 if (v3d->prog.compute == shader)
757 v3d->prog.compute = NULL;
758
759 _mesa_hash_table_remove(v3d->prog.cache[s->info.stage], entry);
760 v3d_free_compiled_shader(shader);
761 }
762
763 ralloc_free(so->base.ir.nir);
764 free(so);
765 }
766
767 static void
768 v3d_fp_state_bind(struct pipe_context *pctx, void *hwcso)
769 {
770 struct v3d_context *v3d = v3d_context(pctx);
771 v3d->prog.bind_fs = hwcso;
772 v3d->dirty |= VC5_DIRTY_UNCOMPILED_FS;
773 }
774
775 static void
776 v3d_vp_state_bind(struct pipe_context *pctx, void *hwcso)
777 {
778 struct v3d_context *v3d = v3d_context(pctx);
779 v3d->prog.bind_vs = hwcso;
780 v3d->dirty |= VC5_DIRTY_UNCOMPILED_VS;
781 }
782
783 static void
784 v3d_compute_state_bind(struct pipe_context *pctx, void *state)
785 {
786 struct v3d_context *v3d = v3d_context(pctx);
787
788 v3d->prog.bind_compute = state;
789 }
790
791 static void *
792 v3d_create_compute_state(struct pipe_context *pctx,
793 const struct pipe_compute_state *cso)
794 {
795 return v3d_uncompiled_shader_create(pctx, cso->ir_type,
796 (void *)cso->prog);
797 }
798
799 void
800 v3d_program_init(struct pipe_context *pctx)
801 {
802 struct v3d_context *v3d = v3d_context(pctx);
803
804 pctx->create_vs_state = v3d_shader_state_create;
805 pctx->delete_vs_state = v3d_shader_state_delete;
806
807 pctx->create_fs_state = v3d_shader_state_create;
808 pctx->delete_fs_state = v3d_shader_state_delete;
809
810 pctx->bind_fs_state = v3d_fp_state_bind;
811 pctx->bind_vs_state = v3d_vp_state_bind;
812
813 if (v3d->screen->has_csd) {
814 pctx->create_compute_state = v3d_create_compute_state;
815 pctx->delete_compute_state = v3d_shader_state_delete;
816 pctx->bind_compute_state = v3d_compute_state_bind;
817 }
818
819 v3d->prog.cache[MESA_SHADER_VERTEX] =
820 _mesa_hash_table_create(pctx, vs_cache_hash, vs_cache_compare);
821 v3d->prog.cache[MESA_SHADER_FRAGMENT] =
822 _mesa_hash_table_create(pctx, fs_cache_hash, fs_cache_compare);
823 v3d->prog.cache[MESA_SHADER_COMPUTE] =
824 _mesa_hash_table_create(pctx, cs_cache_hash, cs_cache_compare);
825 }
826
827 void
828 v3d_program_fini(struct pipe_context *pctx)
829 {
830 struct v3d_context *v3d = v3d_context(pctx);
831
832 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
833 struct hash_table *cache = v3d->prog.cache[i];
834 if (!cache)
835 continue;
836
837 hash_table_foreach(cache, entry) {
838 struct v3d_compiled_shader *shader = entry->data;
839 v3d_free_compiled_shader(shader);
840 _mesa_hash_table_remove(cache, entry);
841 }
842 }
843
844 v3d_bo_unreference(&v3d->prog.spill_bo);
845 }