2 * Copyright © 2014-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_blitter.h"
25 #include "util/u_prim.h"
26 #include "util/u_format.h"
27 #include "util/u_pack_color.h"
28 #include "util/u_prim_restart.h"
29 #include "util/u_upload_mgr.h"
30 #include "indices/u_primconvert.h"
32 #include "v3d_context.h"
33 #include "v3d_resource.h"
35 #include "broadcom/compiler/v3d_compiler.h"
36 #include "broadcom/common/v3d_macros.h"
37 #include "broadcom/cle/v3dx_pack.h"
40 * Does the initial bining command list setup for drawing to a given FBO.
43 v3d_start_draw(struct v3d_context
*v3d
)
45 struct v3d_job
*job
= v3d
->job
;
50 /* Get space to emit our BCL state, using a branch to jump to a new BO
53 v3d_cl_ensure_space_with_branch(&job
->bcl
, 256 /* XXX */);
55 job
->submit
.bcl_start
= job
->bcl
.bo
->offset
;
56 v3d_job_add_bo(job
, job
->bcl
.bo
);
58 job
->tile_alloc
= v3d_bo_alloc(v3d
->screen
, 1024 * 1024, "tile alloc");
59 uint32_t tsda_per_tile_size
= v3d
->screen
->devinfo
.ver
>= 40 ? 256 : 64;
60 job
->tile_state
= v3d_bo_alloc(v3d
->screen
,
67 /* "Binning mode lists start with a Tile Binning Mode Configuration
70 * Part1 signals the end of binning config setup.
72 cl_emit(&job
->bcl
, TILE_BINNING_MODE_CONFIGURATION_PART2
, config
) {
73 config
.tile_allocation_memory_address
=
74 cl_address(job
->tile_alloc
, 0);
75 config
.tile_allocation_memory_size
= job
->tile_alloc
->size
;
79 cl_emit(&job
->bcl
, TILE_BINNING_MODE_CONFIGURATION_PART1
, config
) {
81 config
.width_in_pixels
= v3d
->framebuffer
.width
;
82 config
.height_in_pixels
= v3d
->framebuffer
.height
;
83 config
.number_of_render_targets
=
84 MAX2(v3d
->framebuffer
.nr_cbufs
, 1);
85 #else /* V3D_VERSION < 40 */
86 config
.tile_state_data_array_base_address
=
87 cl_address(job
->tile_state
, 0);
89 config
.width_in_tiles
= job
->draw_tiles_x
;
90 config
.height_in_tiles
= job
->draw_tiles_y
;
92 config
.number_of_render_targets
=
93 MAX2(v3d
->framebuffer
.nr_cbufs
, 1);
94 #endif /* V3D_VERSION < 40 */
96 config
.multisample_mode_4x
= job
->msaa
;
98 config
.maximum_bpp_of_all_render_targets
= job
->internal_bpp
;
101 /* There's definitely nothing in the VCD cache we want. */
102 cl_emit(&job
->bcl
, FLUSH_VCD_CACHE
, bin
);
104 /* Disable any leftover OQ state from another job. */
105 cl_emit(&job
->bcl
, OCCLUSION_QUERY_COUNTER
, counter
);
107 /* "Binning mode lists must have a Start Tile Binning item (6) after
108 * any prefix state data before the binning list proper starts."
110 cl_emit(&job
->bcl
, START_TILE_BINNING
, bin
);
112 job
->needs_flush
= true;
113 job
->draw_width
= v3d
->framebuffer
.width
;
114 job
->draw_height
= v3d
->framebuffer
.height
;
118 v3d_predraw_check_textures(struct pipe_context
*pctx
,
119 struct v3d_texture_stateobj
*stage_tex
)
121 struct v3d_context
*v3d
= v3d_context(pctx
);
123 for (int i
= 0; i
< stage_tex
->num_textures
; i
++) {
124 struct pipe_sampler_view
*view
= stage_tex
->textures
[i
];
128 v3d_flush_jobs_writing_resource(v3d
, view
->texture
);
133 v3d_emit_gl_shader_state(struct v3d_context
*v3d
,
134 const struct pipe_draw_info
*info
)
136 struct v3d_job
*job
= v3d
->job
;
137 /* VC5_DIRTY_VTXSTATE */
138 struct v3d_vertex_stateobj
*vtx
= v3d
->vtx
;
139 /* VC5_DIRTY_VTXBUF */
140 struct v3d_vertexbuf_stateobj
*vertexbuf
= &v3d
->vertexbuf
;
142 /* Upload the uniforms to the indirect CL first */
143 struct v3d_cl_reloc fs_uniforms
=
144 v3d_write_uniforms(v3d
, v3d
->prog
.fs
,
145 &v3d
->constbuf
[PIPE_SHADER_FRAGMENT
],
147 struct v3d_cl_reloc vs_uniforms
=
148 v3d_write_uniforms(v3d
, v3d
->prog
.vs
,
149 &v3d
->constbuf
[PIPE_SHADER_VERTEX
],
151 struct v3d_cl_reloc cs_uniforms
=
152 v3d_write_uniforms(v3d
, v3d
->prog
.cs
,
153 &v3d
->constbuf
[PIPE_SHADER_VERTEX
],
156 /* See GFXH-930 workaround below */
157 uint32_t num_elements_to_emit
= MAX2(vtx
->num_elements
, 1);
158 uint32_t shader_rec_offset
=
159 v3d_cl_ensure_space(&job
->indirect
,
160 cl_packet_length(GL_SHADER_STATE_RECORD
) +
161 num_elements_to_emit
*
162 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD
),
165 cl_emit(&job
->indirect
, GL_SHADER_STATE_RECORD
, shader
) {
166 shader
.enable_clipping
= true;
167 /* VC5_DIRTY_PRIM_MODE | VC5_DIRTY_RASTERIZER */
168 shader
.point_size_in_shaded_vertex_data
=
169 (info
->mode
== PIPE_PRIM_POINTS
&&
170 v3d
->rasterizer
->base
.point_size_per_vertex
);
172 /* Must be set if the shader modifies Z, discards, or modifies
173 * the sample mask. For any of these cases, the fragment
174 * shader needs to write the Z value (even just discards).
176 shader
.fragment_shader_does_z_writes
=
177 (v3d
->prog
.fs
->prog_data
.fs
->writes_z
||
178 v3d
->prog
.fs
->prog_data
.fs
->discard
);
180 shader
.fragment_shader_uses_real_pixel_centre_w_in_addition_to_centroid_w2
=
181 v3d
->prog
.fs
->prog_data
.fs
->uses_center_w
;
183 shader
.number_of_varyings_in_fragment_shader
=
184 v3d
->prog
.fs
->prog_data
.base
->num_inputs
;
186 shader
.coordinate_shader_propagate_nans
= true;
187 shader
.vertex_shader_propagate_nans
= true;
188 shader
.fragment_shader_propagate_nans
= true;
190 shader
.coordinate_shader_code_address
=
191 cl_address(v3d
->prog
.cs
->bo
, 0);
192 shader
.vertex_shader_code_address
=
193 cl_address(v3d
->prog
.vs
->bo
, 0);
194 shader
.fragment_shader_code_address
=
195 cl_address(v3d
->prog
.fs
->bo
, 0);
197 /* XXX: Use combined input/output size flag in the common
200 shader
.coordinate_shader_has_separate_input_and_output_vpm_blocks
= true;
201 shader
.vertex_shader_has_separate_input_and_output_vpm_blocks
= true;
202 shader
.coordinate_shader_input_vpm_segment_size
=
203 MAX2(v3d
->prog
.cs
->prog_data
.vs
->vpm_input_size
, 1);
204 shader
.vertex_shader_input_vpm_segment_size
=
205 MAX2(v3d
->prog
.vs
->prog_data
.vs
->vpm_input_size
, 1);
207 shader
.coordinate_shader_output_vpm_segment_size
=
208 v3d
->prog
.cs
->prog_data
.vs
->vpm_output_size
;
209 shader
.vertex_shader_output_vpm_segment_size
=
210 v3d
->prog
.vs
->prog_data
.vs
->vpm_output_size
;
212 shader
.coordinate_shader_uniforms_address
= cs_uniforms
;
213 shader
.vertex_shader_uniforms_address
= vs_uniforms
;
214 shader
.fragment_shader_uniforms_address
= fs_uniforms
;
216 #if V3D_VERSION >= 41
217 shader
.min_coord_shader_input_segments_required_in_play
= 1;
218 shader
.min_vertex_shader_input_segments_required_in_play
= 1;
220 shader
.coordinate_shader_4_way_threadable
=
221 v3d
->prog
.cs
->prog_data
.vs
->base
.threads
== 4;
222 shader
.vertex_shader_4_way_threadable
=
223 v3d
->prog
.vs
->prog_data
.vs
->base
.threads
== 4;
224 shader
.fragment_shader_4_way_threadable
=
225 v3d
->prog
.fs
->prog_data
.fs
->base
.threads
== 4;
227 shader
.coordinate_shader_start_in_final_thread_section
=
228 v3d
->prog
.cs
->prog_data
.vs
->base
.single_seg
;
229 shader
.vertex_shader_start_in_final_thread_section
=
230 v3d
->prog
.vs
->prog_data
.vs
->base
.single_seg
;
231 shader
.fragment_shader_start_in_final_thread_section
=
232 v3d
->prog
.fs
->prog_data
.fs
->base
.single_seg
;
234 shader
.coordinate_shader_4_way_threadable
=
235 v3d
->prog
.cs
->prog_data
.vs
->base
.threads
== 4;
236 shader
.coordinate_shader_2_way_threadable
=
237 v3d
->prog
.cs
->prog_data
.vs
->base
.threads
== 2;
238 shader
.vertex_shader_4_way_threadable
=
239 v3d
->prog
.vs
->prog_data
.vs
->base
.threads
== 4;
240 shader
.vertex_shader_2_way_threadable
=
241 v3d
->prog
.vs
->prog_data
.vs
->base
.threads
== 2;
242 shader
.fragment_shader_4_way_threadable
=
243 v3d
->prog
.fs
->prog_data
.fs
->base
.threads
== 4;
244 shader
.fragment_shader_2_way_threadable
=
245 v3d
->prog
.fs
->prog_data
.fs
->base
.threads
== 2;
248 shader
.vertex_id_read_by_coordinate_shader
=
249 v3d
->prog
.cs
->prog_data
.vs
->uses_vid
;
250 shader
.instance_id_read_by_coordinate_shader
=
251 v3d
->prog
.cs
->prog_data
.vs
->uses_iid
;
252 shader
.vertex_id_read_by_vertex_shader
=
253 v3d
->prog
.vs
->prog_data
.vs
->uses_vid
;
254 shader
.instance_id_read_by_vertex_shader
=
255 v3d
->prog
.vs
->prog_data
.vs
->uses_iid
;
257 shader
.address_of_default_attribute_values
=
258 cl_address(vtx
->default_attribute_values
, 0);
261 for (int i
= 0; i
< vtx
->num_elements
; i
++) {
262 struct pipe_vertex_element
*elem
= &vtx
->pipe
[i
];
263 struct pipe_vertex_buffer
*vb
=
264 &vertexbuf
->vb
[elem
->vertex_buffer_index
];
265 struct v3d_resource
*rsc
= v3d_resource(vb
->buffer
.resource
);
267 const uint32_t size
=
268 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD
);
269 cl_emit_with_prepacked(&job
->indirect
,
270 GL_SHADER_STATE_ATTRIBUTE_RECORD
,
271 &vtx
->attrs
[i
* size
], attr
) {
272 attr
.stride
= vb
->stride
;
273 attr
.address
= cl_address(rsc
->bo
,
276 attr
.number_of_values_read_by_coordinate_shader
=
277 v3d
->prog
.cs
->prog_data
.vs
->vattr_sizes
[i
];
278 attr
.number_of_values_read_by_vertex_shader
=
279 v3d
->prog
.vs
->prog_data
.vs
->vattr_sizes
[i
];
280 #if V3D_VERSION >= 41
281 attr
.maximum_index
= 0xffffff;
284 STATIC_ASSERT(sizeof(vtx
->attrs
) >= VC5_MAX_ATTRIBUTES
* size
);
287 if (vtx
->num_elements
== 0) {
288 /* GFXH-930: At least one attribute must be enabled and read
289 * by CS and VS. If we have no attributes being consumed by
290 * the shader, set up a dummy to be loaded into the VPM.
292 cl_emit(&job
->indirect
, GL_SHADER_STATE_ATTRIBUTE_RECORD
, attr
) {
293 /* Valid address of data whose value will be unused. */
294 attr
.address
= cl_address(job
->indirect
.bo
, 0);
296 attr
.type
= ATTRIBUTE_FLOAT
;
300 attr
.number_of_values_read_by_coordinate_shader
= 1;
301 attr
.number_of_values_read_by_vertex_shader
= 1;
305 cl_emit(&job
->bcl
, GL_SHADER_STATE
, state
) {
306 state
.address
= cl_address(job
->indirect
.bo
, shader_rec_offset
);
307 state
.number_of_attribute_arrays
= num_elements_to_emit
;
310 v3d_bo_unreference(&cs_uniforms
.bo
);
311 v3d_bo_unreference(&vs_uniforms
.bo
);
312 v3d_bo_unreference(&fs_uniforms
.bo
);
314 job
->shader_rec_count
++;
318 * Computes the various transform feedback statistics, since they can't be
319 * recorded by CL packets.
322 v3d_tf_statistics_record(struct v3d_context
*v3d
,
323 const struct pipe_draw_info
*info
,
326 if (!v3d
->active_queries
)
329 uint32_t prims
= u_prims_for_vertices(info
->mode
, info
->count
);
330 v3d
->prims_generated
+= prims
;
333 /* XXX: Only count if we didn't overflow. */
334 v3d
->tf_prims_generated
+= prims
;
339 v3d_update_job_ez(struct v3d_context
*v3d
, struct v3d_job
*job
)
341 switch (v3d
->zsa
->ez_state
) {
342 case VC5_EZ_UNDECIDED
:
343 /* If the Z/S state didn't pick a direction but didn't
344 * disable, then go along with the current EZ state. This
345 * allows EZ optimization for Z func == EQUAL or NEVER.
351 /* If the Z/S state picked a direction, then it needs to match
352 * the current direction if we've decided on one.
354 if (job
->ez_state
== VC5_EZ_UNDECIDED
)
355 job
->ez_state
= v3d
->zsa
->ez_state
;
356 else if (job
->ez_state
!= v3d
->zsa
->ez_state
)
357 job
->ez_state
= VC5_EZ_DISABLED
;
360 case VC5_EZ_DISABLED
:
361 /* If the current Z/S state disables EZ because of a bad Z
362 * func or stencil operation, then we can't do any more EZ in
365 job
->ez_state
= VC5_EZ_DISABLED
;
369 /* If the FS affects the Z of the pixels, then it may update against
370 * the chosen EZ direction (though we could use
371 * ARB_conservative_depth's hints to avoid this)
373 if (v3d
->prog
.fs
->prog_data
.fs
->writes_z
) {
374 job
->ez_state
= VC5_EZ_DISABLED
;
377 if (job
->first_ez_state
== VC5_EZ_UNDECIDED
&&
378 (job
->ez_state
!= VC5_EZ_DISABLED
|| job
->draw_calls_queued
== 0))
379 job
->first_ez_state
= job
->ez_state
;
383 v3d_draw_vbo(struct pipe_context
*pctx
, const struct pipe_draw_info
*info
)
385 struct v3d_context
*v3d
= v3d_context(pctx
);
387 if (!info
->count_from_stream_output
&& !info
->indirect
&&
388 !info
->primitive_restart
&&
389 !u_trim_pipe_prim(info
->mode
, (unsigned*)&info
->count
))
392 /* Fall back for weird desktop GL primitive restart values. */
393 if (info
->primitive_restart
&&
397 switch (info
->index_size
) {
406 if (info
->restart_index
!= mask
) {
407 util_draw_vbo_without_prim_restart(pctx
, info
);
412 if (info
->mode
>= PIPE_PRIM_QUADS
) {
413 util_primconvert_save_rasterizer_state(v3d
->primconvert
, &v3d
->rasterizer
->base
);
414 util_primconvert_draw_vbo(v3d
->primconvert
, info
);
415 perf_debug("Fallback conversion for %d %s vertices\n",
416 info
->count
, u_prim_name(info
->mode
));
420 /* Before setting up the draw, flush anything writing to the textures
423 v3d_predraw_check_textures(pctx
, &v3d
->verttex
);
424 v3d_predraw_check_textures(pctx
, &v3d
->fragtex
);
426 struct v3d_job
*job
= v3d_get_job_for_fbo(v3d
);
428 /* Get space to emit our draw call into the BCL, using a branch to
429 * jump to a new BO if necessary.
431 v3d_cl_ensure_space_with_branch(&job
->bcl
, 256 /* XXX */);
433 if (v3d
->prim_mode
!= info
->mode
) {
434 v3d
->prim_mode
= info
->mode
;
435 v3d
->dirty
|= VC5_DIRTY_PRIM_MODE
;
439 v3d_update_compiled_shaders(v3d
, info
->mode
);
440 v3d_update_job_ez(v3d
, job
);
442 #if V3D_VERSION >= 41
443 v3d41_emit_state(pctx
);
445 v3d33_emit_state(pctx
);
448 if (v3d
->dirty
& (VC5_DIRTY_VTXBUF
|
450 VC5_DIRTY_PRIM_MODE
|
451 VC5_DIRTY_RASTERIZER
|
452 VC5_DIRTY_COMPILED_CS
|
453 VC5_DIRTY_COMPILED_VS
|
454 VC5_DIRTY_COMPILED_FS
|
455 v3d
->prog
.cs
->uniform_dirty_bits
|
456 v3d
->prog
.vs
->uniform_dirty_bits
|
457 v3d
->prog
.fs
->uniform_dirty_bits
)) {
458 v3d_emit_gl_shader_state(v3d
, info
);
463 /* The Base Vertex/Base Instance packet sets those values to nonzero
464 * for the next draw call only.
466 if (info
->index_bias
|| info
->start_instance
) {
467 cl_emit(&job
->bcl
, BASE_VERTEX_BASE_INSTANCE
, base
) {
468 base
.base_instance
= info
->start_instance
;
469 base
.base_vertex
= info
->index_bias
;
473 uint32_t prim_tf_enable
= 0;
475 /* V3D 3.x: The HW only processes transform feedback on primitives
478 if (v3d
->streamout
.num_targets
)
479 prim_tf_enable
= (V3D_PRIM_POINTS_TF
- V3D_PRIM_POINTS
);
482 v3d_tf_statistics_record(v3d
, info
, v3d
->streamout
.num_targets
);
484 /* Note that the primitive type fields match with OpenGL/gallium
485 * definitions, up to but not including QUADS.
487 if (info
->index_size
) {
488 uint32_t index_size
= info
->index_size
;
489 uint32_t offset
= info
->start
* index_size
;
490 struct pipe_resource
*prsc
;
491 if (info
->has_user_indices
) {
493 u_upload_data(v3d
->uploader
, 0,
494 info
->count
* info
->index_size
, 4,
498 prsc
= info
->index
.resource
;
500 struct v3d_resource
*rsc
= v3d_resource(prsc
);
502 #if V3D_VERSION >= 40
503 cl_emit(&job
->bcl
, INDEX_BUFFER_SETUP
, ib
) {
504 ib
.address
= cl_address(rsc
->bo
, 0);
505 ib
.size
= rsc
->bo
->size
;
509 if (info
->instance_count
> 1) {
510 cl_emit(&job
->bcl
, INDEXED_INSTANCED_PRIMITIVE_LIST
, prim
) {
511 prim
.index_type
= ffs(info
->index_size
) - 1;
512 #if V3D_VERSION >= 40
513 prim
.index_offset
= offset
;
514 #else /* V3D_VERSION < 40 */
515 prim
.maximum_index
= (1u << 31) - 1; /* XXX */
516 prim
.address_of_indices_list
=
517 cl_address(rsc
->bo
, offset
);
518 #endif /* V3D_VERSION < 40 */
519 prim
.mode
= info
->mode
| prim_tf_enable
;
520 prim
.enable_primitive_restarts
= info
->primitive_restart
;
522 prim
.number_of_instances
= info
->instance_count
;
523 prim
.instance_length
= info
->count
;
526 cl_emit(&job
->bcl
, INDEXED_PRIMITIVE_LIST
, prim
) {
527 prim
.index_type
= ffs(info
->index_size
) - 1;
528 prim
.length
= info
->count
;
529 #if V3D_VERSION >= 40
530 prim
.index_offset
= offset
;
531 #else /* V3D_VERSION < 40 */
532 prim
.maximum_index
= (1u << 31) - 1; /* XXX */
533 prim
.address_of_indices_list
=
534 cl_address(rsc
->bo
, offset
);
535 #endif /* V3D_VERSION < 40 */
536 prim
.mode
= info
->mode
| prim_tf_enable
;
537 prim
.enable_primitive_restarts
= info
->primitive_restart
;
541 job
->draw_calls_queued
++;
543 if (info
->has_user_indices
)
544 pipe_resource_reference(&prsc
, NULL
);
546 if (info
->instance_count
> 1) {
547 cl_emit(&job
->bcl
, VERTEX_ARRAY_INSTANCED_PRIMITIVES
, prim
) {
548 prim
.mode
= info
->mode
| prim_tf_enable
;
549 prim
.index_of_first_vertex
= info
->start
;
550 prim
.number_of_instances
= info
->instance_count
;
551 prim
.instance_length
= info
->count
;
554 cl_emit(&job
->bcl
, VERTEX_ARRAY_PRIMITIVES
, prim
) {
555 prim
.mode
= info
->mode
| prim_tf_enable
;
556 prim
.length
= info
->count
;
557 prim
.index_of_first_vertex
= info
->start
;
562 /* A flush is required in between a TF draw and any following TF specs
563 * packet, or the GPU may hang. Just flush each time for now.
565 if (v3d
->streamout
.num_targets
)
566 cl_emit(&job
->bcl
, TRANSFORM_FEEDBACK_FLUSH_AND_COUNT
, flush
);
568 job
->draw_calls_queued
++;
570 /* Increment the TF offsets by how many verts we wrote. XXX: This
571 * needs some clamping to the buffer size.
573 for (int i
= 0; i
< v3d
->streamout
.num_targets
; i
++)
574 v3d
->streamout
.offsets
[i
] += info
->count
;
576 if (v3d
->zsa
&& job
->zsbuf
&& v3d
->zsa
->base
.depth
.enabled
) {
577 struct v3d_resource
*rsc
= v3d_resource(job
->zsbuf
->texture
);
578 v3d_job_add_bo(job
, rsc
->bo
);
580 job
->store
|= PIPE_CLEAR_DEPTH
;
581 rsc
->initialized_buffers
= PIPE_CLEAR_DEPTH
;
584 if (v3d
->zsa
&& job
->zsbuf
&& v3d
->zsa
->base
.stencil
[0].enabled
) {
585 struct v3d_resource
*rsc
= v3d_resource(job
->zsbuf
->texture
);
586 if (rsc
->separate_stencil
)
587 rsc
= rsc
->separate_stencil
;
589 v3d_job_add_bo(job
, rsc
->bo
);
591 job
->store
|= PIPE_CLEAR_STENCIL
;
592 rsc
->initialized_buffers
|= PIPE_CLEAR_STENCIL
;
595 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
596 uint32_t bit
= PIPE_CLEAR_COLOR0
<< i
;
598 if (job
->store
& bit
|| !job
->cbufs
[i
])
600 struct v3d_resource
*rsc
= v3d_resource(job
->cbufs
[i
]->texture
);
603 v3d_job_add_bo(job
, rsc
->bo
);
606 if (job
->referenced_size
> 768 * 1024 * 1024) {
607 perf_debug("Flushing job with %dkb to try to free up memory\n",
608 job
->referenced_size
/ 1024);
612 if (V3D_DEBUG
& V3D_DEBUG_ALWAYS_FLUSH
)
616 /* GFXH-1461: If we were to emit a load of just depth or just stencil, then
617 * the clear for the other may get lost. Just fall back to drawing a quad in
621 v3d_gfxh1461_clear_workaround(struct v3d_context
*v3d
,
622 unsigned buffers
, double depth
, unsigned stencil
)
624 unsigned zsclear
= buffers
& PIPE_CLEAR_DEPTHSTENCIL
;
625 if (!zsclear
|| zsclear
== PIPE_CLEAR_DEPTHSTENCIL
)
628 static const union pipe_color_union dummy_color
= {};
630 v3d_blitter_save(v3d
);
631 util_blitter_clear(v3d
->blitter
,
632 v3d
->framebuffer
.width
,
633 v3d
->framebuffer
.height
,
636 &dummy_color
, depth
, stencil
);
641 v3d_clear(struct pipe_context
*pctx
, unsigned buffers
,
642 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
644 struct v3d_context
*v3d
= v3d_context(pctx
);
645 struct v3d_job
*job
= v3d_get_job_for_fbo(v3d
);
647 if (v3d_gfxh1461_clear_workaround(v3d
, buffers
, depth
, stencil
))
648 buffers
&= ~PIPE_CLEAR_DEPTHSTENCIL
;
653 /* We can't flag new buffers for clearing once we've queued draws. We
654 * could avoid this by using the 3d engine to clear.
656 if (job
->draw_calls_queued
) {
657 perf_debug("Flushing rendering to process new clear.\n");
658 v3d_job_submit(v3d
, job
);
659 job
= v3d_get_job_for_fbo(v3d
);
662 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
663 uint32_t bit
= PIPE_CLEAR_COLOR0
<< i
;
664 if (!(buffers
& bit
))
667 struct pipe_surface
*psurf
= v3d
->framebuffer
.cbufs
[i
];
668 struct v3d_surface
*surf
= v3d_surface(psurf
);
669 struct v3d_resource
*rsc
= v3d_resource(psurf
->texture
);
672 uint32_t internal_size
= 4 << surf
->internal_bpp
;
674 static union pipe_color_union swapped_color
;
675 if (v3d
->swap_color_rb
& (1 << i
)) {
676 swapped_color
.f
[0] = color
->f
[2];
677 swapped_color
.f
[1] = color
->f
[1];
678 swapped_color
.f
[2] = color
->f
[0];
679 swapped_color
.f
[3] = color
->f
[3];
680 color
= &swapped_color
;
683 switch (surf
->internal_type
) {
684 case V3D_INTERNAL_TYPE_8
:
685 util_pack_color(color
->f
, PIPE_FORMAT_R8G8B8A8_UNORM
,
687 memcpy(job
->clear_color
[i
], uc
.ui
, internal_size
);
689 case V3D_INTERNAL_TYPE_8I
:
690 case V3D_INTERNAL_TYPE_8UI
:
691 job
->clear_color
[i
][0] = ((color
->ui
[0] & 0xff) |
692 (color
->ui
[1] & 0xff) << 8 |
693 (color
->ui
[2] & 0xff) << 16 |
694 (color
->ui
[3] & 0xff) << 24);
696 case V3D_INTERNAL_TYPE_16F
:
697 util_pack_color(color
->f
, PIPE_FORMAT_R16G16B16A16_FLOAT
,
699 memcpy(job
->clear_color
[i
], uc
.ui
, internal_size
);
701 case V3D_INTERNAL_TYPE_16I
:
702 case V3D_INTERNAL_TYPE_16UI
:
703 job
->clear_color
[i
][0] = ((color
->ui
[0] & 0xffff) |
705 job
->clear_color
[i
][1] = ((color
->ui
[2] & 0xffff) |
708 case V3D_INTERNAL_TYPE_32F
:
709 case V3D_INTERNAL_TYPE_32I
:
710 case V3D_INTERNAL_TYPE_32UI
:
711 memcpy(job
->clear_color
[i
], color
->ui
, internal_size
);
715 rsc
->initialized_buffers
|= bit
;
718 unsigned zsclear
= buffers
& PIPE_CLEAR_DEPTHSTENCIL
;
720 struct v3d_resource
*rsc
=
721 v3d_resource(v3d
->framebuffer
.zsbuf
->texture
);
723 if (zsclear
& PIPE_CLEAR_DEPTH
)
724 job
->clear_z
= depth
;
725 if (zsclear
& PIPE_CLEAR_STENCIL
)
726 job
->clear_s
= stencil
;
728 rsc
->initialized_buffers
|= zsclear
;
733 job
->draw_max_x
= v3d
->framebuffer
.width
;
734 job
->draw_max_y
= v3d
->framebuffer
.height
;
735 job
->clear
|= buffers
;
736 job
->store
|= buffers
;
742 v3d_clear_render_target(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
743 const union pipe_color_union
*color
,
744 unsigned x
, unsigned y
, unsigned w
, unsigned h
,
745 bool render_condition_enabled
)
747 fprintf(stderr
, "unimpl: clear RT\n");
751 v3d_clear_depth_stencil(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
752 unsigned buffers
, double depth
, unsigned stencil
,
753 unsigned x
, unsigned y
, unsigned w
, unsigned h
,
754 bool render_condition_enabled
)
756 fprintf(stderr
, "unimpl: clear DS\n");
760 v3dX(draw_init
)(struct pipe_context
*pctx
)
762 pctx
->draw_vbo
= v3d_draw_vbo
;
763 pctx
->clear
= v3d_clear
;
764 pctx
->clear_render_target
= v3d_clear_render_target
;
765 pctx
->clear_depth_stencil
= v3d_clear_depth_stencil
;