freedreno: push resource tracking down into 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
40 if (!batch)
41 return NULL;
42
43 pipe_reference_init(&batch->reference, 1);
44 batch->seqno = ++seqno;
45 batch->ctx = ctx;
46
47 /* TODO how to pick a good size? Or maybe we should introduce
48 * fd_ringlist? Also, make sure size is aligned with bo-cache
49 * bucket size, since otherwise that will round up size..
50 */
51 batch->draw = fd_ringbuffer_new(ctx->screen->pipe, 0x10000);
52 batch->binning = fd_ringbuffer_new(ctx->screen->pipe, 0x10000);
53 batch->gmem = fd_ringbuffer_new(ctx->screen->pipe, 0x10000);
54
55 fd_ringbuffer_set_parent(batch->gmem, NULL);
56 fd_ringbuffer_set_parent(batch->draw, batch->gmem);
57 fd_ringbuffer_set_parent(batch->binning, batch->gmem);
58
59 list_inithead(&batch->used_resources);
60
61 return batch;
62 }
63
64 void
65 __fd_batch_destroy(struct fd_batch *batch)
66 {
67 fd_ringbuffer_del(batch->draw);
68 fd_ringbuffer_del(batch->binning);
69 fd_ringbuffer_del(batch->gmem);
70
71 free(batch);
72 }
73
74 void
75 __fd_batch_describe(char* buf, const struct fd_batch *batch)
76 {
77 util_sprintf(buf, "fd_batch<%u>", batch->seqno);
78 }
79
80 void
81 fd_batch_flush(struct fd_batch *batch)
82 {
83 struct fd_resource *rsc, *rsc_tmp;
84
85 fd_gmem_render_tiles(batch->ctx);
86
87 /* go through all the used resources and clear their reading flag */
88 LIST_FOR_EACH_ENTRY_SAFE(rsc, rsc_tmp, &batch->used_resources, list) {
89 debug_assert(rsc->pending_batch == batch);
90 debug_assert(rsc->status != 0);
91 rsc->status = 0;
92 fd_batch_reference(&rsc->pending_batch, NULL);
93 list_delinit(&rsc->list);
94 }
95
96 assert(LIST_IS_EMPTY(&batch->used_resources));
97 }
98
99 void
100 fd_batch_resource_used(struct fd_batch *batch, struct fd_resource *rsc,
101 enum fd_resource_status status)
102 {
103 rsc->status |= status;
104
105 if (rsc->stencil)
106 rsc->stencil->status |= status;
107
108 /* TODO resources can actually be shared across contexts,
109 * so I'm not sure a single list-head will do the trick?
110 */
111 debug_assert((rsc->pending_batch == batch) || !rsc->pending_batch);
112 list_delinit(&rsc->list);
113 list_addtail(&rsc->list, &batch->used_resources);
114 fd_batch_reference(&rsc->pending_batch, batch);
115 }
116
117 void
118 fd_batch_check_size(struct fd_batch *batch)
119 {
120 /* TODO eventually support having a list of draw/binning rb's
121 * and if we are too close to the end, add another to the
122 * list. For now we just flush.
123 */
124 struct fd_ringbuffer *ring = batch->draw;
125 if (((ring->cur - ring->start) > (ring->size/4 - 0x1000)) ||
126 (fd_mesa_debug & FD_DBG_FLUSH))
127 fd_context_render(&batch->ctx->base);
128 }