1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
3bf215546Sopenharmony_ci * Copyright © 2018 Google, Inc.
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10bf215546Sopenharmony_ci * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci * Authors:
25bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
26bf215546Sopenharmony_ci */
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "pipe/p_state.h"
29bf215546Sopenharmony_ci#include "util/u_memory.h"
30bf215546Sopenharmony_ci#include "util/u_prim.h"
31bf215546Sopenharmony_ci#include "util/u_string.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#include "freedreno_resource.h"
34bf215546Sopenharmony_ci#include "freedreno_state.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci#include "fd6_context.h"
37bf215546Sopenharmony_ci#include "fd6_draw.h"
38bf215546Sopenharmony_ci#include "fd6_emit.h"
39bf215546Sopenharmony_ci#include "fd6_program.h"
40bf215546Sopenharmony_ci#include "fd6_vsc.h"
41bf215546Sopenharmony_ci#include "fd6_zsa.h"
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci#include "fd6_pack.h"
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_cistatic void
46bf215546Sopenharmony_cidraw_emit_xfb(struct fd_ringbuffer *ring, struct CP_DRAW_INDX_OFFSET_0 *draw0,
47bf215546Sopenharmony_ci              const struct pipe_draw_info *info,
48bf215546Sopenharmony_ci              const struct pipe_draw_indirect_info *indirect)
49bf215546Sopenharmony_ci{
50bf215546Sopenharmony_ci   struct fd_stream_output_target *target =
51bf215546Sopenharmony_ci      fd_stream_output_target(indirect->count_from_stream_output);
52bf215546Sopenharmony_ci   struct fd_resource *offset = fd_resource(target->offset_buf);
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci   /* All known firmware versions do not wait for WFI's with CP_DRAW_AUTO.
55bf215546Sopenharmony_ci    * Plus, for the common case where the counter buffer is written by
56bf215546Sopenharmony_ci    * vkCmdEndTransformFeedback, we need to wait for the CP_WAIT_MEM_WRITES to
57bf215546Sopenharmony_ci    * complete which means we need a WAIT_FOR_ME anyway.
58bf215546Sopenharmony_ci    */
59bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_WAIT_FOR_ME, 0);
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_DRAW_AUTO, 6);
62bf215546Sopenharmony_ci   OUT_RING(ring, pack_CP_DRAW_INDX_OFFSET_0(*draw0).value);
63bf215546Sopenharmony_ci   OUT_RING(ring, info->instance_count);
64bf215546Sopenharmony_ci   OUT_RELOC(ring, offset->bo, 0, 0, 0);
65bf215546Sopenharmony_ci   OUT_RING(
66bf215546Sopenharmony_ci      ring,
67bf215546Sopenharmony_ci      0); /* byte counter offset subtraced from the value read from above */
68bf215546Sopenharmony_ci   OUT_RING(ring, target->stride);
69bf215546Sopenharmony_ci}
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_cistatic void
72bf215546Sopenharmony_cidraw_emit_indirect(struct fd_ringbuffer *ring,
73bf215546Sopenharmony_ci                   struct CP_DRAW_INDX_OFFSET_0 *draw0,
74bf215546Sopenharmony_ci                   const struct pipe_draw_info *info,
75bf215546Sopenharmony_ci                   const struct pipe_draw_indirect_info *indirect,
76bf215546Sopenharmony_ci                   unsigned index_offset)
77bf215546Sopenharmony_ci{
78bf215546Sopenharmony_ci   struct fd_resource *ind = fd_resource(indirect->buffer);
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   if (info->index_size) {
81bf215546Sopenharmony_ci      struct pipe_resource *idx = info->index.resource;
82bf215546Sopenharmony_ci      unsigned max_indices = (idx->width0 - index_offset) / info->index_size;
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci      OUT_PKT(ring, CP_DRAW_INDX_INDIRECT, pack_CP_DRAW_INDX_OFFSET_0(*draw0),
85bf215546Sopenharmony_ci              A5XX_CP_DRAW_INDX_INDIRECT_INDX_BASE(fd_resource(idx)->bo,
86bf215546Sopenharmony_ci                                                   index_offset),
87bf215546Sopenharmony_ci              A5XX_CP_DRAW_INDX_INDIRECT_3(.max_indices = max_indices),
88bf215546Sopenharmony_ci              A5XX_CP_DRAW_INDX_INDIRECT_INDIRECT(ind->bo, indirect->offset));
89bf215546Sopenharmony_ci   } else {
90bf215546Sopenharmony_ci      OUT_PKT(ring, CP_DRAW_INDIRECT, pack_CP_DRAW_INDX_OFFSET_0(*draw0),
91bf215546Sopenharmony_ci              A5XX_CP_DRAW_INDIRECT_INDIRECT(ind->bo, indirect->offset));
92bf215546Sopenharmony_ci   }
93bf215546Sopenharmony_ci}
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_cistatic void
96bf215546Sopenharmony_cidraw_emit(struct fd_ringbuffer *ring, struct CP_DRAW_INDX_OFFSET_0 *draw0,
97bf215546Sopenharmony_ci          const struct pipe_draw_info *info,
98bf215546Sopenharmony_ci          const struct pipe_draw_start_count_bias *draw, unsigned index_offset)
99bf215546Sopenharmony_ci{
100bf215546Sopenharmony_ci   if (info->index_size) {
101bf215546Sopenharmony_ci      assert(!info->has_user_indices);
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci      struct pipe_resource *idx_buffer = info->index.resource;
104bf215546Sopenharmony_ci      unsigned max_indices =
105bf215546Sopenharmony_ci         (idx_buffer->width0 - index_offset) / info->index_size;
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci      OUT_PKT(ring, CP_DRAW_INDX_OFFSET, pack_CP_DRAW_INDX_OFFSET_0(*draw0),
108bf215546Sopenharmony_ci              CP_DRAW_INDX_OFFSET_1(.num_instances = info->instance_count),
109bf215546Sopenharmony_ci              CP_DRAW_INDX_OFFSET_2(.num_indices = draw->count),
110bf215546Sopenharmony_ci              CP_DRAW_INDX_OFFSET_3(.first_indx = draw->start),
111bf215546Sopenharmony_ci              A5XX_CP_DRAW_INDX_OFFSET_INDX_BASE(fd_resource(idx_buffer)->bo,
112bf215546Sopenharmony_ci                                                 index_offset),
113bf215546Sopenharmony_ci              A5XX_CP_DRAW_INDX_OFFSET_6(.max_indices = max_indices));
114bf215546Sopenharmony_ci   } else {
115bf215546Sopenharmony_ci      OUT_PKT(ring, CP_DRAW_INDX_OFFSET, pack_CP_DRAW_INDX_OFFSET_0(*draw0),
116bf215546Sopenharmony_ci              CP_DRAW_INDX_OFFSET_1(.num_instances = info->instance_count),
117bf215546Sopenharmony_ci              CP_DRAW_INDX_OFFSET_2(.num_indices = draw->count));
118bf215546Sopenharmony_ci   }
119bf215546Sopenharmony_ci}
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_cistatic void
122bf215546Sopenharmony_cifixup_draw_state(struct fd_context *ctx, struct fd6_emit *emit) assert_dt
123bf215546Sopenharmony_ci{
124bf215546Sopenharmony_ci   if (ctx->last.dirty ||
125bf215546Sopenharmony_ci       (ctx->last.primitive_restart != emit->primitive_restart)) {
126bf215546Sopenharmony_ci      /* rasterizer state is effected by primitive-restart: */
127bf215546Sopenharmony_ci      fd_context_dirty(ctx, FD_DIRTY_RASTERIZER);
128bf215546Sopenharmony_ci      ctx->last.primitive_restart = emit->primitive_restart;
129bf215546Sopenharmony_ci   }
130bf215546Sopenharmony_ci}
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_cistatic bool
133bf215546Sopenharmony_cifd6_draw_vbo(struct fd_context *ctx, const struct pipe_draw_info *info,
134bf215546Sopenharmony_ci             unsigned drawid_offset,
135bf215546Sopenharmony_ci             const struct pipe_draw_indirect_info *indirect,
136bf215546Sopenharmony_ci             const struct pipe_draw_start_count_bias *draw,
137bf215546Sopenharmony_ci             unsigned index_offset) assert_dt
138bf215546Sopenharmony_ci{
139bf215546Sopenharmony_ci   struct fd6_context *fd6_ctx = fd6_context(ctx);
140bf215546Sopenharmony_ci   struct shader_info *gs_info = ir3_get_shader_info(ctx->prog.gs);
141bf215546Sopenharmony_ci   struct fd6_emit emit = {
142bf215546Sopenharmony_ci      .ctx = ctx,
143bf215546Sopenharmony_ci      .vtx = &ctx->vtx,
144bf215546Sopenharmony_ci      .info = info,
145bf215546Sopenharmony_ci      .drawid_offset = drawid_offset,
146bf215546Sopenharmony_ci      .indirect = indirect,
147bf215546Sopenharmony_ci      .draw = draw,
148bf215546Sopenharmony_ci      .key = {
149bf215546Sopenharmony_ci         .vs = ctx->prog.vs,
150bf215546Sopenharmony_ci         .gs = ctx->prog.gs,
151bf215546Sopenharmony_ci         .fs = ctx->prog.fs,
152bf215546Sopenharmony_ci         .key = {
153bf215546Sopenharmony_ci            .rasterflat = ctx->rasterizer->flatshade,
154bf215546Sopenharmony_ci            .ucp_enables = ctx->rasterizer->clip_plane_enable,
155bf215546Sopenharmony_ci            .layer_zero = !gs_info || !(gs_info->outputs_written & VARYING_BIT_LAYER),
156bf215546Sopenharmony_ci            .sample_shading = (ctx->min_samples > 1),
157bf215546Sopenharmony_ci            .msaa = (ctx->framebuffer.samples > 1),
158bf215546Sopenharmony_ci         },
159bf215546Sopenharmony_ci         .clip_plane_enable = ctx->rasterizer->clip_plane_enable,
160bf215546Sopenharmony_ci      },
161bf215546Sopenharmony_ci      .rasterflat = ctx->rasterizer->flatshade,
162bf215546Sopenharmony_ci      .sprite_coord_enable = ctx->rasterizer->sprite_coord_enable,
163bf215546Sopenharmony_ci      .sprite_coord_mode = ctx->rasterizer->sprite_coord_mode,
164bf215546Sopenharmony_ci      .primitive_restart = info->primitive_restart && info->index_size,
165bf215546Sopenharmony_ci      .patch_vertices = ctx->patch_vertices,
166bf215546Sopenharmony_ci   };
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci   if (!(ctx->prog.vs && ctx->prog.fs))
169bf215546Sopenharmony_ci      return false;
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   if (info->mode == PIPE_PRIM_PATCHES) {
172bf215546Sopenharmony_ci      emit.key.hs = ctx->prog.hs;
173bf215546Sopenharmony_ci      emit.key.ds = ctx->prog.ds;
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci      if (!(ctx->prog.hs && ctx->prog.ds))
176bf215546Sopenharmony_ci         return false;
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci      struct shader_info *ds_info = ir3_get_shader_info(emit.key.ds);
179bf215546Sopenharmony_ci      emit.key.key.tessellation = ir3_tess_mode(ds_info->tess._primitive_mode);
180bf215546Sopenharmony_ci      ctx->gen_dirty |= BIT(FD6_GROUP_PRIMITIVE_PARAMS);
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci      struct shader_info *fs_info = ir3_get_shader_info(emit.key.fs);
183bf215546Sopenharmony_ci      emit.key.key.tcs_store_primid =
184bf215546Sopenharmony_ci         BITSET_TEST(ds_info->system_values_read, SYSTEM_VALUE_PRIMITIVE_ID) ||
185bf215546Sopenharmony_ci         (gs_info && BITSET_TEST(gs_info->system_values_read, SYSTEM_VALUE_PRIMITIVE_ID)) ||
186bf215546Sopenharmony_ci         (fs_info && (fs_info->inputs_read & (1ull << VARYING_SLOT_PRIMITIVE_ID)));
187bf215546Sopenharmony_ci   }
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci   if (emit.key.gs) {
190bf215546Sopenharmony_ci      emit.key.key.has_gs = true;
191bf215546Sopenharmony_ci      ctx->gen_dirty |= BIT(FD6_GROUP_PRIMITIVE_PARAMS);
192bf215546Sopenharmony_ci   }
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_ci   if (!(emit.key.hs || emit.key.ds || emit.key.gs || indirect))
195bf215546Sopenharmony_ci      fd6_vsc_update_sizes(ctx->batch, info, draw);
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci   ir3_fixup_shader_state(&ctx->base, &emit.key.key);
198bf215546Sopenharmony_ci
199bf215546Sopenharmony_ci   if (!(ctx->gen_dirty & BIT(FD6_GROUP_PROG))) {
200bf215546Sopenharmony_ci      emit.prog = fd6_ctx->prog;
201bf215546Sopenharmony_ci   } else {
202bf215546Sopenharmony_ci      fd6_ctx->prog = fd6_emit_get_prog(&emit);
203bf215546Sopenharmony_ci   }
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci   /* bail if compile failed: */
206bf215546Sopenharmony_ci   if (!fd6_ctx->prog)
207bf215546Sopenharmony_ci      return false;
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci   fixup_draw_state(ctx, &emit);
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci   /* *after* fixup_shader_state(): */
212bf215546Sopenharmony_ci   emit.dirty = ctx->dirty;
213bf215546Sopenharmony_ci   emit.dirty_groups = ctx->gen_dirty;
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_ci   emit.bs = fd6_emit_get_prog(&emit)->bs;
216bf215546Sopenharmony_ci   emit.vs = fd6_emit_get_prog(&emit)->vs;
217bf215546Sopenharmony_ci   emit.hs = fd6_emit_get_prog(&emit)->hs;
218bf215546Sopenharmony_ci   emit.ds = fd6_emit_get_prog(&emit)->ds;
219bf215546Sopenharmony_ci   emit.gs = fd6_emit_get_prog(&emit)->gs;
220bf215546Sopenharmony_ci   emit.fs = fd6_emit_get_prog(&emit)->fs;
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ci   if (emit.vs->need_driver_params || fd6_ctx->has_dp_state)
223bf215546Sopenharmony_ci      emit.dirty_groups |= BIT(FD6_GROUP_DRIVER_PARAMS);
224bf215546Sopenharmony_ci   else if (emit.gs && emit.gs->need_driver_params)
225bf215546Sopenharmony_ci      emit.dirty_groups |= BIT(FD6_GROUP_DRIVER_PARAMS);
226bf215546Sopenharmony_ci   else if (emit.ds && emit.ds->need_driver_params)
227bf215546Sopenharmony_ci      emit.dirty_groups |= BIT(FD6_GROUP_DRIVER_PARAMS);
228bf215546Sopenharmony_ci
229bf215546Sopenharmony_ci   /* If we are doing xfb, we need to emit the xfb state on every draw: */
230bf215546Sopenharmony_ci   if (emit.prog->stream_output)
231bf215546Sopenharmony_ci      emit.dirty_groups |= BIT(FD6_GROUP_SO);
232bf215546Sopenharmony_ci
233bf215546Sopenharmony_ci   if (unlikely(ctx->stats_users > 0)) {
234bf215546Sopenharmony_ci      ctx->stats.vs_regs += ir3_shader_halfregs(emit.vs);
235bf215546Sopenharmony_ci      ctx->stats.hs_regs += COND(emit.hs, ir3_shader_halfregs(emit.hs));
236bf215546Sopenharmony_ci      ctx->stats.ds_regs += COND(emit.ds, ir3_shader_halfregs(emit.ds));
237bf215546Sopenharmony_ci      ctx->stats.gs_regs += COND(emit.gs, ir3_shader_halfregs(emit.gs));
238bf215546Sopenharmony_ci      ctx->stats.fs_regs += ir3_shader_halfregs(emit.fs);
239bf215546Sopenharmony_ci   }
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci   struct fd_ringbuffer *ring = ctx->batch->draw;
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ci   struct CP_DRAW_INDX_OFFSET_0 draw0 = {
244bf215546Sopenharmony_ci      .prim_type = ctx->screen->primtypes[info->mode],
245bf215546Sopenharmony_ci      .vis_cull = USE_VISIBILITY,
246bf215546Sopenharmony_ci      .gs_enable = !!emit.key.gs,
247bf215546Sopenharmony_ci   };
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_ci   if (indirect && indirect->count_from_stream_output) {
250bf215546Sopenharmony_ci      draw0.source_select = DI_SRC_SEL_AUTO_XFB;
251bf215546Sopenharmony_ci   } else if (info->index_size) {
252bf215546Sopenharmony_ci      draw0.source_select = DI_SRC_SEL_DMA;
253bf215546Sopenharmony_ci      draw0.index_size = fd4_size2indextype(info->index_size);
254bf215546Sopenharmony_ci   } else {
255bf215546Sopenharmony_ci      draw0.source_select = DI_SRC_SEL_AUTO_INDEX;
256bf215546Sopenharmony_ci   }
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_ci   if (info->mode == PIPE_PRIM_PATCHES) {
259bf215546Sopenharmony_ci      uint32_t factor_stride = ir3_tess_factor_stride(emit.key.key.tessellation);
260bf215546Sopenharmony_ci
261bf215546Sopenharmony_ci      STATIC_ASSERT(IR3_TESS_ISOLINES == TESS_ISOLINES + 1);
262bf215546Sopenharmony_ci      STATIC_ASSERT(IR3_TESS_TRIANGLES == TESS_TRIANGLES + 1);
263bf215546Sopenharmony_ci      STATIC_ASSERT(IR3_TESS_QUADS == TESS_QUADS + 1);
264bf215546Sopenharmony_ci      draw0.patch_type = emit.key.key.tessellation - 1;
265bf215546Sopenharmony_ci
266bf215546Sopenharmony_ci      draw0.prim_type = DI_PT_PATCHES0 + ctx->patch_vertices;
267bf215546Sopenharmony_ci      draw0.tess_enable = true;
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci      /* maximum number of patches that can fit in tess factor/param buffers */
270bf215546Sopenharmony_ci      uint32_t subdraw_size = MIN2(FD6_TESS_FACTOR_SIZE / factor_stride,
271bf215546Sopenharmony_ci                                   FD6_TESS_PARAM_SIZE / (emit.hs->output_size * 4));
272bf215546Sopenharmony_ci      /* convert from # of patches to draw count */
273bf215546Sopenharmony_ci      subdraw_size *= ctx->patch_vertices;
274bf215546Sopenharmony_ci
275bf215546Sopenharmony_ci      OUT_PKT7(ring, CP_SET_SUBDRAW_SIZE, 1);
276bf215546Sopenharmony_ci      OUT_RING(ring, subdraw_size);
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci      ctx->batch->tessellation = true;
279bf215546Sopenharmony_ci   }
280bf215546Sopenharmony_ci
281bf215546Sopenharmony_ci   uint32_t index_start = info->index_size ? draw->index_bias : draw->start;
282bf215546Sopenharmony_ci   if (ctx->last.dirty || (ctx->last.index_start != index_start)) {
283bf215546Sopenharmony_ci      OUT_PKT4(ring, REG_A6XX_VFD_INDEX_OFFSET, 1);
284bf215546Sopenharmony_ci      OUT_RING(ring, index_start); /* VFD_INDEX_OFFSET */
285bf215546Sopenharmony_ci      ctx->last.index_start = index_start;
286bf215546Sopenharmony_ci   }
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_ci   if (ctx->last.dirty || (ctx->last.instance_start != info->start_instance)) {
289bf215546Sopenharmony_ci      OUT_PKT4(ring, REG_A6XX_VFD_INSTANCE_START_OFFSET, 1);
290bf215546Sopenharmony_ci      OUT_RING(ring, info->start_instance); /* VFD_INSTANCE_START_OFFSET */
291bf215546Sopenharmony_ci      ctx->last.instance_start = info->start_instance;
292bf215546Sopenharmony_ci   }
293bf215546Sopenharmony_ci
294bf215546Sopenharmony_ci   uint32_t restart_index =
295bf215546Sopenharmony_ci      info->primitive_restart ? info->restart_index : 0xffffffff;
296bf215546Sopenharmony_ci   if (ctx->last.dirty || (ctx->last.restart_index != restart_index)) {
297bf215546Sopenharmony_ci      OUT_PKT4(ring, REG_A6XX_PC_RESTART_INDEX, 1);
298bf215546Sopenharmony_ci      OUT_RING(ring, restart_index); /* PC_RESTART_INDEX */
299bf215546Sopenharmony_ci      ctx->last.restart_index = restart_index;
300bf215546Sopenharmony_ci   }
301bf215546Sopenharmony_ci
302bf215546Sopenharmony_ci   // TODO move fd6_emit_streamout.. I think..
303bf215546Sopenharmony_ci   if (emit.dirty_groups)
304bf215546Sopenharmony_ci      fd6_emit_state(ring, &emit);
305bf215546Sopenharmony_ci
306bf215546Sopenharmony_ci   /* for debug after a lock up, write a unique counter value
307bf215546Sopenharmony_ci    * to scratch7 for each draw, to make it easier to match up
308bf215546Sopenharmony_ci    * register dumps to cmdstream.  The combination of IB
309bf215546Sopenharmony_ci    * (scratch6) and DRAW is enough to "triangulate" the
310bf215546Sopenharmony_ci    * particular draw that caused lockup.
311bf215546Sopenharmony_ci    */
312bf215546Sopenharmony_ci   emit_marker6(ring, 7);
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_ci   if (indirect) {
315bf215546Sopenharmony_ci      if (indirect->count_from_stream_output) {
316bf215546Sopenharmony_ci         draw_emit_xfb(ring, &draw0, info, indirect);
317bf215546Sopenharmony_ci      } else {
318bf215546Sopenharmony_ci         draw_emit_indirect(ring, &draw0, info, indirect, index_offset);
319bf215546Sopenharmony_ci      }
320bf215546Sopenharmony_ci   } else {
321bf215546Sopenharmony_ci      draw_emit(ring, &draw0, info, draw, index_offset);
322bf215546Sopenharmony_ci   }
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ci   emit_marker6(ring, 7);
325bf215546Sopenharmony_ci   fd_reset_wfi(ctx->batch);
326bf215546Sopenharmony_ci
327bf215546Sopenharmony_ci   if (emit.streamout_mask) {
328bf215546Sopenharmony_ci      struct fd_ringbuffer *ring = ctx->batch->draw;
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ci      for (unsigned i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
331bf215546Sopenharmony_ci         if (emit.streamout_mask & (1 << i)) {
332bf215546Sopenharmony_ci            fd6_event_write(ctx->batch, ring, FLUSH_SO_0 + i, false);
333bf215546Sopenharmony_ci         }
334bf215546Sopenharmony_ci      }
335bf215546Sopenharmony_ci   }
336bf215546Sopenharmony_ci
337bf215546Sopenharmony_ci   fd_context_all_clean(ctx);
338bf215546Sopenharmony_ci
339bf215546Sopenharmony_ci   return true;
340bf215546Sopenharmony_ci}
341bf215546Sopenharmony_ci
342bf215546Sopenharmony_cistatic void
343bf215546Sopenharmony_cifd6_clear_lrz(struct fd_batch *batch, struct fd_resource *zsbuf, double depth) assert_dt
344bf215546Sopenharmony_ci{
345bf215546Sopenharmony_ci   struct fd_ringbuffer *ring;
346bf215546Sopenharmony_ci   struct fd_screen *screen = batch->ctx->screen;
347bf215546Sopenharmony_ci
348bf215546Sopenharmony_ci   ring = fd_batch_get_prologue(batch);
349bf215546Sopenharmony_ci
350bf215546Sopenharmony_ci   emit_marker6(ring, 7);
351bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_SET_MARKER, 1);
352bf215546Sopenharmony_ci   OUT_RING(ring, A6XX_CP_SET_MARKER_0_MODE(RM6_BYPASS));
353bf215546Sopenharmony_ci   emit_marker6(ring, 7);
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_ci   OUT_WFI5(ring);
356bf215546Sopenharmony_ci
357bf215546Sopenharmony_ci   OUT_REG(ring, A6XX_RB_CCU_CNTL(.color_offset = screen->ccu_offset_bypass));
358bf215546Sopenharmony_ci
359bf215546Sopenharmony_ci   OUT_REG(ring,
360bf215546Sopenharmony_ci           A6XX_HLSQ_INVALIDATE_CMD(.vs_state = true, .hs_state = true,
361bf215546Sopenharmony_ci                                    .ds_state = true, .gs_state = true,
362bf215546Sopenharmony_ci                                    .fs_state = true, .cs_state = true,
363bf215546Sopenharmony_ci                                    .gfx_ibo = true, .cs_ibo = true,
364bf215546Sopenharmony_ci                                    .gfx_shared_const = true,
365bf215546Sopenharmony_ci                                    .gfx_bindless = 0x1f, .cs_bindless = 0x1f));
366bf215546Sopenharmony_ci
367bf215546Sopenharmony_ci   emit_marker6(ring, 7);
368bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_SET_MARKER, 1);
369bf215546Sopenharmony_ci   OUT_RING(ring, A6XX_CP_SET_MARKER_0_MODE(RM6_BLIT2DSCALE));
370bf215546Sopenharmony_ci   emit_marker6(ring, 7);
371bf215546Sopenharmony_ci
372bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_RB_2D_UNKNOWN_8C01, 1);
373bf215546Sopenharmony_ci   OUT_RING(ring, 0x0);
374bf215546Sopenharmony_ci
375bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_SP_PS_2D_SRC_INFO, 13);
376bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
377bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
378bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
379bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
380bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
381bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
382bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
383bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
384bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
385bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
386bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
387bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
388bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
389bf215546Sopenharmony_ci
390bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_SP_2D_DST_FORMAT, 1);
391bf215546Sopenharmony_ci   OUT_RING(ring, 0x0000f410);
392bf215546Sopenharmony_ci
393bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_GRAS_2D_BLIT_CNTL, 1);
394bf215546Sopenharmony_ci   OUT_RING(ring,
395bf215546Sopenharmony_ci            A6XX_GRAS_2D_BLIT_CNTL_COLOR_FORMAT(FMT6_16_UNORM) | 0x4f00080);
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_RB_2D_BLIT_CNTL, 1);
398bf215546Sopenharmony_ci   OUT_RING(ring, A6XX_RB_2D_BLIT_CNTL_COLOR_FORMAT(FMT6_16_UNORM) | 0x4f00080);
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_ci   fd6_event_write(batch, ring, PC_CCU_FLUSH_COLOR_TS, true);
401bf215546Sopenharmony_ci   fd6_event_write(batch, ring, PC_CCU_INVALIDATE_COLOR, false);
402bf215546Sopenharmony_ci   fd_wfi(batch, ring);
403bf215546Sopenharmony_ci
404bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_RB_2D_SRC_SOLID_C0, 4);
405bf215546Sopenharmony_ci   OUT_RING(ring, fui(depth));
406bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
407bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
408bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
409bf215546Sopenharmony_ci
410bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_RB_2D_DST_INFO, 9);
411bf215546Sopenharmony_ci   OUT_RING(ring, A6XX_RB_2D_DST_INFO_COLOR_FORMAT(FMT6_16_UNORM) |
412bf215546Sopenharmony_ci                     A6XX_RB_2D_DST_INFO_TILE_MODE(TILE6_LINEAR) |
413bf215546Sopenharmony_ci                     A6XX_RB_2D_DST_INFO_COLOR_SWAP(WZYX));
414bf215546Sopenharmony_ci   OUT_RELOC(ring, zsbuf->lrz, 0, 0, 0);
415bf215546Sopenharmony_ci   OUT_RING(ring, A6XX_RB_2D_DST_PITCH(zsbuf->lrz_pitch * 2).value);
416bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
417bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
418bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
419bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
420bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
421bf215546Sopenharmony_ci
422bf215546Sopenharmony_ci   OUT_REG(ring, A6XX_GRAS_2D_SRC_TL_X(0), A6XX_GRAS_2D_SRC_BR_X(0),
423bf215546Sopenharmony_ci           A6XX_GRAS_2D_SRC_TL_Y(0), A6XX_GRAS_2D_SRC_BR_Y(0));
424bf215546Sopenharmony_ci
425bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_GRAS_2D_DST_TL, 2);
426bf215546Sopenharmony_ci   OUT_RING(ring, A6XX_GRAS_2D_DST_TL_X(0) | A6XX_GRAS_2D_DST_TL_Y(0));
427bf215546Sopenharmony_ci   OUT_RING(ring, A6XX_GRAS_2D_DST_BR_X(zsbuf->lrz_width - 1) |
428bf215546Sopenharmony_ci                     A6XX_GRAS_2D_DST_BR_Y(zsbuf->lrz_height - 1));
429bf215546Sopenharmony_ci
430bf215546Sopenharmony_ci   fd6_event_write(batch, ring, 0x3f, false);
431bf215546Sopenharmony_ci
432bf215546Sopenharmony_ci   OUT_WFI5(ring);
433bf215546Sopenharmony_ci
434bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_RB_UNKNOWN_8E04, 1);
435bf215546Sopenharmony_ci   OUT_RING(ring, screen->info->a6xx.magic.RB_UNKNOWN_8E04_blit);
436bf215546Sopenharmony_ci
437bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_BLIT, 1);
438bf215546Sopenharmony_ci   OUT_RING(ring, CP_BLIT_0_OP(BLIT_OP_SCALE));
439bf215546Sopenharmony_ci
440bf215546Sopenharmony_ci   OUT_WFI5(ring);
441bf215546Sopenharmony_ci
442bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A6XX_RB_UNKNOWN_8E04, 1);
443bf215546Sopenharmony_ci   OUT_RING(ring, 0x0); /* RB_UNKNOWN_8E04 */
444bf215546Sopenharmony_ci
445bf215546Sopenharmony_ci   fd6_event_write(batch, ring, PC_CCU_FLUSH_COLOR_TS, true);
446bf215546Sopenharmony_ci   fd6_event_write(batch, ring, PC_CCU_FLUSH_DEPTH_TS, true);
447bf215546Sopenharmony_ci   fd6_event_write(batch, ring, CACHE_FLUSH_TS, true);
448bf215546Sopenharmony_ci   fd_wfi(batch, ring);
449bf215546Sopenharmony_ci
450bf215546Sopenharmony_ci   fd6_cache_inv(batch, ring);
451bf215546Sopenharmony_ci}
452bf215546Sopenharmony_ci
453bf215546Sopenharmony_cistatic bool
454bf215546Sopenharmony_ciis_z32(enum pipe_format format)
455bf215546Sopenharmony_ci{
456bf215546Sopenharmony_ci   switch (format) {
457bf215546Sopenharmony_ci   case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
458bf215546Sopenharmony_ci   case PIPE_FORMAT_Z32_UNORM:
459bf215546Sopenharmony_ci   case PIPE_FORMAT_Z32_FLOAT:
460bf215546Sopenharmony_ci      return true;
461bf215546Sopenharmony_ci   default:
462bf215546Sopenharmony_ci      return false;
463bf215546Sopenharmony_ci   }
464bf215546Sopenharmony_ci}
465bf215546Sopenharmony_ci
466bf215546Sopenharmony_cistatic bool
467bf215546Sopenharmony_cifd6_clear(struct fd_context *ctx, unsigned buffers,
468bf215546Sopenharmony_ci          const union pipe_color_union *color, double depth,
469bf215546Sopenharmony_ci          unsigned stencil) assert_dt
470bf215546Sopenharmony_ci{
471bf215546Sopenharmony_ci   struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
472bf215546Sopenharmony_ci   const bool has_depth = pfb->zsbuf;
473bf215546Sopenharmony_ci   unsigned color_buffers = buffers >> 2;
474bf215546Sopenharmony_ci
475bf215546Sopenharmony_ci   /* we need to do multisample clear on 3d pipe, so fallback to u_blitter: */
476bf215546Sopenharmony_ci   if (pfb->samples > 1)
477bf215546Sopenharmony_ci      return false;
478bf215546Sopenharmony_ci
479bf215546Sopenharmony_ci   /* If we're clearing after draws, fallback to 3D pipe clears.  We could
480bf215546Sopenharmony_ci    * use blitter clears in the draw batch but then we'd have to patch up the
481bf215546Sopenharmony_ci    * gmem offsets. This doesn't seem like a useful thing to optimize for
482bf215546Sopenharmony_ci    * however.*/
483bf215546Sopenharmony_ci   if (ctx->batch->num_draws > 0)
484bf215546Sopenharmony_ci      return false;
485bf215546Sopenharmony_ci
486bf215546Sopenharmony_ci   u_foreach_bit (i, color_buffers)
487bf215546Sopenharmony_ci      ctx->batch->clear_color[i] = *color;
488bf215546Sopenharmony_ci   if (buffers & PIPE_CLEAR_DEPTH)
489bf215546Sopenharmony_ci      ctx->batch->clear_depth = depth;
490bf215546Sopenharmony_ci   if (buffers & PIPE_CLEAR_STENCIL)
491bf215546Sopenharmony_ci      ctx->batch->clear_stencil = stencil;
492bf215546Sopenharmony_ci
493bf215546Sopenharmony_ci   ctx->batch->fast_cleared |= buffers;
494bf215546Sopenharmony_ci
495bf215546Sopenharmony_ci   if (has_depth && (buffers & PIPE_CLEAR_DEPTH)) {
496bf215546Sopenharmony_ci      struct fd_resource *zsbuf = fd_resource(pfb->zsbuf->texture);
497bf215546Sopenharmony_ci      if (zsbuf->lrz && !is_z32(pfb->zsbuf->format)) {
498bf215546Sopenharmony_ci         zsbuf->lrz_valid = true;
499bf215546Sopenharmony_ci         zsbuf->lrz_direction = FD_LRZ_UNKNOWN;
500bf215546Sopenharmony_ci         fd6_clear_lrz(ctx->batch, zsbuf, depth);
501bf215546Sopenharmony_ci      }
502bf215546Sopenharmony_ci   }
503bf215546Sopenharmony_ci
504bf215546Sopenharmony_ci   return true;
505bf215546Sopenharmony_ci}
506bf215546Sopenharmony_ci
507bf215546Sopenharmony_civoid
508bf215546Sopenharmony_cifd6_draw_init(struct pipe_context *pctx) disable_thread_safety_analysis
509bf215546Sopenharmony_ci{
510bf215546Sopenharmony_ci   struct fd_context *ctx = fd_context(pctx);
511bf215546Sopenharmony_ci   ctx->draw_vbo = fd6_draw_vbo;
512bf215546Sopenharmony_ci   ctx->clear = fd6_clear;
513bf215546Sopenharmony_ci}
514