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