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
, ((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
));
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.
79 if (vc4
->cleared
& PIPE_CLEAR_COLOR0
) {
80 cl_u8(&vc4
->rcl
, VC4_PACKET_TILE_COORDINATES
);
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 */
89 for (int y
= 0; y
< ytiles
; y
++) {
90 for (int x
= 0; x
< xtiles
; x
++) {
91 bool end_of_frame
= (x
== xtiles
- 1 &&
94 /* Note that the load doesn't actually occur until the
95 * tile coords packet is processed.
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
);
101 VC4_LOADSTORE_TILE_BUFFER_COLOR
|
103 VC4_LOADSTORE_TILE_BUFFER_FORMAT_SHIFT
));
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
,
112 cl_u8(&vc4
->rcl
, VC4_PACKET_TILE_COORDINATES
);
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);
121 if (vc4
->resolve
& PIPE_CLEAR_COLOR0
) {
124 VC4_PACKET_STORE_MS_TILE_BUFFER_AND_EOF
);
127 VC4_PACKET_STORE_MS_TILE_BUFFER
);
130 assert(!"unfinished: Need to end the frame\n");
137 vc4_flush(struct pipe_context
*pctx
)
139 struct vc4_context
*vc4
= vc4_context(pctx
);
141 if (!vc4
->needs_flush
)
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
);
150 struct drm_vc4_submit_cl submit
;
151 memset(&submit
, 0, sizeof(submit
));
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
;
166 if (!(vc4_debug
& VC4_DEBUG_NORAST
)) {
169 #ifndef USE_VC4_SIMULATOR
170 ret
= drmIoctl(vc4
->fd
, DRM_IOCTL_VC4_SUBMIT_CL
, &submit
);
172 ret
= vc4_simulator_flush(vc4
, &submit
);
175 errx(1, "VC4 submit failed\n");
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;
189 vc4
->needs_flush
= false;
190 vc4
->draw_call_queued
= false;
197 vc4_pipe_flush(struct pipe_context
*pctx
, struct pipe_fence_handle
**fence
,
204 * Flushes the current command lists if they reference the given BO.
206 * This helps avoid flushing the command buffers when unnecessary.
209 vc4_flush_for_bo(struct pipe_context
*pctx
, struct vc4_bo
*bo
)
211 struct vc4_context
*vc4
= vc4_context(pctx
);
213 if (!vc4
->needs_flush
)
216 /* Walk all the referenced BOs in the drawing command list to see if
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
) {
228 /* Also check for the Z/color buffers, since the references to those
229 * are only added immediately before submit.
231 struct vc4_surface
*csurf
= vc4_surface(vc4
->framebuffer
.cbufs
[0]);
233 struct vc4_resource
*ctex
= vc4_resource(csurf
->base
.texture
);
234 if (ctex
->bo
== bo
) {
240 struct vc4_surface
*zsurf
= vc4_surface(vc4
->framebuffer
.zsbuf
);
242 struct vc4_resource
*ztex
=
243 vc4_resource(zsurf
->base
.texture
);
244 if (ztex
->bo
== bo
) {
252 vc4_context_destroy(struct pipe_context
*pctx
)
254 struct vc4_context
*vc4
= vc4_context(pctx
);
257 util_blitter_destroy(vc4
->blitter
);
259 if (vc4
->primconvert
)
260 util_primconvert_destroy(vc4
->primconvert
);
262 util_slab_destroy(&vc4
->transfer_pool
);
267 struct pipe_context
*
268 vc4_context_create(struct pipe_screen
*pscreen
, void *priv
)
270 struct vc4_screen
*screen
= vc4_screen(pscreen
);
271 struct vc4_context
*vc4
;
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
;
277 vc4
= CALLOC_STRUCT(vc4_context
);
280 struct pipe_context
*pctx
= &vc4
->base
;
282 vc4
->screen
= screen
;
284 pctx
->screen
= pscreen
;
286 pctx
->destroy
= vc4_context_destroy
;
287 pctx
->flush
= vc4_pipe_flush
;
290 vc4_state_init(pctx
);
291 vc4_program_init(pctx
);
292 vc4_resource_context_init(pctx
);
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
);
300 vc4
->fd
= screen
->fd
;
302 util_slab_create(&vc4
->transfer_pool
, sizeof(struct vc4_transfer
),
303 16, UTIL_SLAB_SINGLETHREADED
);
304 vc4
->blitter
= util_blitter_create(pctx
);
308 vc4
->primconvert
= util_primconvert_create(pctx
,
309 (1 << PIPE_PRIM_QUADS
) - 1);
310 if (!vc4
->primconvert
)
313 vc4_debug
|= saved_shaderdb_flag
;