2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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:
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
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
24 * Rob Clark <robclark@freedesktop.org>
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"
42 fd_context_flush(struct pipe_context
*pctx
, struct pipe_fence_handle
**fencep
,
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
);
50 DBG("%p: flush: flags=%x\n", ctx
->batch
, flags
);
52 /* In some sequence of events, we can end up with a last_fence that is
53 * not an "fd" fence, which results in eglDupNativeFenceFDANDROID()
57 if (flags
& PIPE_FLUSH_FENCE_FD
)
58 fd_fence_ref(&ctx
->last_fence
, NULL
);
60 /* if no rendering since last flush, ie. app just decided it needed
61 * a fence, re-use the last one:
63 if (ctx
->last_fence
) {
64 fd_fence_ref(&fence
, ctx
->last_fence
);
71 /* Take a ref to the batch's fence (batch can be unref'd when flushed: */
72 fd_fence_ref(&fence
, batch
->fence
);
74 if (flags
& PIPE_FLUSH_FENCE_FD
)
75 batch
->needs_out_fence_fd
= true;
77 if (!ctx
->screen
->reorder
) {
78 fd_batch_flush(batch
, true);
79 } else if (flags
& PIPE_FLUSH_DEFERRED
) {
80 fd_bc_flush_deferred(&ctx
->screen
->batch_cache
, ctx
);
82 fd_bc_flush(&ctx
->screen
->batch_cache
, ctx
);
87 fd_fence_ref(fencep
, fence
);
89 fd_fence_ref(&ctx
->last_fence
, fence
);
91 fd_fence_ref(&fence
, NULL
);
95 fd_texture_barrier(struct pipe_context
*pctx
, unsigned flags
)
97 if (flags
== PIPE_TEXTURE_BARRIER_FRAMEBUFFER
) {
98 struct fd_context
*ctx
= fd_context(pctx
);
100 if (ctx
->framebuffer_barrier
) {
101 ctx
->framebuffer_barrier(ctx
);
106 /* On devices that could sample from GMEM we could possibly do better.
107 * Or if we knew that we were doing GMEM bypass we could just emit a
108 * cache flush, perhaps? But we don't know if future draws would cause
109 * us to use GMEM, and a flush in bypass isn't the end of the world.
111 fd_context_flush(pctx
, NULL
, 0);
115 fd_memory_barrier(struct pipe_context
*pctx
, unsigned flags
)
117 if (!(flags
& ~PIPE_BARRIER_UPDATE
))
120 fd_context_flush(pctx
, NULL
, 0);
121 /* TODO do we need to check for persistently mapped buffers and fd_bo_cpu_prep()?? */
125 * emit marker string as payload of a no-op packet, which can be
126 * decoded by cffdump.
129 fd_emit_string_marker(struct pipe_context
*pctx
, const char *string
, int len
)
131 struct fd_context
*ctx
= fd_context(pctx
);
132 struct fd_ringbuffer
*ring
;
133 const uint32_t *buf
= (const void *)string
;
138 ctx
->batch
->needs_flush
= true;
140 ring
= ctx
->batch
->draw
;
142 /* max packet size is 0x3fff dwords: */
143 len
= MIN2(len
, 0x3fff * 4);
145 if (ctx
->screen
->gpu_id
>= 500)
146 OUT_PKT7(ring
, CP_NOP
, align(len
, 4) / 4);
148 OUT_PKT3(ring
, CP_NOP
, align(len
, 4) / 4);
150 OUT_RING(ring
, *buf
);
155 /* copy remainder bytes without reading past end of input string: */
158 memcpy(&w
, buf
, len
);
164 fd_context_destroy(struct pipe_context
*pctx
)
166 struct fd_context
*ctx
= fd_context(pctx
);
171 fd_fence_ref(&ctx
->last_fence
, NULL
);
173 if (ctx
->screen
->reorder
&& util_queue_is_initialized(&ctx
->flush_queue
))
174 util_queue_destroy(&ctx
->flush_queue
);
176 util_copy_framebuffer_state(&ctx
->framebuffer
, NULL
);
177 fd_batch_reference(&ctx
->batch
, NULL
); /* unref current batch */
178 fd_bc_invalidate_context(ctx
);
183 util_blitter_destroy(ctx
->blitter
);
185 if (pctx
->stream_uploader
)
186 u_upload_destroy(pctx
->stream_uploader
);
188 if (ctx
->clear_rs_state
)
189 pctx
->delete_rasterizer_state(pctx
, ctx
->clear_rs_state
);
191 if (ctx
->primconvert
)
192 util_primconvert_destroy(ctx
->primconvert
);
194 slab_destroy_child(&ctx
->transfer_pool
);
196 for (i
= 0; i
< ARRAY_SIZE(ctx
->vsc_pipe
); i
++) {
197 struct fd_vsc_pipe
*pipe
= &ctx
->vsc_pipe
[i
];
203 fd_device_del(ctx
->dev
);
204 fd_pipe_del(ctx
->pipe
);
206 if (fd_mesa_debug
& (FD_DBG_BSTAT
| FD_DBG_MSGS
)) {
207 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_nondraw=%u, batch_restore=%u\n",
208 (uint32_t)ctx
->stats
.batch_total
, (uint32_t)ctx
->stats
.batch_sysmem
,
209 (uint32_t)ctx
->stats
.batch_gmem
, (uint32_t)ctx
->stats
.batch_nondraw
,
210 (uint32_t)ctx
->stats
.batch_restore
);
215 fd_set_debug_callback(struct pipe_context
*pctx
,
216 const struct pipe_debug_callback
*cb
)
218 struct fd_context
*ctx
= fd_context(pctx
);
223 memset(&ctx
->debug
, 0, sizeof(ctx
->debug
));
227 fd_get_reset_count(struct fd_context
*ctx
, bool per_context
)
230 enum fd_param_id param
=
231 per_context
? FD_CTX_FAULTS
: FD_GLOBAL_FAULTS
;
232 int ret
= fd_pipe_get_param(ctx
->pipe
, param
, &val
);
237 static enum pipe_reset_status
238 fd_get_device_reset_status(struct pipe_context
*pctx
)
240 struct fd_context
*ctx
= fd_context(pctx
);
241 int context_faults
= fd_get_reset_count(ctx
, true);
242 int global_faults
= fd_get_reset_count(ctx
, false);
243 enum pipe_reset_status status
;
245 if (context_faults
!= ctx
->context_reset_count
) {
246 status
= PIPE_GUILTY_CONTEXT_RESET
;
247 } else if (global_faults
!= ctx
->global_reset_count
) {
248 status
= PIPE_INNOCENT_CONTEXT_RESET
;
250 status
= PIPE_NO_RESET
;
253 ctx
->context_reset_count
= context_faults
;
254 ctx
->global_reset_count
= global_faults
;
259 /* TODO we could combine a few of these small buffers (solid_vbuf,
260 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
261 * save a tiny bit of memory
264 static struct pipe_resource
*
265 create_solid_vertexbuf(struct pipe_context
*pctx
)
267 static const float init_shader_const
[] = {
268 -1.000000, +1.000000, +1.000000,
269 +1.000000, -1.000000, +1.000000,
271 struct pipe_resource
*prsc
= pipe_buffer_create(pctx
->screen
,
272 PIPE_BIND_CUSTOM
, PIPE_USAGE_IMMUTABLE
, sizeof(init_shader_const
));
273 pipe_buffer_write(pctx
, prsc
, 0,
274 sizeof(init_shader_const
), init_shader_const
);
278 static struct pipe_resource
*
279 create_blit_texcoord_vertexbuf(struct pipe_context
*pctx
)
281 struct pipe_resource
*prsc
= pipe_buffer_create(pctx
->screen
,
282 PIPE_BIND_CUSTOM
, PIPE_USAGE_DYNAMIC
, 16);
287 fd_context_setup_common_vbos(struct fd_context
*ctx
)
289 struct pipe_context
*pctx
= &ctx
->base
;
291 ctx
->solid_vbuf
= create_solid_vertexbuf(pctx
);
292 ctx
->blit_texcoord_vbuf
= create_blit_texcoord_vertexbuf(pctx
);
294 /* setup solid_vbuf_state: */
295 ctx
->solid_vbuf_state
.vtx
= pctx
->create_vertex_elements_state(
296 pctx
, 1, (struct pipe_vertex_element
[]){{
297 .vertex_buffer_index
= 0,
299 .src_format
= PIPE_FORMAT_R32G32B32_FLOAT
,
301 ctx
->solid_vbuf_state
.vertexbuf
.count
= 1;
302 ctx
->solid_vbuf_state
.vertexbuf
.vb
[0].stride
= 12;
303 ctx
->solid_vbuf_state
.vertexbuf
.vb
[0].buffer
.resource
= ctx
->solid_vbuf
;
305 /* setup blit_vbuf_state: */
306 ctx
->blit_vbuf_state
.vtx
= pctx
->create_vertex_elements_state(
307 pctx
, 2, (struct pipe_vertex_element
[]){{
308 .vertex_buffer_index
= 0,
310 .src_format
= PIPE_FORMAT_R32G32_FLOAT
,
312 .vertex_buffer_index
= 1,
314 .src_format
= PIPE_FORMAT_R32G32B32_FLOAT
,
316 ctx
->blit_vbuf_state
.vertexbuf
.count
= 2;
317 ctx
->blit_vbuf_state
.vertexbuf
.vb
[0].stride
= 8;
318 ctx
->blit_vbuf_state
.vertexbuf
.vb
[0].buffer
.resource
= ctx
->blit_texcoord_vbuf
;
319 ctx
->blit_vbuf_state
.vertexbuf
.vb
[1].stride
= 12;
320 ctx
->blit_vbuf_state
.vertexbuf
.vb
[1].buffer
.resource
= ctx
->solid_vbuf
;
324 fd_context_cleanup_common_vbos(struct fd_context
*ctx
)
326 struct pipe_context
*pctx
= &ctx
->base
;
328 pctx
->delete_vertex_elements_state(pctx
, ctx
->solid_vbuf_state
.vtx
);
329 pctx
->delete_vertex_elements_state(pctx
, ctx
->blit_vbuf_state
.vtx
);
331 pipe_resource_reference(&ctx
->solid_vbuf
, NULL
);
332 pipe_resource_reference(&ctx
->blit_texcoord_vbuf
, NULL
);
335 struct pipe_context
*
336 fd_context_init(struct fd_context
*ctx
, struct pipe_screen
*pscreen
,
337 const uint8_t *primtypes
, void *priv
, unsigned flags
)
339 struct fd_screen
*screen
= fd_screen(pscreen
);
340 struct pipe_context
*pctx
;
344 /* lower numerical value == higher priority: */
345 if (fd_mesa_debug
& FD_DBG_HIPRIO
)
347 else if (flags
& PIPE_CONTEXT_HIGH_PRIORITY
)
349 else if (flags
& PIPE_CONTEXT_LOW_PRIORITY
)
352 ctx
->screen
= screen
;
353 ctx
->pipe
= fd_pipe_new2(screen
->dev
, FD_PIPE_3D
, prio
);
355 if (fd_device_version(screen
->dev
) >= FD_VERSION_ROBUSTNESS
) {
356 ctx
->context_reset_count
= fd_get_reset_count(ctx
, true);
357 ctx
->global_reset_count
= fd_get_reset_count(ctx
, false);
360 ctx
->primtypes
= primtypes
;
361 ctx
->primtype_mask
= 0;
362 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++)
364 ctx
->primtype_mask
|= (1 << i
);
366 /* need some sane default in case state tracker doesn't
369 ctx
->sample_mask
= 0xffff;
372 pctx
->screen
= pscreen
;
374 pctx
->flush
= fd_context_flush
;
375 pctx
->emit_string_marker
= fd_emit_string_marker
;
376 pctx
->set_debug_callback
= fd_set_debug_callback
;
377 pctx
->get_device_reset_status
= fd_get_device_reset_status
;
378 pctx
->create_fence_fd
= fd_create_fence_fd
;
379 pctx
->fence_server_sync
= fd_fence_server_sync
;
380 pctx
->texture_barrier
= fd_texture_barrier
;
381 pctx
->memory_barrier
= fd_memory_barrier
;
383 pctx
->stream_uploader
= u_upload_create_default(pctx
);
384 if (!pctx
->stream_uploader
)
386 pctx
->const_uploader
= pctx
->stream_uploader
;
388 if (!ctx
->screen
->reorder
)
389 ctx
->batch
= fd_bc_alloc_batch(&screen
->batch_cache
, ctx
, false);
391 slab_create_child(&ctx
->transfer_pool
, &screen
->transfer_pool
);
394 fd_resource_context_init(pctx
);
395 fd_query_context_init(pctx
);
396 fd_texture_init(pctx
);
399 ctx
->blitter
= util_blitter_create(pctx
);
403 ctx
->primconvert
= util_primconvert_create(pctx
, ctx
->primtype_mask
);
404 if (!ctx
->primconvert
)
407 list_inithead(&ctx
->hw_active_queries
);
408 list_inithead(&ctx
->acc_active_queries
);