freedreno: native fence fd support
[mesa.git] / src / gallium / drivers / freedreno / freedreno_context.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
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:
12 *
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
15 * Software.
16 *
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
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
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
41 static void
42 fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
43 unsigned flags)
44 {
45 struct fd_context *ctx = fd_context(pctx);
46
47 if (flags & PIPE_FLUSH_FENCE_FD)
48 ctx->batch->needs_out_fence_fd = true;
49
50 if (!ctx->screen->reorder) {
51 fd_batch_flush(ctx->batch, true);
52 } else {
53 fd_bc_flush(&ctx->screen->batch_cache, ctx);
54 }
55
56 if (fence)
57 fd_fence_ref(pctx->screen, fence, ctx->last_fence);
58 }
59
60 /**
61 * emit marker string as payload of a no-op packet, which can be
62 * decoded by cffdump.
63 */
64 static void
65 fd_emit_string_marker(struct pipe_context *pctx, const char *string, int len)
66 {
67 struct fd_context *ctx = fd_context(pctx);
68 struct fd_ringbuffer *ring;
69 const uint32_t *buf = (const void *)string;
70
71 if (!ctx->batch)
72 return;
73
74 ring = ctx->batch->draw;
75
76 /* max packet size is 0x3fff dwords: */
77 len = MIN2(len, 0x3fff * 4);
78
79 OUT_PKT3(ring, CP_NOP, align(len, 4) / 4);
80 while (len >= 4) {
81 OUT_RING(ring, *buf);
82 buf++;
83 len -= 4;
84 }
85
86 /* copy remainder bytes without reading past end of input string: */
87 if (len > 0) {
88 uint32_t w = 0;
89 memcpy(&w, buf, len);
90 OUT_RING(ring, w);
91 }
92 }
93
94 void
95 fd_context_destroy(struct pipe_context *pctx)
96 {
97 struct fd_context *ctx = fd_context(pctx);
98 unsigned i;
99
100 DBG("");
101
102 if (ctx->screen->reorder)
103 util_queue_destroy(&ctx->flush_queue);
104
105 fd_batch_reference(&ctx->batch, NULL); /* unref current batch */
106 fd_bc_invalidate_context(ctx);
107
108 fd_fence_ref(pctx->screen, &ctx->last_fence, NULL);
109
110 fd_prog_fini(pctx);
111 fd_hw_query_fini(pctx);
112
113 if (ctx->blitter)
114 util_blitter_destroy(ctx->blitter);
115
116 if (ctx->clear_rs_state)
117 pctx->delete_rasterizer_state(pctx, ctx->clear_rs_state);
118
119 if (ctx->primconvert)
120 util_primconvert_destroy(ctx->primconvert);
121
122 slab_destroy_child(&ctx->transfer_pool);
123
124 for (i = 0; i < ARRAY_SIZE(ctx->pipe); i++) {
125 struct fd_vsc_pipe *pipe = &ctx->pipe[i];
126 if (!pipe->bo)
127 break;
128 fd_bo_del(pipe->bo);
129 }
130
131 fd_device_del(ctx->dev);
132
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);
137 }
138
139 FREE(ctx);
140 }
141
142 static void
143 fd_set_debug_callback(struct pipe_context *pctx,
144 const struct pipe_debug_callback *cb)
145 {
146 struct fd_context *ctx = fd_context(pctx);
147
148 if (cb)
149 ctx->debug = *cb;
150 else
151 memset(&ctx->debug, 0, sizeof(ctx->debug));
152 }
153
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
157 */
158
159 static struct pipe_resource *
160 create_solid_vertexbuf(struct pipe_context *pctx)
161 {
162 static const float init_shader_const[] = {
163 -1.000000, +1.000000, +1.000000,
164 +1.000000, -1.000000, +1.000000,
165 };
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);
170 return prsc;
171 }
172
173 static struct pipe_resource *
174 create_blit_texcoord_vertexbuf(struct pipe_context *pctx)
175 {
176 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
177 PIPE_BIND_CUSTOM, PIPE_USAGE_DYNAMIC, 16);
178 return prsc;
179 }
180
181 void
182 fd_context_setup_common_vbos(struct fd_context *ctx)
183 {
184 struct pipe_context *pctx = &ctx->base;
185
186 ctx->solid_vbuf = create_solid_vertexbuf(pctx);
187 ctx->blit_texcoord_vbuf = create_blit_texcoord_vertexbuf(pctx);
188
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,
193 .src_offset = 0,
194 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
195 }});
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;
199
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,
204 .src_offset = 0,
205 .src_format = PIPE_FORMAT_R32G32_FLOAT,
206 }, {
207 .vertex_buffer_index = 1,
208 .src_offset = 0,
209 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
210 }});
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;
216 }
217
218 void
219 fd_context_cleanup_common_vbos(struct fd_context *ctx)
220 {
221 struct pipe_context *pctx = &ctx->base;
222
223 pctx->delete_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
224 pctx->delete_vertex_elements_state(pctx, ctx->blit_vbuf_state.vtx);
225
226 pipe_resource_reference(&ctx->solid_vbuf, NULL);
227 pipe_resource_reference(&ctx->blit_texcoord_vbuf, NULL);
228 }
229
230 struct pipe_context *
231 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
232 const uint8_t *primtypes, void *priv)
233 {
234 struct fd_screen *screen = fd_screen(pscreen);
235 struct pipe_context *pctx;
236 int i;
237
238 ctx->screen = screen;
239
240 ctx->primtypes = primtypes;
241 ctx->primtype_mask = 0;
242 for (i = 0; i < PIPE_PRIM_MAX; i++)
243 if (primtypes[i])
244 ctx->primtype_mask |= (1 << i);
245
246 /* need some sane default in case state tracker doesn't
247 * set some state:
248 */
249 ctx->sample_mask = 0xffff;
250
251 pctx = &ctx->base;
252 pctx->screen = pscreen;
253 pctx->priv = priv;
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;
259
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)..
263 */
264 if (!screen->reorder) {
265 ctx->batch = fd_bc_alloc_batch(&screen->batch_cache, ctx);
266 }
267
268 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
269
270 fd_draw_init(pctx);
271 fd_resource_context_init(pctx);
272 fd_query_context_init(pctx);
273 fd_texture_init(pctx);
274 fd_state_init(pctx);
275 fd_hw_query_init(pctx);
276
277 ctx->blitter = util_blitter_create(pctx);
278 if (!ctx->blitter)
279 goto fail;
280
281 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask);
282 if (!ctx->primconvert)
283 goto fail;
284
285 return pctx;
286
287 fail:
288 pctx->destroy(pctx);
289 return NULL;
290 }