gallium: decrease the size of pipe_vertex_buffer - 24 -> 16 bytes
[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 #include "util/u_upload_mgr.h"
41
42 static void
43 fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
44 unsigned flags)
45 {
46 struct fd_context *ctx = fd_context(pctx);
47
48 if (flags & PIPE_FLUSH_FENCE_FD)
49 ctx->batch->needs_out_fence_fd = true;
50
51 if (!ctx->screen->reorder) {
52 fd_batch_flush(ctx->batch, true);
53 } else {
54 fd_bc_flush(&ctx->screen->batch_cache, ctx);
55 }
56
57 if (fence) {
58 /* if there hasn't been any rendering submitted yet, we might not
59 * have actually created a fence
60 */
61 if (!ctx->last_fence || ctx->batch->needs_out_fence_fd) {
62 ctx->batch->needs_flush = true;
63 fd_gmem_render_noop(ctx->batch);
64 fd_batch_reset(ctx->batch);
65 }
66 fd_fence_ref(pctx->screen, fence, ctx->last_fence);
67 }
68 }
69
70 /**
71 * emit marker string as payload of a no-op packet, which can be
72 * decoded by cffdump.
73 */
74 static void
75 fd_emit_string_marker(struct pipe_context *pctx, const char *string, int len)
76 {
77 struct fd_context *ctx = fd_context(pctx);
78 struct fd_ringbuffer *ring;
79 const uint32_t *buf = (const void *)string;
80
81 if (!ctx->batch)
82 return;
83
84 ring = ctx->batch->draw;
85
86 /* max packet size is 0x3fff dwords: */
87 len = MIN2(len, 0x3fff * 4);
88
89 if (ctx->screen->gpu_id >= 500)
90 OUT_PKT7(ring, CP_NOP, align(len, 4) / 4);
91 else
92 OUT_PKT3(ring, CP_NOP, align(len, 4) / 4);
93 while (len >= 4) {
94 OUT_RING(ring, *buf);
95 buf++;
96 len -= 4;
97 }
98
99 /* copy remainder bytes without reading past end of input string: */
100 if (len > 0) {
101 uint32_t w = 0;
102 memcpy(&w, buf, len);
103 OUT_RING(ring, w);
104 }
105 }
106
107 void
108 fd_context_destroy(struct pipe_context *pctx)
109 {
110 struct fd_context *ctx = fd_context(pctx);
111 unsigned i;
112
113 DBG("");
114
115 if (ctx->screen->reorder && util_queue_is_initialized(&ctx->flush_queue))
116 util_queue_destroy(&ctx->flush_queue);
117
118 fd_batch_reference(&ctx->batch, NULL); /* unref current batch */
119 fd_bc_invalidate_context(ctx);
120
121 fd_fence_ref(pctx->screen, &ctx->last_fence, NULL);
122
123 fd_prog_fini(pctx);
124
125 if (ctx->blitter)
126 util_blitter_destroy(ctx->blitter);
127
128 if (pctx->stream_uploader)
129 u_upload_destroy(pctx->stream_uploader);
130
131 if (ctx->clear_rs_state)
132 pctx->delete_rasterizer_state(pctx, ctx->clear_rs_state);
133
134 if (ctx->primconvert)
135 util_primconvert_destroy(ctx->primconvert);
136
137 slab_destroy_child(&ctx->transfer_pool);
138
139 for (i = 0; i < ARRAY_SIZE(ctx->pipe); i++) {
140 struct fd_vsc_pipe *pipe = &ctx->pipe[i];
141 if (!pipe->bo)
142 break;
143 fd_bo_del(pipe->bo);
144 }
145
146 fd_device_del(ctx->dev);
147
148 if (fd_mesa_debug & (FD_DBG_BSTAT | FD_DBG_MSGS)) {
149 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_restore=%u\n",
150 (uint32_t)ctx->stats.batch_total, (uint32_t)ctx->stats.batch_sysmem,
151 (uint32_t)ctx->stats.batch_gmem, (uint32_t)ctx->stats.batch_restore);
152 }
153
154 FREE(ctx);
155 }
156
157 static void
158 fd_set_debug_callback(struct pipe_context *pctx,
159 const struct pipe_debug_callback *cb)
160 {
161 struct fd_context *ctx = fd_context(pctx);
162
163 if (cb)
164 ctx->debug = *cb;
165 else
166 memset(&ctx->debug, 0, sizeof(ctx->debug));
167 }
168
169 /* TODO we could combine a few of these small buffers (solid_vbuf,
170 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
171 * save a tiny bit of memory
172 */
173
174 static struct pipe_resource *
175 create_solid_vertexbuf(struct pipe_context *pctx)
176 {
177 static const float init_shader_const[] = {
178 -1.000000, +1.000000, +1.000000,
179 +1.000000, -1.000000, +1.000000,
180 };
181 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
182 PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE, sizeof(init_shader_const));
183 pipe_buffer_write(pctx, prsc, 0,
184 sizeof(init_shader_const), init_shader_const);
185 return prsc;
186 }
187
188 static struct pipe_resource *
189 create_blit_texcoord_vertexbuf(struct pipe_context *pctx)
190 {
191 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
192 PIPE_BIND_CUSTOM, PIPE_USAGE_DYNAMIC, 16);
193 return prsc;
194 }
195
196 void
197 fd_context_setup_common_vbos(struct fd_context *ctx)
198 {
199 struct pipe_context *pctx = &ctx->base;
200
201 ctx->solid_vbuf = create_solid_vertexbuf(pctx);
202 ctx->blit_texcoord_vbuf = create_blit_texcoord_vertexbuf(pctx);
203
204 /* setup solid_vbuf_state: */
205 ctx->solid_vbuf_state.vtx = pctx->create_vertex_elements_state(
206 pctx, 1, (struct pipe_vertex_element[]){{
207 .vertex_buffer_index = 0,
208 .src_offset = 0,
209 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
210 }});
211 ctx->solid_vbuf_state.vertexbuf.count = 1;
212 ctx->solid_vbuf_state.vertexbuf.vb[0].stride = 12;
213 ctx->solid_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->solid_vbuf;
214
215 /* setup blit_vbuf_state: */
216 ctx->blit_vbuf_state.vtx = pctx->create_vertex_elements_state(
217 pctx, 2, (struct pipe_vertex_element[]){{
218 .vertex_buffer_index = 0,
219 .src_offset = 0,
220 .src_format = PIPE_FORMAT_R32G32_FLOAT,
221 }, {
222 .vertex_buffer_index = 1,
223 .src_offset = 0,
224 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
225 }});
226 ctx->blit_vbuf_state.vertexbuf.count = 2;
227 ctx->blit_vbuf_state.vertexbuf.vb[0].stride = 8;
228 ctx->blit_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->blit_texcoord_vbuf;
229 ctx->blit_vbuf_state.vertexbuf.vb[1].stride = 12;
230 ctx->blit_vbuf_state.vertexbuf.vb[1].buffer.resource = ctx->solid_vbuf;
231 }
232
233 void
234 fd_context_cleanup_common_vbos(struct fd_context *ctx)
235 {
236 struct pipe_context *pctx = &ctx->base;
237
238 pctx->delete_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
239 pctx->delete_vertex_elements_state(pctx, ctx->blit_vbuf_state.vtx);
240
241 pipe_resource_reference(&ctx->solid_vbuf, NULL);
242 pipe_resource_reference(&ctx->blit_texcoord_vbuf, NULL);
243 }
244
245 struct pipe_context *
246 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
247 const uint8_t *primtypes, void *priv)
248 {
249 struct fd_screen *screen = fd_screen(pscreen);
250 struct pipe_context *pctx;
251 int i;
252
253 ctx->screen = screen;
254
255 ctx->primtypes = primtypes;
256 ctx->primtype_mask = 0;
257 for (i = 0; i < PIPE_PRIM_MAX; i++)
258 if (primtypes[i])
259 ctx->primtype_mask |= (1 << i);
260
261 /* need some sane default in case state tracker doesn't
262 * set some state:
263 */
264 ctx->sample_mask = 0xffff;
265
266 pctx = &ctx->base;
267 pctx->screen = pscreen;
268 pctx->priv = priv;
269 pctx->flush = fd_context_flush;
270 pctx->emit_string_marker = fd_emit_string_marker;
271 pctx->set_debug_callback = fd_set_debug_callback;
272 pctx->create_fence_fd = fd_create_fence_fd;
273 pctx->fence_server_sync = fd_fence_server_sync;
274
275 pctx->stream_uploader = u_upload_create_default(pctx);
276 if (!pctx->stream_uploader)
277 goto fail;
278 pctx->const_uploader = pctx->stream_uploader;
279
280 /* TODO what about compute? Ideally it creates it's own independent
281 * batches per compute job (since it isn't using tiling, so no point
282 * in getting involved with the re-ordering madness)..
283 */
284 if (!screen->reorder) {
285 ctx->batch = fd_bc_alloc_batch(&screen->batch_cache, ctx);
286 }
287
288 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
289
290 fd_draw_init(pctx);
291 fd_resource_context_init(pctx);
292 fd_query_context_init(pctx);
293 fd_texture_init(pctx);
294 fd_state_init(pctx);
295
296 ctx->blitter = util_blitter_create(pctx);
297 if (!ctx->blitter)
298 goto fail;
299
300 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask);
301 if (!ctx->primconvert)
302 goto fail;
303
304 list_inithead(&ctx->hw_active_queries);
305 list_inithead(&ctx->acc_active_queries);
306
307 return pctx;
308
309 fail:
310 pctx->destroy(pctx);
311 return NULL;
312 }