freedreno: flush immediately when reading a pending batch
[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_blitter.h"
31 #include "freedreno_draw.h"
32 #include "freedreno_fence.h"
33 #include "freedreno_program.h"
34 #include "freedreno_resource.h"
35 #include "freedreno_texture.h"
36 #include "freedreno_state.h"
37 #include "freedreno_gmem.h"
38 #include "freedreno_query.h"
39 #include "freedreno_query_hw.h"
40 #include "freedreno_util.h"
41 #include "util/u_upload_mgr.h"
42
43 static void
44 fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fencep,
45 unsigned flags)
46 {
47 struct fd_context *ctx = fd_context(pctx);
48 struct pipe_fence_handle *fence = NULL;
49
50 DBG("%p: flush: flags=%x\n", ctx->batch, flags);
51
52 /* Take a ref to the batch's fence (batch can be unref'd when flushed: */
53 fd_fence_ref(pctx->screen, &fence, ctx->batch->fence);
54
55 if (flags & PIPE_FLUSH_FENCE_FD)
56 ctx->batch->needs_out_fence_fd = true;
57
58 if (!ctx->screen->reorder) {
59 fd_batch_flush(ctx->batch, true, false);
60 } else if (flags & PIPE_FLUSH_DEFERRED) {
61 fd_bc_flush_deferred(&ctx->screen->batch_cache, ctx);
62 } else {
63 fd_bc_flush(&ctx->screen->batch_cache, ctx);
64 }
65
66 if (fencep)
67 fd_fence_ref(pctx->screen, fencep, fence);
68
69 fd_fence_ref(pctx->screen, &fence, NULL);
70 }
71
72 static void
73 fd_texture_barrier(struct pipe_context *pctx, unsigned flags)
74 {
75 /* On devices that could sample from GMEM we could possibly do better.
76 * Or if we knew that we were doing GMEM bypass we could just emit a
77 * cache flush, perhaps? But we don't know if future draws would cause
78 * us to use GMEM, and a flush in bypass isn't the end of the world.
79 */
80 fd_context_flush(pctx, NULL, 0);
81 }
82
83 static void
84 fd_memory_barrier(struct pipe_context *pctx, unsigned flags)
85 {
86 fd_context_flush(pctx, NULL, 0);
87 /* TODO do we need to check for persistently mapped buffers and fd_bo_cpu_prep()?? */
88 }
89
90 /**
91 * emit marker string as payload of a no-op packet, which can be
92 * decoded by cffdump.
93 */
94 static void
95 fd_emit_string_marker(struct pipe_context *pctx, const char *string, int len)
96 {
97 struct fd_context *ctx = fd_context(pctx);
98 struct fd_ringbuffer *ring;
99 const uint32_t *buf = (const void *)string;
100
101 if (!ctx->batch)
102 return;
103
104 ctx->batch->needs_flush = true;
105
106 ring = ctx->batch->draw;
107
108 /* max packet size is 0x3fff dwords: */
109 len = MIN2(len, 0x3fff * 4);
110
111 if (ctx->screen->gpu_id >= 500)
112 OUT_PKT7(ring, CP_NOP, align(len, 4) / 4);
113 else
114 OUT_PKT3(ring, CP_NOP, align(len, 4) / 4);
115 while (len >= 4) {
116 OUT_RING(ring, *buf);
117 buf++;
118 len -= 4;
119 }
120
121 /* copy remainder bytes without reading past end of input string: */
122 if (len > 0) {
123 uint32_t w = 0;
124 memcpy(&w, buf, len);
125 OUT_RING(ring, w);
126 }
127 }
128
129 void
130 fd_context_destroy(struct pipe_context *pctx)
131 {
132 struct fd_context *ctx = fd_context(pctx);
133 unsigned i;
134
135 DBG("");
136
137 if (ctx->screen->reorder && util_queue_is_initialized(&ctx->flush_queue))
138 util_queue_destroy(&ctx->flush_queue);
139
140 fd_batch_reference(&ctx->batch, NULL); /* unref current batch */
141 fd_bc_invalidate_context(ctx);
142
143 fd_prog_fini(pctx);
144
145 if (ctx->blitter)
146 util_blitter_destroy(ctx->blitter);
147
148 if (pctx->stream_uploader)
149 u_upload_destroy(pctx->stream_uploader);
150
151 if (ctx->clear_rs_state)
152 pctx->delete_rasterizer_state(pctx, ctx->clear_rs_state);
153
154 if (ctx->primconvert)
155 util_primconvert_destroy(ctx->primconvert);
156
157 slab_destroy_child(&ctx->transfer_pool);
158
159 for (i = 0; i < ARRAY_SIZE(ctx->vsc_pipe); i++) {
160 struct fd_vsc_pipe *pipe = &ctx->vsc_pipe[i];
161 if (!pipe->bo)
162 break;
163 fd_bo_del(pipe->bo);
164 }
165
166 fd_device_del(ctx->dev);
167 fd_pipe_del(ctx->pipe);
168
169 if (fd_mesa_debug & (FD_DBG_BSTAT | FD_DBG_MSGS)) {
170 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_nondraw=%u, batch_restore=%u\n",
171 (uint32_t)ctx->stats.batch_total, (uint32_t)ctx->stats.batch_sysmem,
172 (uint32_t)ctx->stats.batch_gmem, (uint32_t)ctx->stats.batch_nondraw,
173 (uint32_t)ctx->stats.batch_restore);
174 }
175
176 FREE(ctx);
177 }
178
179 static void
180 fd_set_debug_callback(struct pipe_context *pctx,
181 const struct pipe_debug_callback *cb)
182 {
183 struct fd_context *ctx = fd_context(pctx);
184
185 if (cb)
186 ctx->debug = *cb;
187 else
188 memset(&ctx->debug, 0, sizeof(ctx->debug));
189 }
190
191 /* TODO we could combine a few of these small buffers (solid_vbuf,
192 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
193 * save a tiny bit of memory
194 */
195
196 static struct pipe_resource *
197 create_solid_vertexbuf(struct pipe_context *pctx)
198 {
199 static const float init_shader_const[] = {
200 -1.000000, +1.000000, +1.000000,
201 +1.000000, -1.000000, +1.000000,
202 };
203 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
204 PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE, sizeof(init_shader_const));
205 pipe_buffer_write(pctx, prsc, 0,
206 sizeof(init_shader_const), init_shader_const);
207 return prsc;
208 }
209
210 static struct pipe_resource *
211 create_blit_texcoord_vertexbuf(struct pipe_context *pctx)
212 {
213 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
214 PIPE_BIND_CUSTOM, PIPE_USAGE_DYNAMIC, 16);
215 return prsc;
216 }
217
218 void
219 fd_context_setup_common_vbos(struct fd_context *ctx)
220 {
221 struct pipe_context *pctx = &ctx->base;
222
223 ctx->solid_vbuf = create_solid_vertexbuf(pctx);
224 ctx->blit_texcoord_vbuf = create_blit_texcoord_vertexbuf(pctx);
225
226 /* setup solid_vbuf_state: */
227 ctx->solid_vbuf_state.vtx = pctx->create_vertex_elements_state(
228 pctx, 1, (struct pipe_vertex_element[]){{
229 .vertex_buffer_index = 0,
230 .src_offset = 0,
231 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
232 }});
233 ctx->solid_vbuf_state.vertexbuf.count = 1;
234 ctx->solid_vbuf_state.vertexbuf.vb[0].stride = 12;
235 ctx->solid_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->solid_vbuf;
236
237 /* setup blit_vbuf_state: */
238 ctx->blit_vbuf_state.vtx = pctx->create_vertex_elements_state(
239 pctx, 2, (struct pipe_vertex_element[]){{
240 .vertex_buffer_index = 0,
241 .src_offset = 0,
242 .src_format = PIPE_FORMAT_R32G32_FLOAT,
243 }, {
244 .vertex_buffer_index = 1,
245 .src_offset = 0,
246 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
247 }});
248 ctx->blit_vbuf_state.vertexbuf.count = 2;
249 ctx->blit_vbuf_state.vertexbuf.vb[0].stride = 8;
250 ctx->blit_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->blit_texcoord_vbuf;
251 ctx->blit_vbuf_state.vertexbuf.vb[1].stride = 12;
252 ctx->blit_vbuf_state.vertexbuf.vb[1].buffer.resource = ctx->solid_vbuf;
253 }
254
255 void
256 fd_context_cleanup_common_vbos(struct fd_context *ctx)
257 {
258 struct pipe_context *pctx = &ctx->base;
259
260 pctx->delete_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
261 pctx->delete_vertex_elements_state(pctx, ctx->blit_vbuf_state.vtx);
262
263 pipe_resource_reference(&ctx->solid_vbuf, NULL);
264 pipe_resource_reference(&ctx->blit_texcoord_vbuf, NULL);
265 }
266
267 struct pipe_context *
268 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
269 const uint8_t *primtypes, void *priv, unsigned flags)
270 {
271 struct fd_screen *screen = fd_screen(pscreen);
272 struct pipe_context *pctx;
273 unsigned prio = 1;
274 int i;
275
276 /* lower numerical value == higher priority: */
277 if (fd_mesa_debug & FD_DBG_HIPRIO)
278 prio = 0;
279 else if (flags & PIPE_CONTEXT_HIGH_PRIORITY)
280 prio = 0;
281 else if (flags & PIPE_CONTEXT_LOW_PRIORITY)
282 prio = 2;
283
284 ctx->screen = screen;
285 ctx->pipe = fd_pipe_new2(screen->dev, FD_PIPE_3D, prio);
286
287 ctx->primtypes = primtypes;
288 ctx->primtype_mask = 0;
289 for (i = 0; i < PIPE_PRIM_MAX; i++)
290 if (primtypes[i])
291 ctx->primtype_mask |= (1 << i);
292
293 /* need some sane default in case state tracker doesn't
294 * set some state:
295 */
296 ctx->sample_mask = 0xffff;
297
298 pctx = &ctx->base;
299 pctx->screen = pscreen;
300 pctx->priv = priv;
301 pctx->flush = fd_context_flush;
302 pctx->emit_string_marker = fd_emit_string_marker;
303 pctx->set_debug_callback = fd_set_debug_callback;
304 pctx->create_fence_fd = fd_create_fence_fd;
305 pctx->fence_server_sync = fd_fence_server_sync;
306 pctx->texture_barrier = fd_texture_barrier;
307 pctx->memory_barrier = fd_memory_barrier;
308
309 pctx->stream_uploader = u_upload_create_default(pctx);
310 if (!pctx->stream_uploader)
311 goto fail;
312 pctx->const_uploader = pctx->stream_uploader;
313
314 ctx->batch = fd_bc_alloc_batch(&screen->batch_cache, ctx);
315
316 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
317
318 if (!ctx->blit)
319 ctx->blit = fd_blitter_blit;
320
321 fd_draw_init(pctx);
322 fd_resource_context_init(pctx);
323 fd_query_context_init(pctx);
324 fd_texture_init(pctx);
325 fd_state_init(pctx);
326
327 ctx->blitter = util_blitter_create(pctx);
328 if (!ctx->blitter)
329 goto fail;
330
331 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask);
332 if (!ctx->primconvert)
333 goto fail;
334
335 list_inithead(&ctx->hw_active_queries);
336 list_inithead(&ctx->acc_active_queries);
337
338 return pctx;
339
340 fail:
341 pctx->destroy(pctx);
342 return NULL;
343 }