freedreno: prepare for a3xx
[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_resource.h"
32 #include "freedreno_texture.h"
33 #include "freedreno_state.h"
34 #include "freedreno_gmem.h"
35 #include "freedreno_util.h"
36
37 /* there are two cases where we currently need to wait for render complete:
38 * 1) pctx->flush() .. since at the moment we have no way for DDX to sync
39 * the presentation blit with the 3d core
40 * 2) wrap-around for ringbuffer.. possibly we can do something more
41 * Intelligent here. Right now we need to ensure there is enough room
42 * at the end of the drawcmds in the cmdstream buffer for all the per-
43 * tile cmds. We do this the lamest way possible, by making the ringbuffer
44 * big, and flushing and resetting back to the beginning if we get too
45 * close to the end.
46 */
47 static void
48 fd_context_wait(struct pipe_context *pctx)
49 {
50 struct fd_context *ctx = fd_context(pctx);
51 uint32_t ts = fd_ringbuffer_timestamp(ctx->ring);
52
53 DBG("wait: %u", ts);
54
55 fd_pipe_wait(ctx->screen->pipe, ts);
56 fd_ringbuffer_reset(ctx->ring);
57 fd_ringmarker_mark(ctx->draw_start);
58 }
59
60 /* emit accumulated render cmds, needed for example if render target has
61 * changed, or for flush()
62 */
63 void
64 fd_context_render(struct pipe_context *pctx)
65 {
66 struct fd_context *ctx = fd_context(pctx);
67 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
68
69 DBG("needs_flush: %d", ctx->needs_flush);
70
71 if (!ctx->needs_flush)
72 return;
73
74 fd_gmem_render_tiles(pctx);
75
76 DBG("%p/%p/%p", ctx->ring->start, ctx->ring->cur, ctx->ring->end);
77
78 /* if size in dwords is more than half the buffer size, then wait and
79 * wrap around:
80 */
81 if ((ctx->ring->cur - ctx->ring->start) > ctx->ring->size/8)
82 fd_context_wait(pctx);
83
84 ctx->needs_flush = false;
85 ctx->cleared = ctx->restore = ctx->resolve = 0;
86
87 fd_resource(pfb->cbufs[0]->texture)->dirty = false;
88 if (pfb->zsbuf)
89 fd_resource(pfb->zsbuf->texture)->dirty = false;
90 }
91
92 static void
93 fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
94 unsigned flags)
95 {
96 DBG("fence=%p", fence);
97
98 #if 0
99 if (fence) {
100 fd_fence_ref(ctx->screen->fence.current,
101 (struct fd_fence **)fence);
102 }
103 #endif
104
105 fd_context_render(pctx);
106 }
107
108 void
109 fd_context_destroy(struct pipe_context *pctx)
110 {
111 struct fd_context *ctx = fd_context(pctx);
112
113 DBG("");
114
115 if (ctx->blitter)
116 util_blitter_destroy(ctx->blitter);
117
118 fd_ringmarker_del(ctx->draw_start);
119 fd_ringmarker_del(ctx->draw_end);
120 fd_ringbuffer_del(ctx->ring);
121
122 FREE(ctx);
123 }
124
125 struct pipe_context *
126 fd_context_init(struct fd_context *ctx,
127 struct pipe_screen *pscreen, void *priv)
128 {
129 struct fd_screen *screen = fd_screen(pscreen);
130 struct pipe_context *pctx;
131
132 ctx->screen = screen;
133
134 /* need some sane default in case state tracker doesn't
135 * set some state:
136 */
137 ctx->sample_mask = 0xffff;
138
139 pctx = &ctx->base;
140 pctx->screen = pscreen;
141 pctx->priv = priv;
142 pctx->flush = fd_context_flush;
143
144 ctx->ring = fd_ringbuffer_new(screen->pipe, 0x100000);
145 if (!ctx->ring)
146 goto fail;
147
148 ctx->draw_start = fd_ringmarker_new(ctx->ring);
149 ctx->draw_end = fd_ringmarker_new(ctx->ring);
150
151 util_slab_create(&ctx->transfer_pool, sizeof(struct pipe_transfer),
152 16, UTIL_SLAB_SINGLETHREADED);
153
154 fd_draw_init(pctx);
155 fd_resource_context_init(pctx);
156 fd_texture_init(pctx);
157 fd_state_init(pctx);
158
159 ctx->blitter = util_blitter_create(pctx);
160 if (!ctx->blitter)
161 goto fail;
162
163
164 return pctx;
165
166 fail:
167 pctx->destroy(pctx);
168 return NULL;
169 }