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_draw.h"
31 #include "freedreno_fence.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"
43 fd_context_flush(struct pipe_context
*pctx
, struct pipe_fence_handle
**fence
,
46 struct fd_context
*ctx
= fd_context(pctx
);
48 if (flags
& PIPE_FLUSH_FENCE_FD
)
49 ctx
->batch
->needs_out_fence_fd
= true;
51 if (!ctx
->screen
->reorder
) {
52 fd_batch_flush(ctx
->batch
, true);
54 fd_bc_flush(&ctx
->screen
->batch_cache
, ctx
);
58 /* if there hasn't been any rendering submitted yet, we might not
59 * have actually created a fence
61 if (!ctx
->last_fence
|| ctx
->batch
->needs_out_fence_fd
) {
62 ctx
->batch
->needs_flush
= true;
63 fd_gmem_render_noop(ctx
->batch
);
64 fd_batch_reset(ctx
->batch
);
66 fd_fence_ref(pctx
->screen
, fence
, ctx
->last_fence
);
71 * emit marker string as payload of a no-op packet, which can be
75 fd_emit_string_marker(struct pipe_context
*pctx
, const char *string
, int len
)
77 struct fd_context
*ctx
= fd_context(pctx
);
78 struct fd_ringbuffer
*ring
;
79 const uint32_t *buf
= (const void *)string
;
84 ring
= ctx
->batch
->draw
;
86 /* max packet size is 0x3fff dwords: */
87 len
= MIN2(len
, 0x3fff * 4);
89 if (ctx
->screen
->gpu_id
>= 500)
90 OUT_PKT7(ring
, CP_NOP
, align(len
, 4) / 4);
92 OUT_PKT3(ring
, CP_NOP
, align(len
, 4) / 4);
99 /* copy remainder bytes without reading past end of input string: */
102 memcpy(&w
, buf
, len
);
108 fd_context_destroy(struct pipe_context
*pctx
)
110 struct fd_context
*ctx
= fd_context(pctx
);
115 if (ctx
->screen
->reorder
&& util_queue_is_initialized(&ctx
->flush_queue
))
116 util_queue_destroy(&ctx
->flush_queue
);
118 fd_batch_reference(&ctx
->batch
, NULL
); /* unref current batch */
119 fd_bc_invalidate_context(ctx
);
121 fd_fence_ref(pctx
->screen
, &ctx
->last_fence
, NULL
);
126 util_blitter_destroy(ctx
->blitter
);
128 if (pctx
->stream_uploader
)
129 u_upload_destroy(pctx
->stream_uploader
);
131 if (ctx
->clear_rs_state
)
132 pctx
->delete_rasterizer_state(pctx
, ctx
->clear_rs_state
);
134 if (ctx
->primconvert
)
135 util_primconvert_destroy(ctx
->primconvert
);
137 slab_destroy_child(&ctx
->transfer_pool
);
139 for (i
= 0; i
< ARRAY_SIZE(ctx
->pipe
); i
++) {
140 struct fd_vsc_pipe
*pipe
= &ctx
->pipe
[i
];
146 fd_device_del(ctx
->dev
);
148 if (fd_mesa_debug
& (FD_DBG_BSTAT
| FD_DBG_MSGS
)) {
149 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_restore=%u\n",
150 (uint32_t)ctx
->stats
.batch_total
, (uint32_t)ctx
->stats
.batch_sysmem
,
151 (uint32_t)ctx
->stats
.batch_gmem
, (uint32_t)ctx
->stats
.batch_restore
);
158 fd_set_debug_callback(struct pipe_context
*pctx
,
159 const struct pipe_debug_callback
*cb
)
161 struct fd_context
*ctx
= fd_context(pctx
);
166 memset(&ctx
->debug
, 0, sizeof(ctx
->debug
));
169 /* TODO we could combine a few of these small buffers (solid_vbuf,
170 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
171 * save a tiny bit of memory
174 static struct pipe_resource
*
175 create_solid_vertexbuf(struct pipe_context
*pctx
)
177 static const float init_shader_const
[] = {
178 -1.000000, +1.000000, +1.000000,
179 +1.000000, -1.000000, +1.000000,
181 struct pipe_resource
*prsc
= pipe_buffer_create(pctx
->screen
,
182 PIPE_BIND_CUSTOM
, PIPE_USAGE_IMMUTABLE
, sizeof(init_shader_const
));
183 pipe_buffer_write(pctx
, prsc
, 0,
184 sizeof(init_shader_const
), init_shader_const
);
188 static struct pipe_resource
*
189 create_blit_texcoord_vertexbuf(struct pipe_context
*pctx
)
191 struct pipe_resource
*prsc
= pipe_buffer_create(pctx
->screen
,
192 PIPE_BIND_CUSTOM
, PIPE_USAGE_DYNAMIC
, 16);
197 fd_context_setup_common_vbos(struct fd_context
*ctx
)
199 struct pipe_context
*pctx
= &ctx
->base
;
201 ctx
->solid_vbuf
= create_solid_vertexbuf(pctx
);
202 ctx
->blit_texcoord_vbuf
= create_blit_texcoord_vertexbuf(pctx
);
204 /* setup solid_vbuf_state: */
205 ctx
->solid_vbuf_state
.vtx
= pctx
->create_vertex_elements_state(
206 pctx
, 1, (struct pipe_vertex_element
[]){{
207 .vertex_buffer_index
= 0,
209 .src_format
= PIPE_FORMAT_R32G32B32_FLOAT
,
211 ctx
->solid_vbuf_state
.vertexbuf
.count
= 1;
212 ctx
->solid_vbuf_state
.vertexbuf
.vb
[0].stride
= 12;
213 ctx
->solid_vbuf_state
.vertexbuf
.vb
[0].buffer
.resource
= ctx
->solid_vbuf
;
215 /* setup blit_vbuf_state: */
216 ctx
->blit_vbuf_state
.vtx
= pctx
->create_vertex_elements_state(
217 pctx
, 2, (struct pipe_vertex_element
[]){{
218 .vertex_buffer_index
= 0,
220 .src_format
= PIPE_FORMAT_R32G32_FLOAT
,
222 .vertex_buffer_index
= 1,
224 .src_format
= PIPE_FORMAT_R32G32B32_FLOAT
,
226 ctx
->blit_vbuf_state
.vertexbuf
.count
= 2;
227 ctx
->blit_vbuf_state
.vertexbuf
.vb
[0].stride
= 8;
228 ctx
->blit_vbuf_state
.vertexbuf
.vb
[0].buffer
.resource
= ctx
->blit_texcoord_vbuf
;
229 ctx
->blit_vbuf_state
.vertexbuf
.vb
[1].stride
= 12;
230 ctx
->blit_vbuf_state
.vertexbuf
.vb
[1].buffer
.resource
= ctx
->solid_vbuf
;
234 fd_context_cleanup_common_vbos(struct fd_context
*ctx
)
236 struct pipe_context
*pctx
= &ctx
->base
;
238 pctx
->delete_vertex_elements_state(pctx
, ctx
->solid_vbuf_state
.vtx
);
239 pctx
->delete_vertex_elements_state(pctx
, ctx
->blit_vbuf_state
.vtx
);
241 pipe_resource_reference(&ctx
->solid_vbuf
, NULL
);
242 pipe_resource_reference(&ctx
->blit_texcoord_vbuf
, NULL
);
245 struct pipe_context
*
246 fd_context_init(struct fd_context
*ctx
, struct pipe_screen
*pscreen
,
247 const uint8_t *primtypes
, void *priv
)
249 struct fd_screen
*screen
= fd_screen(pscreen
);
250 struct pipe_context
*pctx
;
253 ctx
->screen
= screen
;
255 ctx
->primtypes
= primtypes
;
256 ctx
->primtype_mask
= 0;
257 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++)
259 ctx
->primtype_mask
|= (1 << i
);
261 /* need some sane default in case state tracker doesn't
264 ctx
->sample_mask
= 0xffff;
267 pctx
->screen
= pscreen
;
269 pctx
->flush
= fd_context_flush
;
270 pctx
->emit_string_marker
= fd_emit_string_marker
;
271 pctx
->set_debug_callback
= fd_set_debug_callback
;
272 pctx
->create_fence_fd
= fd_create_fence_fd
;
273 pctx
->fence_server_sync
= fd_fence_server_sync
;
275 pctx
->stream_uploader
= u_upload_create_default(pctx
);
276 if (!pctx
->stream_uploader
)
278 pctx
->const_uploader
= pctx
->stream_uploader
;
280 /* TODO what about compute? Ideally it creates it's own independent
281 * batches per compute job (since it isn't using tiling, so no point
282 * in getting involved with the re-ordering madness)..
284 if (!screen
->reorder
) {
285 ctx
->batch
= fd_bc_alloc_batch(&screen
->batch_cache
, ctx
);
288 slab_create_child(&ctx
->transfer_pool
, &screen
->transfer_pool
);
291 fd_resource_context_init(pctx
);
292 fd_query_context_init(pctx
);
293 fd_texture_init(pctx
);
296 ctx
->blitter
= util_blitter_create(pctx
);
300 ctx
->primconvert
= util_primconvert_create(pctx
, ctx
->primtype_mask
);
301 if (!ctx
->primconvert
)
304 list_inithead(&ctx
->hw_active_queries
);
305 list_inithead(&ctx
->acc_active_queries
);