freedreno: add core infrastructure support for MRTs
[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 struct fd_ringbuffer *next_rb(struct fd_context *ctx)
42 {
43 struct fd_ringbuffer *ring;
44 uint32_t ts;
45
46 /* grab next ringbuffer: */
47 ring = ctx->rings[(ctx->rings_idx++) % ARRAY_SIZE(ctx->rings)];
48
49 /* wait for new rb to be idle: */
50 ts = fd_ringbuffer_timestamp(ring);
51 if (ts) {
52 DBG("wait: %u", ts);
53 fd_pipe_wait(ctx->screen->pipe, ts);
54 }
55
56 fd_ringbuffer_reset(ring);
57
58 return ring;
59 }
60
61 static void
62 fd_context_next_rb(struct pipe_context *pctx)
63 {
64 struct fd_context *ctx = fd_context(pctx);
65 struct fd_ringbuffer *ring;
66
67 fd_ringmarker_del(ctx->draw_start);
68 fd_ringmarker_del(ctx->draw_end);
69
70 ring = next_rb(ctx);
71
72 ctx->draw_start = fd_ringmarker_new(ring);
73 ctx->draw_end = fd_ringmarker_new(ring);
74
75 fd_ringbuffer_set_parent(ring, NULL);
76 ctx->ring = ring;
77
78 fd_ringmarker_del(ctx->binning_start);
79 fd_ringmarker_del(ctx->binning_end);
80
81 ring = next_rb(ctx);
82
83 ctx->binning_start = fd_ringmarker_new(ring);
84 ctx->binning_end = fd_ringmarker_new(ring);
85
86 fd_ringbuffer_set_parent(ring, ctx->ring);
87 ctx->binning_ring = ring;
88 }
89
90 /* emit accumulated render cmds, needed for example if render target has
91 * changed, or for flush()
92 */
93 void
94 fd_context_render(struct pipe_context *pctx)
95 {
96 struct fd_context *ctx = fd_context(pctx);
97 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
98 int i;
99
100 DBG("needs_flush: %d", ctx->needs_flush);
101
102 if (!ctx->needs_flush)
103 return;
104
105 fd_gmem_render_tiles(ctx);
106
107 DBG("%p/%p/%p", ctx->ring->start, ctx->ring->cur, ctx->ring->end);
108
109 /* if size in dwords is more than half the buffer size, then wait and
110 * wrap around:
111 */
112 if ((ctx->ring->cur - ctx->ring->start) > ctx->ring->size/8)
113 fd_context_next_rb(pctx);
114
115 ctx->needs_flush = false;
116 ctx->cleared = ctx->partial_cleared = ctx->restore = ctx->resolve = 0;
117 ctx->gmem_reason = 0;
118 ctx->num_draws = 0;
119
120 for (i = 0; i < pfb->nr_cbufs; i++)
121 if (pfb->cbufs[i])
122 fd_resource(pfb->cbufs[i]->texture)->dirty = false;
123 if (pfb->zsbuf)
124 fd_resource(pfb->zsbuf->texture)->dirty = false;
125 }
126
127 static void
128 fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
129 unsigned flags)
130 {
131 fd_context_render(pctx);
132
133 if (fence)
134 *fence = fd_fence_create(pctx);
135 }
136
137 void
138 fd_context_destroy(struct pipe_context *pctx)
139 {
140 struct fd_context *ctx = fd_context(pctx);
141 unsigned i;
142
143 DBG("");
144
145 fd_prog_fini(pctx);
146 fd_hw_query_fini(pctx);
147
148 util_dynarray_fini(&ctx->draw_patches);
149
150 if (ctx->blitter)
151 util_blitter_destroy(ctx->blitter);
152
153 if (ctx->primconvert)
154 util_primconvert_destroy(ctx->primconvert);
155
156 util_slab_destroy(&ctx->transfer_pool);
157
158 fd_ringmarker_del(ctx->draw_start);
159 fd_ringmarker_del(ctx->draw_end);
160 fd_ringmarker_del(ctx->binning_start);
161 fd_ringmarker_del(ctx->binning_end);
162
163 for (i = 0; i < ARRAY_SIZE(ctx->rings); i++)
164 fd_ringbuffer_del(ctx->rings[i]);
165
166 for (i = 0; i < ARRAY_SIZE(ctx->pipe); i++) {
167 struct fd_vsc_pipe *pipe = &ctx->pipe[i];
168 if (!pipe->bo)
169 break;
170 fd_bo_del(pipe->bo);
171 }
172
173 fd_device_del(ctx->dev);
174
175 FREE(ctx);
176 }
177
178 struct pipe_context *
179 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
180 const uint8_t *primtypes, void *priv)
181 {
182 struct fd_screen *screen = fd_screen(pscreen);
183 struct pipe_context *pctx;
184 int i;
185
186 ctx->screen = screen;
187
188 ctx->primtypes = primtypes;
189 ctx->primtype_mask = 0;
190 for (i = 0; i < PIPE_PRIM_MAX; i++)
191 if (primtypes[i])
192 ctx->primtype_mask |= (1 << i);
193
194 /* need some sane default in case state tracker doesn't
195 * set some state:
196 */
197 ctx->sample_mask = 0xffff;
198
199 pctx = &ctx->base;
200 pctx->screen = pscreen;
201 pctx->priv = priv;
202 pctx->flush = fd_context_flush;
203
204 for (i = 0; i < ARRAY_SIZE(ctx->rings); i++) {
205 ctx->rings[i] = fd_ringbuffer_new(screen->pipe, 0x100000);
206 if (!ctx->rings[i])
207 goto fail;
208 }
209
210 fd_context_next_rb(pctx);
211 fd_reset_wfi(ctx);
212
213 util_dynarray_init(&ctx->draw_patches);
214
215 util_slab_create(&ctx->transfer_pool, sizeof(struct pipe_transfer),
216 16, UTIL_SLAB_SINGLETHREADED);
217
218 fd_draw_init(pctx);
219 fd_resource_context_init(pctx);
220 fd_query_context_init(pctx);
221 fd_texture_init(pctx);
222 fd_state_init(pctx);
223 fd_hw_query_init(pctx);
224
225 ctx->blitter = util_blitter_create(pctx);
226 if (!ctx->blitter)
227 goto fail;
228
229 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask);
230 if (!ctx->primconvert)
231 goto fail;
232
233 return pctx;
234
235 fail:
236 pctx->destroy(pctx);
237 return NULL;
238 }