2 * Copyright (c) 2014 Scott Mansell
3 * Copyright © 2014 Broadcom
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include "util/u_prim.h"
26 #include "util/u_format.h"
27 #include "util/u_pack_color.h"
28 #include "util/u_upload_mgr.h"
29 #include "indices/u_primconvert.h"
31 #include "vc4_context.h"
32 #include "vc4_resource.h"
35 vc4_get_draw_cl_space(struct vc4_context
*vc4
)
37 /* Binner gets our packet state -- vc4_emit.c contents,
38 * and the primitive itself.
40 cl_ensure_space(&vc4
->bcl
, 256);
42 /* Nothing for rcl -- that's covered by vc4_context.c */
44 /* shader_rec gets up to 12 dwords of reloc handles plus a maximally
45 * sized shader_rec (104 bytes base for 8 vattrs plus 32 bytes of
48 cl_ensure_space(&vc4
->shader_rec
, 12 * sizeof(uint32_t) + 104 + 8 * 32);
50 /* Uniforms are covered by vc4_write_uniforms(). */
52 /* There could be up to 16 textures per stage, plus misc other
55 cl_ensure_space(&vc4
->bo_handles
, (2 * 16 + 20) * sizeof(uint32_t));
56 cl_ensure_space(&vc4
->bo_pointers
,
57 (2 * 16 + 20) * sizeof(struct vc4_bo
*));
61 * Does the initial bining command list setup for drawing to a given FBO.
64 vc4_start_draw(struct vc4_context
*vc4
)
69 vc4_get_draw_cl_space(vc4
);
71 struct vc4_cl_out
*bcl
= cl_start(&vc4
->bcl
);
72 // Tile state data is 48 bytes per tile, I think it can be thrown away
73 // as soon as binning is finished.
74 cl_u8(&bcl
, VC4_PACKET_TILE_BINNING_MODE_CONFIG
);
75 cl_u32(&bcl
, 0); /* tile alloc addr, filled by kernel */
76 cl_u32(&bcl
, 0); /* tile alloc size, filled by kernel */
77 cl_u32(&bcl
, 0); /* tile state addr, filled by kernel */
78 cl_u8(&bcl
, vc4
->draw_tiles_x
);
79 cl_u8(&bcl
, vc4
->draw_tiles_y
);
80 /* Other flags are filled by kernel. */
81 cl_u8(&bcl
, vc4
->msaa
? VC4_BIN_CONFIG_MS_MODE_4X
: 0);
83 /* START_TILE_BINNING resets the statechange counters in the hardware,
84 * which are what is used when a primitive is binned to a tile to
85 * figure out what new state packets need to be written to that tile's
88 cl_u8(&bcl
, VC4_PACKET_START_TILE_BINNING
);
90 /* Reset the current compressed primitives format. This gets modified
91 * by VC4_PACKET_GL_INDEXED_PRIMITIVE and
92 * VC4_PACKET_GL_ARRAY_PRIMITIVE, so it needs to be reset at the start
95 cl_u8(&bcl
, VC4_PACKET_PRIMITIVE_LIST_FORMAT
);
96 cl_u8(&bcl
, (VC4_PRIMITIVE_LIST_FORMAT_16_INDEX
|
97 VC4_PRIMITIVE_LIST_FORMAT_TYPE_TRIANGLES
));
99 vc4
->needs_flush
= true;
100 vc4
->draw_calls_queued
++;
101 vc4
->draw_width
= vc4
->framebuffer
.width
;
102 vc4
->draw_height
= vc4
->framebuffer
.height
;
104 cl_end(&vc4
->bcl
, bcl
);
108 vc4_update_shadow_textures(struct pipe_context
*pctx
,
109 struct vc4_texture_stateobj
*stage_tex
)
111 for (int i
= 0; i
< stage_tex
->num_textures
; i
++) {
112 struct pipe_sampler_view
*view
= stage_tex
->textures
[i
];
115 struct vc4_resource
*rsc
= vc4_resource(view
->texture
);
116 if (rsc
->shadow_parent
)
117 vc4_update_shadow_baselevel_texture(pctx
, view
);
122 vc4_emit_gl_shader_state(struct vc4_context
*vc4
, const struct pipe_draw_info
*info
)
124 /* VC4_DIRTY_VTXSTATE */
125 struct vc4_vertex_stateobj
*vtx
= vc4
->vtx
;
126 /* VC4_DIRTY_VTXBUF */
127 struct vc4_vertexbuf_stateobj
*vertexbuf
= &vc4
->vertexbuf
;
129 /* The simulator throws a fit if VS or CS don't read an attribute, so
130 * we emit a dummy read.
132 uint32_t num_elements_emit
= MAX2(vtx
->num_elements
, 1);
133 /* Emit the shader record. */
134 struct vc4_cl_out
*shader_rec
=
135 cl_start_shader_reloc(&vc4
->shader_rec
, 3 + num_elements_emit
);
136 /* VC4_DIRTY_PRIM_MODE | VC4_DIRTY_RASTERIZER */
138 VC4_SHADER_FLAG_ENABLE_CLIPPING
|
139 VC4_SHADER_FLAG_FS_SINGLE_THREAD
|
140 ((info
->mode
== PIPE_PRIM_POINTS
&&
141 vc4
->rasterizer
->base
.point_size_per_vertex
) ?
142 VC4_SHADER_FLAG_VS_POINT_SIZE
: 0));
144 /* VC4_DIRTY_COMPILED_FS */
145 cl_u8(&shader_rec
, 0); /* fs num uniforms (unused) */
146 cl_u8(&shader_rec
, vc4
->prog
.fs
->num_inputs
);
147 cl_reloc(vc4
, &vc4
->shader_rec
, &shader_rec
, vc4
->prog
.fs
->bo
, 0);
148 cl_u32(&shader_rec
, 0); /* UBO offset written by kernel */
150 /* VC4_DIRTY_COMPILED_VS */
151 cl_u16(&shader_rec
, 0); /* vs num uniforms */
152 cl_u8(&shader_rec
, vc4
->prog
.vs
->vattrs_live
);
153 cl_u8(&shader_rec
, vc4
->prog
.vs
->vattr_offsets
[8]);
154 cl_reloc(vc4
, &vc4
->shader_rec
, &shader_rec
, vc4
->prog
.vs
->bo
, 0);
155 cl_u32(&shader_rec
, 0); /* UBO offset written by kernel */
157 /* VC4_DIRTY_COMPILED_CS */
158 cl_u16(&shader_rec
, 0); /* cs num uniforms */
159 cl_u8(&shader_rec
, vc4
->prog
.cs
->vattrs_live
);
160 cl_u8(&shader_rec
, vc4
->prog
.cs
->vattr_offsets
[8]);
161 cl_reloc(vc4
, &vc4
->shader_rec
, &shader_rec
, vc4
->prog
.cs
->bo
, 0);
162 cl_u32(&shader_rec
, 0); /* UBO offset written by kernel */
164 uint32_t max_index
= 0xffff;
165 for (int i
= 0; i
< vtx
->num_elements
; i
++) {
166 struct pipe_vertex_element
*elem
= &vtx
->pipe
[i
];
167 struct pipe_vertex_buffer
*vb
=
168 &vertexbuf
->vb
[elem
->vertex_buffer_index
];
169 struct vc4_resource
*rsc
= vc4_resource(vb
->buffer
);
170 /* not vc4->dirty tracked: vc4->last_index_bias */
171 uint32_t offset
= (vb
->buffer_offset
+
173 vb
->stride
* info
->index_bias
);
174 uint32_t vb_size
= rsc
->bo
->size
- offset
;
176 util_format_get_blocksize(elem
->src_format
);
178 cl_reloc(vc4
, &vc4
->shader_rec
, &shader_rec
, rsc
->bo
, offset
);
179 cl_u8(&shader_rec
, elem_size
- 1);
180 cl_u8(&shader_rec
, vb
->stride
);
181 cl_u8(&shader_rec
, vc4
->prog
.vs
->vattr_offsets
[i
]);
182 cl_u8(&shader_rec
, vc4
->prog
.cs
->vattr_offsets
[i
]);
184 if (vb
->stride
> 0) {
185 max_index
= MIN2(max_index
,
186 (vb_size
- elem_size
) / vb
->stride
);
190 if (vtx
->num_elements
== 0) {
191 assert(num_elements_emit
== 1);
192 struct vc4_bo
*bo
= vc4_bo_alloc(vc4
->screen
, 4096, "scratch VBO");
193 cl_reloc(vc4
, &vc4
->shader_rec
, &shader_rec
, bo
, 0);
194 cl_u8(&shader_rec
, 16 - 1); /* element size */
195 cl_u8(&shader_rec
, 0); /* stride */
196 cl_u8(&shader_rec
, 0); /* VS VPM offset */
197 cl_u8(&shader_rec
, 0); /* CS VPM offset */
198 vc4_bo_unreference(&bo
);
200 cl_end(&vc4
->shader_rec
, shader_rec
);
202 struct vc4_cl_out
*bcl
= cl_start(&vc4
->bcl
);
203 /* the actual draw call. */
204 cl_u8(&bcl
, VC4_PACKET_GL_SHADER_STATE
);
205 assert(vtx
->num_elements
<= 8);
206 /* Note that number of attributes == 0 in the packet means 8
207 * attributes. This field also contains the offset into shader_rec.
209 cl_u32(&bcl
, num_elements_emit
& 0x7);
210 cl_end(&vc4
->bcl
, bcl
);
212 vc4_write_uniforms(vc4
, vc4
->prog
.fs
,
213 &vc4
->constbuf
[PIPE_SHADER_FRAGMENT
],
215 vc4_write_uniforms(vc4
, vc4
->prog
.vs
,
216 &vc4
->constbuf
[PIPE_SHADER_VERTEX
],
218 vc4_write_uniforms(vc4
, vc4
->prog
.cs
,
219 &vc4
->constbuf
[PIPE_SHADER_VERTEX
],
222 vc4
->last_index_bias
= info
->index_bias
;
223 vc4
->max_index
= max_index
;
227 * HW-2116 workaround: Flush the batch before triggering the hardware state
228 * counter wraparound behavior.
230 * State updates are tracked by a global counter which increments at the first
231 * state update after a draw or a START_BINNING. Tiles can then have their
232 * state updated at draw time with a set of cheap checks for whether the
233 * state's copy of the global counter matches the global counter the last time
234 * that state was written to the tile.
236 * The state counters are relatively small and wrap around quickly, so you
237 * could get false negatives for needing to update a particular state in the
238 * tile. To avoid this, the hardware attempts to write all of the state in
239 * the tile at wraparound time. This apparently is broken, so we just flush
240 * everything before that behavior is triggered. A batch flush is sufficient
241 * to get our current contents drawn and reset the counters to 0.
243 * Note that we can't just use VC4_PACKET_FLUSH_ALL, because that caps the
244 * tiles with VC4_PACKET_RETURN_FROM_LIST.
247 vc4_hw_2116_workaround(struct pipe_context
*pctx
)
249 struct vc4_context
*vc4
= vc4_context(pctx
);
251 if (vc4
->draw_calls_queued
== 0x1ef0) {
252 perf_debug("Flushing batch due to HW-2116 workaround "
253 "(too many draw calls per scene\n");
259 vc4_draw_vbo(struct pipe_context
*pctx
, const struct pipe_draw_info
*info
)
261 struct vc4_context
*vc4
= vc4_context(pctx
);
263 if (info
->mode
>= PIPE_PRIM_QUADS
) {
264 util_primconvert_save_index_buffer(vc4
->primconvert
, &vc4
->indexbuf
);
265 util_primconvert_save_rasterizer_state(vc4
->primconvert
, &vc4
->rasterizer
->base
);
266 util_primconvert_draw_vbo(vc4
->primconvert
, info
);
267 perf_debug("Fallback conversion for %d %s vertices\n",
268 info
->count
, u_prim_name(info
->mode
));
272 /* Before setting up the draw, do any fixup blits necessary. */
273 vc4_update_shadow_textures(pctx
, &vc4
->verttex
);
274 vc4_update_shadow_textures(pctx
, &vc4
->fragtex
);
276 vc4_hw_2116_workaround(pctx
);
278 vc4_get_draw_cl_space(vc4
);
280 if (vc4
->prim_mode
!= info
->mode
) {
281 vc4
->prim_mode
= info
->mode
;
282 vc4
->dirty
|= VC4_DIRTY_PRIM_MODE
;
286 vc4_update_compiled_shaders(vc4
, info
->mode
);
288 vc4_emit_state(pctx
);
290 if ((vc4
->dirty
& (VC4_DIRTY_VTXBUF
|
292 VC4_DIRTY_PRIM_MODE
|
293 VC4_DIRTY_RASTERIZER
|
294 VC4_DIRTY_COMPILED_CS
|
295 VC4_DIRTY_COMPILED_VS
|
296 VC4_DIRTY_COMPILED_FS
|
297 vc4
->prog
.cs
->uniform_dirty_bits
|
298 vc4
->prog
.vs
->uniform_dirty_bits
|
299 vc4
->prog
.fs
->uniform_dirty_bits
)) ||
300 vc4
->last_index_bias
!= info
->index_bias
) {
301 vc4_emit_gl_shader_state(vc4
, info
);
306 /* Note that the primitive type fields match with OpenGL/gallium
307 * definitions, up to but not including QUADS.
309 struct vc4_cl_out
*bcl
= cl_start(&vc4
->bcl
);
311 uint32_t offset
= vc4
->indexbuf
.offset
;
312 uint32_t index_size
= vc4
->indexbuf
.index_size
;
313 struct pipe_resource
*prsc
;
314 if (vc4
->indexbuf
.index_size
== 4) {
315 prsc
= vc4_get_shadow_index_buffer(pctx
, &vc4
->indexbuf
,
316 info
->count
, &offset
);
319 if (vc4
->indexbuf
.user_buffer
) {
321 u_upload_data(vc4
->uploader
, 0,
322 info
->count
* index_size
, 4,
323 vc4
->indexbuf
.user_buffer
,
326 prsc
= vc4
->indexbuf
.buffer
;
329 struct vc4_resource
*rsc
= vc4_resource(prsc
);
331 cl_start_reloc(&vc4
->bcl
, &bcl
, 1);
332 cl_u8(&bcl
, VC4_PACKET_GL_INDEXED_PRIMITIVE
);
336 VC4_INDEX_BUFFER_U16
:
337 VC4_INDEX_BUFFER_U8
));
338 cl_u32(&bcl
, info
->count
);
339 cl_reloc(vc4
, &vc4
->bcl
, &bcl
, rsc
->bo
, offset
);
340 cl_u32(&bcl
, vc4
->max_index
);
342 if (vc4
->indexbuf
.index_size
== 4 || vc4
->indexbuf
.user_buffer
)
343 pipe_resource_reference(&prsc
, NULL
);
345 cl_u8(&bcl
, VC4_PACKET_GL_ARRAY_PRIMITIVE
);
346 cl_u8(&bcl
, info
->mode
);
347 cl_u32(&bcl
, info
->count
);
348 cl_u32(&bcl
, info
->start
);
350 cl_end(&vc4
->bcl
, bcl
);
352 if (vc4
->zsa
&& vc4
->zsa
->base
.depth
.enabled
) {
353 vc4
->resolve
|= PIPE_CLEAR_DEPTH
;
355 if (vc4
->zsa
&& vc4
->zsa
->base
.stencil
[0].enabled
)
356 vc4
->resolve
|= PIPE_CLEAR_STENCIL
;
357 vc4
->resolve
|= PIPE_CLEAR_COLOR0
;
359 vc4
->shader_rec_count
++;
361 if (vc4_debug
& VC4_DEBUG_ALWAYS_FLUSH
)
366 pack_rgba(enum pipe_format format
, const float *rgba
)
369 util_pack_color(rgba
, format
, &uc
);
370 if (util_format_get_blocksize(format
) == 2)
377 vc4_clear(struct pipe_context
*pctx
, unsigned buffers
,
378 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
380 struct vc4_context
*vc4
= vc4_context(pctx
);
382 /* We can't flag new buffers for clearing once we've queued draws. We
383 * could avoid this by using the 3d engine to clear.
385 if (vc4
->draw_calls_queued
) {
386 perf_debug("Flushing rendering to process new clear.\n");
390 if (buffers
& PIPE_CLEAR_COLOR0
) {
391 vc4
->clear_color
[0] = vc4
->clear_color
[1] =
392 pack_rgba(vc4
->framebuffer
.cbufs
[0]->format
,
396 if (buffers
& PIPE_CLEAR_DEPTH
) {
397 /* Though the depth buffer is stored with Z in the high 24,
398 * for this field we just need to store it in the low 24.
400 vc4
->clear_depth
= util_pack_z(PIPE_FORMAT_Z24X8_UNORM
, depth
);
403 if (buffers
& PIPE_CLEAR_STENCIL
)
404 vc4
->clear_stencil
= stencil
;
408 vc4
->draw_max_x
= vc4
->framebuffer
.width
;
409 vc4
->draw_max_y
= vc4
->framebuffer
.height
;
410 vc4
->cleared
|= buffers
;
411 vc4
->resolve
|= buffers
;
417 vc4_clear_render_target(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
418 const union pipe_color_union
*color
,
419 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
421 fprintf(stderr
, "unimpl: clear RT\n");
425 vc4_clear_depth_stencil(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
426 unsigned buffers
, double depth
, unsigned stencil
,
427 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
429 fprintf(stderr
, "unimpl: clear DS\n");
433 vc4_draw_init(struct pipe_context
*pctx
)
435 pctx
->draw_vbo
= vc4_draw_vbo
;
436 pctx
->clear
= vc4_clear
;
437 pctx
->clear_render_target
= vc4_clear_render_target
;
438 pctx
->clear_depth_stencil
= vc4_clear_depth_stencil
;