u_upload_mgr: remove alignment parameter from u_upload_create
[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 struct vc4_cl_out *bcl = cl_start(&vc4->bcl);
65 cl_u8(&bcl, VC4_PACKET_INCREMENT_SEMAPHORE);
66 /* The FLUSH caps all of our bin lists with a VC4_PACKET_RETURN. */
67 cl_u8(&bcl, VC4_PACKET_FLUSH);
68 cl_end(&vc4->bcl, bcl);
69
70 if (cbuf && (vc4->resolve & PIPE_CLEAR_COLOR0)) {
71 pipe_surface_reference(&vc4->color_write,
72 cbuf->texture->nr_samples > 1 ?
73 NULL : cbuf);
74 pipe_surface_reference(&vc4->msaa_color_write,
75 cbuf->texture->nr_samples > 1 ?
76 cbuf : NULL);
77
78 if (!(vc4->cleared & PIPE_CLEAR_COLOR0)) {
79 pipe_surface_reference(&vc4->color_read, cbuf);
80 } else {
81 pipe_surface_reference(&vc4->color_read, NULL);
82 }
83
84 } else {
85 pipe_surface_reference(&vc4->color_write, NULL);
86 pipe_surface_reference(&vc4->color_read, NULL);
87 pipe_surface_reference(&vc4->msaa_color_write, NULL);
88 }
89
90 if (vc4->framebuffer.zsbuf &&
91 (vc4->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))) {
92 pipe_surface_reference(&vc4->zs_write,
93 zsbuf->texture->nr_samples > 1 ?
94 NULL : zsbuf);
95 pipe_surface_reference(&vc4->msaa_zs_write,
96 zsbuf->texture->nr_samples > 1 ?
97 zsbuf : NULL);
98
99 if (!(vc4->cleared & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))) {
100 pipe_surface_reference(&vc4->zs_read, zsbuf);
101 } else {
102 pipe_surface_reference(&vc4->zs_read, NULL);
103 }
104 } else {
105 pipe_surface_reference(&vc4->zs_write, NULL);
106 pipe_surface_reference(&vc4->zs_read, NULL);
107 pipe_surface_reference(&vc4->msaa_zs_write, NULL);
108 }
109
110 vc4_job_submit(vc4);
111 }
112
113 static void
114 vc4_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
115 unsigned flags)
116 {
117 struct vc4_context *vc4 = vc4_context(pctx);
118
119 vc4_flush(pctx);
120
121 if (fence) {
122 struct pipe_screen *screen = pctx->screen;
123 struct vc4_fence *f = vc4_fence_create(vc4->screen,
124 vc4->last_emit_seqno);
125 screen->fence_reference(screen, fence, NULL);
126 *fence = (struct pipe_fence_handle *)f;
127 }
128 }
129
130 /**
131 * Flushes the current command lists if they reference the given BO.
132 *
133 * This helps avoid flushing the command buffers when unnecessary.
134 */
135 bool
136 vc4_cl_references_bo(struct pipe_context *pctx, struct vc4_bo *bo)
137 {
138 struct vc4_context *vc4 = vc4_context(pctx);
139
140 if (!vc4->needs_flush)
141 return false;
142
143 /* Walk all the referenced BOs in the drawing command list to see if
144 * they match.
145 */
146 struct vc4_bo **referenced_bos = vc4->bo_pointers.base;
147 for (int i = 0; i < cl_offset(&vc4->bo_handles) / 4; i++) {
148 if (referenced_bos[i] == bo) {
149 return true;
150 }
151 }
152
153 /* Also check for the Z/color buffers, since the references to those
154 * are only added immediately before submit.
155 */
156 struct vc4_surface *csurf = vc4_surface(vc4->framebuffer.cbufs[0]);
157 if (csurf) {
158 struct vc4_resource *ctex = vc4_resource(csurf->base.texture);
159 if (ctex->bo == bo) {
160 return true;
161 }
162 }
163
164 struct vc4_surface *zsurf = vc4_surface(vc4->framebuffer.zsbuf);
165 if (zsurf) {
166 struct vc4_resource *ztex =
167 vc4_resource(zsurf->base.texture);
168 if (ztex->bo == bo) {
169 return true;
170 }
171 }
172
173 return false;
174 }
175
176 static void
177 vc4_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
178 {
179 struct vc4_context *vc4 = vc4_context(pctx);
180 struct pipe_surface *zsurf = vc4->framebuffer.zsbuf;
181
182 if (zsurf && zsurf->texture == prsc)
183 vc4->resolve &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
184 }
185
186 static void
187 vc4_context_destroy(struct pipe_context *pctx)
188 {
189 struct vc4_context *vc4 = vc4_context(pctx);
190
191 if (vc4->blitter)
192 util_blitter_destroy(vc4->blitter);
193
194 if (vc4->primconvert)
195 util_primconvert_destroy(vc4->primconvert);
196
197 if (vc4->uploader)
198 u_upload_destroy(vc4->uploader);
199
200 util_slab_destroy(&vc4->transfer_pool);
201
202 pipe_surface_reference(&vc4->framebuffer.cbufs[0], NULL);
203 pipe_surface_reference(&vc4->framebuffer.zsbuf, NULL);
204
205 pipe_surface_reference(&vc4->color_write, NULL);
206 pipe_surface_reference(&vc4->color_read, NULL);
207
208 vc4_program_fini(pctx);
209
210 ralloc_free(vc4);
211 }
212
213 struct pipe_context *
214 vc4_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
215 {
216 struct vc4_screen *screen = vc4_screen(pscreen);
217 struct vc4_context *vc4;
218
219 /* Prevent dumping of the shaders built during context setup. */
220 uint32_t saved_shaderdb_flag = vc4_debug & VC4_DEBUG_SHADERDB;
221 vc4_debug &= ~VC4_DEBUG_SHADERDB;
222
223 vc4 = rzalloc(NULL, struct vc4_context);
224 if (!vc4)
225 return NULL;
226 struct pipe_context *pctx = &vc4->base;
227
228 vc4->screen = screen;
229
230 pctx->screen = pscreen;
231 pctx->priv = priv;
232 pctx->destroy = vc4_context_destroy;
233 pctx->flush = vc4_pipe_flush;
234 pctx->invalidate_resource = vc4_invalidate_resource;
235
236 vc4_draw_init(pctx);
237 vc4_state_init(pctx);
238 vc4_program_init(pctx);
239 vc4_query_init(pctx);
240 vc4_resource_context_init(pctx);
241
242 vc4_job_init(vc4);
243
244 vc4->fd = screen->fd;
245
246 util_slab_create(&vc4->transfer_pool, sizeof(struct vc4_transfer),
247 16, UTIL_SLAB_SINGLETHREADED);
248 vc4->blitter = util_blitter_create(pctx);
249 if (!vc4->blitter)
250 goto fail;
251
252 vc4->primconvert = util_primconvert_create(pctx,
253 (1 << PIPE_PRIM_QUADS) - 1);
254 if (!vc4->primconvert)
255 goto fail;
256
257 vc4->uploader = u_upload_create(pctx, 16 * 1024,
258 PIPE_BIND_INDEX_BUFFER);
259
260 vc4_debug |= saved_shaderdb_flag;
261
262 vc4->sample_mask = (1 << VC4_MAX_SAMPLES) - 1;
263
264 return &vc4->base;
265
266 fail:
267 pctx->destroy(pctx);
268 return NULL;
269 }