2 * Copyright © 2017 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
24 #include "util/u_format.h"
25 #include "vc5_context.h"
26 #include "vc5_tiling.h"
27 #include "broadcom/cle/v3d_packet_v33_pack.h"
30 load_raw(struct vc5_cl
*cl
, struct pipe_surface
*psurf
, int buffer
)
32 struct vc5_surface
*surf
= vc5_surface(psurf
);
33 struct vc5_resource
*rsc
= vc5_resource(psurf
->texture
);
35 cl_emit(cl
, LOAD_TILE_BUFFER_GENERAL
, load
) {
37 load
.buffer_to_load
= buffer
;
38 load
.address
= cl_address(rsc
->bo
, surf
->offset
);
39 load
.padded_height_of_output_image_in_uif_blocks
=
40 surf
->padded_height_of_output_image_in_uif_blocks
;
45 store_raw(struct vc5_cl
*cl
, struct pipe_surface
*psurf
, int buffer
,
46 bool color_clear
, bool z_clear
, bool s_clear
)
48 struct vc5_surface
*surf
= vc5_surface(psurf
);
49 struct vc5_resource
*rsc
= vc5_resource(psurf
->texture
);
51 cl_emit(cl
, STORE_TILE_BUFFER_GENERAL
, store
) {
52 store
.raw_mode
= true;
53 store
.buffer_to_store
= buffer
;
54 store
.address
= cl_address(rsc
->bo
, surf
->offset
);
55 store
.disable_colour_buffers_clear_on_write
= !color_clear
;
56 store
.disable_z_buffer_clear_on_write
= !z_clear
;
57 store
.disable_stencil_buffer_clear_on_write
= !s_clear
;
58 store
.padded_height_of_output_image_in_uif_blocks
=
59 surf
->padded_height_of_output_image_in_uif_blocks
;
64 zs_buffer_from_pipe_bits(int pipe_clear_bits
)
66 switch (pipe_clear_bits
& PIPE_CLEAR_DEPTHSTENCIL
) {
67 case PIPE_CLEAR_DEPTHSTENCIL
:
69 case PIPE_CLEAR_DEPTH
:
71 case PIPE_CLEAR_STENCIL
:
78 /* The HW queues up the load until the tile coordinates show up, but can only
79 * track one at a time. If we need to do more than one load, then we need to
80 * flush out the previous load by emitting the tile coordinates and doing a
84 flush_last_load(struct vc5_cl
*cl
)
86 cl_emit(cl
, TILE_COORDINATES_IMPLICIT
, coords
);
87 cl_emit(cl
, STORE_TILE_BUFFER_GENERAL
, store
) {
88 store
.buffer_to_store
= NONE
;
93 vc5_rcl_emit_generic_per_tile_list(struct vc5_job
*job
, int last_cbuf
)
95 /* Emit the generic list in our indirect state -- the rcl will just
96 * have pointers into it.
98 struct vc5_cl
*cl
= &job
->indirect
;
99 vc5_cl_ensure_space(cl
, 200, 1);
100 struct vc5_cl_reloc tile_list_start
= cl_get_address(cl
);
102 const uint32_t pipe_clear_color_buffers
= (PIPE_CLEAR_COLOR0
|
106 const uint32_t first_color_buffer_bit
= (ffs(PIPE_CLEAR_COLOR0
) - 1);
108 uint32_t read_but_not_cleared
= job
->resolve
& ~job
->cleared
;
110 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
111 uint32_t bit
= PIPE_CLEAR_COLOR0
<< i
;
112 if (!(read_but_not_cleared
& bit
))
115 struct pipe_surface
*psurf
= job
->cbufs
[i
];
116 if (!psurf
|| psurf
->texture
->nr_samples
<= 1)
119 load_raw(cl
, psurf
, RENDER_TARGET_0
+ i
);
120 read_but_not_cleared
&= ~bit
;
122 if (read_but_not_cleared
)
126 if (job
->zsbuf
&& job
->zsbuf
->texture
->nr_samples
> 1 &&
127 read_but_not_cleared
& PIPE_CLEAR_DEPTHSTENCIL
) {
128 load_raw(cl
, job
->zsbuf
,
129 zs_buffer_from_pipe_bits(read_but_not_cleared
));
130 read_but_not_cleared
&= ~PIPE_CLEAR_DEPTHSTENCIL
;
131 if (read_but_not_cleared
)
132 cl_emit(cl
, TILE_COORDINATES_IMPLICIT
, coords
);
135 /* The initial reload will be queued until we get the
138 if (read_but_not_cleared
) {
139 cl_emit(cl
, RELOAD_TILE_COLOUR_BUFFER
, load
) {
140 load
.disable_colour_buffer_load
=
141 (~read_but_not_cleared
& pipe_clear_color_buffers
) >>
142 first_color_buffer_bit
;
144 read_but_not_cleared
& PIPE_CLEAR_DEPTH
;
145 load
.enable_stencil_load
=
146 read_but_not_cleared
& PIPE_CLEAR_STENCIL
;
150 /* Tile Coordinates triggers the reload and sets where the stores
151 * go. There must be one per store packet.
153 cl_emit(cl
, TILE_COORDINATES_IMPLICIT
, coords
);
155 /* The binner starts out writing tiles assuming that the initial mode
156 * is triangles, so make sure that's the case.
158 cl_emit(cl
, PRIMITIVE_LIST_FORMAT
, fmt
) {
159 fmt
.data_type
= LIST_INDEXED
;
160 fmt
.primitive_type
= LIST_TRIANGLES
;
163 cl_emit(cl
, BRANCH_TO_IMPLICIT_TILE_LIST
, branch
);
165 bool needs_color_clear
= job
->cleared
& pipe_clear_color_buffers
;
166 bool needs_z_clear
= job
->cleared
& PIPE_CLEAR_DEPTH
;
167 bool needs_s_clear
= job
->cleared
& PIPE_CLEAR_STENCIL
;
168 /* Note that only the color RT being stored will be cleared by a
169 * STORE_GENERAL, or all of them if the buffer is NONE.
171 bool msaa_color_clear
= (needs_color_clear
&&
172 (job
->cleared
& pipe_clear_color_buffers
) ==
173 (job
->resolve
& pipe_clear_color_buffers
));
175 uint32_t stores_pending
= job
->resolve
;
177 /* Use raw stores for any MSAA surfaces. These output UIF tiled
178 * images where each 4x MSAA pixel is a 2x2 quad, and the format will
179 * be that of the internal_type/internal_bpp, rather than the format
180 * from GL's perspective.
182 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
183 uint32_t bit
= PIPE_CLEAR_COLOR0
<< i
;
184 if (!(job
->resolve
& bit
))
187 struct pipe_surface
*psurf
= job
->cbufs
[i
];
188 if (!psurf
|| psurf
->texture
->nr_samples
<= 1)
191 stores_pending
&= ~bit
;
192 store_raw(cl
, psurf
, RENDER_TARGET_0
+ i
,
193 !stores_pending
&& msaa_color_clear
,
194 !stores_pending
&& needs_z_clear
,
195 !stores_pending
&& needs_s_clear
);
198 cl_emit(cl
, TILE_COORDINATES_IMPLICIT
, coords
);
201 if (job
->resolve
& PIPE_CLEAR_DEPTHSTENCIL
&& job
->zsbuf
&&
202 job
->zsbuf
->texture
->nr_samples
> 1) {
203 stores_pending
&= ~PIPE_CLEAR_DEPTHSTENCIL
;
204 store_raw(cl
, job
->zsbuf
,
205 zs_buffer_from_pipe_bits(job
->resolve
),
207 !stores_pending
&& needs_z_clear
,
208 !stores_pending
&& needs_s_clear
);
211 cl_emit(cl
, TILE_COORDINATES_IMPLICIT
, coords
);
214 if (stores_pending
) {
215 cl_emit(cl
, STORE_MULTI_SAMPLE_RESOLVED_TILE_COLOR_BUFFER_EXTENDED
, store
) {
217 store
.disable_color_buffer_write
=
218 (~stores_pending
>> first_color_buffer_bit
) & 0xf;
219 store
.enable_z_write
= stores_pending
& PIPE_CLEAR_DEPTH
;
220 store
.enable_stencil_write
= stores_pending
& PIPE_CLEAR_STENCIL
;
222 /* Note that when set this will clear all of the color
225 store
.disable_colour_buffers_clear_on_write
=
227 store
.disable_z_buffer_clear_on_write
=
229 store
.disable_stencil_buffer_clear_on_write
=
232 } else if (needs_color_clear
&& !msaa_color_clear
) {
233 /* If we had MSAA color stores that didn't match the set of
234 * MSAA color clears, then we need to clear the color buffers
237 cl_emit(&job
->rcl
, STORE_TILE_BUFFER_GENERAL
, store
) {
238 store
.buffer_to_store
= NONE
;
242 cl_emit(cl
, RETURN_FROM_SUB_LIST
, ret
);
244 cl_emit(&job
->rcl
, START_ADDRESS_OF_GENERIC_TILE_LIST
, branch
) {
245 branch
.start
= tile_list_start
;
246 branch
.end
= cl_get_address(cl
);
250 #define div_round_up(a, b) (((a) + (b) - 1) / b)
253 vc5_emit_rcl(struct vc5_job
*job
)
255 /* The RCL list should be empty. */
256 assert(!job
->rcl
.bo
);
258 vc5_cl_ensure_space_with_branch(&job
->rcl
, 200 + 256 *
259 cl_packet_length(SUPERTILE_COORDINATES
));
260 job
->submit
.rcl_start
= job
->rcl
.bo
->offset
;
261 vc5_job_add_bo(job
, job
->rcl
.bo
);
264 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
269 /* Comon config must be the first TILE_RENDERING_MODE_CONFIGURATION
270 * and Z_STENCIL_CLEAR_VALUES must be last. The ones in between are
271 * optional updates to the previous HW state.
273 cl_emit(&job
->rcl
, TILE_RENDERING_MODE_CONFIGURATION_COMMON_CONFIGURATION
,
275 config
.enable_z_store
= job
->resolve
& PIPE_CLEAR_DEPTH
;
276 config
.enable_stencil_store
= job
->resolve
& PIPE_CLEAR_STENCIL
;
278 config
.early_z_disable
= !job
->uses_early_z
;
280 config
.image_width_pixels
= job
->draw_width
;
281 config
.image_height_pixels
= job
->draw_height
;
283 config
.number_of_render_targets_minus_1
=
284 MAX2(nr_cbufs
, 1) - 1;
286 config
.multisample_mode_4x
= job
->msaa
;
288 config
.maximum_bpp_of_all_render_targets
= job
->internal_bpp
;
291 for (int i
= 0; i
< nr_cbufs
; i
++) {
292 struct pipe_surface
*psurf
= job
->cbufs
[i
];
295 struct vc5_surface
*surf
= vc5_surface(psurf
);
296 struct vc5_resource
*rsc
= vc5_resource(psurf
->texture
);
298 uint32_t config_pad
= 0;
299 uint32_t clear_pad
= 0;
301 /* XXX: Set the pad for raster. */
302 if (surf
->tiling
== VC5_TILING_UIF_NO_XOR
||
303 surf
->tiling
== VC5_TILING_UIF_XOR
) {
304 int uif_block_height
= vc5_utile_height(rsc
->cpp
) * 2;
305 uint32_t implicit_padded_height
= (align(job
->draw_height
, uif_block_height
) /
307 if (surf
->padded_height_of_output_image_in_uif_blocks
-
308 implicit_padded_height
< 15) {
309 config_pad
= (surf
->padded_height_of_output_image_in_uif_blocks
-
310 implicit_padded_height
);
313 clear_pad
= surf
->padded_height_of_output_image_in_uif_blocks
;
317 cl_emit(&job
->rcl
, TILE_RENDERING_MODE_CONFIGURATION_RENDER_TARGET_CONFIG
, rt
) {
318 rt
.address
= cl_address(rsc
->bo
, surf
->offset
);
319 rt
.internal_type
= surf
->internal_type
;
320 rt
.output_image_format
= surf
->format
;
321 rt
.memory_format
= surf
->tiling
;
322 rt
.internal_bpp
= surf
->internal_bpp
;
323 rt
.render_target_number
= i
;
326 if (job
->resolve
& PIPE_CLEAR_COLOR0
<< i
)
330 cl_emit(&job
->rcl
, TILE_RENDERING_MODE_CONFIGURATION_CLEAR_COLORS_PART1
,
332 clear
.clear_color_low_32_bits
= job
->clear_color
[i
][0];
333 clear
.clear_color_next_24_bits
= job
->clear_color
[i
][1] & 0xffffff;
334 clear
.render_target_number
= i
;
337 if (surf
->internal_bpp
>= INTERNAL_BPP_64
) {
338 cl_emit(&job
->rcl
, TILE_RENDERING_MODE_CONFIGURATION_CLEAR_COLORS_PART2
,
340 clear
.clear_color_mid_low_32_bits
=
341 ((job
->clear_color
[i
][1] >> 24) |
342 (job
->clear_color
[i
][2] << 8));
343 clear
.clear_color_mid_high_24_bits
=
344 ((job
->clear_color
[i
][2] >> 24) |
345 ((job
->clear_color
[i
][3] & 0xffff) << 8));
346 clear
.render_target_number
= i
;
350 if (surf
->internal_bpp
>= INTERNAL_BPP_128
|| clear_pad
) {
351 cl_emit(&job
->rcl
, TILE_RENDERING_MODE_CONFIGURATION_CLEAR_COLORS_PART3
,
353 clear
.uif_padded_height_in_uif_blocks
= clear_pad
;
354 clear
.clear_color_high_16_bits
= job
->clear_color
[i
][3] >> 16;
355 clear
.render_target_number
= i
;
360 /* TODO: Don't bother emitting if we don't load/clear Z/S. */
362 struct pipe_surface
*psurf
= job
->zsbuf
;
363 struct vc5_surface
*surf
= vc5_surface(psurf
);
364 struct vc5_resource
*rsc
= vc5_resource(psurf
->texture
);
366 cl_emit(&job
->rcl
, TILE_RENDERING_MODE_CONFIGURATION_Z_STENCIL_CONFIG
, zs
) {
367 zs
.address
= cl_address(rsc
->bo
, surf
->offset
);
369 zs
.internal_type
= surf
->internal_type
;
370 zs
.output_image_format
= surf
->format
;
371 zs
.padded_height_of_output_image_in_uif_blocks
=
372 surf
->padded_height_of_output_image_in_uif_blocks
;
374 assert(surf
->tiling
!= VC5_TILING_RASTER
);
375 zs
.memory_format
= surf
->tiling
;
378 if (job
->resolve
& PIPE_CLEAR_DEPTHSTENCIL
)
381 /* Emit the separate stencil packet if we have a resource for
382 * it. The HW will only load/store this buffer if the
383 * Z/Stencil config doesn't have stencil in its format.
385 if (rsc
->separate_stencil
) {
387 TILE_RENDERING_MODE_CONFIGURATION_Z_STENCIL_CONFIG
,
390 cl_address(rsc
->separate_stencil
->bo
,
391 surf
->separate_stencil_offset
);
393 zs
.z_stencil_id
= 1; /* Separate stencil */
395 zs
.padded_height_of_output_image_in_uif_blocks
=
396 surf
->separate_stencil_padded_height_of_output_image_in_uif_blocks
;
398 assert(surf
->tiling
!= VC5_TILING_RASTER
);
399 zs
.memory_format
= surf
->separate_stencil_tiling
;
404 /* Ends rendering mode config. */
405 cl_emit(&job
->rcl
, TILE_RENDERING_MODE_CONFIGURATION_Z_STENCIL_CLEAR_VALUES
,
407 clear
.z_clear_value
= job
->clear_z
;
408 clear
.stencil_vg_mask_clear_value
= job
->clear_s
;
411 /* Always set initial block size before the first branch, which needs
412 * to match the value from binning mode config.
414 cl_emit(&job
->rcl
, TILE_LIST_INITIAL_BLOCK_SIZE
, init
) {
415 init
.use_auto_chained_tile_lists
= true;
416 init
.size_of_first_block_in_chained_tile_lists
=
417 TILE_ALLOCATION_BLOCK_SIZE_64B
;
420 uint32_t supertile_w
= 1, supertile_h
= 1;
422 /* If doing multicore binning, we would need to initialize each core's
425 cl_emit(&job
->rcl
, MULTICORE_RENDERING_TILE_LIST_SET_BASE
, list
) {
426 list
.address
= cl_address(job
->tile_alloc
, 0);
429 cl_emit(&job
->rcl
, MULTICORE_RENDERING_SUPERTILE_CONFIGURATION
, config
) {
430 uint32_t frame_w_in_supertiles
, frame_h_in_supertiles
;
431 const uint32_t max_supertiles
= 256;
433 /* Size up our supertiles until we get under the limit. */
435 frame_w_in_supertiles
= div_round_up(job
->draw_tiles_x
,
437 frame_h_in_supertiles
= div_round_up(job
->draw_tiles_y
,
439 if (frame_w_in_supertiles
* frame_h_in_supertiles
<
444 if (supertile_w
< supertile_h
)
450 config
.total_frame_width_in_tiles
= job
->draw_tiles_x
;
451 config
.total_frame_height_in_tiles
= job
->draw_tiles_y
;
453 config
.supertile_width_in_tiles_minus_1
= supertile_w
- 1;
454 config
.supertile_height_in_tiles_minus_1
= supertile_h
- 1;
456 config
.total_frame_width_in_supertiles
= frame_w_in_supertiles
;
457 config
.total_frame_height_in_supertiles
= frame_h_in_supertiles
;
460 /* Start by clearing the tile buffer. */
461 cl_emit(&job
->rcl
, TILE_COORDINATES
, coords
) {
462 coords
.tile_column_number
= 0;
463 coords
.tile_row_number
= 0;
466 cl_emit(&job
->rcl
, STORE_TILE_BUFFER_GENERAL
, store
) {
467 store
.buffer_to_store
= NONE
;
470 cl_emit(&job
->rcl
, FLUSH_VCD_CACHE
, flush
);
472 vc5_rcl_emit_generic_per_tile_list(job
, nr_cbufs
- 1);
474 cl_emit(&job
->rcl
, WAIT_ON_SEMAPHORE
, sem
);
476 /* XXX: Use Morton order */
477 uint32_t supertile_w_in_pixels
= job
->tile_width
* supertile_w
;
478 uint32_t supertile_h_in_pixels
= job
->tile_height
* supertile_h
;
479 uint32_t min_x_supertile
= job
->draw_min_x
/ supertile_w_in_pixels
;
480 uint32_t min_y_supertile
= job
->draw_min_y
/ supertile_h_in_pixels
;
481 uint32_t max_x_supertile
= (job
->draw_max_x
- 1) / supertile_w_in_pixels
;
482 uint32_t max_y_supertile
= (job
->draw_max_y
- 1) / supertile_h_in_pixels
;
484 for (int y
= min_y_supertile
; y
<= max_y_supertile
; y
++) {
485 for (int x
= min_x_supertile
; x
<= max_x_supertile
; x
++) {
486 cl_emit(&job
->rcl
, SUPERTILE_COORDINATES
, coords
) {
487 coords
.column_number_in_supertiles
= x
;
488 coords
.row_number_in_supertiles
= y
;
493 cl_emit(&job
->rcl
, END_OF_RENDERING
, end
);