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