freedreno: reduce resource dependency tracking overhead
[mesa.git] / src / gallium / drivers / freedreno / freedreno_context.c
1 /*
2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "freedreno_context.h"
28 #include "freedreno_blitter.h"
29 #include "freedreno_draw.h"
30 #include "freedreno_fence.h"
31 #include "freedreno_program.h"
32 #include "freedreno_resource.h"
33 #include "freedreno_texture.h"
34 #include "freedreno_state.h"
35 #include "freedreno_gmem.h"
36 #include "freedreno_query.h"
37 #include "freedreno_query_hw.h"
38 #include "freedreno_util.h"
39 #include "util/u_upload_mgr.h"
40
41 static void
42 fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fencep,
43 unsigned flags)
44 {
45 struct fd_context *ctx = fd_context(pctx);
46 struct pipe_fence_handle *fence = NULL;
47 // TODO we want to lookup batch if it exists, but not create one if not.
48 struct fd_batch *batch = fd_context_batch(ctx);
49
50 DBG("%p: flush: flags=%x\n", ctx->batch, flags);
51
52 if (!batch)
53 return;
54
55 /* Take a ref to the batch's fence (batch can be unref'd when flushed: */
56 fd_fence_ref(pctx->screen, &fence, batch->fence);
57
58 if (flags & PIPE_FLUSH_FENCE_FD)
59 batch->needs_out_fence_fd = true;
60
61 if (!ctx->screen->reorder) {
62 fd_batch_flush(batch, true, false);
63 } else if (flags & PIPE_FLUSH_DEFERRED) {
64 fd_bc_flush_deferred(&ctx->screen->batch_cache, ctx);
65 } else {
66 fd_bc_flush(&ctx->screen->batch_cache, ctx);
67 }
68
69 if (fencep)
70 fd_fence_ref(pctx->screen, fencep, fence);
71
72 fd_fence_ref(pctx->screen, &fence, NULL);
73 }
74
75 static void
76 fd_texture_barrier(struct pipe_context *pctx, unsigned flags)
77 {
78 /* On devices that could sample from GMEM we could possibly do better.
79 * Or if we knew that we were doing GMEM bypass we could just emit a
80 * cache flush, perhaps? But we don't know if future draws would cause
81 * us to use GMEM, and a flush in bypass isn't the end of the world.
82 */
83 fd_context_flush(pctx, NULL, 0);
84 }
85
86 static void
87 fd_memory_barrier(struct pipe_context *pctx, unsigned flags)
88 {
89 fd_context_flush(pctx, NULL, 0);
90 /* TODO do we need to check for persistently mapped buffers and fd_bo_cpu_prep()?? */
91 }
92
93 /**
94 * emit marker string as payload of a no-op packet, which can be
95 * decoded by cffdump.
96 */
97 static void
98 fd_emit_string_marker(struct pipe_context *pctx, const char *string, int len)
99 {
100 struct fd_context *ctx = fd_context(pctx);
101 struct fd_ringbuffer *ring;
102 const uint32_t *buf = (const void *)string;
103
104 if (!ctx->batch)
105 return;
106
107 ctx->batch->needs_flush = true;
108
109 ring = ctx->batch->draw;
110
111 /* max packet size is 0x3fff dwords: */
112 len = MIN2(len, 0x3fff * 4);
113
114 if (ctx->screen->gpu_id >= 500)
115 OUT_PKT7(ring, CP_NOP, align(len, 4) / 4);
116 else
117 OUT_PKT3(ring, CP_NOP, align(len, 4) / 4);
118 while (len >= 4) {
119 OUT_RING(ring, *buf);
120 buf++;
121 len -= 4;
122 }
123
124 /* copy remainder bytes without reading past end of input string: */
125 if (len > 0) {
126 uint32_t w = 0;
127 memcpy(&w, buf, len);
128 OUT_RING(ring, w);
129 }
130 }
131
132 void
133 fd_context_destroy(struct pipe_context *pctx)
134 {
135 struct fd_context *ctx = fd_context(pctx);
136 unsigned i;
137
138 DBG("");
139
140 if (ctx->screen->reorder && util_queue_is_initialized(&ctx->flush_queue))
141 util_queue_destroy(&ctx->flush_queue);
142
143 util_copy_framebuffer_state(&ctx->framebuffer, NULL);
144 fd_batch_reference(&ctx->batch, NULL); /* unref current batch */
145 fd_bc_invalidate_context(ctx);
146
147 fd_prog_fini(pctx);
148
149 if (ctx->blitter)
150 util_blitter_destroy(ctx->blitter);
151
152 if (pctx->stream_uploader)
153 u_upload_destroy(pctx->stream_uploader);
154
155 if (ctx->clear_rs_state)
156 pctx->delete_rasterizer_state(pctx, ctx->clear_rs_state);
157
158 if (ctx->primconvert)
159 util_primconvert_destroy(ctx->primconvert);
160
161 slab_destroy_child(&ctx->transfer_pool);
162
163 for (i = 0; i < ARRAY_SIZE(ctx->vsc_pipe); i++) {
164 struct fd_vsc_pipe *pipe = &ctx->vsc_pipe[i];
165 if (!pipe->bo)
166 break;
167 fd_bo_del(pipe->bo);
168 }
169
170 fd_device_del(ctx->dev);
171 fd_pipe_del(ctx->pipe);
172
173 if (fd_mesa_debug & (FD_DBG_BSTAT | FD_DBG_MSGS)) {
174 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_nondraw=%u, batch_restore=%u\n",
175 (uint32_t)ctx->stats.batch_total, (uint32_t)ctx->stats.batch_sysmem,
176 (uint32_t)ctx->stats.batch_gmem, (uint32_t)ctx->stats.batch_nondraw,
177 (uint32_t)ctx->stats.batch_restore);
178 }
179 }
180
181 static void
182 fd_set_debug_callback(struct pipe_context *pctx,
183 const struct pipe_debug_callback *cb)
184 {
185 struct fd_context *ctx = fd_context(pctx);
186
187 if (cb)
188 ctx->debug = *cb;
189 else
190 memset(&ctx->debug, 0, sizeof(ctx->debug));
191 }
192
193 /* TODO we could combine a few of these small buffers (solid_vbuf,
194 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
195 * save a tiny bit of memory
196 */
197
198 static struct pipe_resource *
199 create_solid_vertexbuf(struct pipe_context *pctx)
200 {
201 static const float init_shader_const[] = {
202 -1.000000, +1.000000, +1.000000,
203 +1.000000, -1.000000, +1.000000,
204 };
205 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
206 PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE, sizeof(init_shader_const));
207 pipe_buffer_write(pctx, prsc, 0,
208 sizeof(init_shader_const), init_shader_const);
209 return prsc;
210 }
211
212 static struct pipe_resource *
213 create_blit_texcoord_vertexbuf(struct pipe_context *pctx)
214 {
215 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
216 PIPE_BIND_CUSTOM, PIPE_USAGE_DYNAMIC, 16);
217 return prsc;
218 }
219
220 void
221 fd_context_setup_common_vbos(struct fd_context *ctx)
222 {
223 struct pipe_context *pctx = &ctx->base;
224
225 ctx->solid_vbuf = create_solid_vertexbuf(pctx);
226 ctx->blit_texcoord_vbuf = create_blit_texcoord_vertexbuf(pctx);
227
228 /* setup solid_vbuf_state: */
229 ctx->solid_vbuf_state.vtx = pctx->create_vertex_elements_state(
230 pctx, 1, (struct pipe_vertex_element[]){{
231 .vertex_buffer_index = 0,
232 .src_offset = 0,
233 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
234 }});
235 ctx->solid_vbuf_state.vertexbuf.count = 1;
236 ctx->solid_vbuf_state.vertexbuf.vb[0].stride = 12;
237 ctx->solid_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->solid_vbuf;
238
239 /* setup blit_vbuf_state: */
240 ctx->blit_vbuf_state.vtx = pctx->create_vertex_elements_state(
241 pctx, 2, (struct pipe_vertex_element[]){{
242 .vertex_buffer_index = 0,
243 .src_offset = 0,
244 .src_format = PIPE_FORMAT_R32G32_FLOAT,
245 }, {
246 .vertex_buffer_index = 1,
247 .src_offset = 0,
248 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
249 }});
250 ctx->blit_vbuf_state.vertexbuf.count = 2;
251 ctx->blit_vbuf_state.vertexbuf.vb[0].stride = 8;
252 ctx->blit_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->blit_texcoord_vbuf;
253 ctx->blit_vbuf_state.vertexbuf.vb[1].stride = 12;
254 ctx->blit_vbuf_state.vertexbuf.vb[1].buffer.resource = ctx->solid_vbuf;
255 }
256
257 void
258 fd_context_cleanup_common_vbos(struct fd_context *ctx)
259 {
260 struct pipe_context *pctx = &ctx->base;
261
262 pctx->delete_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
263 pctx->delete_vertex_elements_state(pctx, ctx->blit_vbuf_state.vtx);
264
265 pipe_resource_reference(&ctx->solid_vbuf, NULL);
266 pipe_resource_reference(&ctx->blit_texcoord_vbuf, NULL);
267 }
268
269 struct pipe_context *
270 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
271 const uint8_t *primtypes, void *priv, unsigned flags)
272 {
273 struct fd_screen *screen = fd_screen(pscreen);
274 struct pipe_context *pctx;
275 unsigned prio = 1;
276 int i;
277
278 /* lower numerical value == higher priority: */
279 if (fd_mesa_debug & FD_DBG_HIPRIO)
280 prio = 0;
281 else if (flags & PIPE_CONTEXT_HIGH_PRIORITY)
282 prio = 0;
283 else if (flags & PIPE_CONTEXT_LOW_PRIORITY)
284 prio = 2;
285
286 ctx->screen = screen;
287 ctx->pipe = fd_pipe_new2(screen->dev, FD_PIPE_3D, prio);
288
289 ctx->primtypes = primtypes;
290 ctx->primtype_mask = 0;
291 for (i = 0; i < PIPE_PRIM_MAX; i++)
292 if (primtypes[i])
293 ctx->primtype_mask |= (1 << i);
294
295 /* need some sane default in case state tracker doesn't
296 * set some state:
297 */
298 ctx->sample_mask = 0xffff;
299
300 pctx = &ctx->base;
301 pctx->screen = pscreen;
302 pctx->priv = priv;
303 pctx->flush = fd_context_flush;
304 pctx->emit_string_marker = fd_emit_string_marker;
305 pctx->set_debug_callback = fd_set_debug_callback;
306 pctx->create_fence_fd = fd_create_fence_fd;
307 pctx->fence_server_sync = fd_fence_server_sync;
308 pctx->texture_barrier = fd_texture_barrier;
309 pctx->memory_barrier = fd_memory_barrier;
310
311 pctx->stream_uploader = u_upload_create_default(pctx);
312 if (!pctx->stream_uploader)
313 goto fail;
314 pctx->const_uploader = pctx->stream_uploader;
315
316 if (!ctx->screen->reorder)
317 ctx->batch = fd_bc_alloc_batch(&screen->batch_cache, ctx, false);
318
319 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
320
321 if (!ctx->blit)
322 ctx->blit = fd_blitter_blit;
323
324 fd_draw_init(pctx);
325 fd_resource_context_init(pctx);
326 fd_query_context_init(pctx);
327 fd_texture_init(pctx);
328 fd_state_init(pctx);
329
330 ctx->blitter = util_blitter_create(pctx);
331 if (!ctx->blitter)
332 goto fail;
333
334 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask);
335 if (!ctx->primconvert)
336 goto fail;
337
338 list_inithead(&ctx->hw_active_queries);
339 list_inithead(&ctx->acc_active_queries);
340
341 return pctx;
342
343 fail:
344 pctx->destroy(pctx);
345 return NULL;
346 }