1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#ifndef FREEDRENO_DRAW_H_
28bf215546Sopenharmony_ci#define FREEDRENO_DRAW_H_
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "pipe/p_context.h"
31bf215546Sopenharmony_ci#include "pipe/p_state.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#include "freedreno_context.h"
34bf215546Sopenharmony_ci#include "freedreno_resource.h"
35bf215546Sopenharmony_ci#include "freedreno_screen.h"
36bf215546Sopenharmony_ci#include "freedreno_util.h"
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistruct fd_ringbuffer;
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_civoid fd_draw_init(struct pipe_context *pctx);
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_cistatic inline void
43bf215546Sopenharmony_cifd_draw(struct fd_batch *batch, struct fd_ringbuffer *ring,
44bf215546Sopenharmony_ci        enum pc_di_primtype primtype, enum pc_di_vis_cull_mode vismode,
45bf215546Sopenharmony_ci        enum pc_di_src_sel src_sel, uint32_t count, uint8_t instances,
46bf215546Sopenharmony_ci        enum pc_di_index_size idx_type, uint32_t idx_size, uint32_t idx_offset,
47bf215546Sopenharmony_ci        struct pipe_resource *idx_buffer)
48bf215546Sopenharmony_ci{
49bf215546Sopenharmony_ci   /* for debug after a lock up, write a unique counter value
50bf215546Sopenharmony_ci    * to scratch7 for each draw, to make it easier to match up
51bf215546Sopenharmony_ci    * register dumps to cmdstream.  The combination of IB
52bf215546Sopenharmony_ci    * (scratch6) and DRAW is enough to "triangulate" the
53bf215546Sopenharmony_ci    * particular draw that caused lockup.
54bf215546Sopenharmony_ci    */
55bf215546Sopenharmony_ci   emit_marker(ring, 7);
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   if (is_a3xx_p0(batch->ctx->screen)) {
58bf215546Sopenharmony_ci      /* dummy-draw workaround: */
59bf215546Sopenharmony_ci      OUT_PKT3(ring, CP_DRAW_INDX, 3);
60bf215546Sopenharmony_ci      OUT_RING(ring, 0x00000000);
61bf215546Sopenharmony_ci      OUT_RING(ring, DRAW(1, DI_SRC_SEL_AUTO_INDEX, INDEX_SIZE_IGN,
62bf215546Sopenharmony_ci                          USE_VISIBILITY, 0));
63bf215546Sopenharmony_ci      OUT_RING(ring, 0); /* NumIndices */
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci      /* ugg, hard-code register offset to avoid pulling in the
66bf215546Sopenharmony_ci       * a3xx register headers into something #included from a2xx
67bf215546Sopenharmony_ci       */
68bf215546Sopenharmony_ci      OUT_PKT0(ring, 0x2206, 1); /* A3XX_HLSQ_CONST_VSPRESV_RANGE_REG */
69bf215546Sopenharmony_ci      OUT_RING(ring, 0);
70bf215546Sopenharmony_ci   }
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   if (is_a20x(batch->ctx->screen)) {
73bf215546Sopenharmony_ci      /* a20x has a different draw command for drawing with binning data
74bf215546Sopenharmony_ci       * note: if we do patching we will have to insert a NOP
75bf215546Sopenharmony_ci       *
76bf215546Sopenharmony_ci       * binning data is is 1 byte/vertex (8x8x4 bin position of vertex)
77bf215546Sopenharmony_ci       * base ptr set by the CP_SET_DRAW_INIT_FLAGS command
78bf215546Sopenharmony_ci       *
79bf215546Sopenharmony_ci       * TODO: investigate the faceness_cull_select parameter to see how
80bf215546Sopenharmony_ci       * it is used with hw binning to use "faceness" bits
81bf215546Sopenharmony_ci       */
82bf215546Sopenharmony_ci      uint32_t size = 2;
83bf215546Sopenharmony_ci      if (vismode)
84bf215546Sopenharmony_ci         size += 2;
85bf215546Sopenharmony_ci      if (idx_buffer)
86bf215546Sopenharmony_ci         size += 2;
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci      BEGIN_RING(ring, size + 1);
89bf215546Sopenharmony_ci      if (vismode)
90bf215546Sopenharmony_ci         util_dynarray_append(&batch->draw_patches, uint32_t *, ring->cur);
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci      OUT_PKT3(ring, vismode ? CP_DRAW_INDX_BIN : CP_DRAW_INDX, size);
93bf215546Sopenharmony_ci      OUT_RING(ring, 0x00000000);
94bf215546Sopenharmony_ci      OUT_RING(ring, DRAW_A20X(primtype, DI_FACE_CULL_NONE, src_sel, idx_type,
95bf215546Sopenharmony_ci                               vismode, vismode, count));
96bf215546Sopenharmony_ci      if (vismode == USE_VISIBILITY) {
97bf215546Sopenharmony_ci         OUT_RING(ring, batch->num_vertices);
98bf215546Sopenharmony_ci         OUT_RING(ring, count);
99bf215546Sopenharmony_ci      }
100bf215546Sopenharmony_ci   } else {
101bf215546Sopenharmony_ci      OUT_PKT3(ring, CP_DRAW_INDX, idx_buffer ? 5 : 3);
102bf215546Sopenharmony_ci      OUT_RING(ring, 0x00000000); /* viz query info. */
103bf215546Sopenharmony_ci      if (vismode == USE_VISIBILITY) {
104bf215546Sopenharmony_ci         /* leave vis mode blank for now, it will be patched up when
105bf215546Sopenharmony_ci          * we know if we are binning or not
106bf215546Sopenharmony_ci          */
107bf215546Sopenharmony_ci         OUT_RINGP(ring, DRAW(primtype, src_sel, idx_type, 0, instances),
108bf215546Sopenharmony_ci                   &batch->draw_patches);
109bf215546Sopenharmony_ci      } else {
110bf215546Sopenharmony_ci         OUT_RING(ring, DRAW(primtype, src_sel, idx_type, vismode, instances));
111bf215546Sopenharmony_ci      }
112bf215546Sopenharmony_ci      OUT_RING(ring, count); /* NumIndices */
113bf215546Sopenharmony_ci   }
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci   if (idx_buffer) {
116bf215546Sopenharmony_ci      OUT_RELOC(ring, fd_resource(idx_buffer)->bo, idx_offset, 0, 0);
117bf215546Sopenharmony_ci      OUT_RING(ring, idx_size);
118bf215546Sopenharmony_ci   }
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   emit_marker(ring, 7);
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci   fd_reset_wfi(batch);
123bf215546Sopenharmony_ci}
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_cistatic inline enum pc_di_index_size
126bf215546Sopenharmony_cisize2indextype(unsigned index_size)
127bf215546Sopenharmony_ci{
128bf215546Sopenharmony_ci   switch (index_size) {
129bf215546Sopenharmony_ci   case 1:
130bf215546Sopenharmony_ci      return INDEX_SIZE_8_BIT;
131bf215546Sopenharmony_ci   case 2:
132bf215546Sopenharmony_ci      return INDEX_SIZE_16_BIT;
133bf215546Sopenharmony_ci   case 4:
134bf215546Sopenharmony_ci      return INDEX_SIZE_32_BIT;
135bf215546Sopenharmony_ci   }
136bf215546Sopenharmony_ci   DBG("unsupported index size: %d", index_size);
137bf215546Sopenharmony_ci   assert(0);
138bf215546Sopenharmony_ci   return INDEX_SIZE_IGN;
139bf215546Sopenharmony_ci}
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci/* this is same for a2xx/a3xx, so split into helper: */
142bf215546Sopenharmony_cistatic inline void
143bf215546Sopenharmony_cifd_draw_emit(struct fd_batch *batch, struct fd_ringbuffer *ring,
144bf215546Sopenharmony_ci             enum pc_di_primtype primtype, enum pc_di_vis_cull_mode vismode,
145bf215546Sopenharmony_ci             const struct pipe_draw_info *info,
146bf215546Sopenharmony_ci             const struct pipe_draw_start_count_bias *draw, unsigned index_offset)
147bf215546Sopenharmony_ci{
148bf215546Sopenharmony_ci   struct pipe_resource *idx_buffer = NULL;
149bf215546Sopenharmony_ci   enum pc_di_index_size idx_type = INDEX_SIZE_IGN;
150bf215546Sopenharmony_ci   enum pc_di_src_sel src_sel;
151bf215546Sopenharmony_ci   uint32_t idx_size, idx_offset;
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci   if (info->index_size) {
154bf215546Sopenharmony_ci      assert(!info->has_user_indices);
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci      idx_buffer = info->index.resource;
157bf215546Sopenharmony_ci      idx_type = size2indextype(info->index_size);
158bf215546Sopenharmony_ci      idx_size = info->index_size * draw->count;
159bf215546Sopenharmony_ci      idx_offset = index_offset + draw->start * info->index_size;
160bf215546Sopenharmony_ci      src_sel = DI_SRC_SEL_DMA;
161bf215546Sopenharmony_ci   } else {
162bf215546Sopenharmony_ci      idx_buffer = NULL;
163bf215546Sopenharmony_ci      idx_type = INDEX_SIZE_IGN;
164bf215546Sopenharmony_ci      idx_size = 0;
165bf215546Sopenharmony_ci      idx_offset = 0;
166bf215546Sopenharmony_ci      src_sel = DI_SRC_SEL_AUTO_INDEX;
167bf215546Sopenharmony_ci   }
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci   fd_draw(batch, ring, primtype, vismode, src_sel, draw->count,
170bf215546Sopenharmony_ci           info->instance_count - 1, idx_type, idx_size, idx_offset,
171bf215546Sopenharmony_ci           idx_buffer);
172bf215546Sopenharmony_ci}
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci#endif /* FREEDRENO_DRAW_H_ */
175