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/format/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
v3d_get_slot_for_driver_location(nir_shader * s,uint32_t driver_location)48 v3d_get_slot_for_driver_location(nir_shader *s, uint32_t driver_location)
49 {
50         nir_foreach_shader_out_variable(var, s) {
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
v3d_set_transform_feedback_outputs(struct v3d_uncompiled_shader * so,const struct pipe_stream_output_info * stream_output)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
type_size(const struct glsl_type * type,bool bindless)173 type_size(const struct glsl_type *type, bool bindless)
174 {
175         return glsl_count_attribute_slots(type, false);
176 }
177 
178 static void
precompile_all_outputs(nir_shader * s,struct v3d_varying_slot * outputs,uint8_t * num_outputs)179 precompile_all_outputs(nir_shader *s,
180                        struct v3d_varying_slot *outputs,
181                        uint8_t *num_outputs)
182 {
183         nir_foreach_shader_out_variable(var, s) {
184                 const int array_len = MAX2(glsl_get_length(var->type), 1);
185                 for (int j = 0; j < array_len; j++) {
186                         const int slot = var->data.location + j;
187                         const int num_components =
188                                 glsl_get_components(var->type);
189                         for (int i = 0; i < num_components; i++) {
190                                 const int swiz = var->data.location_frac + i;
191                                 outputs[(*num_outputs)++] =
192                                         v3d_slot_from_slot_and_component(slot,
193                                                                          swiz);
194                         }
195                 }
196         }
197 }
198 
199 /**
200  * Precompiles a shader variant at shader state creation time if
201  * V3D_DEBUG=precompile is set.  Used for shader-db
202  * (https://gitlab.freedesktop.org/mesa/shader-db)
203  */
204 static void
v3d_shader_precompile(struct v3d_context * v3d,struct v3d_uncompiled_shader * so)205 v3d_shader_precompile(struct v3d_context *v3d,
206                       struct v3d_uncompiled_shader *so)
207 {
208         nir_shader *s = so->base.ir.nir;
209 
210         if (s->info.stage == MESA_SHADER_FRAGMENT) {
211                 struct v3d_fs_key key = {
212                         .base.shader_state = so,
213                 };
214 
215                 nir_foreach_shader_out_variable(var, s) {
216                         if (var->data.location == FRAG_RESULT_COLOR) {
217                                 key.cbufs |= 1 << 0;
218                         } else if (var->data.location >= FRAG_RESULT_DATA0) {
219                                 key.cbufs |= 1 << (var->data.location -
220                                                    FRAG_RESULT_DATA0);
221                         }
222                 }
223 
224                 key.logicop_func = PIPE_LOGICOP_COPY;
225 
226                 v3d_setup_shared_precompile_key(so, &key.base);
227                 v3d_get_compiled_shader(v3d, &key.base, sizeof(key));
228         } else if (s->info.stage == MESA_SHADER_GEOMETRY) {
229                 struct v3d_gs_key key = {
230                         .base.shader_state = so,
231                         .base.is_last_geometry_stage = true,
232                 };
233 
234                 v3d_setup_shared_precompile_key(so, &key.base);
235 
236                 precompile_all_outputs(s,
237                                        key.used_outputs,
238                                        &key.num_used_outputs);
239 
240                 v3d_get_compiled_shader(v3d, &key.base, sizeof(key));
241 
242                 /* Compile GS bin shader: only position (XXX: include TF) */
243                 key.is_coord = true;
244                 key.num_used_outputs = 0;
245                 for (int i = 0; i < 4; i++) {
246                         key.used_outputs[key.num_used_outputs++] =
247                                 v3d_slot_from_slot_and_component(VARYING_SLOT_POS,
248                                                                  i);
249                 }
250                 v3d_get_compiled_shader(v3d, &key.base, sizeof(key));
251         } else {
252                 assert(s->info.stage == MESA_SHADER_VERTEX);
253                 struct v3d_vs_key key = {
254                         .base.shader_state = so,
255                         /* Emit fixed function outputs */
256                         .base.is_last_geometry_stage = true,
257                 };
258 
259                 v3d_setup_shared_precompile_key(so, &key.base);
260 
261                 precompile_all_outputs(s,
262                                        key.used_outputs,
263                                        &key.num_used_outputs);
264 
265                 v3d_get_compiled_shader(v3d, &key.base, sizeof(key));
266 
267                 /* Compile VS bin shader: only position (XXX: include TF) */
268                 key.is_coord = true;
269                 key.num_used_outputs = 0;
270                 for (int i = 0; i < 4; i++) {
271                         key.used_outputs[key.num_used_outputs++] =
272                                 v3d_slot_from_slot_and_component(VARYING_SLOT_POS,
273                                                                  i);
274                 }
275                 v3d_get_compiled_shader(v3d, &key.base, sizeof(key));
276         }
277 }
278 
279 static void *
v3d_uncompiled_shader_create(struct pipe_context * pctx,enum pipe_shader_ir type,void * ir)280 v3d_uncompiled_shader_create(struct pipe_context *pctx,
281                              enum pipe_shader_ir type, void *ir)
282 {
283         struct v3d_context *v3d = v3d_context(pctx);
284         struct v3d_uncompiled_shader *so = CALLOC_STRUCT(v3d_uncompiled_shader);
285         if (!so)
286                 return NULL;
287 
288         so->program_id = v3d->next_uncompiled_program_id++;
289 
290         nir_shader *s;
291 
292         if (type == PIPE_SHADER_IR_NIR) {
293                 /* The backend takes ownership of the NIR shader on state
294                  * creation.
295                  */
296                 s = ir;
297         } else {
298                 assert(type == PIPE_SHADER_IR_TGSI);
299 
300                 if (V3D_DEBUG & V3D_DEBUG_TGSI) {
301                         fprintf(stderr, "prog %d TGSI:\n",
302                                 so->program_id);
303                         tgsi_dump(ir, 0);
304                         fprintf(stderr, "\n");
305                 }
306                 s = tgsi_to_nir(ir, pctx->screen, false);
307         }
308 
309         if (s->info.stage != MESA_SHADER_VERTEX &&
310             s->info.stage != MESA_SHADER_GEOMETRY) {
311                 NIR_PASS_V(s, nir_lower_io,
312                            nir_var_shader_in | nir_var_shader_out,
313                            type_size, (nir_lower_io_options)0);
314         }
315 
316         NIR_PASS_V(s, nir_lower_regs_to_ssa);
317         NIR_PASS_V(s, nir_normalize_cubemap_coords);
318 
319         NIR_PASS_V(s, nir_lower_load_const_to_scalar);
320 
321         v3d_optimize_nir(s);
322 
323         NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp, NULL);
324 
325         /* Garbage collect dead instructions */
326         nir_sweep(s);
327 
328         so->base.type = PIPE_SHADER_IR_NIR;
329         so->base.ir.nir = s;
330 
331         if (V3D_DEBUG & (V3D_DEBUG_NIR |
332                          v3d_debug_flag_for_shader_stage(s->info.stage))) {
333                 fprintf(stderr, "%s prog %d NIR:\n",
334                         gl_shader_stage_name(s->info.stage),
335                         so->program_id);
336                 nir_print_shader(s, stderr);
337                 fprintf(stderr, "\n");
338         }
339 
340         if (V3D_DEBUG & V3D_DEBUG_PRECOMPILE)
341                 v3d_shader_precompile(v3d, so);
342 
343         return so;
344 }
345 
346 static void
v3d_shader_debug_output(const char * message,void * data)347 v3d_shader_debug_output(const char *message, void *data)
348 {
349         struct v3d_context *v3d = data;
350 
351         pipe_debug_message(&v3d->debug, SHADER_INFO, "%s", message);
352 }
353 
354 static void *
v3d_shader_state_create(struct pipe_context * pctx,const struct pipe_shader_state * cso)355 v3d_shader_state_create(struct pipe_context *pctx,
356                         const struct pipe_shader_state *cso)
357 {
358         struct v3d_uncompiled_shader *so =
359                 v3d_uncompiled_shader_create(pctx,
360                                              cso->type,
361                                              (cso->type == PIPE_SHADER_IR_TGSI ?
362                                               (void *)cso->tokens :
363                                               cso->ir.nir));
364 
365         v3d_set_transform_feedback_outputs(so, &cso->stream_output);
366 
367         return so;
368 }
369 
370 struct v3d_compiled_shader *
v3d_get_compiled_shader(struct v3d_context * v3d,struct v3d_key * key,size_t key_size)371 v3d_get_compiled_shader(struct v3d_context *v3d,
372                         struct v3d_key *key,
373                         size_t key_size)
374 {
375         struct v3d_uncompiled_shader *shader_state = key->shader_state;
376         nir_shader *s = shader_state->base.ir.nir;
377 
378         struct hash_table *ht = v3d->prog.cache[s->info.stage];
379         struct hash_entry *entry = _mesa_hash_table_search(ht, key);
380         if (entry)
381                 return entry->data;
382 
383         struct v3d_compiled_shader *shader =
384                 rzalloc(NULL, struct v3d_compiled_shader);
385 
386         int program_id = shader_state->program_id;
387         int variant_id =
388                 p_atomic_inc_return(&shader_state->compiled_variant_count);
389         uint64_t *qpu_insts;
390         uint32_t shader_size;
391 
392         qpu_insts = v3d_compile(v3d->screen->compiler, key,
393                                 &shader->prog_data.base, s,
394                                 v3d_shader_debug_output,
395                                 v3d,
396                                 program_id, variant_id, &shader_size);
397         ralloc_steal(shader, shader->prog_data.base);
398 
399         v3d_set_shader_uniform_dirty_flags(shader);
400 
401         if (shader_size) {
402                 u_upload_data(v3d->state_uploader, 0, shader_size, 8,
403                               qpu_insts, &shader->offset, &shader->resource);
404         }
405 
406         free(qpu_insts);
407 
408         if (ht) {
409                 struct v3d_key *dup_key;
410                 dup_key = ralloc_size(shader, key_size);
411                 memcpy(dup_key, key, key_size);
412                 _mesa_hash_table_insert(ht, dup_key, shader);
413         }
414 
415         if (shader->prog_data.base->spill_size >
416             v3d->prog.spill_size_per_thread) {
417                 /* The TIDX register we use for choosing the area to access
418                  * for scratch space is: (core << 6) | (qpu << 2) | thread.
419                  * Even at minimum threadcount in a particular shader, that
420                  * means we still multiply by qpus by 4.
421                  */
422                 int total_spill_size = (v3d->screen->devinfo.qpu_count * 4 *
423                                         shader->prog_data.base->spill_size);
424 
425                 v3d_bo_unreference(&v3d->prog.spill_bo);
426                 v3d->prog.spill_bo = v3d_bo_alloc(v3d->screen,
427                                                   total_spill_size, "spill");
428                 v3d->prog.spill_size_per_thread =
429                         shader->prog_data.base->spill_size;
430         }
431 
432         return shader;
433 }
434 
435 static void
v3d_free_compiled_shader(struct v3d_compiled_shader * shader)436 v3d_free_compiled_shader(struct v3d_compiled_shader *shader)
437 {
438         pipe_resource_reference(&shader->resource, NULL);
439         ralloc_free(shader);
440 }
441 
442 static void
v3d_setup_shared_key(struct v3d_context * v3d,struct v3d_key * key,struct v3d_texture_stateobj * texstate)443 v3d_setup_shared_key(struct v3d_context *v3d, struct v3d_key *key,
444                      struct v3d_texture_stateobj *texstate)
445 {
446         const struct v3d_device_info *devinfo = &v3d->screen->devinfo;
447 
448         key->num_tex_used = texstate->num_textures;
449         for (int i = 0; i < texstate->num_textures; i++) {
450                 struct pipe_sampler_view *sampler = texstate->textures[i];
451                 struct v3d_sampler_view *v3d_sampler = v3d_sampler_view(sampler);
452                 struct pipe_sampler_state *sampler_state =
453                         texstate->samplers[i];
454 
455                 if (!sampler)
456                         continue;
457 
458                 key->tex[i].return_size =
459                         v3d_get_tex_return_size(devinfo,
460                                                 sampler->format,
461                                                 sampler_state->compare_mode);
462 
463                 /* For 16-bit, we set up the sampler to always return 2
464                  * channels (meaning no recompiles for most statechanges),
465                  * while for 32 we actually scale the returns with channels.
466                  */
467                 if (key->tex[i].return_size == 16) {
468                         key->tex[i].return_channels = 2;
469                 } else if (devinfo->ver > 40) {
470                         key->tex[i].return_channels = 4;
471                 } else {
472                         key->tex[i].return_channels =
473                                 v3d_get_tex_return_channels(devinfo,
474                                                             sampler->format);
475                 }
476 
477                 if (key->tex[i].return_size == 32 && devinfo->ver < 40) {
478                         memcpy(key->tex[i].swizzle,
479                                v3d_sampler->swizzle,
480                                sizeof(v3d_sampler->swizzle));
481                 } else {
482                         /* For 16-bit returns, we let the sampler state handle
483                          * the swizzle.
484                          */
485                         key->tex[i].swizzle[0] = PIPE_SWIZZLE_X;
486                         key->tex[i].swizzle[1] = PIPE_SWIZZLE_Y;
487                         key->tex[i].swizzle[2] = PIPE_SWIZZLE_Z;
488                         key->tex[i].swizzle[3] = PIPE_SWIZZLE_W;
489                 }
490 
491                 if (sampler) {
492                         key->tex[i].clamp_s =
493                                 sampler_state->wrap_s == PIPE_TEX_WRAP_CLAMP;
494                         key->tex[i].clamp_t =
495                                 sampler_state->wrap_t == PIPE_TEX_WRAP_CLAMP;
496                         key->tex[i].clamp_r =
497                                 sampler_state->wrap_r == PIPE_TEX_WRAP_CLAMP;
498                 }
499         }
500 }
501 
502 static void
v3d_setup_shared_precompile_key(struct v3d_uncompiled_shader * uncompiled,struct v3d_key * key)503 v3d_setup_shared_precompile_key(struct v3d_uncompiled_shader *uncompiled,
504                                 struct v3d_key *key)
505 {
506         nir_shader *s = uncompiled->base.ir.nir;
507 
508         key->num_tex_used = s->info.num_textures;
509         for (int i = 0; i < s->info.num_textures; i++) {
510                 key->tex[i].return_size = 16;
511                 key->tex[i].return_channels = 2;
512 
513                 key->tex[i].swizzle[0] = PIPE_SWIZZLE_X;
514                 key->tex[i].swizzle[1] = PIPE_SWIZZLE_Y;
515                 key->tex[i].swizzle[2] = PIPE_SWIZZLE_Z;
516                 key->tex[i].swizzle[3] = PIPE_SWIZZLE_W;
517         }
518 }
519 
520 static void
v3d_update_compiled_fs(struct v3d_context * v3d,uint8_t prim_mode)521 v3d_update_compiled_fs(struct v3d_context *v3d, uint8_t prim_mode)
522 {
523         struct v3d_job *job = v3d->job;
524         struct v3d_fs_key local_key;
525         struct v3d_fs_key *key = &local_key;
526         nir_shader *s = v3d->prog.bind_fs->base.ir.nir;
527 
528         if (!(v3d->dirty & (VC5_DIRTY_PRIM_MODE |
529                             VC5_DIRTY_BLEND |
530                             VC5_DIRTY_FRAMEBUFFER |
531                             VC5_DIRTY_ZSA |
532                             VC5_DIRTY_RASTERIZER |
533                             VC5_DIRTY_SAMPLE_STATE |
534                             VC5_DIRTY_FRAGTEX |
535                             VC5_DIRTY_UNCOMPILED_FS))) {
536                 return;
537         }
538 
539         memset(key, 0, sizeof(*key));
540         v3d_setup_shared_key(v3d, &key->base, &v3d->tex[PIPE_SHADER_FRAGMENT]);
541         key->base.shader_state = v3d->prog.bind_fs;
542         key->base.ucp_enables = v3d->rasterizer->base.clip_plane_enable;
543         key->is_points = (prim_mode == PIPE_PRIM_POINTS);
544         key->is_lines = (prim_mode >= PIPE_PRIM_LINES &&
545                          prim_mode <= PIPE_PRIM_LINE_STRIP);
546         key->line_smoothing = (key->is_lines &&
547                                v3d_line_smoothing_enabled(v3d));
548         key->clamp_color = v3d->rasterizer->base.clamp_fragment_color;
549         if (v3d->blend->base.logicop_enable) {
550                 key->logicop_func = v3d->blend->base.logicop_func;
551         } else {
552                 key->logicop_func = PIPE_LOGICOP_COPY;
553         }
554         if (job->msaa) {
555                 key->msaa = v3d->rasterizer->base.multisample;
556                 key->sample_coverage = (v3d->rasterizer->base.multisample &&
557                                         v3d->sample_mask != (1 << V3D_MAX_SAMPLES) - 1);
558                 key->sample_alpha_to_coverage = v3d->blend->base.alpha_to_coverage;
559                 key->sample_alpha_to_one = v3d->blend->base.alpha_to_one;
560         }
561 
562         if (v3d->zsa->base.alpha.enabled) {
563                 key->alpha_test = true;
564                 key->alpha_test_func = v3d->zsa->base.alpha.func;
565         }
566 
567         key->swap_color_rb = v3d->swap_color_rb;
568 
569         for (int i = 0; i < v3d->framebuffer.nr_cbufs; i++) {
570                 struct pipe_surface *cbuf = v3d->framebuffer.cbufs[i];
571                 if (!cbuf)
572                         continue;
573 
574                 /* gl_FragColor's propagation to however many bound color
575                  * buffers there are means that the shader compile needs to
576                  * know what buffers are present.
577                  */
578                 key->cbufs |= 1 << i;
579 
580                 /* If logic operations are enabled then we might emit color
581                  * reads and we need to know the color buffer format and
582                  * swizzle for that.
583                  */
584                 if (key->logicop_func != PIPE_LOGICOP_COPY) {
585                         key->color_fmt[i].format = cbuf->format;
586                         key->color_fmt[i].swizzle =
587                                 v3d_get_format_swizzle(&v3d->screen->devinfo,
588                                                        cbuf->format);
589                 }
590 
591                 const struct util_format_description *desc =
592                         util_format_description(cbuf->format);
593 
594                 if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT &&
595                     desc->channel[0].size == 32) {
596                         key->f32_color_rb |= 1 << i;
597                 }
598 
599                 if (s->info.fs.untyped_color_outputs) {
600                         if (util_format_is_pure_uint(cbuf->format))
601                                 key->uint_color_rb |= 1 << i;
602                         else if (util_format_is_pure_sint(cbuf->format))
603                                 key->int_color_rb |= 1 << i;
604                 }
605         }
606 
607         if (key->is_points) {
608                 key->point_sprite_mask =
609                         v3d->rasterizer->base.sprite_coord_enable;
610                 key->point_coord_upper_left =
611                         (v3d->rasterizer->base.sprite_coord_mode ==
612                          PIPE_SPRITE_COORD_UPPER_LEFT);
613         }
614 
615         key->light_twoside = v3d->rasterizer->base.light_twoside;
616         key->shade_model_flat = v3d->rasterizer->base.flatshade;
617 
618         struct v3d_compiled_shader *old_fs = v3d->prog.fs;
619         v3d->prog.fs = v3d_get_compiled_shader(v3d, &key->base, sizeof(*key));
620         if (v3d->prog.fs == old_fs)
621                 return;
622 
623         v3d->dirty |= VC5_DIRTY_COMPILED_FS;
624 
625         if (old_fs) {
626                 if (v3d->prog.fs->prog_data.fs->flat_shade_flags !=
627                     old_fs->prog_data.fs->flat_shade_flags) {
628                         v3d->dirty |= VC5_DIRTY_FLAT_SHADE_FLAGS;
629                 }
630 
631                 if (v3d->prog.fs->prog_data.fs->noperspective_flags !=
632                     old_fs->prog_data.fs->noperspective_flags) {
633                         v3d->dirty |= VC5_DIRTY_NOPERSPECTIVE_FLAGS;
634                 }
635 
636                 if (v3d->prog.fs->prog_data.fs->centroid_flags !=
637                     old_fs->prog_data.fs->centroid_flags) {
638                         v3d->dirty |= VC5_DIRTY_CENTROID_FLAGS;
639                 }
640         }
641 
642         if (old_fs && memcmp(v3d->prog.fs->prog_data.fs->input_slots,
643                              old_fs->prog_data.fs->input_slots,
644                              sizeof(v3d->prog.fs->prog_data.fs->input_slots))) {
645                 v3d->dirty |= VC5_DIRTY_FS_INPUTS;
646         }
647 }
648 
649 static void
v3d_update_compiled_gs(struct v3d_context * v3d,uint8_t prim_mode)650 v3d_update_compiled_gs(struct v3d_context *v3d, uint8_t prim_mode)
651 {
652         struct v3d_gs_key local_key;
653         struct v3d_gs_key *key = &local_key;
654 
655         if (!(v3d->dirty & (VC5_DIRTY_GEOMTEX |
656                             VC5_DIRTY_RASTERIZER |
657                             VC5_DIRTY_UNCOMPILED_GS |
658                             VC5_DIRTY_PRIM_MODE |
659                             VC5_DIRTY_FS_INPUTS))) {
660                 return;
661         }
662 
663         if (!v3d->prog.bind_gs) {
664                 v3d->prog.gs = NULL;
665                 v3d->prog.gs_bin = NULL;
666                 return;
667         }
668 
669         memset(key, 0, sizeof(*key));
670         v3d_setup_shared_key(v3d, &key->base, &v3d->tex[PIPE_SHADER_GEOMETRY]);
671         key->base.shader_state = v3d->prog.bind_gs;
672         key->base.ucp_enables = v3d->rasterizer->base.clip_plane_enable;
673         key->base.is_last_geometry_stage = true;
674         key->num_used_outputs = v3d->prog.fs->prog_data.fs->num_inputs;
675         STATIC_ASSERT(sizeof(key->used_outputs) ==
676                       sizeof(v3d->prog.fs->prog_data.fs->input_slots));
677         memcpy(key->used_outputs, v3d->prog.fs->prog_data.fs->input_slots,
678                sizeof(key->used_outputs));
679 
680         key->per_vertex_point_size =
681                 (prim_mode == PIPE_PRIM_POINTS &&
682                  v3d->rasterizer->base.point_size_per_vertex);
683 
684         struct v3d_compiled_shader *gs =
685                 v3d_get_compiled_shader(v3d, &key->base, sizeof(*key));
686         if (gs != v3d->prog.gs) {
687                 v3d->prog.gs = gs;
688                 v3d->dirty |= VC5_DIRTY_COMPILED_GS;
689         }
690 
691         key->is_coord = true;
692 
693         /* The last bin-mode shader in the geometry pipeline only outputs
694          * varyings used by transform feedback.
695          */
696         struct v3d_uncompiled_shader *shader_state = key->base.shader_state;
697         memcpy(key->used_outputs, shader_state->tf_outputs,
698                sizeof(*key->used_outputs) * shader_state->num_tf_outputs);
699         if (shader_state->num_tf_outputs < key->num_used_outputs) {
700                 uint32_t size = sizeof(*key->used_outputs) *
701                                 (key->num_used_outputs -
702                                  shader_state->num_tf_outputs);
703                 memset(&key->used_outputs[shader_state->num_tf_outputs],
704                        0, size);
705         }
706         key->num_used_outputs = shader_state->num_tf_outputs;
707 
708         struct v3d_compiled_shader *old_gs = v3d->prog.gs;
709         struct v3d_compiled_shader *gs_bin =
710                 v3d_get_compiled_shader(v3d, &key->base, sizeof(*key));
711         if (gs_bin != old_gs) {
712                 v3d->prog.gs_bin = gs_bin;
713                 v3d->dirty |= VC5_DIRTY_COMPILED_GS_BIN;
714         }
715 
716         if (old_gs && memcmp(v3d->prog.gs->prog_data.gs->input_slots,
717                              old_gs->prog_data.gs->input_slots,
718                              sizeof(v3d->prog.gs->prog_data.gs->input_slots))) {
719                 v3d->dirty |= VC5_DIRTY_GS_INPUTS;
720         }
721 }
722 
723 static void
v3d_update_compiled_vs(struct v3d_context * v3d,uint8_t prim_mode)724 v3d_update_compiled_vs(struct v3d_context *v3d, uint8_t prim_mode)
725 {
726         struct v3d_vs_key local_key;
727         struct v3d_vs_key *key = &local_key;
728 
729         if (!(v3d->dirty & (VC5_DIRTY_VERTTEX |
730                             VC5_DIRTY_VTXSTATE |
731                             VC5_DIRTY_UNCOMPILED_VS |
732                             (v3d->prog.bind_gs ? 0 : VC5_DIRTY_RASTERIZER) |
733                             (v3d->prog.bind_gs ? 0 : VC5_DIRTY_PRIM_MODE) |
734                             (v3d->prog.bind_gs ? VC5_DIRTY_GS_INPUTS :
735                                                  VC5_DIRTY_FS_INPUTS)))) {
736                 return;
737         }
738 
739         memset(key, 0, sizeof(*key));
740         v3d_setup_shared_key(v3d, &key->base, &v3d->tex[PIPE_SHADER_VERTEX]);
741         key->base.shader_state = v3d->prog.bind_vs;
742         key->base.ucp_enables = v3d->rasterizer->base.clip_plane_enable;
743         key->base.is_last_geometry_stage = !v3d->prog.bind_gs;
744 
745         if (!v3d->prog.bind_gs) {
746             key->num_used_outputs = v3d->prog.fs->prog_data.fs->num_inputs;
747             STATIC_ASSERT(sizeof(key->used_outputs) ==
748                           sizeof(v3d->prog.fs->prog_data.fs->input_slots));
749             memcpy(key->used_outputs, v3d->prog.fs->prog_data.fs->input_slots,
750                    sizeof(key->used_outputs));
751         } else {
752             key->num_used_outputs = v3d->prog.gs->prog_data.gs->num_inputs;
753             STATIC_ASSERT(sizeof(key->used_outputs) ==
754                           sizeof(v3d->prog.gs->prog_data.gs->input_slots));
755             memcpy(key->used_outputs, v3d->prog.gs->prog_data.gs->input_slots,
756                    sizeof(key->used_outputs));
757         }
758 
759         key->clamp_color = v3d->rasterizer->base.clamp_vertex_color;
760 
761         key->per_vertex_point_size =
762                 (prim_mode == PIPE_PRIM_POINTS &&
763                  v3d->rasterizer->base.point_size_per_vertex);
764 
765         nir_shader *s = v3d->prog.bind_vs->base.ir.nir;
766         uint64_t inputs_read = s->info.inputs_read;
767         assert(util_bitcount(inputs_read) <= v3d->vtx->num_elements);
768 
769         while (inputs_read) {
770                 int location = u_bit_scan64(&inputs_read);
771                 nir_variable *var =
772                         nir_find_variable_with_location(s, nir_var_shader_in, location);
773                 assert (var != NULL);
774                 int driver_location = var->data.driver_location;
775                 switch (v3d->vtx->pipe[driver_location].src_format) {
776                 case PIPE_FORMAT_B8G8R8A8_UNORM:
777                 case PIPE_FORMAT_B10G10R10A2_UNORM:
778                 case PIPE_FORMAT_B10G10R10A2_SNORM:
779                 case PIPE_FORMAT_B10G10R10A2_USCALED:
780                 case PIPE_FORMAT_B10G10R10A2_SSCALED:
781                         key->va_swap_rb_mask |= 1 << location;
782                         break;
783                 default:
784                         break;
785                 }
786         }
787 
788         struct v3d_compiled_shader *vs =
789                 v3d_get_compiled_shader(v3d, &key->base, sizeof(*key));
790         if (vs != v3d->prog.vs) {
791                 v3d->prog.vs = vs;
792                 v3d->dirty |= VC5_DIRTY_COMPILED_VS;
793         }
794 
795         key->is_coord = true;
796 
797         /* Coord shaders only output varyings used by transform feedback,
798          * unless they are linked to other shaders in the geometry side
799          * of the pipeline, since in that case any of the output varyings
800          * could be required in later geometry stages to compute
801          * gl_Position or TF outputs.
802          */
803         if (!v3d->prog.bind_gs) {
804                 struct v3d_uncompiled_shader *shader_state =
805                         key->base.shader_state;
806                 memcpy(key->used_outputs, shader_state->tf_outputs,
807                        sizeof(*key->used_outputs) *
808                        shader_state->num_tf_outputs);
809                 if (shader_state->num_tf_outputs < key->num_used_outputs) {
810                         uint32_t tail_bytes =
811                                 sizeof(*key->used_outputs) *
812                                 (key->num_used_outputs -
813                                  shader_state->num_tf_outputs);
814                         memset(&key->used_outputs[shader_state->num_tf_outputs],
815                                0, tail_bytes);
816                 }
817                 key->num_used_outputs = shader_state->num_tf_outputs;
818         }
819 
820         struct v3d_compiled_shader *cs =
821                 v3d_get_compiled_shader(v3d, &key->base, sizeof(*key));
822         if (cs != v3d->prog.cs) {
823                 v3d->prog.cs = cs;
824                 v3d->dirty |= VC5_DIRTY_COMPILED_CS;
825         }
826 }
827 
828 void
v3d_update_compiled_shaders(struct v3d_context * v3d,uint8_t prim_mode)829 v3d_update_compiled_shaders(struct v3d_context *v3d, uint8_t prim_mode)
830 {
831         v3d_update_compiled_fs(v3d, prim_mode);
832         v3d_update_compiled_gs(v3d, prim_mode);
833         v3d_update_compiled_vs(v3d, prim_mode);
834 }
835 
836 void
v3d_update_compiled_cs(struct v3d_context * v3d)837 v3d_update_compiled_cs(struct v3d_context *v3d)
838 {
839         struct v3d_key local_key;
840         struct v3d_key *key = &local_key;
841 
842         if (!(v3d->dirty & (VC5_DIRTY_UNCOMPILED_CS |
843                             VC5_DIRTY_COMPTEX))) {
844                 return;
845         }
846 
847         memset(key, 0, sizeof(*key));
848         v3d_setup_shared_key(v3d, key, &v3d->tex[PIPE_SHADER_COMPUTE]);
849         key->shader_state = v3d->prog.bind_compute;
850 
851         struct v3d_compiled_shader *cs =
852                 v3d_get_compiled_shader(v3d, key, sizeof(*key));
853         if (cs != v3d->prog.compute) {
854                 v3d->prog.compute = cs;
855                 v3d->dirty |= VC5_DIRTY_COMPILED_CS; /* XXX */
856         }
857 }
858 
859 static uint32_t
fs_cache_hash(const void * key)860 fs_cache_hash(const void *key)
861 {
862         return _mesa_hash_data(key, sizeof(struct v3d_fs_key));
863 }
864 
865 static uint32_t
gs_cache_hash(const void * key)866 gs_cache_hash(const void *key)
867 {
868         return _mesa_hash_data(key, sizeof(struct v3d_gs_key));
869 }
870 
871 static uint32_t
vs_cache_hash(const void * key)872 vs_cache_hash(const void *key)
873 {
874         return _mesa_hash_data(key, sizeof(struct v3d_vs_key));
875 }
876 
877 static uint32_t
cs_cache_hash(const void * key)878 cs_cache_hash(const void *key)
879 {
880         return _mesa_hash_data(key, sizeof(struct v3d_key));
881 }
882 
883 static bool
fs_cache_compare(const void * key1,const void * key2)884 fs_cache_compare(const void *key1, const void *key2)
885 {
886         return memcmp(key1, key2, sizeof(struct v3d_fs_key)) == 0;
887 }
888 
889 static bool
gs_cache_compare(const void * key1,const void * key2)890 gs_cache_compare(const void *key1, const void *key2)
891 {
892         return memcmp(key1, key2, sizeof(struct v3d_gs_key)) == 0;
893 }
894 
895 static bool
vs_cache_compare(const void * key1,const void * key2)896 vs_cache_compare(const void *key1, const void *key2)
897 {
898         return memcmp(key1, key2, sizeof(struct v3d_vs_key)) == 0;
899 }
900 
901 static bool
cs_cache_compare(const void * key1,const void * key2)902 cs_cache_compare(const void *key1, const void *key2)
903 {
904         return memcmp(key1, key2, sizeof(struct v3d_key)) == 0;
905 }
906 
907 static void
v3d_shader_state_delete(struct pipe_context * pctx,void * hwcso)908 v3d_shader_state_delete(struct pipe_context *pctx, void *hwcso)
909 {
910         struct v3d_context *v3d = v3d_context(pctx);
911         struct v3d_uncompiled_shader *so = hwcso;
912         nir_shader *s = so->base.ir.nir;
913 
914         hash_table_foreach(v3d->prog.cache[s->info.stage], entry) {
915                 const struct v3d_key *key = entry->key;
916                 struct v3d_compiled_shader *shader = entry->data;
917 
918                 if (key->shader_state != so)
919                         continue;
920 
921                 if (v3d->prog.fs == shader)
922                         v3d->prog.fs = NULL;
923                 if (v3d->prog.vs == shader)
924                         v3d->prog.vs = NULL;
925                 if (v3d->prog.cs == shader)
926                         v3d->prog.cs = NULL;
927                 if (v3d->prog.compute == shader)
928                         v3d->prog.compute = NULL;
929 
930                 _mesa_hash_table_remove(v3d->prog.cache[s->info.stage], entry);
931                 v3d_free_compiled_shader(shader);
932         }
933 
934         ralloc_free(so->base.ir.nir);
935         free(so);
936 }
937 
938 static void
v3d_fp_state_bind(struct pipe_context * pctx,void * hwcso)939 v3d_fp_state_bind(struct pipe_context *pctx, void *hwcso)
940 {
941         struct v3d_context *v3d = v3d_context(pctx);
942         v3d->prog.bind_fs = hwcso;
943         v3d->dirty |= VC5_DIRTY_UNCOMPILED_FS;
944 }
945 
946 static void
v3d_gp_state_bind(struct pipe_context * pctx,void * hwcso)947 v3d_gp_state_bind(struct pipe_context *pctx, void *hwcso)
948 {
949         struct v3d_context *v3d = v3d_context(pctx);
950         v3d->prog.bind_gs = hwcso;
951         v3d->dirty |= VC5_DIRTY_UNCOMPILED_GS;
952 }
953 
954 static void
v3d_vp_state_bind(struct pipe_context * pctx,void * hwcso)955 v3d_vp_state_bind(struct pipe_context *pctx, void *hwcso)
956 {
957         struct v3d_context *v3d = v3d_context(pctx);
958         v3d->prog.bind_vs = hwcso;
959         v3d->dirty |= VC5_DIRTY_UNCOMPILED_VS;
960 }
961 
962 static void
v3d_compute_state_bind(struct pipe_context * pctx,void * state)963 v3d_compute_state_bind(struct pipe_context *pctx, void *state)
964 {
965         struct v3d_context *v3d = v3d_context(pctx);
966 
967         v3d->prog.bind_compute = state;
968         v3d->dirty |= VC5_DIRTY_UNCOMPILED_CS;
969 }
970 
971 static void *
v3d_create_compute_state(struct pipe_context * pctx,const struct pipe_compute_state * cso)972 v3d_create_compute_state(struct pipe_context *pctx,
973                          const struct pipe_compute_state *cso)
974 {
975         return v3d_uncompiled_shader_create(pctx, cso->ir_type,
976                                             (void *)cso->prog);
977 }
978 
979 void
v3d_program_init(struct pipe_context * pctx)980 v3d_program_init(struct pipe_context *pctx)
981 {
982         struct v3d_context *v3d = v3d_context(pctx);
983 
984         pctx->create_vs_state = v3d_shader_state_create;
985         pctx->delete_vs_state = v3d_shader_state_delete;
986 
987         pctx->create_gs_state = v3d_shader_state_create;
988         pctx->delete_gs_state = v3d_shader_state_delete;
989 
990         pctx->create_fs_state = v3d_shader_state_create;
991         pctx->delete_fs_state = v3d_shader_state_delete;
992 
993         pctx->bind_fs_state = v3d_fp_state_bind;
994         pctx->bind_gs_state = v3d_gp_state_bind;
995         pctx->bind_vs_state = v3d_vp_state_bind;
996 
997         if (v3d->screen->has_csd) {
998                 pctx->create_compute_state = v3d_create_compute_state;
999                 pctx->delete_compute_state = v3d_shader_state_delete;
1000                 pctx->bind_compute_state = v3d_compute_state_bind;
1001         }
1002 
1003         v3d->prog.cache[MESA_SHADER_VERTEX] =
1004                 _mesa_hash_table_create(pctx, vs_cache_hash, vs_cache_compare);
1005         v3d->prog.cache[MESA_SHADER_GEOMETRY] =
1006                 _mesa_hash_table_create(pctx, gs_cache_hash, gs_cache_compare);
1007         v3d->prog.cache[MESA_SHADER_FRAGMENT] =
1008                 _mesa_hash_table_create(pctx, fs_cache_hash, fs_cache_compare);
1009         v3d->prog.cache[MESA_SHADER_COMPUTE] =
1010                 _mesa_hash_table_create(pctx, cs_cache_hash, cs_cache_compare);
1011 }
1012 
1013 void
v3d_program_fini(struct pipe_context * pctx)1014 v3d_program_fini(struct pipe_context *pctx)
1015 {
1016         struct v3d_context *v3d = v3d_context(pctx);
1017 
1018         for (int i = 0; i < MESA_SHADER_STAGES; i++) {
1019                 struct hash_table *cache = v3d->prog.cache[i];
1020                 if (!cache)
1021                         continue;
1022 
1023                 hash_table_foreach(cache, entry) {
1024                         struct v3d_compiled_shader *shader = entry->data;
1025                         v3d_free_compiled_shader(shader);
1026                         _mesa_hash_table_remove(cache, entry);
1027                 }
1028         }
1029 
1030         v3d_bo_unreference(&v3d->prog.spill_bo);
1031 }
1032