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