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