iris: Actually advertise some modifiers
[mesa.git] / src / gallium / drivers / freedreno / freedreno_context.c
1 /*
2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "freedreno_context.h"
28 #include "freedreno_blitter.h"
29 #include "freedreno_draw.h"
30 #include "freedreno_fence.h"
31 #include "freedreno_program.h"
32 #include "freedreno_resource.h"
33 #include "freedreno_texture.h"
34 #include "freedreno_state.h"
35 #include "freedreno_gmem.h"
36 #include "freedreno_query.h"
37 #include "freedreno_query_hw.h"
38 #include "freedreno_util.h"
39 #include "util/u_upload_mgr.h"
40
41 static void
42 fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fencep,
43 unsigned flags)
44 {
45 struct fd_context *ctx = fd_context(pctx);
46 struct pipe_fence_handle *fence = NULL;
47 // TODO we want to lookup batch if it exists, but not create one if not.
48 struct fd_batch *batch = fd_context_batch(ctx);
49
50 DBG("%p: flush: flags=%x\n", ctx->batch, flags);
51
52 /* if no rendering since last flush, ie. app just decided it needed
53 * a fence, re-use the last one:
54 */
55 if (ctx->last_fence) {
56 fd_fence_ref(pctx->screen, &fence, ctx->last_fence);
57 goto out;
58 }
59
60 if (!batch)
61 return;
62
63 /* Take a ref to the batch's fence (batch can be unref'd when flushed: */
64 fd_fence_ref(pctx->screen, &fence, batch->fence);
65
66 /* TODO is it worth trying to figure out if app is using fence-fd's, to
67 * avoid requesting one every batch?
68 */
69 batch->needs_out_fence_fd = true;
70
71 if (!ctx->screen->reorder) {
72 fd_batch_flush(batch, true, false);
73 } else if (flags & PIPE_FLUSH_DEFERRED) {
74 fd_bc_flush_deferred(&ctx->screen->batch_cache, ctx);
75 } else {
76 fd_bc_flush(&ctx->screen->batch_cache, ctx);
77 }
78
79 out:
80 if (fencep)
81 fd_fence_ref(pctx->screen, fencep, fence);
82
83 fd_fence_ref(pctx->screen, &ctx->last_fence, fence);
84
85 fd_fence_ref(pctx->screen, &fence, NULL);
86 }
87
88 static void
89 fd_texture_barrier(struct pipe_context *pctx, unsigned flags)
90 {
91 /* On devices that could sample from GMEM we could possibly do better.
92 * Or if we knew that we were doing GMEM bypass we could just emit a
93 * cache flush, perhaps? But we don't know if future draws would cause
94 * us to use GMEM, and a flush in bypass isn't the end of the world.
95 */
96 fd_context_flush(pctx, NULL, 0);
97 }
98
99 static void
100 fd_memory_barrier(struct pipe_context *pctx, unsigned flags)
101 {
102 if (!(flags & ~PIPE_BARRIER_UPDATE))
103 return;
104
105 fd_context_flush(pctx, NULL, 0);
106 /* TODO do we need to check for persistently mapped buffers and fd_bo_cpu_prep()?? */
107 }
108
109 /**
110 * emit marker string as payload of a no-op packet, which can be
111 * decoded by cffdump.
112 */
113 static void
114 fd_emit_string_marker(struct pipe_context *pctx, const char *string, int len)
115 {
116 struct fd_context *ctx = fd_context(pctx);
117 struct fd_ringbuffer *ring;
118 const uint32_t *buf = (const void *)string;
119
120 if (!ctx->batch)
121 return;
122
123 ctx->batch->needs_flush = true;
124
125 ring = ctx->batch->draw;
126
127 /* max packet size is 0x3fff dwords: */
128 len = MIN2(len, 0x3fff * 4);
129
130 if (ctx->screen->gpu_id >= 500)
131 OUT_PKT7(ring, CP_NOP, align(len, 4) / 4);
132 else
133 OUT_PKT3(ring, CP_NOP, align(len, 4) / 4);
134 while (len >= 4) {
135 OUT_RING(ring, *buf);
136 buf++;
137 len -= 4;
138 }
139
140 /* copy remainder bytes without reading past end of input string: */
141 if (len > 0) {
142 uint32_t w = 0;
143 memcpy(&w, buf, len);
144 OUT_RING(ring, w);
145 }
146 }
147
148 void
149 fd_context_destroy(struct pipe_context *pctx)
150 {
151 struct fd_context *ctx = fd_context(pctx);
152 unsigned i;
153
154 DBG("");
155
156 fd_fence_ref(pctx->screen, &ctx->last_fence, NULL);
157
158 if (ctx->screen->reorder && util_queue_is_initialized(&ctx->flush_queue))
159 util_queue_destroy(&ctx->flush_queue);
160
161 util_copy_framebuffer_state(&ctx->framebuffer, NULL);
162 fd_batch_reference(&ctx->batch, NULL); /* unref current batch */
163 fd_bc_invalidate_context(ctx);
164
165 fd_prog_fini(pctx);
166
167 if (ctx->blitter)
168 util_blitter_destroy(ctx->blitter);
169
170 if (pctx->stream_uploader)
171 u_upload_destroy(pctx->stream_uploader);
172
173 if (ctx->clear_rs_state)
174 pctx->delete_rasterizer_state(pctx, ctx->clear_rs_state);
175
176 if (ctx->primconvert)
177 util_primconvert_destroy(ctx->primconvert);
178
179 slab_destroy_child(&ctx->transfer_pool);
180
181 for (i = 0; i < ARRAY_SIZE(ctx->vsc_pipe); i++) {
182 struct fd_vsc_pipe *pipe = &ctx->vsc_pipe[i];
183 if (!pipe->bo)
184 break;
185 fd_bo_del(pipe->bo);
186 }
187
188 fd_device_del(ctx->dev);
189 fd_pipe_del(ctx->pipe);
190
191 if (fd_mesa_debug & (FD_DBG_BSTAT | FD_DBG_MSGS)) {
192 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_nondraw=%u, batch_restore=%u\n",
193 (uint32_t)ctx->stats.batch_total, (uint32_t)ctx->stats.batch_sysmem,
194 (uint32_t)ctx->stats.batch_gmem, (uint32_t)ctx->stats.batch_nondraw,
195 (uint32_t)ctx->stats.batch_restore);
196 }
197 }
198
199 static void
200 fd_set_debug_callback(struct pipe_context *pctx,
201 const struct pipe_debug_callback *cb)
202 {
203 struct fd_context *ctx = fd_context(pctx);
204
205 if (cb)
206 ctx->debug = *cb;
207 else
208 memset(&ctx->debug, 0, sizeof(ctx->debug));
209 }
210
211 /* TODO we could combine a few of these small buffers (solid_vbuf,
212 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
213 * save a tiny bit of memory
214 */
215
216 static struct pipe_resource *
217 create_solid_vertexbuf(struct pipe_context *pctx)
218 {
219 static const float init_shader_const[] = {
220 -1.000000, +1.000000, +1.000000,
221 +1.000000, -1.000000, +1.000000,
222 };
223 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
224 PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE, sizeof(init_shader_const));
225 pipe_buffer_write(pctx, prsc, 0,
226 sizeof(init_shader_const), init_shader_const);
227 return prsc;
228 }
229
230 static struct pipe_resource *
231 create_blit_texcoord_vertexbuf(struct pipe_context *pctx)
232 {
233 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
234 PIPE_BIND_CUSTOM, PIPE_USAGE_DYNAMIC, 16);
235 return prsc;
236 }
237
238 void
239 fd_context_setup_common_vbos(struct fd_context *ctx)
240 {
241 struct pipe_context *pctx = &ctx->base;
242
243 ctx->solid_vbuf = create_solid_vertexbuf(pctx);
244 ctx->blit_texcoord_vbuf = create_blit_texcoord_vertexbuf(pctx);
245
246 /* setup solid_vbuf_state: */
247 ctx->solid_vbuf_state.vtx = pctx->create_vertex_elements_state(
248 pctx, 1, (struct pipe_vertex_element[]){{
249 .vertex_buffer_index = 0,
250 .src_offset = 0,
251 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
252 }});
253 ctx->solid_vbuf_state.vertexbuf.count = 1;
254 ctx->solid_vbuf_state.vertexbuf.vb[0].stride = 12;
255 ctx->solid_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->solid_vbuf;
256
257 /* setup blit_vbuf_state: */
258 ctx->blit_vbuf_state.vtx = pctx->create_vertex_elements_state(
259 pctx, 2, (struct pipe_vertex_element[]){{
260 .vertex_buffer_index = 0,
261 .src_offset = 0,
262 .src_format = PIPE_FORMAT_R32G32_FLOAT,
263 }, {
264 .vertex_buffer_index = 1,
265 .src_offset = 0,
266 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
267 }});
268 ctx->blit_vbuf_state.vertexbuf.count = 2;
269 ctx->blit_vbuf_state.vertexbuf.vb[0].stride = 8;
270 ctx->blit_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->blit_texcoord_vbuf;
271 ctx->blit_vbuf_state.vertexbuf.vb[1].stride = 12;
272 ctx->blit_vbuf_state.vertexbuf.vb[1].buffer.resource = ctx->solid_vbuf;
273 }
274
275 void
276 fd_context_cleanup_common_vbos(struct fd_context *ctx)
277 {
278 struct pipe_context *pctx = &ctx->base;
279
280 pctx->delete_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
281 pctx->delete_vertex_elements_state(pctx, ctx->blit_vbuf_state.vtx);
282
283 pipe_resource_reference(&ctx->solid_vbuf, NULL);
284 pipe_resource_reference(&ctx->blit_texcoord_vbuf, NULL);
285 }
286
287 struct pipe_context *
288 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
289 const uint8_t *primtypes, void *priv, unsigned flags)
290 {
291 struct fd_screen *screen = fd_screen(pscreen);
292 struct pipe_context *pctx;
293 unsigned prio = 1;
294 int i;
295
296 /* lower numerical value == higher priority: */
297 if (fd_mesa_debug & FD_DBG_HIPRIO)
298 prio = 0;
299 else if (flags & PIPE_CONTEXT_HIGH_PRIORITY)
300 prio = 0;
301 else if (flags & PIPE_CONTEXT_LOW_PRIORITY)
302 prio = 2;
303
304 ctx->screen = screen;
305 ctx->pipe = fd_pipe_new2(screen->dev, FD_PIPE_3D, prio);
306
307 ctx->primtypes = primtypes;
308 ctx->primtype_mask = 0;
309 for (i = 0; i < PIPE_PRIM_MAX; i++)
310 if (primtypes[i])
311 ctx->primtype_mask |= (1 << i);
312
313 /* need some sane default in case state tracker doesn't
314 * set some state:
315 */
316 ctx->sample_mask = 0xffff;
317
318 pctx = &ctx->base;
319 pctx->screen = pscreen;
320 pctx->priv = priv;
321 pctx->flush = fd_context_flush;
322 pctx->emit_string_marker = fd_emit_string_marker;
323 pctx->set_debug_callback = fd_set_debug_callback;
324 pctx->create_fence_fd = fd_create_fence_fd;
325 pctx->fence_server_sync = fd_fence_server_sync;
326 pctx->texture_barrier = fd_texture_barrier;
327 pctx->memory_barrier = fd_memory_barrier;
328
329 pctx->stream_uploader = u_upload_create_default(pctx);
330 if (!pctx->stream_uploader)
331 goto fail;
332 pctx->const_uploader = pctx->stream_uploader;
333
334 if (!ctx->screen->reorder)
335 ctx->batch = fd_bc_alloc_batch(&screen->batch_cache, ctx, false);
336
337 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
338
339 fd_draw_init(pctx);
340 fd_resource_context_init(pctx);
341 fd_query_context_init(pctx);
342 fd_texture_init(pctx);
343 fd_state_init(pctx);
344
345 ctx->blitter = util_blitter_create(pctx);
346 if (!ctx->blitter)
347 goto fail;
348
349 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask);
350 if (!ctx->primconvert)
351 goto fail;
352
353 list_inithead(&ctx->hw_active_queries);
354 list_inithead(&ctx->acc_active_queries);
355
356 return pctx;
357
358 fail:
359 pctx->destroy(pctx);
360 return NULL;
361 }