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
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"
35 #include "vc4_screen.h"
36 #include "vc4_context.h"
37 #include "vc4_resource.h"
40 dump_fbo(struct vc4_context
*vc4
, struct vc4_bo
*fbo
)
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;
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;
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
];
59 black
|= pix
== 0xff000000;
62 for (i
= 0; i
< num_found_colors
; i
++) {
63 if (pix
== found_colors
[i
])
66 if (i
== num_found_colors
&&
67 num_found_colors
< Elements(found_colors
))
68 found_colors
[num_found_colors
++] = pix
;
78 fprintf(stderr
, "\n");
81 for (int i
= 0; i
< num_found_colors
; i
++) {
82 fprintf(stderr
, "color %d: 0x%08x\n", i
, found_colors
[i
]);
88 vc4_setup_rcl(struct vc4_context
*vc4
)
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;
99 fprintf(stderr
, "RCL: resolve 0x%x clear 0x%x resolve uncleared 0x%x\n",
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);
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
));
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.
125 if (vc4
->cleared
& PIPE_CLEAR_COLOR0
) {
126 cl_u8(&vc4
->rcl
, VC4_PACKET_TILE_COORDINATES
);
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 */
135 for (int y
= 0; y
< ytiles
; y
++) {
136 for (int x
= 0; x
< xtiles
; x
++) {
137 bool end_of_frame
= (x
== xtiles
- 1 &&
140 /* Note that the load doesn't actually occur until the
141 * tile coords packet is processed.
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
);
147 VC4_LOADSTORE_TILE_BUFFER_COLOR
|
148 VC4_LOADSTORE_TILE_BUFFER_FORMAT_RASTER
);
150 VC4_LOADSTORE_TILE_BUFFER_RGBA8888
);
151 cl_reloc(vc4
, &vc4
->rcl
, ctex
->bo
,
155 cl_u8(&vc4
->rcl
, VC4_PACKET_TILE_COORDINATES
);
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);
164 if (vc4
->resolve
& PIPE_CLEAR_COLOR0
) {
167 VC4_PACKET_STORE_MS_TILE_BUFFER_AND_EOF
);
170 VC4_PACKET_STORE_MS_TILE_BUFFER
);
173 assert(!"unfinished: Need to end the frame\n");
180 vc4_flush(struct pipe_context
*pctx
)
182 struct vc4_context
*vc4
= vc4_context(pctx
);
184 if (!vc4
->needs_flush
)
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
);
193 struct vc4_surface
*csurf
= vc4_surface(vc4
->framebuffer
.cbufs
[0]);
194 struct vc4_resource
*ctex
= vc4_resource(csurf
->base
.texture
);
196 struct drm_vc4_submit_cl submit
;
197 memset(&submit
, 0, sizeof(submit
));
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
;
212 if (!(vc4_debug
& VC4_DEBUG_NORAST
)) {
215 #ifndef USE_VC4_SIMULATOR
216 ret
= drmIoctl(vc4
->fd
, DRM_IOCTL_VC4_SUBMIT_CL
, &submit
);
218 ret
= vc4_simulator_flush(vc4
, &submit
, csurf
);
221 errx(1, "VC4 submit failed\n");
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;
235 vc4
->needs_flush
= false;
236 vc4
->draw_call_queued
= false;
241 dump_fbo(vc4
, ctex
->bo
);
245 vc4_pipe_flush(struct pipe_context
*pctx
, struct pipe_fence_handle
**fence
,
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 pipe_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
;