1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2014 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 FD4_DRAW_H_
28bf215546Sopenharmony_ci#define FD4_DRAW_H_
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "pipe/p_context.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "freedreno_draw.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_civoid fd4_draw_init(struct pipe_context *pctx);
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci/* draw packet changed on a4xx, so cannot reuse one from a2xx/a3xx.. */
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistatic inline uint32_t
39bf215546Sopenharmony_ciDRAW4(enum pc_di_primtype prim_type, enum pc_di_src_sel source_select,
40bf215546Sopenharmony_ci      enum a4xx_index_size index_size, enum pc_di_vis_cull_mode vis_cull_mode)
41bf215546Sopenharmony_ci{
42bf215546Sopenharmony_ci   return CP_DRAW_INDX_OFFSET_0_PRIM_TYPE(prim_type) |
43bf215546Sopenharmony_ci          CP_DRAW_INDX_OFFSET_0_SOURCE_SELECT(source_select) |
44bf215546Sopenharmony_ci          CP_DRAW_INDX_OFFSET_0_INDEX_SIZE(index_size) |
45bf215546Sopenharmony_ci          CP_DRAW_INDX_OFFSET_0_VIS_CULL(vis_cull_mode);
46bf215546Sopenharmony_ci}
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_cistatic inline void
49bf215546Sopenharmony_cifd4_draw(struct fd_batch *batch, struct fd_ringbuffer *ring,
50bf215546Sopenharmony_ci         enum pc_di_primtype primtype, enum pc_di_vis_cull_mode vismode,
51bf215546Sopenharmony_ci         enum pc_di_src_sel src_sel, uint32_t count, uint32_t instances,
52bf215546Sopenharmony_ci         enum a4xx_index_size idx_type, uint32_t max_indices,
53bf215546Sopenharmony_ci         uint32_t idx_offset, struct pipe_resource *idx_buffer)
54bf215546Sopenharmony_ci{
55bf215546Sopenharmony_ci   /* for debug after a lock up, write a unique counter value
56bf215546Sopenharmony_ci    * to scratch7 for each draw, to make it easier to match up
57bf215546Sopenharmony_ci    * register dumps to cmdstream.  The combination of IB
58bf215546Sopenharmony_ci    * (scratch6) and DRAW is enough to "triangulate" the
59bf215546Sopenharmony_ci    * particular draw that caused lockup.
60bf215546Sopenharmony_ci    */
61bf215546Sopenharmony_ci   emit_marker(ring, 7);
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci   OUT_PKT3(ring, CP_DRAW_INDX_OFFSET, idx_buffer ? 6 : 3);
64bf215546Sopenharmony_ci   if (vismode == USE_VISIBILITY) {
65bf215546Sopenharmony_ci      /* leave vis mode blank for now, it will be patched up when
66bf215546Sopenharmony_ci       * we know if we are binning or not
67bf215546Sopenharmony_ci       */
68bf215546Sopenharmony_ci      OUT_RINGP(ring, DRAW4(primtype, src_sel, idx_type, 0),
69bf215546Sopenharmony_ci                &batch->draw_patches);
70bf215546Sopenharmony_ci   } else {
71bf215546Sopenharmony_ci      OUT_RING(ring, DRAW4(primtype, src_sel, idx_type, vismode));
72bf215546Sopenharmony_ci   }
73bf215546Sopenharmony_ci   OUT_RING(ring, instances); /* NumInstances */
74bf215546Sopenharmony_ci   OUT_RING(ring, count);     /* NumIndices */
75bf215546Sopenharmony_ci   if (idx_buffer) {
76bf215546Sopenharmony_ci      OUT_RING(ring, 0x0); /* XXX */
77bf215546Sopenharmony_ci      OUT_RELOC(ring, fd_resource(idx_buffer)->bo, idx_offset, 0, 0);
78bf215546Sopenharmony_ci      OUT_RING(ring, max_indices);
79bf215546Sopenharmony_ci   }
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   emit_marker(ring, 7);
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   fd_reset_wfi(batch);
84bf215546Sopenharmony_ci}
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_cistatic inline void
87bf215546Sopenharmony_cifd4_draw_emit(struct fd_batch *batch, struct fd_ringbuffer *ring,
88bf215546Sopenharmony_ci              enum pc_di_primtype primtype, enum pc_di_vis_cull_mode vismode,
89bf215546Sopenharmony_ci              const struct pipe_draw_info *info,
90bf215546Sopenharmony_ci              const struct pipe_draw_indirect_info *indirect,
91bf215546Sopenharmony_ci              const struct pipe_draw_start_count_bias *draw, unsigned index_offset)
92bf215546Sopenharmony_ci{
93bf215546Sopenharmony_ci   struct pipe_resource *idx_buffer = NULL;
94bf215546Sopenharmony_ci   enum a4xx_index_size idx_type;
95bf215546Sopenharmony_ci   enum pc_di_src_sel src_sel;
96bf215546Sopenharmony_ci   uint32_t idx_size, idx_offset;
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci   if (indirect && indirect->buffer) {
99bf215546Sopenharmony_ci      struct fd_resource *ind = fd_resource(indirect->buffer);
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci      emit_marker(ring, 7);
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci      if (info->index_size) {
104bf215546Sopenharmony_ci         struct pipe_resource *idx = info->index.resource;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci         OUT_PKT3(ring, CP_DRAW_INDX_INDIRECT, 4);
107bf215546Sopenharmony_ci         OUT_RINGP(ring,
108bf215546Sopenharmony_ci                   DRAW4(primtype, DI_SRC_SEL_DMA,
109bf215546Sopenharmony_ci                         fd4_size2indextype(info->index_size), 0),
110bf215546Sopenharmony_ci                   &batch->draw_patches);
111bf215546Sopenharmony_ci         OUT_RELOC(ring, fd_resource(idx)->bo, index_offset, 0, 0);
112bf215546Sopenharmony_ci         OUT_RING(ring, A4XX_CP_DRAW_INDX_INDIRECT_2_INDX_SIZE(idx->width0 -
113bf215546Sopenharmony_ci                                                               index_offset));
114bf215546Sopenharmony_ci         OUT_RELOC(ring, ind->bo, indirect->offset, 0, 0);
115bf215546Sopenharmony_ci      } else {
116bf215546Sopenharmony_ci         OUT_PKT3(ring, CP_DRAW_INDIRECT, 2);
117bf215546Sopenharmony_ci         OUT_RINGP(ring, DRAW4(primtype, DI_SRC_SEL_AUTO_INDEX, 0, 0),
118bf215546Sopenharmony_ci                   &batch->draw_patches);
119bf215546Sopenharmony_ci         OUT_RELOC(ring, ind->bo, indirect->offset, 0, 0);
120bf215546Sopenharmony_ci      }
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci      emit_marker(ring, 7);
123bf215546Sopenharmony_ci      fd_reset_wfi(batch);
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci      return;
126bf215546Sopenharmony_ci   }
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   if (info->index_size) {
129bf215546Sopenharmony_ci      assert(!info->has_user_indices);
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci      idx_buffer = info->index.resource;
132bf215546Sopenharmony_ci      idx_type = fd4_size2indextype(info->index_size);
133bf215546Sopenharmony_ci      idx_size = info->index_size * draw->count;
134bf215546Sopenharmony_ci      idx_offset = index_offset + draw->start * info->index_size;
135bf215546Sopenharmony_ci      src_sel = DI_SRC_SEL_DMA;
136bf215546Sopenharmony_ci   } else {
137bf215546Sopenharmony_ci      idx_buffer = NULL;
138bf215546Sopenharmony_ci      idx_type = INDEX4_SIZE_32_BIT;
139bf215546Sopenharmony_ci      idx_size = 0;
140bf215546Sopenharmony_ci      idx_offset = 0;
141bf215546Sopenharmony_ci      src_sel = DI_SRC_SEL_AUTO_INDEX;
142bf215546Sopenharmony_ci   }
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci   fd4_draw(batch, ring, primtype, vismode, src_sel, draw->count,
145bf215546Sopenharmony_ci            info->instance_count, idx_type, idx_size, idx_offset, idx_buffer);
146bf215546Sopenharmony_ci}
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci#endif /* FD4_DRAW_H_ */
149