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