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