broadcom/vc5: Fix image_h setup for both loads and stores.
[mesa.git] / src / gallium / drivers / vc5 / vc5_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 "tgsi/tgsi_dump.h"
31 #include "tgsi/tgsi_parse.h"
32 #include "compiler/nir/nir.h"
33 #include "compiler/nir/nir_builder.h"
34 #include "nir/tgsi_to_nir.h"
35 #include "compiler/v3d_compiler.h"
36 #include "vc5_context.h"
37 #include "broadcom/cle/v3d_packet_v33_pack.h"
38
39 static gl_varying_slot
40 vc5_get_slot_for_driver_location(nir_shader *s, uint32_t driver_location)
41 {
42 nir_foreach_variable(var, &s->outputs) {
43 if (var->data.driver_location == driver_location) {
44 return var->data.location;
45 }
46 }
47
48 return -1;
49 }
50
51 static void
52 vc5_set_transform_feedback_outputs(struct vc5_uncompiled_shader *so,
53 const struct pipe_stream_output_info *stream_output)
54 {
55 if (!stream_output->num_outputs)
56 return;
57
58 struct v3d_varying_slot slots[PIPE_MAX_SO_OUTPUTS * 4];
59 int slot_count = 0;
60
61 for (int buffer = 0; buffer < PIPE_MAX_SO_BUFFERS; buffer++) {
62 uint32_t buffer_offset = 0;
63 uint32_t vpm_start = slot_count;
64
65 for (int i = 0; i < stream_output->num_outputs; i++) {
66 const struct pipe_stream_output *output =
67 &stream_output->output[i];
68
69 if (output->output_buffer != buffer)
70 continue;
71
72 /* We assume that the SO outputs appear in increasing
73 * order in the buffer.
74 */
75 assert(output->dst_offset >= buffer_offset);
76
77 /* Pad any undefined slots in the output */
78 for (int j = buffer_offset; j < output->dst_offset; j++) {
79 slots[slot_count] =
80 v3d_slot_from_slot_and_component(VARYING_SLOT_POS, 0);
81 slot_count++;
82 buffer_offset++;
83 }
84
85 /* Set the coordinate shader up to output the
86 * components of this varying.
87 */
88 for (int j = 0; j < output->num_components; j++) {
89 gl_varying_slot slot =
90 vc5_get_slot_for_driver_location(so->base.ir.nir, output->register_index);
91
92 slots[slot_count] =
93 v3d_slot_from_slot_and_component(slot,
94 output->start_component + j);
95 slot_count++;
96 buffer_offset++;
97 }
98 }
99
100 uint32_t vpm_size = slot_count - vpm_start;
101 if (!vpm_size)
102 continue;
103
104 struct V3D33_TRANSFORM_FEEDBACK_OUTPUT_DATA_SPEC unpacked = {
105 /* We need the offset from the coordinate shader's VPM
106 * output block, which has the [X, Y, Z, W, Xs, Ys]
107 * values at the start. Note that this will need some
108 * shifting when PSIZ is also present.
109 */
110 .first_shaded_vertex_value_to_output = vpm_start + 6,
111 .number_of_consecutive_vertex_values_to_output_as_32_bit_values_minus_1 = vpm_size - 1,
112 .output_buffer_to_write_to = buffer,
113 };
114 V3D33_TRANSFORM_FEEDBACK_OUTPUT_DATA_SPEC_pack(NULL,
115 (void *)&so->tf_specs[so->num_tf_specs++],
116 &unpacked);
117 }
118
119 so->num_tf_outputs = slot_count;
120 so->tf_outputs = ralloc_array(so->base.ir.nir, struct v3d_varying_slot,
121 slot_count);
122 memcpy(so->tf_outputs, slots, sizeof(*slots) * slot_count);
123 }
124
125 static int
126 type_size(const struct glsl_type *type)
127 {
128 return glsl_count_attribute_slots(type, false);
129 }
130
131 static void *
132 vc5_shader_state_create(struct pipe_context *pctx,
133 const struct pipe_shader_state *cso)
134 {
135 struct vc5_context *vc5 = vc5_context(pctx);
136 struct vc5_uncompiled_shader *so = CALLOC_STRUCT(vc5_uncompiled_shader);
137 if (!so)
138 return NULL;
139
140 so->program_id = vc5->next_uncompiled_program_id++;
141
142 nir_shader *s;
143
144 if (cso->type == PIPE_SHADER_IR_NIR) {
145 /* The backend takes ownership of the NIR shader on state
146 * creation.
147 */
148 s = cso->ir.nir;
149
150 NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size,
151 (nir_lower_io_options)0);
152 } else {
153 assert(cso->type == PIPE_SHADER_IR_TGSI);
154
155 if (V3D_DEBUG & V3D_DEBUG_TGSI) {
156 fprintf(stderr, "prog %d TGSI:\n",
157 so->program_id);
158 tgsi_dump(cso->tokens, 0);
159 fprintf(stderr, "\n");
160 }
161 s = tgsi_to_nir(cso->tokens, &v3d_nir_options);
162 }
163
164 NIR_PASS_V(s, nir_opt_global_to_local);
165 NIR_PASS_V(s, nir_lower_regs_to_ssa);
166 NIR_PASS_V(s, nir_normalize_cubemap_coords);
167
168 NIR_PASS_V(s, nir_lower_load_const_to_scalar);
169
170 v3d_optimize_nir(s);
171
172 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_local);
173
174 /* Garbage collect dead instructions */
175 nir_sweep(s);
176
177 so->base.type = PIPE_SHADER_IR_NIR;
178 so->base.ir.nir = s;
179
180 vc5_set_transform_feedback_outputs(so, &cso->stream_output);
181
182 if (V3D_DEBUG & (V3D_DEBUG_NIR |
183 v3d_debug_flag_for_shader_stage(s->info.stage))) {
184 fprintf(stderr, "%s prog %d NIR:\n",
185 gl_shader_stage_name(s->info.stage),
186 so->program_id);
187 nir_print_shader(s, stderr);
188 fprintf(stderr, "\n");
189 }
190
191 return so;
192 }
193
194 static struct vc5_compiled_shader *
195 vc5_get_compiled_shader(struct vc5_context *vc5, struct v3d_key *key)
196 {
197 struct vc5_uncompiled_shader *shader_state = key->shader_state;
198 nir_shader *s = shader_state->base.ir.nir;
199
200 struct hash_table *ht;
201 uint32_t key_size;
202 if (s->info.stage == MESA_SHADER_FRAGMENT) {
203 ht = vc5->fs_cache;
204 key_size = sizeof(struct v3d_fs_key);
205 } else {
206 ht = vc5->vs_cache;
207 key_size = sizeof(struct v3d_vs_key);
208 }
209
210 struct hash_entry *entry = _mesa_hash_table_search(ht, key);
211 if (entry)
212 return entry->data;
213
214 struct vc5_compiled_shader *shader =
215 rzalloc(NULL, struct vc5_compiled_shader);
216
217 int program_id = shader_state->program_id;
218 int variant_id =
219 p_atomic_inc_return(&shader_state->compiled_variant_count);
220 uint64_t *qpu_insts;
221 uint32_t shader_size;
222
223 switch (s->info.stage) {
224 case MESA_SHADER_VERTEX:
225 shader->prog_data.vs = rzalloc(shader, struct v3d_vs_prog_data);
226
227 qpu_insts = v3d_compile_vs(vc5->screen->compiler,
228 (struct v3d_vs_key *)key,
229 shader->prog_data.vs, s,
230 program_id, variant_id,
231 &shader_size);
232 break;
233 case MESA_SHADER_FRAGMENT:
234 shader->prog_data.fs = rzalloc(shader, struct v3d_fs_prog_data);
235
236 qpu_insts = v3d_compile_fs(vc5->screen->compiler,
237 (struct v3d_fs_key *)key,
238 shader->prog_data.fs, s,
239 program_id, variant_id,
240 &shader_size);
241 break;
242 default:
243 unreachable("bad stage");
244 }
245
246 vc5_set_shader_uniform_dirty_flags(shader);
247
248 shader->bo = vc5_bo_alloc(vc5->screen, shader_size, "shader");
249 vc5_bo_map(shader->bo);
250 memcpy(shader->bo->map, qpu_insts, shader_size);
251
252 free(qpu_insts);
253
254 struct vc5_key *dup_key;
255 dup_key = ralloc_size(shader, key_size);
256 memcpy(dup_key, key, key_size);
257 _mesa_hash_table_insert(ht, dup_key, shader);
258
259 return shader;
260 }
261
262 static void
263 vc5_setup_shared_key(struct vc5_context *vc5, struct v3d_key *key,
264 struct vc5_texture_stateobj *texstate)
265 {
266 const struct v3d_device_info *devinfo = &vc5->screen->devinfo;
267
268 for (int i = 0; i < texstate->num_textures; i++) {
269 struct pipe_sampler_view *sampler = texstate->textures[i];
270 struct vc5_sampler_view *vc5_sampler = vc5_sampler_view(sampler);
271 struct pipe_sampler_state *sampler_state =
272 texstate->samplers[i];
273
274 if (!sampler)
275 continue;
276
277 key->tex[i].return_size =
278 vc5_get_tex_return_size(devinfo,
279 sampler->format,
280 sampler_state->compare_mode);
281
282 /* For 16-bit, we set up the sampler to always return 2
283 * channels (meaning no recompiles for most statechanges),
284 * while for 32 we actually scale the returns with channels.
285 */
286 if (key->tex[i].return_size == 16) {
287 key->tex[i].return_channels = 2;
288 } else if (devinfo->ver > 40) {
289 key->tex[i].return_channels = 4;
290 } else {
291 key->tex[i].return_channels =
292 vc5_get_tex_return_channels(devinfo,
293 sampler->format);
294 }
295
296 if (key->tex[i].return_size == 32 && devinfo->ver < 40) {
297 memcpy(key->tex[i].swizzle,
298 vc5_sampler->swizzle,
299 sizeof(vc5_sampler->swizzle));
300 } else {
301 /* For 16-bit returns, we let the sampler state handle
302 * the swizzle.
303 */
304 key->tex[i].swizzle[0] = PIPE_SWIZZLE_X;
305 key->tex[i].swizzle[1] = PIPE_SWIZZLE_Y;
306 key->tex[i].swizzle[2] = PIPE_SWIZZLE_Z;
307 key->tex[i].swizzle[3] = PIPE_SWIZZLE_W;
308 }
309
310 if (sampler->texture->nr_samples > 1) {
311 key->tex[i].msaa_width = sampler->texture->width0;
312 key->tex[i].msaa_height = sampler->texture->height0;
313 } else if (sampler){
314 key->tex[i].compare_mode = sampler_state->compare_mode;
315 key->tex[i].compare_func = sampler_state->compare_func;
316 key->tex[i].clamp_s =
317 sampler_state->wrap_s == PIPE_TEX_WRAP_CLAMP;
318 key->tex[i].clamp_t =
319 sampler_state->wrap_t == PIPE_TEX_WRAP_CLAMP;
320 key->tex[i].clamp_r =
321 sampler_state->wrap_r == PIPE_TEX_WRAP_CLAMP;
322 }
323 }
324
325 key->ucp_enables = vc5->rasterizer->base.clip_plane_enable;
326 }
327
328 static void
329 vc5_update_compiled_fs(struct vc5_context *vc5, uint8_t prim_mode)
330 {
331 struct vc5_job *job = vc5->job;
332 struct v3d_fs_key local_key;
333 struct v3d_fs_key *key = &local_key;
334
335 if (!(vc5->dirty & (VC5_DIRTY_PRIM_MODE |
336 VC5_DIRTY_BLEND |
337 VC5_DIRTY_FRAMEBUFFER |
338 VC5_DIRTY_ZSA |
339 VC5_DIRTY_RASTERIZER |
340 VC5_DIRTY_SAMPLE_MASK |
341 VC5_DIRTY_FRAGTEX |
342 VC5_DIRTY_UNCOMPILED_FS))) {
343 return;
344 }
345
346 memset(key, 0, sizeof(*key));
347 vc5_setup_shared_key(vc5, &key->base, &vc5->fragtex);
348 key->base.shader_state = vc5->prog.bind_fs;
349 key->is_points = (prim_mode == PIPE_PRIM_POINTS);
350 key->is_lines = (prim_mode >= PIPE_PRIM_LINES &&
351 prim_mode <= PIPE_PRIM_LINE_STRIP);
352 key->clamp_color = vc5->rasterizer->base.clamp_fragment_color;
353 if (vc5->blend->logicop_enable) {
354 key->logicop_func = vc5->blend->logicop_func;
355 } else {
356 key->logicop_func = PIPE_LOGICOP_COPY;
357 }
358 if (job->msaa) {
359 key->msaa = vc5->rasterizer->base.multisample;
360 key->sample_coverage = (vc5->rasterizer->base.multisample &&
361 vc5->sample_mask != (1 << VC5_MAX_SAMPLES) - 1);
362 key->sample_alpha_to_coverage = vc5->blend->alpha_to_coverage;
363 key->sample_alpha_to_one = vc5->blend->alpha_to_one;
364 }
365
366 key->depth_enabled = (vc5->zsa->base.depth.enabled ||
367 vc5->zsa->base.stencil[0].enabled);
368 if (vc5->zsa->base.alpha.enabled) {
369 key->alpha_test = true;
370 key->alpha_test_func = vc5->zsa->base.alpha.func;
371 }
372
373 /* gl_FragColor's propagation to however many bound color buffers
374 * there are means that the buffer count needs to be in the key.
375 */
376 key->nr_cbufs = vc5->framebuffer.nr_cbufs;
377 key->swap_color_rb = vc5->swap_color_rb;
378
379 for (int i = 0; i < key->nr_cbufs; i++) {
380 struct pipe_surface *cbuf = vc5->framebuffer.cbufs[i];
381 if (!cbuf)
382 continue;
383
384 const struct util_format_description *desc =
385 util_format_description(cbuf->format);
386
387 if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT &&
388 desc->channel[0].size == 32) {
389 key->f32_color_rb |= 1 << i;
390 }
391 }
392
393 if (key->is_points) {
394 key->point_sprite_mask =
395 vc5->rasterizer->base.sprite_coord_enable;
396 key->point_coord_upper_left =
397 (vc5->rasterizer->base.sprite_coord_mode ==
398 PIPE_SPRITE_COORD_UPPER_LEFT);
399 }
400
401 key->light_twoside = vc5->rasterizer->base.light_twoside;
402 key->shade_model_flat = vc5->rasterizer->base.flatshade;
403
404 struct vc5_compiled_shader *old_fs = vc5->prog.fs;
405 vc5->prog.fs = vc5_get_compiled_shader(vc5, &key->base);
406 if (vc5->prog.fs == old_fs)
407 return;
408
409 vc5->dirty |= VC5_DIRTY_COMPILED_FS;
410
411 if (old_fs &&
412 vc5->prog.fs->prog_data.fs->flat_shade_flags !=
413 old_fs->prog_data.fs->flat_shade_flags) {
414 vc5->dirty |= VC5_DIRTY_FLAT_SHADE_FLAGS;
415 }
416
417 if (old_fs && memcmp(vc5->prog.fs->prog_data.fs->input_slots,
418 old_fs->prog_data.fs->input_slots,
419 sizeof(vc5->prog.fs->prog_data.fs->input_slots))) {
420 vc5->dirty |= VC5_DIRTY_FS_INPUTS;
421 }
422 }
423
424 static void
425 vc5_update_compiled_vs(struct vc5_context *vc5, uint8_t prim_mode)
426 {
427 struct v3d_vs_key local_key;
428 struct v3d_vs_key *key = &local_key;
429
430 if (!(vc5->dirty & (VC5_DIRTY_PRIM_MODE |
431 VC5_DIRTY_RASTERIZER |
432 VC5_DIRTY_VERTTEX |
433 VC5_DIRTY_VTXSTATE |
434 VC5_DIRTY_UNCOMPILED_VS |
435 VC5_DIRTY_FS_INPUTS))) {
436 return;
437 }
438
439 memset(key, 0, sizeof(*key));
440 vc5_setup_shared_key(vc5, &key->base, &vc5->verttex);
441 key->base.shader_state = vc5->prog.bind_vs;
442 key->num_fs_inputs = vc5->prog.fs->prog_data.fs->base.num_inputs;
443 STATIC_ASSERT(sizeof(key->fs_inputs) ==
444 sizeof(vc5->prog.fs->prog_data.fs->input_slots));
445 memcpy(key->fs_inputs, vc5->prog.fs->prog_data.fs->input_slots,
446 sizeof(key->fs_inputs));
447 key->clamp_color = vc5->rasterizer->base.clamp_vertex_color;
448
449 key->per_vertex_point_size =
450 (prim_mode == PIPE_PRIM_POINTS &&
451 vc5->rasterizer->base.point_size_per_vertex);
452
453 struct vc5_compiled_shader *vs =
454 vc5_get_compiled_shader(vc5, &key->base);
455 if (vs != vc5->prog.vs) {
456 vc5->prog.vs = vs;
457 vc5->dirty |= VC5_DIRTY_COMPILED_VS;
458 }
459
460 key->is_coord = true;
461 /* Coord shaders only output varyings used by transform feedback. */
462 struct vc5_uncompiled_shader *shader_state = key->base.shader_state;
463 memcpy(key->fs_inputs, shader_state->tf_outputs,
464 sizeof(*key->fs_inputs) * shader_state->num_tf_outputs);
465 if (shader_state->num_tf_outputs < key->num_fs_inputs) {
466 memset(&key->fs_inputs[shader_state->num_tf_outputs],
467 0,
468 sizeof(*key->fs_inputs) * (key->num_fs_inputs -
469 shader_state->num_tf_outputs));
470 }
471 key->num_fs_inputs = shader_state->num_tf_outputs;
472
473 struct vc5_compiled_shader *cs =
474 vc5_get_compiled_shader(vc5, &key->base);
475 if (cs != vc5->prog.cs) {
476 vc5->prog.cs = cs;
477 vc5->dirty |= VC5_DIRTY_COMPILED_CS;
478 }
479 }
480
481 void
482 vc5_update_compiled_shaders(struct vc5_context *vc5, uint8_t prim_mode)
483 {
484 vc5_update_compiled_fs(vc5, prim_mode);
485 vc5_update_compiled_vs(vc5, prim_mode);
486 }
487
488 static uint32_t
489 fs_cache_hash(const void *key)
490 {
491 return _mesa_hash_data(key, sizeof(struct v3d_fs_key));
492 }
493
494 static uint32_t
495 vs_cache_hash(const void *key)
496 {
497 return _mesa_hash_data(key, sizeof(struct v3d_vs_key));
498 }
499
500 static bool
501 fs_cache_compare(const void *key1, const void *key2)
502 {
503 return memcmp(key1, key2, sizeof(struct v3d_fs_key)) == 0;
504 }
505
506 static bool
507 vs_cache_compare(const void *key1, const void *key2)
508 {
509 return memcmp(key1, key2, sizeof(struct v3d_vs_key)) == 0;
510 }
511
512 static void
513 delete_from_cache_if_matches(struct hash_table *ht,
514 struct vc5_compiled_shader **last_compile,
515 struct hash_entry *entry,
516 struct vc5_uncompiled_shader *so)
517 {
518 const struct v3d_key *key = entry->key;
519
520 if (key->shader_state == so) {
521 struct vc5_compiled_shader *shader = entry->data;
522 _mesa_hash_table_remove(ht, entry);
523 vc5_bo_unreference(&shader->bo);
524
525 if (shader == *last_compile)
526 *last_compile = NULL;
527
528 ralloc_free(shader);
529 }
530 }
531
532 static void
533 vc5_shader_state_delete(struct pipe_context *pctx, void *hwcso)
534 {
535 struct vc5_context *vc5 = vc5_context(pctx);
536 struct vc5_uncompiled_shader *so = hwcso;
537
538 struct hash_entry *entry;
539 hash_table_foreach(vc5->fs_cache, entry) {
540 delete_from_cache_if_matches(vc5->fs_cache, &vc5->prog.fs,
541 entry, so);
542 }
543 hash_table_foreach(vc5->vs_cache, entry) {
544 delete_from_cache_if_matches(vc5->vs_cache, &vc5->prog.vs,
545 entry, so);
546 }
547
548 ralloc_free(so->base.ir.nir);
549 free(so);
550 }
551
552 static void
553 vc5_fp_state_bind(struct pipe_context *pctx, void *hwcso)
554 {
555 struct vc5_context *vc5 = vc5_context(pctx);
556 vc5->prog.bind_fs = hwcso;
557 vc5->dirty |= VC5_DIRTY_UNCOMPILED_FS;
558 }
559
560 static void
561 vc5_vp_state_bind(struct pipe_context *pctx, void *hwcso)
562 {
563 struct vc5_context *vc5 = vc5_context(pctx);
564 vc5->prog.bind_vs = hwcso;
565 vc5->dirty |= VC5_DIRTY_UNCOMPILED_VS;
566 }
567
568 void
569 vc5_program_init(struct pipe_context *pctx)
570 {
571 struct vc5_context *vc5 = vc5_context(pctx);
572
573 pctx->create_vs_state = vc5_shader_state_create;
574 pctx->delete_vs_state = vc5_shader_state_delete;
575
576 pctx->create_fs_state = vc5_shader_state_create;
577 pctx->delete_fs_state = vc5_shader_state_delete;
578
579 pctx->bind_fs_state = vc5_fp_state_bind;
580 pctx->bind_vs_state = vc5_vp_state_bind;
581
582 vc5->fs_cache = _mesa_hash_table_create(pctx, fs_cache_hash,
583 fs_cache_compare);
584 vc5->vs_cache = _mesa_hash_table_create(pctx, vs_cache_hash,
585 vs_cache_compare);
586 }
587
588 void
589 vc5_program_fini(struct pipe_context *pctx)
590 {
591 struct vc5_context *vc5 = vc5_context(pctx);
592
593 struct hash_entry *entry;
594 hash_table_foreach(vc5->fs_cache, entry) {
595 struct vc5_compiled_shader *shader = entry->data;
596 vc5_bo_unreference(&shader->bo);
597 ralloc_free(shader);
598 _mesa_hash_table_remove(vc5->fs_cache, entry);
599 }
600
601 hash_table_foreach(vc5->vs_cache, entry) {
602 struct vc5_compiled_shader *shader = entry->data;
603 vc5_bo_unreference(&shader->bo);
604 ralloc_free(shader);
605 _mesa_hash_table_remove(vc5->vs_cache, entry);
606 }
607 }