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