2 * Copyright © 2014 Broadcom
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:
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
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
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"
34 #include "vc4_screen.h"
35 #include "vc4_context.h"
36 #include "vc4_resource.h"
39 vc4_setup_rcl(struct vc4_context
*vc4
)
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;
50 fprintf(stderr
, "RCL: resolve 0x%x clear 0x%x resolve uncleared 0x%x\n",
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
);
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
));
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.
77 if (vc4
->cleared
& PIPE_CLEAR_COLOR0
) {
78 cl_u8(&vc4
->rcl
, VC4_PACKET_TILE_COORDINATES
);
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 */
87 for (int y
= 0; y
< ytiles
; y
++) {
88 for (int x
= 0; x
< xtiles
; x
++) {
89 bool end_of_frame
= (x
== xtiles
- 1 &&
92 /* Note that the load doesn't actually occur until the
93 * tile coords packet is processed.
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
);
99 VC4_LOADSTORE_TILE_BUFFER_COLOR
|
101 VC4_LOADSTORE_TILE_BUFFER_FORMAT_SHIFT
));
103 VC4_LOADSTORE_TILE_BUFFER_RGBA8888
);
104 cl_reloc(vc4
, &vc4
->rcl
, ctex
->bo
,
108 cl_u8(&vc4
->rcl
, VC4_PACKET_TILE_COORDINATES
);
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);
117 if (vc4
->resolve
& PIPE_CLEAR_COLOR0
) {
120 VC4_PACKET_STORE_MS_TILE_BUFFER_AND_EOF
);
123 VC4_PACKET_STORE_MS_TILE_BUFFER
);
126 assert(!"unfinished: Need to end the frame\n");
133 vc4_flush(struct pipe_context
*pctx
)
135 struct vc4_context
*vc4
= vc4_context(pctx
);
137 if (!vc4
->needs_flush
)
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
);
146 struct drm_vc4_submit_cl submit
;
147 memset(&submit
, 0, sizeof(submit
));
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
;
162 if (!(vc4_debug
& VC4_DEBUG_NORAST
)) {
165 #ifndef USE_VC4_SIMULATOR
166 ret
= drmIoctl(vc4
->fd
, DRM_IOCTL_VC4_SUBMIT_CL
, &submit
);
168 ret
= vc4_simulator_flush(vc4
, &submit
);
171 errx(1, "VC4 submit failed\n");
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;
185 vc4
->needs_flush
= false;
186 vc4
->draw_call_queued
= false;
193 vc4_pipe_flush(struct pipe_context
*pctx
, struct pipe_fence_handle
**fence
,
200 * Flushes the current command lists if they reference the given BO.
202 * This helps avoid flushing the command buffers when unnecessary.
205 vc4_flush_for_bo(struct pipe_context
*pctx
, struct vc4_bo
*bo
)
207 struct vc4_context
*vc4
= vc4_context(pctx
);
209 if (!vc4
->needs_flush
)
212 /* Walk all the referenced BOs in the drawing command list to see if
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
) {
224 /* Also check for the Z/color buffers, since the references to those
225 * are only added immediately before submit.
227 struct vc4_surface
*csurf
= vc4_surface(vc4
->framebuffer
.cbufs
[0]);
229 struct vc4_resource
*ctex
= vc4_resource(csurf
->base
.texture
);
230 if (ctex
->bo
== bo
) {
236 struct vc4_surface
*zsurf
= vc4_surface(vc4
->framebuffer
.zsbuf
);
238 struct vc4_resource
*ztex
=
239 vc4_resource(zsurf
->base
.texture
);
240 if (ztex
->bo
== bo
) {
248 vc4_context_destroy(struct pipe_context
*pctx
)
250 struct vc4_context
*vc4
= vc4_context(pctx
);
253 util_blitter_destroy(vc4
->blitter
);
255 if (vc4
->primconvert
)
256 util_primconvert_destroy(vc4
->primconvert
);
258 util_slab_destroy(&vc4
->transfer_pool
);
263 struct pipe_context
*
264 vc4_context_create(struct pipe_screen
*pscreen
, void *priv
)
266 struct vc4_screen
*screen
= vc4_screen(pscreen
);
267 struct vc4_context
*vc4
;
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
;
273 vc4
= CALLOC_STRUCT(vc4_context
);
276 struct pipe_context
*pctx
= &vc4
->base
;
278 vc4
->screen
= screen
;
280 pctx
->screen
= pscreen
;
282 pctx
->destroy
= vc4_context_destroy
;
283 pctx
->flush
= vc4_pipe_flush
;
286 vc4_state_init(pctx
);
287 vc4_program_init(pctx
);
288 vc4_resource_context_init(pctx
);
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
);
296 vc4
->fd
= screen
->fd
;
298 util_slab_create(&vc4
->transfer_pool
, sizeof(struct pipe_transfer
),
299 16, UTIL_SLAB_SINGLETHREADED
);
300 vc4
->blitter
= util_blitter_create(pctx
);
304 vc4
->primconvert
= util_primconvert_create(pctx
,
305 (1 << PIPE_PRIM_QUADS
) - 1);
306 if (!vc4
->primconvert
)
309 vc4_debug
|= saved_shaderdb_flag
;