freedreno/log: avoid duplicate ts's
[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_log.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 // TODO we want to lookup batch if it exists, but not create one if not.
49 struct fd_batch *batch = fd_context_batch(ctx);
50
51 DBG("%p: flush: flags=%x\n", ctx->batch, flags);
52
53 /* In some sequence of events, we can end up with a last_fence that is
54 * not an "fd" fence, which results in eglDupNativeFenceFDANDROID()
55 * errors.
56 *
57 */
58 if (flags & PIPE_FLUSH_FENCE_FD)
59 fd_fence_ref(&ctx->last_fence, NULL);
60
61 /* if no rendering since last flush, ie. app just decided it needed
62 * a fence, re-use the last one:
63 */
64 if (ctx->last_fence) {
65 fd_fence_ref(&fence, ctx->last_fence);
66 goto out;
67 }
68
69 if (!batch)
70 return;
71
72 /* Take a ref to the batch's fence (batch can be unref'd when flushed: */
73 fd_fence_ref(&fence, batch->fence);
74
75 if (flags & PIPE_FLUSH_FENCE_FD)
76 batch->needs_out_fence_fd = true;
77
78 if (!ctx->screen->reorder) {
79 fd_batch_flush(batch);
80 } else if (flags & PIPE_FLUSH_DEFERRED) {
81 fd_bc_flush_deferred(&ctx->screen->batch_cache, ctx);
82 } else {
83 fd_bc_flush(&ctx->screen->batch_cache, ctx);
84 }
85
86 out:
87 if (fencep)
88 fd_fence_ref(fencep, fence);
89
90 fd_fence_ref(&ctx->last_fence, fence);
91
92 fd_fence_ref(&fence, NULL);
93
94 if (flags & PIPE_FLUSH_END_OF_FRAME)
95 fd_log_eof(ctx);
96 }
97
98 static void
99 fd_texture_barrier(struct pipe_context *pctx, unsigned flags)
100 {
101 if (flags == PIPE_TEXTURE_BARRIER_FRAMEBUFFER) {
102 struct fd_context *ctx = fd_context(pctx);
103
104 if (ctx->framebuffer_barrier) {
105 ctx->framebuffer_barrier(ctx);
106 return;
107 }
108 }
109
110 /* On devices that could sample from GMEM we could possibly do better.
111 * Or if we knew that we were doing GMEM bypass we could just emit a
112 * cache flush, perhaps? But we don't know if future draws would cause
113 * us to use GMEM, and a flush in bypass isn't the end of the world.
114 */
115 fd_context_flush(pctx, NULL, 0);
116 }
117
118 static void
119 fd_memory_barrier(struct pipe_context *pctx, unsigned flags)
120 {
121 if (!(flags & ~PIPE_BARRIER_UPDATE))
122 return;
123
124 fd_context_flush(pctx, NULL, 0);
125 /* TODO do we need to check for persistently mapped buffers and fd_bo_cpu_prep()?? */
126 }
127
128 /**
129 * emit marker string as payload of a no-op packet, which can be
130 * decoded by cffdump.
131 */
132 static void
133 fd_emit_string_marker(struct pipe_context *pctx, const char *string, int len)
134 {
135 struct fd_context *ctx = fd_context(pctx);
136 struct fd_ringbuffer *ring;
137 const uint32_t *buf = (const void *)string;
138
139 if (!ctx->batch)
140 return;
141
142 ctx->batch->needs_flush = true;
143
144 ring = ctx->batch->draw;
145
146 /* max packet size is 0x3fff dwords: */
147 len = MIN2(len, 0x3fff * 4);
148
149 if (ctx->screen->gpu_id >= 500)
150 OUT_PKT7(ring, CP_NOP, align(len, 4) / 4);
151 else
152 OUT_PKT3(ring, CP_NOP, align(len, 4) / 4);
153 while (len >= 4) {
154 OUT_RING(ring, *buf);
155 buf++;
156 len -= 4;
157 }
158
159 /* copy remainder bytes without reading past end of input string: */
160 if (len > 0) {
161 uint32_t w = 0;
162 memcpy(&w, buf, len);
163 OUT_RING(ring, w);
164 }
165 }
166
167 void
168 fd_context_destroy(struct pipe_context *pctx)
169 {
170 struct fd_context *ctx = fd_context(pctx);
171 unsigned i;
172
173 DBG("");
174
175 fd_log_process(ctx, true);
176 assert(list_is_empty(&ctx->log_chunks));
177
178 fd_fence_ref(&ctx->last_fence, NULL);
179
180 util_copy_framebuffer_state(&ctx->framebuffer, NULL);
181 fd_batch_reference(&ctx->batch, NULL); /* unref current batch */
182 fd_bc_invalidate_context(ctx);
183
184 fd_prog_fini(pctx);
185
186 if (ctx->blitter)
187 util_blitter_destroy(ctx->blitter);
188
189 if (pctx->stream_uploader)
190 u_upload_destroy(pctx->stream_uploader);
191
192 if (ctx->clear_rs_state)
193 pctx->delete_rasterizer_state(pctx, ctx->clear_rs_state);
194
195 if (ctx->primconvert)
196 util_primconvert_destroy(ctx->primconvert);
197
198 slab_destroy_child(&ctx->transfer_pool);
199
200 for (i = 0; i < ARRAY_SIZE(ctx->vsc_pipe_bo); i++) {
201 if (!ctx->vsc_pipe_bo[i])
202 break;
203 fd_bo_del(ctx->vsc_pipe_bo[i]);
204 }
205
206 fd_device_del(ctx->dev);
207 fd_pipe_del(ctx->pipe);
208
209 mtx_destroy(&ctx->gmem_lock);
210
211 if (fd_mesa_debug & (FD_DBG_BSTAT | FD_DBG_MSGS)) {
212 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_nondraw=%u, batch_restore=%u\n",
213 (uint32_t)ctx->stats.batch_total, (uint32_t)ctx->stats.batch_sysmem,
214 (uint32_t)ctx->stats.batch_gmem, (uint32_t)ctx->stats.batch_nondraw,
215 (uint32_t)ctx->stats.batch_restore);
216 }
217 }
218
219 static void
220 fd_set_debug_callback(struct pipe_context *pctx,
221 const struct pipe_debug_callback *cb)
222 {
223 struct fd_context *ctx = fd_context(pctx);
224
225 if (cb)
226 ctx->debug = *cb;
227 else
228 memset(&ctx->debug, 0, sizeof(ctx->debug));
229 }
230
231 static uint32_t
232 fd_get_reset_count(struct fd_context *ctx, bool per_context)
233 {
234 uint64_t val;
235 enum fd_param_id param =
236 per_context ? FD_CTX_FAULTS : FD_GLOBAL_FAULTS;
237 int ret = fd_pipe_get_param(ctx->pipe, param, &val);
238 debug_assert(!ret);
239 return val;
240 }
241
242 static enum pipe_reset_status
243 fd_get_device_reset_status(struct pipe_context *pctx)
244 {
245 struct fd_context *ctx = fd_context(pctx);
246 int context_faults = fd_get_reset_count(ctx, true);
247 int global_faults = fd_get_reset_count(ctx, false);
248 enum pipe_reset_status status;
249
250 if (context_faults != ctx->context_reset_count) {
251 status = PIPE_GUILTY_CONTEXT_RESET;
252 } else if (global_faults != ctx->global_reset_count) {
253 status = PIPE_INNOCENT_CONTEXT_RESET;
254 } else {
255 status = PIPE_NO_RESET;
256 }
257
258 ctx->context_reset_count = context_faults;
259 ctx->global_reset_count = global_faults;
260
261 return status;
262 }
263
264 /* TODO we could combine a few of these small buffers (solid_vbuf,
265 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
266 * save a tiny bit of memory
267 */
268
269 static struct pipe_resource *
270 create_solid_vertexbuf(struct pipe_context *pctx)
271 {
272 static const float init_shader_const[] = {
273 -1.000000, +1.000000, +1.000000,
274 +1.000000, -1.000000, +1.000000,
275 };
276 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
277 PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE, sizeof(init_shader_const));
278 pipe_buffer_write(pctx, prsc, 0,
279 sizeof(init_shader_const), init_shader_const);
280 return prsc;
281 }
282
283 static struct pipe_resource *
284 create_blit_texcoord_vertexbuf(struct pipe_context *pctx)
285 {
286 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
287 PIPE_BIND_CUSTOM, PIPE_USAGE_DYNAMIC, 16);
288 return prsc;
289 }
290
291 void
292 fd_context_setup_common_vbos(struct fd_context *ctx)
293 {
294 struct pipe_context *pctx = &ctx->base;
295
296 ctx->solid_vbuf = create_solid_vertexbuf(pctx);
297 ctx->blit_texcoord_vbuf = create_blit_texcoord_vertexbuf(pctx);
298
299 /* setup solid_vbuf_state: */
300 ctx->solid_vbuf_state.vtx = pctx->create_vertex_elements_state(
301 pctx, 1, (struct pipe_vertex_element[]){{
302 .vertex_buffer_index = 0,
303 .src_offset = 0,
304 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
305 }});
306 ctx->solid_vbuf_state.vertexbuf.count = 1;
307 ctx->solid_vbuf_state.vertexbuf.vb[0].stride = 12;
308 ctx->solid_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->solid_vbuf;
309
310 /* setup blit_vbuf_state: */
311 ctx->blit_vbuf_state.vtx = pctx->create_vertex_elements_state(
312 pctx, 2, (struct pipe_vertex_element[]){{
313 .vertex_buffer_index = 0,
314 .src_offset = 0,
315 .src_format = PIPE_FORMAT_R32G32_FLOAT,
316 }, {
317 .vertex_buffer_index = 1,
318 .src_offset = 0,
319 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
320 }});
321 ctx->blit_vbuf_state.vertexbuf.count = 2;
322 ctx->blit_vbuf_state.vertexbuf.vb[0].stride = 8;
323 ctx->blit_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->blit_texcoord_vbuf;
324 ctx->blit_vbuf_state.vertexbuf.vb[1].stride = 12;
325 ctx->blit_vbuf_state.vertexbuf.vb[1].buffer.resource = ctx->solid_vbuf;
326 }
327
328 void
329 fd_context_cleanup_common_vbos(struct fd_context *ctx)
330 {
331 struct pipe_context *pctx = &ctx->base;
332
333 pctx->delete_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
334 pctx->delete_vertex_elements_state(pctx, ctx->blit_vbuf_state.vtx);
335
336 pipe_resource_reference(&ctx->solid_vbuf, NULL);
337 pipe_resource_reference(&ctx->blit_texcoord_vbuf, NULL);
338 }
339
340 struct pipe_context *
341 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
342 const uint8_t *primtypes, void *priv, unsigned flags)
343 {
344 struct fd_screen *screen = fd_screen(pscreen);
345 struct pipe_context *pctx;
346 unsigned prio = 1;
347 int i;
348
349 /* lower numerical value == higher priority: */
350 if (fd_mesa_debug & FD_DBG_HIPRIO)
351 prio = 0;
352 else if (flags & PIPE_CONTEXT_HIGH_PRIORITY)
353 prio = 0;
354 else if (flags & PIPE_CONTEXT_LOW_PRIORITY)
355 prio = 2;
356
357 ctx->screen = screen;
358 ctx->pipe = fd_pipe_new2(screen->dev, FD_PIPE_3D, prio);
359
360 if (fd_device_version(screen->dev) >= FD_VERSION_ROBUSTNESS) {
361 ctx->context_reset_count = fd_get_reset_count(ctx, true);
362 ctx->global_reset_count = fd_get_reset_count(ctx, false);
363 }
364
365 ctx->primtypes = primtypes;
366 ctx->primtype_mask = 0;
367 for (i = 0; i < PIPE_PRIM_MAX; i++)
368 if (primtypes[i])
369 ctx->primtype_mask |= (1 << i);
370
371 (void) mtx_init(&ctx->gmem_lock, mtx_plain);
372
373 /* need some sane default in case state tracker doesn't
374 * set some state:
375 */
376 ctx->sample_mask = 0xffff;
377 ctx->active_queries = true;
378
379 pctx = &ctx->base;
380 pctx->screen = pscreen;
381 pctx->priv = priv;
382 pctx->flush = fd_context_flush;
383 pctx->emit_string_marker = fd_emit_string_marker;
384 pctx->set_debug_callback = fd_set_debug_callback;
385 pctx->get_device_reset_status = fd_get_device_reset_status;
386 pctx->create_fence_fd = fd_create_fence_fd;
387 pctx->fence_server_sync = fd_fence_server_sync;
388 pctx->texture_barrier = fd_texture_barrier;
389 pctx->memory_barrier = fd_memory_barrier;
390
391 pctx->stream_uploader = u_upload_create_default(pctx);
392 if (!pctx->stream_uploader)
393 goto fail;
394 pctx->const_uploader = pctx->stream_uploader;
395
396 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
397
398 fd_draw_init(pctx);
399 fd_resource_context_init(pctx);
400 fd_query_context_init(pctx);
401 fd_texture_init(pctx);
402 fd_state_init(pctx);
403
404 ctx->blitter = util_blitter_create(pctx);
405 if (!ctx->blitter)
406 goto fail;
407
408 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask);
409 if (!ctx->primconvert)
410 goto fail;
411
412 list_inithead(&ctx->hw_active_queries);
413 list_inithead(&ctx->acc_active_queries);
414 list_inithead(&ctx->log_chunks);
415
416 if ((fd_mesa_debug & FD_DBG_LOG) &&
417 !(ctx->record_timestamp && ctx->ts_to_ns)) {
418 printf("logging not supported!\n");
419 fd_mesa_debug &= ~FD_DBG_LOG;
420 }
421
422 return pctx;
423
424 fail:
425 pctx->destroy(pctx);
426 return NULL;
427 }