gallium: s/uint/enum pipe_shader_type/ for set_constant_buffer()
[mesa.git] / src / gallium / drivers / radeonsi / si_descriptors.c
1 /*
2 * Copyright 2013 Advanced Micro Devices, Inc.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Marek Olšák <marek.olsak@amd.com>
25 */
26
27 /* Resource binding slots and sampler states (each described with 8 or
28 * 4 dwords) are stored in lists in memory which is accessed by shaders
29 * using scalar load instructions.
30 *
31 * This file is responsible for managing such lists. It keeps a copy of all
32 * descriptors in CPU memory and re-uploads a whole list if some slots have
33 * been changed.
34 *
35 * This code is also reponsible for updating shader pointers to those lists.
36 *
37 * Note that CP DMA can't be used for updating the lists, because a GPU hang
38 * could leave the list in a mid-IB state and the next IB would get wrong
39 * descriptors and the whole context would be unusable at that point.
40 * (Note: The register shadowing can't be used due to the same reason)
41 *
42 * Also, uploading descriptors to newly allocated memory doesn't require
43 * a KCACHE flush.
44 *
45 *
46 * Possible scenarios for one 16 dword image+sampler slot:
47 *
48 * | Image | w/ FMASK | Buffer | NULL
49 * [ 0: 3] Image[0:3] | Image[0:3] | Null[0:3] | Null[0:3]
50 * [ 4: 7] Image[4:7] | Image[4:7] | Buffer[0:3] | 0
51 * [ 8:11] Null[0:3] | Fmask[0:3] | Null[0:3] | Null[0:3]
52 * [12:15] Sampler[0:3] | Fmask[4:7] | Sampler[0:3] | Sampler[0:3]
53 *
54 * FMASK implies MSAA, therefore no sampler state.
55 * Sampler states are never unbound except when FMASK is bound.
56 */
57
58 #include "radeon/r600_cs.h"
59 #include "si_pipe.h"
60 #include "sid.h"
61
62 #include "util/u_format.h"
63 #include "util/u_memory.h"
64 #include "util/u_upload_mgr.h"
65
66
67 /* NULL image and buffer descriptor for textures (alpha = 1) and images
68 * (alpha = 0).
69 *
70 * For images, all fields must be zero except for the swizzle, which
71 * supports arbitrary combinations of 0s and 1s. The texture type must be
72 * any valid type (e.g. 1D). If the texture type isn't set, the hw hangs.
73 *
74 * For buffers, all fields must be zero. If they are not, the hw hangs.
75 *
76 * This is the only reason why the buffer descriptor must be in words [4:7].
77 */
78 static uint32_t null_texture_descriptor[8] = {
79 0,
80 0,
81 0,
82 S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_1) |
83 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D)
84 /* the rest must contain zeros, which is also used by the buffer
85 * descriptor */
86 };
87
88 static uint32_t null_image_descriptor[8] = {
89 0,
90 0,
91 0,
92 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D)
93 /* the rest must contain zeros, which is also used by the buffer
94 * descriptor */
95 };
96
97 static void si_init_descriptors(struct si_descriptors *desc,
98 unsigned shader_userdata_index,
99 unsigned element_dw_size,
100 unsigned num_elements,
101 const uint32_t *null_descriptor,
102 unsigned *ce_offset)
103 {
104 int i;
105
106 assert(num_elements <= sizeof(desc->dirty_mask)*8);
107
108 desc->list = CALLOC(num_elements, element_dw_size * 4);
109 desc->element_dw_size = element_dw_size;
110 desc->num_elements = num_elements;
111 desc->dirty_mask = num_elements == 32 ? ~0u : (1u << num_elements) - 1;
112 desc->shader_userdata_offset = shader_userdata_index * 4;
113
114 if (ce_offset) {
115 desc->ce_offset = *ce_offset;
116
117 /* make sure that ce_offset stays 32 byte aligned */
118 *ce_offset += align(element_dw_size * num_elements * 4, 32);
119 }
120
121 /* Initialize the array to NULL descriptors if the element size is 8. */
122 if (null_descriptor) {
123 assert(element_dw_size % 8 == 0);
124 for (i = 0; i < num_elements * element_dw_size / 8; i++)
125 memcpy(desc->list + i * 8, null_descriptor,
126 8 * 4);
127 }
128 }
129
130 static void si_release_descriptors(struct si_descriptors *desc)
131 {
132 r600_resource_reference(&desc->buffer, NULL);
133 FREE(desc->list);
134 }
135
136 static bool si_ce_upload(struct si_context *sctx, unsigned ce_offset, unsigned size,
137 unsigned *out_offset, struct r600_resource **out_buf) {
138 uint64_t va;
139
140 u_suballocator_alloc(sctx->ce_suballocator, size,
141 sctx->screen->b.info.tcc_cache_line_size,
142 out_offset, (struct pipe_resource**)out_buf);
143 if (!out_buf)
144 return false;
145
146 va = (*out_buf)->gpu_address + *out_offset;
147
148 radeon_emit(sctx->ce_ib, PKT3(PKT3_DUMP_CONST_RAM, 3, 0));
149 radeon_emit(sctx->ce_ib, ce_offset);
150 radeon_emit(sctx->ce_ib, size / 4);
151 radeon_emit(sctx->ce_ib, va);
152 radeon_emit(sctx->ce_ib, va >> 32);
153
154 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx, *out_buf,
155 RADEON_USAGE_READWRITE, RADEON_PRIO_DESCRIPTORS);
156
157 sctx->ce_need_synchronization = true;
158 return true;
159 }
160
161 static void si_ce_reinitialize_descriptors(struct si_context *sctx,
162 struct si_descriptors *desc)
163 {
164 if (desc->buffer) {
165 struct r600_resource *buffer = (struct r600_resource*)desc->buffer;
166 unsigned list_size = desc->num_elements * desc->element_dw_size * 4;
167 uint64_t va = buffer->gpu_address + desc->buffer_offset;
168 struct radeon_winsys_cs *ib = sctx->ce_preamble_ib;
169
170 if (!ib)
171 ib = sctx->ce_ib;
172
173 list_size = align(list_size, 32);
174
175 radeon_emit(ib, PKT3(PKT3_LOAD_CONST_RAM, 3, 0));
176 radeon_emit(ib, va);
177 radeon_emit(ib, va >> 32);
178 radeon_emit(ib, list_size / 4);
179 radeon_emit(ib, desc->ce_offset);
180
181 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx, desc->buffer,
182 RADEON_USAGE_READ, RADEON_PRIO_DESCRIPTORS);
183 }
184 desc->ce_ram_dirty = false;
185 }
186
187 void si_ce_reinitialize_all_descriptors(struct si_context *sctx)
188 {
189 int i;
190
191 for (i = 0; i < SI_NUM_DESCS; ++i)
192 si_ce_reinitialize_descriptors(sctx, &sctx->descriptors[i]);
193 }
194
195 void si_ce_enable_loads(struct radeon_winsys_cs *ib)
196 {
197 radeon_emit(ib, PKT3(PKT3_CONTEXT_CONTROL, 1, 0));
198 radeon_emit(ib, CONTEXT_CONTROL_LOAD_ENABLE(1) |
199 CONTEXT_CONTROL_LOAD_CE_RAM(1));
200 radeon_emit(ib, CONTEXT_CONTROL_SHADOW_ENABLE(1));
201 }
202
203 static bool si_upload_descriptors(struct si_context *sctx,
204 struct si_descriptors *desc,
205 struct r600_atom * atom)
206 {
207 unsigned list_size = desc->num_elements * desc->element_dw_size * 4;
208
209 if (!desc->dirty_mask)
210 return true;
211
212 if (sctx->ce_ib) {
213 uint32_t const* list = (uint32_t const*)desc->list;
214
215 if (desc->ce_ram_dirty)
216 si_ce_reinitialize_descriptors(sctx, desc);
217
218 while(desc->dirty_mask) {
219 int begin, count;
220 u_bit_scan_consecutive_range(&desc->dirty_mask, &begin,
221 &count);
222
223 begin *= desc->element_dw_size;
224 count *= desc->element_dw_size;
225
226 radeon_emit(sctx->ce_ib,
227 PKT3(PKT3_WRITE_CONST_RAM, count, 0));
228 radeon_emit(sctx->ce_ib, desc->ce_offset + begin * 4);
229 radeon_emit_array(sctx->ce_ib, list + begin, count);
230 }
231
232 if (!si_ce_upload(sctx, desc->ce_offset, list_size,
233 &desc->buffer_offset, &desc->buffer))
234 return false;
235 } else {
236 void *ptr;
237
238 u_upload_alloc(sctx->b.b.const_uploader, 0, list_size,
239 sctx->screen->b.info.tcc_cache_line_size,
240 &desc->buffer_offset,
241 (struct pipe_resource**)&desc->buffer, &ptr);
242 if (!desc->buffer)
243 return false; /* skip the draw call */
244
245 util_memcpy_cpu_to_le32(ptr, desc->list, list_size);
246 desc->gpu_list = ptr;
247
248 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx, desc->buffer,
249 RADEON_USAGE_READ, RADEON_PRIO_DESCRIPTORS);
250 }
251 desc->dirty_mask = 0;
252
253 if (atom)
254 si_mark_atom_dirty(sctx, atom);
255
256 return true;
257 }
258
259 static void
260 si_descriptors_begin_new_cs(struct si_context *sctx, struct si_descriptors *desc)
261 {
262 desc->ce_ram_dirty = true;
263
264 if (!desc->buffer)
265 return;
266
267 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx, desc->buffer,
268 RADEON_USAGE_READ, RADEON_PRIO_DESCRIPTORS);
269 }
270
271 /* SAMPLER VIEWS */
272
273 static unsigned
274 si_sampler_descriptors_idx(unsigned shader)
275 {
276 return SI_DESCS_FIRST_SHADER + shader * SI_NUM_SHADER_DESCS +
277 SI_SHADER_DESCS_SAMPLERS;
278 }
279
280 static struct si_descriptors *
281 si_sampler_descriptors(struct si_context *sctx, unsigned shader)
282 {
283 return &sctx->descriptors[si_sampler_descriptors_idx(shader)];
284 }
285
286 static void si_release_sampler_views(struct si_sampler_views *views)
287 {
288 int i;
289
290 for (i = 0; i < ARRAY_SIZE(views->views); i++) {
291 pipe_sampler_view_reference(&views->views[i], NULL);
292 }
293 }
294
295 static void si_sampler_view_add_buffer(struct si_context *sctx,
296 struct pipe_resource *resource,
297 enum radeon_bo_usage usage,
298 bool is_stencil_sampler,
299 bool check_mem)
300 {
301 struct r600_resource *rres;
302 struct r600_texture *rtex;
303 enum radeon_bo_priority priority;
304
305 if (!resource)
306 return;
307
308 if (resource->target != PIPE_BUFFER) {
309 struct r600_texture *tex = (struct r600_texture*)resource;
310
311 if (tex->is_depth && !r600_can_sample_zs(tex, is_stencil_sampler))
312 resource = &tex->flushed_depth_texture->resource.b.b;
313 }
314
315 rres = (struct r600_resource*)resource;
316 priority = r600_get_sampler_view_priority(rres);
317
318 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
319 rres, usage, priority,
320 check_mem);
321
322 if (resource->target == PIPE_BUFFER)
323 return;
324
325 /* Now add separate DCC or HTILE. */
326 rtex = (struct r600_texture*)resource;
327 if (rtex->dcc_separate_buffer) {
328 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
329 rtex->dcc_separate_buffer, usage,
330 RADEON_PRIO_DCC, check_mem);
331 }
332
333 if (rtex->htile_buffer &&
334 rtex->tc_compatible_htile &&
335 !is_stencil_sampler) {
336 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
337 rtex->htile_buffer, usage,
338 RADEON_PRIO_HTILE, check_mem);
339 }
340 }
341
342 static void si_sampler_views_begin_new_cs(struct si_context *sctx,
343 struct si_sampler_views *views)
344 {
345 unsigned mask = views->enabled_mask;
346
347 /* Add buffers to the CS. */
348 while (mask) {
349 int i = u_bit_scan(&mask);
350 struct si_sampler_view *sview = (struct si_sampler_view *)views->views[i];
351
352 si_sampler_view_add_buffer(sctx, sview->base.texture,
353 RADEON_USAGE_READ,
354 sview->is_stencil_sampler, false);
355 }
356 }
357
358 /* Set buffer descriptor fields that can be changed by reallocations. */
359 static void si_set_buf_desc_address(struct r600_resource *buf,
360 uint64_t offset, uint32_t *state)
361 {
362 uint64_t va = buf->gpu_address + offset;
363
364 state[0] = va;
365 state[1] &= C_008F04_BASE_ADDRESS_HI;
366 state[1] |= S_008F04_BASE_ADDRESS_HI(va >> 32);
367 }
368
369 /* Set texture descriptor fields that can be changed by reallocations.
370 *
371 * \param tex texture
372 * \param base_level_info information of the level of BASE_ADDRESS
373 * \param base_level the level of BASE_ADDRESS
374 * \param first_level pipe_sampler_view.u.tex.first_level
375 * \param block_width util_format_get_blockwidth()
376 * \param is_stencil select between separate Z & Stencil
377 * \param state descriptor to update
378 */
379 void si_set_mutable_tex_desc_fields(struct r600_texture *tex,
380 const struct radeon_surf_level *base_level_info,
381 unsigned base_level, unsigned first_level,
382 unsigned block_width, bool is_stencil,
383 uint32_t *state)
384 {
385 uint64_t va;
386 unsigned pitch = base_level_info->nblk_x * block_width;
387
388 if (tex->is_depth && !r600_can_sample_zs(tex, is_stencil)) {
389 tex = tex->flushed_depth_texture;
390 is_stencil = false;
391 }
392
393 va = tex->resource.gpu_address + base_level_info->offset;
394
395 state[1] &= C_008F14_BASE_ADDRESS_HI;
396 state[3] &= C_008F1C_TILING_INDEX;
397 state[4] &= C_008F20_PITCH;
398 state[6] &= C_008F28_COMPRESSION_EN;
399
400 state[0] = va >> 8;
401 state[1] |= S_008F14_BASE_ADDRESS_HI(va >> 40);
402 state[3] |= S_008F1C_TILING_INDEX(si_tile_mode_index(tex, base_level,
403 is_stencil));
404 state[4] |= S_008F20_PITCH(pitch - 1);
405
406 if (tex->dcc_offset && first_level < tex->surface.num_dcc_levels) {
407 state[6] |= S_008F28_COMPRESSION_EN(1);
408 state[7] = ((!tex->dcc_separate_buffer ? tex->resource.gpu_address : 0) +
409 tex->dcc_offset +
410 base_level_info->dcc_offset) >> 8;
411 } else if (tex->tc_compatible_htile) {
412 state[6] |= S_008F28_COMPRESSION_EN(1);
413 state[7] = tex->htile_buffer->gpu_address >> 8;
414 }
415 }
416
417 static void si_set_sampler_view(struct si_context *sctx,
418 unsigned shader,
419 unsigned slot, struct pipe_sampler_view *view,
420 bool disallow_early_out)
421 {
422 struct si_sampler_views *views = &sctx->samplers[shader].views;
423 struct si_sampler_view *rview = (struct si_sampler_view*)view;
424 struct si_descriptors *descs = si_sampler_descriptors(sctx, shader);
425 uint32_t *desc = descs->list + slot * 16;
426
427 if (views->views[slot] == view && !disallow_early_out)
428 return;
429
430 if (view) {
431 struct r600_texture *rtex = (struct r600_texture *)view->texture;
432
433 assert(rtex); /* views with texture == NULL aren't supported */
434 pipe_sampler_view_reference(&views->views[slot], view);
435 memcpy(desc, rview->state, 8*4);
436
437 if (rtex->resource.b.b.target == PIPE_BUFFER) {
438 rtex->resource.bind_history |= PIPE_BIND_SAMPLER_VIEW;
439
440 si_set_buf_desc_address(&rtex->resource,
441 view->u.buf.offset,
442 desc + 4);
443 } else {
444 bool is_separate_stencil =
445 rtex->db_compatible &&
446 rview->is_stencil_sampler;
447
448 si_set_mutable_tex_desc_fields(rtex,
449 rview->base_level_info,
450 rview->base_level,
451 rview->base.u.tex.first_level,
452 rview->block_width,
453 is_separate_stencil,
454 desc);
455 }
456
457 if (rtex->resource.b.b.target != PIPE_BUFFER &&
458 rtex->fmask.size) {
459 memcpy(desc + 8,
460 rview->fmask_state, 8*4);
461 } else {
462 /* Disable FMASK and bind sampler state in [12:15]. */
463 memcpy(desc + 8,
464 null_texture_descriptor, 4*4);
465
466 if (views->sampler_states[slot])
467 memcpy(desc + 12,
468 views->sampler_states[slot]->val, 4*4);
469 }
470
471 views->enabled_mask |= 1u << slot;
472
473 /* Since this can flush, it must be done after enabled_mask is
474 * updated. */
475 si_sampler_view_add_buffer(sctx, view->texture,
476 RADEON_USAGE_READ,
477 rview->is_stencil_sampler, true);
478 } else {
479 pipe_sampler_view_reference(&views->views[slot], NULL);
480 memcpy(desc, null_texture_descriptor, 8*4);
481 /* Only clear the lower dwords of FMASK. */
482 memcpy(desc + 8, null_texture_descriptor, 4*4);
483 /* Re-set the sampler state if we are transitioning from FMASK. */
484 if (views->sampler_states[slot])
485 memcpy(desc + 12,
486 views->sampler_states[slot]->val, 4*4);
487
488 views->enabled_mask &= ~(1u << slot);
489 }
490
491 descs->dirty_mask |= 1u << slot;
492 sctx->descriptors_dirty |= 1u << si_sampler_descriptors_idx(shader);
493 }
494
495 static bool is_compressed_colortex(struct r600_texture *rtex)
496 {
497 return rtex->cmask.size || rtex->fmask.size ||
498 (rtex->dcc_offset && rtex->dirty_level_mask);
499 }
500
501 static void si_update_compressed_tex_shader_mask(struct si_context *sctx,
502 unsigned shader)
503 {
504 struct si_textures_info *samplers = &sctx->samplers[shader];
505 unsigned shader_bit = 1 << shader;
506
507 if (samplers->depth_texture_mask ||
508 samplers->compressed_colortex_mask ||
509 sctx->images[shader].compressed_colortex_mask)
510 sctx->compressed_tex_shader_mask |= shader_bit;
511 else
512 sctx->compressed_tex_shader_mask &= ~shader_bit;
513 }
514
515 static void si_set_sampler_views(struct pipe_context *ctx,
516 enum pipe_shader_type shader, unsigned start,
517 unsigned count,
518 struct pipe_sampler_view **views)
519 {
520 struct si_context *sctx = (struct si_context *)ctx;
521 struct si_textures_info *samplers = &sctx->samplers[shader];
522 int i;
523
524 if (!count || shader >= SI_NUM_SHADERS)
525 return;
526
527 for (i = 0; i < count; i++) {
528 unsigned slot = start + i;
529
530 if (!views || !views[i]) {
531 samplers->depth_texture_mask &= ~(1u << slot);
532 samplers->compressed_colortex_mask &= ~(1u << slot);
533 si_set_sampler_view(sctx, shader, slot, NULL, false);
534 continue;
535 }
536
537 si_set_sampler_view(sctx, shader, slot, views[i], false);
538
539 if (views[i]->texture && views[i]->texture->target != PIPE_BUFFER) {
540 struct r600_texture *rtex =
541 (struct r600_texture*)views[i]->texture;
542 struct si_sampler_view *rview = (struct si_sampler_view *)views[i];
543
544 if (rtex->db_compatible &&
545 (!rtex->tc_compatible_htile || rview->is_stencil_sampler)) {
546 samplers->depth_texture_mask |= 1u << slot;
547 } else {
548 samplers->depth_texture_mask &= ~(1u << slot);
549 }
550 if (is_compressed_colortex(rtex)) {
551 samplers->compressed_colortex_mask |= 1u << slot;
552 } else {
553 samplers->compressed_colortex_mask &= ~(1u << slot);
554 }
555
556 if (rtex->dcc_offset &&
557 p_atomic_read(&rtex->framebuffers_bound))
558 sctx->need_check_render_feedback = true;
559 } else {
560 samplers->depth_texture_mask &= ~(1u << slot);
561 samplers->compressed_colortex_mask &= ~(1u << slot);
562 }
563 }
564
565 si_update_compressed_tex_shader_mask(sctx, shader);
566 }
567
568 static void
569 si_samplers_update_compressed_colortex_mask(struct si_textures_info *samplers)
570 {
571 unsigned mask = samplers->views.enabled_mask;
572
573 while (mask) {
574 int i = u_bit_scan(&mask);
575 struct pipe_resource *res = samplers->views.views[i]->texture;
576
577 if (res && res->target != PIPE_BUFFER) {
578 struct r600_texture *rtex = (struct r600_texture *)res;
579
580 if (is_compressed_colortex(rtex)) {
581 samplers->compressed_colortex_mask |= 1u << i;
582 } else {
583 samplers->compressed_colortex_mask &= ~(1u << i);
584 }
585 }
586 }
587 }
588
589 /* IMAGE VIEWS */
590
591 static unsigned
592 si_image_descriptors_idx(unsigned shader)
593 {
594 return SI_DESCS_FIRST_SHADER + shader * SI_NUM_SHADER_DESCS +
595 SI_SHADER_DESCS_IMAGES;
596 }
597
598 static struct si_descriptors*
599 si_image_descriptors(struct si_context *sctx, unsigned shader)
600 {
601 return &sctx->descriptors[si_image_descriptors_idx(shader)];
602 }
603
604 static void
605 si_release_image_views(struct si_images_info *images)
606 {
607 unsigned i;
608
609 for (i = 0; i < SI_NUM_IMAGES; ++i) {
610 struct pipe_image_view *view = &images->views[i];
611
612 pipe_resource_reference(&view->resource, NULL);
613 }
614 }
615
616 static void
617 si_image_views_begin_new_cs(struct si_context *sctx, struct si_images_info *images)
618 {
619 uint mask = images->enabled_mask;
620
621 /* Add buffers to the CS. */
622 while (mask) {
623 int i = u_bit_scan(&mask);
624 struct pipe_image_view *view = &images->views[i];
625
626 assert(view->resource);
627
628 si_sampler_view_add_buffer(sctx, view->resource,
629 RADEON_USAGE_READWRITE, false, false);
630 }
631 }
632
633 static void
634 si_disable_shader_image(struct si_context *ctx, unsigned shader, unsigned slot)
635 {
636 struct si_images_info *images = &ctx->images[shader];
637
638 if (images->enabled_mask & (1u << slot)) {
639 struct si_descriptors *descs = si_image_descriptors(ctx, shader);
640
641 pipe_resource_reference(&images->views[slot].resource, NULL);
642 images->compressed_colortex_mask &= ~(1 << slot);
643
644 memcpy(descs->list + slot*8, null_image_descriptor, 8*4);
645 images->enabled_mask &= ~(1u << slot);
646 descs->dirty_mask |= 1u << slot;
647 ctx->descriptors_dirty |= 1u << si_image_descriptors_idx(shader);
648 }
649 }
650
651 static void
652 si_mark_image_range_valid(const struct pipe_image_view *view)
653 {
654 struct r600_resource *res = (struct r600_resource *)view->resource;
655
656 assert(res && res->b.b.target == PIPE_BUFFER);
657
658 util_range_add(&res->valid_buffer_range,
659 view->u.buf.offset,
660 view->u.buf.offset + view->u.buf.size);
661 }
662
663 static void si_set_shader_image(struct si_context *ctx,
664 unsigned shader,
665 unsigned slot, const struct pipe_image_view *view,
666 bool skip_decompress)
667 {
668 struct si_screen *screen = ctx->screen;
669 struct si_images_info *images = &ctx->images[shader];
670 struct si_descriptors *descs = si_image_descriptors(ctx, shader);
671 struct r600_resource *res;
672 uint32_t *desc = descs->list + slot * 8;
673
674 if (!view || !view->resource) {
675 si_disable_shader_image(ctx, shader, slot);
676 return;
677 }
678
679 res = (struct r600_resource *)view->resource;
680
681 if (&images->views[slot] != view)
682 util_copy_image_view(&images->views[slot], view);
683
684 if (res->b.b.target == PIPE_BUFFER) {
685 if (view->access & PIPE_IMAGE_ACCESS_WRITE)
686 si_mark_image_range_valid(view);
687
688 si_make_buffer_descriptor(screen, res,
689 view->format,
690 view->u.buf.offset,
691 view->u.buf.size,
692 descs->list + slot * 8);
693 si_set_buf_desc_address(res, view->u.buf.offset, desc + 4);
694
695 images->compressed_colortex_mask &= ~(1 << slot);
696 res->bind_history |= PIPE_BIND_SHADER_IMAGE;
697 } else {
698 static const unsigned char swizzle[4] = { 0, 1, 2, 3 };
699 struct r600_texture *tex = (struct r600_texture *)res;
700 unsigned level = view->u.tex.level;
701 unsigned width, height, depth;
702 bool uses_dcc = tex->dcc_offset &&
703 level < tex->surface.num_dcc_levels;
704
705 assert(!tex->is_depth);
706 assert(tex->fmask.size == 0);
707
708 if (uses_dcc && !skip_decompress &&
709 (view->access & PIPE_IMAGE_ACCESS_WRITE ||
710 !vi_dcc_formats_compatible(res->b.b.format, view->format))) {
711 /* If DCC can't be disabled, at least decompress it.
712 * The decompression is relatively cheap if the surface
713 * has been decompressed already.
714 */
715 if (r600_texture_disable_dcc(&ctx->b, tex))
716 uses_dcc = false;
717 else
718 ctx->b.decompress_dcc(&ctx->b.b, tex);
719 }
720
721 if (is_compressed_colortex(tex)) {
722 images->compressed_colortex_mask |= 1 << slot;
723 } else {
724 images->compressed_colortex_mask &= ~(1 << slot);
725 }
726
727 if (uses_dcc &&
728 p_atomic_read(&tex->framebuffers_bound))
729 ctx->need_check_render_feedback = true;
730
731 /* Always force the base level to the selected level.
732 *
733 * This is required for 3D textures, where otherwise
734 * selecting a single slice for non-layered bindings
735 * fails. It doesn't hurt the other targets.
736 */
737 width = u_minify(res->b.b.width0, level);
738 height = u_minify(res->b.b.height0, level);
739 depth = u_minify(res->b.b.depth0, level);
740
741 si_make_texture_descriptor(screen, tex,
742 false, res->b.b.target,
743 view->format, swizzle,
744 0, 0,
745 view->u.tex.first_layer,
746 view->u.tex.last_layer,
747 width, height, depth,
748 desc, NULL);
749 si_set_mutable_tex_desc_fields(tex, &tex->surface.level[level],
750 level, level,
751 util_format_get_blockwidth(view->format),
752 false, desc);
753 }
754
755 images->enabled_mask |= 1u << slot;
756 descs->dirty_mask |= 1u << slot;
757 ctx->descriptors_dirty |= 1u << si_image_descriptors_idx(shader);
758
759 /* Since this can flush, it must be done after enabled_mask is updated. */
760 si_sampler_view_add_buffer(ctx, &res->b.b,
761 RADEON_USAGE_READWRITE, false, true);
762 }
763
764 static void
765 si_set_shader_images(struct pipe_context *pipe,
766 enum pipe_shader_type shader,
767 unsigned start_slot, unsigned count,
768 const struct pipe_image_view *views)
769 {
770 struct si_context *ctx = (struct si_context *)pipe;
771 unsigned i, slot;
772
773 assert(shader < SI_NUM_SHADERS);
774
775 if (!count)
776 return;
777
778 assert(start_slot + count <= SI_NUM_IMAGES);
779
780 if (views) {
781 for (i = 0, slot = start_slot; i < count; ++i, ++slot)
782 si_set_shader_image(ctx, shader, slot, &views[i], false);
783 } else {
784 for (i = 0, slot = start_slot; i < count; ++i, ++slot)
785 si_set_shader_image(ctx, shader, slot, NULL, false);
786 }
787
788 si_update_compressed_tex_shader_mask(ctx, shader);
789 }
790
791 static void
792 si_images_update_compressed_colortex_mask(struct si_images_info *images)
793 {
794 unsigned mask = images->enabled_mask;
795
796 while (mask) {
797 int i = u_bit_scan(&mask);
798 struct pipe_resource *res = images->views[i].resource;
799
800 if (res && res->target != PIPE_BUFFER) {
801 struct r600_texture *rtex = (struct r600_texture *)res;
802
803 if (is_compressed_colortex(rtex)) {
804 images->compressed_colortex_mask |= 1 << i;
805 } else {
806 images->compressed_colortex_mask &= ~(1 << i);
807 }
808 }
809 }
810 }
811
812 /* SAMPLER STATES */
813
814 static void si_bind_sampler_states(struct pipe_context *ctx,
815 enum pipe_shader_type shader,
816 unsigned start, unsigned count, void **states)
817 {
818 struct si_context *sctx = (struct si_context *)ctx;
819 struct si_textures_info *samplers = &sctx->samplers[shader];
820 struct si_descriptors *desc = si_sampler_descriptors(sctx, shader);
821 struct si_sampler_state **sstates = (struct si_sampler_state**)states;
822 int i;
823
824 if (!count || shader >= SI_NUM_SHADERS)
825 return;
826
827 for (i = 0; i < count; i++) {
828 unsigned slot = start + i;
829
830 if (!sstates[i] ||
831 sstates[i] == samplers->views.sampler_states[slot])
832 continue;
833
834 #ifdef DEBUG
835 assert(sstates[i]->magic == SI_SAMPLER_STATE_MAGIC);
836 #endif
837 samplers->views.sampler_states[slot] = sstates[i];
838
839 /* If FMASK is bound, don't overwrite it.
840 * The sampler state will be set after FMASK is unbound.
841 */
842 if (samplers->views.views[slot] &&
843 samplers->views.views[slot]->texture &&
844 samplers->views.views[slot]->texture->target != PIPE_BUFFER &&
845 ((struct r600_texture*)samplers->views.views[slot]->texture)->fmask.size)
846 continue;
847
848 memcpy(desc->list + slot * 16 + 12, sstates[i]->val, 4*4);
849 desc->dirty_mask |= 1u << slot;
850 sctx->descriptors_dirty |= 1u << si_sampler_descriptors_idx(shader);
851 }
852 }
853
854 /* BUFFER RESOURCES */
855
856 static void si_init_buffer_resources(struct si_buffer_resources *buffers,
857 struct si_descriptors *descs,
858 unsigned num_buffers,
859 unsigned shader_userdata_index,
860 enum radeon_bo_usage shader_usage,
861 enum radeon_bo_priority priority,
862 unsigned *ce_offset)
863 {
864 buffers->shader_usage = shader_usage;
865 buffers->priority = priority;
866 buffers->buffers = CALLOC(num_buffers, sizeof(struct pipe_resource*));
867
868 si_init_descriptors(descs, shader_userdata_index, 4,
869 num_buffers, NULL, ce_offset);
870 }
871
872 static void si_release_buffer_resources(struct si_buffer_resources *buffers,
873 struct si_descriptors *descs)
874 {
875 int i;
876
877 for (i = 0; i < descs->num_elements; i++) {
878 pipe_resource_reference(&buffers->buffers[i], NULL);
879 }
880
881 FREE(buffers->buffers);
882 }
883
884 static void si_buffer_resources_begin_new_cs(struct si_context *sctx,
885 struct si_buffer_resources *buffers)
886 {
887 unsigned mask = buffers->enabled_mask;
888
889 /* Add buffers to the CS. */
890 while (mask) {
891 int i = u_bit_scan(&mask);
892
893 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
894 (struct r600_resource*)buffers->buffers[i],
895 buffers->shader_usage, buffers->priority);
896 }
897 }
898
899 static void si_get_buffer_from_descriptors(struct si_buffer_resources *buffers,
900 struct si_descriptors *descs,
901 unsigned idx, struct pipe_resource **buf,
902 unsigned *offset, unsigned *size)
903 {
904 pipe_resource_reference(buf, buffers->buffers[idx]);
905 if (*buf) {
906 struct r600_resource *res = r600_resource(*buf);
907 const uint32_t *desc = descs->list + idx * 4;
908 uint64_t va;
909
910 *size = desc[2];
911
912 assert(G_008F04_STRIDE(desc[1]) == 0);
913 va = ((uint64_t)desc[1] << 32) | desc[0];
914
915 assert(va >= res->gpu_address && va + *size <= res->gpu_address + res->bo_size);
916 *offset = va - res->gpu_address;
917 }
918 }
919
920 /* VERTEX BUFFERS */
921
922 static void si_vertex_buffers_begin_new_cs(struct si_context *sctx)
923 {
924 struct si_descriptors *desc = &sctx->vertex_buffers;
925 int count = sctx->vertex_elements ? sctx->vertex_elements->count : 0;
926 int i;
927
928 for (i = 0; i < count; i++) {
929 int vb = sctx->vertex_elements->elements[i].vertex_buffer_index;
930
931 if (vb >= ARRAY_SIZE(sctx->vertex_buffer))
932 continue;
933 if (!sctx->vertex_buffer[vb].buffer)
934 continue;
935
936 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
937 (struct r600_resource*)sctx->vertex_buffer[vb].buffer,
938 RADEON_USAGE_READ, RADEON_PRIO_VERTEX_BUFFER);
939 }
940
941 if (!desc->buffer)
942 return;
943 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
944 desc->buffer, RADEON_USAGE_READ,
945 RADEON_PRIO_DESCRIPTORS);
946 }
947
948 bool si_upload_vertex_buffer_descriptors(struct si_context *sctx)
949 {
950 struct si_vertex_element *velems = sctx->vertex_elements;
951 struct si_descriptors *desc = &sctx->vertex_buffers;
952 unsigned i, count = velems->count;
953 unsigned desc_list_byte_size = velems->desc_list_byte_size;
954 uint64_t va;
955 uint32_t *ptr;
956
957 if (!sctx->vertex_buffers_dirty || !count || !velems)
958 return true;
959
960 unsigned first_vb_use_mask = velems->first_vb_use_mask;
961
962 /* Vertex buffer descriptors are the only ones which are uploaded
963 * directly through a staging buffer and don't go through
964 * the fine-grained upload path.
965 */
966 u_upload_alloc(sctx->b.b.const_uploader, 0,
967 desc_list_byte_size,
968 si_optimal_tcc_alignment(sctx, desc_list_byte_size),
969 &desc->buffer_offset,
970 (struct pipe_resource**)&desc->buffer, (void**)&ptr);
971 if (!desc->buffer)
972 return false;
973
974 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
975 desc->buffer, RADEON_USAGE_READ,
976 RADEON_PRIO_DESCRIPTORS);
977
978 assert(count <= SI_MAX_ATTRIBS);
979
980 for (i = 0; i < count; i++) {
981 struct pipe_vertex_element *ve = &velems->elements[i];
982 struct pipe_vertex_buffer *vb;
983 struct r600_resource *rbuffer;
984 unsigned offset;
985 unsigned vbo_index = ve->vertex_buffer_index;
986 uint32_t *desc = &ptr[i*4];
987
988 vb = &sctx->vertex_buffer[vbo_index];
989 rbuffer = (struct r600_resource*)vb->buffer;
990 if (!rbuffer) {
991 memset(desc, 0, 16);
992 continue;
993 }
994
995 offset = vb->buffer_offset + ve->src_offset;
996 va = rbuffer->gpu_address + offset;
997
998 /* Fill in T# buffer resource description */
999 desc[0] = va;
1000 desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
1001 S_008F04_STRIDE(vb->stride);
1002
1003 if (sctx->b.chip_class <= CIK && vb->stride) {
1004 /* Round up by rounding down and adding 1 */
1005 desc[2] = (vb->buffer->width0 - offset -
1006 velems->format_size[i]) /
1007 vb->stride + 1;
1008 } else {
1009 desc[2] = vb->buffer->width0 - offset;
1010 }
1011
1012 desc[3] = velems->rsrc_word3[i];
1013
1014 if (first_vb_use_mask & (1 << i)) {
1015 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
1016 (struct r600_resource*)vb->buffer,
1017 RADEON_USAGE_READ, RADEON_PRIO_VERTEX_BUFFER);
1018 }
1019 }
1020
1021 /* Don't flush the const cache. It would have a very negative effect
1022 * on performance (confirmed by testing). New descriptors are always
1023 * uploaded to a fresh new buffer, so I don't think flushing the const
1024 * cache is needed. */
1025 si_mark_atom_dirty(sctx, &sctx->shader_userdata.atom);
1026 if (sctx->b.chip_class >= CIK)
1027 si_mark_atom_dirty(sctx, &sctx->prefetch_L2);
1028 sctx->vertex_buffers_dirty = false;
1029 sctx->vertex_buffer_pointer_dirty = true;
1030 return true;
1031 }
1032
1033
1034 /* CONSTANT BUFFERS */
1035
1036 static unsigned
1037 si_const_buffer_descriptors_idx(unsigned shader)
1038 {
1039 return SI_DESCS_FIRST_SHADER + shader * SI_NUM_SHADER_DESCS +
1040 SI_SHADER_DESCS_CONST_BUFFERS;
1041 }
1042
1043 static struct si_descriptors *
1044 si_const_buffer_descriptors(struct si_context *sctx, unsigned shader)
1045 {
1046 return &sctx->descriptors[si_const_buffer_descriptors_idx(shader)];
1047 }
1048
1049 void si_upload_const_buffer(struct si_context *sctx, struct r600_resource **rbuffer,
1050 const uint8_t *ptr, unsigned size, uint32_t *const_offset)
1051 {
1052 void *tmp;
1053
1054 u_upload_alloc(sctx->b.b.const_uploader, 0, size,
1055 si_optimal_tcc_alignment(sctx, size),
1056 const_offset,
1057 (struct pipe_resource**)rbuffer, &tmp);
1058 if (*rbuffer)
1059 util_memcpy_cpu_to_le32(tmp, ptr, size);
1060 }
1061
1062 static void si_set_constant_buffer(struct si_context *sctx,
1063 struct si_buffer_resources *buffers,
1064 unsigned descriptors_idx,
1065 uint slot, const struct pipe_constant_buffer *input)
1066 {
1067 struct si_descriptors *descs = &sctx->descriptors[descriptors_idx];
1068 assert(slot < descs->num_elements);
1069 pipe_resource_reference(&buffers->buffers[slot], NULL);
1070
1071 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD is buggy
1072 * with a NULL buffer). We need to use a dummy buffer instead. */
1073 if (sctx->b.chip_class == CIK &&
1074 (!input || (!input->buffer && !input->user_buffer)))
1075 input = &sctx->null_const_buf;
1076
1077 if (input && (input->buffer || input->user_buffer)) {
1078 struct pipe_resource *buffer = NULL;
1079 uint64_t va;
1080
1081 /* Upload the user buffer if needed. */
1082 if (input->user_buffer) {
1083 unsigned buffer_offset;
1084
1085 si_upload_const_buffer(sctx,
1086 (struct r600_resource**)&buffer, input->user_buffer,
1087 input->buffer_size, &buffer_offset);
1088 if (!buffer) {
1089 /* Just unbind on failure. */
1090 si_set_constant_buffer(sctx, buffers, descriptors_idx, slot, NULL);
1091 return;
1092 }
1093 va = r600_resource(buffer)->gpu_address + buffer_offset;
1094 } else {
1095 pipe_resource_reference(&buffer, input->buffer);
1096 va = r600_resource(buffer)->gpu_address + input->buffer_offset;
1097 /* Only track usage for non-user buffers. */
1098 r600_resource(buffer)->bind_history |= PIPE_BIND_CONSTANT_BUFFER;
1099 }
1100
1101 /* Set the descriptor. */
1102 uint32_t *desc = descs->list + slot*4;
1103 desc[0] = va;
1104 desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
1105 S_008F04_STRIDE(0);
1106 desc[2] = input->buffer_size;
1107 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1108 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1109 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1110 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1111 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1112 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
1113
1114 buffers->buffers[slot] = buffer;
1115 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
1116 (struct r600_resource*)buffer,
1117 buffers->shader_usage,
1118 buffers->priority, true);
1119 buffers->enabled_mask |= 1u << slot;
1120 } else {
1121 /* Clear the descriptor. */
1122 memset(descs->list + slot*4, 0, sizeof(uint32_t) * 4);
1123 buffers->enabled_mask &= ~(1u << slot);
1124 }
1125
1126 descs->dirty_mask |= 1u << slot;
1127 sctx->descriptors_dirty |= 1u << descriptors_idx;
1128 }
1129
1130 void si_set_rw_buffer(struct si_context *sctx,
1131 uint slot, const struct pipe_constant_buffer *input)
1132 {
1133 si_set_constant_buffer(sctx, &sctx->rw_buffers,
1134 SI_DESCS_RW_BUFFERS, slot, input);
1135 }
1136
1137 static void si_pipe_set_constant_buffer(struct pipe_context *ctx,
1138 enum pipe_shader_type shader, uint slot,
1139 const struct pipe_constant_buffer *input)
1140 {
1141 struct si_context *sctx = (struct si_context *)ctx;
1142
1143 if (shader >= SI_NUM_SHADERS)
1144 return;
1145
1146 si_set_constant_buffer(sctx, &sctx->const_buffers[shader],
1147 si_const_buffer_descriptors_idx(shader),
1148 slot, input);
1149 }
1150
1151 void si_get_pipe_constant_buffer(struct si_context *sctx, uint shader,
1152 uint slot, struct pipe_constant_buffer *cbuf)
1153 {
1154 cbuf->user_buffer = NULL;
1155 si_get_buffer_from_descriptors(
1156 &sctx->const_buffers[shader],
1157 si_const_buffer_descriptors(sctx, shader),
1158 slot, &cbuf->buffer, &cbuf->buffer_offset, &cbuf->buffer_size);
1159 }
1160
1161 /* SHADER BUFFERS */
1162
1163 static unsigned
1164 si_shader_buffer_descriptors_idx(enum pipe_shader_type shader)
1165 {
1166 return SI_DESCS_FIRST_SHADER + shader * SI_NUM_SHADER_DESCS +
1167 SI_SHADER_DESCS_SHADER_BUFFERS;
1168 }
1169
1170 static struct si_descriptors *
1171 si_shader_buffer_descriptors(struct si_context *sctx,
1172 enum pipe_shader_type shader)
1173 {
1174 return &sctx->descriptors[si_shader_buffer_descriptors_idx(shader)];
1175 }
1176
1177 static void si_set_shader_buffers(struct pipe_context *ctx,
1178 enum pipe_shader_type shader,
1179 unsigned start_slot, unsigned count,
1180 const struct pipe_shader_buffer *sbuffers)
1181 {
1182 struct si_context *sctx = (struct si_context *)ctx;
1183 struct si_buffer_resources *buffers = &sctx->shader_buffers[shader];
1184 struct si_descriptors *descs = si_shader_buffer_descriptors(sctx, shader);
1185 unsigned i;
1186
1187 assert(start_slot + count <= SI_NUM_SHADER_BUFFERS);
1188
1189 for (i = 0; i < count; ++i) {
1190 const struct pipe_shader_buffer *sbuffer = sbuffers ? &sbuffers[i] : NULL;
1191 struct r600_resource *buf;
1192 unsigned slot = start_slot + i;
1193 uint32_t *desc = descs->list + slot * 4;
1194 uint64_t va;
1195
1196 if (!sbuffer || !sbuffer->buffer) {
1197 pipe_resource_reference(&buffers->buffers[slot], NULL);
1198 memset(desc, 0, sizeof(uint32_t) * 4);
1199 buffers->enabled_mask &= ~(1u << slot);
1200 descs->dirty_mask |= 1u << slot;
1201 sctx->descriptors_dirty |=
1202 1u << si_shader_buffer_descriptors_idx(shader);
1203 continue;
1204 }
1205
1206 buf = (struct r600_resource *)sbuffer->buffer;
1207 va = buf->gpu_address + sbuffer->buffer_offset;
1208
1209 desc[0] = va;
1210 desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
1211 S_008F04_STRIDE(0);
1212 desc[2] = sbuffer->buffer_size;
1213 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1214 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1215 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1216 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1217 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1218 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
1219
1220 pipe_resource_reference(&buffers->buffers[slot], &buf->b.b);
1221 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx, buf,
1222 buffers->shader_usage,
1223 buffers->priority, true);
1224 buf->bind_history |= PIPE_BIND_SHADER_BUFFER;
1225
1226 buffers->enabled_mask |= 1u << slot;
1227 descs->dirty_mask |= 1u << slot;
1228 sctx->descriptors_dirty |=
1229 1u << si_shader_buffer_descriptors_idx(shader);
1230 }
1231 }
1232
1233 void si_get_shader_buffers(struct si_context *sctx, uint shader,
1234 uint start_slot, uint count,
1235 struct pipe_shader_buffer *sbuf)
1236 {
1237 struct si_buffer_resources *buffers = &sctx->shader_buffers[shader];
1238 struct si_descriptors *descs = si_shader_buffer_descriptors(sctx, shader);
1239
1240 for (unsigned i = 0; i < count; ++i) {
1241 si_get_buffer_from_descriptors(
1242 buffers, descs, start_slot + i,
1243 &sbuf[i].buffer, &sbuf[i].buffer_offset,
1244 &sbuf[i].buffer_size);
1245 }
1246 }
1247
1248 /* RING BUFFERS */
1249
1250 void si_set_ring_buffer(struct pipe_context *ctx, uint slot,
1251 struct pipe_resource *buffer,
1252 unsigned stride, unsigned num_records,
1253 bool add_tid, bool swizzle,
1254 unsigned element_size, unsigned index_stride, uint64_t offset)
1255 {
1256 struct si_context *sctx = (struct si_context *)ctx;
1257 struct si_buffer_resources *buffers = &sctx->rw_buffers;
1258 struct si_descriptors *descs = &sctx->descriptors[SI_DESCS_RW_BUFFERS];
1259
1260 /* The stride field in the resource descriptor has 14 bits */
1261 assert(stride < (1 << 14));
1262
1263 assert(slot < descs->num_elements);
1264 pipe_resource_reference(&buffers->buffers[slot], NULL);
1265
1266 if (buffer) {
1267 uint64_t va;
1268
1269 va = r600_resource(buffer)->gpu_address + offset;
1270
1271 switch (element_size) {
1272 default:
1273 assert(!"Unsupported ring buffer element size");
1274 case 0:
1275 case 2:
1276 element_size = 0;
1277 break;
1278 case 4:
1279 element_size = 1;
1280 break;
1281 case 8:
1282 element_size = 2;
1283 break;
1284 case 16:
1285 element_size = 3;
1286 break;
1287 }
1288
1289 switch (index_stride) {
1290 default:
1291 assert(!"Unsupported ring buffer index stride");
1292 case 0:
1293 case 8:
1294 index_stride = 0;
1295 break;
1296 case 16:
1297 index_stride = 1;
1298 break;
1299 case 32:
1300 index_stride = 2;
1301 break;
1302 case 64:
1303 index_stride = 3;
1304 break;
1305 }
1306
1307 if (sctx->b.chip_class >= VI && stride)
1308 num_records *= stride;
1309
1310 /* Set the descriptor. */
1311 uint32_t *desc = descs->list + slot*4;
1312 desc[0] = va;
1313 desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
1314 S_008F04_STRIDE(stride) |
1315 S_008F04_SWIZZLE_ENABLE(swizzle);
1316 desc[2] = num_records;
1317 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1318 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1319 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1320 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1321 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1322 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
1323 S_008F0C_ELEMENT_SIZE(element_size) |
1324 S_008F0C_INDEX_STRIDE(index_stride) |
1325 S_008F0C_ADD_TID_ENABLE(add_tid);
1326
1327 pipe_resource_reference(&buffers->buffers[slot], buffer);
1328 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
1329 (struct r600_resource*)buffer,
1330 buffers->shader_usage, buffers->priority);
1331 buffers->enabled_mask |= 1u << slot;
1332 } else {
1333 /* Clear the descriptor. */
1334 memset(descs->list + slot*4, 0, sizeof(uint32_t) * 4);
1335 buffers->enabled_mask &= ~(1u << slot);
1336 }
1337
1338 descs->dirty_mask |= 1u << slot;
1339 sctx->descriptors_dirty |= 1u << SI_DESCS_RW_BUFFERS;
1340 }
1341
1342 /* STREAMOUT BUFFERS */
1343
1344 static void si_set_streamout_targets(struct pipe_context *ctx,
1345 unsigned num_targets,
1346 struct pipe_stream_output_target **targets,
1347 const unsigned *offsets)
1348 {
1349 struct si_context *sctx = (struct si_context *)ctx;
1350 struct si_buffer_resources *buffers = &sctx->rw_buffers;
1351 struct si_descriptors *descs = &sctx->descriptors[SI_DESCS_RW_BUFFERS];
1352 unsigned old_num_targets = sctx->b.streamout.num_targets;
1353 unsigned i, bufidx;
1354
1355 /* We are going to unbind the buffers. Mark which caches need to be flushed. */
1356 if (sctx->b.streamout.num_targets && sctx->b.streamout.begin_emitted) {
1357 /* Since streamout uses vector writes which go through TC L2
1358 * and most other clients can use TC L2 as well, we don't need
1359 * to flush it.
1360 *
1361 * The only cases which requires flushing it is VGT DMA index
1362 * fetching (on <= CIK) and indirect draw data, which are rare
1363 * cases. Thus, flag the TC L2 dirtiness in the resource and
1364 * handle it at draw call time.
1365 */
1366 for (i = 0; i < sctx->b.streamout.num_targets; i++)
1367 if (sctx->b.streamout.targets[i])
1368 r600_resource(sctx->b.streamout.targets[i]->b.buffer)->TC_L2_dirty = true;
1369
1370 /* Invalidate the scalar cache in case a streamout buffer is
1371 * going to be used as a constant buffer.
1372 *
1373 * Invalidate TC L1, because streamout bypasses it (done by
1374 * setting GLC=1 in the store instruction), but it can contain
1375 * outdated data of streamout buffers.
1376 *
1377 * VS_PARTIAL_FLUSH is required if the buffers are going to be
1378 * used as an input immediately.
1379 */
1380 sctx->b.flags |= SI_CONTEXT_INV_SMEM_L1 |
1381 SI_CONTEXT_INV_VMEM_L1 |
1382 SI_CONTEXT_VS_PARTIAL_FLUSH;
1383 }
1384
1385 /* All readers of the streamout targets need to be finished before we can
1386 * start writing to the targets.
1387 */
1388 if (num_targets)
1389 sctx->b.flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
1390 SI_CONTEXT_CS_PARTIAL_FLUSH;
1391
1392 /* Streamout buffers must be bound in 2 places:
1393 * 1) in VGT by setting the VGT_STRMOUT registers
1394 * 2) as shader resources
1395 */
1396
1397 /* Set the VGT regs. */
1398 r600_set_streamout_targets(ctx, num_targets, targets, offsets);
1399
1400 /* Set the shader resources.*/
1401 for (i = 0; i < num_targets; i++) {
1402 bufidx = SI_VS_STREAMOUT_BUF0 + i;
1403
1404 if (targets[i]) {
1405 struct pipe_resource *buffer = targets[i]->buffer;
1406 uint64_t va = r600_resource(buffer)->gpu_address;
1407
1408 /* Set the descriptor.
1409 *
1410 * On VI, the format must be non-INVALID, otherwise
1411 * the buffer will be considered not bound and store
1412 * instructions will be no-ops.
1413 */
1414 uint32_t *desc = descs->list + bufidx*4;
1415 desc[0] = va;
1416 desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32);
1417 desc[2] = 0xffffffff;
1418 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1419 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1420 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1421 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1422 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
1423
1424 /* Set the resource. */
1425 pipe_resource_reference(&buffers->buffers[bufidx],
1426 buffer);
1427 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
1428 (struct r600_resource*)buffer,
1429 buffers->shader_usage,
1430 RADEON_PRIO_SHADER_RW_BUFFER,
1431 true);
1432 r600_resource(buffer)->bind_history |= PIPE_BIND_STREAM_OUTPUT;
1433
1434 buffers->enabled_mask |= 1u << bufidx;
1435 } else {
1436 /* Clear the descriptor and unset the resource. */
1437 memset(descs->list + bufidx*4, 0,
1438 sizeof(uint32_t) * 4);
1439 pipe_resource_reference(&buffers->buffers[bufidx],
1440 NULL);
1441 buffers->enabled_mask &= ~(1u << bufidx);
1442 }
1443 descs->dirty_mask |= 1u << bufidx;
1444 }
1445 for (; i < old_num_targets; i++) {
1446 bufidx = SI_VS_STREAMOUT_BUF0 + i;
1447 /* Clear the descriptor and unset the resource. */
1448 memset(descs->list + bufidx*4, 0, sizeof(uint32_t) * 4);
1449 pipe_resource_reference(&buffers->buffers[bufidx], NULL);
1450 buffers->enabled_mask &= ~(1u << bufidx);
1451 descs->dirty_mask |= 1u << bufidx;
1452 }
1453
1454 sctx->descriptors_dirty |= 1u << SI_DESCS_RW_BUFFERS;
1455 }
1456
1457 static void si_desc_reset_buffer_offset(struct pipe_context *ctx,
1458 uint32_t *desc, uint64_t old_buf_va,
1459 struct pipe_resource *new_buf)
1460 {
1461 /* Retrieve the buffer offset from the descriptor. */
1462 uint64_t old_desc_va =
1463 desc[0] | ((uint64_t)G_008F04_BASE_ADDRESS_HI(desc[1]) << 32);
1464
1465 assert(old_buf_va <= old_desc_va);
1466 uint64_t offset_within_buffer = old_desc_va - old_buf_va;
1467
1468 /* Update the descriptor. */
1469 si_set_buf_desc_address(r600_resource(new_buf), offset_within_buffer,
1470 desc);
1471 }
1472
1473 /* INTERNAL CONST BUFFERS */
1474
1475 static void si_set_polygon_stipple(struct pipe_context *ctx,
1476 const struct pipe_poly_stipple *state)
1477 {
1478 struct si_context *sctx = (struct si_context *)ctx;
1479 struct pipe_constant_buffer cb = {};
1480 unsigned stipple[32];
1481 int i;
1482
1483 for (i = 0; i < 32; i++)
1484 stipple[i] = util_bitreverse(state->stipple[i]);
1485
1486 cb.user_buffer = stipple;
1487 cb.buffer_size = sizeof(stipple);
1488
1489 si_set_rw_buffer(sctx, SI_PS_CONST_POLY_STIPPLE, &cb);
1490 }
1491
1492 /* TEXTURE METADATA ENABLE/DISABLE */
1493
1494 /* CMASK can be enabled (for fast clear) and disabled (for texture export)
1495 * while the texture is bound, possibly by a different context. In that case,
1496 * call this function to update compressed_colortex_masks.
1497 */
1498 void si_update_compressed_colortex_masks(struct si_context *sctx)
1499 {
1500 for (int i = 0; i < SI_NUM_SHADERS; ++i) {
1501 si_samplers_update_compressed_colortex_mask(&sctx->samplers[i]);
1502 si_images_update_compressed_colortex_mask(&sctx->images[i]);
1503 si_update_compressed_tex_shader_mask(sctx, i);
1504 }
1505 }
1506
1507 /* BUFFER DISCARD/INVALIDATION */
1508
1509 /** Reset descriptors of buffer resources after \p buf has been invalidated. */
1510 static void si_reset_buffer_resources(struct si_context *sctx,
1511 struct si_buffer_resources *buffers,
1512 unsigned descriptors_idx,
1513 struct pipe_resource *buf,
1514 uint64_t old_va)
1515 {
1516 struct si_descriptors *descs = &sctx->descriptors[descriptors_idx];
1517 unsigned mask = buffers->enabled_mask;
1518
1519 while (mask) {
1520 unsigned i = u_bit_scan(&mask);
1521 if (buffers->buffers[i] == buf) {
1522 si_desc_reset_buffer_offset(&sctx->b.b,
1523 descs->list + i*4,
1524 old_va, buf);
1525 descs->dirty_mask |= 1u << i;
1526 sctx->descriptors_dirty |= 1u << descriptors_idx;
1527
1528 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
1529 (struct r600_resource *)buf,
1530 buffers->shader_usage,
1531 buffers->priority, true);
1532 }
1533 }
1534 }
1535
1536 /* Reallocate a buffer a update all resource bindings where the buffer is
1537 * bound.
1538 *
1539 * This is used to avoid CPU-GPU synchronizations, because it makes the buffer
1540 * idle by discarding its contents. Apps usually tell us when to do this using
1541 * map_buffer flags, for example.
1542 */
1543 static void si_invalidate_buffer(struct pipe_context *ctx, struct pipe_resource *buf)
1544 {
1545 struct si_context *sctx = (struct si_context*)ctx;
1546 struct r600_resource *rbuffer = r600_resource(buf);
1547 unsigned i, shader;
1548 uint64_t old_va = rbuffer->gpu_address;
1549 unsigned num_elems = sctx->vertex_elements ?
1550 sctx->vertex_elements->count : 0;
1551
1552 /* Reallocate the buffer in the same pipe_resource. */
1553 r600_alloc_resource(&sctx->screen->b, rbuffer);
1554
1555 /* We changed the buffer, now we need to bind it where the old one
1556 * was bound. This consists of 2 things:
1557 * 1) Updating the resource descriptor and dirtying it.
1558 * 2) Adding a relocation to the CS, so that it's usable.
1559 */
1560
1561 /* Vertex buffers. */
1562 if (rbuffer->bind_history & PIPE_BIND_VERTEX_BUFFER) {
1563 for (i = 0; i < num_elems; i++) {
1564 int vb = sctx->vertex_elements->elements[i].vertex_buffer_index;
1565
1566 if (vb >= ARRAY_SIZE(sctx->vertex_buffer))
1567 continue;
1568 if (!sctx->vertex_buffer[vb].buffer)
1569 continue;
1570
1571 if (sctx->vertex_buffer[vb].buffer == buf) {
1572 sctx->vertex_buffers_dirty = true;
1573 break;
1574 }
1575 }
1576 }
1577
1578 /* Streamout buffers. (other internal buffers can't be invalidated) */
1579 if (rbuffer->bind_history & PIPE_BIND_STREAM_OUTPUT) {
1580 for (i = SI_VS_STREAMOUT_BUF0; i <= SI_VS_STREAMOUT_BUF3; i++) {
1581 struct si_buffer_resources *buffers = &sctx->rw_buffers;
1582 struct si_descriptors *descs =
1583 &sctx->descriptors[SI_DESCS_RW_BUFFERS];
1584
1585 if (buffers->buffers[i] != buf)
1586 continue;
1587
1588 si_desc_reset_buffer_offset(ctx, descs->list + i*4,
1589 old_va, buf);
1590 descs->dirty_mask |= 1u << i;
1591 sctx->descriptors_dirty |= 1u << SI_DESCS_RW_BUFFERS;
1592
1593 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
1594 rbuffer, buffers->shader_usage,
1595 RADEON_PRIO_SHADER_RW_BUFFER,
1596 true);
1597
1598 /* Update the streamout state. */
1599 if (sctx->b.streamout.begin_emitted)
1600 r600_emit_streamout_end(&sctx->b);
1601 sctx->b.streamout.append_bitmask =
1602 sctx->b.streamout.enabled_mask;
1603 r600_streamout_buffers_dirty(&sctx->b);
1604 }
1605 }
1606
1607 /* Constant and shader buffers. */
1608 if (rbuffer->bind_history & PIPE_BIND_CONSTANT_BUFFER) {
1609 for (shader = 0; shader < SI_NUM_SHADERS; shader++)
1610 si_reset_buffer_resources(sctx, &sctx->const_buffers[shader],
1611 si_const_buffer_descriptors_idx(shader),
1612 buf, old_va);
1613 }
1614
1615 if (rbuffer->bind_history & PIPE_BIND_SHADER_BUFFER) {
1616 for (shader = 0; shader < SI_NUM_SHADERS; shader++)
1617 si_reset_buffer_resources(sctx, &sctx->shader_buffers[shader],
1618 si_shader_buffer_descriptors_idx(shader),
1619 buf, old_va);
1620 }
1621
1622 if (rbuffer->bind_history & PIPE_BIND_SAMPLER_VIEW) {
1623 /* Texture buffers - update bindings. */
1624 for (shader = 0; shader < SI_NUM_SHADERS; shader++) {
1625 struct si_sampler_views *views = &sctx->samplers[shader].views;
1626 struct si_descriptors *descs =
1627 si_sampler_descriptors(sctx, shader);
1628 unsigned mask = views->enabled_mask;
1629
1630 while (mask) {
1631 unsigned i = u_bit_scan(&mask);
1632 if (views->views[i]->texture == buf) {
1633 si_desc_reset_buffer_offset(ctx,
1634 descs->list +
1635 i * 16 + 4,
1636 old_va, buf);
1637 descs->dirty_mask |= 1u << i;
1638 sctx->descriptors_dirty |=
1639 1u << si_sampler_descriptors_idx(shader);
1640
1641 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
1642 rbuffer, RADEON_USAGE_READ,
1643 RADEON_PRIO_SAMPLER_BUFFER,
1644 true);
1645 }
1646 }
1647 }
1648 }
1649
1650 /* Shader images */
1651 if (rbuffer->bind_history & PIPE_BIND_SHADER_IMAGE) {
1652 for (shader = 0; shader < SI_NUM_SHADERS; ++shader) {
1653 struct si_images_info *images = &sctx->images[shader];
1654 struct si_descriptors *descs =
1655 si_image_descriptors(sctx, shader);
1656 unsigned mask = images->enabled_mask;
1657
1658 while (mask) {
1659 unsigned i = u_bit_scan(&mask);
1660
1661 if (images->views[i].resource == buf) {
1662 if (images->views[i].access & PIPE_IMAGE_ACCESS_WRITE)
1663 si_mark_image_range_valid(&images->views[i]);
1664
1665 si_desc_reset_buffer_offset(
1666 ctx, descs->list + i * 8 + 4,
1667 old_va, buf);
1668 descs->dirty_mask |= 1u << i;
1669 sctx->descriptors_dirty |=
1670 1u << si_image_descriptors_idx(shader);
1671
1672 radeon_add_to_buffer_list_check_mem(
1673 &sctx->b, &sctx->b.gfx, rbuffer,
1674 RADEON_USAGE_READWRITE,
1675 RADEON_PRIO_SAMPLER_BUFFER, true);
1676 }
1677 }
1678 }
1679 }
1680 }
1681
1682 /* Update mutable image descriptor fields of all bound textures. */
1683 void si_update_all_texture_descriptors(struct si_context *sctx)
1684 {
1685 unsigned shader;
1686
1687 for (shader = 0; shader < SI_NUM_SHADERS; shader++) {
1688 struct si_sampler_views *samplers = &sctx->samplers[shader].views;
1689 struct si_images_info *images = &sctx->images[shader];
1690 unsigned mask;
1691
1692 /* Images. */
1693 mask = images->enabled_mask;
1694 while (mask) {
1695 unsigned i = u_bit_scan(&mask);
1696 struct pipe_image_view *view = &images->views[i];
1697
1698 if (!view->resource ||
1699 view->resource->target == PIPE_BUFFER)
1700 continue;
1701
1702 si_set_shader_image(sctx, shader, i, view, true);
1703 }
1704
1705 /* Sampler views. */
1706 mask = samplers->enabled_mask;
1707 while (mask) {
1708 unsigned i = u_bit_scan(&mask);
1709 struct pipe_sampler_view *view = samplers->views[i];
1710
1711 if (!view ||
1712 !view->texture ||
1713 view->texture->target == PIPE_BUFFER)
1714 continue;
1715
1716 si_set_sampler_view(sctx, shader, i,
1717 samplers->views[i], true);
1718 }
1719
1720 si_update_compressed_tex_shader_mask(sctx, shader);
1721 }
1722 }
1723
1724 /* SHADER USER DATA */
1725
1726 static void si_mark_shader_pointers_dirty(struct si_context *sctx,
1727 unsigned shader)
1728 {
1729 sctx->shader_pointers_dirty |=
1730 u_bit_consecutive(SI_DESCS_FIRST_SHADER + shader * SI_NUM_SHADER_DESCS,
1731 SI_NUM_SHADER_DESCS);
1732
1733 if (shader == PIPE_SHADER_VERTEX)
1734 sctx->vertex_buffer_pointer_dirty = sctx->vertex_buffers.buffer != NULL;
1735
1736 si_mark_atom_dirty(sctx, &sctx->shader_userdata.atom);
1737 }
1738
1739 static void si_shader_userdata_begin_new_cs(struct si_context *sctx)
1740 {
1741 sctx->shader_pointers_dirty = u_bit_consecutive(0, SI_NUM_DESCS);
1742 sctx->vertex_buffer_pointer_dirty = sctx->vertex_buffers.buffer != NULL;
1743 si_mark_atom_dirty(sctx, &sctx->shader_userdata.atom);
1744 }
1745
1746 /* Set a base register address for user data constants in the given shader.
1747 * This assigns a mapping from PIPE_SHADER_* to SPI_SHADER_USER_DATA_*.
1748 */
1749 static void si_set_user_data_base(struct si_context *sctx,
1750 unsigned shader, uint32_t new_base)
1751 {
1752 uint32_t *base = &sctx->shader_userdata.sh_base[shader];
1753
1754 if (*base != new_base) {
1755 *base = new_base;
1756
1757 if (new_base)
1758 si_mark_shader_pointers_dirty(sctx, shader);
1759 }
1760 }
1761
1762 /* This must be called when these shaders are changed from non-NULL to NULL
1763 * and vice versa:
1764 * - geometry shader
1765 * - tessellation control shader
1766 * - tessellation evaluation shader
1767 */
1768 void si_shader_change_notify(struct si_context *sctx)
1769 {
1770 /* VS can be bound as VS, ES, or LS. */
1771 if (sctx->tes_shader.cso)
1772 si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
1773 R_00B530_SPI_SHADER_USER_DATA_LS_0);
1774 else if (sctx->gs_shader.cso)
1775 si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
1776 R_00B330_SPI_SHADER_USER_DATA_ES_0);
1777 else
1778 si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
1779 R_00B130_SPI_SHADER_USER_DATA_VS_0);
1780
1781 /* TES can be bound as ES, VS, or not bound. */
1782 if (sctx->tes_shader.cso) {
1783 if (sctx->gs_shader.cso)
1784 si_set_user_data_base(sctx, PIPE_SHADER_TESS_EVAL,
1785 R_00B330_SPI_SHADER_USER_DATA_ES_0);
1786 else
1787 si_set_user_data_base(sctx, PIPE_SHADER_TESS_EVAL,
1788 R_00B130_SPI_SHADER_USER_DATA_VS_0);
1789 } else {
1790 si_set_user_data_base(sctx, PIPE_SHADER_TESS_EVAL, 0);
1791 }
1792 }
1793
1794 static void si_emit_shader_pointer(struct si_context *sctx,
1795 struct si_descriptors *desc,
1796 unsigned sh_base)
1797 {
1798 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
1799 uint64_t va;
1800
1801 assert(desc->buffer);
1802
1803 va = desc->buffer->gpu_address +
1804 desc->buffer_offset;
1805
1806 radeon_emit(cs, PKT3(PKT3_SET_SH_REG, 2, 0));
1807 radeon_emit(cs, (sh_base + desc->shader_userdata_offset - SI_SH_REG_OFFSET) >> 2);
1808 radeon_emit(cs, va);
1809 radeon_emit(cs, va >> 32);
1810 }
1811
1812 void si_emit_graphics_shader_userdata(struct si_context *sctx,
1813 struct r600_atom *atom)
1814 {
1815 unsigned mask;
1816 uint32_t *sh_base = sctx->shader_userdata.sh_base;
1817 struct si_descriptors *descs;
1818
1819 descs = &sctx->descriptors[SI_DESCS_RW_BUFFERS];
1820
1821 if (sctx->shader_pointers_dirty & (1 << SI_DESCS_RW_BUFFERS)) {
1822 si_emit_shader_pointer(sctx, descs,
1823 R_00B030_SPI_SHADER_USER_DATA_PS_0);
1824 si_emit_shader_pointer(sctx, descs,
1825 R_00B130_SPI_SHADER_USER_DATA_VS_0);
1826 si_emit_shader_pointer(sctx, descs,
1827 R_00B230_SPI_SHADER_USER_DATA_GS_0);
1828 si_emit_shader_pointer(sctx, descs,
1829 R_00B330_SPI_SHADER_USER_DATA_ES_0);
1830 si_emit_shader_pointer(sctx, descs,
1831 R_00B430_SPI_SHADER_USER_DATA_HS_0);
1832 }
1833
1834 mask = sctx->shader_pointers_dirty &
1835 u_bit_consecutive(SI_DESCS_FIRST_SHADER,
1836 SI_DESCS_FIRST_COMPUTE - SI_DESCS_FIRST_SHADER);
1837
1838 while (mask) {
1839 unsigned i = u_bit_scan(&mask);
1840 unsigned shader = (i - SI_DESCS_FIRST_SHADER) / SI_NUM_SHADER_DESCS;
1841 unsigned base = sh_base[shader];
1842
1843 if (base)
1844 si_emit_shader_pointer(sctx, descs + i, base);
1845 }
1846 sctx->shader_pointers_dirty &=
1847 ~u_bit_consecutive(SI_DESCS_RW_BUFFERS, SI_DESCS_FIRST_COMPUTE);
1848
1849 if (sctx->vertex_buffer_pointer_dirty) {
1850 si_emit_shader_pointer(sctx, &sctx->vertex_buffers,
1851 sh_base[PIPE_SHADER_VERTEX]);
1852 sctx->vertex_buffer_pointer_dirty = false;
1853 }
1854 }
1855
1856 void si_emit_compute_shader_userdata(struct si_context *sctx)
1857 {
1858 unsigned base = R_00B900_COMPUTE_USER_DATA_0;
1859 struct si_descriptors *descs = sctx->descriptors;
1860 unsigned compute_mask =
1861 u_bit_consecutive(SI_DESCS_FIRST_COMPUTE, SI_NUM_SHADER_DESCS);
1862 unsigned mask = sctx->shader_pointers_dirty & compute_mask;
1863
1864 while (mask) {
1865 unsigned i = u_bit_scan(&mask);
1866
1867 si_emit_shader_pointer(sctx, descs + i, base);
1868 }
1869 sctx->shader_pointers_dirty &= ~compute_mask;
1870 }
1871
1872 /* INIT/DEINIT/UPLOAD */
1873
1874 void si_init_all_descriptors(struct si_context *sctx)
1875 {
1876 int i;
1877 unsigned ce_offset = 0;
1878
1879 for (i = 0; i < SI_NUM_SHADERS; i++) {
1880 si_init_buffer_resources(&sctx->const_buffers[i],
1881 si_const_buffer_descriptors(sctx, i),
1882 SI_NUM_CONST_BUFFERS, SI_SGPR_CONST_BUFFERS,
1883 RADEON_USAGE_READ, RADEON_PRIO_CONST_BUFFER,
1884 &ce_offset);
1885 si_init_buffer_resources(&sctx->shader_buffers[i],
1886 si_shader_buffer_descriptors(sctx, i),
1887 SI_NUM_SHADER_BUFFERS, SI_SGPR_SHADER_BUFFERS,
1888 RADEON_USAGE_READWRITE, RADEON_PRIO_SHADER_RW_BUFFER,
1889 &ce_offset);
1890
1891 si_init_descriptors(si_sampler_descriptors(sctx, i),
1892 SI_SGPR_SAMPLERS, 16, SI_NUM_SAMPLERS,
1893 null_texture_descriptor, &ce_offset);
1894
1895 si_init_descriptors(si_image_descriptors(sctx, i),
1896 SI_SGPR_IMAGES, 8, SI_NUM_IMAGES,
1897 null_image_descriptor, &ce_offset);
1898 }
1899
1900 si_init_buffer_resources(&sctx->rw_buffers,
1901 &sctx->descriptors[SI_DESCS_RW_BUFFERS],
1902 SI_NUM_RW_BUFFERS, SI_SGPR_RW_BUFFERS,
1903 RADEON_USAGE_READWRITE, RADEON_PRIO_SHADER_RINGS,
1904 &ce_offset);
1905 si_init_descriptors(&sctx->vertex_buffers, SI_SGPR_VERTEX_BUFFERS,
1906 4, SI_NUM_VERTEX_BUFFERS, NULL, NULL);
1907
1908 sctx->descriptors_dirty = u_bit_consecutive(0, SI_NUM_DESCS);
1909
1910 assert(ce_offset <= 32768);
1911
1912 /* Set pipe_context functions. */
1913 sctx->b.b.bind_sampler_states = si_bind_sampler_states;
1914 sctx->b.b.set_shader_images = si_set_shader_images;
1915 sctx->b.b.set_constant_buffer = si_pipe_set_constant_buffer;
1916 sctx->b.b.set_polygon_stipple = si_set_polygon_stipple;
1917 sctx->b.b.set_shader_buffers = si_set_shader_buffers;
1918 sctx->b.b.set_sampler_views = si_set_sampler_views;
1919 sctx->b.b.set_stream_output_targets = si_set_streamout_targets;
1920 sctx->b.invalidate_buffer = si_invalidate_buffer;
1921
1922 /* Shader user data. */
1923 si_init_atom(sctx, &sctx->shader_userdata.atom, &sctx->atoms.s.shader_userdata,
1924 si_emit_graphics_shader_userdata);
1925
1926 /* Set default and immutable mappings. */
1927 si_set_user_data_base(sctx, PIPE_SHADER_VERTEX, R_00B130_SPI_SHADER_USER_DATA_VS_0);
1928 si_set_user_data_base(sctx, PIPE_SHADER_TESS_CTRL, R_00B430_SPI_SHADER_USER_DATA_HS_0);
1929 si_set_user_data_base(sctx, PIPE_SHADER_GEOMETRY, R_00B230_SPI_SHADER_USER_DATA_GS_0);
1930 si_set_user_data_base(sctx, PIPE_SHADER_FRAGMENT, R_00B030_SPI_SHADER_USER_DATA_PS_0);
1931 }
1932
1933 bool si_upload_graphics_shader_descriptors(struct si_context *sctx)
1934 {
1935 const unsigned mask = u_bit_consecutive(0, SI_DESCS_FIRST_COMPUTE);
1936 unsigned dirty = sctx->descriptors_dirty & mask;
1937
1938 /* Assume nothing will go wrong: */
1939 sctx->shader_pointers_dirty |= dirty;
1940
1941 while (dirty) {
1942 unsigned i = u_bit_scan(&dirty);
1943
1944 if (!si_upload_descriptors(sctx, &sctx->descriptors[i],
1945 &sctx->shader_userdata.atom))
1946 return false;
1947 }
1948
1949 sctx->descriptors_dirty &= ~mask;
1950 return true;
1951 }
1952
1953 bool si_upload_compute_shader_descriptors(struct si_context *sctx)
1954 {
1955 /* Does not update rw_buffers as that is not needed for compute shaders
1956 * and the input buffer is using the same SGPR's anyway.
1957 */
1958 const unsigned mask = u_bit_consecutive(SI_DESCS_FIRST_COMPUTE,
1959 SI_NUM_DESCS - SI_DESCS_FIRST_COMPUTE);
1960 unsigned dirty = sctx->descriptors_dirty & mask;
1961
1962 /* Assume nothing will go wrong: */
1963 sctx->shader_pointers_dirty |= dirty;
1964
1965 while (dirty) {
1966 unsigned i = u_bit_scan(&dirty);
1967
1968 if (!si_upload_descriptors(sctx, &sctx->descriptors[i], NULL))
1969 return false;
1970 }
1971
1972 sctx->descriptors_dirty &= ~mask;
1973
1974 return true;
1975 }
1976
1977 void si_release_all_descriptors(struct si_context *sctx)
1978 {
1979 int i;
1980
1981 for (i = 0; i < SI_NUM_SHADERS; i++) {
1982 si_release_buffer_resources(&sctx->const_buffers[i],
1983 si_const_buffer_descriptors(sctx, i));
1984 si_release_buffer_resources(&sctx->shader_buffers[i],
1985 si_shader_buffer_descriptors(sctx, i));
1986 si_release_sampler_views(&sctx->samplers[i].views);
1987 si_release_image_views(&sctx->images[i]);
1988 }
1989 si_release_buffer_resources(&sctx->rw_buffers,
1990 &sctx->descriptors[SI_DESCS_RW_BUFFERS]);
1991
1992 for (i = 0; i < SI_NUM_DESCS; ++i)
1993 si_release_descriptors(&sctx->descriptors[i]);
1994 si_release_descriptors(&sctx->vertex_buffers);
1995 }
1996
1997 void si_all_descriptors_begin_new_cs(struct si_context *sctx)
1998 {
1999 int i;
2000
2001 for (i = 0; i < SI_NUM_SHADERS; i++) {
2002 si_buffer_resources_begin_new_cs(sctx, &sctx->const_buffers[i]);
2003 si_buffer_resources_begin_new_cs(sctx, &sctx->shader_buffers[i]);
2004 si_sampler_views_begin_new_cs(sctx, &sctx->samplers[i].views);
2005 si_image_views_begin_new_cs(sctx, &sctx->images[i]);
2006 }
2007 si_buffer_resources_begin_new_cs(sctx, &sctx->rw_buffers);
2008 si_vertex_buffers_begin_new_cs(sctx);
2009
2010 for (i = 0; i < SI_NUM_DESCS; ++i)
2011 si_descriptors_begin_new_cs(sctx, &sctx->descriptors[i]);
2012
2013 si_shader_userdata_begin_new_cs(sctx);
2014 }