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"
42 fd_context_flush(struct pipe_context
*pctx
, struct pipe_fence_handle
**fence
,
45 struct fd_context
*ctx
= fd_context(pctx
);
47 if (flags
& PIPE_FLUSH_FENCE_FD
)
48 ctx
->batch
->needs_out_fence_fd
= true;
50 if (!ctx
->screen
->reorder
) {
51 fd_batch_flush(ctx
->batch
, true);
53 fd_bc_flush(&ctx
->screen
->batch_cache
, ctx
);
57 fd_fence_ref(pctx
->screen
, fence
, ctx
->last_fence
);
61 * emit marker string as payload of a no-op packet, which can be
65 fd_emit_string_marker(struct pipe_context
*pctx
, const char *string
, int len
)
67 struct fd_context
*ctx
= fd_context(pctx
);
68 struct fd_ringbuffer
*ring
;
69 const uint32_t *buf
= (const void *)string
;
74 ring
= ctx
->batch
->draw
;
76 /* max packet size is 0x3fff dwords: */
77 len
= MIN2(len
, 0x3fff * 4);
79 OUT_PKT3(ring
, CP_NOP
, align(len
, 4) / 4);
86 /* copy remainder bytes without reading past end of input string: */
95 fd_context_destroy(struct pipe_context
*pctx
)
97 struct fd_context
*ctx
= fd_context(pctx
);
102 if (ctx
->screen
->reorder
)
103 util_queue_destroy(&ctx
->flush_queue
);
105 fd_batch_reference(&ctx
->batch
, NULL
); /* unref current batch */
106 fd_bc_invalidate_context(ctx
);
108 fd_fence_ref(pctx
->screen
, &ctx
->last_fence
, NULL
);
111 fd_hw_query_fini(pctx
);
114 util_blitter_destroy(ctx
->blitter
);
116 if (ctx
->clear_rs_state
)
117 pctx
->delete_rasterizer_state(pctx
, ctx
->clear_rs_state
);
119 if (ctx
->primconvert
)
120 util_primconvert_destroy(ctx
->primconvert
);
122 slab_destroy_child(&ctx
->transfer_pool
);
124 for (i
= 0; i
< ARRAY_SIZE(ctx
->pipe
); i
++) {
125 struct fd_vsc_pipe
*pipe
= &ctx
->pipe
[i
];
131 fd_device_del(ctx
->dev
);
133 if (fd_mesa_debug
& (FD_DBG_BSTAT
| FD_DBG_MSGS
)) {
134 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_restore=%u\n",
135 (uint32_t)ctx
->stats
.batch_total
, (uint32_t)ctx
->stats
.batch_sysmem
,
136 (uint32_t)ctx
->stats
.batch_gmem
, (uint32_t)ctx
->stats
.batch_restore
);
143 fd_set_debug_callback(struct pipe_context
*pctx
,
144 const struct pipe_debug_callback
*cb
)
146 struct fd_context
*ctx
= fd_context(pctx
);
151 memset(&ctx
->debug
, 0, sizeof(ctx
->debug
));
154 /* TODO we could combine a few of these small buffers (solid_vbuf,
155 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
156 * save a tiny bit of memory
159 static struct pipe_resource
*
160 create_solid_vertexbuf(struct pipe_context
*pctx
)
162 static const float init_shader_const
[] = {
163 -1.000000, +1.000000, +1.000000,
164 +1.000000, -1.000000, +1.000000,
166 struct pipe_resource
*prsc
= pipe_buffer_create(pctx
->screen
,
167 PIPE_BIND_CUSTOM
, PIPE_USAGE_IMMUTABLE
, sizeof(init_shader_const
));
168 pipe_buffer_write(pctx
, prsc
, 0,
169 sizeof(init_shader_const
), init_shader_const
);
173 static struct pipe_resource
*
174 create_blit_texcoord_vertexbuf(struct pipe_context
*pctx
)
176 struct pipe_resource
*prsc
= pipe_buffer_create(pctx
->screen
,
177 PIPE_BIND_CUSTOM
, PIPE_USAGE_DYNAMIC
, 16);
182 fd_context_setup_common_vbos(struct fd_context
*ctx
)
184 struct pipe_context
*pctx
= &ctx
->base
;
186 ctx
->solid_vbuf
= create_solid_vertexbuf(pctx
);
187 ctx
->blit_texcoord_vbuf
= create_blit_texcoord_vertexbuf(pctx
);
189 /* setup solid_vbuf_state: */
190 ctx
->solid_vbuf_state
.vtx
= pctx
->create_vertex_elements_state(
191 pctx
, 1, (struct pipe_vertex_element
[]){{
192 .vertex_buffer_index
= 0,
194 .src_format
= PIPE_FORMAT_R32G32B32_FLOAT
,
196 ctx
->solid_vbuf_state
.vertexbuf
.count
= 1;
197 ctx
->solid_vbuf_state
.vertexbuf
.vb
[0].stride
= 12;
198 ctx
->solid_vbuf_state
.vertexbuf
.vb
[0].buffer
= ctx
->solid_vbuf
;
200 /* setup blit_vbuf_state: */
201 ctx
->blit_vbuf_state
.vtx
= pctx
->create_vertex_elements_state(
202 pctx
, 2, (struct pipe_vertex_element
[]){{
203 .vertex_buffer_index
= 0,
205 .src_format
= PIPE_FORMAT_R32G32_FLOAT
,
207 .vertex_buffer_index
= 1,
209 .src_format
= PIPE_FORMAT_R32G32B32_FLOAT
,
211 ctx
->blit_vbuf_state
.vertexbuf
.count
= 2;
212 ctx
->blit_vbuf_state
.vertexbuf
.vb
[0].stride
= 8;
213 ctx
->blit_vbuf_state
.vertexbuf
.vb
[0].buffer
= ctx
->blit_texcoord_vbuf
;
214 ctx
->blit_vbuf_state
.vertexbuf
.vb
[1].stride
= 12;
215 ctx
->blit_vbuf_state
.vertexbuf
.vb
[1].buffer
= ctx
->solid_vbuf
;
219 fd_context_cleanup_common_vbos(struct fd_context
*ctx
)
221 struct pipe_context
*pctx
= &ctx
->base
;
223 pctx
->delete_vertex_elements_state(pctx
, ctx
->solid_vbuf_state
.vtx
);
224 pctx
->delete_vertex_elements_state(pctx
, ctx
->blit_vbuf_state
.vtx
);
226 pipe_resource_reference(&ctx
->solid_vbuf
, NULL
);
227 pipe_resource_reference(&ctx
->blit_texcoord_vbuf
, NULL
);
230 struct pipe_context
*
231 fd_context_init(struct fd_context
*ctx
, struct pipe_screen
*pscreen
,
232 const uint8_t *primtypes
, void *priv
)
234 struct fd_screen
*screen
= fd_screen(pscreen
);
235 struct pipe_context
*pctx
;
238 ctx
->screen
= screen
;
240 ctx
->primtypes
= primtypes
;
241 ctx
->primtype_mask
= 0;
242 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++)
244 ctx
->primtype_mask
|= (1 << i
);
246 /* need some sane default in case state tracker doesn't
249 ctx
->sample_mask
= 0xffff;
252 pctx
->screen
= pscreen
;
254 pctx
->flush
= fd_context_flush
;
255 pctx
->emit_string_marker
= fd_emit_string_marker
;
256 pctx
->set_debug_callback
= fd_set_debug_callback
;
257 pctx
->create_fence_fd
= fd_create_fence_fd
;
258 pctx
->fence_server_sync
= fd_fence_server_sync
;
260 /* TODO what about compute? Ideally it creates it's own independent
261 * batches per compute job (since it isn't using tiling, so no point
262 * in getting involved with the re-ordering madness)..
264 if (!screen
->reorder
) {
265 ctx
->batch
= fd_bc_alloc_batch(&screen
->batch_cache
, ctx
);
268 slab_create_child(&ctx
->transfer_pool
, &screen
->transfer_pool
);
271 fd_resource_context_init(pctx
);
272 fd_query_context_init(pctx
);
273 fd_texture_init(pctx
);
275 fd_hw_query_init(pctx
);
277 ctx
->blitter
= util_blitter_create(pctx
);
281 ctx
->primconvert
= util_primconvert_create(pctx
, ctx
->primtype_mask
);
282 if (!ctx
->primconvert
)