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 /* if no rendering since last flush, ie. app just decided it needed
53 * a fence, re-use the last one:
55 if (ctx
->last_fence
) {
56 fd_fence_ref(pctx
->screen
, &fence
, ctx
->last_fence
);
63 /* Take a ref to the batch's fence (batch can be unref'd when flushed: */
64 fd_fence_ref(pctx
->screen
, &fence
, batch
->fence
);
66 /* TODO is it worth trying to figure out if app is using fence-fd's, to
67 * avoid requesting one every batch?
69 batch
->needs_out_fence_fd
= true;
71 if (!ctx
->screen
->reorder
) {
72 fd_batch_flush(batch
, true, false);
73 } else if (flags
& PIPE_FLUSH_DEFERRED
) {
74 fd_bc_flush_deferred(&ctx
->screen
->batch_cache
, ctx
);
76 fd_bc_flush(&ctx
->screen
->batch_cache
, ctx
);
81 fd_fence_ref(pctx
->screen
, fencep
, fence
);
83 fd_fence_ref(pctx
->screen
, &ctx
->last_fence
, fence
);
85 fd_fence_ref(pctx
->screen
, &fence
, NULL
);
89 fd_texture_barrier(struct pipe_context
*pctx
, unsigned flags
)
91 /* On devices that could sample from GMEM we could possibly do better.
92 * Or if we knew that we were doing GMEM bypass we could just emit a
93 * cache flush, perhaps? But we don't know if future draws would cause
94 * us to use GMEM, and a flush in bypass isn't the end of the world.
96 fd_context_flush(pctx
, NULL
, 0);
100 fd_memory_barrier(struct pipe_context
*pctx
, unsigned flags
)
102 fd_context_flush(pctx
, NULL
, 0);
103 /* TODO do we need to check for persistently mapped buffers and fd_bo_cpu_prep()?? */
107 * emit marker string as payload of a no-op packet, which can be
108 * decoded by cffdump.
111 fd_emit_string_marker(struct pipe_context
*pctx
, const char *string
, int len
)
113 struct fd_context
*ctx
= fd_context(pctx
);
114 struct fd_ringbuffer
*ring
;
115 const uint32_t *buf
= (const void *)string
;
120 ctx
->batch
->needs_flush
= true;
122 ring
= ctx
->batch
->draw
;
124 /* max packet size is 0x3fff dwords: */
125 len
= MIN2(len
, 0x3fff * 4);
127 if (ctx
->screen
->gpu_id
>= 500)
128 OUT_PKT7(ring
, CP_NOP
, align(len
, 4) / 4);
130 OUT_PKT3(ring
, CP_NOP
, align(len
, 4) / 4);
132 OUT_RING(ring
, *buf
);
137 /* copy remainder bytes without reading past end of input string: */
140 memcpy(&w
, buf
, len
);
146 fd_context_destroy(struct pipe_context
*pctx
)
148 struct fd_context
*ctx
= fd_context(pctx
);
153 fd_fence_ref(pctx
->screen
, &ctx
->last_fence
, NULL
);
155 if (ctx
->screen
->reorder
&& util_queue_is_initialized(&ctx
->flush_queue
))
156 util_queue_destroy(&ctx
->flush_queue
);
158 util_copy_framebuffer_state(&ctx
->framebuffer
, NULL
);
159 fd_batch_reference(&ctx
->batch
, NULL
); /* unref current batch */
160 fd_bc_invalidate_context(ctx
);
165 util_blitter_destroy(ctx
->blitter
);
167 if (pctx
->stream_uploader
)
168 u_upload_destroy(pctx
->stream_uploader
);
170 if (ctx
->clear_rs_state
)
171 pctx
->delete_rasterizer_state(pctx
, ctx
->clear_rs_state
);
173 if (ctx
->primconvert
)
174 util_primconvert_destroy(ctx
->primconvert
);
176 slab_destroy_child(&ctx
->transfer_pool
);
178 for (i
= 0; i
< ARRAY_SIZE(ctx
->vsc_pipe
); i
++) {
179 struct fd_vsc_pipe
*pipe
= &ctx
->vsc_pipe
[i
];
185 fd_device_del(ctx
->dev
);
186 fd_pipe_del(ctx
->pipe
);
188 if (fd_mesa_debug
& (FD_DBG_BSTAT
| FD_DBG_MSGS
)) {
189 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_nondraw=%u, batch_restore=%u\n",
190 (uint32_t)ctx
->stats
.batch_total
, (uint32_t)ctx
->stats
.batch_sysmem
,
191 (uint32_t)ctx
->stats
.batch_gmem
, (uint32_t)ctx
->stats
.batch_nondraw
,
192 (uint32_t)ctx
->stats
.batch_restore
);
197 fd_set_debug_callback(struct pipe_context
*pctx
,
198 const struct pipe_debug_callback
*cb
)
200 struct fd_context
*ctx
= fd_context(pctx
);
205 memset(&ctx
->debug
, 0, sizeof(ctx
->debug
));
208 /* TODO we could combine a few of these small buffers (solid_vbuf,
209 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
210 * save a tiny bit of memory
213 static struct pipe_resource
*
214 create_solid_vertexbuf(struct pipe_context
*pctx
)
216 static const float init_shader_const
[] = {
217 -1.000000, +1.000000, +1.000000,
218 +1.000000, -1.000000, +1.000000,
220 struct pipe_resource
*prsc
= pipe_buffer_create(pctx
->screen
,
221 PIPE_BIND_CUSTOM
, PIPE_USAGE_IMMUTABLE
, sizeof(init_shader_const
));
222 pipe_buffer_write(pctx
, prsc
, 0,
223 sizeof(init_shader_const
), init_shader_const
);
227 static struct pipe_resource
*
228 create_blit_texcoord_vertexbuf(struct pipe_context
*pctx
)
230 struct pipe_resource
*prsc
= pipe_buffer_create(pctx
->screen
,
231 PIPE_BIND_CUSTOM
, PIPE_USAGE_DYNAMIC
, 16);
236 fd_context_setup_common_vbos(struct fd_context
*ctx
)
238 struct pipe_context
*pctx
= &ctx
->base
;
240 ctx
->solid_vbuf
= create_solid_vertexbuf(pctx
);
241 ctx
->blit_texcoord_vbuf
= create_blit_texcoord_vertexbuf(pctx
);
243 /* setup solid_vbuf_state: */
244 ctx
->solid_vbuf_state
.vtx
= pctx
->create_vertex_elements_state(
245 pctx
, 1, (struct pipe_vertex_element
[]){{
246 .vertex_buffer_index
= 0,
248 .src_format
= PIPE_FORMAT_R32G32B32_FLOAT
,
250 ctx
->solid_vbuf_state
.vertexbuf
.count
= 1;
251 ctx
->solid_vbuf_state
.vertexbuf
.vb
[0].stride
= 12;
252 ctx
->solid_vbuf_state
.vertexbuf
.vb
[0].buffer
.resource
= ctx
->solid_vbuf
;
254 /* setup blit_vbuf_state: */
255 ctx
->blit_vbuf_state
.vtx
= pctx
->create_vertex_elements_state(
256 pctx
, 2, (struct pipe_vertex_element
[]){{
257 .vertex_buffer_index
= 0,
259 .src_format
= PIPE_FORMAT_R32G32_FLOAT
,
261 .vertex_buffer_index
= 1,
263 .src_format
= PIPE_FORMAT_R32G32B32_FLOAT
,
265 ctx
->blit_vbuf_state
.vertexbuf
.count
= 2;
266 ctx
->blit_vbuf_state
.vertexbuf
.vb
[0].stride
= 8;
267 ctx
->blit_vbuf_state
.vertexbuf
.vb
[0].buffer
.resource
= ctx
->blit_texcoord_vbuf
;
268 ctx
->blit_vbuf_state
.vertexbuf
.vb
[1].stride
= 12;
269 ctx
->blit_vbuf_state
.vertexbuf
.vb
[1].buffer
.resource
= ctx
->solid_vbuf
;
273 fd_context_cleanup_common_vbos(struct fd_context
*ctx
)
275 struct pipe_context
*pctx
= &ctx
->base
;
277 pctx
->delete_vertex_elements_state(pctx
, ctx
->solid_vbuf_state
.vtx
);
278 pctx
->delete_vertex_elements_state(pctx
, ctx
->blit_vbuf_state
.vtx
);
280 pipe_resource_reference(&ctx
->solid_vbuf
, NULL
);
281 pipe_resource_reference(&ctx
->blit_texcoord_vbuf
, NULL
);
284 struct pipe_context
*
285 fd_context_init(struct fd_context
*ctx
, struct pipe_screen
*pscreen
,
286 const uint8_t *primtypes
, void *priv
, unsigned flags
)
288 struct fd_screen
*screen
= fd_screen(pscreen
);
289 struct pipe_context
*pctx
;
293 /* lower numerical value == higher priority: */
294 if (fd_mesa_debug
& FD_DBG_HIPRIO
)
296 else if (flags
& PIPE_CONTEXT_HIGH_PRIORITY
)
298 else if (flags
& PIPE_CONTEXT_LOW_PRIORITY
)
301 ctx
->screen
= screen
;
302 ctx
->pipe
= fd_pipe_new2(screen
->dev
, FD_PIPE_3D
, prio
);
304 ctx
->primtypes
= primtypes
;
305 ctx
->primtype_mask
= 0;
306 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++)
308 ctx
->primtype_mask
|= (1 << i
);
310 /* need some sane default in case state tracker doesn't
313 ctx
->sample_mask
= 0xffff;
316 pctx
->screen
= pscreen
;
318 pctx
->flush
= fd_context_flush
;
319 pctx
->emit_string_marker
= fd_emit_string_marker
;
320 pctx
->set_debug_callback
= fd_set_debug_callback
;
321 pctx
->create_fence_fd
= fd_create_fence_fd
;
322 pctx
->fence_server_sync
= fd_fence_server_sync
;
323 pctx
->texture_barrier
= fd_texture_barrier
;
324 pctx
->memory_barrier
= fd_memory_barrier
;
326 pctx
->stream_uploader
= u_upload_create_default(pctx
);
327 if (!pctx
->stream_uploader
)
329 pctx
->const_uploader
= pctx
->stream_uploader
;
331 if (!ctx
->screen
->reorder
)
332 ctx
->batch
= fd_bc_alloc_batch(&screen
->batch_cache
, ctx
, false);
334 slab_create_child(&ctx
->transfer_pool
, &screen
->transfer_pool
);
337 fd_resource_context_init(pctx
);
338 fd_query_context_init(pctx
);
339 fd_texture_init(pctx
);
342 ctx
->blitter
= util_blitter_create(pctx
);
346 ctx
->primconvert
= util_primconvert_create(pctx
, ctx
->primtype_mask
);
347 if (!ctx
->primconvert
)
350 list_inithead(&ctx
->hw_active_queries
);
351 list_inithead(&ctx
->acc_active_queries
);