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 /* XXX perf: If we're reading from the output of TF in this job, we
149 * should instead be using the wait for transform feedback
153 /* Flush writes to textures we're sampling. */
154 for (int i
= 0; i
< v3d
->tex
[s
].num_textures
; i
++) {
155 struct pipe_sampler_view
*pview
= v3d
->tex
[s
].textures
[i
];
158 struct v3d_sampler_view
*view
= v3d_sampler_view(pview
);
160 if (view
->texture
!= view
->base
.texture
&&
161 view
->base
.format
!= PIPE_FORMAT_X32_S8X24_UINT
)
162 v3d_update_shadow_texture(pctx
, &view
->base
);
164 v3d_flush_jobs_writing_resource(v3d
, view
->texture
);
167 /* Flush writes to UBOs. */
168 foreach_bit(i
, v3d
->constbuf
[s
].enabled_mask
) {
169 struct pipe_constant_buffer
*cb
= &v3d
->constbuf
[s
].cb
[i
];
171 v3d_flush_jobs_writing_resource(v3d
, cb
->buffer
);
174 /* Flush writes to our image views */
175 foreach_bit(i
, v3d
->shaderimg
[s
].enabled_mask
) {
176 struct v3d_image_view
*view
= &v3d
->shaderimg
[s
].si
[i
];
178 v3d_flush_jobs_writing_resource(v3d
, view
->base
.resource
);
183 v3d_emit_gl_shader_state(struct v3d_context
*v3d
,
184 const struct pipe_draw_info
*info
)
186 struct v3d_job
*job
= v3d
->job
;
187 /* VC5_DIRTY_VTXSTATE */
188 struct v3d_vertex_stateobj
*vtx
= v3d
->vtx
;
189 /* VC5_DIRTY_VTXBUF */
190 struct v3d_vertexbuf_stateobj
*vertexbuf
= &v3d
->vertexbuf
;
192 /* Upload the uniforms to the indirect CL first */
193 struct v3d_cl_reloc fs_uniforms
=
194 v3d_write_uniforms(v3d
, v3d
->prog
.fs
,
195 PIPE_SHADER_FRAGMENT
);
196 struct v3d_cl_reloc vs_uniforms
=
197 v3d_write_uniforms(v3d
, v3d
->prog
.vs
,
199 struct v3d_cl_reloc cs_uniforms
=
200 v3d_write_uniforms(v3d
, v3d
->prog
.cs
,
203 /* See GFXH-930 workaround below */
204 uint32_t num_elements_to_emit
= MAX2(vtx
->num_elements
, 1);
205 uint32_t shader_rec_offset
=
206 v3d_cl_ensure_space(&job
->indirect
,
207 cl_packet_length(GL_SHADER_STATE_RECORD
) +
208 num_elements_to_emit
*
209 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD
),
212 /* XXX perf: We should move most of the SHADER_STATE_RECORD setup to
213 * compile time, so that we mostly just have to OR the VS and FS
214 * records together at draw time.
216 cl_emit(&job
->indirect
, GL_SHADER_STATE_RECORD
, shader
) {
217 shader
.enable_clipping
= true;
218 /* VC5_DIRTY_PRIM_MODE | VC5_DIRTY_RASTERIZER */
219 shader
.point_size_in_shaded_vertex_data
=
220 (info
->mode
== PIPE_PRIM_POINTS
&&
221 v3d
->rasterizer
->base
.point_size_per_vertex
);
223 /* Must be set if the shader modifies Z, discards, or modifies
224 * the sample mask. For any of these cases, the fragment
225 * shader needs to write the Z value (even just discards).
227 shader
.fragment_shader_does_z_writes
=
228 v3d
->prog
.fs
->prog_data
.fs
->writes_z
;
229 /* Set if the EZ test must be disabled (due to shader side
230 * effects and the early_z flag not being present in the
233 shader
.turn_off_early_z_test
=
234 v3d
->prog
.fs
->prog_data
.fs
->disable_ez
;
236 shader
.fragment_shader_uses_real_pixel_centre_w_in_addition_to_centroid_w2
=
237 v3d
->prog
.fs
->prog_data
.fs
->uses_center_w
;
239 shader
.number_of_varyings_in_fragment_shader
=
240 v3d
->prog
.fs
->prog_data
.fs
->num_inputs
;
242 shader
.coordinate_shader_propagate_nans
= true;
243 shader
.vertex_shader_propagate_nans
= true;
244 shader
.fragment_shader_propagate_nans
= true;
246 shader
.coordinate_shader_code_address
=
247 cl_address(v3d_resource(v3d
->prog
.cs
->resource
)->bo
,
248 v3d
->prog
.cs
->offset
);
249 shader
.vertex_shader_code_address
=
250 cl_address(v3d_resource(v3d
->prog
.vs
->resource
)->bo
,
251 v3d
->prog
.vs
->offset
);
252 shader
.fragment_shader_code_address
=
253 cl_address(v3d_resource(v3d
->prog
.fs
->resource
)->bo
,
254 v3d
->prog
.fs
->offset
);
256 /* XXX: Use combined input/output size flag in the common
259 shader
.coordinate_shader_has_separate_input_and_output_vpm_blocks
=
260 v3d
->prog
.cs
->prog_data
.vs
->separate_segments
;
261 shader
.vertex_shader_has_separate_input_and_output_vpm_blocks
=
262 v3d
->prog
.vs
->prog_data
.vs
->separate_segments
;
264 shader
.coordinate_shader_input_vpm_segment_size
=
265 v3d
->prog
.cs
->prog_data
.vs
->separate_segments
?
266 v3d
->prog
.cs
->prog_data
.vs
->vpm_input_size
: 1;
267 shader
.vertex_shader_input_vpm_segment_size
=
268 v3d
->prog
.vs
->prog_data
.vs
->separate_segments
?
269 v3d
->prog
.vs
->prog_data
.vs
->vpm_input_size
: 1;
271 shader
.coordinate_shader_output_vpm_segment_size
=
272 v3d
->prog
.cs
->prog_data
.vs
->vpm_output_size
;
273 shader
.vertex_shader_output_vpm_segment_size
=
274 v3d
->prog
.vs
->prog_data
.vs
->vpm_output_size
;
276 shader
.coordinate_shader_uniforms_address
= cs_uniforms
;
277 shader
.vertex_shader_uniforms_address
= vs_uniforms
;
278 shader
.fragment_shader_uniforms_address
= fs_uniforms
;
280 #if V3D_VERSION >= 41
281 shader
.min_coord_shader_input_segments_required_in_play
= 1;
282 shader
.min_vertex_shader_input_segments_required_in_play
= 1;
284 shader
.coordinate_shader_4_way_threadable
=
285 v3d
->prog
.cs
->prog_data
.vs
->base
.threads
== 4;
286 shader
.vertex_shader_4_way_threadable
=
287 v3d
->prog
.vs
->prog_data
.vs
->base
.threads
== 4;
288 shader
.fragment_shader_4_way_threadable
=
289 v3d
->prog
.fs
->prog_data
.fs
->base
.threads
== 4;
291 shader
.coordinate_shader_start_in_final_thread_section
=
292 v3d
->prog
.cs
->prog_data
.vs
->base
.single_seg
;
293 shader
.vertex_shader_start_in_final_thread_section
=
294 v3d
->prog
.vs
->prog_data
.vs
->base
.single_seg
;
295 shader
.fragment_shader_start_in_final_thread_section
=
296 v3d
->prog
.fs
->prog_data
.fs
->base
.single_seg
;
298 shader
.coordinate_shader_4_way_threadable
=
299 v3d
->prog
.cs
->prog_data
.vs
->base
.threads
== 4;
300 shader
.coordinate_shader_2_way_threadable
=
301 v3d
->prog
.cs
->prog_data
.vs
->base
.threads
== 2;
302 shader
.vertex_shader_4_way_threadable
=
303 v3d
->prog
.vs
->prog_data
.vs
->base
.threads
== 4;
304 shader
.vertex_shader_2_way_threadable
=
305 v3d
->prog
.vs
->prog_data
.vs
->base
.threads
== 2;
306 shader
.fragment_shader_4_way_threadable
=
307 v3d
->prog
.fs
->prog_data
.fs
->base
.threads
== 4;
308 shader
.fragment_shader_2_way_threadable
=
309 v3d
->prog
.fs
->prog_data
.fs
->base
.threads
== 2;
312 shader
.vertex_id_read_by_coordinate_shader
=
313 v3d
->prog
.cs
->prog_data
.vs
->uses_vid
;
314 shader
.instance_id_read_by_coordinate_shader
=
315 v3d
->prog
.cs
->prog_data
.vs
->uses_iid
;
316 shader
.vertex_id_read_by_vertex_shader
=
317 v3d
->prog
.vs
->prog_data
.vs
->uses_vid
;
318 shader
.instance_id_read_by_vertex_shader
=
319 v3d
->prog
.vs
->prog_data
.vs
->uses_iid
;
321 shader
.address_of_default_attribute_values
=
322 cl_address(v3d_resource(vtx
->defaults
)->bo
,
323 vtx
->defaults_offset
);
326 bool cs_loaded_any
= false;
327 for (int i
= 0; i
< vtx
->num_elements
; i
++) {
328 struct pipe_vertex_element
*elem
= &vtx
->pipe
[i
];
329 struct pipe_vertex_buffer
*vb
=
330 &vertexbuf
->vb
[elem
->vertex_buffer_index
];
331 struct v3d_resource
*rsc
= v3d_resource(vb
->buffer
.resource
);
333 const uint32_t size
=
334 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD
);
335 cl_emit_with_prepacked(&job
->indirect
,
336 GL_SHADER_STATE_ATTRIBUTE_RECORD
,
337 &vtx
->attrs
[i
* size
], attr
) {
338 attr
.stride
= vb
->stride
;
339 attr
.address
= cl_address(rsc
->bo
,
342 attr
.number_of_values_read_by_coordinate_shader
=
343 v3d
->prog
.cs
->prog_data
.vs
->vattr_sizes
[i
];
344 attr
.number_of_values_read_by_vertex_shader
=
345 v3d
->prog
.vs
->prog_data
.vs
->vattr_sizes
[i
];
347 /* GFXH-930: At least one attribute must be enabled
348 * and read by CS and VS. If we have attributes being
349 * consumed by the VS but not the CS, then set up a
350 * dummy load of the last attribute into the CS's VPM
351 * inputs. (Since CS is just dead-code-elimination
352 * compared to VS, we can't have CS loading but not
355 if (v3d
->prog
.cs
->prog_data
.vs
->vattr_sizes
[i
])
356 cs_loaded_any
= true;
357 if (i
== vtx
->num_elements
- 1 && !cs_loaded_any
) {
358 attr
.number_of_values_read_by_coordinate_shader
= 1;
360 #if V3D_VERSION >= 41
361 attr
.maximum_index
= 0xffffff;
364 STATIC_ASSERT(sizeof(vtx
->attrs
) >= V3D_MAX_VS_INPUTS
/ 4 * size
);
367 if (vtx
->num_elements
== 0) {
368 /* GFXH-930: At least one attribute must be enabled and read
369 * by CS and VS. If we have no attributes being consumed by
370 * the shader, set up a dummy to be loaded into the VPM.
372 cl_emit(&job
->indirect
, GL_SHADER_STATE_ATTRIBUTE_RECORD
, attr
) {
373 /* Valid address of data whose value will be unused. */
374 attr
.address
= cl_address(job
->indirect
.bo
, 0);
376 attr
.type
= ATTRIBUTE_FLOAT
;
380 attr
.number_of_values_read_by_coordinate_shader
= 1;
381 attr
.number_of_values_read_by_vertex_shader
= 1;
385 cl_emit(&job
->bcl
, VCM_CACHE_SIZE
, vcm
) {
386 vcm
.number_of_16_vertex_batches_for_binning
=
387 v3d
->prog
.cs
->prog_data
.vs
->vcm_cache_size
;
388 vcm
.number_of_16_vertex_batches_for_rendering
=
389 v3d
->prog
.vs
->prog_data
.vs
->vcm_cache_size
;
392 cl_emit(&job
->bcl
, GL_SHADER_STATE
, state
) {
393 state
.address
= cl_address(job
->indirect
.bo
, shader_rec_offset
);
394 state
.number_of_attribute_arrays
= num_elements_to_emit
;
397 v3d_bo_unreference(&cs_uniforms
.bo
);
398 v3d_bo_unreference(&vs_uniforms
.bo
);
399 v3d_bo_unreference(&fs_uniforms
.bo
);
401 job
->shader_rec_count
++;
405 * Computes the various transform feedback statistics, since they can't be
406 * recorded by CL packets.
409 v3d_tf_statistics_record(struct v3d_context
*v3d
,
410 const struct pipe_draw_info
*info
,
413 if (!v3d
->active_queries
)
416 uint32_t prims
= u_prims_for_vertices(info
->mode
, info
->count
);
417 v3d
->prims_generated
+= prims
;
420 /* XXX: Only count if we didn't overflow. */
421 v3d
->tf_prims_generated
+= prims
;
426 v3d_update_job_ez(struct v3d_context
*v3d
, struct v3d_job
*job
)
428 switch (v3d
->zsa
->ez_state
) {
429 case VC5_EZ_UNDECIDED
:
430 /* If the Z/S state didn't pick a direction but didn't
431 * disable, then go along with the current EZ state. This
432 * allows EZ optimization for Z func == EQUAL or NEVER.
438 /* If the Z/S state picked a direction, then it needs to match
439 * the current direction if we've decided on one.
441 if (job
->ez_state
== VC5_EZ_UNDECIDED
)
442 job
->ez_state
= v3d
->zsa
->ez_state
;
443 else if (job
->ez_state
!= v3d
->zsa
->ez_state
)
444 job
->ez_state
= VC5_EZ_DISABLED
;
447 case VC5_EZ_DISABLED
:
448 /* If the current Z/S state disables EZ because of a bad Z
449 * func or stencil operation, then we can't do any more EZ in
452 job
->ez_state
= VC5_EZ_DISABLED
;
456 /* If the FS affects the Z of the pixels, then it may update against
457 * the chosen EZ direction (though we could use
458 * ARB_conservative_depth's hints to avoid this)
460 if (v3d
->prog
.fs
->prog_data
.fs
->writes_z
) {
461 job
->ez_state
= VC5_EZ_DISABLED
;
464 if (job
->first_ez_state
== VC5_EZ_UNDECIDED
&&
465 (job
->ez_state
!= VC5_EZ_DISABLED
|| job
->draw_calls_queued
== 0))
466 job
->first_ez_state
= job
->ez_state
;
470 v3d_draw_vbo(struct pipe_context
*pctx
, const struct pipe_draw_info
*info
)
472 struct v3d_context
*v3d
= v3d_context(pctx
);
474 if (!info
->count_from_stream_output
&& !info
->indirect
&&
475 !info
->primitive_restart
&&
476 !u_trim_pipe_prim(info
->mode
, (unsigned*)&info
->count
))
479 /* Fall back for weird desktop GL primitive restart values. */
480 if (info
->primitive_restart
&&
484 switch (info
->index_size
) {
493 if (info
->restart_index
!= mask
) {
494 util_draw_vbo_without_prim_restart(pctx
, info
);
499 if (info
->mode
>= PIPE_PRIM_QUADS
) {
500 util_primconvert_save_rasterizer_state(v3d
->primconvert
, &v3d
->rasterizer
->base
);
501 util_primconvert_draw_vbo(v3d
->primconvert
, info
);
502 perf_debug("Fallback conversion for %d %s vertices\n",
503 info
->count
, u_prim_name(info
->mode
));
507 /* Before setting up the draw, flush anything writing to the textures
510 for (int s
= 0; s
< PIPE_SHADER_COMPUTE
; s
++)
511 v3d_predraw_check_stage_inputs(pctx
, s
);
514 v3d_flush_jobs_writing_resource(v3d
, info
->indirect
->buffer
);
516 struct v3d_job
*job
= v3d_get_job_for_fbo(v3d
);
518 /* If vertex texturing depends on the output of rendering, we need to
519 * ensure that that rendering is complete before we run a coordinate
520 * shader that depends on it.
522 * Given that doing that is unusual, for now we just block the binner
523 * on the last submitted render, rather than tracking the last
524 * rendering to each texture's BO.
526 if (v3d
->tex
[PIPE_SHADER_VERTEX
].num_textures
|| info
->indirect
) {
527 perf_debug("Blocking binner on last render "
528 "due to vertex texturing or indirect drawing.\n");
529 job
->submit
.in_sync_bcl
= v3d
->out_sync
;
532 /* Mark SSBOs as being written. We don't actually know which ones are
533 * read vs written, so just assume the worst
535 for (int s
= 0; s
< PIPE_SHADER_COMPUTE
; s
++) {
536 foreach_bit(i
, v3d
->ssbo
[s
].enabled_mask
) {
537 v3d_job_add_write_resource(job
,
538 v3d
->ssbo
[s
].sb
[i
].buffer
);
539 job
->tmu_dirty_rcl
= true;
542 foreach_bit(i
, v3d
->shaderimg
[s
].enabled_mask
) {
543 v3d_job_add_write_resource(job
,
544 v3d
->shaderimg
[s
].si
[i
].base
.resource
);
545 job
->tmu_dirty_rcl
= true;
549 /* Get space to emit our draw call into the BCL, using a branch to
550 * jump to a new BO if necessary.
552 v3d_cl_ensure_space_with_branch(&job
->bcl
, 256 /* XXX */);
554 if (v3d
->prim_mode
!= info
->mode
) {
555 v3d
->prim_mode
= info
->mode
;
556 v3d
->dirty
|= VC5_DIRTY_PRIM_MODE
;
560 v3d_update_compiled_shaders(v3d
, info
->mode
);
561 v3d_update_job_ez(v3d
, job
);
563 #if V3D_VERSION >= 41
564 v3d41_emit_state(pctx
);
566 v3d33_emit_state(pctx
);
569 if (v3d
->dirty
& (VC5_DIRTY_VTXBUF
|
571 VC5_DIRTY_PRIM_MODE
|
572 VC5_DIRTY_RASTERIZER
|
573 VC5_DIRTY_COMPILED_CS
|
574 VC5_DIRTY_COMPILED_VS
|
575 VC5_DIRTY_COMPILED_FS
|
576 v3d
->prog
.cs
->uniform_dirty_bits
|
577 v3d
->prog
.vs
->uniform_dirty_bits
|
578 v3d
->prog
.fs
->uniform_dirty_bits
)) {
579 v3d_emit_gl_shader_state(v3d
, info
);
584 /* The Base Vertex/Base Instance packet sets those values to nonzero
585 * for the next draw call only.
587 if (info
->index_bias
|| info
->start_instance
) {
588 cl_emit(&job
->bcl
, BASE_VERTEX_BASE_INSTANCE
, base
) {
589 base
.base_instance
= info
->start_instance
;
590 base
.base_vertex
= info
->index_bias
;
594 uint32_t prim_tf_enable
= 0;
596 /* V3D 3.x: The HW only processes transform feedback on primitives
599 if (v3d
->streamout
.num_targets
)
600 prim_tf_enable
= (V3D_PRIM_POINTS_TF
- V3D_PRIM_POINTS
);
603 v3d_tf_statistics_record(v3d
, info
, v3d
->streamout
.num_targets
);
605 /* Note that the primitive type fields match with OpenGL/gallium
606 * definitions, up to but not including QUADS.
608 if (info
->index_size
) {
609 uint32_t index_size
= info
->index_size
;
610 uint32_t offset
= info
->start
* index_size
;
611 struct pipe_resource
*prsc
;
612 if (info
->has_user_indices
) {
614 u_upload_data(v3d
->uploader
, 0,
615 info
->count
* info
->index_size
, 4,
619 prsc
= info
->index
.resource
;
621 struct v3d_resource
*rsc
= v3d_resource(prsc
);
623 #if V3D_VERSION >= 40
624 cl_emit(&job
->bcl
, INDEX_BUFFER_SETUP
, ib
) {
625 ib
.address
= cl_address(rsc
->bo
, 0);
626 ib
.size
= rsc
->bo
->size
;
630 if (info
->indirect
) {
631 cl_emit(&job
->bcl
, INDIRECT_INDEXED_INSTANCED_PRIM_LIST
, prim
) {
632 prim
.index_type
= ffs(info
->index_size
) - 1;
634 prim
.address_of_indices_list
=
635 cl_address(rsc
->bo
, offset
);
636 #endif /* V3D_VERSION < 40 */
637 prim
.mode
= info
->mode
| prim_tf_enable
;
638 prim
.enable_primitive_restarts
= info
->primitive_restart
;
640 prim
.number_of_draw_indirect_indexed_records
= info
->indirect
->draw_count
;
642 prim
.stride_in_multiples_of_4_bytes
= info
->indirect
->stride
>> 2;
643 prim
.address
= cl_address(v3d_resource(info
->indirect
->buffer
)->bo
,
644 info
->indirect
->offset
);
646 } else if (info
->instance_count
> 1) {
647 cl_emit(&job
->bcl
, INDEXED_INSTANCED_PRIM_LIST
, prim
) {
648 prim
.index_type
= ffs(info
->index_size
) - 1;
649 #if V3D_VERSION >= 40
650 prim
.index_offset
= offset
;
651 #else /* V3D_VERSION < 40 */
652 prim
.maximum_index
= (1u << 31) - 1; /* XXX */
653 prim
.address_of_indices_list
=
654 cl_address(rsc
->bo
, offset
);
655 #endif /* V3D_VERSION < 40 */
656 prim
.mode
= info
->mode
| prim_tf_enable
;
657 prim
.enable_primitive_restarts
= info
->primitive_restart
;
659 prim
.number_of_instances
= info
->instance_count
;
660 prim
.instance_length
= info
->count
;
663 cl_emit(&job
->bcl
, INDEXED_PRIM_LIST
, prim
) {
664 prim
.index_type
= ffs(info
->index_size
) - 1;
665 prim
.length
= info
->count
;
666 #if V3D_VERSION >= 40
667 prim
.index_offset
= offset
;
668 #else /* V3D_VERSION < 40 */
669 prim
.maximum_index
= (1u << 31) - 1; /* XXX */
670 prim
.address_of_indices_list
=
671 cl_address(rsc
->bo
, offset
);
672 #endif /* V3D_VERSION < 40 */
673 prim
.mode
= info
->mode
| prim_tf_enable
;
674 prim
.enable_primitive_restarts
= info
->primitive_restart
;
678 job
->draw_calls_queued
++;
680 if (info
->has_user_indices
)
681 pipe_resource_reference(&prsc
, NULL
);
683 if (info
->indirect
) {
684 cl_emit(&job
->bcl
, INDIRECT_VERTEX_ARRAY_INSTANCED_PRIMS
, prim
) {
685 prim
.mode
= info
->mode
| prim_tf_enable
;
686 prim
.number_of_draw_indirect_array_records
= info
->indirect
->draw_count
;
688 prim
.stride_in_multiples_of_4_bytes
= info
->indirect
->stride
>> 2;
689 prim
.address
= cl_address(v3d_resource(info
->indirect
->buffer
)->bo
,
690 info
->indirect
->offset
);
692 } else if (info
->instance_count
> 1) {
693 cl_emit(&job
->bcl
, VERTEX_ARRAY_INSTANCED_PRIMS
, prim
) {
694 prim
.mode
= info
->mode
| prim_tf_enable
;
695 prim
.index_of_first_vertex
= info
->start
;
696 prim
.number_of_instances
= info
->instance_count
;
697 prim
.instance_length
= info
->count
;
700 cl_emit(&job
->bcl
, VERTEX_ARRAY_PRIMS
, prim
) {
701 prim
.mode
= info
->mode
| prim_tf_enable
;
702 prim
.length
= info
->count
;
703 prim
.index_of_first_vertex
= info
->start
;
708 /* A flush is required in between a TF draw and any following TF specs
709 * packet, or the GPU may hang. Just flush each time for now.
711 if (v3d
->streamout
.num_targets
)
712 cl_emit(&job
->bcl
, TRANSFORM_FEEDBACK_FLUSH_AND_COUNT
, flush
);
714 job
->draw_calls_queued
++;
716 /* Increment the TF offsets by how many verts we wrote. XXX: This
717 * needs some clamping to the buffer size.
719 for (int i
= 0; i
< v3d
->streamout
.num_targets
; i
++)
720 v3d
->streamout
.offsets
[i
] += info
->count
;
722 if (v3d
->zsa
&& job
->zsbuf
&& v3d
->zsa
->base
.depth
.enabled
) {
723 struct v3d_resource
*rsc
= v3d_resource(job
->zsbuf
->texture
);
724 v3d_job_add_bo(job
, rsc
->bo
);
726 job
->load
|= PIPE_CLEAR_DEPTH
& ~job
->clear
;
727 if (v3d
->zsa
->base
.depth
.writemask
)
728 job
->store
|= PIPE_CLEAR_DEPTH
;
729 rsc
->initialized_buffers
= PIPE_CLEAR_DEPTH
;
732 if (v3d
->zsa
&& job
->zsbuf
&& v3d
->zsa
->base
.stencil
[0].enabled
) {
733 struct v3d_resource
*rsc
= v3d_resource(job
->zsbuf
->texture
);
734 if (rsc
->separate_stencil
)
735 rsc
= rsc
->separate_stencil
;
737 v3d_job_add_bo(job
, rsc
->bo
);
739 job
->load
|= PIPE_CLEAR_STENCIL
& ~job
->clear
;
740 if (v3d
->zsa
->base
.stencil
[0].writemask
||
741 v3d
->zsa
->base
.stencil
[1].writemask
) {
742 job
->store
|= PIPE_CLEAR_STENCIL
;
744 rsc
->initialized_buffers
|= PIPE_CLEAR_STENCIL
;
747 for (int i
= 0; i
< V3D_MAX_DRAW_BUFFERS
; i
++) {
748 uint32_t bit
= PIPE_CLEAR_COLOR0
<< i
;
749 int blend_rt
= v3d
->blend
->base
.independent_blend_enable
? i
: 0;
751 if (job
->store
& bit
|| !job
->cbufs
[i
])
753 struct v3d_resource
*rsc
= v3d_resource(job
->cbufs
[i
]->texture
);
755 job
->load
|= bit
& ~job
->clear
;
756 if (v3d
->blend
->base
.rt
[blend_rt
].colormask
)
758 v3d_job_add_bo(job
, rsc
->bo
);
761 if (job
->referenced_size
> 768 * 1024 * 1024) {
762 perf_debug("Flushing job with %dkb to try to free up memory\n",
763 job
->referenced_size
/ 1024);
767 if (V3D_DEBUG
& V3D_DEBUG_ALWAYS_FLUSH
)
772 * Implements gallium's clear() hook (glClear()) by drawing a pair of triangles.
775 v3d_draw_clear(struct v3d_context
*v3d
,
777 const union pipe_color_union
*color
,
778 double depth
, unsigned stencil
)
780 static const union pipe_color_union dummy_color
= {};
782 /* The blitter util dereferences the color regardless, even though the
783 * gallium clear API may not pass one in when only Z/S are cleared.
786 color
= &dummy_color
;
788 v3d_blitter_save(v3d
);
789 util_blitter_clear(v3d
->blitter
,
790 v3d
->framebuffer
.width
,
791 v3d
->framebuffer
.height
,
792 util_framebuffer_get_num_layers(&v3d
->framebuffer
),
793 buffers
, color
, depth
, stencil
);
797 * Attempts to perform the GL clear by using the TLB's fast clear at the start
801 v3d_tlb_clear(struct v3d_job
*job
, unsigned buffers
,
802 const union pipe_color_union
*color
,
803 double depth
, unsigned stencil
)
805 struct v3d_context
*v3d
= job
->v3d
;
807 if (job
->draw_calls_queued
) {
808 /* If anything in the CL has drawn using the buffer, then the
809 * TLB clear we're trying to add now would happen before that
812 buffers
&= ~(job
->load
| job
->store
);
815 /* GFXH-1461: If we were to emit a load of just depth or just stencil,
816 * then the clear for the other may get lost. We need to decide now
817 * if it would be possible to need to emit a load of just one after
818 * we've set up our TLB clears.
820 if (buffers
& PIPE_CLEAR_DEPTHSTENCIL
&&
821 (buffers
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
&&
823 util_format_is_depth_and_stencil(job
->zsbuf
->texture
->format
)) {
824 buffers
&= ~PIPE_CLEAR_DEPTHSTENCIL
;
827 for (int i
= 0; i
< V3D_MAX_DRAW_BUFFERS
; i
++) {
828 uint32_t bit
= PIPE_CLEAR_COLOR0
<< i
;
829 if (!(buffers
& bit
))
832 struct pipe_surface
*psurf
= v3d
->framebuffer
.cbufs
[i
];
833 struct v3d_surface
*surf
= v3d_surface(psurf
);
834 struct v3d_resource
*rsc
= v3d_resource(psurf
->texture
);
837 uint32_t internal_size
= 4 << surf
->internal_bpp
;
839 static union pipe_color_union swapped_color
;
840 if (v3d
->swap_color_rb
& (1 << i
)) {
841 swapped_color
.f
[0] = color
->f
[2];
842 swapped_color
.f
[1] = color
->f
[1];
843 swapped_color
.f
[2] = color
->f
[0];
844 swapped_color
.f
[3] = color
->f
[3];
845 color
= &swapped_color
;
848 switch (surf
->internal_type
) {
849 case V3D_INTERNAL_TYPE_8
:
850 util_pack_color(color
->f
, PIPE_FORMAT_R8G8B8A8_UNORM
,
852 memcpy(job
->clear_color
[i
], uc
.ui
, internal_size
);
854 case V3D_INTERNAL_TYPE_8I
:
855 case V3D_INTERNAL_TYPE_8UI
:
856 job
->clear_color
[i
][0] = ((color
->ui
[0] & 0xff) |
857 (color
->ui
[1] & 0xff) << 8 |
858 (color
->ui
[2] & 0xff) << 16 |
859 (color
->ui
[3] & 0xff) << 24);
861 case V3D_INTERNAL_TYPE_16F
:
862 util_pack_color(color
->f
, PIPE_FORMAT_R16G16B16A16_FLOAT
,
864 memcpy(job
->clear_color
[i
], uc
.ui
, internal_size
);
866 case V3D_INTERNAL_TYPE_16I
:
867 case V3D_INTERNAL_TYPE_16UI
:
868 job
->clear_color
[i
][0] = ((color
->ui
[0] & 0xffff) |
870 job
->clear_color
[i
][1] = ((color
->ui
[2] & 0xffff) |
873 case V3D_INTERNAL_TYPE_32F
:
874 case V3D_INTERNAL_TYPE_32I
:
875 case V3D_INTERNAL_TYPE_32UI
:
876 memcpy(job
->clear_color
[i
], color
->ui
, internal_size
);
880 rsc
->initialized_buffers
|= bit
;
883 unsigned zsclear
= buffers
& PIPE_CLEAR_DEPTHSTENCIL
;
885 struct v3d_resource
*rsc
=
886 v3d_resource(v3d
->framebuffer
.zsbuf
->texture
);
888 if (zsclear
& PIPE_CLEAR_DEPTH
)
889 job
->clear_z
= depth
;
890 if (zsclear
& PIPE_CLEAR_STENCIL
)
891 job
->clear_s
= stencil
;
893 rsc
->initialized_buffers
|= zsclear
;
898 job
->draw_max_x
= v3d
->framebuffer
.width
;
899 job
->draw_max_y
= v3d
->framebuffer
.height
;
900 job
->clear
|= buffers
;
901 job
->store
|= buffers
;
909 v3d_clear(struct pipe_context
*pctx
, unsigned buffers
,
910 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
912 struct v3d_context
*v3d
= v3d_context(pctx
);
913 struct v3d_job
*job
= v3d_get_job_for_fbo(v3d
);
915 buffers
&= ~v3d_tlb_clear(job
, buffers
, color
, depth
, stencil
);
918 v3d_draw_clear(v3d
, buffers
, color
, depth
, stencil
);
922 v3d_clear_render_target(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
923 const union pipe_color_union
*color
,
924 unsigned x
, unsigned y
, unsigned w
, unsigned h
,
925 bool render_condition_enabled
)
927 fprintf(stderr
, "unimpl: clear RT\n");
931 v3d_clear_depth_stencil(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
932 unsigned buffers
, double depth
, unsigned stencil
,
933 unsigned x
, unsigned y
, unsigned w
, unsigned h
,
934 bool render_condition_enabled
)
936 fprintf(stderr
, "unimpl: clear DS\n");
940 v3dX(draw_init
)(struct pipe_context
*pctx
)
942 pctx
->draw_vbo
= v3d_draw_vbo
;
943 pctx
->clear
= v3d_clear
;
944 pctx
->clear_render_target
= v3d_clear_render_target
;
945 pctx
->clear_depth_stencil
= v3d_clear_depth_stencil
;