vc4: Keep a reference to BOs queued for rendering.
[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 #include <stdio.h>
27
28 #include "pipe/p_defines.h"
29 #include "util/u_inlines.h"
30 #include "util/u_memory.h"
31 #include "util/u_blitter.h"
32 #include "indices/u_primconvert.h"
33 #include "pipe/p_screen.h"
34
35 #include "vc4_screen.h"
36 #include "vc4_context.h"
37 #include "vc4_resource.h"
38
39 static void
40 dump_fbo(struct vc4_context *vc4, struct vc4_bo *fbo)
41 {
42 #ifndef USE_VC4_SIMULATOR
43 uint32_t *map = vc4_bo_map(fbo);
44 uint32_t width = vc4->framebuffer.width;
45 uint32_t height = vc4->framebuffer.height;
46 uint32_t chunk_w = width / 79;
47 uint32_t chunk_h = height / 40;
48 uint32_t found_colors[10];
49 uint32_t num_found_colors = 0;
50
51 for (int by = 0; by < height; by += chunk_h) {
52 for (int bx = 0; bx < width; bx += chunk_w) {
53 bool on = false, black = false;
54
55 for (int y = by; y < MIN2(height, by + chunk_h); y++) {
56 for (int x = bx; x < MIN2(width, bx + chunk_w); x++) {
57 uint32_t pix = map[y * width + x];
58 on |= pix != 0;
59 black |= pix == 0xff000000;
60
61 int i;
62 for (i = 0; i < num_found_colors; i++) {
63 if (pix == found_colors[i])
64 break;
65 }
66 if (i == num_found_colors &&
67 num_found_colors < Elements(found_colors))
68 found_colors[num_found_colors++] = pix;
69 }
70 }
71 if (black)
72 fprintf(stderr, "O");
73 else if (on)
74 fprintf(stderr, "X");
75 else
76 fprintf(stderr, ".");
77 }
78 fprintf(stderr, "\n");
79 }
80
81 for (int i = 0; i < num_found_colors; i++) {
82 fprintf(stderr, "color %d: 0x%08x\n", i, found_colors[i]);
83 }
84 #endif
85 }
86
87 static void
88 vc4_setup_rcl(struct vc4_context *vc4)
89 {
90 struct vc4_surface *csurf = vc4_surface(vc4->framebuffer.cbufs[0]);
91 struct vc4_resource *ctex = vc4_resource(csurf->base.texture);
92 uint32_t resolve_uncleared = vc4->resolve & ~vc4->cleared;
93 uint32_t width = vc4->framebuffer.width;
94 uint32_t height = vc4->framebuffer.height;
95 uint32_t xtiles = align(width, 64) / 64;
96 uint32_t ytiles = align(height, 64) / 64;
97
98 #if 0
99 fprintf(stderr, "RCL: resolve 0x%x clear 0x%x resolve uncleared 0x%x\n",
100 vc4->resolve,
101 vc4->cleared,
102 resolve_uncleared);
103 #endif
104
105 cl_u8(&vc4->rcl, VC4_PACKET_CLEAR_COLORS);
106 cl_u32(&vc4->rcl, vc4->clear_color[0]);
107 cl_u32(&vc4->rcl, vc4->clear_color[1]);
108 cl_u32(&vc4->rcl, 0);
109 cl_u8(&vc4->rcl, 0);
110
111 cl_start_reloc(&vc4->rcl, 1);
112 cl_u8(&vc4->rcl, VC4_PACKET_TILE_RENDERING_MODE_CONFIG);
113 cl_reloc(vc4, &vc4->rcl, ctex->bo, csurf->offset);
114 cl_u16(&vc4->rcl, width);
115 cl_u16(&vc4->rcl, height);
116 cl_u8(&vc4->rcl, (VC4_RENDER_CONFIG_MEMORY_FORMAT_LINEAR |
117 VC4_RENDER_CONFIG_FORMAT_RGBA8888));
118 cl_u8(&vc4->rcl, 0);
119
120 /* The tile buffer normally gets cleared when the previous tile is
121 * stored. If the clear values changed between frames, then the tile
122 * buffer has stale clear values in it, so we have to do a store in
123 * None mode (no writes) so that we trigger the tile buffer clear.
124 */
125 if (vc4->cleared & PIPE_CLEAR_COLOR0) {
126 cl_u8(&vc4->rcl, VC4_PACKET_TILE_COORDINATES);
127 cl_u8(&vc4->rcl, 0);
128 cl_u8(&vc4->rcl, 0);
129
130 cl_u8(&vc4->rcl, VC4_PACKET_STORE_TILE_BUFFER_GENERAL);
131 cl_u16(&vc4->rcl, VC4_LOADSTORE_TILE_BUFFER_NONE);
132 cl_u32(&vc4->rcl, 0); /* no address, since we're in None mode */
133 }
134
135 for (int y = 0; y < ytiles; y++) {
136 for (int x = 0; x < xtiles; x++) {
137 bool end_of_frame = (x == xtiles - 1 &&
138 y == ytiles - 1);
139
140 /* Note that the load doesn't actually occur until the
141 * tile coords packet is processed.
142 */
143 if (resolve_uncleared & PIPE_CLEAR_COLOR) {
144 cl_start_reloc(&vc4->rcl, 1);
145 cl_u8(&vc4->rcl, VC4_PACKET_LOAD_TILE_BUFFER_GENERAL);
146 cl_u8(&vc4->rcl,
147 VC4_LOADSTORE_TILE_BUFFER_COLOR |
148 VC4_LOADSTORE_TILE_BUFFER_FORMAT_RASTER);
149 cl_u8(&vc4->rcl,
150 VC4_LOADSTORE_TILE_BUFFER_RGBA8888);
151 cl_reloc(vc4, &vc4->rcl, ctex->bo,
152 csurf->offset);
153 }
154
155 cl_u8(&vc4->rcl, VC4_PACKET_TILE_COORDINATES);
156 cl_u8(&vc4->rcl, x);
157 cl_u8(&vc4->rcl, y);
158
159 cl_start_reloc(&vc4->rcl, 1);
160 cl_u8(&vc4->rcl, VC4_PACKET_BRANCH_TO_SUB_LIST);
161 cl_reloc(vc4, &vc4->rcl, vc4->tile_alloc,
162 (y * xtiles + x) * 32);
163
164 if (vc4->resolve & PIPE_CLEAR_COLOR0) {
165 if (end_of_frame) {
166 cl_u8(&vc4->rcl,
167 VC4_PACKET_STORE_MS_TILE_BUFFER_AND_EOF);
168 } else {
169 cl_u8(&vc4->rcl,
170 VC4_PACKET_STORE_MS_TILE_BUFFER);
171 }
172 } else {
173 assert(!"unfinished: Need to end the frame\n");
174 }
175 }
176 }
177 }
178
179 void
180 vc4_flush(struct pipe_context *pctx)
181 {
182 struct vc4_context *vc4 = vc4_context(pctx);
183
184 if (!vc4->needs_flush)
185 return;
186
187 cl_u8(&vc4->bcl, VC4_PACKET_FLUSH_ALL);
188 cl_u8(&vc4->bcl, VC4_PACKET_NOP);
189 cl_u8(&vc4->bcl, VC4_PACKET_HALT);
190
191 vc4_setup_rcl(vc4);
192
193 struct vc4_surface *csurf = vc4_surface(vc4->framebuffer.cbufs[0]);
194 struct vc4_resource *ctex = vc4_resource(csurf->base.texture);
195
196 struct drm_vc4_submit_cl submit;
197 memset(&submit, 0, sizeof(submit));
198
199 submit.bo_handles = vc4->bo_handles.base;
200 submit.bo_handle_count = (vc4->bo_handles.next -
201 vc4->bo_handles.base) / 4;
202 submit.bin_cl = vc4->bcl.base;
203 submit.bin_cl_size = vc4->bcl.next - vc4->bcl.base;
204 submit.render_cl = vc4->rcl.base;
205 submit.render_cl_size = vc4->rcl.next - vc4->rcl.base;
206 submit.shader_rec = vc4->shader_rec.base;
207 submit.shader_rec_size = vc4->shader_rec.next - vc4->shader_rec.base;
208 submit.shader_rec_count = vc4->shader_rec_count;
209 submit.uniforms = vc4->uniforms.base;
210 submit.uniforms_size = vc4->uniforms.next - vc4->uniforms.base;
211
212 if (!(vc4_debug & VC4_DEBUG_NORAST)) {
213 int ret;
214
215 #ifndef USE_VC4_SIMULATOR
216 ret = drmIoctl(vc4->fd, DRM_IOCTL_VC4_SUBMIT_CL, &submit);
217 #else
218 ret = vc4_simulator_flush(vc4, &submit, csurf);
219 #endif
220 if (ret)
221 errx(1, "VC4 submit failed\n");
222 }
223
224 vc4_reset_cl(&vc4->bcl);
225 vc4_reset_cl(&vc4->rcl);
226 vc4_reset_cl(&vc4->shader_rec);
227 vc4_reset_cl(&vc4->uniforms);
228 vc4_reset_cl(&vc4->bo_handles);
229 struct vc4_bo **referenced_bos = vc4->bo_pointers.base;
230 for (int i = 0; i < submit.bo_handle_count; i++)
231 vc4_bo_unreference(&referenced_bos[i]);
232 vc4_reset_cl(&vc4->bo_pointers);
233 vc4->shader_rec_count = 0;
234
235 vc4->needs_flush = false;
236 vc4->draw_call_queued = false;
237 vc4->dirty = ~0;
238 vc4->resolve = 0;
239 vc4->cleared = 0;
240
241 dump_fbo(vc4, ctex->bo);
242 }
243
244 static void
245 vc4_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
246 unsigned flags)
247 {
248 vc4_flush(pctx);
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 pipe_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 }