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