1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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:
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
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
26 * Rob Clark <robclark@freedesktop.org>
29 #include "freedreno_context.h"
30 #include "freedreno_blitter.h"
31 #include "freedreno_draw.h"
32 #include "freedreno_fence.h"
33 #include "freedreno_program.h"
34 #include "freedreno_resource.h"
35 #include "freedreno_texture.h"
36 #include "freedreno_state.h"
37 #include "freedreno_gmem.h"
38 #include "freedreno_query.h"
39 #include "freedreno_query_hw.h"
40 #include "freedreno_util.h"
41 #include "util/u_upload_mgr.h"
44 fd_context_flush(struct pipe_context
*pctx
, struct pipe_fence_handle
**fencep
,
47 struct fd_context
*ctx
= fd_context(pctx
);
48 struct pipe_fence_handle
*fence
= NULL
;
50 /* Take a ref to the batch's fence (batch can be unref'd when flushed: */
51 fd_fence_ref(pctx
->screen
, &fence
, ctx
->batch
->fence
);
53 if (flags
& PIPE_FLUSH_FENCE_FD
)
54 ctx
->batch
->needs_out_fence_fd
= true;
56 if (!ctx
->screen
->reorder
) {
57 fd_batch_flush(ctx
->batch
, true, false);
58 } else if (flags
& PIPE_FLUSH_DEFERRED
) {
59 fd_bc_flush_deferred(&ctx
->screen
->batch_cache
, ctx
);
61 fd_bc_flush(&ctx
->screen
->batch_cache
, ctx
);
65 fd_fence_ref(pctx
->screen
, fencep
, fence
);
67 fd_fence_ref(pctx
->screen
, &fence
, NULL
);
71 fd_texture_barrier(struct pipe_context
*pctx
, unsigned flags
)
73 /* On devices that could sample from GMEM we could possibly do better.
74 * Or if we knew that we were doing GMEM bypass we could just emit a
75 * cache flush, perhaps? But we don't know if future draws would cause
76 * us to use GMEM, and a flush in bypass isn't the end of the world.
78 fd_context_flush(pctx
, NULL
, 0);
82 * emit marker string as payload of a no-op packet, which can be
86 fd_emit_string_marker(struct pipe_context
*pctx
, const char *string
, int len
)
88 struct fd_context
*ctx
= fd_context(pctx
);
89 struct fd_ringbuffer
*ring
;
90 const uint32_t *buf
= (const void *)string
;
95 ring
= ctx
->batch
->draw
;
97 /* max packet size is 0x3fff dwords: */
98 len
= MIN2(len
, 0x3fff * 4);
100 if (ctx
->screen
->gpu_id
>= 500)
101 OUT_PKT7(ring
, CP_NOP
, align(len
, 4) / 4);
103 OUT_PKT3(ring
, CP_NOP
, align(len
, 4) / 4);
105 OUT_RING(ring
, *buf
);
110 /* copy remainder bytes without reading past end of input string: */
113 memcpy(&w
, buf
, len
);
119 fd_context_destroy(struct pipe_context
*pctx
)
121 struct fd_context
*ctx
= fd_context(pctx
);
126 if (ctx
->screen
->reorder
&& util_queue_is_initialized(&ctx
->flush_queue
))
127 util_queue_destroy(&ctx
->flush_queue
);
129 fd_batch_reference(&ctx
->batch
, NULL
); /* unref current batch */
130 fd_bc_invalidate_context(ctx
);
135 util_blitter_destroy(ctx
->blitter
);
137 if (pctx
->stream_uploader
)
138 u_upload_destroy(pctx
->stream_uploader
);
140 if (ctx
->clear_rs_state
)
141 pctx
->delete_rasterizer_state(pctx
, ctx
->clear_rs_state
);
143 if (ctx
->primconvert
)
144 util_primconvert_destroy(ctx
->primconvert
);
146 slab_destroy_child(&ctx
->transfer_pool
);
148 for (i
= 0; i
< ARRAY_SIZE(ctx
->vsc_pipe
); i
++) {
149 struct fd_vsc_pipe
*pipe
= &ctx
->vsc_pipe
[i
];
155 fd_device_del(ctx
->dev
);
156 fd_pipe_del(ctx
->pipe
);
158 if (fd_mesa_debug
& (FD_DBG_BSTAT
| FD_DBG_MSGS
)) {
159 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_nondraw=%u, batch_restore=%u\n",
160 (uint32_t)ctx
->stats
.batch_total
, (uint32_t)ctx
->stats
.batch_sysmem
,
161 (uint32_t)ctx
->stats
.batch_gmem
, (uint32_t)ctx
->stats
.batch_nondraw
,
162 (uint32_t)ctx
->stats
.batch_restore
);
169 fd_set_debug_callback(struct pipe_context
*pctx
,
170 const struct pipe_debug_callback
*cb
)
172 struct fd_context
*ctx
= fd_context(pctx
);
177 memset(&ctx
->debug
, 0, sizeof(ctx
->debug
));
180 /* TODO we could combine a few of these small buffers (solid_vbuf,
181 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
182 * save a tiny bit of memory
185 static struct pipe_resource
*
186 create_solid_vertexbuf(struct pipe_context
*pctx
)
188 static const float init_shader_const
[] = {
189 -1.000000, +1.000000, +1.000000,
190 +1.000000, -1.000000, +1.000000,
192 struct pipe_resource
*prsc
= pipe_buffer_create(pctx
->screen
,
193 PIPE_BIND_CUSTOM
, PIPE_USAGE_IMMUTABLE
, sizeof(init_shader_const
));
194 pipe_buffer_write(pctx
, prsc
, 0,
195 sizeof(init_shader_const
), init_shader_const
);
199 static struct pipe_resource
*
200 create_blit_texcoord_vertexbuf(struct pipe_context
*pctx
)
202 struct pipe_resource
*prsc
= pipe_buffer_create(pctx
->screen
,
203 PIPE_BIND_CUSTOM
, PIPE_USAGE_DYNAMIC
, 16);
208 fd_context_setup_common_vbos(struct fd_context
*ctx
)
210 struct pipe_context
*pctx
= &ctx
->base
;
212 ctx
->solid_vbuf
= create_solid_vertexbuf(pctx
);
213 ctx
->blit_texcoord_vbuf
= create_blit_texcoord_vertexbuf(pctx
);
215 /* setup solid_vbuf_state: */
216 ctx
->solid_vbuf_state
.vtx
= pctx
->create_vertex_elements_state(
217 pctx
, 1, (struct pipe_vertex_element
[]){{
218 .vertex_buffer_index
= 0,
220 .src_format
= PIPE_FORMAT_R32G32B32_FLOAT
,
222 ctx
->solid_vbuf_state
.vertexbuf
.count
= 1;
223 ctx
->solid_vbuf_state
.vertexbuf
.vb
[0].stride
= 12;
224 ctx
->solid_vbuf_state
.vertexbuf
.vb
[0].buffer
.resource
= ctx
->solid_vbuf
;
226 /* setup blit_vbuf_state: */
227 ctx
->blit_vbuf_state
.vtx
= pctx
->create_vertex_elements_state(
228 pctx
, 2, (struct pipe_vertex_element
[]){{
229 .vertex_buffer_index
= 0,
231 .src_format
= PIPE_FORMAT_R32G32_FLOAT
,
233 .vertex_buffer_index
= 1,
235 .src_format
= PIPE_FORMAT_R32G32B32_FLOAT
,
237 ctx
->blit_vbuf_state
.vertexbuf
.count
= 2;
238 ctx
->blit_vbuf_state
.vertexbuf
.vb
[0].stride
= 8;
239 ctx
->blit_vbuf_state
.vertexbuf
.vb
[0].buffer
.resource
= ctx
->blit_texcoord_vbuf
;
240 ctx
->blit_vbuf_state
.vertexbuf
.vb
[1].stride
= 12;
241 ctx
->blit_vbuf_state
.vertexbuf
.vb
[1].buffer
.resource
= ctx
->solid_vbuf
;
245 fd_context_cleanup_common_vbos(struct fd_context
*ctx
)
247 struct pipe_context
*pctx
= &ctx
->base
;
249 pctx
->delete_vertex_elements_state(pctx
, ctx
->solid_vbuf_state
.vtx
);
250 pctx
->delete_vertex_elements_state(pctx
, ctx
->blit_vbuf_state
.vtx
);
252 pipe_resource_reference(&ctx
->solid_vbuf
, NULL
);
253 pipe_resource_reference(&ctx
->blit_texcoord_vbuf
, NULL
);
256 struct pipe_context
*
257 fd_context_init(struct fd_context
*ctx
, struct pipe_screen
*pscreen
,
258 const uint8_t *primtypes
, void *priv
, unsigned flags
)
260 struct fd_screen
*screen
= fd_screen(pscreen
);
261 struct pipe_context
*pctx
;
264 ctx
->screen
= screen
;
265 ctx
->pipe
= fd_pipe_new(screen
->dev
, FD_PIPE_3D
);
267 ctx
->primtypes
= primtypes
;
268 ctx
->primtype_mask
= 0;
269 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++)
271 ctx
->primtype_mask
|= (1 << i
);
273 /* need some sane default in case state tracker doesn't
276 ctx
->sample_mask
= 0xffff;
279 pctx
->screen
= pscreen
;
281 pctx
->flush
= fd_context_flush
;
282 pctx
->emit_string_marker
= fd_emit_string_marker
;
283 pctx
->set_debug_callback
= fd_set_debug_callback
;
284 pctx
->create_fence_fd
= fd_create_fence_fd
;
285 pctx
->fence_server_sync
= fd_fence_server_sync
;
286 pctx
->texture_barrier
= fd_texture_barrier
;
288 pctx
->stream_uploader
= u_upload_create_default(pctx
);
289 if (!pctx
->stream_uploader
)
291 pctx
->const_uploader
= pctx
->stream_uploader
;
293 ctx
->batch
= fd_bc_alloc_batch(&screen
->batch_cache
, ctx
);
295 slab_create_child(&ctx
->transfer_pool
, &screen
->transfer_pool
);
298 ctx
->blit
= fd_blitter_blit
;
301 fd_resource_context_init(pctx
);
302 fd_query_context_init(pctx
);
303 fd_texture_init(pctx
);
306 ctx
->blitter
= util_blitter_create(pctx
);
310 ctx
->primconvert
= util_primconvert_create(pctx
, ctx
->primtype_mask
);
311 if (!ctx
->primconvert
)
314 list_inithead(&ctx
->hw_active_queries
);
315 list_inithead(&ctx
->acc_active_queries
);