freedreno: move more batch related tracking to fd_batch
[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 /* reset maximal bounds: */
68 batch->max_scissor.minx = batch->max_scissor.miny = ~0;
69 batch->max_scissor.maxx = batch->max_scissor.maxy = 0;
70
71 util_dynarray_init(&batch->draw_patches);
72
73 if (is_a3xx(ctx->screen))
74 util_dynarray_init(&batch->rbrc_patches);
75
76 return batch;
77 }
78
79 void
80 __fd_batch_destroy(struct fd_batch *batch)
81 {
82 util_copy_framebuffer_state(&batch->framebuffer, NULL);
83 fd_ringbuffer_del(batch->draw);
84 fd_ringbuffer_del(batch->binning);
85 fd_ringbuffer_del(batch->gmem);
86
87 util_dynarray_fini(&batch->draw_patches);
88
89 if (is_a3xx(batch->ctx->screen))
90 util_dynarray_fini(&batch->rbrc_patches);
91
92 free(batch);
93 }
94
95 void
96 __fd_batch_describe(char* buf, const struct fd_batch *batch)
97 {
98 util_sprintf(buf, "fd_batch<%u>", batch->seqno);
99 }
100
101 void
102 fd_batch_flush(struct fd_batch *batch)
103 {
104 struct fd_resource *rsc, *rsc_tmp;
105
106 DBG("%p: needs_flush=%d", batch, batch->needs_flush);
107
108 if (!batch->needs_flush)
109 return;
110
111 fd_gmem_render_tiles(batch);
112
113 /* go through all the used resources and clear their reading flag */
114 LIST_FOR_EACH_ENTRY_SAFE(rsc, rsc_tmp, &batch->used_resources, list) {
115 debug_assert(rsc->pending_batch == batch);
116 debug_assert(rsc->status != 0);
117 rsc->status = 0;
118 fd_batch_reference(&rsc->pending_batch, NULL);
119 list_delinit(&rsc->list);
120 }
121
122 assert(LIST_IS_EMPTY(&batch->used_resources));
123 }
124
125 void
126 fd_batch_resource_used(struct fd_batch *batch, struct fd_resource *rsc,
127 enum fd_resource_status status)
128 {
129 rsc->status |= status;
130
131 if (rsc->stencil)
132 rsc->stencil->status |= status;
133
134 /* TODO resources can actually be shared across contexts,
135 * so I'm not sure a single list-head will do the trick?
136 */
137 debug_assert((rsc->pending_batch == batch) || !rsc->pending_batch);
138 list_delinit(&rsc->list);
139 list_addtail(&rsc->list, &batch->used_resources);
140 fd_batch_reference(&rsc->pending_batch, batch);
141 }
142
143 void
144 fd_batch_check_size(struct fd_batch *batch)
145 {
146 if (fd_device_version(batch->ctx->screen->dev) >= FD_VERSION_UNLIMITED_CMDS)
147 return;
148
149 struct fd_ringbuffer *ring = batch->draw;
150 if (((ring->cur - ring->start) > (ring->size/4 - 0x1000)) ||
151 (fd_mesa_debug & FD_DBG_FLUSH))
152 fd_context_render(&batch->ctx->base);
153 }