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/format/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 /* The PTB will request the tile alloc initial size per tile at start
61 uint32_t tile_alloc_size
= (job
->draw_tiles_x
*
62 job
->draw_tiles_y
) * 64;
63 /* The PTB allocates in aligned 4k chunks after the initial setup. */
64 tile_alloc_size
= align(tile_alloc_size
, 4096);
66 /* Include the first two chunk allocations that the PTB does so that
67 * we definitely clear the OOM condition before triggering one (the HW
68 * won't trigger OOM during the first allocations).
70 tile_alloc_size
+= 8192;
72 /* For performance, allocate some extra initial memory after the PTB's
73 * minimal allocations, so that we hopefully don't have to block the
74 * GPU on the kernel handling an OOM signal.
76 tile_alloc_size
+= 512 * 1024;
78 job
->tile_alloc
= v3d_bo_alloc(v3d
->screen
, tile_alloc_size
,
80 uint32_t tsda_per_tile_size
= v3d
->screen
->devinfo
.ver
>= 40 ? 256 : 64;
81 job
->tile_state
= v3d_bo_alloc(v3d
->screen
,
88 cl_emit(&job
->bcl
, TILE_BINNING_MODE_CFG
, config
) {
89 config
.width_in_pixels
= v3d
->framebuffer
.width
;
90 config
.height_in_pixels
= v3d
->framebuffer
.height
;
91 config
.number_of_render_targets
=
92 MAX2(v3d
->framebuffer
.nr_cbufs
, 1);
94 config
.multisample_mode_4x
= job
->msaa
;
96 config
.maximum_bpp_of_all_render_targets
= job
->internal_bpp
;
98 #else /* V3D_VERSION < 40 */
99 /* "Binning mode lists start with a Tile Binning Mode Configuration
102 * Part1 signals the end of binning config setup.
104 cl_emit(&job
->bcl
, TILE_BINNING_MODE_CFG_PART2
, config
) {
105 config
.tile_allocation_memory_address
=
106 cl_address(job
->tile_alloc
, 0);
107 config
.tile_allocation_memory_size
= job
->tile_alloc
->size
;
110 cl_emit(&job
->bcl
, TILE_BINNING_MODE_CFG_PART1
, config
) {
111 config
.tile_state_data_array_base_address
=
112 cl_address(job
->tile_state
, 0);
114 config
.width_in_tiles
= job
->draw_tiles_x
;
115 config
.height_in_tiles
= job
->draw_tiles_y
;
117 config
.number_of_render_targets
=
118 MAX2(v3d
->framebuffer
.nr_cbufs
, 1);
120 config
.multisample_mode_4x
= job
->msaa
;
122 config
.maximum_bpp_of_all_render_targets
= job
->internal_bpp
;
124 #endif /* V3D_VERSION < 40 */
126 /* There's definitely nothing in the VCD cache we want. */
127 cl_emit(&job
->bcl
, FLUSH_VCD_CACHE
, bin
);
129 /* Disable any leftover OQ state from another job. */
130 cl_emit(&job
->bcl
, OCCLUSION_QUERY_COUNTER
, counter
);
132 /* "Binning mode lists must have a Start Tile Binning item (6) after
133 * any prefix state data before the binning list proper starts."
135 cl_emit(&job
->bcl
, START_TILE_BINNING
, bin
);
137 job
->needs_flush
= true;
138 job
->draw_width
= v3d
->framebuffer
.width
;
139 job
->draw_height
= v3d
->framebuffer
.height
;
143 v3d_predraw_check_stage_inputs(struct pipe_context
*pctx
,
144 enum pipe_shader_type s
)
146 struct v3d_context
*v3d
= v3d_context(pctx
);
148 /* Flush writes to textures we're sampling. */
149 for (int i
= 0; i
< v3d
->tex
[s
].num_textures
; i
++) {
150 struct pipe_sampler_view
*pview
= v3d
->tex
[s
].textures
[i
];
153 struct v3d_sampler_view
*view
= v3d_sampler_view(pview
);
155 if (view
->texture
!= view
->base
.texture
&&
156 view
->base
.format
!= PIPE_FORMAT_X32_S8X24_UINT
)
157 v3d_update_shadow_texture(pctx
, &view
->base
);
159 v3d_flush_jobs_writing_resource(v3d
, view
->texture
,
163 /* Flush writes to UBOs. */
164 foreach_bit(i
, v3d
->constbuf
[s
].enabled_mask
) {
165 struct pipe_constant_buffer
*cb
= &v3d
->constbuf
[s
].cb
[i
];
167 v3d_flush_jobs_writing_resource(v3d
, cb
->buffer
,
172 /* Flush reads/writes to our SSBOs */
173 foreach_bit(i
, v3d
->ssbo
[s
].enabled_mask
) {
174 struct pipe_shader_buffer
*sb
= &v3d
->ssbo
[s
].sb
[i
];
176 v3d_flush_jobs_reading_resource(v3d
, sb
->buffer
,
177 V3D_FLUSH_NOT_CURRENT_JOB
);
181 /* Flush reads/writes to our image views */
182 foreach_bit(i
, v3d
->shaderimg
[s
].enabled_mask
) {
183 struct v3d_image_view
*view
= &v3d
->shaderimg
[s
].si
[i
];
185 v3d_flush_jobs_reading_resource(v3d
, view
->base
.resource
,
186 V3D_FLUSH_NOT_CURRENT_JOB
);
189 /* Flush writes to our vertex buffers (i.e. from transform feedback) */
190 if (s
== PIPE_SHADER_VERTEX
) {
191 foreach_bit(i
, v3d
->vertexbuf
.enabled_mask
) {
192 struct pipe_vertex_buffer
*vb
= &v3d
->vertexbuf
.vb
[i
];
194 v3d_flush_jobs_writing_resource(v3d
, vb
->buffer
.resource
,
201 v3d_predraw_check_outputs(struct pipe_context
*pctx
)
203 struct v3d_context
*v3d
= v3d_context(pctx
);
205 /* Flush jobs reading from TF buffers that we are about to write. */
206 if (v3d_transform_feedback_enabled(v3d
)) {
207 struct v3d_streamout_stateobj
*so
= &v3d
->streamout
;
209 for (int i
= 0; i
< so
->num_targets
; i
++) {
213 const struct pipe_stream_output_target
*target
=
215 v3d_flush_jobs_reading_resource(v3d
, target
->buffer
,
222 * Checks if the state for the current draw reads a particular resource in
223 * in the given shader stage.
226 v3d_state_reads_resource(struct v3d_context
*v3d
,
227 struct pipe_resource
*prsc
,
228 enum pipe_shader_type s
)
230 struct v3d_resource
*rsc
= v3d_resource(prsc
);
233 if (s
== PIPE_SHADER_VERTEX
) {
234 foreach_bit(i
, v3d
->vertexbuf
.enabled_mask
) {
235 struct pipe_vertex_buffer
*vb
= &v3d
->vertexbuf
.vb
[i
];
236 if (!vb
->buffer
.resource
)
239 struct v3d_resource
*vb_rsc
=
240 v3d_resource(vb
->buffer
.resource
);
241 if (rsc
->bo
== vb_rsc
->bo
)
246 /* Constant buffers */
247 foreach_bit(i
, v3d
->constbuf
[s
].enabled_mask
) {
248 struct pipe_constant_buffer
*cb
= &v3d
->constbuf
[s
].cb
[i
];
252 struct v3d_resource
*cb_rsc
= v3d_resource(cb
->buffer
);
253 if (rsc
->bo
== cb_rsc
->bo
)
257 /* Shader storage buffers */
258 foreach_bit(i
, v3d
->ssbo
[s
].enabled_mask
) {
259 struct pipe_shader_buffer
*sb
= &v3d
->ssbo
[s
].sb
[i
];
263 struct v3d_resource
*sb_rsc
= v3d_resource(sb
->buffer
);
264 if (rsc
->bo
== sb_rsc
->bo
)
269 for (int i
= 0; i
< v3d
->tex
[s
].num_textures
; i
++) {
270 struct pipe_sampler_view
*pview
= v3d
->tex
[s
].textures
[i
];
274 struct v3d_sampler_view
*view
= v3d_sampler_view(pview
);
275 struct v3d_resource
*v_rsc
= v3d_resource(view
->texture
);
276 if (rsc
->bo
== v_rsc
->bo
)
284 v3d_emit_wait_for_tf(struct v3d_job
*job
)
286 /* XXX: we might be able to skip this in some cases, for now we
289 cl_emit(&job
->bcl
, FLUSH_TRANSFORM_FEEDBACK_DATA
, flush
);
291 cl_emit(&job
->bcl
, WAIT_FOR_TRANSFORM_FEEDBACK
, wait
) {
292 /* XXX: Wait for all outstanding writes... maybe we can do
293 * better in some cases.
295 wait
.block_count
= 255;
298 /* We have just flushed all our outstanding TF work in this job so make
299 * sure we don't emit TF flushes again for any of it again.
301 _mesa_set_clear(job
->tf_write_prscs
, NULL
);
305 v3d_emit_wait_for_tf_if_needed(struct v3d_context
*v3d
, struct v3d_job
*job
)
307 if (!job
->tf_enabled
)
310 set_foreach(job
->tf_write_prscs
, entry
) {
311 struct pipe_resource
*prsc
= (struct pipe_resource
*)entry
->key
;
312 for (int s
= 0; s
< PIPE_SHADER_COMPUTE
; s
++) {
313 /* Fragment shaders can only start executing after all
314 * binning (and thus TF) is complete.
316 * XXX: For VS/GS/TES, if the binning shader does not
317 * read the resource then we could also avoid emitting
320 if (s
== PIPE_SHADER_FRAGMENT
)
323 if (v3d_state_reads_resource(v3d
, prsc
, s
)) {
324 v3d_emit_wait_for_tf(job
);
341 #if V3D_VERSION >= 41
343 v3d_emit_gs_state_record(struct v3d_job
*job
,
344 struct v3d_compiled_shader
*gs_bin
,
345 struct v3d_cl_reloc gs_bin_uniforms
,
346 struct v3d_compiled_shader
*gs
,
347 struct v3d_cl_reloc gs_render_uniforms
)
349 cl_emit(&job
->indirect
, GEOMETRY_SHADER_STATE_RECORD
, shader
) {
350 shader
.geometry_bin_mode_shader_code_address
=
351 cl_address(v3d_resource(gs_bin
->resource
)->bo
,
353 shader
.geometry_bin_mode_shader_4_way_threadable
=
354 gs_bin
->prog_data
.gs
->base
.threads
== 4;
355 shader
.geometry_bin_mode_shader_start_in_final_thread_section
=
356 gs_bin
->prog_data
.gs
->base
.single_seg
;
357 shader
.geometry_bin_mode_shader_propagate_nans
= true;
358 shader
.geometry_bin_mode_shader_uniforms_address
=
361 shader
.geometry_render_mode_shader_code_address
=
362 cl_address(v3d_resource(gs
->resource
)->bo
, gs
->offset
);
363 shader
.geometry_render_mode_shader_4_way_threadable
=
364 gs
->prog_data
.gs
->base
.threads
== 4;
365 shader
.geometry_render_mode_shader_start_in_final_thread_section
=
366 gs
->prog_data
.gs
->base
.single_seg
;
367 shader
.geometry_render_mode_shader_propagate_nans
= true;
368 shader
.geometry_render_mode_shader_uniforms_address
=
374 v3d_gs_output_primitive(uint32_t prim_type
)
378 return GEOMETRY_SHADER_POINTS
;
380 return GEOMETRY_SHADER_LINE_STRIP
;
381 case GL_TRIANGLE_STRIP
:
382 return GEOMETRY_SHADER_TRI_STRIP
;
384 unreachable("Unsupported primitive type");
389 v3d_emit_tes_gs_common_params(struct v3d_job
*job
,
390 uint8_t gs_out_prim_type
,
391 uint8_t gs_num_invocations
)
393 /* This, and v3d_emit_tes_gs_shader_params below, fill in default
394 * values for tessellation fields even though we don't support
395 * tessellation yet because our packing functions (and the simulator)
396 * complain if we don't.
398 cl_emit(&job
->indirect
, TESSELLATION_GEOMETRY_COMMON_PARAMS
, shader
) {
399 shader
.tessellation_type
= TESSELLATION_TYPE_TRIANGLE
;
400 shader
.tessellation_point_mode
= false;
401 shader
.tessellation_edge_spacing
= TESSELLATION_EDGE_SPACING_EVEN
;
402 shader
.tessellation_clockwise
= true;
403 shader
.tessellation_invocations
= 1;
405 shader
.geometry_shader_output_format
=
406 v3d_gs_output_primitive(gs_out_prim_type
);
407 shader
.geometry_shader_instances
= gs_num_invocations
& 0x1F;
412 simd_width_to_gs_pack_mode(uint32_t width
)
416 return V3D_PACK_MODE_16_WAY
;
418 return V3D_PACK_MODE_8_WAY
;
420 return V3D_PACK_MODE_4_WAY
;
422 return V3D_PACK_MODE_1_WAY
;
424 unreachable("Invalid SIMD width");
429 v3d_emit_tes_gs_shader_params(struct v3d_job
*job
,
431 uint32_t gs_vpm_output_size
,
432 uint32_t gs_max_vpm_input_size_per_batch
)
434 cl_emit(&job
->indirect
, TESSELLATION_GEOMETRY_SHADER_PARAMS
, shader
) {
435 shader
.tcs_batch_flush_mode
= V3D_TCS_FLUSH_MODE_FULLY_PACKED
;
436 shader
.per_patch_data_column_depth
= 1;
437 shader
.tcs_output_segment_size_in_sectors
= 1;
438 shader
.tcs_output_segment_pack_mode
= V3D_PACK_MODE_16_WAY
;
439 shader
.tes_output_segment_size_in_sectors
= 1;
440 shader
.tes_output_segment_pack_mode
= V3D_PACK_MODE_16_WAY
;
441 shader
.gs_output_segment_size_in_sectors
= gs_vpm_output_size
;
442 shader
.gs_output_segment_pack_mode
=
443 simd_width_to_gs_pack_mode(gs_simd
);
444 shader
.tbg_max_patches_per_tcs_batch
= 1;
445 shader
.tbg_max_extra_vertex_segs_for_patches_after_first
= 0;
446 shader
.tbg_min_tcs_output_segments_required_in_play
= 1;
447 shader
.tbg_min_per_patch_data_segments_required_in_play
= 1;
448 shader
.tpg_max_patches_per_tes_batch
= 1;
449 shader
.tpg_max_vertex_segments_per_tes_batch
= 0;
450 shader
.tpg_max_tcs_output_segments_per_tes_batch
= 1;
451 shader
.tpg_min_tes_output_segments_required_in_play
= 1;
452 shader
.gbg_max_tes_output_vertex_segments_per_gs_batch
=
453 gs_max_vpm_input_size_per_batch
;
454 shader
.gbg_min_gs_output_segments_required_in_play
= 1;
458 static inline uint32_t
459 compute_vpm_size_in_sectors(const struct v3d_device_info
*devinfo
)
461 assert(devinfo
->vpm_size
> 0);
462 const uint32_t sector_size
= V3D_CHANNELS
* sizeof(uint32_t) * 8;
463 return devinfo
->vpm_size
/ sector_size
;
466 /* Computes various parameters affecting VPM memory configuration for programs
467 * involving geometry shaders to ensure the program fits in memory and honors
468 * requirements described in section "VPM usage" of the programming manual.
471 compute_vpm_config_gs(struct v3d_device_info
*devinfo
,
472 struct v3d_vs_prog_data
*vs
,
473 struct v3d_gs_prog_data
*gs
,
474 struct vpm_config
*vpm_cfg_out
)
476 const uint32_t A
= vs
->separate_segments
? 1 : 0;
477 const uint32_t Ad
= vs
->vpm_input_size
;
478 const uint32_t Vd
= vs
->vpm_output_size
;
480 const uint32_t vpm_size
= compute_vpm_size_in_sectors(devinfo
);
482 /* Try to fit program into our VPM memory budget by adjusting
483 * configurable parameters iteratively. We do this in two phases:
484 * the first phase tries to fit the program into the total available
485 * VPM memory. If we suceed at that, then the second phase attempts
486 * to fit the program into half of that budget so we can run bin and
487 * render programs in parallel.
489 struct vpm_config vpm_cfg
[2];
490 struct vpm_config
*final_vpm_cfg
= NULL
;
493 vpm_cfg
[phase
].As
= 1;
494 vpm_cfg
[phase
].Gs
= 1;
495 vpm_cfg
[phase
].Gd
= gs
->vpm_output_size
;
496 vpm_cfg
[phase
].gs_width
= gs
->simd_width
;
498 /* While there is a requirement that Vc >= [Vn / 16], this is
499 * always the case when tessellation is not present because in that
500 * case Vn can only be 6 at most (when input primitive is triangles
503 * We always choose Vc=2. We can't go lower than this due to GFXH-1744,
504 * and Broadcom has not found it worth it to increase it beyond this
505 * in general. Increasing Vc also increases VPM memory pressure which
506 * can turn up being detrimental for performance in some scenarios.
508 vpm_cfg
[phase
].Vc
= 2;
510 /* Gv is a constraint on the hardware to not exceed the
511 * specified number of vertex segments per GS batch. If adding a
512 * new primitive to a GS batch would result in a range of more
513 * than Gv vertex segments being referenced by the batch, then
514 * the hardware will flush the batch and start a new one. This
515 * means that we can choose any value we want, we just need to
516 * be aware that larger values improve GS batch utilization
517 * at the expense of more VPM memory pressure (which can affect
518 * other performance aspects, such as GS dispatch width).
519 * We start with the largest value, and will reduce it if we
520 * find that total memory pressure is too high.
522 vpm_cfg
[phase
].Gv
= 3;
524 /* When GS is present in absence of TES, then we need to satisfy
525 * that Ve >= Gv. We go with the smallest value of Ve to avoid
526 * increasing memory pressure.
528 vpm_cfg
[phase
].Ve
= vpm_cfg
[phase
].Gv
;
530 uint32_t vpm_sectors
=
531 A
* vpm_cfg
[phase
].As
* Ad
+
532 (vpm_cfg
[phase
].Vc
+ vpm_cfg
[phase
].Ve
) * Vd
+
533 vpm_cfg
[phase
].Gs
* vpm_cfg
[phase
].Gd
;
535 /* Ideally we want to use no more than half of the available
536 * memory so we can execute a bin and render program in parallel
537 * without stalls. If we achieved that then we are done.
539 if (vpm_sectors
<= vpm_size
/ 2) {
540 final_vpm_cfg
= &vpm_cfg
[phase
];
544 /* At the very least, we should not allocate more than the
545 * total available VPM memory. If we have a configuration that
546 * succeeds at this we save it and continue to see if we can
547 * meet the half-memory-use criteria too.
549 if (phase
== 0 && vpm_sectors
<= vpm_size
) {
550 vpm_cfg
[1] = vpm_cfg
[0];
554 /* Try lowering Gv */
555 if (vpm_cfg
[phase
].Gv
> 0) {
560 /* Try lowering GS dispatch width */
561 if (vpm_cfg
[phase
].gs_width
> 1) {
563 vpm_cfg
[phase
].gs_width
>>= 1;
565 align(vpm_cfg
[phase
].Gd
, 2) / 2;
566 } while (vpm_cfg
[phase
].gs_width
== 2);
568 /* Reset Gv to max after dropping dispatch width */
569 vpm_cfg
[phase
].Gv
= 3;
573 /* We ran out of options to reduce memory pressure. If we
574 * are at phase 1 we have at least a valid configuration, so we
578 final_vpm_cfg
= &vpm_cfg
[0];
582 if (!final_vpm_cfg
) {
583 /* FIXME: maybe return a boolean to indicate failure and use
584 * that to stop the submission for this draw call.
586 fprintf(stderr
, "Failed to allocate VPM memory.\n");
590 assert(final_vpm_cfg
);
591 assert(final_vpm_cfg
->Gd
<= 16);
592 assert(final_vpm_cfg
->Gv
< 4);
593 assert(final_vpm_cfg
->Ve
< 4);
594 assert(final_vpm_cfg
->Vc
>= 2 && final_vpm_cfg
->Vc
<= 4);
595 assert(final_vpm_cfg
->gs_width
== 1 ||
596 final_vpm_cfg
->gs_width
== 4 ||
597 final_vpm_cfg
->gs_width
== 8 ||
598 final_vpm_cfg
->gs_width
== 16);
600 *vpm_cfg_out
= *final_vpm_cfg
;
605 v3d_emit_gl_shader_state(struct v3d_context
*v3d
,
606 const struct pipe_draw_info
*info
)
608 struct v3d_job
*job
= v3d
->job
;
609 /* VC5_DIRTY_VTXSTATE */
610 struct v3d_vertex_stateobj
*vtx
= v3d
->vtx
;
611 /* VC5_DIRTY_VTXBUF */
612 struct v3d_vertexbuf_stateobj
*vertexbuf
= &v3d
->vertexbuf
;
614 /* Upload the uniforms to the indirect CL first */
615 struct v3d_cl_reloc fs_uniforms
=
616 v3d_write_uniforms(v3d
, job
, v3d
->prog
.fs
,
617 PIPE_SHADER_FRAGMENT
);
619 struct v3d_cl_reloc gs_uniforms
= { NULL
, 0 };
620 struct v3d_cl_reloc gs_bin_uniforms
= { NULL
, 0 };
622 gs_uniforms
= v3d_write_uniforms(v3d
, job
, v3d
->prog
.gs
,
623 PIPE_SHADER_GEOMETRY
);
625 if (v3d
->prog
.gs_bin
) {
626 gs_bin_uniforms
= v3d_write_uniforms(v3d
, job
, v3d
->prog
.gs_bin
,
627 PIPE_SHADER_GEOMETRY
);
630 struct v3d_cl_reloc vs_uniforms
=
631 v3d_write_uniforms(v3d
, job
, v3d
->prog
.vs
,
633 struct v3d_cl_reloc cs_uniforms
=
634 v3d_write_uniforms(v3d
, job
, v3d
->prog
.cs
,
637 /* Update the cache dirty flag based on the shader progs data */
638 job
->tmu_dirty_rcl
|= v3d
->prog
.cs
->prog_data
.vs
->base
.tmu_dirty_rcl
;
639 job
->tmu_dirty_rcl
|= v3d
->prog
.vs
->prog_data
.vs
->base
.tmu_dirty_rcl
;
640 if (v3d
->prog
.gs_bin
) {
641 job
->tmu_dirty_rcl
|=
642 v3d
->prog
.gs_bin
->prog_data
.gs
->base
.tmu_dirty_rcl
;
645 job
->tmu_dirty_rcl
|=
646 v3d
->prog
.gs
->prog_data
.gs
->base
.tmu_dirty_rcl
;
648 job
->tmu_dirty_rcl
|= v3d
->prog
.fs
->prog_data
.fs
->base
.tmu_dirty_rcl
;
650 /* See GFXH-930 workaround below */
651 uint32_t num_elements_to_emit
= MAX2(vtx
->num_elements
, 1);
653 uint32_t shader_state_record_length
=
654 cl_packet_length(GL_SHADER_STATE_RECORD
);
655 #if V3D_VERSION >= 41
657 shader_state_record_length
+=
658 cl_packet_length(GEOMETRY_SHADER_STATE_RECORD
) +
659 cl_packet_length(TESSELLATION_GEOMETRY_COMMON_PARAMS
) +
660 2 * cl_packet_length(TESSELLATION_GEOMETRY_SHADER_PARAMS
);
664 uint32_t shader_rec_offset
=
665 v3d_cl_ensure_space(&job
->indirect
,
666 shader_state_record_length
+
667 num_elements_to_emit
*
668 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD
),
671 /* XXX perf: We should move most of the SHADER_STATE_RECORD setup to
672 * compile time, so that we mostly just have to OR the VS and FS
673 * records together at draw time.
676 struct vpm_config vpm_cfg_bin
, vpm_cfg
;
678 assert(v3d
->screen
->devinfo
.ver
>= 41 || !v3d
->prog
.gs
);
682 vpm_cfg_bin
.Vc
= v3d
->prog
.cs
->prog_data
.vs
->vcm_cache_size
;
686 vpm_cfg
.Vc
= v3d
->prog
.vs
->prog_data
.vs
->vcm_cache_size
;
688 #if V3D_VERSION >= 41
690 v3d_emit_gs_state_record(v3d
->job
,
691 v3d
->prog
.gs_bin
, gs_bin_uniforms
,
692 v3d
->prog
.gs
, gs_uniforms
);
694 struct v3d_gs_prog_data
*gs
= v3d
->prog
.gs
->prog_data
.gs
;
695 struct v3d_gs_prog_data
*gs_bin
= v3d
->prog
.gs_bin
->prog_data
.gs
;
697 v3d_emit_tes_gs_common_params(v3d
->job
,
699 gs
->num_invocations
);
701 /* Bin Tes/Gs params */
702 struct v3d_vs_prog_data
*vs_bin
= v3d
->prog
.cs
->prog_data
.vs
;
703 compute_vpm_config_gs(&v3d
->screen
->devinfo
,
704 vs_bin
, gs_bin
, &vpm_cfg_bin
);
706 v3d_emit_tes_gs_shader_params(v3d
->job
,
707 vpm_cfg_bin
.gs_width
,
711 /* Render Tes/Gs params */
712 struct v3d_vs_prog_data
*vs
= v3d
->prog
.vs
->prog_data
.vs
;
713 compute_vpm_config_gs(&v3d
->screen
->devinfo
,
716 v3d_emit_tes_gs_shader_params(v3d
->job
,
723 cl_emit(&job
->indirect
, GL_SHADER_STATE_RECORD
, shader
) {
724 shader
.enable_clipping
= true;
725 /* VC5_DIRTY_PRIM_MODE | VC5_DIRTY_RASTERIZER */
726 shader
.point_size_in_shaded_vertex_data
=
727 (info
->mode
== PIPE_PRIM_POINTS
&&
728 v3d
->rasterizer
->base
.point_size_per_vertex
);
730 /* Must be set if the shader modifies Z, discards, or modifies
731 * the sample mask. For any of these cases, the fragment
732 * shader needs to write the Z value (even just discards).
734 shader
.fragment_shader_does_z_writes
=
735 v3d
->prog
.fs
->prog_data
.fs
->writes_z
;
736 /* Set if the EZ test must be disabled (due to shader side
737 * effects and the early_z flag not being present in the
740 shader
.turn_off_early_z_test
=
741 v3d
->prog
.fs
->prog_data
.fs
->disable_ez
;
743 shader
.fragment_shader_uses_real_pixel_centre_w_in_addition_to_centroid_w2
=
744 v3d
->prog
.fs
->prog_data
.fs
->uses_center_w
;
746 #if V3D_VERSION >= 41
747 shader
.any_shader_reads_hardware_written_primitive_id
=
748 v3d
->prog
.gs
? v3d
->prog
.gs
->prog_data
.gs
->uses_pid
:
752 #if V3D_VERSION >= 40
753 shader
.do_scoreboard_wait_on_first_thread_switch
=
754 v3d
->prog
.fs
->prog_data
.fs
->lock_scoreboard_on_first_thrsw
;
755 shader
.disable_implicit_point_line_varyings
=
756 !v3d
->prog
.fs
->prog_data
.fs
->uses_implicit_point_line_varyings
;
759 shader
.number_of_varyings_in_fragment_shader
=
760 v3d
->prog
.fs
->prog_data
.fs
->num_inputs
;
762 shader
.coordinate_shader_propagate_nans
= true;
763 shader
.vertex_shader_propagate_nans
= true;
764 shader
.fragment_shader_propagate_nans
= true;
766 shader
.coordinate_shader_code_address
=
767 cl_address(v3d_resource(v3d
->prog
.cs
->resource
)->bo
,
768 v3d
->prog
.cs
->offset
);
769 shader
.vertex_shader_code_address
=
770 cl_address(v3d_resource(v3d
->prog
.vs
->resource
)->bo
,
771 v3d
->prog
.vs
->offset
);
772 shader
.fragment_shader_code_address
=
773 cl_address(v3d_resource(v3d
->prog
.fs
->resource
)->bo
,
774 v3d
->prog
.fs
->offset
);
776 /* XXX: Use combined input/output size flag in the common
779 shader
.coordinate_shader_has_separate_input_and_output_vpm_blocks
=
780 v3d
->prog
.cs
->prog_data
.vs
->separate_segments
;
781 shader
.vertex_shader_has_separate_input_and_output_vpm_blocks
=
782 v3d
->prog
.vs
->prog_data
.vs
->separate_segments
;
784 shader
.coordinate_shader_input_vpm_segment_size
=
785 v3d
->prog
.cs
->prog_data
.vs
->separate_segments
?
786 v3d
->prog
.cs
->prog_data
.vs
->vpm_input_size
: 1;
787 shader
.vertex_shader_input_vpm_segment_size
=
788 v3d
->prog
.vs
->prog_data
.vs
->separate_segments
?
789 v3d
->prog
.vs
->prog_data
.vs
->vpm_input_size
: 1;
791 shader
.coordinate_shader_output_vpm_segment_size
=
792 v3d
->prog
.cs
->prog_data
.vs
->vpm_output_size
;
793 shader
.vertex_shader_output_vpm_segment_size
=
794 v3d
->prog
.vs
->prog_data
.vs
->vpm_output_size
;
796 shader
.coordinate_shader_uniforms_address
= cs_uniforms
;
797 shader
.vertex_shader_uniforms_address
= vs_uniforms
;
798 shader
.fragment_shader_uniforms_address
= fs_uniforms
;
800 #if V3D_VERSION >= 41
801 shader
.min_coord_shader_input_segments_required_in_play
=
803 shader
.min_vertex_shader_input_segments_required_in_play
=
806 shader
.min_coord_shader_output_segments_required_in_play_in_addition_to_vcm_cache_size
=
808 shader
.min_vertex_shader_output_segments_required_in_play_in_addition_to_vcm_cache_size
=
811 shader
.coordinate_shader_4_way_threadable
=
812 v3d
->prog
.cs
->prog_data
.vs
->base
.threads
== 4;
813 shader
.vertex_shader_4_way_threadable
=
814 v3d
->prog
.vs
->prog_data
.vs
->base
.threads
== 4;
815 shader
.fragment_shader_4_way_threadable
=
816 v3d
->prog
.fs
->prog_data
.fs
->base
.threads
== 4;
818 shader
.coordinate_shader_start_in_final_thread_section
=
819 v3d
->prog
.cs
->prog_data
.vs
->base
.single_seg
;
820 shader
.vertex_shader_start_in_final_thread_section
=
821 v3d
->prog
.vs
->prog_data
.vs
->base
.single_seg
;
822 shader
.fragment_shader_start_in_final_thread_section
=
823 v3d
->prog
.fs
->prog_data
.fs
->base
.single_seg
;
825 shader
.coordinate_shader_4_way_threadable
=
826 v3d
->prog
.cs
->prog_data
.vs
->base
.threads
== 4;
827 shader
.coordinate_shader_2_way_threadable
=
828 v3d
->prog
.cs
->prog_data
.vs
->base
.threads
== 2;
829 shader
.vertex_shader_4_way_threadable
=
830 v3d
->prog
.vs
->prog_data
.vs
->base
.threads
== 4;
831 shader
.vertex_shader_2_way_threadable
=
832 v3d
->prog
.vs
->prog_data
.vs
->base
.threads
== 2;
833 shader
.fragment_shader_4_way_threadable
=
834 v3d
->prog
.fs
->prog_data
.fs
->base
.threads
== 4;
835 shader
.fragment_shader_2_way_threadable
=
836 v3d
->prog
.fs
->prog_data
.fs
->base
.threads
== 2;
839 shader
.vertex_id_read_by_coordinate_shader
=
840 v3d
->prog
.cs
->prog_data
.vs
->uses_vid
;
841 shader
.instance_id_read_by_coordinate_shader
=
842 v3d
->prog
.cs
->prog_data
.vs
->uses_iid
;
843 shader
.vertex_id_read_by_vertex_shader
=
844 v3d
->prog
.vs
->prog_data
.vs
->uses_vid
;
845 shader
.instance_id_read_by_vertex_shader
=
846 v3d
->prog
.vs
->prog_data
.vs
->uses_iid
;
848 shader
.address_of_default_attribute_values
=
849 cl_address(v3d_resource(vtx
->defaults
)->bo
,
850 vtx
->defaults_offset
);
853 bool cs_loaded_any
= false;
854 for (int i
= 0; i
< vtx
->num_elements
; i
++) {
855 struct pipe_vertex_element
*elem
= &vtx
->pipe
[i
];
856 struct pipe_vertex_buffer
*vb
=
857 &vertexbuf
->vb
[elem
->vertex_buffer_index
];
858 struct v3d_resource
*rsc
= v3d_resource(vb
->buffer
.resource
);
860 const uint32_t size
=
861 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD
);
862 cl_emit_with_prepacked(&job
->indirect
,
863 GL_SHADER_STATE_ATTRIBUTE_RECORD
,
864 &vtx
->attrs
[i
* size
], attr
) {
865 attr
.stride
= vb
->stride
;
866 attr
.address
= cl_address(rsc
->bo
,
869 attr
.number_of_values_read_by_coordinate_shader
=
870 v3d
->prog
.cs
->prog_data
.vs
->vattr_sizes
[i
];
871 attr
.number_of_values_read_by_vertex_shader
=
872 v3d
->prog
.vs
->prog_data
.vs
->vattr_sizes
[i
];
874 /* GFXH-930: At least one attribute must be enabled
875 * and read by CS and VS. If we have attributes being
876 * consumed by the VS but not the CS, then set up a
877 * dummy load of the last attribute into the CS's VPM
878 * inputs. (Since CS is just dead-code-elimination
879 * compared to VS, we can't have CS loading but not
882 if (v3d
->prog
.cs
->prog_data
.vs
->vattr_sizes
[i
])
883 cs_loaded_any
= true;
884 if (i
== vtx
->num_elements
- 1 && !cs_loaded_any
) {
885 attr
.number_of_values_read_by_coordinate_shader
= 1;
887 #if V3D_VERSION >= 41
888 attr
.maximum_index
= 0xffffff;
891 STATIC_ASSERT(sizeof(vtx
->attrs
) >= V3D_MAX_VS_INPUTS
/ 4 * size
);
894 if (vtx
->num_elements
== 0) {
895 /* GFXH-930: At least one attribute must be enabled and read
896 * by CS and VS. If we have no attributes being consumed by
897 * the shader, set up a dummy to be loaded into the VPM.
899 cl_emit(&job
->indirect
, GL_SHADER_STATE_ATTRIBUTE_RECORD
, attr
) {
900 /* Valid address of data whose value will be unused. */
901 attr
.address
= cl_address(job
->indirect
.bo
, 0);
903 attr
.type
= ATTRIBUTE_FLOAT
;
907 attr
.number_of_values_read_by_coordinate_shader
= 1;
908 attr
.number_of_values_read_by_vertex_shader
= 1;
912 cl_emit(&job
->bcl
, VCM_CACHE_SIZE
, vcm
) {
913 vcm
.number_of_16_vertex_batches_for_binning
= vpm_cfg_bin
.Vc
;
914 vcm
.number_of_16_vertex_batches_for_rendering
= vpm_cfg
.Vc
;
917 #if V3D_VERSION >= 41
919 cl_emit(&job
->bcl
, GL_SHADER_STATE_INCLUDING_GS
, state
) {
920 state
.address
= cl_address(job
->indirect
.bo
,
922 state
.number_of_attribute_arrays
= num_elements_to_emit
;
925 cl_emit(&job
->bcl
, GL_SHADER_STATE
, state
) {
926 state
.address
= cl_address(job
->indirect
.bo
,
928 state
.number_of_attribute_arrays
= num_elements_to_emit
;
932 assert(!v3d
->prog
.gs
);
933 cl_emit(&job
->bcl
, GL_SHADER_STATE
, state
) {
934 state
.address
= cl_address(job
->indirect
.bo
, shader_rec_offset
);
935 state
.number_of_attribute_arrays
= num_elements_to_emit
;
939 v3d_bo_unreference(&cs_uniforms
.bo
);
940 v3d_bo_unreference(&vs_uniforms
.bo
);
942 v3d_bo_unreference(&gs_uniforms
.bo
);
943 if (gs_bin_uniforms
.bo
)
944 v3d_bo_unreference(&gs_bin_uniforms
.bo
);
945 v3d_bo_unreference(&fs_uniforms
.bo
);
949 * Updates the number of primitvies generated from the number of vertices
950 * to draw. We do this here instead of using PRIMITIVE_COUNTS_FEEDBACK because
951 * using the GPU packet for this might require sync waits and this is trivial
952 * to handle in the CPU instead.
955 v3d_update_primitives_generated_counter(struct v3d_context
*v3d
,
956 const struct pipe_draw_info
*info
)
958 if (!v3d
->active_queries
)
961 uint32_t prims
= u_prims_for_vertices(info
->mode
, info
->count
);
962 v3d
->prims_generated
+= prims
;
966 v3d_update_job_ez(struct v3d_context
*v3d
, struct v3d_job
*job
)
968 switch (v3d
->zsa
->ez_state
) {
969 case VC5_EZ_UNDECIDED
:
970 /* If the Z/S state didn't pick a direction but didn't
971 * disable, then go along with the current EZ state. This
972 * allows EZ optimization for Z func == EQUAL or NEVER.
978 /* If the Z/S state picked a direction, then it needs to match
979 * the current direction if we've decided on one.
981 if (job
->ez_state
== VC5_EZ_UNDECIDED
)
982 job
->ez_state
= v3d
->zsa
->ez_state
;
983 else if (job
->ez_state
!= v3d
->zsa
->ez_state
)
984 job
->ez_state
= VC5_EZ_DISABLED
;
987 case VC5_EZ_DISABLED
:
988 /* If the current Z/S state disables EZ because of a bad Z
989 * func or stencil operation, then we can't do any more EZ in
992 job
->ez_state
= VC5_EZ_DISABLED
;
996 /* If the FS affects the Z of the pixels, then it may update against
997 * the chosen EZ direction (though we could use
998 * ARB_conservative_depth's hints to avoid this)
1000 if (v3d
->prog
.fs
->prog_data
.fs
->writes_z
) {
1001 job
->ez_state
= VC5_EZ_DISABLED
;
1004 if (job
->first_ez_state
== VC5_EZ_UNDECIDED
&&
1005 (job
->ez_state
!= VC5_EZ_DISABLED
|| job
->draw_calls_queued
== 0))
1006 job
->first_ez_state
= job
->ez_state
;
1010 v3d_draw_vbo(struct pipe_context
*pctx
, const struct pipe_draw_info
*info
)
1012 struct v3d_context
*v3d
= v3d_context(pctx
);
1014 if (!info
->count_from_stream_output
&& !info
->indirect
&&
1015 !info
->primitive_restart
&&
1016 !u_trim_pipe_prim(info
->mode
, (unsigned*)&info
->count
))
1019 /* Fall back for weird desktop GL primitive restart values. */
1020 if (info
->primitive_restart
&&
1024 switch (info
->index_size
) {
1033 if (info
->restart_index
!= mask
) {
1034 util_draw_vbo_without_prim_restart(pctx
, info
);
1039 if (info
->mode
>= PIPE_PRIM_QUADS
) {
1040 util_primconvert_save_rasterizer_state(v3d
->primconvert
, &v3d
->rasterizer
->base
);
1041 util_primconvert_draw_vbo(v3d
->primconvert
, info
);
1042 perf_debug("Fallback conversion for %d %s vertices\n",
1043 info
->count
, u_prim_name(info
->mode
));
1047 /* Before setting up the draw, flush anything writing to the resources
1048 * that we read from or reading from resources we write to.
1050 for (int s
= 0; s
< PIPE_SHADER_COMPUTE
; s
++)
1051 v3d_predraw_check_stage_inputs(pctx
, s
);
1053 if (info
->indirect
) {
1054 v3d_flush_jobs_writing_resource(v3d
, info
->indirect
->buffer
,
1058 v3d_predraw_check_outputs(pctx
);
1060 /* If transform feedback is active and we are switching primitive type
1061 * we need to submit the job before drawing and update the vertex count
1062 * written to TF based on the primitive type since we will need to
1063 * know the exact vertex count if the application decides to call
1064 * glDrawTransformFeedback() later.
1066 if (v3d
->streamout
.num_targets
> 0 &&
1067 u_base_prim_type(info
->mode
) != u_base_prim_type(v3d
->prim_mode
)) {
1068 v3d_tf_update_counters(v3d
);
1071 struct v3d_job
*job
= v3d_get_job_for_fbo(v3d
);
1073 /* If vertex texturing depends on the output of rendering, we need to
1074 * ensure that that rendering is complete before we run a coordinate
1075 * shader that depends on it.
1077 * Given that doing that is unusual, for now we just block the binner
1078 * on the last submitted render, rather than tracking the last
1079 * rendering to each texture's BO.
1081 if (v3d
->tex
[PIPE_SHADER_VERTEX
].num_textures
|| info
->indirect
) {
1082 perf_debug("Blocking binner on last render "
1083 "due to vertex texturing or indirect drawing.\n");
1084 job
->submit
.in_sync_bcl
= v3d
->out_sync
;
1087 /* Mark SSBOs and images as being written. We don't actually know
1088 * which ones are read vs written, so just assume the worst.
1090 for (int s
= 0; s
< PIPE_SHADER_COMPUTE
; s
++) {
1091 foreach_bit(i
, v3d
->ssbo
[s
].enabled_mask
) {
1092 v3d_job_add_write_resource(job
,
1093 v3d
->ssbo
[s
].sb
[i
].buffer
);
1094 job
->tmu_dirty_rcl
= true;
1097 foreach_bit(i
, v3d
->shaderimg
[s
].enabled_mask
) {
1098 v3d_job_add_write_resource(job
,
1099 v3d
->shaderimg
[s
].si
[i
].base
.resource
);
1100 job
->tmu_dirty_rcl
= true;
1104 /* Get space to emit our draw call into the BCL, using a branch to
1105 * jump to a new BO if necessary.
1107 v3d_cl_ensure_space_with_branch(&job
->bcl
, 256 /* XXX */);
1109 if (v3d
->prim_mode
!= info
->mode
) {
1110 v3d
->prim_mode
= info
->mode
;
1111 v3d
->dirty
|= VC5_DIRTY_PRIM_MODE
;
1114 v3d_start_draw(v3d
);
1115 v3d_update_compiled_shaders(v3d
, info
->mode
);
1116 v3d_update_job_ez(v3d
, job
);
1118 /* If this job was writing to transform feedback buffers before this
1119 * draw and we are reading from them here, then we need to wait for TF
1120 * to complete before we emit this draw.
1122 * Notice this check needs to happen before we emit state for the
1123 * current draw call, where we update job->tf_enabled, so we can ensure
1124 * that we only check TF writes for prior draws.
1126 v3d_emit_wait_for_tf_if_needed(v3d
, job
);
1128 #if V3D_VERSION >= 41
1129 v3d41_emit_state(pctx
);
1131 v3d33_emit_state(pctx
);
1134 if (v3d
->dirty
& (VC5_DIRTY_VTXBUF
|
1135 VC5_DIRTY_VTXSTATE
|
1136 VC5_DIRTY_PRIM_MODE
|
1137 VC5_DIRTY_RASTERIZER
|
1138 VC5_DIRTY_COMPILED_CS
|
1139 VC5_DIRTY_COMPILED_VS
|
1140 VC5_DIRTY_COMPILED_GS_BIN
|
1141 VC5_DIRTY_COMPILED_GS
|
1142 VC5_DIRTY_COMPILED_FS
|
1143 v3d
->prog
.cs
->uniform_dirty_bits
|
1144 v3d
->prog
.vs
->uniform_dirty_bits
|
1146 v3d
->prog
.gs_bin
->uniform_dirty_bits
: 0) |
1148 v3d
->prog
.gs
->uniform_dirty_bits
: 0) |
1149 v3d
->prog
.fs
->uniform_dirty_bits
)) {
1150 v3d_emit_gl_shader_state(v3d
, info
);
1155 /* The Base Vertex/Base Instance packet sets those values to nonzero
1156 * for the next draw call only.
1158 if (info
->index_bias
|| info
->start_instance
) {
1159 cl_emit(&job
->bcl
, BASE_VERTEX_BASE_INSTANCE
, base
) {
1160 base
.base_instance
= info
->start_instance
;
1161 base
.base_vertex
= info
->index_bias
;
1165 uint32_t prim_tf_enable
= 0;
1166 #if V3D_VERSION < 40
1167 /* V3D 3.x: The HW only processes transform feedback on primitives
1168 * with the flag set.
1170 if (v3d
->streamout
.num_targets
)
1171 prim_tf_enable
= (V3D_PRIM_POINTS_TF
- V3D_PRIM_POINTS
);
1174 v3d_update_primitives_generated_counter(v3d
, info
);
1176 /* Note that the primitive type fields match with OpenGL/gallium
1177 * definitions, up to but not including QUADS.
1179 if (info
->index_size
) {
1180 uint32_t index_size
= info
->index_size
;
1181 uint32_t offset
= info
->start
* index_size
;
1182 struct pipe_resource
*prsc
;
1183 if (info
->has_user_indices
) {
1185 u_upload_data(v3d
->uploader
, 0,
1186 info
->count
* info
->index_size
, 4,
1190 prsc
= info
->index
.resource
;
1192 struct v3d_resource
*rsc
= v3d_resource(prsc
);
1194 #if V3D_VERSION >= 40
1195 cl_emit(&job
->bcl
, INDEX_BUFFER_SETUP
, ib
) {
1196 ib
.address
= cl_address(rsc
->bo
, 0);
1197 ib
.size
= rsc
->bo
->size
;
1201 if (info
->indirect
) {
1202 cl_emit(&job
->bcl
, INDIRECT_INDEXED_INSTANCED_PRIM_LIST
, prim
) {
1203 prim
.index_type
= ffs(info
->index_size
) - 1;
1204 #if V3D_VERSION < 40
1205 prim
.address_of_indices_list
=
1206 cl_address(rsc
->bo
, offset
);
1207 #endif /* V3D_VERSION < 40 */
1208 prim
.mode
= info
->mode
| prim_tf_enable
;
1209 prim
.enable_primitive_restarts
= info
->primitive_restart
;
1211 prim
.number_of_draw_indirect_indexed_records
= info
->indirect
->draw_count
;
1213 prim
.stride_in_multiples_of_4_bytes
= info
->indirect
->stride
>> 2;
1214 prim
.address
= cl_address(v3d_resource(info
->indirect
->buffer
)->bo
,
1215 info
->indirect
->offset
);
1217 } else if (info
->instance_count
> 1) {
1218 cl_emit(&job
->bcl
, INDEXED_INSTANCED_PRIM_LIST
, prim
) {
1219 prim
.index_type
= ffs(info
->index_size
) - 1;
1220 #if V3D_VERSION >= 40
1221 prim
.index_offset
= offset
;
1222 #else /* V3D_VERSION < 40 */
1223 prim
.maximum_index
= (1u << 31) - 1; /* XXX */
1224 prim
.address_of_indices_list
=
1225 cl_address(rsc
->bo
, offset
);
1226 #endif /* V3D_VERSION < 40 */
1227 prim
.mode
= info
->mode
| prim_tf_enable
;
1228 prim
.enable_primitive_restarts
= info
->primitive_restart
;
1230 prim
.number_of_instances
= info
->instance_count
;
1231 prim
.instance_length
= info
->count
;
1234 cl_emit(&job
->bcl
, INDEXED_PRIM_LIST
, prim
) {
1235 prim
.index_type
= ffs(info
->index_size
) - 1;
1236 prim
.length
= info
->count
;
1237 #if V3D_VERSION >= 40
1238 prim
.index_offset
= offset
;
1239 #else /* V3D_VERSION < 40 */
1240 prim
.maximum_index
= (1u << 31) - 1; /* XXX */
1241 prim
.address_of_indices_list
=
1242 cl_address(rsc
->bo
, offset
);
1243 #endif /* V3D_VERSION < 40 */
1244 prim
.mode
= info
->mode
| prim_tf_enable
;
1245 prim
.enable_primitive_restarts
= info
->primitive_restart
;
1249 if (info
->has_user_indices
)
1250 pipe_resource_reference(&prsc
, NULL
);
1252 if (info
->indirect
) {
1253 cl_emit(&job
->bcl
, INDIRECT_VERTEX_ARRAY_INSTANCED_PRIMS
, prim
) {
1254 prim
.mode
= info
->mode
| prim_tf_enable
;
1255 prim
.number_of_draw_indirect_array_records
= info
->indirect
->draw_count
;
1257 prim
.stride_in_multiples_of_4_bytes
= info
->indirect
->stride
>> 2;
1258 prim
.address
= cl_address(v3d_resource(info
->indirect
->buffer
)->bo
,
1259 info
->indirect
->offset
);
1261 } else if (info
->instance_count
> 1) {
1262 struct pipe_stream_output_target
*so
=
1263 info
->count_from_stream_output
;
1264 uint32_t vert_count
= so
?
1265 v3d_stream_output_target_get_vertex_count(so
) :
1267 cl_emit(&job
->bcl
, VERTEX_ARRAY_INSTANCED_PRIMS
, prim
) {
1268 prim
.mode
= info
->mode
| prim_tf_enable
;
1269 prim
.index_of_first_vertex
= info
->start
;
1270 prim
.number_of_instances
= info
->instance_count
;
1271 prim
.instance_length
= vert_count
;
1274 struct pipe_stream_output_target
*so
=
1275 info
->count_from_stream_output
;
1276 uint32_t vert_count
= so
?
1277 v3d_stream_output_target_get_vertex_count(so
) :
1279 cl_emit(&job
->bcl
, VERTEX_ARRAY_PRIMS
, prim
) {
1280 prim
.mode
= info
->mode
| prim_tf_enable
;
1281 prim
.length
= vert_count
;
1282 prim
.index_of_first_vertex
= info
->start
;
1287 /* A flush is required in between a TF draw and any following TF specs
1288 * packet, or the GPU may hang. Just flush each time for now.
1290 if (v3d
->streamout
.num_targets
)
1291 cl_emit(&job
->bcl
, TRANSFORM_FEEDBACK_FLUSH_AND_COUNT
, flush
);
1293 job
->draw_calls_queued
++;
1294 if (v3d
->streamout
.num_targets
)
1295 job
->tf_draw_calls_queued
++;
1297 /* Increment the TF offsets by how many verts we wrote. XXX: This
1298 * needs some clamping to the buffer size.
1300 for (int i
= 0; i
< v3d
->streamout
.num_targets
; i
++)
1301 v3d
->streamout
.offsets
[i
] += info
->count
;
1303 if (v3d
->zsa
&& job
->zsbuf
&& v3d
->zsa
->base
.depth
.enabled
) {
1304 struct v3d_resource
*rsc
= v3d_resource(job
->zsbuf
->texture
);
1305 v3d_job_add_bo(job
, rsc
->bo
);
1307 job
->load
|= PIPE_CLEAR_DEPTH
& ~job
->clear
;
1308 if (v3d
->zsa
->base
.depth
.writemask
)
1309 job
->store
|= PIPE_CLEAR_DEPTH
;
1310 rsc
->initialized_buffers
= PIPE_CLEAR_DEPTH
;
1313 if (v3d
->zsa
&& job
->zsbuf
&& v3d
->zsa
->base
.stencil
[0].enabled
) {
1314 struct v3d_resource
*rsc
= v3d_resource(job
->zsbuf
->texture
);
1315 if (rsc
->separate_stencil
)
1316 rsc
= rsc
->separate_stencil
;
1318 v3d_job_add_bo(job
, rsc
->bo
);
1320 job
->load
|= PIPE_CLEAR_STENCIL
& ~job
->clear
;
1321 if (v3d
->zsa
->base
.stencil
[0].writemask
||
1322 v3d
->zsa
->base
.stencil
[1].writemask
) {
1323 job
->store
|= PIPE_CLEAR_STENCIL
;
1325 rsc
->initialized_buffers
|= PIPE_CLEAR_STENCIL
;
1328 for (int i
= 0; i
< V3D_MAX_DRAW_BUFFERS
; i
++) {
1329 uint32_t bit
= PIPE_CLEAR_COLOR0
<< i
;
1330 int blend_rt
= v3d
->blend
->base
.independent_blend_enable
? i
: 0;
1332 if (job
->store
& bit
|| !job
->cbufs
[i
])
1334 struct v3d_resource
*rsc
= v3d_resource(job
->cbufs
[i
]->texture
);
1336 job
->load
|= bit
& ~job
->clear
;
1337 if (v3d
->blend
->base
.rt
[blend_rt
].colormask
)
1339 v3d_job_add_bo(job
, rsc
->bo
);
1342 if (job
->referenced_size
> 768 * 1024 * 1024) {
1343 perf_debug("Flushing job with %dkb to try to free up memory\n",
1344 job
->referenced_size
/ 1024);
1348 if (V3D_DEBUG
& V3D_DEBUG_ALWAYS_FLUSH
)
1352 #if V3D_VERSION >= 41
1353 #define V3D_CSD_CFG012_WG_COUNT_SHIFT 16
1354 #define V3D_CSD_CFG012_WG_OFFSET_SHIFT 0
1355 /* Allow this dispatch to start while the last one is still running. */
1356 #define V3D_CSD_CFG3_OVERLAP_WITH_PREV (1 << 26)
1357 /* Maximum supergroup ID. 6 bits. */
1358 #define V3D_CSD_CFG3_MAX_SG_ID_SHIFT 20
1359 /* Batches per supergroup minus 1. 8 bits. */
1360 #define V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT 12
1361 /* Workgroups per supergroup, 0 means 16 */
1362 #define V3D_CSD_CFG3_WGS_PER_SG_SHIFT 8
1363 #define V3D_CSD_CFG3_WG_SIZE_SHIFT 0
1365 #define V3D_CSD_CFG5_PROPAGATE_NANS (1 << 2)
1366 #define V3D_CSD_CFG5_SINGLE_SEG (1 << 1)
1367 #define V3D_CSD_CFG5_THREADING (1 << 0)
1370 v3d_launch_grid(struct pipe_context
*pctx
, const struct pipe_grid_info
*info
)
1372 struct v3d_context
*v3d
= v3d_context(pctx
);
1373 struct v3d_screen
*screen
= v3d
->screen
;
1375 v3d_predraw_check_stage_inputs(pctx
, PIPE_SHADER_COMPUTE
);
1377 v3d_update_compiled_cs(v3d
);
1379 if (!v3d
->prog
.compute
->resource
) {
1380 static bool warned
= false;
1383 "Compute shader failed to compile. "
1384 "Expect corruption.\n");
1390 /* Some of the units of scale:
1392 * - Batches of 16 work items (shader invocations) that will be queued
1393 * to the run on a QPU at once.
1395 * - Workgroups composed of work items based on the shader's layout
1398 * - Supergroups of 1-16 workgroups. There can only be 16 supergroups
1399 * running at a time on the core, so we want to keep them large to
1400 * keep the QPUs busy, but a whole supergroup will sync at a barrier
1401 * so we want to keep them small if one is present.
1403 struct drm_v3d_submit_csd submit
= { 0 };
1404 struct v3d_job
*job
= v3d_job_create(v3d
);
1406 /* Set up the actual number of workgroups, synchronously mapping the
1407 * indirect buffer if necessary to get the dimensions.
1409 if (info
->indirect
) {
1410 struct pipe_transfer
*transfer
;
1411 uint32_t *map
= pipe_buffer_map_range(pctx
, info
->indirect
,
1412 info
->indirect_offset
,
1413 3 * sizeof(uint32_t),
1416 memcpy(v3d
->compute_num_workgroups
, map
, 3 * sizeof(uint32_t));
1417 pipe_buffer_unmap(pctx
, transfer
);
1419 if (v3d
->compute_num_workgroups
[0] == 0 ||
1420 v3d
->compute_num_workgroups
[1] == 0 ||
1421 v3d
->compute_num_workgroups
[2] == 0) {
1422 /* Nothing to dispatch, so skip the draw (CSD can't
1423 * handle 0 workgroups).
1428 v3d
->compute_num_workgroups
[0] = info
->grid
[0];
1429 v3d
->compute_num_workgroups
[1] = info
->grid
[1];
1430 v3d
->compute_num_workgroups
[2] = info
->grid
[2];
1433 for (int i
= 0; i
< 3; i
++) {
1434 submit
.cfg
[i
] |= (v3d
->compute_num_workgroups
[i
] <<
1435 V3D_CSD_CFG012_WG_COUNT_SHIFT
);
1438 perf_debug("CSD only using single WG per SG currently, "
1439 "should increase that when possible.");
1441 int wg_size
= info
->block
[0] * info
->block
[1] * info
->block
[2];
1442 submit
.cfg
[3] |= wgs_per_sg
<< V3D_CSD_CFG3_WGS_PER_SG_SHIFT
;
1443 submit
.cfg
[3] |= ((DIV_ROUND_UP(wgs_per_sg
* wg_size
, 16) - 1) <<
1444 V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT
);
1445 submit
.cfg
[3] |= (wg_size
& 0xff) << V3D_CSD_CFG3_WG_SIZE_SHIFT
;
1447 int batches_per_wg
= DIV_ROUND_UP(wg_size
, 16);
1448 /* Number of batches the dispatch will invoke (minus 1). */
1449 submit
.cfg
[4] = batches_per_wg
* (v3d
->compute_num_workgroups
[0] *
1450 v3d
->compute_num_workgroups
[1] *
1451 v3d
->compute_num_workgroups
[2]) - 1;
1453 /* Make sure we didn't accidentally underflow. */
1454 assert(submit
.cfg
[4] != ~0);
1456 v3d_job_add_bo(job
, v3d_resource(v3d
->prog
.compute
->resource
)->bo
);
1457 submit
.cfg
[5] = (v3d_resource(v3d
->prog
.compute
->resource
)->bo
->offset
+
1458 v3d
->prog
.compute
->offset
);
1459 submit
.cfg
[5] |= V3D_CSD_CFG5_PROPAGATE_NANS
;
1460 if (v3d
->prog
.compute
->prog_data
.base
->single_seg
)
1461 submit
.cfg
[5] |= V3D_CSD_CFG5_SINGLE_SEG
;
1462 if (v3d
->prog
.compute
->prog_data
.base
->threads
== 4)
1463 submit
.cfg
[5] |= V3D_CSD_CFG5_THREADING
;
1465 if (v3d
->prog
.compute
->prog_data
.compute
->shared_size
) {
1466 v3d
->compute_shared_memory
=
1467 v3d_bo_alloc(v3d
->screen
,
1468 v3d
->prog
.compute
->prog_data
.compute
->shared_size
*
1473 struct v3d_cl_reloc uniforms
= v3d_write_uniforms(v3d
, job
,
1475 PIPE_SHADER_COMPUTE
);
1476 v3d_job_add_bo(job
, uniforms
.bo
);
1477 submit
.cfg
[6] = uniforms
.bo
->offset
+ uniforms
.offset
;
1479 /* Pull some job state that was stored in a SUBMIT_CL struct out to
1480 * our SUBMIT_CSD struct
1482 submit
.bo_handles
= job
->submit
.bo_handles
;
1483 submit
.bo_handle_count
= job
->submit
.bo_handle_count
;
1485 /* Serialize this in the rest of our command stream. */
1486 submit
.in_sync
= v3d
->out_sync
;
1487 submit
.out_sync
= v3d
->out_sync
;
1489 if (!(V3D_DEBUG
& V3D_DEBUG_NORAST
)) {
1490 int ret
= v3d_ioctl(screen
->fd
, DRM_IOCTL_V3D_SUBMIT_CSD
,
1492 static bool warned
= false;
1493 if (ret
&& !warned
) {
1494 fprintf(stderr
, "CSD submit call returned %s. "
1495 "Expect corruption.\n", strerror(errno
));
1500 v3d_job_free(v3d
, job
);
1502 /* Mark SSBOs as being written.. we don't actually know which ones are
1503 * read vs written, so just assume the worst
1505 foreach_bit(i
, v3d
->ssbo
[PIPE_SHADER_COMPUTE
].enabled_mask
) {
1506 struct v3d_resource
*rsc
= v3d_resource(
1507 v3d
->ssbo
[PIPE_SHADER_COMPUTE
].sb
[i
].buffer
);
1508 rsc
->writes
++; /* XXX */
1511 foreach_bit(i
, v3d
->shaderimg
[PIPE_SHADER_COMPUTE
].enabled_mask
) {
1512 struct v3d_resource
*rsc
= v3d_resource(
1513 v3d
->shaderimg
[PIPE_SHADER_COMPUTE
].si
[i
].base
.resource
);
1517 v3d_bo_unreference(&uniforms
.bo
);
1518 v3d_bo_unreference(&v3d
->compute_shared_memory
);
1523 * Implements gallium's clear() hook (glClear()) by drawing a pair of triangles.
1526 v3d_draw_clear(struct v3d_context
*v3d
,
1528 const union pipe_color_union
*color
,
1529 double depth
, unsigned stencil
)
1531 static const union pipe_color_union dummy_color
= {};
1533 /* The blitter util dereferences the color regardless, even though the
1534 * gallium clear API may not pass one in when only Z/S are cleared.
1537 color
= &dummy_color
;
1539 v3d_blitter_save(v3d
);
1540 util_blitter_clear(v3d
->blitter
,
1541 v3d
->framebuffer
.width
,
1542 v3d
->framebuffer
.height
,
1543 util_framebuffer_get_num_layers(&v3d
->framebuffer
),
1544 buffers
, color
, depth
, stencil
,
1545 util_framebuffer_get_num_samples(&v3d
->framebuffer
) > 1);
1549 * Attempts to perform the GL clear by using the TLB's fast clear at the start
1553 v3d_tlb_clear(struct v3d_job
*job
, unsigned buffers
,
1554 const union pipe_color_union
*color
,
1555 double depth
, unsigned stencil
)
1557 struct v3d_context
*v3d
= job
->v3d
;
1559 if (job
->draw_calls_queued
) {
1560 /* If anything in the CL has drawn using the buffer, then the
1561 * TLB clear we're trying to add now would happen before that
1564 buffers
&= ~(job
->load
| job
->store
);
1567 /* GFXH-1461: If we were to emit a load of just depth or just stencil,
1568 * then the clear for the other may get lost. We need to decide now
1569 * if it would be possible to need to emit a load of just one after
1570 * we've set up our TLB clears.
1572 if (buffers
& PIPE_CLEAR_DEPTHSTENCIL
&&
1573 (buffers
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
&&
1575 util_format_is_depth_and_stencil(job
->zsbuf
->texture
->format
)) {
1576 buffers
&= ~PIPE_CLEAR_DEPTHSTENCIL
;
1579 for (int i
= 0; i
< V3D_MAX_DRAW_BUFFERS
; i
++) {
1580 uint32_t bit
= PIPE_CLEAR_COLOR0
<< i
;
1581 if (!(buffers
& bit
))
1584 struct pipe_surface
*psurf
= v3d
->framebuffer
.cbufs
[i
];
1585 struct v3d_surface
*surf
= v3d_surface(psurf
);
1586 struct v3d_resource
*rsc
= v3d_resource(psurf
->texture
);
1588 union util_color uc
;
1589 uint32_t internal_size
= 4 << surf
->internal_bpp
;
1591 static union pipe_color_union swapped_color
;
1592 if (v3d
->swap_color_rb
& (1 << i
)) {
1593 swapped_color
.f
[0] = color
->f
[2];
1594 swapped_color
.f
[1] = color
->f
[1];
1595 swapped_color
.f
[2] = color
->f
[0];
1596 swapped_color
.f
[3] = color
->f
[3];
1597 color
= &swapped_color
;
1600 switch (surf
->internal_type
) {
1601 case V3D_INTERNAL_TYPE_8
:
1602 util_pack_color(color
->f
, PIPE_FORMAT_R8G8B8A8_UNORM
,
1604 memcpy(job
->clear_color
[i
], uc
.ui
, internal_size
);
1606 case V3D_INTERNAL_TYPE_8I
:
1607 case V3D_INTERNAL_TYPE_8UI
:
1608 job
->clear_color
[i
][0] = ((color
->ui
[0] & 0xff) |
1609 (color
->ui
[1] & 0xff) << 8 |
1610 (color
->ui
[2] & 0xff) << 16 |
1611 (color
->ui
[3] & 0xff) << 24);
1613 case V3D_INTERNAL_TYPE_16F
:
1614 util_pack_color(color
->f
, PIPE_FORMAT_R16G16B16A16_FLOAT
,
1616 memcpy(job
->clear_color
[i
], uc
.ui
, internal_size
);
1618 case V3D_INTERNAL_TYPE_16I
:
1619 case V3D_INTERNAL_TYPE_16UI
:
1620 job
->clear_color
[i
][0] = ((color
->ui
[0] & 0xffff) |
1621 color
->ui
[1] << 16);
1622 job
->clear_color
[i
][1] = ((color
->ui
[2] & 0xffff) |
1623 color
->ui
[3] << 16);
1625 case V3D_INTERNAL_TYPE_32F
:
1626 case V3D_INTERNAL_TYPE_32I
:
1627 case V3D_INTERNAL_TYPE_32UI
:
1628 memcpy(job
->clear_color
[i
], color
->ui
, internal_size
);
1632 rsc
->initialized_buffers
|= bit
;
1635 unsigned zsclear
= buffers
& PIPE_CLEAR_DEPTHSTENCIL
;
1637 struct v3d_resource
*rsc
=
1638 v3d_resource(v3d
->framebuffer
.zsbuf
->texture
);
1640 if (zsclear
& PIPE_CLEAR_DEPTH
)
1641 job
->clear_z
= depth
;
1642 if (zsclear
& PIPE_CLEAR_STENCIL
)
1643 job
->clear_s
= stencil
;
1645 rsc
->initialized_buffers
|= zsclear
;
1648 job
->draw_min_x
= 0;
1649 job
->draw_min_y
= 0;
1650 job
->draw_max_x
= v3d
->framebuffer
.width
;
1651 job
->draw_max_y
= v3d
->framebuffer
.height
;
1652 job
->clear
|= buffers
;
1653 job
->store
|= buffers
;
1655 v3d_start_draw(v3d
);
1661 v3d_clear(struct pipe_context
*pctx
, unsigned buffers
,
1662 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
1664 struct v3d_context
*v3d
= v3d_context(pctx
);
1665 struct v3d_job
*job
= v3d_get_job_for_fbo(v3d
);
1667 buffers
&= ~v3d_tlb_clear(job
, buffers
, color
, depth
, stencil
);
1670 v3d_draw_clear(v3d
, buffers
, color
, depth
, stencil
);
1674 v3d_clear_render_target(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
1675 const union pipe_color_union
*color
,
1676 unsigned x
, unsigned y
, unsigned w
, unsigned h
,
1677 bool render_condition_enabled
)
1679 fprintf(stderr
, "unimpl: clear RT\n");
1683 v3d_clear_depth_stencil(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
1684 unsigned buffers
, double depth
, unsigned stencil
,
1685 unsigned x
, unsigned y
, unsigned w
, unsigned h
,
1686 bool render_condition_enabled
)
1688 fprintf(stderr
, "unimpl: clear DS\n");
1692 v3dX(draw_init
)(struct pipe_context
*pctx
)
1694 pctx
->draw_vbo
= v3d_draw_vbo
;
1695 pctx
->clear
= v3d_clear
;
1696 pctx
->clear_render_target
= v3d_clear_render_target
;
1697 pctx
->clear_depth_stencil
= v3d_clear_depth_stencil
;
1698 #if V3D_VERSION >= 41
1699 if (v3d_context(pctx
)->screen
->has_csd
)
1700 pctx
->launch_grid
= v3d_launch_grid
;