6c890e7b97e16d28b85709c134be6049970c8619
[mesa.git] / src / gallium / drivers / freedreno / freedreno_draw.h
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #ifndef FREEDRENO_DRAW_H_
30 #define FREEDRENO_DRAW_H_
31
32 #include "pipe/p_state.h"
33 #include "pipe/p_context.h"
34
35 #include "freedreno_context.h"
36 #include "freedreno_resource.h"
37 #include "freedreno_screen.h"
38 #include "freedreno_util.h"
39
40 struct fd_ringbuffer;
41
42 void fd_draw_init(struct pipe_context *pctx);
43
44 static inline void
45 fd_draw(struct fd_context *ctx, struct fd_ringbuffer *ring,
46 enum pc_di_primtype primtype,
47 enum pc_di_vis_cull_mode vismode,
48 enum pc_di_src_sel src_sel, uint32_t count,
49 enum pc_di_index_size idx_type,
50 uint32_t idx_size, uint32_t idx_offset,
51 struct fd_bo *idx_bo)
52 {
53 /* for debug after a lock up, write a unique counter value
54 * to scratch7 for each draw, to make it easier to match up
55 * register dumps to cmdstream. The combination of IB
56 * (scratch6) and DRAW is enough to "triangulate" the
57 * particular draw that caused lockup.
58 */
59 emit_marker(ring, 7);
60
61 if (is_a3xx_p0(ctx->screen)) {
62 /* dummy-draw workaround: */
63 OUT_PKT3(ring, CP_DRAW_INDX, 3);
64 OUT_RING(ring, 0x00000000);
65 OUT_RING(ring, DRAW(1, DI_SRC_SEL_AUTO_INDEX,
66 INDEX_SIZE_IGN, USE_VISIBILITY));
67 OUT_RING(ring, 0); /* NumIndices */
68
69 /* ugg, hard-code register offset to avoid pulling in the
70 * a3xx register headers into something #included from a2xx
71 */
72 OUT_PKT0(ring, 0x2206, 1); /* A3XX_HLSQ_CONST_VSPRESV_RANGE_REG */
73 OUT_RING(ring, 0);
74 }
75
76 OUT_PKT3(ring, CP_DRAW_INDX, idx_bo ? 5 : 3);
77 OUT_RING(ring, 0x00000000); /* viz query info. */
78 if (vismode == USE_VISIBILITY) {
79 /* leave vis mode blank for now, it will be patched up when
80 * we know if we are binning or not
81 */
82 OUT_RINGP(ring, DRAW(primtype, src_sel, idx_type, 0),
83 &ctx->draw_patches);
84 } else {
85 OUT_RING(ring, DRAW(primtype, src_sel, idx_type, vismode));
86 }
87 OUT_RING(ring, count); /* NumIndices */
88 if (idx_bo) {
89 OUT_RELOC(ring, idx_bo, idx_offset, 0, 0);
90 OUT_RING (ring, idx_size);
91 }
92
93 emit_marker(ring, 7);
94
95 fd_reset_wfi(ctx);
96 }
97
98
99 static inline enum pc_di_index_size
100 size2indextype(unsigned index_size)
101 {
102 switch (index_size) {
103 case 1: return INDEX_SIZE_8_BIT;
104 case 2: return INDEX_SIZE_16_BIT;
105 case 4: return INDEX_SIZE_32_BIT;
106 }
107 DBG("unsupported index size: %d", index_size);
108 assert(0);
109 return INDEX_SIZE_IGN;
110 }
111
112 /* this is same for a2xx/a3xx, so split into helper: */
113 static inline void
114 fd_draw_emit(struct fd_context *ctx, struct fd_ringbuffer *ring,
115 enum pc_di_vis_cull_mode vismode,
116 const struct pipe_draw_info *info)
117 {
118 struct pipe_index_buffer *idx = &ctx->indexbuf;
119 struct fd_bo *idx_bo = NULL;
120 enum pc_di_index_size idx_type = INDEX_SIZE_IGN;
121 enum pc_di_src_sel src_sel;
122 uint32_t idx_size, idx_offset;
123
124 if (info->indexed) {
125 assert(!idx->user_buffer);
126
127 idx_bo = fd_resource(idx->buffer)->bo;
128 idx_type = size2indextype(idx->index_size);
129 idx_size = idx->index_size * info->count;
130 idx_offset = idx->offset + (info->start * idx->index_size);
131 src_sel = DI_SRC_SEL_DMA;
132 } else {
133 idx_bo = NULL;
134 idx_type = INDEX_SIZE_IGN;
135 idx_size = 0;
136 idx_offset = 0;
137 src_sel = DI_SRC_SEL_AUTO_INDEX;
138 }
139
140 fd_draw(ctx, ring, ctx->primtypes[info->mode], vismode, src_sel,
141 info->count, idx_type, idx_size, idx_offset, idx_bo);
142 }
143
144 #endif /* FREEDRENO_DRAW_H_ */