gallium: switch drivers to the slab allocator in src/util
[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 uint32_t timestamp;
47
48 if (!ctx->screen->reorder) {
49 struct fd_batch *batch = NULL;
50 fd_batch_reference(&batch, ctx->batch);
51 fd_batch_flush(batch, true);
52 timestamp = fd_ringbuffer_timestamp(batch->gmem);
53 fd_batch_reference(&batch, NULL);
54 } else {
55 timestamp = fd_bc_flush(&ctx->screen->batch_cache, ctx);
56 }
57
58 if (fence) {
59 fd_screen_fence_ref(pctx->screen, fence, NULL);
60 *fence = fd_fence_create(pctx, timestamp);
61 }
62 }
63
64 /**
65 * emit marker string as payload of a no-op packet, which can be
66 * decoded by cffdump.
67 */
68 static void
69 fd_emit_string_marker(struct pipe_context *pctx, const char *string, int len)
70 {
71 struct fd_context *ctx = fd_context(pctx);
72 struct fd_ringbuffer *ring;
73 const uint32_t *buf = (const void *)string;
74
75 if (!ctx->batch)
76 return;
77
78 ring = ctx->batch->draw;
79
80 /* max packet size is 0x3fff dwords: */
81 len = MIN2(len, 0x3fff * 4);
82
83 OUT_PKT3(ring, CP_NOP, align(len, 4) / 4);
84 while (len >= 4) {
85 OUT_RING(ring, *buf);
86 buf++;
87 len -= 4;
88 }
89
90 /* copy remainder bytes without reading past end of input string: */
91 if (len > 0) {
92 uint32_t w = 0;
93 memcpy(&w, buf, len);
94 OUT_RING(ring, w);
95 }
96 }
97
98 void
99 fd_context_destroy(struct pipe_context *pctx)
100 {
101 struct fd_context *ctx = fd_context(pctx);
102 unsigned i;
103
104 DBG("");
105
106 if (ctx->screen->reorder)
107 util_queue_destroy(&ctx->flush_queue);
108
109 fd_batch_reference(&ctx->batch, NULL); /* unref current batch */
110 fd_bc_invalidate_context(ctx);
111
112 fd_prog_fini(pctx);
113 fd_hw_query_fini(pctx);
114
115 if (ctx->blitter)
116 util_blitter_destroy(ctx->blitter);
117
118 if (ctx->clear_rs_state)
119 pctx->delete_rasterizer_state(pctx, ctx->clear_rs_state);
120
121 if (ctx->primconvert)
122 util_primconvert_destroy(ctx->primconvert);
123
124 slab_destroy(&ctx->transfer_pool);
125
126 for (i = 0; i < ARRAY_SIZE(ctx->pipe); i++) {
127 struct fd_vsc_pipe *pipe = &ctx->pipe[i];
128 if (!pipe->bo)
129 break;
130 fd_bo_del(pipe->bo);
131 }
132
133 fd_device_del(ctx->dev);
134
135 if (fd_mesa_debug & (FD_DBG_BSTAT | FD_DBG_MSGS)) {
136 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_restore=%u\n",
137 (uint32_t)ctx->stats.batch_total, (uint32_t)ctx->stats.batch_sysmem,
138 (uint32_t)ctx->stats.batch_gmem, (uint32_t)ctx->stats.batch_restore);
139 }
140
141 FREE(ctx);
142 }
143
144 static void
145 fd_set_debug_callback(struct pipe_context *pctx,
146 const struct pipe_debug_callback *cb)
147 {
148 struct fd_context *ctx = fd_context(pctx);
149
150 if (cb)
151 ctx->debug = *cb;
152 else
153 memset(&ctx->debug, 0, sizeof(ctx->debug));
154 }
155
156 /* TODO we could combine a few of these small buffers (solid_vbuf,
157 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
158 * save a tiny bit of memory
159 */
160
161 static struct pipe_resource *
162 create_solid_vertexbuf(struct pipe_context *pctx)
163 {
164 static const float init_shader_const[] = {
165 -1.000000, +1.000000, +1.000000,
166 +1.000000, -1.000000, +1.000000,
167 };
168 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
169 PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE, sizeof(init_shader_const));
170 pipe_buffer_write(pctx, prsc, 0,
171 sizeof(init_shader_const), init_shader_const);
172 return prsc;
173 }
174
175 static struct pipe_resource *
176 create_blit_texcoord_vertexbuf(struct pipe_context *pctx)
177 {
178 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
179 PIPE_BIND_CUSTOM, PIPE_USAGE_DYNAMIC, 16);
180 return prsc;
181 }
182
183 void
184 fd_context_setup_common_vbos(struct fd_context *ctx)
185 {
186 struct pipe_context *pctx = &ctx->base;
187
188 ctx->solid_vbuf = create_solid_vertexbuf(pctx);
189 ctx->blit_texcoord_vbuf = create_blit_texcoord_vertexbuf(pctx);
190
191 /* setup solid_vbuf_state: */
192 ctx->solid_vbuf_state.vtx = pctx->create_vertex_elements_state(
193 pctx, 1, (struct pipe_vertex_element[]){{
194 .vertex_buffer_index = 0,
195 .src_offset = 0,
196 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
197 }});
198 ctx->solid_vbuf_state.vertexbuf.count = 1;
199 ctx->solid_vbuf_state.vertexbuf.vb[0].stride = 12;
200 ctx->solid_vbuf_state.vertexbuf.vb[0].buffer = ctx->solid_vbuf;
201
202 /* setup blit_vbuf_state: */
203 ctx->blit_vbuf_state.vtx = pctx->create_vertex_elements_state(
204 pctx, 2, (struct pipe_vertex_element[]){{
205 .vertex_buffer_index = 0,
206 .src_offset = 0,
207 .src_format = PIPE_FORMAT_R32G32_FLOAT,
208 }, {
209 .vertex_buffer_index = 1,
210 .src_offset = 0,
211 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
212 }});
213 ctx->blit_vbuf_state.vertexbuf.count = 2;
214 ctx->blit_vbuf_state.vertexbuf.vb[0].stride = 8;
215 ctx->blit_vbuf_state.vertexbuf.vb[0].buffer = ctx->blit_texcoord_vbuf;
216 ctx->blit_vbuf_state.vertexbuf.vb[1].stride = 12;
217 ctx->blit_vbuf_state.vertexbuf.vb[1].buffer = ctx->solid_vbuf;
218 }
219
220 void
221 fd_context_cleanup_common_vbos(struct fd_context *ctx)
222 {
223 struct pipe_context *pctx = &ctx->base;
224
225 pctx->delete_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
226 pctx->delete_vertex_elements_state(pctx, ctx->blit_vbuf_state.vtx);
227
228 pipe_resource_reference(&ctx->solid_vbuf, NULL);
229 pipe_resource_reference(&ctx->blit_texcoord_vbuf, NULL);
230 }
231
232 struct pipe_context *
233 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
234 const uint8_t *primtypes, void *priv)
235 {
236 struct fd_screen *screen = fd_screen(pscreen);
237 struct pipe_context *pctx;
238 int i;
239
240 ctx->screen = screen;
241
242 ctx->primtypes = primtypes;
243 ctx->primtype_mask = 0;
244 for (i = 0; i < PIPE_PRIM_MAX; i++)
245 if (primtypes[i])
246 ctx->primtype_mask |= (1 << i);
247
248 /* need some sane default in case state tracker doesn't
249 * set some state:
250 */
251 ctx->sample_mask = 0xffff;
252
253 pctx = &ctx->base;
254 pctx->screen = pscreen;
255 pctx->priv = priv;
256 pctx->flush = fd_context_flush;
257 pctx->emit_string_marker = fd_emit_string_marker;
258 pctx->set_debug_callback = fd_set_debug_callback;
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(&ctx->transfer_pool, sizeof(struct fd_transfer),
269 16);
270
271 fd_draw_init(pctx);
272 fd_resource_context_init(pctx);
273 fd_query_context_init(pctx);
274 fd_texture_init(pctx);
275 fd_state_init(pctx);
276 fd_hw_query_init(pctx);
277
278 ctx->blitter = util_blitter_create(pctx);
279 if (!ctx->blitter)
280 goto fail;
281
282 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask);
283 if (!ctx->primconvert)
284 goto fail;
285
286 return pctx;
287
288 fail:
289 pctx->destroy(pctx);
290 return NULL;
291 }