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