96c83844193104129128d5d3b685c89aa15a90b4
[mesa.git] / src / gallium / drivers / freedreno / freedreno_context.c
1 /*
2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
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"
40
41 static void
42 fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fencep,
43 unsigned flags)
44 {
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);
49
50 DBG("%p: flush: flags=%x\n", ctx->batch, flags);
51
52 /* In some sequence of events, we can end up with a last_fence that is
53 * not an "fd" fence, which results in eglDupNativeFenceFDANDROID()
54 * errors.
55 *
56 */
57 if (flags & PIPE_FLUSH_FENCE_FD)
58 fd_fence_ref(&ctx->last_fence, NULL);
59
60 /* if no rendering since last flush, ie. app just decided it needed
61 * a fence, re-use the last one:
62 */
63 if (ctx->last_fence) {
64 fd_fence_ref(&fence, ctx->last_fence);
65 goto out;
66 }
67
68 if (!batch)
69 return;
70
71 /* Take a ref to the batch's fence (batch can be unref'd when flushed: */
72 fd_fence_ref(&fence, batch->fence);
73
74 if (flags & PIPE_FLUSH_FENCE_FD)
75 batch->needs_out_fence_fd = true;
76
77 if (!ctx->screen->reorder) {
78 fd_batch_flush(batch, true);
79 } else if (flags & PIPE_FLUSH_DEFERRED) {
80 fd_bc_flush_deferred(&ctx->screen->batch_cache, ctx);
81 } else {
82 fd_bc_flush(&ctx->screen->batch_cache, ctx);
83 }
84
85 out:
86 if (fencep)
87 fd_fence_ref(fencep, fence);
88
89 fd_fence_ref(&ctx->last_fence, fence);
90
91 fd_fence_ref(&fence, NULL);
92 }
93
94 static void
95 fd_texture_barrier(struct pipe_context *pctx, unsigned flags)
96 {
97 if (flags == PIPE_TEXTURE_BARRIER_FRAMEBUFFER) {
98 struct fd_context *ctx = fd_context(pctx);
99
100 if (ctx->framebuffer_barrier) {
101 ctx->framebuffer_barrier(ctx);
102 return;
103 }
104 }
105
106 /* On devices that could sample from GMEM we could possibly do better.
107 * Or if we knew that we were doing GMEM bypass we could just emit a
108 * cache flush, perhaps? But we don't know if future draws would cause
109 * us to use GMEM, and a flush in bypass isn't the end of the world.
110 */
111 fd_context_flush(pctx, NULL, 0);
112 }
113
114 static void
115 fd_memory_barrier(struct pipe_context *pctx, unsigned flags)
116 {
117 if (!(flags & ~PIPE_BARRIER_UPDATE))
118 return;
119
120 fd_context_flush(pctx, NULL, 0);
121 /* TODO do we need to check for persistently mapped buffers and fd_bo_cpu_prep()?? */
122 }
123
124 /**
125 * emit marker string as payload of a no-op packet, which can be
126 * decoded by cffdump.
127 */
128 static void
129 fd_emit_string_marker(struct pipe_context *pctx, const char *string, int len)
130 {
131 struct fd_context *ctx = fd_context(pctx);
132 struct fd_ringbuffer *ring;
133 const uint32_t *buf = (const void *)string;
134
135 if (!ctx->batch)
136 return;
137
138 ctx->batch->needs_flush = true;
139
140 ring = ctx->batch->draw;
141
142 /* max packet size is 0x3fff dwords: */
143 len = MIN2(len, 0x3fff * 4);
144
145 if (ctx->screen->gpu_id >= 500)
146 OUT_PKT7(ring, CP_NOP, align(len, 4) / 4);
147 else
148 OUT_PKT3(ring, CP_NOP, align(len, 4) / 4);
149 while (len >= 4) {
150 OUT_RING(ring, *buf);
151 buf++;
152 len -= 4;
153 }
154
155 /* copy remainder bytes without reading past end of input string: */
156 if (len > 0) {
157 uint32_t w = 0;
158 memcpy(&w, buf, len);
159 OUT_RING(ring, w);
160 }
161 }
162
163 void
164 fd_context_destroy(struct pipe_context *pctx)
165 {
166 struct fd_context *ctx = fd_context(pctx);
167 unsigned i;
168
169 DBG("");
170
171 fd_fence_ref(&ctx->last_fence, NULL);
172
173 if (ctx->screen->reorder && util_queue_is_initialized(&ctx->flush_queue))
174 util_queue_destroy(&ctx->flush_queue);
175
176 util_copy_framebuffer_state(&ctx->framebuffer, NULL);
177 fd_batch_reference(&ctx->batch, NULL); /* unref current batch */
178 fd_bc_invalidate_context(ctx);
179
180 fd_prog_fini(pctx);
181
182 if (ctx->blitter)
183 util_blitter_destroy(ctx->blitter);
184
185 if (pctx->stream_uploader)
186 u_upload_destroy(pctx->stream_uploader);
187
188 if (ctx->clear_rs_state)
189 pctx->delete_rasterizer_state(pctx, ctx->clear_rs_state);
190
191 if (ctx->primconvert)
192 util_primconvert_destroy(ctx->primconvert);
193
194 slab_destroy_child(&ctx->transfer_pool);
195
196 for (i = 0; i < ARRAY_SIZE(ctx->vsc_pipe_bo); i++) {
197 if (!ctx->vsc_pipe_bo[i])
198 break;
199 fd_bo_del(ctx->vsc_pipe_bo[i]);
200 }
201
202 fd_device_del(ctx->dev);
203 fd_pipe_del(ctx->pipe);
204
205 if (fd_mesa_debug & (FD_DBG_BSTAT | FD_DBG_MSGS)) {
206 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_nondraw=%u, batch_restore=%u\n",
207 (uint32_t)ctx->stats.batch_total, (uint32_t)ctx->stats.batch_sysmem,
208 (uint32_t)ctx->stats.batch_gmem, (uint32_t)ctx->stats.batch_nondraw,
209 (uint32_t)ctx->stats.batch_restore);
210 }
211 }
212
213 static void
214 fd_set_debug_callback(struct pipe_context *pctx,
215 const struct pipe_debug_callback *cb)
216 {
217 struct fd_context *ctx = fd_context(pctx);
218
219 if (cb)
220 ctx->debug = *cb;
221 else
222 memset(&ctx->debug, 0, sizeof(ctx->debug));
223 }
224
225 static uint32_t
226 fd_get_reset_count(struct fd_context *ctx, bool per_context)
227 {
228 uint64_t val;
229 enum fd_param_id param =
230 per_context ? FD_CTX_FAULTS : FD_GLOBAL_FAULTS;
231 int ret = fd_pipe_get_param(ctx->pipe, param, &val);
232 debug_assert(!ret);
233 return val;
234 }
235
236 static enum pipe_reset_status
237 fd_get_device_reset_status(struct pipe_context *pctx)
238 {
239 struct fd_context *ctx = fd_context(pctx);
240 int context_faults = fd_get_reset_count(ctx, true);
241 int global_faults = fd_get_reset_count(ctx, false);
242 enum pipe_reset_status status;
243
244 if (context_faults != ctx->context_reset_count) {
245 status = PIPE_GUILTY_CONTEXT_RESET;
246 } else if (global_faults != ctx->global_reset_count) {
247 status = PIPE_INNOCENT_CONTEXT_RESET;
248 } else {
249 status = PIPE_NO_RESET;
250 }
251
252 ctx->context_reset_count = context_faults;
253 ctx->global_reset_count = global_faults;
254
255 return status;
256 }
257
258 /* TODO we could combine a few of these small buffers (solid_vbuf,
259 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
260 * save a tiny bit of memory
261 */
262
263 static struct pipe_resource *
264 create_solid_vertexbuf(struct pipe_context *pctx)
265 {
266 static const float init_shader_const[] = {
267 -1.000000, +1.000000, +1.000000,
268 +1.000000, -1.000000, +1.000000,
269 };
270 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
271 PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE, sizeof(init_shader_const));
272 pipe_buffer_write(pctx, prsc, 0,
273 sizeof(init_shader_const), init_shader_const);
274 return prsc;
275 }
276
277 static struct pipe_resource *
278 create_blit_texcoord_vertexbuf(struct pipe_context *pctx)
279 {
280 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
281 PIPE_BIND_CUSTOM, PIPE_USAGE_DYNAMIC, 16);
282 return prsc;
283 }
284
285 void
286 fd_context_setup_common_vbos(struct fd_context *ctx)
287 {
288 struct pipe_context *pctx = &ctx->base;
289
290 ctx->solid_vbuf = create_solid_vertexbuf(pctx);
291 ctx->blit_texcoord_vbuf = create_blit_texcoord_vertexbuf(pctx);
292
293 /* setup solid_vbuf_state: */
294 ctx->solid_vbuf_state.vtx = pctx->create_vertex_elements_state(
295 pctx, 1, (struct pipe_vertex_element[]){{
296 .vertex_buffer_index = 0,
297 .src_offset = 0,
298 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
299 }});
300 ctx->solid_vbuf_state.vertexbuf.count = 1;
301 ctx->solid_vbuf_state.vertexbuf.vb[0].stride = 12;
302 ctx->solid_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->solid_vbuf;
303
304 /* setup blit_vbuf_state: */
305 ctx->blit_vbuf_state.vtx = pctx->create_vertex_elements_state(
306 pctx, 2, (struct pipe_vertex_element[]){{
307 .vertex_buffer_index = 0,
308 .src_offset = 0,
309 .src_format = PIPE_FORMAT_R32G32_FLOAT,
310 }, {
311 .vertex_buffer_index = 1,
312 .src_offset = 0,
313 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
314 }});
315 ctx->blit_vbuf_state.vertexbuf.count = 2;
316 ctx->blit_vbuf_state.vertexbuf.vb[0].stride = 8;
317 ctx->blit_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->blit_texcoord_vbuf;
318 ctx->blit_vbuf_state.vertexbuf.vb[1].stride = 12;
319 ctx->blit_vbuf_state.vertexbuf.vb[1].buffer.resource = ctx->solid_vbuf;
320 }
321
322 void
323 fd_context_cleanup_common_vbos(struct fd_context *ctx)
324 {
325 struct pipe_context *pctx = &ctx->base;
326
327 pctx->delete_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
328 pctx->delete_vertex_elements_state(pctx, ctx->blit_vbuf_state.vtx);
329
330 pipe_resource_reference(&ctx->solid_vbuf, NULL);
331 pipe_resource_reference(&ctx->blit_texcoord_vbuf, NULL);
332 }
333
334 struct pipe_context *
335 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
336 const uint8_t *primtypes, void *priv, unsigned flags)
337 {
338 struct fd_screen *screen = fd_screen(pscreen);
339 struct pipe_context *pctx;
340 unsigned prio = 1;
341 int i;
342
343 /* lower numerical value == higher priority: */
344 if (fd_mesa_debug & FD_DBG_HIPRIO)
345 prio = 0;
346 else if (flags & PIPE_CONTEXT_HIGH_PRIORITY)
347 prio = 0;
348 else if (flags & PIPE_CONTEXT_LOW_PRIORITY)
349 prio = 2;
350
351 ctx->screen = screen;
352 ctx->pipe = fd_pipe_new2(screen->dev, FD_PIPE_3D, prio);
353
354 if (fd_device_version(screen->dev) >= FD_VERSION_ROBUSTNESS) {
355 ctx->context_reset_count = fd_get_reset_count(ctx, true);
356 ctx->global_reset_count = fd_get_reset_count(ctx, false);
357 }
358
359 ctx->primtypes = primtypes;
360 ctx->primtype_mask = 0;
361 for (i = 0; i < PIPE_PRIM_MAX; i++)
362 if (primtypes[i])
363 ctx->primtype_mask |= (1 << i);
364
365 /* need some sane default in case state tracker doesn't
366 * set some state:
367 */
368 ctx->sample_mask = 0xffff;
369
370 pctx = &ctx->base;
371 pctx->screen = pscreen;
372 pctx->priv = priv;
373 pctx->flush = fd_context_flush;
374 pctx->emit_string_marker = fd_emit_string_marker;
375 pctx->set_debug_callback = fd_set_debug_callback;
376 pctx->get_device_reset_status = fd_get_device_reset_status;
377 pctx->create_fence_fd = fd_create_fence_fd;
378 pctx->fence_server_sync = fd_fence_server_sync;
379 pctx->texture_barrier = fd_texture_barrier;
380 pctx->memory_barrier = fd_memory_barrier;
381
382 pctx->stream_uploader = u_upload_create_default(pctx);
383 if (!pctx->stream_uploader)
384 goto fail;
385 pctx->const_uploader = pctx->stream_uploader;
386
387 if (!ctx->screen->reorder)
388 ctx->batch = fd_bc_alloc_batch(&screen->batch_cache, ctx, false);
389
390 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
391
392 fd_draw_init(pctx);
393 fd_resource_context_init(pctx);
394 fd_query_context_init(pctx);
395 fd_texture_init(pctx);
396 fd_state_init(pctx);
397
398 ctx->blitter = util_blitter_create(pctx);
399 if (!ctx->blitter)
400 goto fail;
401
402 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask);
403 if (!ctx->primconvert)
404 goto fail;
405
406 list_inithead(&ctx->hw_active_queries);
407 list_inithead(&ctx->acc_active_queries);
408
409 return pctx;
410
411 fail:
412 pctx->destroy(pctx);
413 return NULL;
414 }