radeonsi: update copyrights
[mesa.git] / src / gallium / drivers / radeonsi / si_descriptors.c
1 /*
2 * Copyright 2013 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /* Resource binding slots and sampler states (each described with 8 or
26 * 4 dwords) are stored in lists in memory which is accessed by shaders
27 * using scalar load instructions.
28 *
29 * This file is responsible for managing such lists. It keeps a copy of all
30 * descriptors in CPU memory and re-uploads a whole list if some slots have
31 * been changed.
32 *
33 * This code is also reponsible for updating shader pointers to those lists.
34 *
35 * Note that CP DMA can't be used for updating the lists, because a GPU hang
36 * could leave the list in a mid-IB state and the next IB would get wrong
37 * descriptors and the whole context would be unusable at that point.
38 * (Note: The register shadowing can't be used due to the same reason)
39 *
40 * Also, uploading descriptors to newly allocated memory doesn't require
41 * a KCACHE flush.
42 *
43 *
44 * Possible scenarios for one 16 dword image+sampler slot:
45 *
46 * | Image | w/ FMASK | Buffer | NULL
47 * [ 0: 3] Image[0:3] | Image[0:3] | Null[0:3] | Null[0:3]
48 * [ 4: 7] Image[4:7] | Image[4:7] | Buffer[0:3] | 0
49 * [ 8:11] Null[0:3] | Fmask[0:3] | Null[0:3] | Null[0:3]
50 * [12:15] Sampler[0:3] | Fmask[4:7] | Sampler[0:3] | Sampler[0:3]
51 *
52 * FMASK implies MSAA, therefore no sampler state.
53 * Sampler states are never unbound except when FMASK is bound.
54 */
55
56 #include "radeon/r600_cs.h"
57 #include "si_pipe.h"
58 #include "sid.h"
59 #include "gfx9d.h"
60
61 #include "util/hash_table.h"
62 #include "util/u_idalloc.h"
63 #include "util/u_format.h"
64 #include "util/u_memory.h"
65 #include "util/u_upload_mgr.h"
66
67
68 /* NULL image and buffer descriptor for textures (alpha = 1) and images
69 * (alpha = 0).
70 *
71 * For images, all fields must be zero except for the swizzle, which
72 * supports arbitrary combinations of 0s and 1s. The texture type must be
73 * any valid type (e.g. 1D). If the texture type isn't set, the hw hangs.
74 *
75 * For buffers, all fields must be zero. If they are not, the hw hangs.
76 *
77 * This is the only reason why the buffer descriptor must be in words [4:7].
78 */
79 static uint32_t null_texture_descriptor[8] = {
80 0,
81 0,
82 0,
83 S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_1) |
84 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D)
85 /* the rest must contain zeros, which is also used by the buffer
86 * descriptor */
87 };
88
89 static uint32_t null_image_descriptor[8] = {
90 0,
91 0,
92 0,
93 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D)
94 /* the rest must contain zeros, which is also used by the buffer
95 * descriptor */
96 };
97
98 static uint64_t si_desc_extract_buffer_address(uint32_t *desc)
99 {
100 uint64_t va = desc[0] |
101 ((uint64_t)G_008F04_BASE_ADDRESS_HI(desc[1]) << 32);
102
103 /* Sign-extend the 48-bit address. */
104 if (va & (1ull << 47))
105 va |= 0xffffull << 48;
106 return va;
107 }
108
109 static void si_init_descriptor_list(uint32_t *desc_list,
110 unsigned element_dw_size,
111 unsigned num_elements,
112 const uint32_t *null_descriptor)
113 {
114 int i;
115
116 /* Initialize the array to NULL descriptors if the element size is 8. */
117 if (null_descriptor) {
118 assert(element_dw_size % 8 == 0);
119 for (i = 0; i < num_elements * element_dw_size / 8; i++)
120 memcpy(desc_list + i * 8, null_descriptor, 8 * 4);
121 }
122 }
123
124 static void si_init_descriptors(struct si_descriptors *desc,
125 short shader_userdata_rel_index,
126 unsigned element_dw_size,
127 unsigned num_elements)
128 {
129 desc->list = CALLOC(num_elements, element_dw_size * 4);
130 desc->element_dw_size = element_dw_size;
131 desc->num_elements = num_elements;
132 desc->shader_userdata_offset = shader_userdata_rel_index * 4;
133 desc->slot_index_to_bind_directly = -1;
134 }
135
136 static void si_release_descriptors(struct si_descriptors *desc)
137 {
138 r600_resource_reference(&desc->buffer, NULL);
139 FREE(desc->list);
140 }
141
142 static bool si_upload_descriptors(struct si_context *sctx,
143 struct si_descriptors *desc)
144 {
145 unsigned slot_size = desc->element_dw_size * 4;
146 unsigned first_slot_offset = desc->first_active_slot * slot_size;
147 unsigned upload_size = desc->num_active_slots * slot_size;
148
149 /* Skip the upload if no shader is using the descriptors. dirty_mask
150 * will stay dirty and the descriptors will be uploaded when there is
151 * a shader using them.
152 */
153 if (!upload_size)
154 return true;
155
156 /* If there is just one active descriptor, bind it directly. */
157 if ((int)desc->first_active_slot == desc->slot_index_to_bind_directly &&
158 desc->num_active_slots == 1) {
159 uint32_t *descriptor = &desc->list[desc->slot_index_to_bind_directly *
160 desc->element_dw_size];
161
162 /* The buffer is already in the buffer list. */
163 r600_resource_reference(&desc->buffer, NULL);
164 desc->gpu_list = NULL;
165 desc->gpu_address = si_desc_extract_buffer_address(descriptor);
166 si_mark_atom_dirty(sctx, &sctx->shader_pointers.atom);
167 return true;
168 }
169
170 uint32_t *ptr;
171 unsigned buffer_offset;
172 u_upload_alloc(sctx->b.b.const_uploader, first_slot_offset, upload_size,
173 si_optimal_tcc_alignment(sctx, upload_size),
174 &buffer_offset, (struct pipe_resource**)&desc->buffer,
175 (void**)&ptr);
176 if (!desc->buffer) {
177 desc->gpu_address = 0;
178 return false; /* skip the draw call */
179 }
180
181 util_memcpy_cpu_to_le32(ptr, (char*)desc->list + first_slot_offset,
182 upload_size);
183 desc->gpu_list = ptr - first_slot_offset / 4;
184
185 radeon_add_to_buffer_list(sctx, sctx->b.gfx_cs, desc->buffer,
186 RADEON_USAGE_READ, RADEON_PRIO_DESCRIPTORS);
187
188 /* The shader pointer should point to slot 0. */
189 buffer_offset -= first_slot_offset;
190 desc->gpu_address = desc->buffer->gpu_address + buffer_offset;
191
192 assert(desc->buffer->flags & RADEON_FLAG_32BIT);
193 assert((desc->buffer->gpu_address >> 32) == sctx->screen->info.address32_hi);
194 assert((desc->gpu_address >> 32) == sctx->screen->info.address32_hi);
195
196 si_mark_atom_dirty(sctx, &sctx->shader_pointers.atom);
197 return true;
198 }
199
200 static void
201 si_descriptors_begin_new_cs(struct si_context *sctx, struct si_descriptors *desc)
202 {
203 if (!desc->buffer)
204 return;
205
206 radeon_add_to_buffer_list(sctx, sctx->b.gfx_cs, desc->buffer,
207 RADEON_USAGE_READ, RADEON_PRIO_DESCRIPTORS);
208 }
209
210 /* SAMPLER VIEWS */
211
212 static inline enum radeon_bo_priority
213 si_get_sampler_view_priority(struct r600_resource *res)
214 {
215 if (res->b.b.target == PIPE_BUFFER)
216 return RADEON_PRIO_SAMPLER_BUFFER;
217
218 if (res->b.b.nr_samples > 1)
219 return RADEON_PRIO_SAMPLER_TEXTURE_MSAA;
220
221 return RADEON_PRIO_SAMPLER_TEXTURE;
222 }
223
224 static unsigned
225 si_sampler_and_image_descriptors_idx(unsigned shader)
226 {
227 return SI_DESCS_FIRST_SHADER + shader * SI_NUM_SHADER_DESCS +
228 SI_SHADER_DESCS_SAMPLERS_AND_IMAGES;
229 }
230
231 static struct si_descriptors *
232 si_sampler_and_image_descriptors(struct si_context *sctx, unsigned shader)
233 {
234 return &sctx->descriptors[si_sampler_and_image_descriptors_idx(shader)];
235 }
236
237 static void si_release_sampler_views(struct si_samplers *samplers)
238 {
239 int i;
240
241 for (i = 0; i < ARRAY_SIZE(samplers->views); i++) {
242 pipe_sampler_view_reference(&samplers->views[i], NULL);
243 }
244 }
245
246 static void si_sampler_view_add_buffer(struct si_context *sctx,
247 struct pipe_resource *resource,
248 enum radeon_bo_usage usage,
249 bool is_stencil_sampler,
250 bool check_mem)
251 {
252 struct r600_resource *rres;
253 struct r600_texture *rtex;
254 enum radeon_bo_priority priority;
255
256 if (!resource)
257 return;
258
259 if (resource->target != PIPE_BUFFER) {
260 struct r600_texture *tex = (struct r600_texture*)resource;
261
262 if (tex->is_depth && !si_can_sample_zs(tex, is_stencil_sampler))
263 resource = &tex->flushed_depth_texture->resource.b.b;
264 }
265
266 rres = (struct r600_resource*)resource;
267 priority = si_get_sampler_view_priority(rres);
268
269 radeon_add_to_gfx_buffer_list_check_mem(sctx, rres, usage, priority,
270 check_mem);
271
272 if (resource->target == PIPE_BUFFER)
273 return;
274
275 /* Now add separate DCC or HTILE. */
276 rtex = (struct r600_texture*)resource;
277 if (rtex->dcc_separate_buffer) {
278 radeon_add_to_gfx_buffer_list_check_mem(sctx, rtex->dcc_separate_buffer,
279 usage, RADEON_PRIO_DCC, check_mem);
280 }
281 }
282
283 static void si_sampler_views_begin_new_cs(struct si_context *sctx,
284 struct si_samplers *samplers)
285 {
286 unsigned mask = samplers->enabled_mask;
287
288 /* Add buffers to the CS. */
289 while (mask) {
290 int i = u_bit_scan(&mask);
291 struct si_sampler_view *sview = (struct si_sampler_view *)samplers->views[i];
292
293 si_sampler_view_add_buffer(sctx, sview->base.texture,
294 RADEON_USAGE_READ,
295 sview->is_stencil_sampler, false);
296 }
297 }
298
299 /* Set buffer descriptor fields that can be changed by reallocations. */
300 static void si_set_buf_desc_address(struct r600_resource *buf,
301 uint64_t offset, uint32_t *state)
302 {
303 uint64_t va = buf->gpu_address + offset;
304
305 state[0] = va;
306 state[1] &= C_008F04_BASE_ADDRESS_HI;
307 state[1] |= S_008F04_BASE_ADDRESS_HI(va >> 32);
308 }
309
310 /* Set texture descriptor fields that can be changed by reallocations.
311 *
312 * \param tex texture
313 * \param base_level_info information of the level of BASE_ADDRESS
314 * \param base_level the level of BASE_ADDRESS
315 * \param first_level pipe_sampler_view.u.tex.first_level
316 * \param block_width util_format_get_blockwidth()
317 * \param is_stencil select between separate Z & Stencil
318 * \param state descriptor to update
319 */
320 void si_set_mutable_tex_desc_fields(struct si_screen *sscreen,
321 struct r600_texture *tex,
322 const struct legacy_surf_level *base_level_info,
323 unsigned base_level, unsigned first_level,
324 unsigned block_width, bool is_stencil,
325 uint32_t *state)
326 {
327 uint64_t va, meta_va = 0;
328
329 if (tex->is_depth && !si_can_sample_zs(tex, is_stencil)) {
330 tex = tex->flushed_depth_texture;
331 is_stencil = false;
332 }
333
334 va = tex->resource.gpu_address;
335
336 if (sscreen->info.chip_class >= GFX9) {
337 /* Only stencil_offset needs to be added here. */
338 if (is_stencil)
339 va += tex->surface.u.gfx9.stencil_offset;
340 else
341 va += tex->surface.u.gfx9.surf_offset;
342 } else {
343 va += base_level_info->offset;
344 }
345
346 state[0] = va >> 8;
347 state[1] &= C_008F14_BASE_ADDRESS_HI;
348 state[1] |= S_008F14_BASE_ADDRESS_HI(va >> 40);
349
350 /* Only macrotiled modes can set tile swizzle.
351 * GFX9 doesn't use (legacy) base_level_info.
352 */
353 if (sscreen->info.chip_class >= GFX9 ||
354 base_level_info->mode == RADEON_SURF_MODE_2D)
355 state[0] |= tex->surface.tile_swizzle;
356
357 if (sscreen->info.chip_class >= VI) {
358 state[6] &= C_008F28_COMPRESSION_EN;
359 state[7] = 0;
360
361 if (vi_dcc_enabled(tex, first_level)) {
362 meta_va = (!tex->dcc_separate_buffer ? tex->resource.gpu_address : 0) +
363 tex->dcc_offset;
364
365 if (sscreen->info.chip_class == VI) {
366 meta_va += base_level_info->dcc_offset;
367 assert(base_level_info->mode == RADEON_SURF_MODE_2D);
368 }
369
370 meta_va |= (uint32_t)tex->surface.tile_swizzle << 8;
371 } else if (vi_tc_compat_htile_enabled(tex, first_level)) {
372 meta_va = tex->resource.gpu_address + tex->htile_offset;
373 }
374
375 if (meta_va) {
376 state[6] |= S_008F28_COMPRESSION_EN(1);
377 state[7] = meta_va >> 8;
378 }
379 }
380
381 if (sscreen->info.chip_class >= GFX9) {
382 state[3] &= C_008F1C_SW_MODE;
383 state[4] &= C_008F20_PITCH_GFX9;
384
385 if (is_stencil) {
386 state[3] |= S_008F1C_SW_MODE(tex->surface.u.gfx9.stencil.swizzle_mode);
387 state[4] |= S_008F20_PITCH_GFX9(tex->surface.u.gfx9.stencil.epitch);
388 } else {
389 state[3] |= S_008F1C_SW_MODE(tex->surface.u.gfx9.surf.swizzle_mode);
390 state[4] |= S_008F20_PITCH_GFX9(tex->surface.u.gfx9.surf.epitch);
391 }
392
393 state[5] &= C_008F24_META_DATA_ADDRESS &
394 C_008F24_META_PIPE_ALIGNED &
395 C_008F24_META_RB_ALIGNED;
396 if (meta_va) {
397 struct gfx9_surf_meta_flags meta;
398
399 if (tex->dcc_offset)
400 meta = tex->surface.u.gfx9.dcc;
401 else
402 meta = tex->surface.u.gfx9.htile;
403
404 state[5] |= S_008F24_META_DATA_ADDRESS(meta_va >> 40) |
405 S_008F24_META_PIPE_ALIGNED(meta.pipe_aligned) |
406 S_008F24_META_RB_ALIGNED(meta.rb_aligned);
407 }
408 } else {
409 /* SI-CI-VI */
410 unsigned pitch = base_level_info->nblk_x * block_width;
411 unsigned index = si_tile_mode_index(tex, base_level, is_stencil);
412
413 state[3] &= C_008F1C_TILING_INDEX;
414 state[3] |= S_008F1C_TILING_INDEX(index);
415 state[4] &= C_008F20_PITCH_GFX6;
416 state[4] |= S_008F20_PITCH_GFX6(pitch - 1);
417 }
418 }
419
420 static void si_set_sampler_state_desc(struct si_sampler_state *sstate,
421 struct si_sampler_view *sview,
422 struct r600_texture *tex,
423 uint32_t *desc)
424 {
425 if (sview && sview->is_integer)
426 memcpy(desc, sstate->integer_val, 4*4);
427 else if (tex && tex->upgraded_depth &&
428 (!sview || !sview->is_stencil_sampler))
429 memcpy(desc, sstate->upgraded_depth_val, 4*4);
430 else
431 memcpy(desc, sstate->val, 4*4);
432 }
433
434 static void si_set_sampler_view_desc(struct si_context *sctx,
435 struct si_sampler_view *sview,
436 struct si_sampler_state *sstate,
437 uint32_t *desc)
438 {
439 struct pipe_sampler_view *view = &sview->base;
440 struct r600_texture *rtex = (struct r600_texture *)view->texture;
441 bool is_buffer = rtex->resource.b.b.target == PIPE_BUFFER;
442
443 if (unlikely(!is_buffer && sview->dcc_incompatible)) {
444 if (vi_dcc_enabled(rtex, view->u.tex.first_level))
445 if (!si_texture_disable_dcc(sctx, rtex))
446 si_decompress_dcc(&sctx->b.b, rtex);
447
448 sview->dcc_incompatible = false;
449 }
450
451 assert(rtex); /* views with texture == NULL aren't supported */
452 memcpy(desc, sview->state, 8*4);
453
454 if (is_buffer) {
455 si_set_buf_desc_address(&rtex->resource,
456 sview->base.u.buf.offset,
457 desc + 4);
458 } else {
459 bool is_separate_stencil = rtex->db_compatible &&
460 sview->is_stencil_sampler;
461
462 si_set_mutable_tex_desc_fields(sctx->screen, rtex,
463 sview->base_level_info,
464 sview->base_level,
465 sview->base.u.tex.first_level,
466 sview->block_width,
467 is_separate_stencil,
468 desc);
469 }
470
471 if (!is_buffer && rtex->fmask.size) {
472 memcpy(desc + 8, sview->fmask_state, 8*4);
473 } else {
474 /* Disable FMASK and bind sampler state in [12:15]. */
475 memcpy(desc + 8, null_texture_descriptor, 4*4);
476
477 if (sstate)
478 si_set_sampler_state_desc(sstate, sview,
479 is_buffer ? NULL : rtex,
480 desc + 12);
481 }
482 }
483
484 static bool color_needs_decompression(struct r600_texture *rtex)
485 {
486 return rtex->fmask.size ||
487 (rtex->dirty_level_mask &&
488 (rtex->cmask.size || rtex->dcc_offset));
489 }
490
491 static bool depth_needs_decompression(struct r600_texture *rtex)
492 {
493 /* If the depth/stencil texture is TC-compatible, no decompression
494 * will be done. The decompression function will only flush DB caches
495 * to make it coherent with shaders. That's necessary because the driver
496 * doesn't flush DB caches in any other case.
497 */
498 return rtex->db_compatible;
499 }
500
501 static void si_set_sampler_view(struct si_context *sctx,
502 unsigned shader,
503 unsigned slot, struct pipe_sampler_view *view,
504 bool disallow_early_out)
505 {
506 struct si_samplers *samplers = &sctx->samplers[shader];
507 struct si_sampler_view *rview = (struct si_sampler_view*)view;
508 struct si_descriptors *descs = si_sampler_and_image_descriptors(sctx, shader);
509 unsigned desc_slot = si_get_sampler_slot(slot);
510 uint32_t *desc = descs->list + desc_slot * 16;
511
512 if (samplers->views[slot] == view && !disallow_early_out)
513 return;
514
515 if (view) {
516 struct r600_texture *rtex = (struct r600_texture *)view->texture;
517
518 si_set_sampler_view_desc(sctx, rview,
519 samplers->sampler_states[slot], desc);
520
521 if (rtex->resource.b.b.target == PIPE_BUFFER) {
522 rtex->resource.bind_history |= PIPE_BIND_SAMPLER_VIEW;
523 samplers->needs_depth_decompress_mask &= ~(1u << slot);
524 samplers->needs_color_decompress_mask &= ~(1u << slot);
525 } else {
526 if (depth_needs_decompression(rtex)) {
527 samplers->needs_depth_decompress_mask |= 1u << slot;
528 } else {
529 samplers->needs_depth_decompress_mask &= ~(1u << slot);
530 }
531 if (color_needs_decompression(rtex)) {
532 samplers->needs_color_decompress_mask |= 1u << slot;
533 } else {
534 samplers->needs_color_decompress_mask &= ~(1u << slot);
535 }
536
537 if (rtex->dcc_offset &&
538 p_atomic_read(&rtex->framebuffers_bound))
539 sctx->need_check_render_feedback = true;
540 }
541
542 pipe_sampler_view_reference(&samplers->views[slot], view);
543 samplers->enabled_mask |= 1u << slot;
544
545 /* Since this can flush, it must be done after enabled_mask is
546 * updated. */
547 si_sampler_view_add_buffer(sctx, view->texture,
548 RADEON_USAGE_READ,
549 rview->is_stencil_sampler, true);
550 } else {
551 pipe_sampler_view_reference(&samplers->views[slot], NULL);
552 memcpy(desc, null_texture_descriptor, 8*4);
553 /* Only clear the lower dwords of FMASK. */
554 memcpy(desc + 8, null_texture_descriptor, 4*4);
555 /* Re-set the sampler state if we are transitioning from FMASK. */
556 if (samplers->sampler_states[slot])
557 si_set_sampler_state_desc(samplers->sampler_states[slot], NULL, NULL,
558 desc + 12);
559
560 samplers->enabled_mask &= ~(1u << slot);
561 samplers->needs_depth_decompress_mask &= ~(1u << slot);
562 samplers->needs_color_decompress_mask &= ~(1u << slot);
563 }
564
565 sctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
566 }
567
568 static void si_update_shader_needs_decompress_mask(struct si_context *sctx,
569 unsigned shader)
570 {
571 struct si_samplers *samplers = &sctx->samplers[shader];
572 unsigned shader_bit = 1 << shader;
573
574 if (samplers->needs_depth_decompress_mask ||
575 samplers->needs_color_decompress_mask ||
576 sctx->images[shader].needs_color_decompress_mask)
577 sctx->shader_needs_decompress_mask |= shader_bit;
578 else
579 sctx->shader_needs_decompress_mask &= ~shader_bit;
580 }
581
582 static void si_set_sampler_views(struct pipe_context *ctx,
583 enum pipe_shader_type shader, unsigned start,
584 unsigned count,
585 struct pipe_sampler_view **views)
586 {
587 struct si_context *sctx = (struct si_context *)ctx;
588 int i;
589
590 if (!count || shader >= SI_NUM_SHADERS)
591 return;
592
593 if (views) {
594 for (i = 0; i < count; i++)
595 si_set_sampler_view(sctx, shader, start + i, views[i], false);
596 } else {
597 for (i = 0; i < count; i++)
598 si_set_sampler_view(sctx, shader, start + i, NULL, false);
599 }
600
601 si_update_shader_needs_decompress_mask(sctx, shader);
602 }
603
604 static void
605 si_samplers_update_needs_color_decompress_mask(struct si_samplers *samplers)
606 {
607 unsigned mask = samplers->enabled_mask;
608
609 while (mask) {
610 int i = u_bit_scan(&mask);
611 struct pipe_resource *res = samplers->views[i]->texture;
612
613 if (res && res->target != PIPE_BUFFER) {
614 struct r600_texture *rtex = (struct r600_texture *)res;
615
616 if (color_needs_decompression(rtex)) {
617 samplers->needs_color_decompress_mask |= 1u << i;
618 } else {
619 samplers->needs_color_decompress_mask &= ~(1u << i);
620 }
621 }
622 }
623 }
624
625 /* IMAGE VIEWS */
626
627 static void
628 si_release_image_views(struct si_images *images)
629 {
630 unsigned i;
631
632 for (i = 0; i < SI_NUM_IMAGES; ++i) {
633 struct pipe_image_view *view = &images->views[i];
634
635 pipe_resource_reference(&view->resource, NULL);
636 }
637 }
638
639 static void
640 si_image_views_begin_new_cs(struct si_context *sctx, struct si_images *images)
641 {
642 uint mask = images->enabled_mask;
643
644 /* Add buffers to the CS. */
645 while (mask) {
646 int i = u_bit_scan(&mask);
647 struct pipe_image_view *view = &images->views[i];
648
649 assert(view->resource);
650
651 si_sampler_view_add_buffer(sctx, view->resource,
652 RADEON_USAGE_READWRITE, false, false);
653 }
654 }
655
656 static void
657 si_disable_shader_image(struct si_context *ctx, unsigned shader, unsigned slot)
658 {
659 struct si_images *images = &ctx->images[shader];
660
661 if (images->enabled_mask & (1u << slot)) {
662 struct si_descriptors *descs = si_sampler_and_image_descriptors(ctx, shader);
663 unsigned desc_slot = si_get_image_slot(slot);
664
665 pipe_resource_reference(&images->views[slot].resource, NULL);
666 images->needs_color_decompress_mask &= ~(1 << slot);
667
668 memcpy(descs->list + desc_slot*8, null_image_descriptor, 8*4);
669 images->enabled_mask &= ~(1u << slot);
670 ctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
671 }
672 }
673
674 static void
675 si_mark_image_range_valid(const struct pipe_image_view *view)
676 {
677 struct r600_resource *res = (struct r600_resource *)view->resource;
678
679 assert(res && res->b.b.target == PIPE_BUFFER);
680
681 util_range_add(&res->valid_buffer_range,
682 view->u.buf.offset,
683 view->u.buf.offset + view->u.buf.size);
684 }
685
686 static void si_set_shader_image_desc(struct si_context *ctx,
687 const struct pipe_image_view *view,
688 bool skip_decompress,
689 uint32_t *desc, uint32_t *fmask_desc)
690 {
691 struct si_screen *screen = ctx->screen;
692 struct r600_resource *res;
693
694 res = (struct r600_resource *)view->resource;
695
696 if (res->b.b.target == PIPE_BUFFER) {
697 if (view->access & PIPE_IMAGE_ACCESS_WRITE)
698 si_mark_image_range_valid(view);
699
700 si_make_buffer_descriptor(screen, res,
701 view->format,
702 view->u.buf.offset,
703 view->u.buf.size, desc);
704 si_set_buf_desc_address(res, view->u.buf.offset, desc + 4);
705 } else {
706 static const unsigned char swizzle[4] = { 0, 1, 2, 3 };
707 struct r600_texture *tex = (struct r600_texture *)res;
708 unsigned level = view->u.tex.level;
709 unsigned width, height, depth, hw_level;
710 bool uses_dcc = vi_dcc_enabled(tex, level);
711 unsigned access = view->access;
712
713 /* Clear the write flag when writes can't occur.
714 * Note that DCC_DECOMPRESS for MSAA doesn't work in some cases,
715 * so we don't wanna trigger it.
716 */
717 if (tex->is_depth ||
718 (!fmask_desc && tex->fmask.size != 0)) {
719 assert(!"Z/S and MSAA image stores are not supported");
720 access &= ~PIPE_IMAGE_ACCESS_WRITE;
721 }
722
723 assert(!tex->is_depth);
724 assert(fmask_desc || tex->fmask.size == 0);
725
726 if (uses_dcc && !skip_decompress &&
727 (view->access & PIPE_IMAGE_ACCESS_WRITE ||
728 !vi_dcc_formats_compatible(res->b.b.format, view->format))) {
729 /* If DCC can't be disabled, at least decompress it.
730 * The decompression is relatively cheap if the surface
731 * has been decompressed already.
732 */
733 if (!si_texture_disable_dcc(ctx, tex))
734 si_decompress_dcc(&ctx->b.b, tex);
735 }
736
737 if (ctx->b.chip_class >= GFX9) {
738 /* Always set the base address. The swizzle modes don't
739 * allow setting mipmap level offsets as the base.
740 */
741 width = res->b.b.width0;
742 height = res->b.b.height0;
743 depth = res->b.b.depth0;
744 hw_level = level;
745 } else {
746 /* Always force the base level to the selected level.
747 *
748 * This is required for 3D textures, where otherwise
749 * selecting a single slice for non-layered bindings
750 * fails. It doesn't hurt the other targets.
751 */
752 width = u_minify(res->b.b.width0, level);
753 height = u_minify(res->b.b.height0, level);
754 depth = u_minify(res->b.b.depth0, level);
755 hw_level = 0;
756 }
757
758 si_make_texture_descriptor(screen, tex,
759 false, res->b.b.target,
760 view->format, swizzle,
761 hw_level, hw_level,
762 view->u.tex.first_layer,
763 view->u.tex.last_layer,
764 width, height, depth,
765 desc, fmask_desc);
766 si_set_mutable_tex_desc_fields(screen, tex,
767 &tex->surface.u.legacy.level[level],
768 level, level,
769 util_format_get_blockwidth(view->format),
770 false, desc);
771 }
772 }
773
774 static void si_set_shader_image(struct si_context *ctx,
775 unsigned shader,
776 unsigned slot, const struct pipe_image_view *view,
777 bool skip_decompress)
778 {
779 struct si_images *images = &ctx->images[shader];
780 struct si_descriptors *descs = si_sampler_and_image_descriptors(ctx, shader);
781 struct r600_resource *res;
782 unsigned desc_slot = si_get_image_slot(slot);
783 uint32_t *desc = descs->list + desc_slot * 8;
784
785 if (!view || !view->resource) {
786 si_disable_shader_image(ctx, shader, slot);
787 return;
788 }
789
790 res = (struct r600_resource *)view->resource;
791
792 if (&images->views[slot] != view)
793 util_copy_image_view(&images->views[slot], view);
794
795 si_set_shader_image_desc(ctx, view, skip_decompress, desc, NULL);
796
797 if (res->b.b.target == PIPE_BUFFER) {
798 images->needs_color_decompress_mask &= ~(1 << slot);
799 res->bind_history |= PIPE_BIND_SHADER_IMAGE;
800 } else {
801 struct r600_texture *tex = (struct r600_texture *)res;
802 unsigned level = view->u.tex.level;
803
804 if (color_needs_decompression(tex)) {
805 images->needs_color_decompress_mask |= 1 << slot;
806 } else {
807 images->needs_color_decompress_mask &= ~(1 << slot);
808 }
809
810 if (vi_dcc_enabled(tex, level) &&
811 p_atomic_read(&tex->framebuffers_bound))
812 ctx->need_check_render_feedback = true;
813 }
814
815 images->enabled_mask |= 1u << slot;
816 ctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
817
818 /* Since this can flush, it must be done after enabled_mask is updated. */
819 si_sampler_view_add_buffer(ctx, &res->b.b,
820 (view->access & PIPE_IMAGE_ACCESS_WRITE) ?
821 RADEON_USAGE_READWRITE : RADEON_USAGE_READ,
822 false, true);
823 }
824
825 static void
826 si_set_shader_images(struct pipe_context *pipe,
827 enum pipe_shader_type shader,
828 unsigned start_slot, unsigned count,
829 const struct pipe_image_view *views)
830 {
831 struct si_context *ctx = (struct si_context *)pipe;
832 unsigned i, slot;
833
834 assert(shader < SI_NUM_SHADERS);
835
836 if (!count)
837 return;
838
839 assert(start_slot + count <= SI_NUM_IMAGES);
840
841 if (views) {
842 for (i = 0, slot = start_slot; i < count; ++i, ++slot)
843 si_set_shader_image(ctx, shader, slot, &views[i], false);
844 } else {
845 for (i = 0, slot = start_slot; i < count; ++i, ++slot)
846 si_set_shader_image(ctx, shader, slot, NULL, false);
847 }
848
849 si_update_shader_needs_decompress_mask(ctx, shader);
850 }
851
852 static void
853 si_images_update_needs_color_decompress_mask(struct si_images *images)
854 {
855 unsigned mask = images->enabled_mask;
856
857 while (mask) {
858 int i = u_bit_scan(&mask);
859 struct pipe_resource *res = images->views[i].resource;
860
861 if (res && res->target != PIPE_BUFFER) {
862 struct r600_texture *rtex = (struct r600_texture *)res;
863
864 if (color_needs_decompression(rtex)) {
865 images->needs_color_decompress_mask |= 1 << i;
866 } else {
867 images->needs_color_decompress_mask &= ~(1 << i);
868 }
869 }
870 }
871 }
872
873 void si_update_ps_colorbuf0_slot(struct si_context *sctx)
874 {
875 struct si_buffer_resources *buffers = &sctx->rw_buffers;
876 struct si_descriptors *descs = &sctx->descriptors[SI_DESCS_RW_BUFFERS];
877 unsigned slot = SI_PS_IMAGE_COLORBUF0;
878 struct pipe_surface *surf = NULL;
879
880 /* si_texture_disable_dcc can get us here again. */
881 if (sctx->blitter->running)
882 return;
883
884 /* See whether FBFETCH is used and color buffer 0 is set. */
885 if (sctx->ps_shader.cso &&
886 sctx->ps_shader.cso->info.opcode_count[TGSI_OPCODE_FBFETCH] &&
887 sctx->framebuffer.state.nr_cbufs &&
888 sctx->framebuffer.state.cbufs[0])
889 surf = sctx->framebuffer.state.cbufs[0];
890
891 /* Return if FBFETCH transitions from disabled to disabled. */
892 if (!buffers->buffers[slot] && !surf)
893 return;
894
895 sctx->ps_uses_fbfetch = surf != NULL;
896 si_update_ps_iter_samples(sctx);
897
898 if (surf) {
899 struct r600_texture *tex = (struct r600_texture*)surf->texture;
900 struct pipe_image_view view;
901
902 assert(tex);
903 assert(!tex->is_depth);
904
905 /* Disable DCC, because the texture is used as both a sampler
906 * and color buffer.
907 */
908 si_texture_disable_dcc(sctx, tex);
909
910 if (tex->resource.b.b.nr_samples <= 1 && tex->cmask_buffer) {
911 /* Disable CMASK. */
912 assert(tex->cmask_buffer != &tex->resource);
913 si_eliminate_fast_color_clear(sctx, tex);
914 si_texture_discard_cmask(sctx->screen, tex);
915 }
916
917 view.resource = surf->texture;
918 view.format = surf->format;
919 view.access = PIPE_IMAGE_ACCESS_READ;
920 view.u.tex.first_layer = surf->u.tex.first_layer;
921 view.u.tex.last_layer = surf->u.tex.last_layer;
922 view.u.tex.level = surf->u.tex.level;
923
924 /* Set the descriptor. */
925 uint32_t *desc = descs->list + slot*4;
926 memset(desc, 0, 16 * 4);
927 si_set_shader_image_desc(sctx, &view, true, desc, desc + 8);
928
929 pipe_resource_reference(&buffers->buffers[slot], &tex->resource.b.b);
930 radeon_add_to_buffer_list(sctx, sctx->b.gfx_cs,
931 &tex->resource, RADEON_USAGE_READ,
932 RADEON_PRIO_SHADER_RW_IMAGE);
933 buffers->enabled_mask |= 1u << slot;
934 } else {
935 /* Clear the descriptor. */
936 memset(descs->list + slot*4, 0, 8*4);
937 pipe_resource_reference(&buffers->buffers[slot], NULL);
938 buffers->enabled_mask &= ~(1u << slot);
939 }
940
941 sctx->descriptors_dirty |= 1u << SI_DESCS_RW_BUFFERS;
942 }
943
944 /* SAMPLER STATES */
945
946 static void si_bind_sampler_states(struct pipe_context *ctx,
947 enum pipe_shader_type shader,
948 unsigned start, unsigned count, void **states)
949 {
950 struct si_context *sctx = (struct si_context *)ctx;
951 struct si_samplers *samplers = &sctx->samplers[shader];
952 struct si_descriptors *desc = si_sampler_and_image_descriptors(sctx, shader);
953 struct si_sampler_state **sstates = (struct si_sampler_state**)states;
954 int i;
955
956 if (!count || shader >= SI_NUM_SHADERS)
957 return;
958
959 for (i = 0; i < count; i++) {
960 unsigned slot = start + i;
961 unsigned desc_slot = si_get_sampler_slot(slot);
962
963 if (!sstates[i] ||
964 sstates[i] == samplers->sampler_states[slot])
965 continue;
966
967 #ifdef DEBUG
968 assert(sstates[i]->magic == SI_SAMPLER_STATE_MAGIC);
969 #endif
970 samplers->sampler_states[slot] = sstates[i];
971
972 /* If FMASK is bound, don't overwrite it.
973 * The sampler state will be set after FMASK is unbound.
974 */
975 struct si_sampler_view *sview =
976 (struct si_sampler_view *)samplers->views[slot];
977
978 struct r600_texture *tex = NULL;
979
980 if (sview && sview->base.texture &&
981 sview->base.texture->target != PIPE_BUFFER)
982 tex = (struct r600_texture *)sview->base.texture;
983
984 if (tex && tex->fmask.size)
985 continue;
986
987 si_set_sampler_state_desc(sstates[i], sview, tex,
988 desc->list + desc_slot * 16 + 12);
989
990 sctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
991 }
992 }
993
994 /* BUFFER RESOURCES */
995
996 static void si_init_buffer_resources(struct si_buffer_resources *buffers,
997 struct si_descriptors *descs,
998 unsigned num_buffers,
999 short shader_userdata_rel_index,
1000 enum radeon_bo_usage shader_usage,
1001 enum radeon_bo_usage shader_usage_constbuf,
1002 enum radeon_bo_priority priority,
1003 enum radeon_bo_priority priority_constbuf)
1004 {
1005 buffers->shader_usage = shader_usage;
1006 buffers->shader_usage_constbuf = shader_usage_constbuf;
1007 buffers->priority = priority;
1008 buffers->priority_constbuf = priority_constbuf;
1009 buffers->buffers = CALLOC(num_buffers, sizeof(struct pipe_resource*));
1010
1011 si_init_descriptors(descs, shader_userdata_rel_index, 4, num_buffers);
1012 }
1013
1014 static void si_release_buffer_resources(struct si_buffer_resources *buffers,
1015 struct si_descriptors *descs)
1016 {
1017 int i;
1018
1019 for (i = 0; i < descs->num_elements; i++) {
1020 pipe_resource_reference(&buffers->buffers[i], NULL);
1021 }
1022
1023 FREE(buffers->buffers);
1024 }
1025
1026 static void si_buffer_resources_begin_new_cs(struct si_context *sctx,
1027 struct si_buffer_resources *buffers)
1028 {
1029 unsigned mask = buffers->enabled_mask;
1030
1031 /* Add buffers to the CS. */
1032 while (mask) {
1033 int i = u_bit_scan(&mask);
1034
1035 radeon_add_to_buffer_list(sctx, sctx->b.gfx_cs,
1036 r600_resource(buffers->buffers[i]),
1037 i < SI_NUM_SHADER_BUFFERS ? buffers->shader_usage :
1038 buffers->shader_usage_constbuf,
1039 i < SI_NUM_SHADER_BUFFERS ? buffers->priority :
1040 buffers->priority_constbuf);
1041 }
1042 }
1043
1044 static void si_get_buffer_from_descriptors(struct si_buffer_resources *buffers,
1045 struct si_descriptors *descs,
1046 unsigned idx, struct pipe_resource **buf,
1047 unsigned *offset, unsigned *size)
1048 {
1049 pipe_resource_reference(buf, buffers->buffers[idx]);
1050 if (*buf) {
1051 struct r600_resource *res = r600_resource(*buf);
1052 const uint32_t *desc = descs->list + idx * 4;
1053 uint64_t va;
1054
1055 *size = desc[2];
1056
1057 assert(G_008F04_STRIDE(desc[1]) == 0);
1058 va = ((uint64_t)desc[1] << 32) | desc[0];
1059
1060 assert(va >= res->gpu_address && va + *size <= res->gpu_address + res->bo_size);
1061 *offset = va - res->gpu_address;
1062 }
1063 }
1064
1065 /* VERTEX BUFFERS */
1066
1067 static void si_vertex_buffers_begin_new_cs(struct si_context *sctx)
1068 {
1069 int count = sctx->vertex_elements ? sctx->vertex_elements->count : 0;
1070 int i;
1071
1072 for (i = 0; i < count; i++) {
1073 int vb = sctx->vertex_elements->vertex_buffer_index[i];
1074
1075 if (vb >= ARRAY_SIZE(sctx->vertex_buffer))
1076 continue;
1077 if (!sctx->vertex_buffer[vb].buffer.resource)
1078 continue;
1079
1080 radeon_add_to_buffer_list(sctx, sctx->b.gfx_cs,
1081 (struct r600_resource*)sctx->vertex_buffer[vb].buffer.resource,
1082 RADEON_USAGE_READ, RADEON_PRIO_VERTEX_BUFFER);
1083 }
1084
1085 if (!sctx->vb_descriptors_buffer)
1086 return;
1087 radeon_add_to_buffer_list(sctx, sctx->b.gfx_cs,
1088 sctx->vb_descriptors_buffer, RADEON_USAGE_READ,
1089 RADEON_PRIO_DESCRIPTORS);
1090 }
1091
1092 bool si_upload_vertex_buffer_descriptors(struct si_context *sctx)
1093 {
1094 struct si_vertex_elements *velems = sctx->vertex_elements;
1095 unsigned i, count;
1096 unsigned desc_list_byte_size;
1097 unsigned first_vb_use_mask;
1098 uint32_t *ptr;
1099
1100 if (!sctx->vertex_buffers_dirty || !velems)
1101 return true;
1102
1103 count = velems->count;
1104
1105 if (!count)
1106 return true;
1107
1108 desc_list_byte_size = velems->desc_list_byte_size;
1109 first_vb_use_mask = velems->first_vb_use_mask;
1110
1111 /* Vertex buffer descriptors are the only ones which are uploaded
1112 * directly through a staging buffer and don't go through
1113 * the fine-grained upload path.
1114 */
1115 u_upload_alloc(sctx->b.b.const_uploader, 0,
1116 desc_list_byte_size,
1117 si_optimal_tcc_alignment(sctx, desc_list_byte_size),
1118 &sctx->vb_descriptors_offset,
1119 (struct pipe_resource**)&sctx->vb_descriptors_buffer,
1120 (void**)&ptr);
1121 if (!sctx->vb_descriptors_buffer) {
1122 sctx->vb_descriptors_offset = 0;
1123 sctx->vb_descriptors_gpu_list = NULL;
1124 return false;
1125 }
1126
1127 sctx->vb_descriptors_gpu_list = ptr;
1128 radeon_add_to_buffer_list(sctx, sctx->b.gfx_cs,
1129 sctx->vb_descriptors_buffer, RADEON_USAGE_READ,
1130 RADEON_PRIO_DESCRIPTORS);
1131
1132 assert(count <= SI_MAX_ATTRIBS);
1133
1134 for (i = 0; i < count; i++) {
1135 struct pipe_vertex_buffer *vb;
1136 struct r600_resource *rbuffer;
1137 unsigned vbo_index = velems->vertex_buffer_index[i];
1138 uint32_t *desc = &ptr[i*4];
1139
1140 vb = &sctx->vertex_buffer[vbo_index];
1141 rbuffer = (struct r600_resource*)vb->buffer.resource;
1142 if (!rbuffer) {
1143 memset(desc, 0, 16);
1144 continue;
1145 }
1146
1147 int64_t offset = (int64_t)((int)vb->buffer_offset) +
1148 velems->src_offset[i];
1149 uint64_t va = rbuffer->gpu_address + offset;
1150
1151 int64_t num_records = (int64_t)rbuffer->b.b.width0 - offset;
1152 if (sctx->b.chip_class != VI && vb->stride) {
1153 /* Round up by rounding down and adding 1 */
1154 num_records = (num_records - velems->format_size[i]) /
1155 vb->stride + 1;
1156 }
1157 assert(num_records >= 0 && num_records <= UINT_MAX);
1158
1159 desc[0] = va;
1160 desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
1161 S_008F04_STRIDE(vb->stride);
1162 desc[2] = num_records;
1163 desc[3] = velems->rsrc_word3[i];
1164
1165 if (first_vb_use_mask & (1 << i)) {
1166 radeon_add_to_buffer_list(sctx, sctx->b.gfx_cs,
1167 (struct r600_resource*)vb->buffer.resource,
1168 RADEON_USAGE_READ, RADEON_PRIO_VERTEX_BUFFER);
1169 }
1170 }
1171
1172 /* Don't flush the const cache. It would have a very negative effect
1173 * on performance (confirmed by testing). New descriptors are always
1174 * uploaded to a fresh new buffer, so I don't think flushing the const
1175 * cache is needed. */
1176 si_mark_atom_dirty(sctx, &sctx->shader_pointers.atom);
1177 sctx->vertex_buffers_dirty = false;
1178 sctx->vertex_buffer_pointer_dirty = true;
1179 sctx->prefetch_L2_mask |= SI_PREFETCH_VBO_DESCRIPTORS;
1180 return true;
1181 }
1182
1183
1184 /* CONSTANT BUFFERS */
1185
1186 static unsigned
1187 si_const_and_shader_buffer_descriptors_idx(unsigned shader)
1188 {
1189 return SI_DESCS_FIRST_SHADER + shader * SI_NUM_SHADER_DESCS +
1190 SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS;
1191 }
1192
1193 static struct si_descriptors *
1194 si_const_and_shader_buffer_descriptors(struct si_context *sctx, unsigned shader)
1195 {
1196 return &sctx->descriptors[si_const_and_shader_buffer_descriptors_idx(shader)];
1197 }
1198
1199 void si_upload_const_buffer(struct si_context *sctx, struct r600_resource **rbuffer,
1200 const uint8_t *ptr, unsigned size, uint32_t *const_offset)
1201 {
1202 void *tmp;
1203
1204 u_upload_alloc(sctx->b.b.const_uploader, 0, size,
1205 si_optimal_tcc_alignment(sctx, size),
1206 const_offset,
1207 (struct pipe_resource**)rbuffer, &tmp);
1208 if (*rbuffer)
1209 util_memcpy_cpu_to_le32(tmp, ptr, size);
1210 }
1211
1212 static void si_set_constant_buffer(struct si_context *sctx,
1213 struct si_buffer_resources *buffers,
1214 unsigned descriptors_idx,
1215 uint slot, const struct pipe_constant_buffer *input)
1216 {
1217 struct si_descriptors *descs = &sctx->descriptors[descriptors_idx];
1218 assert(slot < descs->num_elements);
1219 pipe_resource_reference(&buffers->buffers[slot], NULL);
1220
1221 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD is buggy
1222 * with a NULL buffer). We need to use a dummy buffer instead. */
1223 if (sctx->b.chip_class == CIK &&
1224 (!input || (!input->buffer && !input->user_buffer)))
1225 input = &sctx->null_const_buf;
1226
1227 if (input && (input->buffer || input->user_buffer)) {
1228 struct pipe_resource *buffer = NULL;
1229 uint64_t va;
1230
1231 /* Upload the user buffer if needed. */
1232 if (input->user_buffer) {
1233 unsigned buffer_offset;
1234
1235 si_upload_const_buffer(sctx,
1236 (struct r600_resource**)&buffer, input->user_buffer,
1237 input->buffer_size, &buffer_offset);
1238 if (!buffer) {
1239 /* Just unbind on failure. */
1240 si_set_constant_buffer(sctx, buffers, descriptors_idx, slot, NULL);
1241 return;
1242 }
1243 va = r600_resource(buffer)->gpu_address + buffer_offset;
1244 } else {
1245 pipe_resource_reference(&buffer, input->buffer);
1246 va = r600_resource(buffer)->gpu_address + input->buffer_offset;
1247 /* Only track usage for non-user buffers. */
1248 r600_resource(buffer)->bind_history |= PIPE_BIND_CONSTANT_BUFFER;
1249 }
1250
1251 /* Set the descriptor. */
1252 uint32_t *desc = descs->list + slot*4;
1253 desc[0] = va;
1254 desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
1255 S_008F04_STRIDE(0);
1256 desc[2] = input->buffer_size;
1257 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1258 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1259 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1260 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1261 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1262 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
1263
1264 buffers->buffers[slot] = buffer;
1265 radeon_add_to_gfx_buffer_list_check_mem(sctx,
1266 (struct r600_resource*)buffer,
1267 buffers->shader_usage_constbuf,
1268 buffers->priority_constbuf, true);
1269 buffers->enabled_mask |= 1u << slot;
1270 } else {
1271 /* Clear the descriptor. */
1272 memset(descs->list + slot*4, 0, sizeof(uint32_t) * 4);
1273 buffers->enabled_mask &= ~(1u << slot);
1274 }
1275
1276 sctx->descriptors_dirty |= 1u << descriptors_idx;
1277 }
1278
1279 void si_set_rw_buffer(struct si_context *sctx,
1280 uint slot, const struct pipe_constant_buffer *input)
1281 {
1282 si_set_constant_buffer(sctx, &sctx->rw_buffers,
1283 SI_DESCS_RW_BUFFERS, slot, input);
1284 }
1285
1286 static void si_pipe_set_constant_buffer(struct pipe_context *ctx,
1287 enum pipe_shader_type shader, uint slot,
1288 const struct pipe_constant_buffer *input)
1289 {
1290 struct si_context *sctx = (struct si_context *)ctx;
1291
1292 if (shader >= SI_NUM_SHADERS)
1293 return;
1294
1295 if (slot == 0 && input && input->buffer &&
1296 !(r600_resource(input->buffer)->flags & RADEON_FLAG_32BIT)) {
1297 assert(!"constant buffer 0 must have a 32-bit VM address, use const_uploader");
1298 return;
1299 }
1300
1301 slot = si_get_constbuf_slot(slot);
1302 si_set_constant_buffer(sctx, &sctx->const_and_shader_buffers[shader],
1303 si_const_and_shader_buffer_descriptors_idx(shader),
1304 slot, input);
1305 }
1306
1307 void si_get_pipe_constant_buffer(struct si_context *sctx, uint shader,
1308 uint slot, struct pipe_constant_buffer *cbuf)
1309 {
1310 cbuf->user_buffer = NULL;
1311 si_get_buffer_from_descriptors(
1312 &sctx->const_and_shader_buffers[shader],
1313 si_const_and_shader_buffer_descriptors(sctx, shader),
1314 si_get_constbuf_slot(slot),
1315 &cbuf->buffer, &cbuf->buffer_offset, &cbuf->buffer_size);
1316 }
1317
1318 /* SHADER BUFFERS */
1319
1320 static void si_set_shader_buffers(struct pipe_context *ctx,
1321 enum pipe_shader_type shader,
1322 unsigned start_slot, unsigned count,
1323 const struct pipe_shader_buffer *sbuffers)
1324 {
1325 struct si_context *sctx = (struct si_context *)ctx;
1326 struct si_buffer_resources *buffers = &sctx->const_and_shader_buffers[shader];
1327 struct si_descriptors *descs = si_const_and_shader_buffer_descriptors(sctx, shader);
1328 unsigned i;
1329
1330 assert(start_slot + count <= SI_NUM_SHADER_BUFFERS);
1331
1332 for (i = 0; i < count; ++i) {
1333 const struct pipe_shader_buffer *sbuffer = sbuffers ? &sbuffers[i] : NULL;
1334 struct r600_resource *buf;
1335 unsigned slot = si_get_shaderbuf_slot(start_slot + i);
1336 uint32_t *desc = descs->list + slot * 4;
1337 uint64_t va;
1338
1339 if (!sbuffer || !sbuffer->buffer) {
1340 pipe_resource_reference(&buffers->buffers[slot], NULL);
1341 memset(desc, 0, sizeof(uint32_t) * 4);
1342 buffers->enabled_mask &= ~(1u << slot);
1343 sctx->descriptors_dirty |=
1344 1u << si_const_and_shader_buffer_descriptors_idx(shader);
1345 continue;
1346 }
1347
1348 buf = (struct r600_resource *)sbuffer->buffer;
1349 va = buf->gpu_address + sbuffer->buffer_offset;
1350
1351 desc[0] = va;
1352 desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
1353 S_008F04_STRIDE(0);
1354 desc[2] = sbuffer->buffer_size;
1355 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1356 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1357 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1358 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1359 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1360 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
1361
1362 pipe_resource_reference(&buffers->buffers[slot], &buf->b.b);
1363 radeon_add_to_gfx_buffer_list_check_mem(sctx, buf,
1364 buffers->shader_usage,
1365 buffers->priority, true);
1366 buf->bind_history |= PIPE_BIND_SHADER_BUFFER;
1367
1368 buffers->enabled_mask |= 1u << slot;
1369 sctx->descriptors_dirty |=
1370 1u << si_const_and_shader_buffer_descriptors_idx(shader);
1371
1372 util_range_add(&buf->valid_buffer_range, sbuffer->buffer_offset,
1373 sbuffer->buffer_offset + sbuffer->buffer_size);
1374 }
1375 }
1376
1377 void si_get_shader_buffers(struct si_context *sctx,
1378 enum pipe_shader_type shader,
1379 uint start_slot, uint count,
1380 struct pipe_shader_buffer *sbuf)
1381 {
1382 struct si_buffer_resources *buffers = &sctx->const_and_shader_buffers[shader];
1383 struct si_descriptors *descs = si_const_and_shader_buffer_descriptors(sctx, shader);
1384
1385 for (unsigned i = 0; i < count; ++i) {
1386 si_get_buffer_from_descriptors(
1387 buffers, descs,
1388 si_get_shaderbuf_slot(start_slot + i),
1389 &sbuf[i].buffer, &sbuf[i].buffer_offset,
1390 &sbuf[i].buffer_size);
1391 }
1392 }
1393
1394 /* RING BUFFERS */
1395
1396 void si_set_ring_buffer(struct pipe_context *ctx, uint slot,
1397 struct pipe_resource *buffer,
1398 unsigned stride, unsigned num_records,
1399 bool add_tid, bool swizzle,
1400 unsigned element_size, unsigned index_stride, uint64_t offset)
1401 {
1402 struct si_context *sctx = (struct si_context *)ctx;
1403 struct si_buffer_resources *buffers = &sctx->rw_buffers;
1404 struct si_descriptors *descs = &sctx->descriptors[SI_DESCS_RW_BUFFERS];
1405
1406 /* The stride field in the resource descriptor has 14 bits */
1407 assert(stride < (1 << 14));
1408
1409 assert(slot < descs->num_elements);
1410 pipe_resource_reference(&buffers->buffers[slot], NULL);
1411
1412 if (buffer) {
1413 uint64_t va;
1414
1415 va = r600_resource(buffer)->gpu_address + offset;
1416
1417 switch (element_size) {
1418 default:
1419 assert(!"Unsupported ring buffer element size");
1420 case 0:
1421 case 2:
1422 element_size = 0;
1423 break;
1424 case 4:
1425 element_size = 1;
1426 break;
1427 case 8:
1428 element_size = 2;
1429 break;
1430 case 16:
1431 element_size = 3;
1432 break;
1433 }
1434
1435 switch (index_stride) {
1436 default:
1437 assert(!"Unsupported ring buffer index stride");
1438 case 0:
1439 case 8:
1440 index_stride = 0;
1441 break;
1442 case 16:
1443 index_stride = 1;
1444 break;
1445 case 32:
1446 index_stride = 2;
1447 break;
1448 case 64:
1449 index_stride = 3;
1450 break;
1451 }
1452
1453 if (sctx->b.chip_class >= VI && stride)
1454 num_records *= stride;
1455
1456 /* Set the descriptor. */
1457 uint32_t *desc = descs->list + slot*4;
1458 desc[0] = va;
1459 desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
1460 S_008F04_STRIDE(stride) |
1461 S_008F04_SWIZZLE_ENABLE(swizzle);
1462 desc[2] = num_records;
1463 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1464 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1465 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1466 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1467 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1468 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
1469 S_008F0C_INDEX_STRIDE(index_stride) |
1470 S_008F0C_ADD_TID_ENABLE(add_tid);
1471
1472 if (sctx->b.chip_class >= GFX9)
1473 assert(!swizzle || element_size == 1); /* always 4 bytes on GFX9 */
1474 else
1475 desc[3] |= S_008F0C_ELEMENT_SIZE(element_size);
1476
1477 pipe_resource_reference(&buffers->buffers[slot], buffer);
1478 radeon_add_to_buffer_list(sctx, sctx->b.gfx_cs,
1479 (struct r600_resource*)buffer,
1480 buffers->shader_usage, buffers->priority);
1481 buffers->enabled_mask |= 1u << slot;
1482 } else {
1483 /* Clear the descriptor. */
1484 memset(descs->list + slot*4, 0, sizeof(uint32_t) * 4);
1485 buffers->enabled_mask &= ~(1u << slot);
1486 }
1487
1488 sctx->descriptors_dirty |= 1u << SI_DESCS_RW_BUFFERS;
1489 }
1490
1491 static void si_desc_reset_buffer_offset(struct pipe_context *ctx,
1492 uint32_t *desc, uint64_t old_buf_va,
1493 struct pipe_resource *new_buf)
1494 {
1495 /* Retrieve the buffer offset from the descriptor. */
1496 uint64_t old_desc_va = si_desc_extract_buffer_address(desc);
1497
1498 assert(old_buf_va <= old_desc_va);
1499 uint64_t offset_within_buffer = old_desc_va - old_buf_va;
1500
1501 /* Update the descriptor. */
1502 si_set_buf_desc_address(r600_resource(new_buf), offset_within_buffer,
1503 desc);
1504 }
1505
1506 /* INTERNAL CONST BUFFERS */
1507
1508 static void si_set_polygon_stipple(struct pipe_context *ctx,
1509 const struct pipe_poly_stipple *state)
1510 {
1511 struct si_context *sctx = (struct si_context *)ctx;
1512 struct pipe_constant_buffer cb = {};
1513 unsigned stipple[32];
1514 int i;
1515
1516 for (i = 0; i < 32; i++)
1517 stipple[i] = util_bitreverse(state->stipple[i]);
1518
1519 cb.user_buffer = stipple;
1520 cb.buffer_size = sizeof(stipple);
1521
1522 si_set_rw_buffer(sctx, SI_PS_CONST_POLY_STIPPLE, &cb);
1523 }
1524
1525 /* TEXTURE METADATA ENABLE/DISABLE */
1526
1527 static void
1528 si_resident_handles_update_needs_color_decompress(struct si_context *sctx)
1529 {
1530 util_dynarray_clear(&sctx->resident_tex_needs_color_decompress);
1531 util_dynarray_clear(&sctx->resident_img_needs_color_decompress);
1532
1533 util_dynarray_foreach(&sctx->resident_tex_handles,
1534 struct si_texture_handle *, tex_handle) {
1535 struct pipe_resource *res = (*tex_handle)->view->texture;
1536 struct r600_texture *rtex;
1537
1538 if (!res || res->target == PIPE_BUFFER)
1539 continue;
1540
1541 rtex = (struct r600_texture *)res;
1542 if (!color_needs_decompression(rtex))
1543 continue;
1544
1545 util_dynarray_append(&sctx->resident_tex_needs_color_decompress,
1546 struct si_texture_handle *, *tex_handle);
1547 }
1548
1549 util_dynarray_foreach(&sctx->resident_img_handles,
1550 struct si_image_handle *, img_handle) {
1551 struct pipe_image_view *view = &(*img_handle)->view;
1552 struct pipe_resource *res = view->resource;
1553 struct r600_texture *rtex;
1554
1555 if (!res || res->target == PIPE_BUFFER)
1556 continue;
1557
1558 rtex = (struct r600_texture *)res;
1559 if (!color_needs_decompression(rtex))
1560 continue;
1561
1562 util_dynarray_append(&sctx->resident_img_needs_color_decompress,
1563 struct si_image_handle *, *img_handle);
1564 }
1565 }
1566
1567 /* CMASK can be enabled (for fast clear) and disabled (for texture export)
1568 * while the texture is bound, possibly by a different context. In that case,
1569 * call this function to update needs_*_decompress_masks.
1570 */
1571 void si_update_needs_color_decompress_masks(struct si_context *sctx)
1572 {
1573 for (int i = 0; i < SI_NUM_SHADERS; ++i) {
1574 si_samplers_update_needs_color_decompress_mask(&sctx->samplers[i]);
1575 si_images_update_needs_color_decompress_mask(&sctx->images[i]);
1576 si_update_shader_needs_decompress_mask(sctx, i);
1577 }
1578
1579 si_resident_handles_update_needs_color_decompress(sctx);
1580 }
1581
1582 /* BUFFER DISCARD/INVALIDATION */
1583
1584 /** Reset descriptors of buffer resources after \p buf has been invalidated. */
1585 static void si_reset_buffer_resources(struct si_context *sctx,
1586 struct si_buffer_resources *buffers,
1587 unsigned descriptors_idx,
1588 unsigned slot_mask,
1589 struct pipe_resource *buf,
1590 uint64_t old_va,
1591 enum radeon_bo_usage usage,
1592 enum radeon_bo_priority priority)
1593 {
1594 struct si_descriptors *descs = &sctx->descriptors[descriptors_idx];
1595 unsigned mask = buffers->enabled_mask & slot_mask;
1596
1597 while (mask) {
1598 unsigned i = u_bit_scan(&mask);
1599 if (buffers->buffers[i] == buf) {
1600 si_desc_reset_buffer_offset(&sctx->b.b,
1601 descs->list + i*4,
1602 old_va, buf);
1603 sctx->descriptors_dirty |= 1u << descriptors_idx;
1604
1605 radeon_add_to_gfx_buffer_list_check_mem(sctx,
1606 (struct r600_resource *)buf,
1607 usage, priority, true);
1608 }
1609 }
1610 }
1611
1612 /* Update all resource bindings where the buffer is bound, including
1613 * all resource descriptors. This is invalidate_buffer without
1614 * the invalidation. */
1615 void si_rebind_buffer(struct pipe_context *ctx, struct pipe_resource *buf,
1616 uint64_t old_va)
1617 {
1618 struct si_context *sctx = (struct si_context*)ctx;
1619 struct r600_resource *rbuffer = r600_resource(buf);
1620 unsigned i, shader;
1621 unsigned num_elems = sctx->vertex_elements ?
1622 sctx->vertex_elements->count : 0;
1623
1624 /* We changed the buffer, now we need to bind it where the old one
1625 * was bound. This consists of 2 things:
1626 * 1) Updating the resource descriptor and dirtying it.
1627 * 2) Adding a relocation to the CS, so that it's usable.
1628 */
1629
1630 /* Vertex buffers. */
1631 if (rbuffer->bind_history & PIPE_BIND_VERTEX_BUFFER) {
1632 for (i = 0; i < num_elems; i++) {
1633 int vb = sctx->vertex_elements->vertex_buffer_index[i];
1634
1635 if (vb >= ARRAY_SIZE(sctx->vertex_buffer))
1636 continue;
1637 if (!sctx->vertex_buffer[vb].buffer.resource)
1638 continue;
1639
1640 if (sctx->vertex_buffer[vb].buffer.resource == buf) {
1641 sctx->vertex_buffers_dirty = true;
1642 break;
1643 }
1644 }
1645 }
1646
1647 /* Streamout buffers. (other internal buffers can't be invalidated) */
1648 if (rbuffer->bind_history & PIPE_BIND_STREAM_OUTPUT) {
1649 for (i = SI_VS_STREAMOUT_BUF0; i <= SI_VS_STREAMOUT_BUF3; i++) {
1650 struct si_buffer_resources *buffers = &sctx->rw_buffers;
1651 struct si_descriptors *descs =
1652 &sctx->descriptors[SI_DESCS_RW_BUFFERS];
1653
1654 if (buffers->buffers[i] != buf)
1655 continue;
1656
1657 si_desc_reset_buffer_offset(ctx, descs->list + i*4,
1658 old_va, buf);
1659 sctx->descriptors_dirty |= 1u << SI_DESCS_RW_BUFFERS;
1660
1661 radeon_add_to_gfx_buffer_list_check_mem(sctx,
1662 rbuffer, buffers->shader_usage,
1663 RADEON_PRIO_SHADER_RW_BUFFER,
1664 true);
1665
1666 /* Update the streamout state. */
1667 if (sctx->streamout.begin_emitted)
1668 si_emit_streamout_end(sctx);
1669 sctx->streamout.append_bitmask =
1670 sctx->streamout.enabled_mask;
1671 si_streamout_buffers_dirty(sctx);
1672 }
1673 }
1674
1675 /* Constant and shader buffers. */
1676 if (rbuffer->bind_history & PIPE_BIND_CONSTANT_BUFFER) {
1677 for (shader = 0; shader < SI_NUM_SHADERS; shader++)
1678 si_reset_buffer_resources(sctx, &sctx->const_and_shader_buffers[shader],
1679 si_const_and_shader_buffer_descriptors_idx(shader),
1680 u_bit_consecutive(SI_NUM_SHADER_BUFFERS, SI_NUM_CONST_BUFFERS),
1681 buf, old_va,
1682 sctx->const_and_shader_buffers[shader].shader_usage_constbuf,
1683 sctx->const_and_shader_buffers[shader].priority_constbuf);
1684 }
1685
1686 if (rbuffer->bind_history & PIPE_BIND_SHADER_BUFFER) {
1687 for (shader = 0; shader < SI_NUM_SHADERS; shader++)
1688 si_reset_buffer_resources(sctx, &sctx->const_and_shader_buffers[shader],
1689 si_const_and_shader_buffer_descriptors_idx(shader),
1690 u_bit_consecutive(0, SI_NUM_SHADER_BUFFERS),
1691 buf, old_va,
1692 sctx->const_and_shader_buffers[shader].shader_usage,
1693 sctx->const_and_shader_buffers[shader].priority);
1694 }
1695
1696 if (rbuffer->bind_history & PIPE_BIND_SAMPLER_VIEW) {
1697 /* Texture buffers - update bindings. */
1698 for (shader = 0; shader < SI_NUM_SHADERS; shader++) {
1699 struct si_samplers *samplers = &sctx->samplers[shader];
1700 struct si_descriptors *descs =
1701 si_sampler_and_image_descriptors(sctx, shader);
1702 unsigned mask = samplers->enabled_mask;
1703
1704 while (mask) {
1705 unsigned i = u_bit_scan(&mask);
1706 if (samplers->views[i]->texture == buf) {
1707 unsigned desc_slot = si_get_sampler_slot(i);
1708
1709 si_desc_reset_buffer_offset(ctx,
1710 descs->list +
1711 desc_slot * 16 + 4,
1712 old_va, buf);
1713 sctx->descriptors_dirty |=
1714 1u << si_sampler_and_image_descriptors_idx(shader);
1715
1716 radeon_add_to_gfx_buffer_list_check_mem(sctx,
1717 rbuffer, RADEON_USAGE_READ,
1718 RADEON_PRIO_SAMPLER_BUFFER,
1719 true);
1720 }
1721 }
1722 }
1723 }
1724
1725 /* Shader images */
1726 if (rbuffer->bind_history & PIPE_BIND_SHADER_IMAGE) {
1727 for (shader = 0; shader < SI_NUM_SHADERS; ++shader) {
1728 struct si_images *images = &sctx->images[shader];
1729 struct si_descriptors *descs =
1730 si_sampler_and_image_descriptors(sctx, shader);
1731 unsigned mask = images->enabled_mask;
1732
1733 while (mask) {
1734 unsigned i = u_bit_scan(&mask);
1735
1736 if (images->views[i].resource == buf) {
1737 unsigned desc_slot = si_get_image_slot(i);
1738
1739 if (images->views[i].access & PIPE_IMAGE_ACCESS_WRITE)
1740 si_mark_image_range_valid(&images->views[i]);
1741
1742 si_desc_reset_buffer_offset(
1743 ctx, descs->list + desc_slot * 8 + 4,
1744 old_va, buf);
1745 sctx->descriptors_dirty |=
1746 1u << si_sampler_and_image_descriptors_idx(shader);
1747
1748 radeon_add_to_gfx_buffer_list_check_mem(
1749 sctx, rbuffer,
1750 RADEON_USAGE_READWRITE,
1751 RADEON_PRIO_SAMPLER_BUFFER, true);
1752 }
1753 }
1754 }
1755 }
1756
1757 /* Bindless texture handles */
1758 if (rbuffer->texture_handle_allocated) {
1759 struct si_descriptors *descs = &sctx->bindless_descriptors;
1760
1761 util_dynarray_foreach(&sctx->resident_tex_handles,
1762 struct si_texture_handle *, tex_handle) {
1763 struct pipe_sampler_view *view = (*tex_handle)->view;
1764 unsigned desc_slot = (*tex_handle)->desc_slot;
1765
1766 if (view->texture == buf) {
1767 si_set_buf_desc_address(rbuffer,
1768 view->u.buf.offset,
1769 descs->list +
1770 desc_slot * 16 + 4);
1771
1772 (*tex_handle)->desc_dirty = true;
1773 sctx->bindless_descriptors_dirty = true;
1774
1775 radeon_add_to_gfx_buffer_list_check_mem(
1776 sctx, rbuffer,
1777 RADEON_USAGE_READ,
1778 RADEON_PRIO_SAMPLER_BUFFER, true);
1779 }
1780 }
1781 }
1782
1783 /* Bindless image handles */
1784 if (rbuffer->image_handle_allocated) {
1785 struct si_descriptors *descs = &sctx->bindless_descriptors;
1786
1787 util_dynarray_foreach(&sctx->resident_img_handles,
1788 struct si_image_handle *, img_handle) {
1789 struct pipe_image_view *view = &(*img_handle)->view;
1790 unsigned desc_slot = (*img_handle)->desc_slot;
1791
1792 if (view->resource == buf) {
1793 if (view->access & PIPE_IMAGE_ACCESS_WRITE)
1794 si_mark_image_range_valid(view);
1795
1796 si_set_buf_desc_address(rbuffer,
1797 view->u.buf.offset,
1798 descs->list +
1799 desc_slot * 16 + 4);
1800
1801 (*img_handle)->desc_dirty = true;
1802 sctx->bindless_descriptors_dirty = true;
1803
1804 radeon_add_to_gfx_buffer_list_check_mem(
1805 sctx, rbuffer,
1806 RADEON_USAGE_READWRITE,
1807 RADEON_PRIO_SAMPLER_BUFFER, true);
1808 }
1809 }
1810 }
1811 }
1812
1813 static void si_upload_bindless_descriptor(struct si_context *sctx,
1814 unsigned desc_slot,
1815 unsigned num_dwords)
1816 {
1817 struct si_descriptors *desc = &sctx->bindless_descriptors;
1818 struct radeon_winsys_cs *cs = sctx->b.gfx_cs;
1819 unsigned desc_slot_offset = desc_slot * 16;
1820 uint32_t *data;
1821 uint64_t va;
1822
1823 data = desc->list + desc_slot_offset;
1824 va = desc->gpu_address + desc_slot_offset * 4;
1825
1826 radeon_emit(cs, PKT3(PKT3_WRITE_DATA, 2 + num_dwords, 0));
1827 radeon_emit(cs, S_370_DST_SEL(V_370_TC_L2) |
1828 S_370_WR_CONFIRM(1) |
1829 S_370_ENGINE_SEL(V_370_ME));
1830 radeon_emit(cs, va);
1831 radeon_emit(cs, va >> 32);
1832 radeon_emit_array(cs, data, num_dwords);
1833 }
1834
1835 static void si_upload_bindless_descriptors(struct si_context *sctx)
1836 {
1837 if (!sctx->bindless_descriptors_dirty)
1838 return;
1839
1840 /* Wait for graphics/compute to be idle before updating the resident
1841 * descriptors directly in memory, in case the GPU is using them.
1842 */
1843 sctx->b.flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
1844 SI_CONTEXT_CS_PARTIAL_FLUSH;
1845 si_emit_cache_flush(sctx);
1846
1847 util_dynarray_foreach(&sctx->resident_tex_handles,
1848 struct si_texture_handle *, tex_handle) {
1849 unsigned desc_slot = (*tex_handle)->desc_slot;
1850
1851 if (!(*tex_handle)->desc_dirty)
1852 continue;
1853
1854 si_upload_bindless_descriptor(sctx, desc_slot, 16);
1855 (*tex_handle)->desc_dirty = false;
1856 }
1857
1858 util_dynarray_foreach(&sctx->resident_img_handles,
1859 struct si_image_handle *, img_handle) {
1860 unsigned desc_slot = (*img_handle)->desc_slot;
1861
1862 if (!(*img_handle)->desc_dirty)
1863 continue;
1864
1865 si_upload_bindless_descriptor(sctx, desc_slot, 8);
1866 (*img_handle)->desc_dirty = false;
1867 }
1868
1869 /* Invalidate L1 because it doesn't know that L2 changed. */
1870 sctx->b.flags |= SI_CONTEXT_INV_SMEM_L1;
1871 si_emit_cache_flush(sctx);
1872
1873 sctx->bindless_descriptors_dirty = false;
1874 }
1875
1876 /* Update mutable image descriptor fields of all resident textures. */
1877 static void si_update_bindless_texture_descriptor(struct si_context *sctx,
1878 struct si_texture_handle *tex_handle)
1879 {
1880 struct si_sampler_view *sview = (struct si_sampler_view *)tex_handle->view;
1881 struct si_descriptors *desc = &sctx->bindless_descriptors;
1882 unsigned desc_slot_offset = tex_handle->desc_slot * 16;
1883 uint32_t desc_list[16];
1884
1885 if (sview->base.texture->target == PIPE_BUFFER)
1886 return;
1887
1888 memcpy(desc_list, desc->list + desc_slot_offset, sizeof(desc_list));
1889 si_set_sampler_view_desc(sctx, sview, &tex_handle->sstate,
1890 desc->list + desc_slot_offset);
1891
1892 if (memcmp(desc_list, desc->list + desc_slot_offset,
1893 sizeof(desc_list))) {
1894 tex_handle->desc_dirty = true;
1895 sctx->bindless_descriptors_dirty = true;
1896 }
1897 }
1898
1899 static void si_update_bindless_image_descriptor(struct si_context *sctx,
1900 struct si_image_handle *img_handle)
1901 {
1902 struct si_descriptors *desc = &sctx->bindless_descriptors;
1903 unsigned desc_slot_offset = img_handle->desc_slot * 16;
1904 struct pipe_image_view *view = &img_handle->view;
1905 uint32_t desc_list[8];
1906
1907 if (view->resource->target == PIPE_BUFFER)
1908 return;
1909
1910 memcpy(desc_list, desc->list + desc_slot_offset,
1911 sizeof(desc_list));
1912 si_set_shader_image_desc(sctx, view, true,
1913 desc->list + desc_slot_offset, NULL);
1914
1915 if (memcmp(desc_list, desc->list + desc_slot_offset,
1916 sizeof(desc_list))) {
1917 img_handle->desc_dirty = true;
1918 sctx->bindless_descriptors_dirty = true;
1919 }
1920 }
1921
1922 static void si_update_all_resident_texture_descriptors(struct si_context *sctx)
1923 {
1924 util_dynarray_foreach(&sctx->resident_tex_handles,
1925 struct si_texture_handle *, tex_handle) {
1926 si_update_bindless_texture_descriptor(sctx, *tex_handle);
1927 }
1928
1929 util_dynarray_foreach(&sctx->resident_img_handles,
1930 struct si_image_handle *, img_handle) {
1931 si_update_bindless_image_descriptor(sctx, *img_handle);
1932 }
1933
1934 si_upload_bindless_descriptors(sctx);
1935 }
1936
1937 /* Update mutable image descriptor fields of all bound textures. */
1938 void si_update_all_texture_descriptors(struct si_context *sctx)
1939 {
1940 unsigned shader;
1941
1942 for (shader = 0; shader < SI_NUM_SHADERS; shader++) {
1943 struct si_samplers *samplers = &sctx->samplers[shader];
1944 struct si_images *images = &sctx->images[shader];
1945 unsigned mask;
1946
1947 /* Images. */
1948 mask = images->enabled_mask;
1949 while (mask) {
1950 unsigned i = u_bit_scan(&mask);
1951 struct pipe_image_view *view = &images->views[i];
1952
1953 if (!view->resource ||
1954 view->resource->target == PIPE_BUFFER)
1955 continue;
1956
1957 si_set_shader_image(sctx, shader, i, view, true);
1958 }
1959
1960 /* Sampler views. */
1961 mask = samplers->enabled_mask;
1962 while (mask) {
1963 unsigned i = u_bit_scan(&mask);
1964 struct pipe_sampler_view *view = samplers->views[i];
1965
1966 if (!view ||
1967 !view->texture ||
1968 view->texture->target == PIPE_BUFFER)
1969 continue;
1970
1971 si_set_sampler_view(sctx, shader, i,
1972 samplers->views[i], true);
1973 }
1974
1975 si_update_shader_needs_decompress_mask(sctx, shader);
1976 }
1977
1978 si_update_all_resident_texture_descriptors(sctx);
1979 si_update_ps_colorbuf0_slot(sctx);
1980 }
1981
1982 /* SHADER USER DATA */
1983
1984 static void si_mark_shader_pointers_dirty(struct si_context *sctx,
1985 unsigned shader)
1986 {
1987 sctx->shader_pointers_dirty |=
1988 u_bit_consecutive(SI_DESCS_FIRST_SHADER + shader * SI_NUM_SHADER_DESCS,
1989 SI_NUM_SHADER_DESCS);
1990
1991 if (shader == PIPE_SHADER_VERTEX)
1992 sctx->vertex_buffer_pointer_dirty = sctx->vb_descriptors_buffer != NULL;
1993
1994 si_mark_atom_dirty(sctx, &sctx->shader_pointers.atom);
1995 }
1996
1997 static void si_shader_pointers_begin_new_cs(struct si_context *sctx)
1998 {
1999 sctx->shader_pointers_dirty = u_bit_consecutive(0, SI_NUM_DESCS);
2000 sctx->vertex_buffer_pointer_dirty = sctx->vb_descriptors_buffer != NULL;
2001 si_mark_atom_dirty(sctx, &sctx->shader_pointers.atom);
2002 sctx->graphics_bindless_pointer_dirty = sctx->bindless_descriptors.buffer != NULL;
2003 sctx->compute_bindless_pointer_dirty = sctx->bindless_descriptors.buffer != NULL;
2004 }
2005
2006 /* Set a base register address for user data constants in the given shader.
2007 * This assigns a mapping from PIPE_SHADER_* to SPI_SHADER_USER_DATA_*.
2008 */
2009 static void si_set_user_data_base(struct si_context *sctx,
2010 unsigned shader, uint32_t new_base)
2011 {
2012 uint32_t *base = &sctx->shader_pointers.sh_base[shader];
2013
2014 if (*base != new_base) {
2015 *base = new_base;
2016
2017 if (new_base) {
2018 si_mark_shader_pointers_dirty(sctx, shader);
2019
2020 if (shader == PIPE_SHADER_VERTEX)
2021 sctx->last_vs_state = ~0;
2022 }
2023 }
2024 }
2025
2026 /* This must be called when these shaders are changed from non-NULL to NULL
2027 * and vice versa:
2028 * - geometry shader
2029 * - tessellation control shader
2030 * - tessellation evaluation shader
2031 */
2032 void si_shader_change_notify(struct si_context *sctx)
2033 {
2034 /* VS can be bound as VS, ES, or LS. */
2035 if (sctx->tes_shader.cso) {
2036 if (sctx->b.chip_class >= GFX9) {
2037 si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
2038 R_00B430_SPI_SHADER_USER_DATA_LS_0);
2039 } else {
2040 si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
2041 R_00B530_SPI_SHADER_USER_DATA_LS_0);
2042 }
2043 } else if (sctx->gs_shader.cso) {
2044 si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
2045 R_00B330_SPI_SHADER_USER_DATA_ES_0);
2046 } else {
2047 si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
2048 R_00B130_SPI_SHADER_USER_DATA_VS_0);
2049 }
2050
2051 /* TES can be bound as ES, VS, or not bound. */
2052 if (sctx->tes_shader.cso) {
2053 if (sctx->gs_shader.cso)
2054 si_set_user_data_base(sctx, PIPE_SHADER_TESS_EVAL,
2055 R_00B330_SPI_SHADER_USER_DATA_ES_0);
2056 else
2057 si_set_user_data_base(sctx, PIPE_SHADER_TESS_EVAL,
2058 R_00B130_SPI_SHADER_USER_DATA_VS_0);
2059 } else {
2060 si_set_user_data_base(sctx, PIPE_SHADER_TESS_EVAL, 0);
2061 }
2062 }
2063
2064 static void si_emit_shader_pointer_head(struct radeon_winsys_cs *cs,
2065 unsigned sh_offset,
2066 unsigned pointer_count)
2067 {
2068 radeon_emit(cs, PKT3(PKT3_SET_SH_REG, pointer_count * (HAVE_32BIT_POINTERS ? 1 : 2), 0));
2069 radeon_emit(cs, (sh_offset - SI_SH_REG_OFFSET) >> 2);
2070 }
2071
2072 static void si_emit_shader_pointer_body(struct si_screen *sscreen,
2073 struct radeon_winsys_cs *cs,
2074 uint64_t va)
2075 {
2076 radeon_emit(cs, va);
2077
2078 if (HAVE_32BIT_POINTERS)
2079 assert(va == 0 || (va >> 32) == sscreen->info.address32_hi);
2080 else
2081 radeon_emit(cs, va >> 32);
2082 }
2083
2084 static void si_emit_shader_pointer(struct si_context *sctx,
2085 struct si_descriptors *desc,
2086 unsigned sh_base)
2087 {
2088 struct radeon_winsys_cs *cs = sctx->b.gfx_cs;
2089 unsigned sh_offset = sh_base + desc->shader_userdata_offset;
2090
2091 si_emit_shader_pointer_head(cs, sh_offset, 1);
2092 si_emit_shader_pointer_body(sctx->screen, cs, desc->gpu_address);
2093 }
2094
2095 static void si_emit_consecutive_shader_pointers(struct si_context *sctx,
2096 unsigned pointer_mask,
2097 unsigned sh_base)
2098 {
2099 if (!sh_base)
2100 return;
2101
2102 struct radeon_winsys_cs *cs = sctx->b.gfx_cs;
2103 unsigned mask = sctx->shader_pointers_dirty & pointer_mask;
2104
2105 while (mask) {
2106 int start, count;
2107 u_bit_scan_consecutive_range(&mask, &start, &count);
2108
2109 struct si_descriptors *descs = &sctx->descriptors[start];
2110 unsigned sh_offset = sh_base + descs->shader_userdata_offset;
2111
2112 si_emit_shader_pointer_head(cs, sh_offset, count);
2113 for (int i = 0; i < count; i++)
2114 si_emit_shader_pointer_body(sctx->screen, cs,
2115 descs[i].gpu_address);
2116 }
2117 }
2118
2119 static void si_emit_disjoint_shader_pointers(struct si_context *sctx,
2120 unsigned pointer_mask,
2121 unsigned sh_base)
2122 {
2123 if (!sh_base)
2124 return;
2125
2126 struct radeon_winsys_cs *cs = sctx->b.gfx_cs;
2127 unsigned mask = sctx->shader_pointers_dirty & pointer_mask;
2128
2129 while (mask) {
2130 struct si_descriptors *descs = &sctx->descriptors[u_bit_scan(&mask)];
2131 unsigned sh_offset = sh_base + descs->shader_userdata_offset;
2132
2133 si_emit_shader_pointer_head(cs, sh_offset, 1);
2134 si_emit_shader_pointer_body(sctx->screen, cs, descs->gpu_address);
2135 }
2136 }
2137
2138 static void si_emit_global_shader_pointers(struct si_context *sctx,
2139 struct si_descriptors *descs)
2140 {
2141 if (sctx->b.chip_class == GFX9) {
2142 /* Broadcast it to all shader stages. */
2143 si_emit_shader_pointer(sctx, descs,
2144 R_00B530_SPI_SHADER_USER_DATA_COMMON_0);
2145 return;
2146 }
2147
2148 si_emit_shader_pointer(sctx, descs,
2149 R_00B030_SPI_SHADER_USER_DATA_PS_0);
2150 si_emit_shader_pointer(sctx, descs,
2151 R_00B130_SPI_SHADER_USER_DATA_VS_0);
2152 si_emit_shader_pointer(sctx, descs,
2153 R_00B330_SPI_SHADER_USER_DATA_ES_0);
2154 si_emit_shader_pointer(sctx, descs,
2155 R_00B230_SPI_SHADER_USER_DATA_GS_0);
2156 si_emit_shader_pointer(sctx, descs,
2157 R_00B430_SPI_SHADER_USER_DATA_HS_0);
2158 si_emit_shader_pointer(sctx, descs,
2159 R_00B530_SPI_SHADER_USER_DATA_LS_0);
2160 }
2161
2162 void si_emit_graphics_shader_pointers(struct si_context *sctx,
2163 struct r600_atom *atom)
2164 {
2165 uint32_t *sh_base = sctx->shader_pointers.sh_base;
2166
2167 if (sctx->shader_pointers_dirty & (1 << SI_DESCS_RW_BUFFERS)) {
2168 si_emit_global_shader_pointers(sctx,
2169 &sctx->descriptors[SI_DESCS_RW_BUFFERS]);
2170 }
2171
2172 si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(VERTEX),
2173 sh_base[PIPE_SHADER_VERTEX]);
2174 si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(TESS_EVAL),
2175 sh_base[PIPE_SHADER_TESS_EVAL]);
2176 si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(FRAGMENT),
2177 sh_base[PIPE_SHADER_FRAGMENT]);
2178 if (HAVE_32BIT_POINTERS || sctx->b.chip_class <= VI) {
2179 si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(TESS_CTRL),
2180 sh_base[PIPE_SHADER_TESS_CTRL]);
2181 si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(GEOMETRY),
2182 sh_base[PIPE_SHADER_GEOMETRY]);
2183 } else {
2184 si_emit_disjoint_shader_pointers(sctx, SI_DESCS_SHADER_MASK(TESS_CTRL),
2185 sh_base[PIPE_SHADER_TESS_CTRL]);
2186 si_emit_disjoint_shader_pointers(sctx, SI_DESCS_SHADER_MASK(GEOMETRY),
2187 sh_base[PIPE_SHADER_GEOMETRY]);
2188 }
2189
2190 sctx->shader_pointers_dirty &=
2191 ~u_bit_consecutive(SI_DESCS_RW_BUFFERS, SI_DESCS_FIRST_COMPUTE);
2192
2193 if (sctx->vertex_buffer_pointer_dirty) {
2194 struct radeon_winsys_cs *cs = sctx->b.gfx_cs;
2195
2196 /* Find the location of the VB descriptor pointer. */
2197 /* TODO: In the future, the pointer will be packed in unused
2198 * bits of the first 2 VB descriptors. */
2199 unsigned sh_dw_offset = SI_VS_NUM_USER_SGPR;
2200 if (sctx->b.chip_class >= GFX9) {
2201 if (sctx->tes_shader.cso)
2202 sh_dw_offset = GFX9_TCS_NUM_USER_SGPR;
2203 else if (sctx->gs_shader.cso)
2204 sh_dw_offset = GFX9_VSGS_NUM_USER_SGPR;
2205 }
2206
2207 unsigned sh_offset = sh_base[PIPE_SHADER_VERTEX] + sh_dw_offset * 4;
2208 si_emit_shader_pointer_head(cs, sh_offset, 1);
2209 si_emit_shader_pointer_body(sctx->screen, cs,
2210 sctx->vb_descriptors_buffer->gpu_address +
2211 sctx->vb_descriptors_offset);
2212 sctx->vertex_buffer_pointer_dirty = false;
2213 }
2214
2215 if (sctx->graphics_bindless_pointer_dirty) {
2216 si_emit_global_shader_pointers(sctx,
2217 &sctx->bindless_descriptors);
2218 sctx->graphics_bindless_pointer_dirty = false;
2219 }
2220 }
2221
2222 void si_emit_compute_shader_pointers(struct si_context *sctx)
2223 {
2224 unsigned base = R_00B900_COMPUTE_USER_DATA_0;
2225
2226 si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(COMPUTE),
2227 R_00B900_COMPUTE_USER_DATA_0);
2228 sctx->shader_pointers_dirty &= ~SI_DESCS_SHADER_MASK(COMPUTE);
2229
2230 if (sctx->compute_bindless_pointer_dirty) {
2231 si_emit_shader_pointer(sctx, &sctx->bindless_descriptors, base);
2232 sctx->compute_bindless_pointer_dirty = false;
2233 }
2234 }
2235
2236 /* BINDLESS */
2237
2238 static void si_init_bindless_descriptors(struct si_context *sctx,
2239 struct si_descriptors *desc,
2240 short shader_userdata_rel_index,
2241 unsigned num_elements)
2242 {
2243 MAYBE_UNUSED unsigned desc_slot;
2244
2245 si_init_descriptors(desc, shader_userdata_rel_index, 16, num_elements);
2246 sctx->bindless_descriptors.num_active_slots = num_elements;
2247
2248 /* The first bindless descriptor is stored at slot 1, because 0 is not
2249 * considered to be a valid handle.
2250 */
2251 sctx->num_bindless_descriptors = 1;
2252
2253 /* Track which bindless slots are used (or not). */
2254 util_idalloc_init(&sctx->bindless_used_slots);
2255 util_idalloc_resize(&sctx->bindless_used_slots, num_elements);
2256
2257 /* Reserve slot 0 because it's an invalid handle for bindless. */
2258 desc_slot = util_idalloc_alloc(&sctx->bindless_used_slots);
2259 assert(desc_slot == 0);
2260 }
2261
2262 static void si_release_bindless_descriptors(struct si_context *sctx)
2263 {
2264 si_release_descriptors(&sctx->bindless_descriptors);
2265 util_idalloc_fini(&sctx->bindless_used_slots);
2266 }
2267
2268 static unsigned si_get_first_free_bindless_slot(struct si_context *sctx)
2269 {
2270 struct si_descriptors *desc = &sctx->bindless_descriptors;
2271 unsigned desc_slot;
2272
2273 desc_slot = util_idalloc_alloc(&sctx->bindless_used_slots);
2274 if (desc_slot >= desc->num_elements) {
2275 /* The array of bindless descriptors is full, resize it. */
2276 unsigned slot_size = desc->element_dw_size * 4;
2277 unsigned new_num_elements = desc->num_elements * 2;
2278
2279 desc->list = REALLOC(desc->list, desc->num_elements * slot_size,
2280 new_num_elements * slot_size);
2281 desc->num_elements = new_num_elements;
2282 desc->num_active_slots = new_num_elements;
2283 }
2284
2285 assert(desc_slot);
2286 return desc_slot;
2287 }
2288
2289 static unsigned
2290 si_create_bindless_descriptor(struct si_context *sctx, uint32_t *desc_list,
2291 unsigned size)
2292 {
2293 struct si_descriptors *desc = &sctx->bindless_descriptors;
2294 unsigned desc_slot, desc_slot_offset;
2295
2296 /* Find a free slot. */
2297 desc_slot = si_get_first_free_bindless_slot(sctx);
2298
2299 /* For simplicity, sampler and image bindless descriptors use fixed
2300 * 16-dword slots for now. Image descriptors only need 8-dword but this
2301 * doesn't really matter because no real apps use image handles.
2302 */
2303 desc_slot_offset = desc_slot * 16;
2304
2305 /* Copy the descriptor into the array. */
2306 memcpy(desc->list + desc_slot_offset, desc_list, size);
2307
2308 /* Re-upload the whole array of bindless descriptors into a new buffer.
2309 */
2310 if (!si_upload_descriptors(sctx, desc))
2311 return 0;
2312
2313 /* Make sure to re-emit the shader pointers for all stages. */
2314 sctx->graphics_bindless_pointer_dirty = true;
2315 sctx->compute_bindless_pointer_dirty = true;
2316
2317 return desc_slot;
2318 }
2319
2320 static void si_update_bindless_buffer_descriptor(struct si_context *sctx,
2321 unsigned desc_slot,
2322 struct pipe_resource *resource,
2323 uint64_t offset,
2324 bool *desc_dirty)
2325 {
2326 struct si_descriptors *desc = &sctx->bindless_descriptors;
2327 struct r600_resource *buf = r600_resource(resource);
2328 unsigned desc_slot_offset = desc_slot * 16;
2329 uint32_t *desc_list = desc->list + desc_slot_offset + 4;
2330 uint64_t old_desc_va;
2331
2332 assert(resource->target == PIPE_BUFFER);
2333
2334 /* Retrieve the old buffer addr from the descriptor. */
2335 old_desc_va = si_desc_extract_buffer_address(desc_list);
2336
2337 if (old_desc_va != buf->gpu_address + offset) {
2338 /* The buffer has been invalidated when the handle wasn't
2339 * resident, update the descriptor and the dirty flag.
2340 */
2341 si_set_buf_desc_address(buf, offset, &desc_list[0]);
2342
2343 *desc_dirty = true;
2344 }
2345 }
2346
2347 static uint64_t si_create_texture_handle(struct pipe_context *ctx,
2348 struct pipe_sampler_view *view,
2349 const struct pipe_sampler_state *state)
2350 {
2351 struct si_sampler_view *sview = (struct si_sampler_view *)view;
2352 struct si_context *sctx = (struct si_context *)ctx;
2353 struct si_texture_handle *tex_handle;
2354 struct si_sampler_state *sstate;
2355 uint32_t desc_list[16];
2356 uint64_t handle;
2357
2358 tex_handle = CALLOC_STRUCT(si_texture_handle);
2359 if (!tex_handle)
2360 return 0;
2361
2362 memset(desc_list, 0, sizeof(desc_list));
2363 si_init_descriptor_list(&desc_list[0], 16, 1, null_texture_descriptor);
2364
2365 sstate = ctx->create_sampler_state(ctx, state);
2366 if (!sstate) {
2367 FREE(tex_handle);
2368 return 0;
2369 }
2370
2371 si_set_sampler_view_desc(sctx, sview, sstate, &desc_list[0]);
2372 memcpy(&tex_handle->sstate, sstate, sizeof(*sstate));
2373 ctx->delete_sampler_state(ctx, sstate);
2374
2375 tex_handle->desc_slot = si_create_bindless_descriptor(sctx, desc_list,
2376 sizeof(desc_list));
2377 if (!tex_handle->desc_slot) {
2378 FREE(tex_handle);
2379 return 0;
2380 }
2381
2382 handle = tex_handle->desc_slot;
2383
2384 if (!_mesa_hash_table_insert(sctx->tex_handles,
2385 (void *)(uintptr_t)handle,
2386 tex_handle)) {
2387 FREE(tex_handle);
2388 return 0;
2389 }
2390
2391 pipe_sampler_view_reference(&tex_handle->view, view);
2392
2393 r600_resource(sview->base.texture)->texture_handle_allocated = true;
2394
2395 return handle;
2396 }
2397
2398 static void si_delete_texture_handle(struct pipe_context *ctx, uint64_t handle)
2399 {
2400 struct si_context *sctx = (struct si_context *)ctx;
2401 struct si_texture_handle *tex_handle;
2402 struct hash_entry *entry;
2403
2404 entry = _mesa_hash_table_search(sctx->tex_handles,
2405 (void *)(uintptr_t)handle);
2406 if (!entry)
2407 return;
2408
2409 tex_handle = (struct si_texture_handle *)entry->data;
2410
2411 /* Allow this descriptor slot to be re-used. */
2412 util_idalloc_free(&sctx->bindless_used_slots, tex_handle->desc_slot);
2413
2414 pipe_sampler_view_reference(&tex_handle->view, NULL);
2415 _mesa_hash_table_remove(sctx->tex_handles, entry);
2416 FREE(tex_handle);
2417 }
2418
2419 static void si_make_texture_handle_resident(struct pipe_context *ctx,
2420 uint64_t handle, bool resident)
2421 {
2422 struct si_context *sctx = (struct si_context *)ctx;
2423 struct si_texture_handle *tex_handle;
2424 struct si_sampler_view *sview;
2425 struct hash_entry *entry;
2426
2427 entry = _mesa_hash_table_search(sctx->tex_handles,
2428 (void *)(uintptr_t)handle);
2429 if (!entry)
2430 return;
2431
2432 tex_handle = (struct si_texture_handle *)entry->data;
2433 sview = (struct si_sampler_view *)tex_handle->view;
2434
2435 if (resident) {
2436 if (sview->base.texture->target != PIPE_BUFFER) {
2437 struct r600_texture *rtex =
2438 (struct r600_texture *)sview->base.texture;
2439
2440 if (depth_needs_decompression(rtex)) {
2441 util_dynarray_append(
2442 &sctx->resident_tex_needs_depth_decompress,
2443 struct si_texture_handle *,
2444 tex_handle);
2445 }
2446
2447 if (color_needs_decompression(rtex)) {
2448 util_dynarray_append(
2449 &sctx->resident_tex_needs_color_decompress,
2450 struct si_texture_handle *,
2451 tex_handle);
2452 }
2453
2454 if (rtex->dcc_offset &&
2455 p_atomic_read(&rtex->framebuffers_bound))
2456 sctx->need_check_render_feedback = true;
2457
2458 si_update_bindless_texture_descriptor(sctx, tex_handle);
2459 } else {
2460 si_update_bindless_buffer_descriptor(sctx,
2461 tex_handle->desc_slot,
2462 sview->base.texture,
2463 sview->base.u.buf.offset,
2464 &tex_handle->desc_dirty);
2465 }
2466
2467 /* Re-upload the descriptor if it has been updated while it
2468 * wasn't resident.
2469 */
2470 if (tex_handle->desc_dirty)
2471 sctx->bindless_descriptors_dirty = true;
2472
2473 /* Add the texture handle to the per-context list. */
2474 util_dynarray_append(&sctx->resident_tex_handles,
2475 struct si_texture_handle *, tex_handle);
2476
2477 /* Add the buffers to the current CS in case si_begin_new_cs()
2478 * is not going to be called.
2479 */
2480 si_sampler_view_add_buffer(sctx, sview->base.texture,
2481 RADEON_USAGE_READ,
2482 sview->is_stencil_sampler, false);
2483 } else {
2484 /* Remove the texture handle from the per-context list. */
2485 util_dynarray_delete_unordered(&sctx->resident_tex_handles,
2486 struct si_texture_handle *,
2487 tex_handle);
2488
2489 if (sview->base.texture->target != PIPE_BUFFER) {
2490 util_dynarray_delete_unordered(
2491 &sctx->resident_tex_needs_depth_decompress,
2492 struct si_texture_handle *, tex_handle);
2493
2494 util_dynarray_delete_unordered(
2495 &sctx->resident_tex_needs_color_decompress,
2496 struct si_texture_handle *, tex_handle);
2497 }
2498 }
2499 }
2500
2501 static uint64_t si_create_image_handle(struct pipe_context *ctx,
2502 const struct pipe_image_view *view)
2503 {
2504 struct si_context *sctx = (struct si_context *)ctx;
2505 struct si_image_handle *img_handle;
2506 uint32_t desc_list[8];
2507 uint64_t handle;
2508
2509 if (!view || !view->resource)
2510 return 0;
2511
2512 img_handle = CALLOC_STRUCT(si_image_handle);
2513 if (!img_handle)
2514 return 0;
2515
2516 memset(desc_list, 0, sizeof(desc_list));
2517 si_init_descriptor_list(&desc_list[0], 8, 1, null_image_descriptor);
2518
2519 si_set_shader_image_desc(sctx, view, false, &desc_list[0], NULL);
2520
2521 img_handle->desc_slot = si_create_bindless_descriptor(sctx, desc_list,
2522 sizeof(desc_list));
2523 if (!img_handle->desc_slot) {
2524 FREE(img_handle);
2525 return 0;
2526 }
2527
2528 handle = img_handle->desc_slot;
2529
2530 if (!_mesa_hash_table_insert(sctx->img_handles,
2531 (void *)(uintptr_t)handle,
2532 img_handle)) {
2533 FREE(img_handle);
2534 return 0;
2535 }
2536
2537 util_copy_image_view(&img_handle->view, view);
2538
2539 r600_resource(view->resource)->image_handle_allocated = true;
2540
2541 return handle;
2542 }
2543
2544 static void si_delete_image_handle(struct pipe_context *ctx, uint64_t handle)
2545 {
2546 struct si_context *sctx = (struct si_context *)ctx;
2547 struct si_image_handle *img_handle;
2548 struct hash_entry *entry;
2549
2550 entry = _mesa_hash_table_search(sctx->img_handles,
2551 (void *)(uintptr_t)handle);
2552 if (!entry)
2553 return;
2554
2555 img_handle = (struct si_image_handle *)entry->data;
2556
2557 util_copy_image_view(&img_handle->view, NULL);
2558 _mesa_hash_table_remove(sctx->img_handles, entry);
2559 FREE(img_handle);
2560 }
2561
2562 static void si_make_image_handle_resident(struct pipe_context *ctx,
2563 uint64_t handle, unsigned access,
2564 bool resident)
2565 {
2566 struct si_context *sctx = (struct si_context *)ctx;
2567 struct si_image_handle *img_handle;
2568 struct pipe_image_view *view;
2569 struct r600_resource *res;
2570 struct hash_entry *entry;
2571
2572 entry = _mesa_hash_table_search(sctx->img_handles,
2573 (void *)(uintptr_t)handle);
2574 if (!entry)
2575 return;
2576
2577 img_handle = (struct si_image_handle *)entry->data;
2578 view = &img_handle->view;
2579 res = (struct r600_resource *)view->resource;
2580
2581 if (resident) {
2582 if (res->b.b.target != PIPE_BUFFER) {
2583 struct r600_texture *rtex = (struct r600_texture *)res;
2584 unsigned level = view->u.tex.level;
2585
2586 if (color_needs_decompression(rtex)) {
2587 util_dynarray_append(
2588 &sctx->resident_img_needs_color_decompress,
2589 struct si_image_handle *,
2590 img_handle);
2591 }
2592
2593 if (vi_dcc_enabled(rtex, level) &&
2594 p_atomic_read(&rtex->framebuffers_bound))
2595 sctx->need_check_render_feedback = true;
2596
2597 si_update_bindless_image_descriptor(sctx, img_handle);
2598 } else {
2599 si_update_bindless_buffer_descriptor(sctx,
2600 img_handle->desc_slot,
2601 view->resource,
2602 view->u.buf.offset,
2603 &img_handle->desc_dirty);
2604 }
2605
2606 /* Re-upload the descriptor if it has been updated while it
2607 * wasn't resident.
2608 */
2609 if (img_handle->desc_dirty)
2610 sctx->bindless_descriptors_dirty = true;
2611
2612 /* Add the image handle to the per-context list. */
2613 util_dynarray_append(&sctx->resident_img_handles,
2614 struct si_image_handle *, img_handle);
2615
2616 /* Add the buffers to the current CS in case si_begin_new_cs()
2617 * is not going to be called.
2618 */
2619 si_sampler_view_add_buffer(sctx, view->resource,
2620 (access & PIPE_IMAGE_ACCESS_WRITE) ?
2621 RADEON_USAGE_READWRITE :
2622 RADEON_USAGE_READ, false, false);
2623 } else {
2624 /* Remove the image handle from the per-context list. */
2625 util_dynarray_delete_unordered(&sctx->resident_img_handles,
2626 struct si_image_handle *,
2627 img_handle);
2628
2629 if (res->b.b.target != PIPE_BUFFER) {
2630 util_dynarray_delete_unordered(
2631 &sctx->resident_img_needs_color_decompress,
2632 struct si_image_handle *,
2633 img_handle);
2634 }
2635 }
2636 }
2637
2638
2639 void si_all_resident_buffers_begin_new_cs(struct si_context *sctx)
2640 {
2641 unsigned num_resident_tex_handles, num_resident_img_handles;
2642
2643 num_resident_tex_handles = sctx->resident_tex_handles.size /
2644 sizeof(struct si_texture_handle *);
2645 num_resident_img_handles = sctx->resident_img_handles.size /
2646 sizeof(struct si_image_handle *);
2647
2648 /* Add all resident texture handles. */
2649 util_dynarray_foreach(&sctx->resident_tex_handles,
2650 struct si_texture_handle *, tex_handle) {
2651 struct si_sampler_view *sview =
2652 (struct si_sampler_view *)(*tex_handle)->view;
2653
2654 si_sampler_view_add_buffer(sctx, sview->base.texture,
2655 RADEON_USAGE_READ,
2656 sview->is_stencil_sampler, false);
2657 }
2658
2659 /* Add all resident image handles. */
2660 util_dynarray_foreach(&sctx->resident_img_handles,
2661 struct si_image_handle *, img_handle) {
2662 struct pipe_image_view *view = &(*img_handle)->view;
2663
2664 si_sampler_view_add_buffer(sctx, view->resource,
2665 RADEON_USAGE_READWRITE,
2666 false, false);
2667 }
2668
2669 sctx->b.num_resident_handles += num_resident_tex_handles +
2670 num_resident_img_handles;
2671 }
2672
2673 /* INIT/DEINIT/UPLOAD */
2674
2675 void si_init_all_descriptors(struct si_context *sctx)
2676 {
2677 int i;
2678
2679 #if !HAVE_32BIT_POINTERS
2680 STATIC_ASSERT(GFX9_SGPR_2ND_SAMPLERS_AND_IMAGES % 2 == 0);
2681 #endif
2682
2683 for (i = 0; i < SI_NUM_SHADERS; i++) {
2684 bool is_2nd = sctx->b.chip_class >= GFX9 &&
2685 (i == PIPE_SHADER_TESS_CTRL ||
2686 i == PIPE_SHADER_GEOMETRY);
2687 unsigned num_sampler_slots = SI_NUM_IMAGES / 2 + SI_NUM_SAMPLERS;
2688 unsigned num_buffer_slots = SI_NUM_SHADER_BUFFERS + SI_NUM_CONST_BUFFERS;
2689 int rel_dw_offset;
2690 struct si_descriptors *desc;
2691
2692 if (is_2nd) {
2693 if (i == PIPE_SHADER_TESS_CTRL) {
2694 rel_dw_offset = (R_00B408_SPI_SHADER_USER_DATA_ADDR_LO_HS -
2695 R_00B430_SPI_SHADER_USER_DATA_LS_0) / 4;
2696 } else { /* PIPE_SHADER_GEOMETRY */
2697 rel_dw_offset = (R_00B208_SPI_SHADER_USER_DATA_ADDR_LO_GS -
2698 R_00B330_SPI_SHADER_USER_DATA_ES_0) / 4;
2699 }
2700 } else {
2701 rel_dw_offset = SI_SGPR_CONST_AND_SHADER_BUFFERS;
2702 }
2703 desc = si_const_and_shader_buffer_descriptors(sctx, i);
2704 si_init_buffer_resources(&sctx->const_and_shader_buffers[i], desc,
2705 num_buffer_slots, rel_dw_offset,
2706 RADEON_USAGE_READWRITE,
2707 RADEON_USAGE_READ,
2708 RADEON_PRIO_SHADER_RW_BUFFER,
2709 RADEON_PRIO_CONST_BUFFER);
2710 desc->slot_index_to_bind_directly = si_get_constbuf_slot(0);
2711
2712 if (is_2nd) {
2713 #if HAVE_32BIT_POINTERS
2714 if (i == PIPE_SHADER_TESS_CTRL) {
2715 rel_dw_offset = (R_00B40C_SPI_SHADER_USER_DATA_ADDR_HI_HS -
2716 R_00B430_SPI_SHADER_USER_DATA_LS_0) / 4;
2717 } else { /* PIPE_SHADER_GEOMETRY */
2718 rel_dw_offset = (R_00B20C_SPI_SHADER_USER_DATA_ADDR_HI_GS -
2719 R_00B330_SPI_SHADER_USER_DATA_ES_0) / 4;
2720 }
2721 #else
2722 rel_dw_offset = GFX9_SGPR_2ND_SAMPLERS_AND_IMAGES;
2723 #endif
2724 } else {
2725 rel_dw_offset = SI_SGPR_SAMPLERS_AND_IMAGES;
2726 }
2727
2728 desc = si_sampler_and_image_descriptors(sctx, i);
2729 si_init_descriptors(desc, rel_dw_offset, 16, num_sampler_slots);
2730
2731 int j;
2732 for (j = 0; j < SI_NUM_IMAGES; j++)
2733 memcpy(desc->list + j * 8, null_image_descriptor, 8 * 4);
2734 for (; j < SI_NUM_IMAGES + SI_NUM_SAMPLERS * 2; j++)
2735 memcpy(desc->list + j * 8, null_texture_descriptor, 8 * 4);
2736 }
2737
2738 si_init_buffer_resources(&sctx->rw_buffers,
2739 &sctx->descriptors[SI_DESCS_RW_BUFFERS],
2740 SI_NUM_RW_BUFFERS, SI_SGPR_RW_BUFFERS,
2741 /* The second set of usage/priority is used by
2742 * const buffers in RW buffer slots. */
2743 RADEON_USAGE_READWRITE, RADEON_USAGE_READ,
2744 RADEON_PRIO_SHADER_RINGS, RADEON_PRIO_CONST_BUFFER);
2745 sctx->descriptors[SI_DESCS_RW_BUFFERS].num_active_slots = SI_NUM_RW_BUFFERS;
2746
2747 /* Initialize an array of 1024 bindless descriptors, when the limit is
2748 * reached, just make it larger and re-upload the whole array.
2749 */
2750 si_init_bindless_descriptors(sctx, &sctx->bindless_descriptors,
2751 SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES,
2752 1024);
2753
2754 sctx->descriptors_dirty = u_bit_consecutive(0, SI_NUM_DESCS);
2755
2756 /* Set pipe_context functions. */
2757 sctx->b.b.bind_sampler_states = si_bind_sampler_states;
2758 sctx->b.b.set_shader_images = si_set_shader_images;
2759 sctx->b.b.set_constant_buffer = si_pipe_set_constant_buffer;
2760 sctx->b.b.set_polygon_stipple = si_set_polygon_stipple;
2761 sctx->b.b.set_shader_buffers = si_set_shader_buffers;
2762 sctx->b.b.set_sampler_views = si_set_sampler_views;
2763 sctx->b.b.create_texture_handle = si_create_texture_handle;
2764 sctx->b.b.delete_texture_handle = si_delete_texture_handle;
2765 sctx->b.b.make_texture_handle_resident = si_make_texture_handle_resident;
2766 sctx->b.b.create_image_handle = si_create_image_handle;
2767 sctx->b.b.delete_image_handle = si_delete_image_handle;
2768 sctx->b.b.make_image_handle_resident = si_make_image_handle_resident;
2769
2770 /* Shader user data. */
2771 si_init_atom(sctx, &sctx->shader_pointers.atom, &sctx->atoms.s.shader_pointers,
2772 si_emit_graphics_shader_pointers);
2773
2774 /* Set default and immutable mappings. */
2775 si_set_user_data_base(sctx, PIPE_SHADER_VERTEX, R_00B130_SPI_SHADER_USER_DATA_VS_0);
2776
2777 if (sctx->b.chip_class >= GFX9) {
2778 si_set_user_data_base(sctx, PIPE_SHADER_TESS_CTRL,
2779 R_00B430_SPI_SHADER_USER_DATA_LS_0);
2780 si_set_user_data_base(sctx, PIPE_SHADER_GEOMETRY,
2781 R_00B330_SPI_SHADER_USER_DATA_ES_0);
2782 } else {
2783 si_set_user_data_base(sctx, PIPE_SHADER_TESS_CTRL,
2784 R_00B430_SPI_SHADER_USER_DATA_HS_0);
2785 si_set_user_data_base(sctx, PIPE_SHADER_GEOMETRY,
2786 R_00B230_SPI_SHADER_USER_DATA_GS_0);
2787 }
2788 si_set_user_data_base(sctx, PIPE_SHADER_FRAGMENT, R_00B030_SPI_SHADER_USER_DATA_PS_0);
2789 }
2790
2791 static bool si_upload_shader_descriptors(struct si_context *sctx, unsigned mask)
2792 {
2793 unsigned dirty = sctx->descriptors_dirty & mask;
2794
2795 /* Assume nothing will go wrong: */
2796 sctx->shader_pointers_dirty |= dirty;
2797
2798 while (dirty) {
2799 unsigned i = u_bit_scan(&dirty);
2800
2801 if (!si_upload_descriptors(sctx, &sctx->descriptors[i]))
2802 return false;
2803 }
2804
2805 sctx->descriptors_dirty &= ~mask;
2806
2807 si_upload_bindless_descriptors(sctx);
2808
2809 return true;
2810 }
2811
2812 bool si_upload_graphics_shader_descriptors(struct si_context *sctx)
2813 {
2814 const unsigned mask = u_bit_consecutive(0, SI_DESCS_FIRST_COMPUTE);
2815 return si_upload_shader_descriptors(sctx, mask);
2816 }
2817
2818 bool si_upload_compute_shader_descriptors(struct si_context *sctx)
2819 {
2820 /* Does not update rw_buffers as that is not needed for compute shaders
2821 * and the input buffer is using the same SGPR's anyway.
2822 */
2823 const unsigned mask = u_bit_consecutive(SI_DESCS_FIRST_COMPUTE,
2824 SI_NUM_DESCS - SI_DESCS_FIRST_COMPUTE);
2825 return si_upload_shader_descriptors(sctx, mask);
2826 }
2827
2828 void si_release_all_descriptors(struct si_context *sctx)
2829 {
2830 int i;
2831
2832 for (i = 0; i < SI_NUM_SHADERS; i++) {
2833 si_release_buffer_resources(&sctx->const_and_shader_buffers[i],
2834 si_const_and_shader_buffer_descriptors(sctx, i));
2835 si_release_sampler_views(&sctx->samplers[i]);
2836 si_release_image_views(&sctx->images[i]);
2837 }
2838 si_release_buffer_resources(&sctx->rw_buffers,
2839 &sctx->descriptors[SI_DESCS_RW_BUFFERS]);
2840 for (i = 0; i < SI_NUM_VERTEX_BUFFERS; i++)
2841 pipe_vertex_buffer_unreference(&sctx->vertex_buffer[i]);
2842
2843 for (i = 0; i < SI_NUM_DESCS; ++i)
2844 si_release_descriptors(&sctx->descriptors[i]);
2845
2846 r600_resource_reference(&sctx->vb_descriptors_buffer, NULL);
2847 sctx->vb_descriptors_gpu_list = NULL; /* points into a mapped buffer */
2848
2849 si_release_bindless_descriptors(sctx);
2850 }
2851
2852 void si_all_descriptors_begin_new_cs(struct si_context *sctx)
2853 {
2854 int i;
2855
2856 for (i = 0; i < SI_NUM_SHADERS; i++) {
2857 si_buffer_resources_begin_new_cs(sctx, &sctx->const_and_shader_buffers[i]);
2858 si_sampler_views_begin_new_cs(sctx, &sctx->samplers[i]);
2859 si_image_views_begin_new_cs(sctx, &sctx->images[i]);
2860 }
2861 si_buffer_resources_begin_new_cs(sctx, &sctx->rw_buffers);
2862 si_vertex_buffers_begin_new_cs(sctx);
2863
2864 for (i = 0; i < SI_NUM_DESCS; ++i)
2865 si_descriptors_begin_new_cs(sctx, &sctx->descriptors[i]);
2866 si_descriptors_begin_new_cs(sctx, &sctx->bindless_descriptors);
2867
2868 si_shader_pointers_begin_new_cs(sctx);
2869 }
2870
2871 void si_set_active_descriptors(struct si_context *sctx, unsigned desc_idx,
2872 uint64_t new_active_mask)
2873 {
2874 struct si_descriptors *desc = &sctx->descriptors[desc_idx];
2875
2876 /* Ignore no-op updates and updates that disable all slots. */
2877 if (!new_active_mask ||
2878 new_active_mask == u_bit_consecutive64(desc->first_active_slot,
2879 desc->num_active_slots))
2880 return;
2881
2882 int first, count;
2883 u_bit_scan_consecutive_range64(&new_active_mask, &first, &count);
2884 assert(new_active_mask == 0);
2885
2886 /* Upload/dump descriptors if slots are being enabled. */
2887 if (first < desc->first_active_slot ||
2888 first + count > desc->first_active_slot + desc->num_active_slots)
2889 sctx->descriptors_dirty |= 1u << desc_idx;
2890
2891 desc->first_active_slot = first;
2892 desc->num_active_slots = count;
2893 }
2894
2895 void si_set_active_descriptors_for_shader(struct si_context *sctx,
2896 struct si_shader_selector *sel)
2897 {
2898 if (!sel)
2899 return;
2900
2901 si_set_active_descriptors(sctx,
2902 si_const_and_shader_buffer_descriptors_idx(sel->type),
2903 sel->active_const_and_shader_buffers);
2904 si_set_active_descriptors(sctx,
2905 si_sampler_and_image_descriptors_idx(sel->type),
2906 sel->active_samplers_and_images);
2907 }