freedreno: dynamically sized/growable cmd buffers
[mesa.git] / src / gallium / drivers / freedreno / freedreno_batch.c
1 /*
2 * Copyright (C) 2016 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 "util/list.h"
28 #include "util/u_string.h"
29
30 #include "freedreno_batch.h"
31 #include "freedreno_context.h"
32 #include "freedreno_resource.h"
33
34 struct fd_batch *
35 fd_batch_create(struct fd_context *ctx)
36 {
37 struct fd_batch *batch = CALLOC_STRUCT(fd_batch);
38 static unsigned seqno = 0;
39 unsigned size = 0;
40
41 if (!batch)
42 return NULL;
43
44 pipe_reference_init(&batch->reference, 1);
45 batch->seqno = ++seqno;
46 batch->ctx = ctx;
47
48 /* if kernel is too old to support unlimited # of cmd buffers, we
49 * have no option but to allocate large worst-case sizes so that
50 * we don't need to grow the ringbuffer. Performance is likely to
51 * suffer, but there is no good alternative.
52 */
53 if (fd_device_version(ctx->screen->dev) < FD_VERSION_UNLIMITED_CMDS) {
54 size = 0x100000;
55 }
56
57 batch->draw = fd_ringbuffer_new(ctx->screen->pipe, size);
58 batch->binning = fd_ringbuffer_new(ctx->screen->pipe, size);
59 batch->gmem = fd_ringbuffer_new(ctx->screen->pipe, size);
60
61 fd_ringbuffer_set_parent(batch->gmem, NULL);
62 fd_ringbuffer_set_parent(batch->draw, batch->gmem);
63 fd_ringbuffer_set_parent(batch->binning, batch->gmem);
64
65 list_inithead(&batch->used_resources);
66
67 return batch;
68 }
69
70 void
71 __fd_batch_destroy(struct fd_batch *batch)
72 {
73 fd_ringbuffer_del(batch->draw);
74 fd_ringbuffer_del(batch->binning);
75 fd_ringbuffer_del(batch->gmem);
76
77 free(batch);
78 }
79
80 void
81 __fd_batch_describe(char* buf, const struct fd_batch *batch)
82 {
83 util_sprintf(buf, "fd_batch<%u>", batch->seqno);
84 }
85
86 void
87 fd_batch_flush(struct fd_batch *batch)
88 {
89 struct fd_resource *rsc, *rsc_tmp;
90
91 fd_gmem_render_tiles(batch->ctx);
92
93 /* go through all the used resources and clear their reading flag */
94 LIST_FOR_EACH_ENTRY_SAFE(rsc, rsc_tmp, &batch->used_resources, list) {
95 debug_assert(rsc->pending_batch == batch);
96 debug_assert(rsc->status != 0);
97 rsc->status = 0;
98 fd_batch_reference(&rsc->pending_batch, NULL);
99 list_delinit(&rsc->list);
100 }
101
102 assert(LIST_IS_EMPTY(&batch->used_resources));
103 }
104
105 void
106 fd_batch_resource_used(struct fd_batch *batch, struct fd_resource *rsc,
107 enum fd_resource_status status)
108 {
109 rsc->status |= status;
110
111 if (rsc->stencil)
112 rsc->stencil->status |= status;
113
114 /* TODO resources can actually be shared across contexts,
115 * so I'm not sure a single list-head will do the trick?
116 */
117 debug_assert((rsc->pending_batch == batch) || !rsc->pending_batch);
118 list_delinit(&rsc->list);
119 list_addtail(&rsc->list, &batch->used_resources);
120 fd_batch_reference(&rsc->pending_batch, batch);
121 }
122
123 void
124 fd_batch_check_size(struct fd_batch *batch)
125 {
126 if (fd_device_version(batch->ctx->screen->dev) >= FD_VERSION_UNLIMITED_CMDS)
127 return;
128
129 struct fd_ringbuffer *ring = batch->draw;
130 if (((ring->cur - ring->start) > (ring->size/4 - 0x1000)) ||
131 (fd_mesa_debug & FD_DBG_FLUSH))
132 fd_context_render(&batch->ctx->base);
133 }