91ff7d784e92e51ccf7a87c5673c2404b76cc260
[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/u_inlines.h"
29 #include "util/u_memory.h"
30 #include "util/u_blitter.h"
31 #include "indices/u_primconvert.h"
32 #include "pipe/p_screen.h"
33
34 #include "vc4_screen.h"
35 #include "vc4_context.h"
36 #include "vc4_resource.h"
37
38 static void
39 vc4_setup_rcl(struct vc4_context *vc4)
40 {
41 struct vc4_surface *csurf = vc4_surface(vc4->framebuffer.cbufs[0]);
42 struct vc4_resource *ctex = vc4_resource(csurf->base.texture);
43 uint32_t resolve_uncleared = vc4->resolve & ~vc4->cleared;
44 uint32_t width = vc4->framebuffer.width;
45 uint32_t height = vc4->framebuffer.height;
46 uint32_t xtiles = align(width, 64) / 64;
47 uint32_t ytiles = align(height, 64) / 64;
48
49 #if 0
50 fprintf(stderr, "RCL: resolve 0x%x clear 0x%x resolve uncleared 0x%x\n",
51 vc4->resolve,
52 vc4->cleared,
53 resolve_uncleared);
54 #endif
55
56 cl_u8(&vc4->rcl, VC4_PACKET_CLEAR_COLORS);
57 cl_u32(&vc4->rcl, vc4->clear_color[0]);
58 cl_u32(&vc4->rcl, vc4->clear_color[1]);
59 cl_u32(&vc4->rcl, vc4->clear_depth);
60 cl_u8(&vc4->rcl, 0);
61
62 cl_start_reloc(&vc4->rcl, 1);
63 cl_u8(&vc4->rcl, VC4_PACKET_TILE_RENDERING_MODE_CONFIG);
64 cl_reloc(vc4, &vc4->rcl, ctex->bo, csurf->offset);
65 cl_u16(&vc4->rcl, width);
66 cl_u16(&vc4->rcl, height);
67 cl_u16(&vc4->rcl, ((csurf->tiling <<
68 VC4_RENDER_CONFIG_MEMORY_FORMAT_SHIFT) |
69 (vc4_rt_format_is_565(csurf->base.format) ?
70 VC4_RENDER_CONFIG_FORMAT_BGR565 :
71 VC4_RENDER_CONFIG_FORMAT_RGBA8888) |
72 VC4_RENDER_CONFIG_EARLY_Z_COVERAGE_DISABLE));
73
74 /* The tile buffer normally gets cleared when the previous tile is
75 * stored. If the clear values changed between frames, then the tile
76 * buffer has stale clear values in it, so we have to do a store in
77 * None mode (no writes) so that we trigger the tile buffer clear.
78 */
79 if (vc4->cleared & PIPE_CLEAR_COLOR0) {
80 cl_u8(&vc4->rcl, VC4_PACKET_TILE_COORDINATES);
81 cl_u8(&vc4->rcl, 0);
82 cl_u8(&vc4->rcl, 0);
83
84 cl_u8(&vc4->rcl, VC4_PACKET_STORE_TILE_BUFFER_GENERAL);
85 cl_u16(&vc4->rcl, VC4_LOADSTORE_TILE_BUFFER_NONE);
86 cl_u32(&vc4->rcl, 0); /* no address, since we're in None mode */
87 }
88
89 for (int y = 0; y < ytiles; y++) {
90 for (int x = 0; x < xtiles; x++) {
91 bool end_of_frame = (x == xtiles - 1 &&
92 y == ytiles - 1);
93
94 /* Note that the load doesn't actually occur until the
95 * tile coords packet is processed.
96 */
97 if (resolve_uncleared & PIPE_CLEAR_COLOR) {
98 cl_start_reloc(&vc4->rcl, 1);
99 cl_u8(&vc4->rcl, VC4_PACKET_LOAD_TILE_BUFFER_GENERAL);
100 cl_u8(&vc4->rcl,
101 VC4_LOADSTORE_TILE_BUFFER_COLOR |
102 (csurf->tiling <<
103 VC4_LOADSTORE_TILE_BUFFER_FORMAT_SHIFT));
104 cl_u8(&vc4->rcl,
105 vc4_rt_format_is_565(csurf->base.format) ?
106 VC4_LOADSTORE_TILE_BUFFER_BGR565 :
107 VC4_LOADSTORE_TILE_BUFFER_RGBA8888);
108 cl_reloc(vc4, &vc4->rcl, ctex->bo,
109 csurf->offset);
110 }
111
112 cl_u8(&vc4->rcl, VC4_PACKET_TILE_COORDINATES);
113 cl_u8(&vc4->rcl, x);
114 cl_u8(&vc4->rcl, y);
115
116 cl_start_reloc(&vc4->rcl, 1);
117 cl_u8(&vc4->rcl, VC4_PACKET_BRANCH_TO_SUB_LIST);
118 cl_reloc(vc4, &vc4->rcl, vc4->tile_alloc,
119 (y * xtiles + x) * 32);
120
121 if (vc4->resolve & PIPE_CLEAR_COLOR0) {
122 if (end_of_frame) {
123 cl_u8(&vc4->rcl,
124 VC4_PACKET_STORE_MS_TILE_BUFFER_AND_EOF);
125 } else {
126 cl_u8(&vc4->rcl,
127 VC4_PACKET_STORE_MS_TILE_BUFFER);
128 }
129 } else {
130 assert(!"unfinished: Need to end the frame\n");
131 }
132 }
133 }
134 }
135
136 void
137 vc4_flush(struct pipe_context *pctx)
138 {
139 struct vc4_context *vc4 = vc4_context(pctx);
140
141 if (!vc4->needs_flush)
142 return;
143
144 cl_u8(&vc4->bcl, VC4_PACKET_FLUSH_ALL);
145 cl_u8(&vc4->bcl, VC4_PACKET_NOP);
146 cl_u8(&vc4->bcl, VC4_PACKET_HALT);
147
148 vc4_setup_rcl(vc4);
149
150 struct drm_vc4_submit_cl submit;
151 memset(&submit, 0, sizeof(submit));
152
153 submit.bo_handles = vc4->bo_handles.base;
154 submit.bo_handle_count = (vc4->bo_handles.next -
155 vc4->bo_handles.base) / 4;
156 submit.bin_cl = vc4->bcl.base;
157 submit.bin_cl_size = vc4->bcl.next - vc4->bcl.base;
158 submit.render_cl = vc4->rcl.base;
159 submit.render_cl_size = vc4->rcl.next - vc4->rcl.base;
160 submit.shader_rec = vc4->shader_rec.base;
161 submit.shader_rec_size = vc4->shader_rec.next - vc4->shader_rec.base;
162 submit.shader_rec_count = vc4->shader_rec_count;
163 submit.uniforms = vc4->uniforms.base;
164 submit.uniforms_size = vc4->uniforms.next - vc4->uniforms.base;
165
166 if (!(vc4_debug & VC4_DEBUG_NORAST)) {
167 int ret;
168
169 #ifndef USE_VC4_SIMULATOR
170 ret = drmIoctl(vc4->fd, DRM_IOCTL_VC4_SUBMIT_CL, &submit);
171 #else
172 ret = vc4_simulator_flush(vc4, &submit);
173 #endif
174 if (ret)
175 errx(1, "VC4 submit failed\n");
176 }
177
178 vc4_reset_cl(&vc4->bcl);
179 vc4_reset_cl(&vc4->rcl);
180 vc4_reset_cl(&vc4->shader_rec);
181 vc4_reset_cl(&vc4->uniforms);
182 vc4_reset_cl(&vc4->bo_handles);
183 struct vc4_bo **referenced_bos = vc4->bo_pointers.base;
184 for (int i = 0; i < submit.bo_handle_count; i++)
185 vc4_bo_unreference(&referenced_bos[i]);
186 vc4_reset_cl(&vc4->bo_pointers);
187 vc4->shader_rec_count = 0;
188
189 vc4->needs_flush = false;
190 vc4->draw_call_queued = false;
191 vc4->dirty = ~0;
192 vc4->resolve = 0;
193 vc4->cleared = 0;
194 }
195
196 static void
197 vc4_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
198 unsigned flags)
199 {
200 vc4_flush(pctx);
201 }
202
203 /**
204 * Flushes the current command lists if they reference the given BO.
205 *
206 * This helps avoid flushing the command buffers when unnecessary.
207 */
208 void
209 vc4_flush_for_bo(struct pipe_context *pctx, struct vc4_bo *bo)
210 {
211 struct vc4_context *vc4 = vc4_context(pctx);
212
213 if (!vc4->needs_flush)
214 return;
215
216 /* Walk all the referenced BOs in the drawing command list to see if
217 * they match.
218 */
219 struct vc4_bo **referenced_bos = vc4->bo_pointers.base;
220 for (int i = 0; i < (vc4->bo_handles.next -
221 vc4->bo_handles.base) / 4; i++) {
222 if (referenced_bos[i] == bo) {
223 vc4_flush(pctx);
224 return;
225 }
226 }
227
228 /* Also check for the Z/color buffers, since the references to those
229 * are only added immediately before submit.
230 */
231 struct vc4_surface *csurf = vc4_surface(vc4->framebuffer.cbufs[0]);
232 if (csurf) {
233 struct vc4_resource *ctex = vc4_resource(csurf->base.texture);
234 if (ctex->bo == bo) {
235 vc4_flush(pctx);
236 return;
237 }
238 }
239
240 struct vc4_surface *zsurf = vc4_surface(vc4->framebuffer.zsbuf);
241 if (zsurf) {
242 struct vc4_resource *ztex =
243 vc4_resource(zsurf->base.texture);
244 if (ztex->bo == bo) {
245 vc4_flush(pctx);
246 return;
247 }
248 }
249 }
250
251 static void
252 vc4_context_destroy(struct pipe_context *pctx)
253 {
254 struct vc4_context *vc4 = vc4_context(pctx);
255
256 if (vc4->blitter)
257 util_blitter_destroy(vc4->blitter);
258
259 if (vc4->primconvert)
260 util_primconvert_destroy(vc4->primconvert);
261
262 util_slab_destroy(&vc4->transfer_pool);
263
264 free(vc4);
265 }
266
267 struct pipe_context *
268 vc4_context_create(struct pipe_screen *pscreen, void *priv)
269 {
270 struct vc4_screen *screen = vc4_screen(pscreen);
271 struct vc4_context *vc4;
272
273 /* Prevent dumping of the shaders built during context setup. */
274 uint32_t saved_shaderdb_flag = vc4_debug & VC4_DEBUG_SHADERDB;
275 vc4_debug &= ~VC4_DEBUG_SHADERDB;
276
277 vc4 = CALLOC_STRUCT(vc4_context);
278 if (vc4 == NULL)
279 return NULL;
280 struct pipe_context *pctx = &vc4->base;
281
282 vc4->screen = screen;
283
284 pctx->screen = pscreen;
285 pctx->priv = priv;
286 pctx->destroy = vc4_context_destroy;
287 pctx->flush = vc4_pipe_flush;
288
289 vc4_draw_init(pctx);
290 vc4_state_init(pctx);
291 vc4_program_init(pctx);
292 vc4_resource_context_init(pctx);
293
294 vc4_init_cl(vc4, &vc4->bcl);
295 vc4_init_cl(vc4, &vc4->rcl);
296 vc4_init_cl(vc4, &vc4->shader_rec);
297 vc4_init_cl(vc4, &vc4->bo_handles);
298
299 vc4->dirty = ~0;
300 vc4->fd = screen->fd;
301
302 util_slab_create(&vc4->transfer_pool, sizeof(struct vc4_transfer),
303 16, UTIL_SLAB_SINGLETHREADED);
304 vc4->blitter = util_blitter_create(pctx);
305 if (!vc4->blitter)
306 goto fail;
307
308 vc4->primconvert = util_primconvert_create(pctx,
309 (1 << PIPE_PRIM_QUADS) - 1);
310 if (!vc4->primconvert)
311 goto fail;
312
313 vc4_debug |= saved_shaderdb_flag;
314
315 return &vc4->base;
316
317 fail:
318 pctx->destroy(pctx);
319 return NULL;
320 }