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