1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2018 Advanced Micro Devices, Inc.
3bf215546Sopenharmony_ci * All Rights Reserved.
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
9bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
10bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci */
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "si_pipe.h"
27bf215546Sopenharmony_ci#include "util/format/u_format.h"
28bf215546Sopenharmony_ci#include "util/format_srgb.h"
29bf215546Sopenharmony_ci#include "util/u_helpers.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_cistatic bool si_can_use_compute_blit(struct si_context *sctx, enum pipe_format format,
32bf215546Sopenharmony_ci                                    unsigned num_samples, bool is_store, bool has_dcc)
33bf215546Sopenharmony_ci{
34bf215546Sopenharmony_ci   /* TODO: This format fails AMD_TEST=imagecopy. */
35bf215546Sopenharmony_ci   if (format == PIPE_FORMAT_A8R8_UNORM && is_store)
36bf215546Sopenharmony_ci      return false;
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   if (num_samples > 1)
39bf215546Sopenharmony_ci      return false;
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci   if (util_format_is_depth_or_stencil(format))
42bf215546Sopenharmony_ci      return false;
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   /* Image stores support DCC since GFX10. */
45bf215546Sopenharmony_ci   if (has_dcc && is_store && sctx->gfx_level < GFX10)
46bf215546Sopenharmony_ci      return false;
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   return true;
49bf215546Sopenharmony_ci}
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_cistatic void si_use_compute_copy_for_float_formats(struct si_context *sctx,
52bf215546Sopenharmony_ci                                                  struct pipe_resource *texture,
53bf215546Sopenharmony_ci                                                  unsigned level)
54bf215546Sopenharmony_ci{
55bf215546Sopenharmony_ci   struct si_texture *tex = (struct si_texture *)texture;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   /* If we are uploading into FP16 or R11G11B10_FLOAT via a blit, CB clobbers NaNs,
58bf215546Sopenharmony_ci    * so in order to preserve them exactly, we have to use the compute blit.
59bf215546Sopenharmony_ci    * The compute blit is used only when the destination doesn't have DCC, so
60bf215546Sopenharmony_ci    * disable it here, which is kinda a hack.
61bf215546Sopenharmony_ci    * If we are uploading into 32-bit floats with DCC via a blit, NaNs will also get
62bf215546Sopenharmony_ci    * lost so we need to disable DCC as well.
63bf215546Sopenharmony_ci    *
64bf215546Sopenharmony_ci    * This makes KHR-GL45.texture_view.view_classes pass on gfx9.
65bf215546Sopenharmony_ci    */
66bf215546Sopenharmony_ci   if (vi_dcc_enabled(tex, level) &&
67bf215546Sopenharmony_ci       util_format_is_float(texture->format) &&
68bf215546Sopenharmony_ci       /* Check if disabling DCC enables the compute copy. */
69bf215546Sopenharmony_ci       !si_can_use_compute_blit(sctx, texture->format, texture->nr_samples, true, true) &&
70bf215546Sopenharmony_ci       si_can_use_compute_blit(sctx, texture->format, texture->nr_samples, true, false)) {
71bf215546Sopenharmony_ci      si_texture_disable_dcc(sctx, tex);
72bf215546Sopenharmony_ci   }
73bf215546Sopenharmony_ci}
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci/* Determine the cache policy. */
76bf215546Sopenharmony_cistatic enum si_cache_policy get_cache_policy(struct si_context *sctx, enum si_coherency coher,
77bf215546Sopenharmony_ci                                             uint64_t size)
78bf215546Sopenharmony_ci{
79bf215546Sopenharmony_ci   if ((sctx->gfx_level >= GFX9 && (coher == SI_COHERENCY_CB_META ||
80bf215546Sopenharmony_ci                                     coher == SI_COHERENCY_DB_META ||
81bf215546Sopenharmony_ci                                     coher == SI_COHERENCY_CP)) ||
82bf215546Sopenharmony_ci       (sctx->gfx_level >= GFX7 && coher == SI_COHERENCY_SHADER))
83bf215546Sopenharmony_ci      return L2_LRU; /* it's faster if L2 doesn't evict anything  */
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   return L2_BYPASS;
86bf215546Sopenharmony_ci}
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ciunsigned si_get_flush_flags(struct si_context *sctx, enum si_coherency coher,
89bf215546Sopenharmony_ci                            enum si_cache_policy cache_policy)
90bf215546Sopenharmony_ci{
91bf215546Sopenharmony_ci   switch (coher) {
92bf215546Sopenharmony_ci   default:
93bf215546Sopenharmony_ci   case SI_COHERENCY_NONE:
94bf215546Sopenharmony_ci   case SI_COHERENCY_CP:
95bf215546Sopenharmony_ci      return 0;
96bf215546Sopenharmony_ci   case SI_COHERENCY_SHADER:
97bf215546Sopenharmony_ci      return SI_CONTEXT_INV_SCACHE | SI_CONTEXT_INV_VCACHE |
98bf215546Sopenharmony_ci             (cache_policy == L2_BYPASS ? SI_CONTEXT_INV_L2 : 0);
99bf215546Sopenharmony_ci   case SI_COHERENCY_CB_META:
100bf215546Sopenharmony_ci      return SI_CONTEXT_FLUSH_AND_INV_CB;
101bf215546Sopenharmony_ci   case SI_COHERENCY_DB_META:
102bf215546Sopenharmony_ci      return SI_CONTEXT_FLUSH_AND_INV_DB;
103bf215546Sopenharmony_ci   }
104bf215546Sopenharmony_ci}
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_cistatic bool si_is_buffer_idle(struct si_context *sctx, struct si_resource *buf,
107bf215546Sopenharmony_ci                              unsigned usage)
108bf215546Sopenharmony_ci{
109bf215546Sopenharmony_ci   return !si_cs_is_buffer_referenced(sctx, buf->buf, usage) &&
110bf215546Sopenharmony_ci          sctx->ws->buffer_wait(sctx->ws, buf->buf, 0, usage);
111bf215546Sopenharmony_ci}
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_cistatic void si_improve_sync_flags(struct si_context *sctx, struct pipe_resource *dst,
114bf215546Sopenharmony_ci                                  struct pipe_resource *src, unsigned *flags)
115bf215546Sopenharmony_ci{
116bf215546Sopenharmony_ci   if (dst->target != PIPE_BUFFER || (src && src->target != PIPE_BUFFER))
117bf215546Sopenharmony_ci      return;
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   if (si_is_buffer_idle(sctx, si_resource(dst), RADEON_USAGE_READWRITE) &&
120bf215546Sopenharmony_ci       (!src || si_is_buffer_idle(sctx, si_resource(src), RADEON_USAGE_WRITE))) {
121bf215546Sopenharmony_ci      /* Idle buffers don't have to sync. */
122bf215546Sopenharmony_ci      *flags &= ~(SI_OP_SYNC_GE_BEFORE | SI_OP_SYNC_PS_BEFORE | SI_OP_SYNC_CS_BEFORE |
123bf215546Sopenharmony_ci                  SI_OP_SYNC_CPDMA_BEFORE);
124bf215546Sopenharmony_ci      return;
125bf215546Sopenharmony_ci   }
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci   const unsigned cs_mask = SI_BIND_CONSTANT_BUFFER(PIPE_SHADER_COMPUTE) |
128bf215546Sopenharmony_ci                            SI_BIND_SHADER_BUFFER(PIPE_SHADER_COMPUTE) |
129bf215546Sopenharmony_ci                            SI_BIND_IMAGE_BUFFER(PIPE_SHADER_COMPUTE) |
130bf215546Sopenharmony_ci                            SI_BIND_SAMPLER_BUFFER(PIPE_SHADER_COMPUTE);
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci   const unsigned ps_mask = SI_BIND_CONSTANT_BUFFER(PIPE_SHADER_FRAGMENT) |
133bf215546Sopenharmony_ci                            SI_BIND_SHADER_BUFFER(PIPE_SHADER_FRAGMENT) |
134bf215546Sopenharmony_ci                            SI_BIND_IMAGE_BUFFER(PIPE_SHADER_FRAGMENT) |
135bf215546Sopenharmony_ci                            SI_BIND_SAMPLER_BUFFER(PIPE_SHADER_FRAGMENT);
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   unsigned bind_history = si_resource(dst)->bind_history |
138bf215546Sopenharmony_ci                           (src ? si_resource(src)->bind_history : 0);
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   /* Clear SI_OP_SYNC_CS_BEFORE if the buffer has never been used with a CS. */
141bf215546Sopenharmony_ci   if (*flags & SI_OP_SYNC_CS_BEFORE && !(bind_history & cs_mask))
142bf215546Sopenharmony_ci      *flags &= ~SI_OP_SYNC_CS_BEFORE;
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci   /* Clear SI_OP_SYNC_PS_BEFORE if the buffer has never been used with a PS. */
145bf215546Sopenharmony_ci   if (*flags & SI_OP_SYNC_PS_BEFORE && !(bind_history & ps_mask)) {
146bf215546Sopenharmony_ci      *flags &= ~SI_OP_SYNC_PS_BEFORE;
147bf215546Sopenharmony_ci      *flags |= SI_OP_SYNC_GE_BEFORE;
148bf215546Sopenharmony_ci   }
149bf215546Sopenharmony_ci}
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_cistatic void si_launch_grid_internal(struct si_context *sctx, const struct pipe_grid_info *info,
152bf215546Sopenharmony_ci                                    void *shader, unsigned flags)
153bf215546Sopenharmony_ci{
154bf215546Sopenharmony_ci   /* Wait for previous shaders to finish. */
155bf215546Sopenharmony_ci   if (flags & SI_OP_SYNC_GE_BEFORE)
156bf215546Sopenharmony_ci      sctx->flags |= SI_CONTEXT_VS_PARTIAL_FLUSH;
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci   if (flags & SI_OP_SYNC_PS_BEFORE)
159bf215546Sopenharmony_ci      sctx->flags |= SI_CONTEXT_PS_PARTIAL_FLUSH;
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci   if (flags & SI_OP_SYNC_CS_BEFORE)
162bf215546Sopenharmony_ci      sctx->flags |= SI_CONTEXT_CS_PARTIAL_FLUSH;
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci   if (!(flags & SI_OP_CS_IMAGE))
165bf215546Sopenharmony_ci      sctx->flags |= SI_CONTEXT_PFP_SYNC_ME;
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci   /* Invalidate L0-L1 caches. */
168bf215546Sopenharmony_ci   /* sL0 is never invalidated, because src resources don't use it. */
169bf215546Sopenharmony_ci   if (!(flags & SI_OP_SKIP_CACHE_INV_BEFORE))
170bf215546Sopenharmony_ci      sctx->flags |= SI_CONTEXT_INV_VCACHE;
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci   /* Set settings for driver-internal compute dispatches. */
173bf215546Sopenharmony_ci   sctx->flags &= ~SI_CONTEXT_START_PIPELINE_STATS;
174bf215546Sopenharmony_ci   sctx->flags |= SI_CONTEXT_STOP_PIPELINE_STATS;
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci   if (!(flags & SI_OP_CS_RENDER_COND_ENABLE))
177bf215546Sopenharmony_ci      sctx->render_cond_enabled = false;
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci   /* Skip decompression to prevent infinite recursion. */
180bf215546Sopenharmony_ci   sctx->blitter_running = true;
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci   /* Dispatch compute. */
183bf215546Sopenharmony_ci   void *saved_cs = sctx->cs_shader_state.program;
184bf215546Sopenharmony_ci   sctx->b.bind_compute_state(&sctx->b, shader);
185bf215546Sopenharmony_ci   sctx->b.launch_grid(&sctx->b, info);
186bf215546Sopenharmony_ci   sctx->b.bind_compute_state(&sctx->b, saved_cs);
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci   /* Restore default settings. */
189bf215546Sopenharmony_ci   sctx->flags &= ~SI_CONTEXT_STOP_PIPELINE_STATS;
190bf215546Sopenharmony_ci   sctx->flags |= SI_CONTEXT_START_PIPELINE_STATS;
191bf215546Sopenharmony_ci   sctx->render_cond_enabled = sctx->render_cond;
192bf215546Sopenharmony_ci   sctx->blitter_running = false;
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_ci   if (flags & SI_OP_SYNC_AFTER) {
195bf215546Sopenharmony_ci      sctx->flags |= SI_CONTEXT_CS_PARTIAL_FLUSH;
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci      if (flags & SI_OP_CS_IMAGE) {
198bf215546Sopenharmony_ci         /* Make sure image stores are visible to CB, which doesn't use L2 on GFX6-8. */
199bf215546Sopenharmony_ci         sctx->flags |= sctx->gfx_level <= GFX8 ? SI_CONTEXT_WB_L2 : 0;
200bf215546Sopenharmony_ci         /* Make sure image stores are visible to all CUs. */
201bf215546Sopenharmony_ci         sctx->flags |= SI_CONTEXT_INV_VCACHE;
202bf215546Sopenharmony_ci         /* Make sure RBs see our DCC changes. */
203bf215546Sopenharmony_ci         if (sctx->gfx_level >= GFX10 && sctx->screen->info.tcc_rb_non_coherent) {
204bf215546Sopenharmony_ci            unsigned enabled_mask = sctx->images[PIPE_SHADER_COMPUTE].enabled_mask;
205bf215546Sopenharmony_ci            while (enabled_mask) {
206bf215546Sopenharmony_ci               int i = u_bit_scan(&enabled_mask);
207bf215546Sopenharmony_ci               if (sctx->images[PIPE_SHADER_COMPUTE].views[i].access & SI_IMAGE_ACCESS_ALLOW_DCC_STORE) {
208bf215546Sopenharmony_ci                  sctx->flags |= SI_CONTEXT_INV_L2;
209bf215546Sopenharmony_ci                  break;
210bf215546Sopenharmony_ci               }
211bf215546Sopenharmony_ci            }
212bf215546Sopenharmony_ci         }
213bf215546Sopenharmony_ci      } else {
214bf215546Sopenharmony_ci         /* Make sure buffer stores are visible to all CUs. */
215bf215546Sopenharmony_ci         sctx->flags |= SI_CONTEXT_INV_SCACHE | SI_CONTEXT_INV_VCACHE | SI_CONTEXT_PFP_SYNC_ME;
216bf215546Sopenharmony_ci      }
217bf215546Sopenharmony_ci   }
218bf215546Sopenharmony_ci}
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_civoid si_launch_grid_internal_ssbos(struct si_context *sctx, struct pipe_grid_info *info,
221bf215546Sopenharmony_ci                                   void *shader, unsigned flags, enum si_coherency coher,
222bf215546Sopenharmony_ci                                   unsigned num_buffers, const struct pipe_shader_buffer *buffers,
223bf215546Sopenharmony_ci                                   unsigned writeable_bitmask)
224bf215546Sopenharmony_ci{
225bf215546Sopenharmony_ci   if (!(flags & SI_OP_SKIP_CACHE_INV_BEFORE))
226bf215546Sopenharmony_ci      sctx->flags |= si_get_flush_flags(sctx, coher, SI_COMPUTE_DST_CACHE_POLICY);
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci   /* Save states. */
229bf215546Sopenharmony_ci   struct pipe_shader_buffer saved_sb[3] = {};
230bf215546Sopenharmony_ci   assert(num_buffers <= ARRAY_SIZE(saved_sb));
231bf215546Sopenharmony_ci   si_get_shader_buffers(sctx, PIPE_SHADER_COMPUTE, 0, num_buffers, saved_sb);
232bf215546Sopenharmony_ci
233bf215546Sopenharmony_ci   unsigned saved_writable_mask = 0;
234bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_buffers; i++) {
235bf215546Sopenharmony_ci      if (sctx->const_and_shader_buffers[PIPE_SHADER_COMPUTE].writable_mask &
236bf215546Sopenharmony_ci          (1u << si_get_shaderbuf_slot(i)))
237bf215546Sopenharmony_ci         saved_writable_mask |= 1 << i;
238bf215546Sopenharmony_ci   }
239bf215546Sopenharmony_ci
240bf215546Sopenharmony_ci   /* Bind buffers and launch compute. */
241bf215546Sopenharmony_ci   si_set_shader_buffers(&sctx->b, PIPE_SHADER_COMPUTE, 0, num_buffers, buffers,
242bf215546Sopenharmony_ci                         writeable_bitmask,
243bf215546Sopenharmony_ci                         true /* don't update bind_history to prevent unnecessary syncs later */);
244bf215546Sopenharmony_ci   si_launch_grid_internal(sctx, info, shader, flags);
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_ci   /* Do cache flushing at the end. */
247bf215546Sopenharmony_ci   if (get_cache_policy(sctx, coher, 0) == L2_BYPASS) {
248bf215546Sopenharmony_ci      if (flags & SI_OP_SYNC_AFTER)
249bf215546Sopenharmony_ci         sctx->flags |= SI_CONTEXT_WB_L2;
250bf215546Sopenharmony_ci   } else {
251bf215546Sopenharmony_ci      while (writeable_bitmask)
252bf215546Sopenharmony_ci         si_resource(buffers[u_bit_scan(&writeable_bitmask)].buffer)->TC_L2_dirty = true;
253bf215546Sopenharmony_ci   }
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_ci   /* Restore states. */
256bf215546Sopenharmony_ci   sctx->b.set_shader_buffers(&sctx->b, PIPE_SHADER_COMPUTE, 0, num_buffers, saved_sb,
257bf215546Sopenharmony_ci                              saved_writable_mask);
258bf215546Sopenharmony_ci   for (int i = 0; i < num_buffers; i++)
259bf215546Sopenharmony_ci      pipe_resource_reference(&saved_sb[i].buffer, NULL);
260bf215546Sopenharmony_ci}
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci/**
263bf215546Sopenharmony_ci * Clear a buffer using read-modify-write with a 32-bit write bitmask.
264bf215546Sopenharmony_ci * The clear value has 32 bits.
265bf215546Sopenharmony_ci */
266bf215546Sopenharmony_civoid si_compute_clear_buffer_rmw(struct si_context *sctx, struct pipe_resource *dst,
267bf215546Sopenharmony_ci                                 unsigned dst_offset, unsigned size,
268bf215546Sopenharmony_ci                                 uint32_t clear_value, uint32_t writebitmask,
269bf215546Sopenharmony_ci                                 unsigned flags, enum si_coherency coher)
270bf215546Sopenharmony_ci{
271bf215546Sopenharmony_ci   assert(dst_offset % 4 == 0);
272bf215546Sopenharmony_ci   assert(size % 4 == 0);
273bf215546Sopenharmony_ci
274bf215546Sopenharmony_ci   assert(dst->target != PIPE_BUFFER || dst_offset + size <= dst->width0);
275bf215546Sopenharmony_ci
276bf215546Sopenharmony_ci   /* Use buffer_load_dwordx4 and buffer_store_dwordx4 per thread. */
277bf215546Sopenharmony_ci   unsigned dwords_per_instruction = 4;
278bf215546Sopenharmony_ci   unsigned block_size = 64; /* it's always 64x1x1 */
279bf215546Sopenharmony_ci   unsigned dwords_per_wave = dwords_per_instruction * block_size;
280bf215546Sopenharmony_ci
281bf215546Sopenharmony_ci   unsigned num_dwords = size / 4;
282bf215546Sopenharmony_ci   unsigned num_instructions = DIV_ROUND_UP(num_dwords, dwords_per_instruction);
283bf215546Sopenharmony_ci
284bf215546Sopenharmony_ci   struct pipe_grid_info info = {};
285bf215546Sopenharmony_ci   info.block[0] = MIN2(block_size, num_instructions);
286bf215546Sopenharmony_ci   info.block[1] = 1;
287bf215546Sopenharmony_ci   info.block[2] = 1;
288bf215546Sopenharmony_ci   info.grid[0] = DIV_ROUND_UP(num_dwords, dwords_per_wave);
289bf215546Sopenharmony_ci   info.grid[1] = 1;
290bf215546Sopenharmony_ci   info.grid[2] = 1;
291bf215546Sopenharmony_ci
292bf215546Sopenharmony_ci   struct pipe_shader_buffer sb = {};
293bf215546Sopenharmony_ci   sb.buffer = dst;
294bf215546Sopenharmony_ci   sb.buffer_offset = dst_offset;
295bf215546Sopenharmony_ci   sb.buffer_size = size;
296bf215546Sopenharmony_ci
297bf215546Sopenharmony_ci   sctx->cs_user_data[0] = clear_value & writebitmask;
298bf215546Sopenharmony_ci   sctx->cs_user_data[1] = ~writebitmask;
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci   if (!sctx->cs_clear_buffer_rmw)
301bf215546Sopenharmony_ci      sctx->cs_clear_buffer_rmw = si_create_clear_buffer_rmw_cs(sctx);
302bf215546Sopenharmony_ci
303bf215546Sopenharmony_ci   si_launch_grid_internal_ssbos(sctx, &info, sctx->cs_clear_buffer_rmw, flags, coher,
304bf215546Sopenharmony_ci                                 1, &sb, 0x1);
305bf215546Sopenharmony_ci}
306bf215546Sopenharmony_ci
307bf215546Sopenharmony_cistatic void si_compute_clear_12bytes_buffer(struct si_context *sctx, struct pipe_resource *dst,
308bf215546Sopenharmony_ci                                            unsigned dst_offset, unsigned size,
309bf215546Sopenharmony_ci                                            const uint32_t *clear_value, unsigned flags,
310bf215546Sopenharmony_ci                                            enum si_coherency coher)
311bf215546Sopenharmony_ci{
312bf215546Sopenharmony_ci   struct pipe_context *ctx = &sctx->b;
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_ci   assert(dst_offset % 4 == 0);
315bf215546Sopenharmony_ci   assert(size % 4 == 0);
316bf215546Sopenharmony_ci   unsigned size_12 = DIV_ROUND_UP(size, 12);
317bf215546Sopenharmony_ci
318bf215546Sopenharmony_ci   struct pipe_shader_buffer sb = {0};
319bf215546Sopenharmony_ci   sb.buffer = dst;
320bf215546Sopenharmony_ci   sb.buffer_offset = dst_offset;
321bf215546Sopenharmony_ci   sb.buffer_size = size;
322bf215546Sopenharmony_ci
323bf215546Sopenharmony_ci   memcpy(sctx->cs_user_data, clear_value, 12);
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_ci   struct pipe_grid_info info = {0};
326bf215546Sopenharmony_ci
327bf215546Sopenharmony_ci   if (!sctx->cs_clear_12bytes_buffer)
328bf215546Sopenharmony_ci      sctx->cs_clear_12bytes_buffer = si_clear_12bytes_buffer_shader(ctx);
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ci   info.block[0] = 64;
331bf215546Sopenharmony_ci   info.last_block[0] = size_12 % 64;
332bf215546Sopenharmony_ci   info.block[1] = 1;
333bf215546Sopenharmony_ci   info.block[2] = 1;
334bf215546Sopenharmony_ci   info.grid[0] = DIV_ROUND_UP(size_12, 64);
335bf215546Sopenharmony_ci   info.grid[1] = 1;
336bf215546Sopenharmony_ci   info.grid[2] = 1;
337bf215546Sopenharmony_ci
338bf215546Sopenharmony_ci   si_launch_grid_internal_ssbos(sctx, &info, sctx->cs_clear_12bytes_buffer, flags, coher,
339bf215546Sopenharmony_ci                                 1, &sb, 0x1);
340bf215546Sopenharmony_ci}
341bf215546Sopenharmony_ci
342bf215546Sopenharmony_cistatic void si_compute_do_clear_or_copy(struct si_context *sctx, struct pipe_resource *dst,
343bf215546Sopenharmony_ci                                        unsigned dst_offset, struct pipe_resource *src,
344bf215546Sopenharmony_ci                                        unsigned src_offset, unsigned size,
345bf215546Sopenharmony_ci                                        const uint32_t *clear_value, unsigned clear_value_size,
346bf215546Sopenharmony_ci                                        unsigned flags, enum si_coherency coher)
347bf215546Sopenharmony_ci{
348bf215546Sopenharmony_ci   assert(src_offset % 4 == 0);
349bf215546Sopenharmony_ci   assert(dst_offset % 4 == 0);
350bf215546Sopenharmony_ci   assert(size % 4 == 0);
351bf215546Sopenharmony_ci
352bf215546Sopenharmony_ci   assert(dst->target != PIPE_BUFFER || dst_offset + size <= dst->width0);
353bf215546Sopenharmony_ci   assert(!src || src_offset + size <= src->width0);
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_ci   /* The memory accesses are coalesced, meaning that the 1st instruction writes
356bf215546Sopenharmony_ci    * the 1st contiguous block of data for the whole wave, the 2nd instruction
357bf215546Sopenharmony_ci    * writes the 2nd contiguous block of data, etc.
358bf215546Sopenharmony_ci    */
359bf215546Sopenharmony_ci   unsigned dwords_per_thread =
360bf215546Sopenharmony_ci      src ? SI_COMPUTE_COPY_DW_PER_THREAD : SI_COMPUTE_CLEAR_DW_PER_THREAD;
361bf215546Sopenharmony_ci   unsigned instructions_per_thread = MAX2(1, dwords_per_thread / 4);
362bf215546Sopenharmony_ci   unsigned dwords_per_instruction = dwords_per_thread / instructions_per_thread;
363bf215546Sopenharmony_ci   /* The shader declares the block size like this: */
364bf215546Sopenharmony_ci   unsigned block_size = si_determine_wave_size(sctx->screen, NULL);
365bf215546Sopenharmony_ci   unsigned dwords_per_wave = dwords_per_thread * block_size;
366bf215546Sopenharmony_ci
367bf215546Sopenharmony_ci   unsigned num_dwords = size / 4;
368bf215546Sopenharmony_ci   unsigned num_instructions = DIV_ROUND_UP(num_dwords, dwords_per_instruction);
369bf215546Sopenharmony_ci
370bf215546Sopenharmony_ci   struct pipe_grid_info info = {};
371bf215546Sopenharmony_ci   info.block[0] = MIN2(block_size, num_instructions);
372bf215546Sopenharmony_ci   info.block[1] = 1;
373bf215546Sopenharmony_ci   info.block[2] = 1;
374bf215546Sopenharmony_ci   info.grid[0] = DIV_ROUND_UP(num_dwords, dwords_per_wave);
375bf215546Sopenharmony_ci   info.grid[1] = 1;
376bf215546Sopenharmony_ci   info.grid[2] = 1;
377bf215546Sopenharmony_ci
378bf215546Sopenharmony_ci   struct pipe_shader_buffer sb[2] = {};
379bf215546Sopenharmony_ci   sb[0].buffer = dst;
380bf215546Sopenharmony_ci   sb[0].buffer_offset = dst_offset;
381bf215546Sopenharmony_ci   sb[0].buffer_size = size;
382bf215546Sopenharmony_ci
383bf215546Sopenharmony_ci   bool shader_dst_stream_policy = SI_COMPUTE_DST_CACHE_POLICY != L2_LRU;
384bf215546Sopenharmony_ci
385bf215546Sopenharmony_ci   if (src) {
386bf215546Sopenharmony_ci      sb[1].buffer = src;
387bf215546Sopenharmony_ci      sb[1].buffer_offset = src_offset;
388bf215546Sopenharmony_ci      sb[1].buffer_size = size;
389bf215546Sopenharmony_ci
390bf215546Sopenharmony_ci      if (!sctx->cs_copy_buffer) {
391bf215546Sopenharmony_ci         sctx->cs_copy_buffer = si_create_dma_compute_shader(
392bf215546Sopenharmony_ci            &sctx->b, SI_COMPUTE_COPY_DW_PER_THREAD, shader_dst_stream_policy, true);
393bf215546Sopenharmony_ci      }
394bf215546Sopenharmony_ci
395bf215546Sopenharmony_ci      si_launch_grid_internal_ssbos(sctx, &info, sctx->cs_copy_buffer, flags, coher,
396bf215546Sopenharmony_ci                                    2, sb, 0x1);
397bf215546Sopenharmony_ci   } else {
398bf215546Sopenharmony_ci      assert(clear_value_size >= 4 && clear_value_size <= 16 &&
399bf215546Sopenharmony_ci             util_is_power_of_two_or_zero(clear_value_size));
400bf215546Sopenharmony_ci
401bf215546Sopenharmony_ci      for (unsigned i = 0; i < 4; i++)
402bf215546Sopenharmony_ci         sctx->cs_user_data[i] = clear_value[i % (clear_value_size / 4)];
403bf215546Sopenharmony_ci
404bf215546Sopenharmony_ci      if (!sctx->cs_clear_buffer) {
405bf215546Sopenharmony_ci         sctx->cs_clear_buffer = si_create_dma_compute_shader(
406bf215546Sopenharmony_ci            &sctx->b, SI_COMPUTE_CLEAR_DW_PER_THREAD, shader_dst_stream_policy, false);
407bf215546Sopenharmony_ci      }
408bf215546Sopenharmony_ci
409bf215546Sopenharmony_ci      si_launch_grid_internal_ssbos(sctx, &info, sctx->cs_clear_buffer, flags, coher,
410bf215546Sopenharmony_ci                                    1, sb, 0x1);
411bf215546Sopenharmony_ci   }
412bf215546Sopenharmony_ci}
413bf215546Sopenharmony_ci
414bf215546Sopenharmony_civoid si_clear_buffer(struct si_context *sctx, struct pipe_resource *dst,
415bf215546Sopenharmony_ci                     uint64_t offset, uint64_t size, uint32_t *clear_value,
416bf215546Sopenharmony_ci                     uint32_t clear_value_size, unsigned flags,
417bf215546Sopenharmony_ci                     enum si_coherency coher, enum si_clear_method method)
418bf215546Sopenharmony_ci{
419bf215546Sopenharmony_ci   if (!size)
420bf215546Sopenharmony_ci      return;
421bf215546Sopenharmony_ci
422bf215546Sopenharmony_ci   si_improve_sync_flags(sctx, dst, NULL, &flags);
423bf215546Sopenharmony_ci
424bf215546Sopenharmony_ci   ASSERTED unsigned clear_alignment = MIN2(clear_value_size, 4);
425bf215546Sopenharmony_ci
426bf215546Sopenharmony_ci   assert(clear_value_size != 3 && clear_value_size != 6); /* 12 is allowed. */
427bf215546Sopenharmony_ci   assert(offset % clear_alignment == 0);
428bf215546Sopenharmony_ci   assert(size % clear_alignment == 0);
429bf215546Sopenharmony_ci   assert(size < (UINT_MAX & ~0xf)); /* TODO: test 64-bit sizes in all codepaths */
430bf215546Sopenharmony_ci
431bf215546Sopenharmony_ci   uint32_t clamped;
432bf215546Sopenharmony_ci   if (util_lower_clearsize_to_dword(clear_value, (int*)&clear_value_size, &clamped))
433bf215546Sopenharmony_ci      clear_value = &clamped;
434bf215546Sopenharmony_ci
435bf215546Sopenharmony_ci   if (clear_value_size == 12) {
436bf215546Sopenharmony_ci      si_compute_clear_12bytes_buffer(sctx, dst, offset, size, clear_value, flags, coher);
437bf215546Sopenharmony_ci      return;
438bf215546Sopenharmony_ci   }
439bf215546Sopenharmony_ci
440bf215546Sopenharmony_ci   uint64_t aligned_size = size & ~3ull;
441bf215546Sopenharmony_ci   if (aligned_size >= 4) {
442bf215546Sopenharmony_ci      uint64_t compute_min_size;
443bf215546Sopenharmony_ci
444bf215546Sopenharmony_ci      if (sctx->gfx_level <= GFX8) {
445bf215546Sopenharmony_ci         /* CP DMA clears are terribly slow with GTT on GFX6-8, which can always
446bf215546Sopenharmony_ci          * happen due to BO evictions.
447bf215546Sopenharmony_ci          */
448bf215546Sopenharmony_ci         compute_min_size = 0;
449bf215546Sopenharmony_ci      } else {
450bf215546Sopenharmony_ci         /* Use a small enough size because CP DMA is slower than compute with bigger sizes. */
451bf215546Sopenharmony_ci         compute_min_size = 4 * 1024;
452bf215546Sopenharmony_ci      }
453bf215546Sopenharmony_ci
454bf215546Sopenharmony_ci      /* TODO: use compute for unaligned big sizes */
455bf215546Sopenharmony_ci      if (method == SI_AUTO_SELECT_CLEAR_METHOD && (
456bf215546Sopenharmony_ci           clear_value_size > 4 ||
457bf215546Sopenharmony_ci           (clear_value_size == 4 && offset % 4 == 0 && size > compute_min_size))) {
458bf215546Sopenharmony_ci         method = SI_COMPUTE_CLEAR_METHOD;
459bf215546Sopenharmony_ci      }
460bf215546Sopenharmony_ci      if (method == SI_COMPUTE_CLEAR_METHOD) {
461bf215546Sopenharmony_ci         si_compute_do_clear_or_copy(sctx, dst, offset, NULL, 0, aligned_size, clear_value,
462bf215546Sopenharmony_ci                                     clear_value_size, flags, coher);
463bf215546Sopenharmony_ci      } else {
464bf215546Sopenharmony_ci         assert(clear_value_size == 4);
465bf215546Sopenharmony_ci         si_cp_dma_clear_buffer(sctx, &sctx->gfx_cs, dst, offset, aligned_size, *clear_value,
466bf215546Sopenharmony_ci                                flags, coher, get_cache_policy(sctx, coher, size));
467bf215546Sopenharmony_ci      }
468bf215546Sopenharmony_ci
469bf215546Sopenharmony_ci      offset += aligned_size;
470bf215546Sopenharmony_ci      size -= aligned_size;
471bf215546Sopenharmony_ci   }
472bf215546Sopenharmony_ci
473bf215546Sopenharmony_ci   /* Handle non-dword alignment. */
474bf215546Sopenharmony_ci   if (size) {
475bf215546Sopenharmony_ci      assert(dst);
476bf215546Sopenharmony_ci      assert(dst->target == PIPE_BUFFER);
477bf215546Sopenharmony_ci      assert(size < 4);
478bf215546Sopenharmony_ci
479bf215546Sopenharmony_ci      sctx->b.buffer_subdata(&sctx->b, dst,
480bf215546Sopenharmony_ci                             PIPE_MAP_WRITE |
481bf215546Sopenharmony_ci                             /* TC forbids drivers to invalidate buffers and infer unsychronized mappings,
482bf215546Sopenharmony_ci                              * so suppress those optimizations. */
483bf215546Sopenharmony_ci                             (sctx->tc ? TC_TRANSFER_MAP_NO_INFER_UNSYNCHRONIZED |
484bf215546Sopenharmony_ci                                         TC_TRANSFER_MAP_NO_INVALIDATE : 0),
485bf215546Sopenharmony_ci                             offset, size, clear_value);
486bf215546Sopenharmony_ci   }
487bf215546Sopenharmony_ci}
488bf215546Sopenharmony_ci
489bf215546Sopenharmony_civoid si_screen_clear_buffer(struct si_screen *sscreen, struct pipe_resource *dst, uint64_t offset,
490bf215546Sopenharmony_ci                            uint64_t size, unsigned value, unsigned flags)
491bf215546Sopenharmony_ci{
492bf215546Sopenharmony_ci   struct si_context *ctx = si_get_aux_context(sscreen);
493bf215546Sopenharmony_ci   si_clear_buffer(ctx, dst, offset, size, &value, 4, flags,
494bf215546Sopenharmony_ci                   SI_COHERENCY_SHADER, SI_AUTO_SELECT_CLEAR_METHOD);
495bf215546Sopenharmony_ci   si_put_aux_context_flush(sscreen);
496bf215546Sopenharmony_ci}
497bf215546Sopenharmony_ci
498bf215546Sopenharmony_cistatic void si_pipe_clear_buffer(struct pipe_context *ctx, struct pipe_resource *dst,
499bf215546Sopenharmony_ci                                 unsigned offset, unsigned size, const void *clear_value,
500bf215546Sopenharmony_ci                                 int clear_value_size)
501bf215546Sopenharmony_ci{
502bf215546Sopenharmony_ci   si_clear_buffer((struct si_context *)ctx, dst, offset, size, (uint32_t *)clear_value,
503bf215546Sopenharmony_ci                   clear_value_size, SI_OP_SYNC_BEFORE_AFTER, SI_COHERENCY_SHADER,
504bf215546Sopenharmony_ci                   SI_AUTO_SELECT_CLEAR_METHOD);
505bf215546Sopenharmony_ci}
506bf215546Sopenharmony_ci
507bf215546Sopenharmony_civoid si_copy_buffer(struct si_context *sctx, struct pipe_resource *dst, struct pipe_resource *src,
508bf215546Sopenharmony_ci                    uint64_t dst_offset, uint64_t src_offset, unsigned size, unsigned flags)
509bf215546Sopenharmony_ci{
510bf215546Sopenharmony_ci   if (!size)
511bf215546Sopenharmony_ci      return;
512bf215546Sopenharmony_ci
513bf215546Sopenharmony_ci   enum si_coherency coher = SI_COHERENCY_SHADER;
514bf215546Sopenharmony_ci   enum si_cache_policy cache_policy = get_cache_policy(sctx, coher, size);
515bf215546Sopenharmony_ci   uint64_t compute_min_size = 8 * 1024;
516bf215546Sopenharmony_ci
517bf215546Sopenharmony_ci   si_improve_sync_flags(sctx, dst, src, &flags);
518bf215546Sopenharmony_ci
519bf215546Sopenharmony_ci   /* Only use compute for VRAM copies on dGPUs. */
520bf215546Sopenharmony_ci   /* TODO: use compute for unaligned big sizes */
521bf215546Sopenharmony_ci   if (sctx->screen->info.has_dedicated_vram && si_resource(dst)->domains & RADEON_DOMAIN_VRAM &&
522bf215546Sopenharmony_ci       si_resource(src)->domains & RADEON_DOMAIN_VRAM && size > compute_min_size &&
523bf215546Sopenharmony_ci       dst_offset % 4 == 0 && src_offset % 4 == 0 && size % 4 == 0) {
524bf215546Sopenharmony_ci      si_compute_do_clear_or_copy(sctx, dst, dst_offset, src, src_offset, size, NULL, 0,
525bf215546Sopenharmony_ci                                  flags, coher);
526bf215546Sopenharmony_ci   } else {
527bf215546Sopenharmony_ci      si_cp_dma_copy_buffer(sctx, dst, src, dst_offset, src_offset, size,
528bf215546Sopenharmony_ci                            flags, coher, cache_policy);
529bf215546Sopenharmony_ci   }
530bf215546Sopenharmony_ci}
531bf215546Sopenharmony_ci
532bf215546Sopenharmony_cistatic void
533bf215546Sopenharmony_ciset_work_size(struct pipe_grid_info *info, unsigned block_x, unsigned block_y, unsigned block_z,
534bf215546Sopenharmony_ci              unsigned work_x, unsigned work_y, unsigned work_z)
535bf215546Sopenharmony_ci{
536bf215546Sopenharmony_ci   info->block[0] = block_x;
537bf215546Sopenharmony_ci   info->block[1] = block_y;
538bf215546Sopenharmony_ci   info->block[2] = block_z;
539bf215546Sopenharmony_ci
540bf215546Sopenharmony_ci   unsigned work[3] = {work_x, work_y, work_z};
541bf215546Sopenharmony_ci   for (int i = 0; i < 3; ++i) {
542bf215546Sopenharmony_ci      info->last_block[i] = work[i] % info->block[i];
543bf215546Sopenharmony_ci      info->grid[i] = DIV_ROUND_UP(work[i], info->block[i]);
544bf215546Sopenharmony_ci   }
545bf215546Sopenharmony_ci}
546bf215546Sopenharmony_ci
547bf215546Sopenharmony_cistatic void si_launch_grid_internal_images(struct si_context *sctx,
548bf215546Sopenharmony_ci                                           struct pipe_image_view *images,
549bf215546Sopenharmony_ci                                           unsigned num_images,
550bf215546Sopenharmony_ci                                           const struct pipe_grid_info *info,
551bf215546Sopenharmony_ci                                           void *shader, unsigned flags)
552bf215546Sopenharmony_ci{
553bf215546Sopenharmony_ci   struct pipe_image_view saved_image[2] = {};
554bf215546Sopenharmony_ci   assert(num_images <= ARRAY_SIZE(saved_image));
555bf215546Sopenharmony_ci
556bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_images; i++) {
557bf215546Sopenharmony_ci      assert(sctx->b.screen->is_format_supported(sctx->b.screen, images[i].format,
558bf215546Sopenharmony_ci                                                 images[i].resource->target,
559bf215546Sopenharmony_ci                                                 images[i].resource->nr_samples,
560bf215546Sopenharmony_ci                                                 images[i].resource->nr_storage_samples,
561bf215546Sopenharmony_ci                                                 PIPE_BIND_SHADER_IMAGE));
562bf215546Sopenharmony_ci
563bf215546Sopenharmony_ci      /* Always allow DCC stores on gfx10+. */
564bf215546Sopenharmony_ci      if (sctx->gfx_level >= GFX10 &&
565bf215546Sopenharmony_ci          images[i].access & PIPE_IMAGE_ACCESS_WRITE &&
566bf215546Sopenharmony_ci          !(images[i].access & SI_IMAGE_ACCESS_DCC_OFF))
567bf215546Sopenharmony_ci         images[i].access |= SI_IMAGE_ACCESS_ALLOW_DCC_STORE;
568bf215546Sopenharmony_ci
569bf215546Sopenharmony_ci      /* Simplify the format according to what image stores support. */
570bf215546Sopenharmony_ci      if (images[i].access & PIPE_IMAGE_ACCESS_WRITE) {
571bf215546Sopenharmony_ci         images[i].format = util_format_linear(images[i].format); /* SRGB not supported */
572bf215546Sopenharmony_ci         images[i].format = util_format_luminance_to_red(images[i].format);
573bf215546Sopenharmony_ci         images[i].format = util_format_intensity_to_red(images[i].format);
574bf215546Sopenharmony_ci         images[i].format = util_format_rgbx_to_rgba(images[i].format); /* prevent partial writes */
575bf215546Sopenharmony_ci      }
576bf215546Sopenharmony_ci
577bf215546Sopenharmony_ci      /* Save the image. */
578bf215546Sopenharmony_ci      util_copy_image_view(&saved_image[i], &sctx->images[PIPE_SHADER_COMPUTE].views[i]);
579bf215546Sopenharmony_ci   }
580bf215546Sopenharmony_ci
581bf215546Sopenharmony_ci   /* This might invoke DCC decompression, so do it first. */
582bf215546Sopenharmony_ci   sctx->b.set_shader_images(&sctx->b, PIPE_SHADER_COMPUTE, 0, num_images, 0, images);
583bf215546Sopenharmony_ci
584bf215546Sopenharmony_ci   /* This should be done after set_shader_images. */
585bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_images; i++) {
586bf215546Sopenharmony_ci      /* The driver doesn't decompress resources automatically here, so do it manually. */
587bf215546Sopenharmony_ci      si_decompress_subresource(&sctx->b, images[i].resource, PIPE_MASK_RGBAZS,
588bf215546Sopenharmony_ci                                images[i].u.tex.level, images[i].u.tex.first_layer,
589bf215546Sopenharmony_ci                                images[i].u.tex.last_layer,
590bf215546Sopenharmony_ci                                images[i].access & PIPE_IMAGE_ACCESS_WRITE);
591bf215546Sopenharmony_ci   }
592bf215546Sopenharmony_ci
593bf215546Sopenharmony_ci   /* This must be done before the compute shader. */
594bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_images; i++) {
595bf215546Sopenharmony_ci      si_make_CB_shader_coherent(sctx, images[i].resource->nr_samples, true,
596bf215546Sopenharmony_ci            ((struct si_texture*)images[i].resource)->surface.u.gfx9.color.dcc.pipe_aligned);
597bf215546Sopenharmony_ci   }
598bf215546Sopenharmony_ci
599bf215546Sopenharmony_ci   si_launch_grid_internal(sctx, info, shader, flags | SI_OP_CS_IMAGE);
600bf215546Sopenharmony_ci
601bf215546Sopenharmony_ci   /* Restore images. */
602bf215546Sopenharmony_ci   sctx->b.set_shader_images(&sctx->b, PIPE_SHADER_COMPUTE, 0, num_images, 0, saved_image);
603bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_images; i++)
604bf215546Sopenharmony_ci      pipe_resource_reference(&saved_image[i].resource, NULL);
605bf215546Sopenharmony_ci}
606bf215546Sopenharmony_ci
607bf215546Sopenharmony_cibool si_compute_copy_image(struct si_context *sctx, struct pipe_resource *dst, unsigned dst_level,
608bf215546Sopenharmony_ci                           struct pipe_resource *src, unsigned src_level, unsigned dstx,
609bf215546Sopenharmony_ci                           unsigned dsty, unsigned dstz, const struct pipe_box *src_box,
610bf215546Sopenharmony_ci                           unsigned flags)
611bf215546Sopenharmony_ci{
612bf215546Sopenharmony_ci   struct si_texture *ssrc = (struct si_texture*)src;
613bf215546Sopenharmony_ci   struct si_texture *sdst = (struct si_texture*)dst;
614bf215546Sopenharmony_ci
615bf215546Sopenharmony_ci   si_use_compute_copy_for_float_formats(sctx, dst, dst_level);
616bf215546Sopenharmony_ci
617bf215546Sopenharmony_ci   /* The compute copy is mandatory for compressed and subsampled formats because the gfx copy
618bf215546Sopenharmony_ci    * doesn't support them. In all other cases, call si_can_use_compute_blit.
619bf215546Sopenharmony_ci    *
620bf215546Sopenharmony_ci    * The format is identical (we only need to check the src format) except compressed formats,
621bf215546Sopenharmony_ci    * which can be paired with an equivalent integer format.
622bf215546Sopenharmony_ci    */
623bf215546Sopenharmony_ci   if (!util_format_is_compressed(src->format) &&
624bf215546Sopenharmony_ci       !util_format_is_compressed(dst->format) &&
625bf215546Sopenharmony_ci       !util_format_is_subsampled_422(src->format) &&
626bf215546Sopenharmony_ci       (!si_can_use_compute_blit(sctx, dst->format, dst->nr_samples, true,
627bf215546Sopenharmony_ci                                 vi_dcc_enabled(sdst, dst_level)) ||
628bf215546Sopenharmony_ci        !si_can_use_compute_blit(sctx, src->format, src->nr_samples, false,
629bf215546Sopenharmony_ci                                 vi_dcc_enabled(ssrc, src_level))))
630bf215546Sopenharmony_ci      return false;
631bf215546Sopenharmony_ci
632bf215546Sopenharmony_ci   enum pipe_format src_format = util_format_linear(src->format);
633bf215546Sopenharmony_ci   enum pipe_format dst_format = util_format_linear(dst->format);
634bf215546Sopenharmony_ci   bool is_linear = ssrc->surface.is_linear || sdst->surface.is_linear;
635bf215546Sopenharmony_ci
636bf215546Sopenharmony_ci   assert(util_format_is_subsampled_422(src_format) == util_format_is_subsampled_422(dst_format));
637bf215546Sopenharmony_ci
638bf215546Sopenharmony_ci   /* Interpret as integer values to avoid NaN issues */
639bf215546Sopenharmony_ci   if (!vi_dcc_enabled(ssrc, src_level) &&
640bf215546Sopenharmony_ci       !vi_dcc_enabled(sdst, dst_level) &&
641bf215546Sopenharmony_ci       src_format == dst_format &&
642bf215546Sopenharmony_ci       util_format_is_float(src_format) &&
643bf215546Sopenharmony_ci       !util_format_is_compressed(src_format)) {
644bf215546Sopenharmony_ci      switch(util_format_get_blocksizebits(src_format)) {
645bf215546Sopenharmony_ci        case 16:
646bf215546Sopenharmony_ci          src_format = dst_format = PIPE_FORMAT_R16_UINT;
647bf215546Sopenharmony_ci          break;
648bf215546Sopenharmony_ci        case 32:
649bf215546Sopenharmony_ci          src_format = dst_format = PIPE_FORMAT_R32_UINT;
650bf215546Sopenharmony_ci          break;
651bf215546Sopenharmony_ci        case 64:
652bf215546Sopenharmony_ci          src_format = dst_format = PIPE_FORMAT_R32G32_UINT;
653bf215546Sopenharmony_ci          break;
654bf215546Sopenharmony_ci        case 128:
655bf215546Sopenharmony_ci          src_format = dst_format = PIPE_FORMAT_R32G32B32A32_UINT;
656bf215546Sopenharmony_ci          break;
657bf215546Sopenharmony_ci        default:
658bf215546Sopenharmony_ci          assert(false);
659bf215546Sopenharmony_ci      }
660bf215546Sopenharmony_ci   }
661bf215546Sopenharmony_ci
662bf215546Sopenharmony_ci   /* Interpret compressed formats as UINT. */
663bf215546Sopenharmony_ci   struct pipe_box new_box;
664bf215546Sopenharmony_ci   unsigned src_access = 0, dst_access = 0;
665bf215546Sopenharmony_ci
666bf215546Sopenharmony_ci   /* Note that staging copies do compressed<->UINT, so one of the formats is already UINT. */
667bf215546Sopenharmony_ci   if (util_format_is_compressed(src_format) || util_format_is_compressed(dst_format)) {
668bf215546Sopenharmony_ci      if (util_format_is_compressed(src_format))
669bf215546Sopenharmony_ci         src_access |= SI_IMAGE_ACCESS_BLOCK_FORMAT_AS_UINT;
670bf215546Sopenharmony_ci      if (util_format_is_compressed(dst_format))
671bf215546Sopenharmony_ci         dst_access |= SI_IMAGE_ACCESS_BLOCK_FORMAT_AS_UINT;
672bf215546Sopenharmony_ci
673bf215546Sopenharmony_ci      dstx = util_format_get_nblocksx(dst_format, dstx);
674bf215546Sopenharmony_ci      dsty = util_format_get_nblocksy(dst_format, dsty);
675bf215546Sopenharmony_ci
676bf215546Sopenharmony_ci      new_box.x = util_format_get_nblocksx(src_format, src_box->x);
677bf215546Sopenharmony_ci      new_box.y = util_format_get_nblocksy(src_format, src_box->y);
678bf215546Sopenharmony_ci      new_box.z = src_box->z;
679bf215546Sopenharmony_ci      new_box.width = util_format_get_nblocksx(src_format, src_box->width);
680bf215546Sopenharmony_ci      new_box.height = util_format_get_nblocksy(src_format, src_box->height);
681bf215546Sopenharmony_ci      new_box.depth = src_box->depth;
682bf215546Sopenharmony_ci      src_box = &new_box;
683bf215546Sopenharmony_ci
684bf215546Sopenharmony_ci      if (ssrc->surface.bpe == 8)
685bf215546Sopenharmony_ci         src_format = dst_format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
686bf215546Sopenharmony_ci      else
687bf215546Sopenharmony_ci         src_format = dst_format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
688bf215546Sopenharmony_ci   }
689bf215546Sopenharmony_ci
690bf215546Sopenharmony_ci   if (util_format_is_subsampled_422(src_format)) {
691bf215546Sopenharmony_ci      assert(src_format == dst_format);
692bf215546Sopenharmony_ci
693bf215546Sopenharmony_ci      src_access |= SI_IMAGE_ACCESS_BLOCK_FORMAT_AS_UINT;
694bf215546Sopenharmony_ci      dst_access |= SI_IMAGE_ACCESS_BLOCK_FORMAT_AS_UINT;
695bf215546Sopenharmony_ci
696bf215546Sopenharmony_ci      dstx = util_format_get_nblocksx(src_format, dstx);
697bf215546Sopenharmony_ci
698bf215546Sopenharmony_ci      new_box = *src_box;
699bf215546Sopenharmony_ci      new_box.x = util_format_get_nblocksx(src_format, src_box->x);
700bf215546Sopenharmony_ci      new_box.width = util_format_get_nblocksx(src_format, src_box->width);
701bf215546Sopenharmony_ci      src_box = &new_box;
702bf215546Sopenharmony_ci
703bf215546Sopenharmony_ci      src_format = dst_format = PIPE_FORMAT_R32_UINT;
704bf215546Sopenharmony_ci
705bf215546Sopenharmony_ci      /* Interpreting 422 subsampled format (16 bpp) as 32 bpp
706bf215546Sopenharmony_ci       * should force us to divide src_box->x, dstx and width by 2.
707bf215546Sopenharmony_ci       * But given that ac_surface allocates this format as 32 bpp
708bf215546Sopenharmony_ci       * and that surf_size is then modified to pack the values
709bf215546Sopenharmony_ci       * we must keep the original values to get the correct results.
710bf215546Sopenharmony_ci       */
711bf215546Sopenharmony_ci   }
712bf215546Sopenharmony_ci
713bf215546Sopenharmony_ci   /* SNORM blitting has precision issues. Use the SINT equivalent instead, which doesn't
714bf215546Sopenharmony_ci    * force DCC decompression.
715bf215546Sopenharmony_ci    */
716bf215546Sopenharmony_ci   if (util_format_is_snorm(dst_format))
717bf215546Sopenharmony_ci      src_format = dst_format = util_format_snorm_to_sint(dst_format);
718bf215546Sopenharmony_ci
719bf215546Sopenharmony_ci   if (src_box->width == 0 || src_box->height == 0 || src_box->depth == 0)
720bf215546Sopenharmony_ci      return true; /* success - nothing to do */
721bf215546Sopenharmony_ci
722bf215546Sopenharmony_ci   struct pipe_image_view image[2] = {0};
723bf215546Sopenharmony_ci   image[0].resource = src;
724bf215546Sopenharmony_ci   image[0].shader_access = image[0].access = PIPE_IMAGE_ACCESS_READ | src_access;
725bf215546Sopenharmony_ci   image[0].format = src_format;
726bf215546Sopenharmony_ci   image[0].u.tex.level = src_level;
727bf215546Sopenharmony_ci   image[0].u.tex.first_layer = 0;
728bf215546Sopenharmony_ci   image[0].u.tex.last_layer = util_max_layer(src, src_level);
729bf215546Sopenharmony_ci   image[1].resource = dst;
730bf215546Sopenharmony_ci   image[1].shader_access = image[1].access = PIPE_IMAGE_ACCESS_WRITE | dst_access;
731bf215546Sopenharmony_ci   image[1].format = dst_format;
732bf215546Sopenharmony_ci   image[1].u.tex.level = dst_level;
733bf215546Sopenharmony_ci   image[1].u.tex.first_layer = 0;
734bf215546Sopenharmony_ci   image[1].u.tex.last_layer = util_max_layer(dst, dst_level);
735bf215546Sopenharmony_ci
736bf215546Sopenharmony_ci   struct pipe_grid_info info = {0};
737bf215546Sopenharmony_ci
738bf215546Sopenharmony_ci   bool dst_is_1d = dst->target == PIPE_TEXTURE_1D ||
739bf215546Sopenharmony_ci                    dst->target == PIPE_TEXTURE_1D_ARRAY;
740bf215546Sopenharmony_ci   bool src_is_1d = src->target == PIPE_TEXTURE_1D ||
741bf215546Sopenharmony_ci                    src->target == PIPE_TEXTURE_1D_ARRAY;
742bf215546Sopenharmony_ci   int block_x, block_y;
743bf215546Sopenharmony_ci   int block_z = 1;
744bf215546Sopenharmony_ci
745bf215546Sopenharmony_ci   /* Choose the block dimensions based on the copy area size. */
746bf215546Sopenharmony_ci   if (src_box->height <= 4) {
747bf215546Sopenharmony_ci      block_y = util_next_power_of_two(src_box->height);
748bf215546Sopenharmony_ci      block_x = 64 / block_y;
749bf215546Sopenharmony_ci   } else if (src_box->width <= 4) {
750bf215546Sopenharmony_ci      block_x = util_next_power_of_two(src_box->width);
751bf215546Sopenharmony_ci      block_y = 64 / block_x;
752bf215546Sopenharmony_ci   } else if (is_linear) {
753bf215546Sopenharmony_ci      block_x = 64;
754bf215546Sopenharmony_ci      block_y = 1;
755bf215546Sopenharmony_ci   } else {
756bf215546Sopenharmony_ci      block_x = 8;
757bf215546Sopenharmony_ci      block_y = 8;
758bf215546Sopenharmony_ci   }
759bf215546Sopenharmony_ci
760bf215546Sopenharmony_ci   sctx->cs_user_data[0] = src_box->x | (dstx << 16);
761bf215546Sopenharmony_ci   sctx->cs_user_data[1] = src_box->y | (dsty << 16);
762bf215546Sopenharmony_ci   sctx->cs_user_data[2] = src_box->z | (dstz << 16);
763bf215546Sopenharmony_ci
764bf215546Sopenharmony_ci   set_work_size(&info, block_x, block_y, block_z,
765bf215546Sopenharmony_ci                 src_box->width, src_box->height, src_box->depth);
766bf215546Sopenharmony_ci
767bf215546Sopenharmony_ci   void **copy_image_cs_ptr = &sctx->cs_copy_image[src_is_1d][dst_is_1d];
768bf215546Sopenharmony_ci   if (!*copy_image_cs_ptr)
769bf215546Sopenharmony_ci      *copy_image_cs_ptr = si_create_copy_image_cs(sctx, src_is_1d, dst_is_1d);
770bf215546Sopenharmony_ci
771bf215546Sopenharmony_ci   assert(*copy_image_cs_ptr);
772bf215546Sopenharmony_ci
773bf215546Sopenharmony_ci   si_launch_grid_internal_images(sctx, image, 2, &info, *copy_image_cs_ptr, flags);
774bf215546Sopenharmony_ci   return true;
775bf215546Sopenharmony_ci}
776bf215546Sopenharmony_ci
777bf215546Sopenharmony_civoid si_retile_dcc(struct si_context *sctx, struct si_texture *tex)
778bf215546Sopenharmony_ci{
779bf215546Sopenharmony_ci   /* Set the DCC buffer. */
780bf215546Sopenharmony_ci   assert(tex->surface.meta_offset && tex->surface.meta_offset <= UINT_MAX);
781bf215546Sopenharmony_ci   assert(tex->surface.display_dcc_offset && tex->surface.display_dcc_offset <= UINT_MAX);
782bf215546Sopenharmony_ci   assert(tex->surface.display_dcc_offset < tex->surface.meta_offset);
783bf215546Sopenharmony_ci   assert(tex->buffer.bo_size <= UINT_MAX);
784bf215546Sopenharmony_ci
785bf215546Sopenharmony_ci   struct pipe_shader_buffer sb = {};
786bf215546Sopenharmony_ci   sb.buffer = &tex->buffer.b.b;
787bf215546Sopenharmony_ci   sb.buffer_offset = tex->surface.display_dcc_offset;
788bf215546Sopenharmony_ci   sb.buffer_size = tex->buffer.bo_size - sb.buffer_offset;
789bf215546Sopenharmony_ci
790bf215546Sopenharmony_ci   sctx->cs_user_data[0] = tex->surface.meta_offset - tex->surface.display_dcc_offset;
791bf215546Sopenharmony_ci   sctx->cs_user_data[1] = (tex->surface.u.gfx9.color.dcc_pitch_max + 1) |
792bf215546Sopenharmony_ci                           (tex->surface.u.gfx9.color.dcc_height << 16);
793bf215546Sopenharmony_ci   sctx->cs_user_data[2] = (tex->surface.u.gfx9.color.display_dcc_pitch_max + 1) |
794bf215546Sopenharmony_ci                           (tex->surface.u.gfx9.color.display_dcc_height << 16);
795bf215546Sopenharmony_ci
796bf215546Sopenharmony_ci   /* We have only 1 variant per bpp for now, so expect 32 bpp. */
797bf215546Sopenharmony_ci   assert(tex->surface.bpe == 4);
798bf215546Sopenharmony_ci
799bf215546Sopenharmony_ci   void **shader = &sctx->cs_dcc_retile[tex->surface.u.gfx9.swizzle_mode];
800bf215546Sopenharmony_ci   if (!*shader)
801bf215546Sopenharmony_ci      *shader = si_create_dcc_retile_cs(sctx, &tex->surface);
802bf215546Sopenharmony_ci
803bf215546Sopenharmony_ci   /* Dispatch compute. */
804bf215546Sopenharmony_ci   unsigned width = DIV_ROUND_UP(tex->buffer.b.b.width0, tex->surface.u.gfx9.color.dcc_block_width);
805bf215546Sopenharmony_ci   unsigned height = DIV_ROUND_UP(tex->buffer.b.b.height0, tex->surface.u.gfx9.color.dcc_block_height);
806bf215546Sopenharmony_ci
807bf215546Sopenharmony_ci   struct pipe_grid_info info = {};
808bf215546Sopenharmony_ci   info.block[0] = 8;
809bf215546Sopenharmony_ci   info.block[1] = 8;
810bf215546Sopenharmony_ci   info.block[2] = 1;
811bf215546Sopenharmony_ci   info.last_block[0] = width % info.block[0];
812bf215546Sopenharmony_ci   info.last_block[1] = height % info.block[1];
813bf215546Sopenharmony_ci   info.grid[0] = DIV_ROUND_UP(width, info.block[0]);
814bf215546Sopenharmony_ci   info.grid[1] = DIV_ROUND_UP(height, info.block[1]);
815bf215546Sopenharmony_ci   info.grid[2] = 1;
816bf215546Sopenharmony_ci
817bf215546Sopenharmony_ci   si_launch_grid_internal_ssbos(sctx, &info, *shader, SI_OP_SYNC_BEFORE,
818bf215546Sopenharmony_ci                                 SI_COHERENCY_CB_META, 1, &sb, 0x1);
819bf215546Sopenharmony_ci
820bf215546Sopenharmony_ci   /* Don't flush caches. L2 will be flushed by the kernel fence. */
821bf215546Sopenharmony_ci}
822bf215546Sopenharmony_ci
823bf215546Sopenharmony_civoid gfx9_clear_dcc_msaa(struct si_context *sctx, struct pipe_resource *res, uint32_t clear_value,
824bf215546Sopenharmony_ci                         unsigned flags, enum si_coherency coher)
825bf215546Sopenharmony_ci{
826bf215546Sopenharmony_ci   struct si_texture *tex = (struct si_texture*)res;
827bf215546Sopenharmony_ci
828bf215546Sopenharmony_ci   assert(sctx->gfx_level < GFX11);
829bf215546Sopenharmony_ci
830bf215546Sopenharmony_ci   /* Set the DCC buffer. */
831bf215546Sopenharmony_ci   assert(tex->surface.meta_offset && tex->surface.meta_offset <= UINT_MAX);
832bf215546Sopenharmony_ci   assert(tex->buffer.bo_size <= UINT_MAX);
833bf215546Sopenharmony_ci
834bf215546Sopenharmony_ci   struct pipe_shader_buffer sb = {};
835bf215546Sopenharmony_ci   sb.buffer = &tex->buffer.b.b;
836bf215546Sopenharmony_ci   sb.buffer_offset = tex->surface.meta_offset;
837bf215546Sopenharmony_ci   sb.buffer_size = tex->buffer.bo_size - sb.buffer_offset;
838bf215546Sopenharmony_ci
839bf215546Sopenharmony_ci   sctx->cs_user_data[0] = (tex->surface.u.gfx9.color.dcc_pitch_max + 1) |
840bf215546Sopenharmony_ci                           (tex->surface.u.gfx9.color.dcc_height << 16);
841bf215546Sopenharmony_ci   sctx->cs_user_data[1] = (clear_value & 0xffff) |
842bf215546Sopenharmony_ci                           ((uint32_t)tex->surface.tile_swizzle << 16);
843bf215546Sopenharmony_ci
844bf215546Sopenharmony_ci   /* These variables identify the shader variant. */
845bf215546Sopenharmony_ci   unsigned swizzle_mode = tex->surface.u.gfx9.swizzle_mode;
846bf215546Sopenharmony_ci   unsigned bpe_log2 = util_logbase2(tex->surface.bpe);
847bf215546Sopenharmony_ci   unsigned log2_samples = util_logbase2(tex->buffer.b.b.nr_samples);
848bf215546Sopenharmony_ci   bool fragments8 = tex->buffer.b.b.nr_storage_samples == 8;
849bf215546Sopenharmony_ci   bool is_array = tex->buffer.b.b.array_size > 1;
850bf215546Sopenharmony_ci   void **shader = &sctx->cs_clear_dcc_msaa[swizzle_mode][bpe_log2][fragments8][log2_samples - 2][is_array];
851bf215546Sopenharmony_ci
852bf215546Sopenharmony_ci   if (!*shader)
853bf215546Sopenharmony_ci      *shader = gfx9_create_clear_dcc_msaa_cs(sctx, tex);
854bf215546Sopenharmony_ci
855bf215546Sopenharmony_ci   /* Dispatch compute. */
856bf215546Sopenharmony_ci   unsigned width = DIV_ROUND_UP(tex->buffer.b.b.width0, tex->surface.u.gfx9.color.dcc_block_width);
857bf215546Sopenharmony_ci   unsigned height = DIV_ROUND_UP(tex->buffer.b.b.height0, tex->surface.u.gfx9.color.dcc_block_height);
858bf215546Sopenharmony_ci   unsigned depth = DIV_ROUND_UP(tex->buffer.b.b.array_size, tex->surface.u.gfx9.color.dcc_block_depth);
859bf215546Sopenharmony_ci
860bf215546Sopenharmony_ci   struct pipe_grid_info info = {};
861bf215546Sopenharmony_ci   info.block[0] = 8;
862bf215546Sopenharmony_ci   info.block[1] = 8;
863bf215546Sopenharmony_ci   info.block[2] = 1;
864bf215546Sopenharmony_ci   info.last_block[0] = width % info.block[0];
865bf215546Sopenharmony_ci   info.last_block[1] = height % info.block[1];
866bf215546Sopenharmony_ci   info.grid[0] = DIV_ROUND_UP(width, info.block[0]);
867bf215546Sopenharmony_ci   info.grid[1] = DIV_ROUND_UP(height, info.block[1]);
868bf215546Sopenharmony_ci   info.grid[2] = depth;
869bf215546Sopenharmony_ci
870bf215546Sopenharmony_ci   si_launch_grid_internal_ssbos(sctx, &info, *shader, flags, coher, 1, &sb, 0x1);
871bf215546Sopenharmony_ci}
872bf215546Sopenharmony_ci
873bf215546Sopenharmony_ci/* Expand FMASK to make it identity, so that image stores can ignore it. */
874bf215546Sopenharmony_civoid si_compute_expand_fmask(struct pipe_context *ctx, struct pipe_resource *tex)
875bf215546Sopenharmony_ci{
876bf215546Sopenharmony_ci   struct si_context *sctx = (struct si_context *)ctx;
877bf215546Sopenharmony_ci   bool is_array = tex->target == PIPE_TEXTURE_2D_ARRAY;
878bf215546Sopenharmony_ci   unsigned log_fragments = util_logbase2(tex->nr_storage_samples);
879bf215546Sopenharmony_ci   unsigned log_samples = util_logbase2(tex->nr_samples);
880bf215546Sopenharmony_ci   assert(tex->nr_samples >= 2);
881bf215546Sopenharmony_ci
882bf215546Sopenharmony_ci   assert(sctx->gfx_level < GFX11);
883bf215546Sopenharmony_ci
884bf215546Sopenharmony_ci   /* EQAA FMASK expansion is unimplemented. */
885bf215546Sopenharmony_ci   if (tex->nr_samples != tex->nr_storage_samples)
886bf215546Sopenharmony_ci      return;
887bf215546Sopenharmony_ci
888bf215546Sopenharmony_ci   si_make_CB_shader_coherent(sctx, tex->nr_samples, true,
889bf215546Sopenharmony_ci                              ((struct si_texture*)tex)->surface.u.gfx9.color.dcc.pipe_aligned);
890bf215546Sopenharmony_ci
891bf215546Sopenharmony_ci   /* Save states. */
892bf215546Sopenharmony_ci   struct pipe_image_view saved_image = {0};
893bf215546Sopenharmony_ci   util_copy_image_view(&saved_image, &sctx->images[PIPE_SHADER_COMPUTE].views[0]);
894bf215546Sopenharmony_ci
895bf215546Sopenharmony_ci   /* Bind the image. */
896bf215546Sopenharmony_ci   struct pipe_image_view image = {0};
897bf215546Sopenharmony_ci   image.resource = tex;
898bf215546Sopenharmony_ci   /* Don't set WRITE so as not to trigger FMASK expansion, causing
899bf215546Sopenharmony_ci    * an infinite loop. */
900bf215546Sopenharmony_ci   image.shader_access = image.access = PIPE_IMAGE_ACCESS_READ;
901bf215546Sopenharmony_ci   image.format = util_format_linear(tex->format);
902bf215546Sopenharmony_ci   if (is_array)
903bf215546Sopenharmony_ci      image.u.tex.last_layer = tex->array_size - 1;
904bf215546Sopenharmony_ci
905bf215546Sopenharmony_ci   ctx->set_shader_images(ctx, PIPE_SHADER_COMPUTE, 0, 1, 0, &image);
906bf215546Sopenharmony_ci
907bf215546Sopenharmony_ci   /* Bind the shader. */
908bf215546Sopenharmony_ci   void **shader = &sctx->cs_fmask_expand[log_samples - 1][is_array];
909bf215546Sopenharmony_ci   if (!*shader)
910bf215546Sopenharmony_ci      *shader = si_create_fmask_expand_cs(ctx, tex->nr_samples, is_array);
911bf215546Sopenharmony_ci
912bf215546Sopenharmony_ci   /* Dispatch compute. */
913bf215546Sopenharmony_ci   struct pipe_grid_info info = {0};
914bf215546Sopenharmony_ci   info.block[0] = 8;
915bf215546Sopenharmony_ci   info.last_block[0] = tex->width0 % 8;
916bf215546Sopenharmony_ci   info.block[1] = 8;
917bf215546Sopenharmony_ci   info.last_block[1] = tex->height0 % 8;
918bf215546Sopenharmony_ci   info.block[2] = 1;
919bf215546Sopenharmony_ci   info.grid[0] = DIV_ROUND_UP(tex->width0, 8);
920bf215546Sopenharmony_ci   info.grid[1] = DIV_ROUND_UP(tex->height0, 8);
921bf215546Sopenharmony_ci   info.grid[2] = is_array ? tex->array_size : 1;
922bf215546Sopenharmony_ci
923bf215546Sopenharmony_ci   si_launch_grid_internal(sctx, &info, *shader, SI_OP_SYNC_BEFORE_AFTER);
924bf215546Sopenharmony_ci
925bf215546Sopenharmony_ci   /* Restore previous states. */
926bf215546Sopenharmony_ci   ctx->set_shader_images(ctx, PIPE_SHADER_COMPUTE, 0, 1, 0, &saved_image);
927bf215546Sopenharmony_ci   pipe_resource_reference(&saved_image.resource, NULL);
928bf215546Sopenharmony_ci
929bf215546Sopenharmony_ci   /* Array of fully expanded FMASK values, arranged by [log2(fragments)][log2(samples)-1]. */
930bf215546Sopenharmony_ci#define INVALID 0 /* never used */
931bf215546Sopenharmony_ci   static const uint64_t fmask_expand_values[][4] = {
932bf215546Sopenharmony_ci      /* samples */
933bf215546Sopenharmony_ci      /* 2 (8 bpp) 4 (8 bpp)   8 (8-32bpp) 16 (16-64bpp)      fragments */
934bf215546Sopenharmony_ci      {0x02020202, 0x0E0E0E0E, 0xFEFEFEFE, 0xFFFEFFFE},      /* 1 */
935bf215546Sopenharmony_ci      {0x02020202, 0xA4A4A4A4, 0xAAA4AAA4, 0xAAAAAAA4},      /* 2 */
936bf215546Sopenharmony_ci      {INVALID, 0xE4E4E4E4, 0x44443210, 0x4444444444443210}, /* 4 */
937bf215546Sopenharmony_ci      {INVALID, INVALID, 0x76543210, 0x8888888876543210},    /* 8 */
938bf215546Sopenharmony_ci   };
939bf215546Sopenharmony_ci
940bf215546Sopenharmony_ci   /* Clear FMASK to identity. */
941bf215546Sopenharmony_ci   struct si_texture *stex = (struct si_texture *)tex;
942bf215546Sopenharmony_ci   si_clear_buffer(sctx, tex, stex->surface.fmask_offset, stex->surface.fmask_size,
943bf215546Sopenharmony_ci                   (uint32_t *)&fmask_expand_values[log_fragments][log_samples - 1],
944bf215546Sopenharmony_ci                   log_fragments >= 2 && log_samples == 4 ? 8 : 4, SI_OP_SYNC_AFTER,
945bf215546Sopenharmony_ci                   SI_COHERENCY_SHADER, SI_AUTO_SELECT_CLEAR_METHOD);
946bf215546Sopenharmony_ci}
947bf215546Sopenharmony_ci
948bf215546Sopenharmony_civoid si_init_compute_blit_functions(struct si_context *sctx)
949bf215546Sopenharmony_ci{
950bf215546Sopenharmony_ci   sctx->b.clear_buffer = si_pipe_clear_buffer;
951bf215546Sopenharmony_ci}
952bf215546Sopenharmony_ci
953bf215546Sopenharmony_ci/* Clear a region of a color surface to a constant value. */
954bf215546Sopenharmony_civoid si_compute_clear_render_target(struct pipe_context *ctx, struct pipe_surface *dstsurf,
955bf215546Sopenharmony_ci                                    const union pipe_color_union *color, unsigned dstx,
956bf215546Sopenharmony_ci                                    unsigned dsty, unsigned width, unsigned height,
957bf215546Sopenharmony_ci                                    bool render_condition_enabled)
958bf215546Sopenharmony_ci{
959bf215546Sopenharmony_ci   struct si_context *sctx = (struct si_context *)ctx;
960bf215546Sopenharmony_ci   unsigned num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
961bf215546Sopenharmony_ci   unsigned data[4 + sizeof(color->ui)] = {dstx, dsty, dstsurf->u.tex.first_layer, 0};
962bf215546Sopenharmony_ci
963bf215546Sopenharmony_ci   if (width == 0 || height == 0)
964bf215546Sopenharmony_ci      return;
965bf215546Sopenharmony_ci
966bf215546Sopenharmony_ci   if (util_format_is_srgb(dstsurf->format)) {
967bf215546Sopenharmony_ci      union pipe_color_union color_srgb;
968bf215546Sopenharmony_ci      for (int i = 0; i < 3; i++)
969bf215546Sopenharmony_ci         color_srgb.f[i] = util_format_linear_to_srgb_float(color->f[i]);
970bf215546Sopenharmony_ci      color_srgb.f[3] = color->f[3];
971bf215546Sopenharmony_ci      memcpy(data + 4, color_srgb.ui, sizeof(color->ui));
972bf215546Sopenharmony_ci   } else {
973bf215546Sopenharmony_ci      memcpy(data + 4, color->ui, sizeof(color->ui));
974bf215546Sopenharmony_ci   }
975bf215546Sopenharmony_ci
976bf215546Sopenharmony_ci   struct pipe_constant_buffer saved_cb = {};
977bf215546Sopenharmony_ci   si_get_pipe_constant_buffer(sctx, PIPE_SHADER_COMPUTE, 0, &saved_cb);
978bf215546Sopenharmony_ci
979bf215546Sopenharmony_ci   struct pipe_constant_buffer cb = {};
980bf215546Sopenharmony_ci   cb.buffer_size = sizeof(data);
981bf215546Sopenharmony_ci   cb.user_buffer = data;
982bf215546Sopenharmony_ci   ctx->set_constant_buffer(ctx, PIPE_SHADER_COMPUTE, 0, false, &cb);
983bf215546Sopenharmony_ci
984bf215546Sopenharmony_ci   struct pipe_image_view image = {0};
985bf215546Sopenharmony_ci   image.resource = dstsurf->texture;
986bf215546Sopenharmony_ci   image.shader_access = image.access = PIPE_IMAGE_ACCESS_WRITE;
987bf215546Sopenharmony_ci   image.format = util_format_linear(dstsurf->format);
988bf215546Sopenharmony_ci   image.u.tex.level = dstsurf->u.tex.level;
989bf215546Sopenharmony_ci   image.u.tex.first_layer = 0; /* 3D images ignore first_layer (BASE_ARRAY) */
990bf215546Sopenharmony_ci   image.u.tex.last_layer = dstsurf->u.tex.last_layer;
991bf215546Sopenharmony_ci
992bf215546Sopenharmony_ci   struct pipe_grid_info info = {0};
993bf215546Sopenharmony_ci   void *shader;
994bf215546Sopenharmony_ci
995bf215546Sopenharmony_ci   if (dstsurf->texture->target != PIPE_TEXTURE_1D_ARRAY) {
996bf215546Sopenharmony_ci      if (!sctx->cs_clear_render_target)
997bf215546Sopenharmony_ci         sctx->cs_clear_render_target = si_clear_render_target_shader(ctx);
998bf215546Sopenharmony_ci      shader = sctx->cs_clear_render_target;
999bf215546Sopenharmony_ci
1000bf215546Sopenharmony_ci      info.block[0] = 8;
1001bf215546Sopenharmony_ci      info.last_block[0] = width % 8;
1002bf215546Sopenharmony_ci      info.block[1] = 8;
1003bf215546Sopenharmony_ci      info.last_block[1] = height % 8;
1004bf215546Sopenharmony_ci      info.block[2] = 1;
1005bf215546Sopenharmony_ci      info.grid[0] = DIV_ROUND_UP(width, 8);
1006bf215546Sopenharmony_ci      info.grid[1] = DIV_ROUND_UP(height, 8);
1007bf215546Sopenharmony_ci      info.grid[2] = num_layers;
1008bf215546Sopenharmony_ci   } else {
1009bf215546Sopenharmony_ci      if (!sctx->cs_clear_render_target_1d_array)
1010bf215546Sopenharmony_ci         sctx->cs_clear_render_target_1d_array = si_clear_render_target_shader_1d_array(ctx);
1011bf215546Sopenharmony_ci      shader = sctx->cs_clear_render_target_1d_array;
1012bf215546Sopenharmony_ci
1013bf215546Sopenharmony_ci      info.block[0] = 64;
1014bf215546Sopenharmony_ci      info.last_block[0] = width % 64;
1015bf215546Sopenharmony_ci      info.block[1] = 1;
1016bf215546Sopenharmony_ci      info.block[2] = 1;
1017bf215546Sopenharmony_ci      info.grid[0] = DIV_ROUND_UP(width, 64);
1018bf215546Sopenharmony_ci      info.grid[1] = num_layers;
1019bf215546Sopenharmony_ci      info.grid[2] = 1;
1020bf215546Sopenharmony_ci   }
1021bf215546Sopenharmony_ci
1022bf215546Sopenharmony_ci   si_launch_grid_internal_images(sctx, &image, 1, &info, shader,
1023bf215546Sopenharmony_ci                                  SI_OP_SYNC_BEFORE_AFTER |
1024bf215546Sopenharmony_ci                                  (render_condition_enabled ? SI_OP_CS_RENDER_COND_ENABLE : 0));
1025bf215546Sopenharmony_ci
1026bf215546Sopenharmony_ci   ctx->set_constant_buffer(ctx, PIPE_SHADER_COMPUTE, 0, true, &saved_cb);
1027bf215546Sopenharmony_ci}
1028