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
);
55 /* Take a ref to the batch's fence (batch can be unref'd when flushed: */
56 fd_fence_ref(pctx
->screen
, &fence
, batch
->fence
);
58 if (flags
& PIPE_FLUSH_FENCE_FD
)
59 batch
->needs_out_fence_fd
= true;
61 if (!ctx
->screen
->reorder
) {
62 fd_batch_flush(batch
, true, false);
63 } else if (flags
& PIPE_FLUSH_DEFERRED
) {
64 fd_bc_flush_deferred(&ctx
->screen
->batch_cache
, ctx
);
66 fd_bc_flush(&ctx
->screen
->batch_cache
, ctx
);
70 fd_fence_ref(pctx
->screen
, fencep
, fence
);
72 fd_fence_ref(pctx
->screen
, &fence
, NULL
);
76 fd_texture_barrier(struct pipe_context
*pctx
, unsigned flags
)
78 /* On devices that could sample from GMEM we could possibly do better.
79 * Or if we knew that we were doing GMEM bypass we could just emit a
80 * cache flush, perhaps? But we don't know if future draws would cause
81 * us to use GMEM, and a flush in bypass isn't the end of the world.
83 fd_context_flush(pctx
, NULL
, 0);
87 fd_memory_barrier(struct pipe_context
*pctx
, unsigned flags
)
89 fd_context_flush(pctx
, NULL
, 0);
90 /* TODO do we need to check for persistently mapped buffers and fd_bo_cpu_prep()?? */
94 * emit marker string as payload of a no-op packet, which can be
98 fd_emit_string_marker(struct pipe_context
*pctx
, const char *string
, int len
)
100 struct fd_context
*ctx
= fd_context(pctx
);
101 struct fd_ringbuffer
*ring
;
102 const uint32_t *buf
= (const void *)string
;
107 ctx
->batch
->needs_flush
= true;
109 ring
= ctx
->batch
->draw
;
111 /* max packet size is 0x3fff dwords: */
112 len
= MIN2(len
, 0x3fff * 4);
114 if (ctx
->screen
->gpu_id
>= 500)
115 OUT_PKT7(ring
, CP_NOP
, align(len
, 4) / 4);
117 OUT_PKT3(ring
, CP_NOP
, align(len
, 4) / 4);
119 OUT_RING(ring
, *buf
);
124 /* copy remainder bytes without reading past end of input string: */
127 memcpy(&w
, buf
, len
);
133 fd_context_destroy(struct pipe_context
*pctx
)
135 struct fd_context
*ctx
= fd_context(pctx
);
140 if (ctx
->screen
->reorder
&& util_queue_is_initialized(&ctx
->flush_queue
))
141 util_queue_destroy(&ctx
->flush_queue
);
143 util_copy_framebuffer_state(&ctx
->framebuffer
, NULL
);
144 fd_batch_reference(&ctx
->batch
, NULL
); /* unref current batch */
145 fd_bc_invalidate_context(ctx
);
150 util_blitter_destroy(ctx
->blitter
);
152 if (pctx
->stream_uploader
)
153 u_upload_destroy(pctx
->stream_uploader
);
155 if (ctx
->clear_rs_state
)
156 pctx
->delete_rasterizer_state(pctx
, ctx
->clear_rs_state
);
158 if (ctx
->primconvert
)
159 util_primconvert_destroy(ctx
->primconvert
);
161 slab_destroy_child(&ctx
->transfer_pool
);
163 for (i
= 0; i
< ARRAY_SIZE(ctx
->vsc_pipe
); i
++) {
164 struct fd_vsc_pipe
*pipe
= &ctx
->vsc_pipe
[i
];
170 fd_device_del(ctx
->dev
);
171 fd_pipe_del(ctx
->pipe
);
173 if (fd_mesa_debug
& (FD_DBG_BSTAT
| FD_DBG_MSGS
)) {
174 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_nondraw=%u, batch_restore=%u\n",
175 (uint32_t)ctx
->stats
.batch_total
, (uint32_t)ctx
->stats
.batch_sysmem
,
176 (uint32_t)ctx
->stats
.batch_gmem
, (uint32_t)ctx
->stats
.batch_nondraw
,
177 (uint32_t)ctx
->stats
.batch_restore
);
182 fd_set_debug_callback(struct pipe_context
*pctx
,
183 const struct pipe_debug_callback
*cb
)
185 struct fd_context
*ctx
= fd_context(pctx
);
190 memset(&ctx
->debug
, 0, sizeof(ctx
->debug
));
193 /* TODO we could combine a few of these small buffers (solid_vbuf,
194 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
195 * save a tiny bit of memory
198 static struct pipe_resource
*
199 create_solid_vertexbuf(struct pipe_context
*pctx
)
201 static const float init_shader_const
[] = {
202 -1.000000, +1.000000, +1.000000,
203 +1.000000, -1.000000, +1.000000,
205 struct pipe_resource
*prsc
= pipe_buffer_create(pctx
->screen
,
206 PIPE_BIND_CUSTOM
, PIPE_USAGE_IMMUTABLE
, sizeof(init_shader_const
));
207 pipe_buffer_write(pctx
, prsc
, 0,
208 sizeof(init_shader_const
), init_shader_const
);
212 static struct pipe_resource
*
213 create_blit_texcoord_vertexbuf(struct pipe_context
*pctx
)
215 struct pipe_resource
*prsc
= pipe_buffer_create(pctx
->screen
,
216 PIPE_BIND_CUSTOM
, PIPE_USAGE_DYNAMIC
, 16);
221 fd_context_setup_common_vbos(struct fd_context
*ctx
)
223 struct pipe_context
*pctx
= &ctx
->base
;
225 ctx
->solid_vbuf
= create_solid_vertexbuf(pctx
);
226 ctx
->blit_texcoord_vbuf
= create_blit_texcoord_vertexbuf(pctx
);
228 /* setup solid_vbuf_state: */
229 ctx
->solid_vbuf_state
.vtx
= pctx
->create_vertex_elements_state(
230 pctx
, 1, (struct pipe_vertex_element
[]){{
231 .vertex_buffer_index
= 0,
233 .src_format
= PIPE_FORMAT_R32G32B32_FLOAT
,
235 ctx
->solid_vbuf_state
.vertexbuf
.count
= 1;
236 ctx
->solid_vbuf_state
.vertexbuf
.vb
[0].stride
= 12;
237 ctx
->solid_vbuf_state
.vertexbuf
.vb
[0].buffer
.resource
= ctx
->solid_vbuf
;
239 /* setup blit_vbuf_state: */
240 ctx
->blit_vbuf_state
.vtx
= pctx
->create_vertex_elements_state(
241 pctx
, 2, (struct pipe_vertex_element
[]){{
242 .vertex_buffer_index
= 0,
244 .src_format
= PIPE_FORMAT_R32G32_FLOAT
,
246 .vertex_buffer_index
= 1,
248 .src_format
= PIPE_FORMAT_R32G32B32_FLOAT
,
250 ctx
->blit_vbuf_state
.vertexbuf
.count
= 2;
251 ctx
->blit_vbuf_state
.vertexbuf
.vb
[0].stride
= 8;
252 ctx
->blit_vbuf_state
.vertexbuf
.vb
[0].buffer
.resource
= ctx
->blit_texcoord_vbuf
;
253 ctx
->blit_vbuf_state
.vertexbuf
.vb
[1].stride
= 12;
254 ctx
->blit_vbuf_state
.vertexbuf
.vb
[1].buffer
.resource
= ctx
->solid_vbuf
;
258 fd_context_cleanup_common_vbos(struct fd_context
*ctx
)
260 struct pipe_context
*pctx
= &ctx
->base
;
262 pctx
->delete_vertex_elements_state(pctx
, ctx
->solid_vbuf_state
.vtx
);
263 pctx
->delete_vertex_elements_state(pctx
, ctx
->blit_vbuf_state
.vtx
);
265 pipe_resource_reference(&ctx
->solid_vbuf
, NULL
);
266 pipe_resource_reference(&ctx
->blit_texcoord_vbuf
, NULL
);
269 struct pipe_context
*
270 fd_context_init(struct fd_context
*ctx
, struct pipe_screen
*pscreen
,
271 const uint8_t *primtypes
, void *priv
, unsigned flags
)
273 struct fd_screen
*screen
= fd_screen(pscreen
);
274 struct pipe_context
*pctx
;
278 /* lower numerical value == higher priority: */
279 if (fd_mesa_debug
& FD_DBG_HIPRIO
)
281 else if (flags
& PIPE_CONTEXT_HIGH_PRIORITY
)
283 else if (flags
& PIPE_CONTEXT_LOW_PRIORITY
)
286 ctx
->screen
= screen
;
287 ctx
->pipe
= fd_pipe_new2(screen
->dev
, FD_PIPE_3D
, prio
);
289 ctx
->primtypes
= primtypes
;
290 ctx
->primtype_mask
= 0;
291 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++)
293 ctx
->primtype_mask
|= (1 << i
);
295 /* need some sane default in case state tracker doesn't
298 ctx
->sample_mask
= 0xffff;
301 pctx
->screen
= pscreen
;
303 pctx
->flush
= fd_context_flush
;
304 pctx
->emit_string_marker
= fd_emit_string_marker
;
305 pctx
->set_debug_callback
= fd_set_debug_callback
;
306 pctx
->create_fence_fd
= fd_create_fence_fd
;
307 pctx
->fence_server_sync
= fd_fence_server_sync
;
308 pctx
->texture_barrier
= fd_texture_barrier
;
309 pctx
->memory_barrier
= fd_memory_barrier
;
311 pctx
->stream_uploader
= u_upload_create_default(pctx
);
312 if (!pctx
->stream_uploader
)
314 pctx
->const_uploader
= pctx
->stream_uploader
;
316 if (!ctx
->screen
->reorder
)
317 ctx
->batch
= fd_bc_alloc_batch(&screen
->batch_cache
, ctx
, false);
319 slab_create_child(&ctx
->transfer_pool
, &screen
->transfer_pool
);
322 ctx
->blit
= fd_blitter_blit
;
325 fd_resource_context_init(pctx
);
326 fd_query_context_init(pctx
);
327 fd_texture_init(pctx
);
330 ctx
->blitter
= util_blitter_create(pctx
);
334 ctx
->primconvert
= util_primconvert_create(pctx
, ctx
->primtype_mask
);
335 if (!ctx
->primconvert
)
338 list_inithead(&ctx
->hw_active_queries
);
339 list_inithead(&ctx
->acc_active_queries
);