Merge remote-tracking branch 'mattst88/nir-lower-pack-unpack' into vulkan
[mesa.git] / src / gallium / drivers / freedreno / freedreno_context.c
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 #include "freedreno_context.h"
30 #include "freedreno_draw.h"
31 #include "freedreno_fence.h"
32 #include "freedreno_program.h"
33 #include "freedreno_resource.h"
34 #include "freedreno_texture.h"
35 #include "freedreno_state.h"
36 #include "freedreno_gmem.h"
37 #include "freedreno_query.h"
38 #include "freedreno_query_hw.h"
39 #include "freedreno_util.h"
40
41 static struct fd_ringbuffer *next_rb(struct fd_context *ctx)
42 {
43 struct fd_ringbuffer *ring;
44 uint32_t ts;
45
46 /* grab next ringbuffer: */
47 ring = ctx->rings[(ctx->rings_idx++) % ARRAY_SIZE(ctx->rings)];
48
49 /* wait for new rb to be idle: */
50 ts = fd_ringbuffer_timestamp(ring);
51 if (ts) {
52 DBG("wait: %u", ts);
53 fd_pipe_wait(ctx->screen->pipe, ts);
54 }
55
56 fd_ringbuffer_reset(ring);
57
58 return ring;
59 }
60
61 static void
62 fd_context_next_rb(struct pipe_context *pctx)
63 {
64 struct fd_context *ctx = fd_context(pctx);
65 struct fd_ringbuffer *ring;
66
67 fd_ringmarker_del(ctx->draw_start);
68 fd_ringmarker_del(ctx->draw_end);
69
70 ring = next_rb(ctx);
71
72 ctx->draw_start = fd_ringmarker_new(ring);
73 ctx->draw_end = fd_ringmarker_new(ring);
74
75 fd_ringbuffer_set_parent(ring, NULL);
76 ctx->ring = ring;
77
78 fd_ringmarker_del(ctx->binning_start);
79 fd_ringmarker_del(ctx->binning_end);
80
81 ring = next_rb(ctx);
82
83 ctx->binning_start = fd_ringmarker_new(ring);
84 ctx->binning_end = fd_ringmarker_new(ring);
85
86 fd_ringbuffer_set_parent(ring, ctx->ring);
87 ctx->binning_ring = ring;
88 }
89
90 /* emit accumulated render cmds, needed for example if render target has
91 * changed, or for flush()
92 */
93 void
94 fd_context_render(struct pipe_context *pctx)
95 {
96 struct fd_context *ctx = fd_context(pctx);
97 struct fd_resource *rsc, *rsc_tmp;
98
99 DBG("needs_flush: %d", ctx->needs_flush);
100
101 if (!ctx->needs_flush)
102 return;
103
104 fd_gmem_render_tiles(ctx);
105
106 DBG("%p/%p/%p", ctx->ring->start, ctx->ring->cur, ctx->ring->end);
107
108 /* if size in dwords is more than half the buffer size, then wait and
109 * wrap around:
110 */
111 if ((ctx->ring->cur - ctx->ring->start) > ctx->ring->size/8)
112 fd_context_next_rb(pctx);
113
114 ctx->needs_flush = false;
115 ctx->cleared = ctx->partial_cleared = ctx->restore = ctx->resolve = 0;
116 ctx->gmem_reason = 0;
117 ctx->num_draws = 0;
118
119 /* go through all the used resources and clear their reading flag */
120 LIST_FOR_EACH_ENTRY_SAFE(rsc, rsc_tmp, &ctx->used_resources, list) {
121 debug_assert(rsc->status != 0);
122 rsc->status = 0;
123 rsc->pending_ctx = NULL;
124 list_delinit(&rsc->list);
125 }
126
127 assert(LIST_IS_EMPTY(&ctx->used_resources));
128 }
129
130 static void
131 fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
132 unsigned flags)
133 {
134 struct fd_ringbuffer *ring = fd_context(pctx)->ring;
135
136 fd_context_render(pctx);
137
138 if (fence) {
139 fd_screen_fence_ref(pctx->screen, fence, NULL);
140 *fence = fd_fence_create(pctx, fd_ringbuffer_timestamp(ring));
141 }
142 }
143
144 /**
145 * emit marker string as payload of a no-op packet, which can be
146 * decoded by cffdump.
147 */
148 static void
149 fd_emit_string_marker(struct pipe_context *pctx, const char *string, int len)
150 {
151 struct fd_context *ctx = fd_context(pctx);
152 struct fd_ringbuffer *ring = ctx->ring;
153 const uint32_t *buf = (const void *)string;
154
155 OUT_PKT3(ring, CP_NOP, align(len, 4) / 4);
156 while (len >= 4) {
157 OUT_RING(ring, *buf);
158 buf++;
159 len -= 4;
160 }
161
162 /* copy remainder bytes without reading past end of input string: */
163 if (len > 0) {
164 uint32_t w = 0;
165 memcpy(&w, buf, len);
166 OUT_RING(ring, w);
167 }
168 }
169
170 void
171 fd_context_destroy(struct pipe_context *pctx)
172 {
173 struct fd_context *ctx = fd_context(pctx);
174 unsigned i;
175
176 DBG("");
177
178 fd_prog_fini(pctx);
179 fd_hw_query_fini(pctx);
180
181 util_dynarray_fini(&ctx->draw_patches);
182
183 if (ctx->blitter)
184 util_blitter_destroy(ctx->blitter);
185
186 if (ctx->primconvert)
187 util_primconvert_destroy(ctx->primconvert);
188
189 util_slab_destroy(&ctx->transfer_pool);
190
191 fd_ringmarker_del(ctx->draw_start);
192 fd_ringmarker_del(ctx->draw_end);
193 fd_ringmarker_del(ctx->binning_start);
194 fd_ringmarker_del(ctx->binning_end);
195
196 for (i = 0; i < ARRAY_SIZE(ctx->rings); i++)
197 fd_ringbuffer_del(ctx->rings[i]);
198
199 for (i = 0; i < ARRAY_SIZE(ctx->pipe); i++) {
200 struct fd_vsc_pipe *pipe = &ctx->pipe[i];
201 if (!pipe->bo)
202 break;
203 fd_bo_del(pipe->bo);
204 }
205
206 fd_device_del(ctx->dev);
207
208 FREE(ctx);
209 }
210
211 struct pipe_context *
212 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
213 const uint8_t *primtypes, void *priv)
214 {
215 struct fd_screen *screen = fd_screen(pscreen);
216 struct pipe_context *pctx;
217 int i;
218
219 ctx->screen = screen;
220
221 ctx->primtypes = primtypes;
222 ctx->primtype_mask = 0;
223 for (i = 0; i < PIPE_PRIM_MAX; i++)
224 if (primtypes[i])
225 ctx->primtype_mask |= (1 << i);
226
227 /* need some sane default in case state tracker doesn't
228 * set some state:
229 */
230 ctx->sample_mask = 0xffff;
231
232 pctx = &ctx->base;
233 pctx->screen = pscreen;
234 pctx->priv = priv;
235 pctx->flush = fd_context_flush;
236 pctx->emit_string_marker = fd_emit_string_marker;
237
238 for (i = 0; i < ARRAY_SIZE(ctx->rings); i++) {
239 ctx->rings[i] = fd_ringbuffer_new(screen->pipe, 0x100000);
240 if (!ctx->rings[i])
241 goto fail;
242 }
243
244 fd_context_next_rb(pctx);
245 fd_reset_wfi(ctx);
246
247 util_dynarray_init(&ctx->draw_patches);
248
249 util_slab_create(&ctx->transfer_pool, sizeof(struct fd_transfer),
250 16, UTIL_SLAB_SINGLETHREADED);
251
252 fd_draw_init(pctx);
253 fd_resource_context_init(pctx);
254 fd_query_context_init(pctx);
255 fd_texture_init(pctx);
256 fd_state_init(pctx);
257 fd_hw_query_init(pctx);
258
259 ctx->blitter = util_blitter_create(pctx);
260 if (!ctx->blitter)
261 goto fail;
262
263 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask);
264 if (!ctx->primconvert)
265 goto fail;
266
267 return pctx;
268
269 fail:
270 pctx->destroy(pctx);
271 return NULL;
272 }