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 /* 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
);
332 v3d_emit_gl_shader_state(struct v3d_context
*v3d
,
333 const struct pipe_draw_info
*info
)
335 struct v3d_job
*job
= v3d
->job
;
336 /* VC5_DIRTY_VTXSTATE */
337 struct v3d_vertex_stateobj
*vtx
= v3d
->vtx
;
338 /* VC5_DIRTY_VTXBUF */
339 struct v3d_vertexbuf_stateobj
*vertexbuf
= &v3d
->vertexbuf
;
341 /* Upload the uniforms to the indirect CL first */
342 struct v3d_cl_reloc fs_uniforms
=
343 v3d_write_uniforms(v3d
, v3d
->prog
.fs
,
344 PIPE_SHADER_FRAGMENT
);
345 struct v3d_cl_reloc vs_uniforms
=
346 v3d_write_uniforms(v3d
, v3d
->prog
.vs
,
348 struct v3d_cl_reloc cs_uniforms
=
349 v3d_write_uniforms(v3d
, v3d
->prog
.cs
,
352 /* See GFXH-930 workaround below */
353 uint32_t num_elements_to_emit
= MAX2(vtx
->num_elements
, 1);
354 uint32_t shader_rec_offset
=
355 v3d_cl_ensure_space(&job
->indirect
,
356 cl_packet_length(GL_SHADER_STATE_RECORD
) +
357 num_elements_to_emit
*
358 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD
),
361 /* XXX perf: We should move most of the SHADER_STATE_RECORD setup to
362 * compile time, so that we mostly just have to OR the VS and FS
363 * records together at draw time.
365 cl_emit(&job
->indirect
, GL_SHADER_STATE_RECORD
, shader
) {
366 shader
.enable_clipping
= true;
367 /* VC5_DIRTY_PRIM_MODE | VC5_DIRTY_RASTERIZER */
368 shader
.point_size_in_shaded_vertex_data
=
369 (info
->mode
== PIPE_PRIM_POINTS
&&
370 v3d
->rasterizer
->base
.point_size_per_vertex
);
372 /* Must be set if the shader modifies Z, discards, or modifies
373 * the sample mask. For any of these cases, the fragment
374 * shader needs to write the Z value (even just discards).
376 shader
.fragment_shader_does_z_writes
=
377 v3d
->prog
.fs
->prog_data
.fs
->writes_z
;
378 /* Set if the EZ test must be disabled (due to shader side
379 * effects and the early_z flag not being present in the
382 shader
.turn_off_early_z_test
=
383 v3d
->prog
.fs
->prog_data
.fs
->disable_ez
;
385 shader
.fragment_shader_uses_real_pixel_centre_w_in_addition_to_centroid_w2
=
386 v3d
->prog
.fs
->prog_data
.fs
->uses_center_w
;
388 #if V3D_VERSION >= 40
389 shader
.do_scoreboard_wait_on_first_thread_switch
=
390 v3d
->prog
.fs
->prog_data
.fs
->lock_scoreboard_on_first_thrsw
;
391 shader
.disable_implicit_point_line_varyings
=
392 !v3d
->prog
.fs
->prog_data
.fs
->uses_implicit_point_line_varyings
;
395 shader
.number_of_varyings_in_fragment_shader
=
396 v3d
->prog
.fs
->prog_data
.fs
->num_inputs
;
398 shader
.coordinate_shader_propagate_nans
= true;
399 shader
.vertex_shader_propagate_nans
= true;
400 shader
.fragment_shader_propagate_nans
= true;
402 shader
.coordinate_shader_code_address
=
403 cl_address(v3d_resource(v3d
->prog
.cs
->resource
)->bo
,
404 v3d
->prog
.cs
->offset
);
405 shader
.vertex_shader_code_address
=
406 cl_address(v3d_resource(v3d
->prog
.vs
->resource
)->bo
,
407 v3d
->prog
.vs
->offset
);
408 shader
.fragment_shader_code_address
=
409 cl_address(v3d_resource(v3d
->prog
.fs
->resource
)->bo
,
410 v3d
->prog
.fs
->offset
);
412 /* XXX: Use combined input/output size flag in the common
415 shader
.coordinate_shader_has_separate_input_and_output_vpm_blocks
=
416 v3d
->prog
.cs
->prog_data
.vs
->separate_segments
;
417 shader
.vertex_shader_has_separate_input_and_output_vpm_blocks
=
418 v3d
->prog
.vs
->prog_data
.vs
->separate_segments
;
420 shader
.coordinate_shader_input_vpm_segment_size
=
421 v3d
->prog
.cs
->prog_data
.vs
->separate_segments
?
422 v3d
->prog
.cs
->prog_data
.vs
->vpm_input_size
: 1;
423 shader
.vertex_shader_input_vpm_segment_size
=
424 v3d
->prog
.vs
->prog_data
.vs
->separate_segments
?
425 v3d
->prog
.vs
->prog_data
.vs
->vpm_input_size
: 1;
427 shader
.coordinate_shader_output_vpm_segment_size
=
428 v3d
->prog
.cs
->prog_data
.vs
->vpm_output_size
;
429 shader
.vertex_shader_output_vpm_segment_size
=
430 v3d
->prog
.vs
->prog_data
.vs
->vpm_output_size
;
432 shader
.coordinate_shader_uniforms_address
= cs_uniforms
;
433 shader
.vertex_shader_uniforms_address
= vs_uniforms
;
434 shader
.fragment_shader_uniforms_address
= fs_uniforms
;
436 #if V3D_VERSION >= 41
437 shader
.min_coord_shader_input_segments_required_in_play
= 1;
438 shader
.min_vertex_shader_input_segments_required_in_play
= 1;
440 shader
.coordinate_shader_4_way_threadable
=
441 v3d
->prog
.cs
->prog_data
.vs
->base
.threads
== 4;
442 shader
.vertex_shader_4_way_threadable
=
443 v3d
->prog
.vs
->prog_data
.vs
->base
.threads
== 4;
444 shader
.fragment_shader_4_way_threadable
=
445 v3d
->prog
.fs
->prog_data
.fs
->base
.threads
== 4;
447 shader
.coordinate_shader_start_in_final_thread_section
=
448 v3d
->prog
.cs
->prog_data
.vs
->base
.single_seg
;
449 shader
.vertex_shader_start_in_final_thread_section
=
450 v3d
->prog
.vs
->prog_data
.vs
->base
.single_seg
;
451 shader
.fragment_shader_start_in_final_thread_section
=
452 v3d
->prog
.fs
->prog_data
.fs
->base
.single_seg
;
454 shader
.coordinate_shader_4_way_threadable
=
455 v3d
->prog
.cs
->prog_data
.vs
->base
.threads
== 4;
456 shader
.coordinate_shader_2_way_threadable
=
457 v3d
->prog
.cs
->prog_data
.vs
->base
.threads
== 2;
458 shader
.vertex_shader_4_way_threadable
=
459 v3d
->prog
.vs
->prog_data
.vs
->base
.threads
== 4;
460 shader
.vertex_shader_2_way_threadable
=
461 v3d
->prog
.vs
->prog_data
.vs
->base
.threads
== 2;
462 shader
.fragment_shader_4_way_threadable
=
463 v3d
->prog
.fs
->prog_data
.fs
->base
.threads
== 4;
464 shader
.fragment_shader_2_way_threadable
=
465 v3d
->prog
.fs
->prog_data
.fs
->base
.threads
== 2;
468 shader
.vertex_id_read_by_coordinate_shader
=
469 v3d
->prog
.cs
->prog_data
.vs
->uses_vid
;
470 shader
.instance_id_read_by_coordinate_shader
=
471 v3d
->prog
.cs
->prog_data
.vs
->uses_iid
;
472 shader
.vertex_id_read_by_vertex_shader
=
473 v3d
->prog
.vs
->prog_data
.vs
->uses_vid
;
474 shader
.instance_id_read_by_vertex_shader
=
475 v3d
->prog
.vs
->prog_data
.vs
->uses_iid
;
477 shader
.address_of_default_attribute_values
=
478 cl_address(v3d_resource(vtx
->defaults
)->bo
,
479 vtx
->defaults_offset
);
482 bool cs_loaded_any
= false;
483 for (int i
= 0; i
< vtx
->num_elements
; i
++) {
484 struct pipe_vertex_element
*elem
= &vtx
->pipe
[i
];
485 struct pipe_vertex_buffer
*vb
=
486 &vertexbuf
->vb
[elem
->vertex_buffer_index
];
487 struct v3d_resource
*rsc
= v3d_resource(vb
->buffer
.resource
);
489 const uint32_t size
=
490 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD
);
491 cl_emit_with_prepacked(&job
->indirect
,
492 GL_SHADER_STATE_ATTRIBUTE_RECORD
,
493 &vtx
->attrs
[i
* size
], attr
) {
494 attr
.stride
= vb
->stride
;
495 attr
.address
= cl_address(rsc
->bo
,
498 attr
.number_of_values_read_by_coordinate_shader
=
499 v3d
->prog
.cs
->prog_data
.vs
->vattr_sizes
[i
];
500 attr
.number_of_values_read_by_vertex_shader
=
501 v3d
->prog
.vs
->prog_data
.vs
->vattr_sizes
[i
];
503 /* GFXH-930: At least one attribute must be enabled
504 * and read by CS and VS. If we have attributes being
505 * consumed by the VS but not the CS, then set up a
506 * dummy load of the last attribute into the CS's VPM
507 * inputs. (Since CS is just dead-code-elimination
508 * compared to VS, we can't have CS loading but not
511 if (v3d
->prog
.cs
->prog_data
.vs
->vattr_sizes
[i
])
512 cs_loaded_any
= true;
513 if (i
== vtx
->num_elements
- 1 && !cs_loaded_any
) {
514 attr
.number_of_values_read_by_coordinate_shader
= 1;
516 #if V3D_VERSION >= 41
517 attr
.maximum_index
= 0xffffff;
520 STATIC_ASSERT(sizeof(vtx
->attrs
) >= V3D_MAX_VS_INPUTS
/ 4 * size
);
523 if (vtx
->num_elements
== 0) {
524 /* GFXH-930: At least one attribute must be enabled and read
525 * by CS and VS. If we have no attributes being consumed by
526 * the shader, set up a dummy to be loaded into the VPM.
528 cl_emit(&job
->indirect
, GL_SHADER_STATE_ATTRIBUTE_RECORD
, attr
) {
529 /* Valid address of data whose value will be unused. */
530 attr
.address
= cl_address(job
->indirect
.bo
, 0);
532 attr
.type
= ATTRIBUTE_FLOAT
;
536 attr
.number_of_values_read_by_coordinate_shader
= 1;
537 attr
.number_of_values_read_by_vertex_shader
= 1;
541 cl_emit(&job
->bcl
, VCM_CACHE_SIZE
, vcm
) {
542 vcm
.number_of_16_vertex_batches_for_binning
=
543 v3d
->prog
.cs
->prog_data
.vs
->vcm_cache_size
;
544 vcm
.number_of_16_vertex_batches_for_rendering
=
545 v3d
->prog
.vs
->prog_data
.vs
->vcm_cache_size
;
548 cl_emit(&job
->bcl
, GL_SHADER_STATE
, state
) {
549 state
.address
= cl_address(job
->indirect
.bo
, shader_rec_offset
);
550 state
.number_of_attribute_arrays
= num_elements_to_emit
;
553 v3d_bo_unreference(&cs_uniforms
.bo
);
554 v3d_bo_unreference(&vs_uniforms
.bo
);
555 v3d_bo_unreference(&fs_uniforms
.bo
);
559 * Updates the number of primitvies generated from the number of vertices
560 * to draw. We do this here instead of using PRIMITIVE_COUNTS_FEEDBACK because
561 * using the GPU packet for this might require sync waits and this is trivial
562 * to handle in the CPU instead.
565 v3d_update_primitives_generated_counter(struct v3d_context
*v3d
,
566 const struct pipe_draw_info
*info
)
568 if (!v3d
->active_queries
)
571 uint32_t prims
= u_prims_for_vertices(info
->mode
, info
->count
);
572 v3d
->prims_generated
+= prims
;
576 v3d_update_job_ez(struct v3d_context
*v3d
, struct v3d_job
*job
)
578 switch (v3d
->zsa
->ez_state
) {
579 case VC5_EZ_UNDECIDED
:
580 /* If the Z/S state didn't pick a direction but didn't
581 * disable, then go along with the current EZ state. This
582 * allows EZ optimization for Z func == EQUAL or NEVER.
588 /* If the Z/S state picked a direction, then it needs to match
589 * the current direction if we've decided on one.
591 if (job
->ez_state
== VC5_EZ_UNDECIDED
)
592 job
->ez_state
= v3d
->zsa
->ez_state
;
593 else if (job
->ez_state
!= v3d
->zsa
->ez_state
)
594 job
->ez_state
= VC5_EZ_DISABLED
;
597 case VC5_EZ_DISABLED
:
598 /* If the current Z/S state disables EZ because of a bad Z
599 * func or stencil operation, then we can't do any more EZ in
602 job
->ez_state
= VC5_EZ_DISABLED
;
606 /* If the FS affects the Z of the pixels, then it may update against
607 * the chosen EZ direction (though we could use
608 * ARB_conservative_depth's hints to avoid this)
610 if (v3d
->prog
.fs
->prog_data
.fs
->writes_z
) {
611 job
->ez_state
= VC5_EZ_DISABLED
;
614 if (job
->first_ez_state
== VC5_EZ_UNDECIDED
&&
615 (job
->ez_state
!= VC5_EZ_DISABLED
|| job
->draw_calls_queued
== 0))
616 job
->first_ez_state
= job
->ez_state
;
620 v3d_draw_vbo(struct pipe_context
*pctx
, const struct pipe_draw_info
*info
)
622 struct v3d_context
*v3d
= v3d_context(pctx
);
624 if (!info
->count_from_stream_output
&& !info
->indirect
&&
625 !info
->primitive_restart
&&
626 !u_trim_pipe_prim(info
->mode
, (unsigned*)&info
->count
))
629 /* Fall back for weird desktop GL primitive restart values. */
630 if (info
->primitive_restart
&&
634 switch (info
->index_size
) {
643 if (info
->restart_index
!= mask
) {
644 util_draw_vbo_without_prim_restart(pctx
, info
);
649 if (info
->mode
>= PIPE_PRIM_QUADS
) {
650 util_primconvert_save_rasterizer_state(v3d
->primconvert
, &v3d
->rasterizer
->base
);
651 util_primconvert_draw_vbo(v3d
->primconvert
, info
);
652 perf_debug("Fallback conversion for %d %s vertices\n",
653 info
->count
, u_prim_name(info
->mode
));
657 /* Before setting up the draw, flush anything writing to the resources
658 * that we read from or reading from resources we write to.
660 for (int s
= 0; s
< PIPE_SHADER_COMPUTE
; s
++)
661 v3d_predraw_check_stage_inputs(pctx
, s
);
663 if (info
->indirect
) {
664 v3d_flush_jobs_writing_resource(v3d
, info
->indirect
->buffer
,
668 v3d_predraw_check_outputs(pctx
);
670 /* If transform feedback is active and we are switching primitive type
671 * we need to submit the job before drawing and update the vertex count
672 * written to TF based on the primitive type since we will need to
673 * know the exact vertex count if the application decides to call
674 * glDrawTransformFeedback() later.
676 if (v3d
->streamout
.num_targets
> 0 &&
677 u_base_prim_type(info
->mode
) != u_base_prim_type(v3d
->prim_mode
)) {
678 v3d_tf_update_counters(v3d
);
681 struct v3d_job
*job
= v3d_get_job_for_fbo(v3d
);
683 /* If vertex texturing depends on the output of rendering, we need to
684 * ensure that that rendering is complete before we run a coordinate
685 * shader that depends on it.
687 * Given that doing that is unusual, for now we just block the binner
688 * on the last submitted render, rather than tracking the last
689 * rendering to each texture's BO.
691 if (v3d
->tex
[PIPE_SHADER_VERTEX
].num_textures
|| info
->indirect
) {
692 perf_debug("Blocking binner on last render "
693 "due to vertex texturing or indirect drawing.\n");
694 job
->submit
.in_sync_bcl
= v3d
->out_sync
;
697 /* Mark SSBOs and images as being written. We don't actually know
698 * which ones are read vs written, so just assume the worst.
700 for (int s
= 0; s
< PIPE_SHADER_COMPUTE
; s
++) {
701 foreach_bit(i
, v3d
->ssbo
[s
].enabled_mask
) {
702 v3d_job_add_write_resource(job
,
703 v3d
->ssbo
[s
].sb
[i
].buffer
);
704 job
->tmu_dirty_rcl
= true;
707 foreach_bit(i
, v3d
->shaderimg
[s
].enabled_mask
) {
708 v3d_job_add_write_resource(job
,
709 v3d
->shaderimg
[s
].si
[i
].base
.resource
);
710 job
->tmu_dirty_rcl
= true;
714 /* Get space to emit our draw call into the BCL, using a branch to
715 * jump to a new BO if necessary.
717 v3d_cl_ensure_space_with_branch(&job
->bcl
, 256 /* XXX */);
719 if (v3d
->prim_mode
!= info
->mode
) {
720 v3d
->prim_mode
= info
->mode
;
721 v3d
->dirty
|= VC5_DIRTY_PRIM_MODE
;
725 v3d_update_compiled_shaders(v3d
, info
->mode
);
726 v3d_update_job_ez(v3d
, job
);
728 /* If this job was writing to transform feedback buffers before this
729 * draw and we are reading from them here, then we need to wait for TF
730 * to complete before we emit this draw.
732 * Notice this check needs to happen before we emit state for the
733 * current draw call, where we update job->tf_enabled, so we can ensure
734 * that we only check TF writes for prior draws.
736 v3d_emit_wait_for_tf_if_needed(v3d
, job
);
738 #if V3D_VERSION >= 41
739 v3d41_emit_state(pctx
);
741 v3d33_emit_state(pctx
);
744 if (v3d
->dirty
& (VC5_DIRTY_VTXBUF
|
746 VC5_DIRTY_PRIM_MODE
|
747 VC5_DIRTY_RASTERIZER
|
748 VC5_DIRTY_COMPILED_CS
|
749 VC5_DIRTY_COMPILED_VS
|
750 VC5_DIRTY_COMPILED_FS
|
751 v3d
->prog
.cs
->uniform_dirty_bits
|
752 v3d
->prog
.vs
->uniform_dirty_bits
|
753 v3d
->prog
.fs
->uniform_dirty_bits
)) {
754 v3d_emit_gl_shader_state(v3d
, info
);
759 /* The Base Vertex/Base Instance packet sets those values to nonzero
760 * for the next draw call only.
762 if (info
->index_bias
|| info
->start_instance
) {
763 cl_emit(&job
->bcl
, BASE_VERTEX_BASE_INSTANCE
, base
) {
764 base
.base_instance
= info
->start_instance
;
765 base
.base_vertex
= info
->index_bias
;
769 uint32_t prim_tf_enable
= 0;
771 /* V3D 3.x: The HW only processes transform feedback on primitives
774 if (v3d
->streamout
.num_targets
)
775 prim_tf_enable
= (V3D_PRIM_POINTS_TF
- V3D_PRIM_POINTS
);
778 v3d_update_primitives_generated_counter(v3d
, info
);
780 /* Note that the primitive type fields match with OpenGL/gallium
781 * definitions, up to but not including QUADS.
783 if (info
->index_size
) {
784 uint32_t index_size
= info
->index_size
;
785 uint32_t offset
= info
->start
* index_size
;
786 struct pipe_resource
*prsc
;
787 if (info
->has_user_indices
) {
789 u_upload_data(v3d
->uploader
, 0,
790 info
->count
* info
->index_size
, 4,
794 prsc
= info
->index
.resource
;
796 struct v3d_resource
*rsc
= v3d_resource(prsc
);
798 #if V3D_VERSION >= 40
799 cl_emit(&job
->bcl
, INDEX_BUFFER_SETUP
, ib
) {
800 ib
.address
= cl_address(rsc
->bo
, 0);
801 ib
.size
= rsc
->bo
->size
;
805 if (info
->indirect
) {
806 cl_emit(&job
->bcl
, INDIRECT_INDEXED_INSTANCED_PRIM_LIST
, prim
) {
807 prim
.index_type
= ffs(info
->index_size
) - 1;
809 prim
.address_of_indices_list
=
810 cl_address(rsc
->bo
, offset
);
811 #endif /* V3D_VERSION < 40 */
812 prim
.mode
= info
->mode
| prim_tf_enable
;
813 prim
.enable_primitive_restarts
= info
->primitive_restart
;
815 prim
.number_of_draw_indirect_indexed_records
= info
->indirect
->draw_count
;
817 prim
.stride_in_multiples_of_4_bytes
= info
->indirect
->stride
>> 2;
818 prim
.address
= cl_address(v3d_resource(info
->indirect
->buffer
)->bo
,
819 info
->indirect
->offset
);
821 } else if (info
->instance_count
> 1) {
822 cl_emit(&job
->bcl
, INDEXED_INSTANCED_PRIM_LIST
, prim
) {
823 prim
.index_type
= ffs(info
->index_size
) - 1;
824 #if V3D_VERSION >= 40
825 prim
.index_offset
= offset
;
826 #else /* V3D_VERSION < 40 */
827 prim
.maximum_index
= (1u << 31) - 1; /* XXX */
828 prim
.address_of_indices_list
=
829 cl_address(rsc
->bo
, offset
);
830 #endif /* V3D_VERSION < 40 */
831 prim
.mode
= info
->mode
| prim_tf_enable
;
832 prim
.enable_primitive_restarts
= info
->primitive_restart
;
834 prim
.number_of_instances
= info
->instance_count
;
835 prim
.instance_length
= info
->count
;
838 cl_emit(&job
->bcl
, INDEXED_PRIM_LIST
, prim
) {
839 prim
.index_type
= ffs(info
->index_size
) - 1;
840 prim
.length
= info
->count
;
841 #if V3D_VERSION >= 40
842 prim
.index_offset
= offset
;
843 #else /* V3D_VERSION < 40 */
844 prim
.maximum_index
= (1u << 31) - 1; /* XXX */
845 prim
.address_of_indices_list
=
846 cl_address(rsc
->bo
, offset
);
847 #endif /* V3D_VERSION < 40 */
848 prim
.mode
= info
->mode
| prim_tf_enable
;
849 prim
.enable_primitive_restarts
= info
->primitive_restart
;
853 if (info
->has_user_indices
)
854 pipe_resource_reference(&prsc
, NULL
);
856 if (info
->indirect
) {
857 cl_emit(&job
->bcl
, INDIRECT_VERTEX_ARRAY_INSTANCED_PRIMS
, prim
) {
858 prim
.mode
= info
->mode
| prim_tf_enable
;
859 prim
.number_of_draw_indirect_array_records
= info
->indirect
->draw_count
;
861 prim
.stride_in_multiples_of_4_bytes
= info
->indirect
->stride
>> 2;
862 prim
.address
= cl_address(v3d_resource(info
->indirect
->buffer
)->bo
,
863 info
->indirect
->offset
);
865 } else if (info
->instance_count
> 1) {
866 struct pipe_stream_output_target
*so
=
867 info
->count_from_stream_output
;
868 uint32_t vert_count
= so
?
869 v3d_stream_output_target_get_vertex_count(so
) :
871 cl_emit(&job
->bcl
, VERTEX_ARRAY_INSTANCED_PRIMS
, prim
) {
872 prim
.mode
= info
->mode
| prim_tf_enable
;
873 prim
.index_of_first_vertex
= info
->start
;
874 prim
.number_of_instances
= info
->instance_count
;
875 prim
.instance_length
= vert_count
;
878 struct pipe_stream_output_target
*so
=
879 info
->count_from_stream_output
;
880 uint32_t vert_count
= so
?
881 v3d_stream_output_target_get_vertex_count(so
) :
883 cl_emit(&job
->bcl
, VERTEX_ARRAY_PRIMS
, prim
) {
884 prim
.mode
= info
->mode
| prim_tf_enable
;
885 prim
.length
= vert_count
;
886 prim
.index_of_first_vertex
= info
->start
;
891 /* A flush is required in between a TF draw and any following TF specs
892 * packet, or the GPU may hang. Just flush each time for now.
894 if (v3d
->streamout
.num_targets
)
895 cl_emit(&job
->bcl
, TRANSFORM_FEEDBACK_FLUSH_AND_COUNT
, flush
);
897 job
->draw_calls_queued
++;
898 if (v3d
->streamout
.num_targets
)
899 job
->tf_draw_calls_queued
++;
901 /* Increment the TF offsets by how many verts we wrote. XXX: This
902 * needs some clamping to the buffer size.
904 for (int i
= 0; i
< v3d
->streamout
.num_targets
; i
++)
905 v3d
->streamout
.offsets
[i
] += info
->count
;
907 if (v3d
->zsa
&& job
->zsbuf
&& v3d
->zsa
->base
.depth
.enabled
) {
908 struct v3d_resource
*rsc
= v3d_resource(job
->zsbuf
->texture
);
909 v3d_job_add_bo(job
, rsc
->bo
);
911 job
->load
|= PIPE_CLEAR_DEPTH
& ~job
->clear
;
912 if (v3d
->zsa
->base
.depth
.writemask
)
913 job
->store
|= PIPE_CLEAR_DEPTH
;
914 rsc
->initialized_buffers
= PIPE_CLEAR_DEPTH
;
917 if (v3d
->zsa
&& job
->zsbuf
&& v3d
->zsa
->base
.stencil
[0].enabled
) {
918 struct v3d_resource
*rsc
= v3d_resource(job
->zsbuf
->texture
);
919 if (rsc
->separate_stencil
)
920 rsc
= rsc
->separate_stencil
;
922 v3d_job_add_bo(job
, rsc
->bo
);
924 job
->load
|= PIPE_CLEAR_STENCIL
& ~job
->clear
;
925 if (v3d
->zsa
->base
.stencil
[0].writemask
||
926 v3d
->zsa
->base
.stencil
[1].writemask
) {
927 job
->store
|= PIPE_CLEAR_STENCIL
;
929 rsc
->initialized_buffers
|= PIPE_CLEAR_STENCIL
;
932 for (int i
= 0; i
< V3D_MAX_DRAW_BUFFERS
; i
++) {
933 uint32_t bit
= PIPE_CLEAR_COLOR0
<< i
;
934 int blend_rt
= v3d
->blend
->base
.independent_blend_enable
? i
: 0;
936 if (job
->store
& bit
|| !job
->cbufs
[i
])
938 struct v3d_resource
*rsc
= v3d_resource(job
->cbufs
[i
]->texture
);
940 job
->load
|= bit
& ~job
->clear
;
941 if (v3d
->blend
->base
.rt
[blend_rt
].colormask
)
943 v3d_job_add_bo(job
, rsc
->bo
);
946 if (job
->referenced_size
> 768 * 1024 * 1024) {
947 perf_debug("Flushing job with %dkb to try to free up memory\n",
948 job
->referenced_size
/ 1024);
952 if (V3D_DEBUG
& V3D_DEBUG_ALWAYS_FLUSH
)
957 * Implements gallium's clear() hook (glClear()) by drawing a pair of triangles.
960 v3d_draw_clear(struct v3d_context
*v3d
,
962 const union pipe_color_union
*color
,
963 double depth
, unsigned stencil
)
965 static const union pipe_color_union dummy_color
= {};
967 /* The blitter util dereferences the color regardless, even though the
968 * gallium clear API may not pass one in when only Z/S are cleared.
971 color
= &dummy_color
;
973 v3d_blitter_save(v3d
);
974 util_blitter_clear(v3d
->blitter
,
975 v3d
->framebuffer
.width
,
976 v3d
->framebuffer
.height
,
977 util_framebuffer_get_num_layers(&v3d
->framebuffer
),
978 buffers
, color
, depth
, stencil
,
979 util_framebuffer_get_num_samples(&v3d
->framebuffer
) > 1);
983 * Attempts to perform the GL clear by using the TLB's fast clear at the start
987 v3d_tlb_clear(struct v3d_job
*job
, unsigned buffers
,
988 const union pipe_color_union
*color
,
989 double depth
, unsigned stencil
)
991 struct v3d_context
*v3d
= job
->v3d
;
993 if (job
->draw_calls_queued
) {
994 /* If anything in the CL has drawn using the buffer, then the
995 * TLB clear we're trying to add now would happen before that
998 buffers
&= ~(job
->load
| job
->store
);
1001 /* GFXH-1461: If we were to emit a load of just depth or just stencil,
1002 * then the clear for the other may get lost. We need to decide now
1003 * if it would be possible to need to emit a load of just one after
1004 * we've set up our TLB clears.
1006 if (buffers
& PIPE_CLEAR_DEPTHSTENCIL
&&
1007 (buffers
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
&&
1009 util_format_is_depth_and_stencil(job
->zsbuf
->texture
->format
)) {
1010 buffers
&= ~PIPE_CLEAR_DEPTHSTENCIL
;
1013 for (int i
= 0; i
< V3D_MAX_DRAW_BUFFERS
; i
++) {
1014 uint32_t bit
= PIPE_CLEAR_COLOR0
<< i
;
1015 if (!(buffers
& bit
))
1018 struct pipe_surface
*psurf
= v3d
->framebuffer
.cbufs
[i
];
1019 struct v3d_surface
*surf
= v3d_surface(psurf
);
1020 struct v3d_resource
*rsc
= v3d_resource(psurf
->texture
);
1022 union util_color uc
;
1023 uint32_t internal_size
= 4 << surf
->internal_bpp
;
1025 static union pipe_color_union swapped_color
;
1026 if (v3d
->swap_color_rb
& (1 << i
)) {
1027 swapped_color
.f
[0] = color
->f
[2];
1028 swapped_color
.f
[1] = color
->f
[1];
1029 swapped_color
.f
[2] = color
->f
[0];
1030 swapped_color
.f
[3] = color
->f
[3];
1031 color
= &swapped_color
;
1034 switch (surf
->internal_type
) {
1035 case V3D_INTERNAL_TYPE_8
:
1036 util_pack_color(color
->f
, PIPE_FORMAT_R8G8B8A8_UNORM
,
1038 memcpy(job
->clear_color
[i
], uc
.ui
, internal_size
);
1040 case V3D_INTERNAL_TYPE_8I
:
1041 case V3D_INTERNAL_TYPE_8UI
:
1042 job
->clear_color
[i
][0] = ((color
->ui
[0] & 0xff) |
1043 (color
->ui
[1] & 0xff) << 8 |
1044 (color
->ui
[2] & 0xff) << 16 |
1045 (color
->ui
[3] & 0xff) << 24);
1047 case V3D_INTERNAL_TYPE_16F
:
1048 util_pack_color(color
->f
, PIPE_FORMAT_R16G16B16A16_FLOAT
,
1050 memcpy(job
->clear_color
[i
], uc
.ui
, internal_size
);
1052 case V3D_INTERNAL_TYPE_16I
:
1053 case V3D_INTERNAL_TYPE_16UI
:
1054 job
->clear_color
[i
][0] = ((color
->ui
[0] & 0xffff) |
1055 color
->ui
[1] << 16);
1056 job
->clear_color
[i
][1] = ((color
->ui
[2] & 0xffff) |
1057 color
->ui
[3] << 16);
1059 case V3D_INTERNAL_TYPE_32F
:
1060 case V3D_INTERNAL_TYPE_32I
:
1061 case V3D_INTERNAL_TYPE_32UI
:
1062 memcpy(job
->clear_color
[i
], color
->ui
, internal_size
);
1066 rsc
->initialized_buffers
|= bit
;
1069 unsigned zsclear
= buffers
& PIPE_CLEAR_DEPTHSTENCIL
;
1071 struct v3d_resource
*rsc
=
1072 v3d_resource(v3d
->framebuffer
.zsbuf
->texture
);
1074 if (zsclear
& PIPE_CLEAR_DEPTH
)
1075 job
->clear_z
= depth
;
1076 if (zsclear
& PIPE_CLEAR_STENCIL
)
1077 job
->clear_s
= stencil
;
1079 rsc
->initialized_buffers
|= zsclear
;
1082 job
->draw_min_x
= 0;
1083 job
->draw_min_y
= 0;
1084 job
->draw_max_x
= v3d
->framebuffer
.width
;
1085 job
->draw_max_y
= v3d
->framebuffer
.height
;
1086 job
->clear
|= buffers
;
1087 job
->store
|= buffers
;
1089 v3d_start_draw(v3d
);
1095 v3d_clear(struct pipe_context
*pctx
, unsigned buffers
,
1096 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
1098 struct v3d_context
*v3d
= v3d_context(pctx
);
1099 struct v3d_job
*job
= v3d_get_job_for_fbo(v3d
);
1101 buffers
&= ~v3d_tlb_clear(job
, buffers
, color
, depth
, stencil
);
1104 v3d_draw_clear(v3d
, buffers
, color
, depth
, stencil
);
1108 v3d_clear_render_target(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
1109 const union pipe_color_union
*color
,
1110 unsigned x
, unsigned y
, unsigned w
, unsigned h
,
1111 bool render_condition_enabled
)
1113 fprintf(stderr
, "unimpl: clear RT\n");
1117 v3d_clear_depth_stencil(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
1118 unsigned buffers
, double depth
, unsigned stencil
,
1119 unsigned x
, unsigned y
, unsigned w
, unsigned h
,
1120 bool render_condition_enabled
)
1122 fprintf(stderr
, "unimpl: clear DS\n");
1126 v3dX(draw_init
)(struct pipe_context
*pctx
)
1128 pctx
->draw_vbo
= v3d_draw_vbo
;
1129 pctx
->clear
= v3d_clear
;
1130 pctx
->clear_render_target
= v3d_clear_render_target
;
1131 pctx
->clear_depth_stencil
= v3d_clear_depth_stencil
;