60da218e59e61495c1585dbcb1d1c5f0a67ee208
[mesa.git] / src / gallium / drivers / vc4 / vc4_context.c
1 /*
2 * Copyright © 2014 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <xf86drm.h>
25 #include <err.h>
26
27 #include "pipe/p_defines.h"
28 #include "util/ralloc.h"
29 #include "util/u_inlines.h"
30 #include "util/u_memory.h"
31 #include "util/u_blitter.h"
32 #include "util/u_upload_mgr.h"
33 #include "indices/u_primconvert.h"
34 #include "pipe/p_screen.h"
35
36 #include "vc4_screen.h"
37 #include "vc4_context.h"
38 #include "vc4_resource.h"
39
40 void
41 vc4_flush(struct pipe_context *pctx)
42 {
43 struct vc4_context *vc4 = vc4_context(pctx);
44 struct pipe_surface *cbuf = vc4->framebuffer.cbufs[0];
45 struct pipe_surface *zsbuf = vc4->framebuffer.zsbuf;
46
47 if (!vc4->needs_flush)
48 return;
49
50 /* The RCL setup would choke if the draw bounds cause no drawing, so
51 * just drop the drawing if that's the case.
52 */
53 if (vc4->draw_max_x <= vc4->draw_min_x ||
54 vc4->draw_max_y <= vc4->draw_min_y) {
55 vc4_job_reset(vc4);
56 return;
57 }
58
59 /* Increment the semaphore indicating that binning is done and
60 * unblocking the render thread. Note that this doesn't act until the
61 * FLUSH completes.
62 */
63 cl_ensure_space(&vc4->bcl, 8);
64 cl_u8(&vc4->bcl, VC4_PACKET_INCREMENT_SEMAPHORE);
65 /* The FLUSH caps all of our bin lists with a VC4_PACKET_RETURN. */
66 cl_u8(&vc4->bcl, VC4_PACKET_FLUSH);
67
68 if (cbuf && (vc4->resolve & PIPE_CLEAR_COLOR0)) {
69 pipe_surface_reference(&vc4->color_write, cbuf);
70 if (!(vc4->cleared & PIPE_CLEAR_COLOR0)) {
71 pipe_surface_reference(&vc4->color_read, cbuf);
72 } else {
73 pipe_surface_reference(&vc4->color_read, NULL);
74 }
75
76 } else {
77 pipe_surface_reference(&vc4->color_write, NULL);
78 pipe_surface_reference(&vc4->color_read, NULL);
79 }
80
81 if (vc4->framebuffer.zsbuf &&
82 (vc4->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))) {
83 pipe_surface_reference(&vc4->zs_write, zsbuf);
84 if (!(vc4->cleared & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))) {
85 pipe_surface_reference(&vc4->zs_read, zsbuf);
86 } else {
87 pipe_surface_reference(&vc4->zs_read, NULL);
88 }
89 } else {
90 pipe_surface_reference(&vc4->zs_write, NULL);
91 pipe_surface_reference(&vc4->zs_read, NULL);
92 }
93
94 vc4_job_submit(vc4);
95 }
96
97 static void
98 vc4_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
99 unsigned flags)
100 {
101 struct vc4_context *vc4 = vc4_context(pctx);
102
103 vc4_flush(pctx);
104
105 if (fence) {
106 struct pipe_screen *screen = pctx->screen;
107 struct vc4_fence *f = vc4_fence_create(vc4->screen,
108 vc4->last_emit_seqno);
109 screen->fence_reference(screen, fence, NULL);
110 *fence = (struct pipe_fence_handle *)f;
111 }
112 }
113
114 /**
115 * Flushes the current command lists if they reference the given BO.
116 *
117 * This helps avoid flushing the command buffers when unnecessary.
118 */
119 bool
120 vc4_cl_references_bo(struct pipe_context *pctx, struct vc4_bo *bo)
121 {
122 struct vc4_context *vc4 = vc4_context(pctx);
123
124 if (!vc4->needs_flush)
125 return false;
126
127 /* Walk all the referenced BOs in the drawing command list to see if
128 * they match.
129 */
130 struct vc4_bo **referenced_bos = vc4->bo_pointers.base;
131 for (int i = 0; i < cl_offset(&vc4->bo_handles) / 4; i++) {
132 if (referenced_bos[i] == bo) {
133 return true;
134 }
135 }
136
137 /* Also check for the Z/color buffers, since the references to those
138 * are only added immediately before submit.
139 */
140 struct vc4_surface *csurf = vc4_surface(vc4->framebuffer.cbufs[0]);
141 if (csurf) {
142 struct vc4_resource *ctex = vc4_resource(csurf->base.texture);
143 if (ctex->bo == bo) {
144 return true;
145 }
146 }
147
148 struct vc4_surface *zsurf = vc4_surface(vc4->framebuffer.zsbuf);
149 if (zsurf) {
150 struct vc4_resource *ztex =
151 vc4_resource(zsurf->base.texture);
152 if (ztex->bo == bo) {
153 return true;
154 }
155 }
156
157 return false;
158 }
159
160 static void
161 vc4_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
162 {
163 struct vc4_context *vc4 = vc4_context(pctx);
164 struct pipe_surface *zsurf = vc4->framebuffer.zsbuf;
165
166 if (zsurf && zsurf->texture == prsc)
167 vc4->resolve &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
168 }
169
170 static void
171 vc4_context_destroy(struct pipe_context *pctx)
172 {
173 struct vc4_context *vc4 = vc4_context(pctx);
174
175 if (vc4->blitter)
176 util_blitter_destroy(vc4->blitter);
177
178 if (vc4->primconvert)
179 util_primconvert_destroy(vc4->primconvert);
180
181 if (vc4->uploader)
182 u_upload_destroy(vc4->uploader);
183
184 util_slab_destroy(&vc4->transfer_pool);
185
186 pipe_surface_reference(&vc4->framebuffer.cbufs[0], NULL);
187 pipe_surface_reference(&vc4->framebuffer.zsbuf, NULL);
188
189 vc4_program_fini(pctx);
190
191 ralloc_free(vc4);
192 }
193
194 struct pipe_context *
195 vc4_context_create(struct pipe_screen *pscreen, void *priv)
196 {
197 struct vc4_screen *screen = vc4_screen(pscreen);
198 struct vc4_context *vc4;
199
200 /* Prevent dumping of the shaders built during context setup. */
201 uint32_t saved_shaderdb_flag = vc4_debug & VC4_DEBUG_SHADERDB;
202 vc4_debug &= ~VC4_DEBUG_SHADERDB;
203
204 vc4 = rzalloc(NULL, struct vc4_context);
205 if (vc4 == NULL)
206 return NULL;
207 struct pipe_context *pctx = &vc4->base;
208
209 vc4->screen = screen;
210
211 pctx->screen = pscreen;
212 pctx->priv = priv;
213 pctx->destroy = vc4_context_destroy;
214 pctx->flush = vc4_pipe_flush;
215 pctx->invalidate_resource = vc4_invalidate_resource;
216
217 vc4_draw_init(pctx);
218 vc4_state_init(pctx);
219 vc4_program_init(pctx);
220 vc4_query_init(pctx);
221 vc4_resource_context_init(pctx);
222
223 vc4_job_init(vc4);
224
225 vc4->fd = screen->fd;
226
227 util_slab_create(&vc4->transfer_pool, sizeof(struct vc4_transfer),
228 16, UTIL_SLAB_SINGLETHREADED);
229 vc4->blitter = util_blitter_create(pctx);
230 if (!vc4->blitter)
231 goto fail;
232
233 vc4->primconvert = util_primconvert_create(pctx,
234 (1 << PIPE_PRIM_QUADS) - 1);
235 if (!vc4->primconvert)
236 goto fail;
237
238 vc4->uploader = u_upload_create(pctx, 16 * 1024, 4,
239 PIPE_BIND_INDEX_BUFFER);
240
241 vc4_debug |= saved_shaderdb_flag;
242
243 return &vc4->base;
244
245 fail:
246 pctx->destroy(pctx);
247 return NULL;
248 }