1 /*
2 * Copyright © 2014-2017 Broadcom
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include "util/u_blitter.h"
25 #include "util/u_draw.h"
26 #include "util/u_prim.h"
27 #include "util/format/u_format.h"
28 #include "util/u_helpers.h"
29 #include "util/u_pack_color.h"
30 #include "util/u_prim_restart.h"
31 #include "util/u_upload_mgr.h"
32
33 #include "v3d_context.h"
34 #include "v3d_resource.h"
35 #include "v3d_cl.h"
36 #include "broadcom/compiler/v3d_compiler.h"
37 #include "broadcom/common/v3d_macros.h"
38 #include "broadcom/common/v3d_util.h"
39 #include "broadcom/common/v3d_csd.h"
40 #include "broadcom/cle/v3dx_pack.h"
41
42 void
v3dX(start_binning)43 v3dX(start_binning)(struct v3d_context *v3d, struct v3d_job *job)
44 {
45 assert(job->needs_flush);
46
47 /* Get space to emit our BCL state, using a branch to jump to a new BO
48 * if necessary.
49 */
50
51 v3d_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);
52
53 job->submit.bcl_start = job->bcl.bo->offset;
54 v3d_job_add_bo(job, job->bcl.bo);
55
56 /* The PTB will request the tile alloc initial size per tile at start
57 * of tile binning.
58 */
59 uint32_t tile_alloc_size =
60 MAX2(job->num_layers, 1) * job->draw_tiles_x * job->draw_tiles_y * 64;
61
62 /* The PTB allocates in aligned 4k chunks after the initial setup. */
63 tile_alloc_size = align(tile_alloc_size, 4096);
64
65 /* Include the first two chunk allocations that the PTB does so that
66 * we definitely clear the OOM condition before triggering one (the HW
67 * won't trigger OOM during the first allocations).
68 */
69 tile_alloc_size += 8192;
70
71 /* For performance, allocate some extra initial memory after the PTB's
72 * minimal allocations, so that we hopefully don't have to block the
73 * GPU on the kernel handling an OOM signal.
74 */
75 tile_alloc_size += 512 * 1024;
76
77 job->tile_alloc = v3d_bo_alloc(v3d->screen, tile_alloc_size,
78 "tile_alloc");
79 uint32_t tsda_per_tile_size = 256;
80 job->tile_state = v3d_bo_alloc(v3d->screen,
81 MAX2(job->num_layers, 1) *
82 job->draw_tiles_y *
83 job->draw_tiles_x *
84 tsda_per_tile_size,
85 "TSDA");
86
87 /* This must go before the binning mode configuration. It is
88 * required for layered framebuffers to work.
89 */
90 if (job->num_layers > 0) {
91 cl_emit(&job->bcl, NUMBER_OF_LAYERS, config) {
92 config.number_of_layers = job->num_layers;
93 }
94 }
95
96 assert(!job->msaa || !job->double_buffer);
97 #if V3D_VERSION >= 71
98 cl_emit(&job->bcl, TILE_BINNING_MODE_CFG, config) {
99 config.width_in_pixels = job->draw_width;
100 config.height_in_pixels = job->draw_height;
101
102 config.log2_tile_width = log2_tile_size(job->tile_width);
103 config.log2_tile_height = log2_tile_size(job->tile_height);
104
105 /* FIXME: ideallly we would like next assert on the packet header (as is
106 * general, so also applies to GL). We would need to expand
107 * gen_pack_header for that.
108 */
109 assert(config.log2_tile_width == config.log2_tile_height ||
110 config.log2_tile_width == config.log2_tile_height + 1);
111 }
112
113 #endif
114
115 #if V3D_VERSION == 42
116 cl_emit(&job->bcl, TILE_BINNING_MODE_CFG, config) {
117 config.width_in_pixels = job->draw_width;
118 config.height_in_pixels = job->draw_height;
119 config.number_of_render_targets =
120 MAX2(job->nr_cbufs, 1);
121
122 config.multisample_mode_4x = job->msaa;
123 config.double_buffer_in_non_ms_mode = job->double_buffer;
124
125 config.maximum_bpp_of_all_render_targets = job->internal_bpp;
126 }
127 #endif
128
129 /* There's definitely nothing in the VCD cache we want. */
130 cl_emit(&job->bcl, FLUSH_VCD_CACHE, bin);
131
132 /* Disable any leftover OQ state from another job. */
133 cl_emit(&job->bcl, OCCLUSION_QUERY_COUNTER, counter);
134
135 /* "Binning mode lists must have a Start Tile Binning item (6) after
136 * any prefix state data before the binning list proper starts."
137 */
138 cl_emit(&job->bcl, START_TILE_BINNING, bin);
139 }
140 /**
141 * Does the initial bining command list setup for drawing to a given FBO.
142 */
143 static void
v3d_start_draw(struct v3d_context * v3d)144 v3d_start_draw(struct v3d_context *v3d)
145 {
146 struct v3d_job *job = v3d->job;
147
148 if (job->needs_flush)
149 return;
150
151 job->needs_flush = true;
152 job->draw_width = v3d->framebuffer.width;
153 job->draw_height = v3d->framebuffer.height;
154 job->num_layers = util_framebuffer_get_num_layers(&v3d->framebuffer);
155
156 v3dX(start_binning)(v3d, job);
157 }
158
159 static void
v3d_predraw_check_stage_inputs(struct pipe_context * pctx,enum pipe_shader_type s)160 v3d_predraw_check_stage_inputs(struct pipe_context *pctx,
161 enum pipe_shader_type s)
162 {
163 struct v3d_context *v3d = v3d_context(pctx);
164 unsigned i;
165
166 /* Flush writes to textures we're sampling. */
167 for (i = 0; i < v3d->tex[s].num_textures; i++) {
168 struct pipe_sampler_view *pview = v3d->tex[s].textures[i];
169 if (!pview)
170 continue;
171 struct v3d_sampler_view *view = v3d_sampler_view(pview);
172
173 if (view->texture != view->base.texture &&
174 view->base.format != PIPE_FORMAT_X32_S8X24_UINT)
175 v3d_update_shadow_texture(pctx, &view->base);
176
177 v3d_flush_jobs_writing_resource(v3d, view->texture,
178 V3D_FLUSH_NOT_CURRENT_JOB,
179 s == PIPE_SHADER_COMPUTE);
180 }
181
182 /* Flush writes to UBOs. */
183 BITSET_FOREACH_SET(i, v3d->constbuf[s].enabled_mask,
184 PIPE_MAX_CONSTANT_BUFFERS) {
185 struct pipe_constant_buffer *cb = &v3d->constbuf[s].cb[i];
186 if (cb->buffer) {
187 v3d_flush_jobs_writing_resource(v3d, cb->buffer,
188 V3D_FLUSH_DEFAULT,
189 s == PIPE_SHADER_COMPUTE);
190 }
191 }
192
193 /* Flush reads/writes to our SSBOs */
194 BITSET_FOREACH_SET(i, v3d->ssbo[s].enabled_mask,
195 PIPE_MAX_SHADER_BUFFERS) {
196 struct pipe_shader_buffer *sb = &v3d->ssbo[s].sb[i];
197 if (sb->buffer) {
198 v3d_flush_jobs_reading_resource(v3d, sb->buffer,
199 V3D_FLUSH_NOT_CURRENT_JOB,
200 s == PIPE_SHADER_COMPUTE);
201 }
202 }
203
204 /* Flush reads/writes to our image views */
205 BITSET_FOREACH_SET(i, v3d->shaderimg[s].enabled_mask,
206 PIPE_MAX_SHADER_IMAGES) {
207 struct v3d_image_view *view = &v3d->shaderimg[s].si[i];
208
209 v3d_flush_jobs_reading_resource(v3d, view->base.resource,
210 V3D_FLUSH_NOT_CURRENT_JOB,
211 s == PIPE_SHADER_COMPUTE);
212 }
213
214 /* Flush writes to our vertex buffers (i.e. from transform feedback) */
215 if (s == PIPE_SHADER_VERTEX) {
216 BITSET_FOREACH_SET(i, v3d->vertexbuf.enabled_mask,
217 PIPE_MAX_ATTRIBS) {
218 struct pipe_vertex_buffer *vb = &v3d->vertexbuf.vb[i];
219
220 v3d_flush_jobs_writing_resource(v3d, vb->buffer.resource,
221 V3D_FLUSH_DEFAULT,
222 false);
223 }
224 }
225 }
226
227 static void
v3d_predraw_check_outputs(struct pipe_context * pctx)228 v3d_predraw_check_outputs(struct pipe_context *pctx)
229 {
230 struct v3d_context *v3d = v3d_context(pctx);
231
232 /* Flush jobs reading from TF buffers that we are about to write. */
233 if (v3d_transform_feedback_enabled(v3d)) {
234 struct v3d_streamout_stateobj *so = &v3d->streamout;
235
236 for (int i = 0; i < so->num_targets; i++) {
237 if (!so->targets[i])
238 continue;
239
240 const struct pipe_stream_output_target *target =
241 so->targets[i];
242 v3d_flush_jobs_reading_resource(v3d, target->buffer,
243 V3D_FLUSH_DEFAULT,
244 false);
245 }
246 }
247 }
248
249 /**
250 * Checks if the state for the current draw reads a particular resource in
251 * in the given shader stage.
252 */
253 static bool
v3d_state_reads_resource(struct v3d_context * v3d,struct pipe_resource * prsc,enum pipe_shader_type s)254 v3d_state_reads_resource(struct v3d_context *v3d,
255 struct pipe_resource *prsc,
256 enum pipe_shader_type s)
257 {
258 struct v3d_resource *rsc = v3d_resource(prsc);
259 unsigned i;
260
261 /* Vertex buffers */
262 if (s == PIPE_SHADER_VERTEX) {
263 BITSET_FOREACH_SET(i, v3d->vertexbuf.enabled_mask,
264 PIPE_MAX_ATTRIBS) {
265 struct pipe_vertex_buffer *vb = &v3d->vertexbuf.vb[i];
266 if (!vb->buffer.resource)
267 continue;
268
269 struct v3d_resource *vb_rsc =
270 v3d_resource(vb->buffer.resource);
271 if (rsc->bo == vb_rsc->bo)
272 return true;
273 }
274 }
275
276 /* Constant buffers */
277 BITSET_FOREACH_SET(i, v3d->constbuf[s].enabled_mask,
278 PIPE_MAX_CONSTANT_BUFFERS) {
279 struct pipe_constant_buffer *cb = &v3d->constbuf[s].cb[i];
280 if (!cb->buffer)
281 continue;
282
283 struct v3d_resource *cb_rsc = v3d_resource(cb->buffer);
284 if (rsc->bo == cb_rsc->bo)
285 return true;
286 }
287
288 /* Shader storage buffers */
289 BITSET_FOREACH_SET(i, v3d->ssbo[s].enabled_mask,
290 PIPE_MAX_SHADER_BUFFERS) {
291 struct pipe_shader_buffer *sb = &v3d->ssbo[s].sb[i];
292 if (!sb->buffer)
293 continue;
294
295 struct v3d_resource *sb_rsc = v3d_resource(sb->buffer);
296 if (rsc->bo == sb_rsc->bo)
297 return true;
298 }
299
300 /* Textures */
301 for (int i = 0; i < v3d->tex[s].num_textures; i++) {
302 struct pipe_sampler_view *pview = v3d->tex[s].textures[i];
303 if (!pview)
304 continue;
305
306 struct v3d_sampler_view *view = v3d_sampler_view(pview);
307 struct v3d_resource *v_rsc = v3d_resource(view->texture);
308 if (rsc->bo == v_rsc->bo)
309 return true;
310 }
311
312 return false;
313 }
314
315 static void
v3d_emit_wait_for_tf(struct v3d_job * job)316 v3d_emit_wait_for_tf(struct v3d_job *job)
317 {
318 /* XXX: we might be able to skip this in some cases, for now we
319 * always emit it.
320 */
321 cl_emit(&job->bcl, FLUSH_TRANSFORM_FEEDBACK_DATA, flush);
322
323 cl_emit(&job->bcl, WAIT_FOR_TRANSFORM_FEEDBACK, wait) {
324 /* XXX: Wait for all outstanding writes... maybe we can do
325 * better in some cases.
326 */
327 wait.block_count = 255;
328 }
329
330 /* We have just flushed all our outstanding TF work in this job so make
331 * sure we don't emit TF flushes again for any of it again.
332 */
333 _mesa_set_clear(job->tf_write_prscs, NULL);
334 }
335
336 static void
v3d_emit_wait_for_tf_if_needed(struct v3d_context * v3d,struct v3d_job * job)337 v3d_emit_wait_for_tf_if_needed(struct v3d_context *v3d, struct v3d_job *job)
338 {
339 if (!job->tf_enabled)
340 return;
341
342 set_foreach(job->tf_write_prscs, entry) {
343 struct pipe_resource *prsc = (struct pipe_resource *)entry->key;
344 for (int s = 0; s < PIPE_SHADER_COMPUTE; s++) {
345 /* Fragment shaders can only start executing after all
346 * binning (and thus TF) is complete.
347 *
348 * XXX: For VS/GS/TES, if the binning shader does not
349 * read the resource then we could also avoid emitting
350 * the wait.
351 */
352 if (s == PIPE_SHADER_FRAGMENT)
353 continue;
354
355 if (v3d_state_reads_resource(v3d, prsc, s)) {
356 v3d_emit_wait_for_tf(job);
357 return;
358 }
359 }
360 }
361 }
362
363 static void
v3d_emit_gs_state_record(struct v3d_job * job,struct v3d_compiled_shader * gs_bin,struct v3d_cl_reloc gs_bin_uniforms,struct v3d_compiled_shader * gs,struct v3d_cl_reloc gs_render_uniforms)364 v3d_emit_gs_state_record(struct v3d_job *job,
365 struct v3d_compiled_shader *gs_bin,
366 struct v3d_cl_reloc gs_bin_uniforms,
367 struct v3d_compiled_shader *gs,
368 struct v3d_cl_reloc gs_render_uniforms)
369 {
370 cl_emit(&job->indirect, GEOMETRY_SHADER_STATE_RECORD, shader) {
371 shader.geometry_bin_mode_shader_code_address =
372 cl_address(v3d_resource(gs_bin->resource)->bo,
373 gs_bin->offset);
374 shader.geometry_bin_mode_shader_4_way_threadable =
375 gs_bin->prog_data.gs->base.threads == 4;
376 shader.geometry_bin_mode_shader_start_in_final_thread_section =
377 gs_bin->prog_data.gs->base.single_seg;
378 #if V3D_VERSION == 42
379 shader.geometry_bin_mode_shader_propagate_nans = true;
380 #endif
381 shader.geometry_bin_mode_shader_uniforms_address =
382 gs_bin_uniforms;
383
384 shader.geometry_render_mode_shader_code_address =
385 cl_address(v3d_resource(gs->resource)->bo, gs->offset);
386 shader.geometry_render_mode_shader_4_way_threadable =
387 gs->prog_data.gs->base.threads == 4;
388 shader.geometry_render_mode_shader_start_in_final_thread_section =
389 gs->prog_data.gs->base.single_seg;
390 #if V3D_VERSION == 42
391 shader.geometry_render_mode_shader_propagate_nans = true;
392 #endif
393 shader.geometry_render_mode_shader_uniforms_address =
394 gs_render_uniforms;
395 }
396 }
397
398 static uint8_t
v3d_gs_output_primitive(enum mesa_prim prim_type)399 v3d_gs_output_primitive(enum mesa_prim prim_type)
400 {
401 switch (prim_type) {
402 case MESA_PRIM_POINTS:
403 return GEOMETRY_SHADER_POINTS;
404 case MESA_PRIM_LINE_STRIP:
405 return GEOMETRY_SHADER_LINE_STRIP;
406 case MESA_PRIM_TRIANGLE_STRIP:
407 return GEOMETRY_SHADER_TRI_STRIP;
408 default:
409 unreachable("Unsupported primitive type");
410 }
411 }
412
413 static void
v3d_emit_tes_gs_common_params(struct v3d_job * job,uint8_t gs_out_prim_type,uint8_t gs_num_invocations)414 v3d_emit_tes_gs_common_params(struct v3d_job *job,
415 uint8_t gs_out_prim_type,
416 uint8_t gs_num_invocations)
417 {
418 /* This, and v3d_emit_tes_gs_shader_params below, fill in default
419 * values for tessellation fields even though we don't support
420 * tessellation yet because our packing functions (and the simulator)
421 * complain if we don't.
422 */
423 cl_emit(&job->indirect, TESSELLATION_GEOMETRY_COMMON_PARAMS, shader) {
424 shader.tessellation_type = TESSELLATION_TYPE_TRIANGLE;
425 shader.tessellation_point_mode = false;
426 shader.tessellation_edge_spacing = TESSELLATION_EDGE_SPACING_EVEN;
427 shader.tessellation_clockwise = true;
428 shader.tessellation_invocations = 1;
429
430 shader.geometry_shader_output_format =
431 v3d_gs_output_primitive(gs_out_prim_type);
432 shader.geometry_shader_instances = gs_num_invocations & 0x1F;
433 }
434 }
435
436 static uint8_t
simd_width_to_gs_pack_mode(uint32_t width)437 simd_width_to_gs_pack_mode(uint32_t width)
438 {
439 switch (width) {
440 case 16:
441 return V3D_PACK_MODE_16_WAY;
442 case 8:
443 return V3D_PACK_MODE_8_WAY;
444 case 4:
445 return V3D_PACK_MODE_4_WAY;
446 case 1:
447 return V3D_PACK_MODE_1_WAY;
448 default:
449 unreachable("Invalid SIMD width");
450 };
451 }
452
453 static void
v3d_emit_tes_gs_shader_params(struct v3d_job * job,uint32_t gs_simd,uint32_t gs_vpm_output_size,uint32_t gs_max_vpm_input_size_per_batch)454 v3d_emit_tes_gs_shader_params(struct v3d_job *job,
455 uint32_t gs_simd,
456 uint32_t gs_vpm_output_size,
457 uint32_t gs_max_vpm_input_size_per_batch)
458 {
459 cl_emit(&job->indirect, TESSELLATION_GEOMETRY_SHADER_PARAMS, shader) {
460 shader.tcs_batch_flush_mode = V3D_TCS_FLUSH_MODE_FULLY_PACKED;
461 shader.per_patch_data_column_depth = 1;
462 shader.tcs_output_segment_size_in_sectors = 1;
463 shader.tcs_output_segment_pack_mode = V3D_PACK_MODE_16_WAY;
464 shader.tes_output_segment_size_in_sectors = 1;
465 shader.tes_output_segment_pack_mode = V3D_PACK_MODE_16_WAY;
466 shader.gs_output_segment_size_in_sectors = gs_vpm_output_size;
467 shader.gs_output_segment_pack_mode =
468 simd_width_to_gs_pack_mode(gs_simd);
469 shader.tbg_max_patches_per_tcs_batch = 1;
470 shader.tbg_max_extra_vertex_segs_for_patches_after_first = 0;
471 shader.tbg_min_tcs_output_segments_required_in_play = 1;
472 shader.tbg_min_per_patch_data_segments_required_in_play = 1;
473 shader.tpg_max_patches_per_tes_batch = 1;
474 shader.tpg_max_vertex_segments_per_tes_batch = 0;
475 shader.tpg_max_tcs_output_segments_per_tes_batch = 1;
476 shader.tpg_min_tes_output_segments_required_in_play = 1;
477 shader.gbg_max_tes_output_vertex_segments_per_gs_batch =
478 gs_max_vpm_input_size_per_batch;
479 shader.gbg_min_gs_output_segments_required_in_play = 1;
480 }
481 }
482
483 static void
emit_shader_state_record(struct v3d_context * v3d,struct v3d_job * job,const struct pipe_draw_info * info,struct v3d_vertex_stateobj * vtx,struct v3d_cl_reloc cs_uniforms,struct v3d_cl_reloc vs_uniforms,struct v3d_cl_reloc fs_uniforms,struct vpm_config * vpm_cfg_bin,struct vpm_config * vpm_cfg)484 emit_shader_state_record(struct v3d_context *v3d,
485 struct v3d_job *job,
486 const struct pipe_draw_info *info,
487 struct v3d_vertex_stateobj *vtx,
488 struct v3d_cl_reloc cs_uniforms,
489 struct v3d_cl_reloc vs_uniforms,
490 struct v3d_cl_reloc fs_uniforms,
491 struct vpm_config *vpm_cfg_bin,
492 struct vpm_config *vpm_cfg)
493 {
494 #if V3D_VERSION >= 71
495 /* 2712D0 (V3D 7.1.10) has included draw index and base vertex,
496 * shuffling all the fields in the packet. Since the versioning
497 * framework doesn't handle revision numbers, the XML has a
498 * different shader state record packet including the new fields
499 * and we decide at run time which packet we need to emit.
500 */
501 if (v3d_device_has_draw_index(&v3d->screen->devinfo)) {
502 cl_emit(&job->indirect, GL_SHADER_STATE_RECORD_DRAW_INDEX, shader) {
503 shader.enable_clipping = true;
504 shader.point_size_in_shaded_vertex_data =
505 (info->mode == MESA_PRIM_POINTS &&
506 v3d->rasterizer->base.point_size_per_vertex);
507 shader.fragment_shader_does_z_writes =
508 v3d->prog.fs->prog_data.fs->writes_z;
509 shader.turn_off_early_z_test =
510 v3d->prog.fs->prog_data.fs->disable_ez;
511 shader.fragment_shader_uses_real_pixel_centre_w_in_addition_to_centroid_w2 =
512 v3d->prog.fs->prog_data.fs->uses_center_w;
513 shader.any_shader_reads_hardware_written_primitive_id =
514 (v3d->prog.gs && v3d->prog.gs->prog_data.gs->uses_pid) ||
515 v3d->prog.fs->prog_data.fs->uses_pid;
516 shader.insert_primitive_id_as_first_varying_to_fragment_shader =
517 !v3d->prog.gs && v3d->prog.fs->prog_data.fs->uses_pid;
518 shader.do_scoreboard_wait_on_first_thread_switch =
519 v3d->prog.fs->prog_data.fs->lock_scoreboard_on_first_thrsw;
520 shader.disable_implicit_point_line_varyings =
521 !v3d->prog.fs->prog_data.fs->uses_implicit_point_line_varyings;
522 shader.number_of_varyings_in_fragment_shader =
523 v3d->prog.fs->prog_data.fs->num_inputs;
524 shader.coordinate_shader_code_address =
525 cl_address(v3d_resource(v3d->prog.cs->resource)->bo,
526 v3d->prog.cs->offset);
527 shader.vertex_shader_code_address =
528 cl_address(v3d_resource(v3d->prog.vs->resource)->bo,
529 v3d->prog.vs->offset);
530 shader.fragment_shader_code_address =
531 cl_address(v3d_resource(v3d->prog.fs->resource)->bo,
532 v3d->prog.fs->offset);
533 shader.coordinate_shader_input_vpm_segment_size =
534 v3d->prog.cs->prog_data.vs->vpm_input_size;
535 shader.vertex_shader_input_vpm_segment_size =
536 v3d->prog.vs->prog_data.vs->vpm_input_size;
537 shader.coordinate_shader_output_vpm_segment_size =
538 v3d->prog.cs->prog_data.vs->vpm_output_size;
539 shader.vertex_shader_output_vpm_segment_size =
540 v3d->prog.vs->prog_data.vs->vpm_output_size;
541 shader.coordinate_shader_uniforms_address = cs_uniforms;
542 shader.vertex_shader_uniforms_address = vs_uniforms;
543 shader.fragment_shader_uniforms_address = fs_uniforms;
544 shader.min_coord_shader_input_segments_required_in_play =
545 vpm_cfg_bin->As;
546 shader.min_vertex_shader_input_segments_required_in_play =
547 vpm_cfg->As;
548 shader.min_coord_shader_output_segments_required_in_play_in_addition_to_vcm_cache_size =
549 vpm_cfg_bin->Ve;
550 shader.min_vertex_shader_output_segments_required_in_play_in_addition_to_vcm_cache_size =
551 vpm_cfg->Ve;
552 shader.coordinate_shader_4_way_threadable =
553 v3d->prog.cs->prog_data.vs->base.threads == 4;
554 shader.vertex_shader_4_way_threadable =
555 v3d->prog.vs->prog_data.vs->base.threads == 4;
556 shader.fragment_shader_4_way_threadable =
557 v3d->prog.fs->prog_data.fs->base.threads == 4;
558 shader.coordinate_shader_start_in_final_thread_section =
559 v3d->prog.cs->prog_data.vs->base.single_seg;
560 shader.vertex_shader_start_in_final_thread_section =
561 v3d->prog.vs->prog_data.vs->base.single_seg;
562 shader.fragment_shader_start_in_final_thread_section =
563 v3d->prog.fs->prog_data.fs->base.single_seg;
564 shader.vertex_id_read_by_coordinate_shader =
565 v3d->prog.cs->prog_data.vs->uses_vid;
566 shader.instance_id_read_by_coordinate_shader =
567 v3d->prog.cs->prog_data.vs->uses_iid;
568 shader.vertex_id_read_by_vertex_shader =
569 v3d->prog.vs->prog_data.vs->uses_vid;
570 shader.instance_id_read_by_vertex_shader =
571 v3d->prog.vs->prog_data.vs->uses_iid;
572 }
573 return;
574 }
575 #endif
576
577 assert(!v3d_device_has_draw_index(&v3d->screen->devinfo));
578 cl_emit(&job->indirect, GL_SHADER_STATE_RECORD, shader) {
579 shader.enable_clipping = true;
580 /* V3D_DIRTY_PRIM_MODE | V3D_DIRTY_RASTERIZER */
581 shader.point_size_in_shaded_vertex_data =
582 (info->mode == MESA_PRIM_POINTS &&
583 v3d->rasterizer->base.point_size_per_vertex);
584
585 /* Must be set if the shader modifies Z, discards, or modifies
586 * the sample mask. For any of these cases, the fragment
587 * shader needs to write the Z value (even just discards).
588 */
589 shader.fragment_shader_does_z_writes =
590 v3d->prog.fs->prog_data.fs->writes_z;
591
592 /* Set if the EZ test must be disabled (due to shader side
593 * effects and the early_z flag not being present in the
594 * shader).
595 */
596 shader.turn_off_early_z_test =
597 v3d->prog.fs->prog_data.fs->disable_ez;
598
599 shader.fragment_shader_uses_real_pixel_centre_w_in_addition_to_centroid_w2 =
600 v3d->prog.fs->prog_data.fs->uses_center_w;
601
602 shader.any_shader_reads_hardware_written_primitive_id =
603 (v3d->prog.gs && v3d->prog.gs->prog_data.gs->uses_pid) ||
604 v3d->prog.fs->prog_data.fs->uses_pid;
605 shader.insert_primitive_id_as_first_varying_to_fragment_shader =
606 !v3d->prog.gs && v3d->prog.fs->prog_data.fs->uses_pid;
607
608 shader.do_scoreboard_wait_on_first_thread_switch =
609 v3d->prog.fs->prog_data.fs->lock_scoreboard_on_first_thrsw;
610 shader.disable_implicit_point_line_varyings =
611 !v3d->prog.fs->prog_data.fs->uses_implicit_point_line_varyings;
612
613 shader.number_of_varyings_in_fragment_shader =
614 v3d->prog.fs->prog_data.fs->num_inputs;
615
616 shader.coordinate_shader_code_address =
617 cl_address(v3d_resource(v3d->prog.cs->resource)->bo,
618 v3d->prog.cs->offset);
619 shader.vertex_shader_code_address =
620 cl_address(v3d_resource(v3d->prog.vs->resource)->bo,
621 v3d->prog.vs->offset);
622 shader.fragment_shader_code_address =
623 cl_address(v3d_resource(v3d->prog.fs->resource)->bo,
624 v3d->prog.fs->offset);
625
626 #if V3D_VERSION == 42
627 shader.coordinate_shader_propagate_nans = true;
628 shader.vertex_shader_propagate_nans = true;
629 shader.fragment_shader_propagate_nans = true;
630
631 /* XXX: Use combined input/output size flag in the common
632 * case.
633 */
634 shader.coordinate_shader_has_separate_input_and_output_vpm_blocks =
635 v3d->prog.cs->prog_data.vs->separate_segments;
636 shader.vertex_shader_has_separate_input_and_output_vpm_blocks =
637 v3d->prog.vs->prog_data.vs->separate_segments;
638 shader.coordinate_shader_input_vpm_segment_size =
639 v3d->prog.cs->prog_data.vs->separate_segments ?
640 v3d->prog.cs->prog_data.vs->vpm_input_size : 1;
641 shader.vertex_shader_input_vpm_segment_size =
642 v3d->prog.vs->prog_data.vs->separate_segments ?
643 v3d->prog.vs->prog_data.vs->vpm_input_size : 1;
644 #endif
645 /* On V3D 7.1 there isn't a specific flag to set if we are using
646 * shared/separate segments or not. We just set the value of
647 * vpm_input_size to 0, and set output to the max needed. That should be
648 * already properly set on prog_data_vs_bin
649 */
650 #if V3D_VERSION == 71
651 shader.coordinate_shader_input_vpm_segment_size =
652 v3d->prog.cs->prog_data.vs->vpm_input_size;
653 shader.vertex_shader_input_vpm_segment_size =
654 v3d->prog.vs->prog_data.vs->vpm_input_size;
655 #endif
656
657 shader.coordinate_shader_output_vpm_segment_size =
658 v3d->prog.cs->prog_data.vs->vpm_output_size;
659 shader.vertex_shader_output_vpm_segment_size =
660 v3d->prog.vs->prog_data.vs->vpm_output_size;
661
662 shader.coordinate_shader_uniforms_address = cs_uniforms;
663 shader.vertex_shader_uniforms_address = vs_uniforms;
664 shader.fragment_shader_uniforms_address = fs_uniforms;
665
666 shader.min_coord_shader_input_segments_required_in_play =
667 vpm_cfg_bin->As;
668 shader.min_vertex_shader_input_segments_required_in_play =
669 vpm_cfg->As;
670
671 shader.min_coord_shader_output_segments_required_in_play_in_addition_to_vcm_cache_size =
672 vpm_cfg_bin->Ve;
673 shader.min_vertex_shader_output_segments_required_in_play_in_addition_to_vcm_cache_size =
674 vpm_cfg->Ve;
675
676 shader.coordinate_shader_4_way_threadable =
677 v3d->prog.cs->prog_data.vs->base.threads == 4;
678 shader.vertex_shader_4_way_threadable =
679 v3d->prog.vs->prog_data.vs->base.threads == 4;
680 shader.fragment_shader_4_way_threadable =
681 v3d->prog.fs->prog_data.fs->base.threads == 4;
682
683 shader.coordinate_shader_start_in_final_thread_section =
684 v3d->prog.cs->prog_data.vs->base.single_seg;
685 shader.vertex_shader_start_in_final_thread_section =
686 v3d->prog.vs->prog_data.vs->base.single_seg;
687 shader.fragment_shader_start_in_final_thread_section =
688 v3d->prog.fs->prog_data.fs->base.single_seg;
689
690 shader.vertex_id_read_by_coordinate_shader =
691 v3d->prog.cs->prog_data.vs->uses_vid;
692 shader.instance_id_read_by_coordinate_shader =
693 v3d->prog.cs->prog_data.vs->uses_iid;
694 shader.vertex_id_read_by_vertex_shader =
695 v3d->prog.vs->prog_data.vs->uses_vid;
696 shader.instance_id_read_by_vertex_shader =
697 v3d->prog.vs->prog_data.vs->uses_iid;
698
699 #if V3D_VERSION == 42
700 shader.address_of_default_attribute_values =
701 cl_address(v3d_resource(vtx->defaults)->bo,
702 vtx->defaults_offset);
703 #endif
704 }
705 }
706
707 static void
v3d_emit_gl_shader_state(struct v3d_context * v3d,const struct pipe_draw_info * info)708 v3d_emit_gl_shader_state(struct v3d_context *v3d,
709 const struct pipe_draw_info *info)
710 {
711 struct v3d_job *job = v3d->job;
712 /* V3D_DIRTY_VTXSTATE */
713 struct v3d_vertex_stateobj *vtx = v3d->vtx;
714 /* V3D_DIRTY_VTXBUF */
715 struct v3d_vertexbuf_stateobj *vertexbuf = &v3d->vertexbuf;
716
717 /* Upload the uniforms to the indirect CL first */
718 struct v3d_cl_reloc fs_uniforms =
719 v3d_write_uniforms(v3d, job, v3d->prog.fs,
720 PIPE_SHADER_FRAGMENT);
721
722 struct v3d_cl_reloc gs_uniforms = { NULL, 0 };
723 struct v3d_cl_reloc gs_bin_uniforms = { NULL, 0 };
724 if (v3d->prog.gs) {
725 gs_uniforms = v3d_write_uniforms(v3d, job, v3d->prog.gs,
726 PIPE_SHADER_GEOMETRY);
727 }
728 if (v3d->prog.gs_bin) {
729 gs_bin_uniforms = v3d_write_uniforms(v3d, job, v3d->prog.gs_bin,
730 PIPE_SHADER_GEOMETRY);
731 }
732
733 struct v3d_cl_reloc vs_uniforms =
734 v3d_write_uniforms(v3d, job, v3d->prog.vs,
735 PIPE_SHADER_VERTEX);
736 struct v3d_cl_reloc cs_uniforms =
737 v3d_write_uniforms(v3d, job, v3d->prog.cs,
738 PIPE_SHADER_VERTEX);
739
740 /* Update the cache dirty flag based on the shader progs data */
741 job->tmu_dirty_rcl |= v3d->prog.cs->prog_data.vs->base.tmu_dirty_rcl;
742 job->tmu_dirty_rcl |= v3d->prog.vs->prog_data.vs->base.tmu_dirty_rcl;
743 if (v3d->prog.gs_bin) {
744 job->tmu_dirty_rcl |=
745 v3d->prog.gs_bin->prog_data.gs->base.tmu_dirty_rcl;
746 }
747 if (v3d->prog.gs) {
748 job->tmu_dirty_rcl |=
749 v3d->prog.gs->prog_data.gs->base.tmu_dirty_rcl;
750 }
751 job->tmu_dirty_rcl |= v3d->prog.fs->prog_data.fs->base.tmu_dirty_rcl;
752
753 uint32_t num_elements_to_emit = 0;
754 for (int i = 0; i < vtx->num_elements; i++) {
755 struct pipe_vertex_element *elem = &vtx->pipe[i];
756 struct pipe_vertex_buffer *vb =
757 &vertexbuf->vb[elem->vertex_buffer_index];
758 if (vb->buffer.resource)
759 num_elements_to_emit++;
760 }
761
762 uint32_t shader_state_record_length =
763 cl_packet_length(GL_SHADER_STATE_RECORD);
764 if (v3d->prog.gs) {
765 shader_state_record_length +=
766 cl_packet_length(GEOMETRY_SHADER_STATE_RECORD) +
767 cl_packet_length(TESSELLATION_GEOMETRY_COMMON_PARAMS) +
768 2 * cl_packet_length(TESSELLATION_GEOMETRY_SHADER_PARAMS);
769 }
770
771 /* See GFXH-930 workaround below */
772 uint32_t shader_rec_offset =
773 v3d_cl_ensure_space(&job->indirect,
774 shader_state_record_length +
775 MAX2(num_elements_to_emit, 1) *
776 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD),
777 32);
778
779 /* XXX perf: We should move most of the SHADER_STATE_RECORD setup to
780 * compile time, so that we mostly just have to OR the VS and FS
781 * records together at draw time.
782 */
783
784 struct vpm_config vpm_cfg_bin, vpm_cfg;
785 v3d_compute_vpm_config(&v3d->screen->devinfo,
786 v3d->prog.cs->prog_data.vs,
787 v3d->prog.vs->prog_data.vs,
788 v3d->prog.gs ? v3d->prog.gs_bin->prog_data.gs : NULL,
789 v3d->prog.gs ? v3d->prog.gs->prog_data.gs : NULL,
790 &vpm_cfg_bin,
791 &vpm_cfg);
792
793 if (v3d->prog.gs) {
794 v3d_emit_gs_state_record(v3d->job,
795 v3d->prog.gs_bin, gs_bin_uniforms,
796 v3d->prog.gs, gs_uniforms);
797
798 struct v3d_gs_prog_data *gs = v3d->prog.gs->prog_data.gs;
799 v3d_emit_tes_gs_common_params(v3d->job,
800 gs->out_prim_type,
801 gs->num_invocations);
802
803 /* Bin Tes/Gs params */
804 v3d_emit_tes_gs_shader_params(v3d->job,
805 vpm_cfg_bin.gs_width,
806 vpm_cfg_bin.Gd,
807 vpm_cfg_bin.Gv);
808
809 /* Render Tes/Gs params */
810 v3d_emit_tes_gs_shader_params(v3d->job,
811 vpm_cfg.gs_width,
812 vpm_cfg.Gd,
813 vpm_cfg.Gv);
814 }
815
816 emit_shader_state_record(v3d, job, info, vtx,
817 cs_uniforms, vs_uniforms, fs_uniforms,
818 &vpm_cfg_bin, &vpm_cfg);
819
820 bool cs_loaded_any = false;
821 const bool cs_uses_builtins = v3d->prog.cs->prog_data.vs->uses_iid ||
822 v3d->prog.cs->prog_data.vs->uses_biid ||
823 v3d->prog.cs->prog_data.vs->uses_vid;
824 for (int i = 0; i < vtx->num_elements; i++) {
825 struct pipe_vertex_element *elem = &vtx->pipe[i];
826 struct pipe_vertex_buffer *vb =
827 &vertexbuf->vb[elem->vertex_buffer_index];
828 struct v3d_resource *rsc = v3d_resource(vb->buffer.resource);
829
830 if (!rsc)
831 continue;
832
833 enum { size = cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD) };
834 cl_emit_with_prepacked(&job->indirect,
835 GL_SHADER_STATE_ATTRIBUTE_RECORD,
836 &vtx->attrs[i * size], attr) {
837 attr.stride = elem->src_stride;
838 attr.address = cl_address(rsc->bo,
839 vb->buffer_offset +
840 elem->src_offset);
841 attr.number_of_values_read_by_coordinate_shader =
842 v3d->prog.cs->prog_data.vs->vattr_sizes[i];
843 attr.number_of_values_read_by_vertex_shader =
844 v3d->prog.vs->prog_data.vs->vattr_sizes[i];
845
846 /* GFXH-930: At least one attribute must be enabled
847 * and read by CS and VS. If we have attributes being
848 * consumed by the VS but not the CS, then set up a
849 * dummy load of the last attribute into the CS's VPM
850 * inputs. (Since CS is just dead-code-elimination
851 * compared to VS, we can't have CS loading but not
852 * VS).
853 *
854 * GFXH-1602: first attribute must be active if using
855 * builtins.
856 */
857 if (v3d->prog.cs->prog_data.vs->vattr_sizes[i])
858 cs_loaded_any = true;
859 if (i == 0 && cs_uses_builtins && !cs_loaded_any) {
860 attr.number_of_values_read_by_coordinate_shader = 1;
861 cs_loaded_any = true;
862 } else if (i == vtx->num_elements - 1 && !cs_loaded_any) {
863 attr.number_of_values_read_by_coordinate_shader = 1;
864 cs_loaded_any = true;
865 }
866 attr.maximum_index = 0xffffff;
867 }
868 STATIC_ASSERT(sizeof(vtx->attrs) >= V3D_MAX_VS_INPUTS / 4 * size);
869 }
870
871 if (num_elements_to_emit == 0) {
872 /* GFXH-930: At least one attribute must be enabled and read
873 * by CS and VS. If we have no attributes being consumed by
874 * the shader, set up a dummy to be loaded into the VPM.
875 */
876 cl_emit(&job->indirect, GL_SHADER_STATE_ATTRIBUTE_RECORD, attr) {
877 /* Valid address of data whose value will be unused. */
878 attr.address = cl_address(job->indirect.bo, 0);
879
880 attr.type = ATTRIBUTE_FLOAT;
881 attr.stride = 0;
882 attr.vec_size = 1;
883
884 attr.number_of_values_read_by_coordinate_shader = 1;
885 attr.number_of_values_read_by_vertex_shader = 1;
886 }
887 num_elements_to_emit = 1;
888 }
889
890 cl_emit(&job->bcl, VCM_CACHE_SIZE, vcm) {
891 vcm.number_of_16_vertex_batches_for_binning = vpm_cfg_bin.Vc;
892 vcm.number_of_16_vertex_batches_for_rendering = vpm_cfg.Vc;
893 }
894
895 if (v3d->prog.gs) {
896 cl_emit(&job->bcl, GL_SHADER_STATE_INCLUDING_GS, state) {
897 state.address = cl_address(job->indirect.bo,
898 shader_rec_offset);
899 state.number_of_attribute_arrays = num_elements_to_emit;
900 }
901 } else {
902 cl_emit(&job->bcl, GL_SHADER_STATE, state) {
903 state.address = cl_address(job->indirect.bo,
904 shader_rec_offset);
905 state.number_of_attribute_arrays = num_elements_to_emit;
906 }
907 }
908
909 v3d_bo_unreference(&cs_uniforms.bo);
910 v3d_bo_unreference(&vs_uniforms.bo);
911 if (gs_uniforms.bo)
912 v3d_bo_unreference(&gs_uniforms.bo);
913 if (gs_bin_uniforms.bo)
914 v3d_bo_unreference(&gs_bin_uniforms.bo);
915 v3d_bo_unreference(&fs_uniforms.bo);
916 }
917
918 /**
919 * Updates the number of primitives generated from the number of vertices
920 * to draw. This only works when no GS is present, since otherwise the number
921 * of primitives generated cannot be determined in advance and we need to
922 * use the PRIMITIVE_COUNTS_FEEDBACK command instead, however, that requires
923 * a sync wait for the draw to complete, so we only use that when GS is present.
924 */
925 static void
v3d_update_primitives_generated_counter(struct v3d_context * v3d,const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw)926 v3d_update_primitives_generated_counter(struct v3d_context *v3d,
927 const struct pipe_draw_info *info,
928 const struct pipe_draw_start_count_bias *draw)
929 {
930 assert(!v3d->prog.gs);
931
932 if (!v3d->active_queries)
933 return;
934
935 uint32_t prims = u_prims_for_vertices(info->mode, draw->count);
936 v3d->prims_generated += prims;
937 }
938
939 static void
v3d_update_job_ez(struct v3d_context * v3d,struct v3d_job * job)940 v3d_update_job_ez(struct v3d_context *v3d, struct v3d_job *job)
941 {
942 /* If first_ez_state is V3D_EZ_DISABLED it means that we have already
943 * determined that we should disable EZ completely for all draw calls
944 * in this job. This will cause us to disable EZ for the entire job in
945 * the Tile Rendering Mode RCL packet and when we do that we need to
946 * make sure we never emit a draw call in the job with EZ enabled in
947 * the CFG_BITS packet, so ez_state must also be V3D_EZ_DISABLED.
948 */
949 if (job->first_ez_state == V3D_EZ_DISABLED) {
950 assert(job->ez_state == V3D_EZ_DISABLED);
951 return;
952 }
953
954 /* When we update the EZ state we first check if there is anything
955 * that requires disabling it completely for the entire job (based on
956 * state that is not related to the current draw call and pipeline
957 * state).
958 */
959 if (!job->decided_global_ez_enable ||
960 job->global_ez_zsa_decision_state != v3d->zsa) {
961 job->decided_global_ez_enable = true;
962 job->global_ez_zsa_decision_state = v3d->zsa;
963
964 if (!job->zsbuf) {
965 job->first_ez_state = V3D_EZ_DISABLED;
966 job->ez_state = V3D_EZ_DISABLED;
967 return;
968 }
969
970 /* GFXH-1918: the early-Z buffer may load incorrect depth
971 * values if the frame has odd width or height, or if the
972 * buffer is 16-bit and multisampled. Disable early-Z in these
973 * cases.
974 */
975 bool needs_depth_load = v3d->zsa && job->zsbuf &&
976 v3d->zsa->base.depth_enabled &&
977 (PIPE_CLEAR_DEPTH & ~job->clear_tlb);
978 if (needs_depth_load) {
979 if (job->zsbuf->texture->format == PIPE_FORMAT_Z16_UNORM &&
980 job->zsbuf->texture->nr_samples > 0) {
981 perf_debug("Loading 16-bit multisampled depth buffer "
982 "disables early-Z tests\n");
983 job->first_ez_state = V3D_EZ_DISABLED;
984 job->ez_state = V3D_EZ_DISABLED;
985 return;
986 }
987 if ((job->draw_width % 2 != 0) || (job->draw_height % 2 != 0)) {
988 perf_debug("Loading depth buffer for framebuffer with "
989 "odd width or height disables early-Z tests\n");
990 job->first_ez_state = V3D_EZ_DISABLED;
991 job->ez_state = V3D_EZ_DISABLED;
992 return;
993 }
994 }
995 }
996
997 switch (v3d->zsa->ez_state) {
998 case V3D_EZ_UNDECIDED:
999 /* If the Z/S state didn't pick a direction but didn't
1000 * disable, then go along with the current EZ state. This
1001 * allows EZ optimization for Z func == EQUAL or NEVER.
1002 */
1003 break;
1004
1005 case V3D_EZ_LT_LE:
1006 case V3D_EZ_GT_GE:
1007 /* If the Z/S state picked a direction, then it needs to match
1008 * the current direction if we've decided on one.
1009 */
1010 if (job->ez_state == V3D_EZ_UNDECIDED)
1011 job->ez_state = v3d->zsa->ez_state;
1012 else if (job->ez_state != v3d->zsa->ez_state)
1013 job->ez_state = V3D_EZ_DISABLED;
1014 break;
1015
1016 case V3D_EZ_DISABLED:
1017 /* If the current Z/S state disables EZ because of a bad Z
1018 * func or stencil operation, then we can't do any more EZ in
1019 * this frame.
1020 */
1021 job->ez_state = V3D_EZ_DISABLED;
1022 break;
1023 }
1024
1025 /* If the FS affects the Z of the pixels, then it may update against
1026 * the chosen EZ direction (though we could use
1027 * ARB_conservative_depth's hints to avoid this)
1028 */
1029 if (v3d->prog.fs->prog_data.fs->writes_z &&
1030 !v3d->prog.fs->prog_data.fs->writes_z_from_fep) {
1031 job->ez_state = V3D_EZ_DISABLED;
1032 }
1033
1034 if (job->first_ez_state == V3D_EZ_UNDECIDED &&
1035 (job->ez_state != V3D_EZ_DISABLED || job->draw_calls_queued == 0))
1036 job->first_ez_state = job->ez_state;
1037 }
1038
1039 static bool
v3d_check_compiled_shaders(struct v3d_context * v3d)1040 v3d_check_compiled_shaders(struct v3d_context *v3d)
1041 {
1042 static bool warned[5] = { 0 };
1043
1044 uint32_t failed_stage = MESA_SHADER_NONE;
1045 if (!v3d->prog.vs->resource || !v3d->prog.cs->resource) {
1046 failed_stage = MESA_SHADER_VERTEX;
1047 } else if ((v3d->prog.gs_bin && !v3d->prog.gs_bin->resource) ||
1048 (v3d->prog.gs && !v3d->prog.gs->resource)) {
1049 failed_stage = MESA_SHADER_GEOMETRY;
1050 } else if (v3d->prog.fs && !v3d->prog.fs->resource) {
1051 failed_stage = MESA_SHADER_FRAGMENT;
1052 }
1053
1054 if (likely(failed_stage == MESA_SHADER_NONE))
1055 return true;
1056
1057 if (!warned[failed_stage]) {
1058 fprintf(stderr,
1059 "%s shader failed to compile. Expect corruption.\n",
1060 _mesa_shader_stage_to_string(failed_stage));
1061 warned[failed_stage] = true;
1062 }
1063 return false;
1064 }
1065
1066 static void
v3d_draw_vbo(struct pipe_context * pctx,const struct pipe_draw_info * info,unsigned drawid_offset,const struct pipe_draw_indirect_info * indirect,const struct pipe_draw_start_count_bias * draws,unsigned num_draws)1067 v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info,
1068 unsigned drawid_offset,
1069 const struct pipe_draw_indirect_info *indirect,
1070 const struct pipe_draw_start_count_bias *draws,
1071 unsigned num_draws)
1072 {
1073 if (num_draws > 1) {
1074 util_draw_multi(pctx, info, drawid_offset, indirect, draws, num_draws);
1075 return;
1076 }
1077
1078 if (!indirect && (!draws[0].count || !info->instance_count))
1079 return;
1080
1081 struct v3d_context *v3d = v3d_context(pctx);
1082
1083 if (!indirect &&
1084 !info->primitive_restart &&
1085 !u_trim_pipe_prim(info->mode, (unsigned*)&draws[0].count))
1086 return;
1087
1088 if (!v3d_render_condition_check(v3d))
1089 return;
1090
1091 /* Fall back for weird desktop GL primitive restart values. */
1092 if (info->primitive_restart &&
1093 info->index_size) {
1094 uint32_t mask = util_prim_restart_index_from_size(info->index_size);
1095 if (info->restart_index != mask) {
1096 util_draw_vbo_without_prim_restart(pctx, info, drawid_offset, indirect, &draws[0]);
1097 return;
1098 }
1099 }
1100
1101 /* Before setting up the draw, flush anything writing to the resources
1102 * that we read from or reading from resources we write to.
1103 */
1104 for (int s = 0; s < PIPE_SHADER_COMPUTE; s++)
1105 v3d_predraw_check_stage_inputs(pctx, s);
1106
1107 if (indirect && indirect->buffer) {
1108 v3d_flush_jobs_writing_resource(v3d, indirect->buffer,
1109 V3D_FLUSH_DEFAULT, false);
1110 }
1111
1112 v3d_predraw_check_outputs(pctx);
1113
1114 /* If transform feedback is active and we are switching primitive type
1115 * we need to submit the job before drawing and update the vertex count
1116 * written to TF based on the primitive type since we will need to
1117 * know the exact vertex count if the application decides to call
1118 * glDrawTransformFeedback() later.
1119 */
1120 if (v3d->streamout.num_targets > 0 &&
1121 u_base_prim_type(info->mode) != u_base_prim_type(v3d->prim_mode)) {
1122 v3d_update_primitive_counters(v3d);
1123 }
1124
1125 struct v3d_job *job = v3d_get_job_for_fbo(v3d);
1126
1127 /* If vertex texturing depends on the output of rendering, we need to
1128 * ensure that that rendering is complete before we run a coordinate
1129 * shader that depends on it.
1130 *
1131 * Given that doing that is unusual, for now we just block the binner
1132 * on the last submitted render, rather than tracking the last
1133 * rendering to each texture's BO.
1134 */
1135 if (v3d->tex[PIPE_SHADER_VERTEX].num_textures || (indirect && indirect->buffer)) {
1136 static bool warned = false;
1137 if (!warned) {
1138 perf_debug("Blocking binner on last render due to "
1139 "vertex texturing or indirect drawing.\n");
1140 warned = true;
1141 }
1142 job->submit.in_sync_bcl = v3d->out_sync;
1143 }
1144
1145 /* We also need to ensure that compute is complete when render depends
1146 * on resources written by it.
1147 */
1148 if (v3d->sync_on_last_compute_job) {
1149 job->submit.in_sync_bcl = v3d->out_sync;
1150 v3d->sync_on_last_compute_job = false;
1151 }
1152
1153 /* Mark SSBOs and images as being written. We don't actually know
1154 * which ones are read vs written, so just assume the worst.
1155 */
1156 for (int s = 0; s < PIPE_SHADER_COMPUTE; s++) {
1157 unsigned i;
1158 BITSET_FOREACH_SET(i, v3d->ssbo[s].enabled_mask,
1159 PIPE_MAX_SHADER_BUFFERS) {
1160 v3d_job_add_write_resource(job,
1161 v3d->ssbo[s].sb[i].buffer);
1162 struct v3d_resource *rsc= v3d_resource(v3d->ssbo[s].sb[i].buffer);
1163 rsc->graphics_written = true;
1164 job->tmu_dirty_rcl = true;
1165 }
1166
1167 BITSET_FOREACH_SET(i, v3d->shaderimg[s].enabled_mask,
1168 PIPE_MAX_SHADER_IMAGES) {
1169 v3d_job_add_write_resource(job,
1170 v3d->shaderimg[s].si[i].base.resource);
1171 struct v3d_resource *rsc= v3d_resource(v3d->shaderimg[s].si[i].base.resource);
1172 rsc->graphics_written = true;
1173 job->tmu_dirty_rcl = true;
1174 }
1175 }
1176
1177 /* Get space to emit our draw call into the BCL, using a branch to
1178 * jump to a new BO if necessary.
1179 */
1180 v3d_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);
1181
1182 if (v3d->prim_mode != info->mode) {
1183 v3d->prim_mode = info->mode;
1184 v3d->dirty |= V3D_DIRTY_PRIM_MODE;
1185 }
1186
1187 v3d_start_draw(v3d);
1188 v3d_update_compiled_shaders(v3d, info->mode);
1189 if (!v3d_check_compiled_shaders(v3d))
1190 return;
1191 v3d_update_job_ez(v3d, job);
1192
1193 /* If this job was writing to transform feedback buffers before this
1194 * draw and we are reading from them here, then we need to wait for TF
1195 * to complete before we emit this draw.
1196 *
1197 * Notice this check needs to happen before we emit state for the
1198 * current draw call, where we update job->tf_enabled, so we can ensure
1199 * that we only check TF writes for prior draws.
1200 */
1201 v3d_emit_wait_for_tf_if_needed(v3d, job);
1202
1203 v3dX(emit_state)(pctx);
1204
1205 if (v3d->dirty & (V3D_DIRTY_VTXBUF |
1206 V3D_DIRTY_VTXSTATE |
1207 V3D_DIRTY_PRIM_MODE |
1208 V3D_DIRTY_RASTERIZER |
1209 V3D_DIRTY_COMPILED_CS |
1210 V3D_DIRTY_COMPILED_VS |
1211 V3D_DIRTY_COMPILED_GS_BIN |
1212 V3D_DIRTY_COMPILED_GS |
1213 V3D_DIRTY_COMPILED_FS |
1214 v3d->prog.cs->uniform_dirty_bits |
1215 v3d->prog.vs->uniform_dirty_bits |
1216 (v3d->prog.gs_bin ?
1217 v3d->prog.gs_bin->uniform_dirty_bits : 0) |
1218 (v3d->prog.gs ?
1219 v3d->prog.gs->uniform_dirty_bits : 0) |
1220 v3d->prog.fs->uniform_dirty_bits)) {
1221 v3d_emit_gl_shader_state(v3d, info);
1222 }
1223
1224 v3d->dirty = 0;
1225
1226 /* The Base Vertex/Base Instance packet sets those values to nonzero
1227 * for the next draw call only.
1228 */
1229 if ((info->index_size && draws->index_bias) || info->start_instance) {
1230 cl_emit(&job->bcl, BASE_VERTEX_BASE_INSTANCE, base) {
1231 base.base_instance = info->start_instance;
1232 base.base_vertex = info->index_size ? draws->index_bias : 0;
1233 }
1234 }
1235
1236 uint32_t prim_tf_enable = 0;
1237
1238 v3d->prim_restart = info->primitive_restart;
1239
1240 if (!v3d->prog.gs && !v3d->prim_restart)
1241 v3d_update_primitives_generated_counter(v3d, info, &draws[0]);
1242
1243 uint32_t hw_prim_type = v3d_hw_prim_type(info->mode);
1244 if (info->index_size) {
1245 uint32_t index_size = info->index_size;
1246 uint32_t offset = draws[0].start * index_size;
1247 struct pipe_resource *prsc;
1248 if (info->has_user_indices) {
1249 unsigned start_offset = draws[0].start * info->index_size;
1250 prsc = NULL;
1251 u_upload_data(v3d->uploader, start_offset,
1252 draws[0].count * info->index_size, 4,
1253 (char*)info->index.user + start_offset,
1254 &offset, &prsc);
1255 } else {
1256 prsc = info->index.resource;
1257 }
1258 struct v3d_resource *rsc = v3d_resource(prsc);
1259
1260 cl_emit(&job->bcl, INDEX_BUFFER_SETUP, ib) {
1261 ib.address = cl_address(rsc->bo, 0);
1262 ib.size = rsc->bo->size;
1263 }
1264
1265 if (indirect && indirect->buffer) {
1266 cl_emit(&job->bcl, INDIRECT_INDEXED_INSTANCED_PRIM_LIST, prim) {
1267 prim.index_type = ffs(info->index_size) - 1;
1268 prim.mode = hw_prim_type | prim_tf_enable;
1269 prim.enable_primitive_restarts = info->primitive_restart;
1270
1271 prim.number_of_draw_indirect_indexed_records = indirect->draw_count;
1272
1273 prim.stride_in_multiples_of_4_bytes = indirect->stride >> 2;
1274 prim.address = cl_address(v3d_resource(indirect->buffer)->bo,
1275 indirect->offset);
1276 }
1277 } else if (info->instance_count > 1) {
1278 cl_emit(&job->bcl, INDEXED_INSTANCED_PRIM_LIST, prim) {
1279 prim.index_type = ffs(info->index_size) - 1;
1280 prim.index_offset = offset;
1281 prim.mode = hw_prim_type | prim_tf_enable;
1282 prim.enable_primitive_restarts = info->primitive_restart;
1283
1284 prim.number_of_instances = info->instance_count;
1285 prim.instance_length = draws[0].count;
1286 }
1287 } else {
1288 cl_emit(&job->bcl, INDEXED_PRIM_LIST, prim) {
1289 prim.index_type = ffs(info->index_size) - 1;
1290 prim.length = draws[0].count;
1291 prim.index_offset = offset;
1292 prim.mode = hw_prim_type | prim_tf_enable;
1293 prim.enable_primitive_restarts = info->primitive_restart;
1294 }
1295 }
1296
1297 if (info->has_user_indices)
1298 pipe_resource_reference(&prsc, NULL);
1299 } else {
1300 if (indirect && indirect->buffer) {
1301 cl_emit(&job->bcl, INDIRECT_VERTEX_ARRAY_INSTANCED_PRIMS, prim) {
1302 prim.mode = hw_prim_type | prim_tf_enable;
1303 prim.number_of_draw_indirect_array_records = indirect->draw_count;
1304
1305 prim.stride_in_multiples_of_4_bytes = indirect->stride >> 2;
1306 prim.address = cl_address(v3d_resource(indirect->buffer)->bo,
1307 indirect->offset);
1308 }
1309 } else if (info->instance_count > 1) {
1310 struct pipe_stream_output_target *so =
1311 indirect && indirect->count_from_stream_output ?
1312 indirect->count_from_stream_output : NULL;
1313 uint32_t vert_count = so ?
1314 v3d_stream_output_target_get_vertex_count(so) :
1315 draws[0].count;
1316 cl_emit(&job->bcl, VERTEX_ARRAY_INSTANCED_PRIMS, prim) {
1317 prim.mode = hw_prim_type | prim_tf_enable;
1318 prim.index_of_first_vertex = draws[0].start;
1319 prim.number_of_instances = info->instance_count;
1320 prim.instance_length = vert_count;
1321 }
1322 } else {
1323 struct pipe_stream_output_target *so =
1324 indirect && indirect->count_from_stream_output ?
1325 indirect->count_from_stream_output : NULL;
1326 uint32_t vert_count = so ?
1327 v3d_stream_output_target_get_vertex_count(so) :
1328 draws[0].count;
1329 cl_emit(&job->bcl, VERTEX_ARRAY_PRIMS, prim) {
1330 prim.mode = hw_prim_type | prim_tf_enable;
1331 prim.length = vert_count;
1332 prim.index_of_first_vertex = draws[0].start;
1333 }
1334 }
1335 }
1336
1337 /* A flush is required in between a TF draw and any following TF specs
1338 * packet, or the GPU may hang. Just flush each time for now.
1339 */
1340 if (v3d->streamout.num_targets)
1341 cl_emit(&job->bcl, TRANSFORM_FEEDBACK_FLUSH_AND_COUNT, flush);
1342
1343 job->draw_calls_queued++;
1344 if (v3d->streamout.num_targets)
1345 job->tf_draw_calls_queued++;
1346
1347 /* Increment the TF offsets by how many verts we wrote. XXX: This
1348 * needs some clamping to the buffer size.
1349 *
1350 * If primitive restart is enabled or we have a geometry shader, we
1351 * update it later, when we can query the device to know how many
1352 * vertices were written.
1353 */
1354 if (!v3d->prog.gs && !v3d->prim_restart) {
1355 for (int i = 0; i < v3d->streamout.num_targets; i++)
1356 v3d_stream_output_target(v3d->streamout.targets[i])->offset +=
1357 u_stream_outputs_for_vertices(info->mode, draws[0].count);
1358 }
1359
1360 if (v3d->zsa && job->zsbuf) {
1361 struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);
1362 if (rsc->invalidated) {
1363 /* Currently gallium only applies invalidates if it
1364 * affects both depth and stencil together.
1365 */
1366 job->invalidated_load |=
1367 PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL;
1368 rsc->invalidated = false;
1369 if (rsc->separate_stencil)
1370 rsc->separate_stencil->invalidated = false;
1371 }
1372 }
1373
1374 uint32_t no_load_mask =
1375 job->clear_tlb | job->clear_draw | job->invalidated_load;
1376 if (v3d->zsa && job->zsbuf && v3d->zsa->base.depth_enabled) {
1377 struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);
1378 v3d_job_add_bo(job, rsc->bo);
1379 job->load |= PIPE_CLEAR_DEPTH & ~no_load_mask;
1380 if (v3d->zsa->base.depth_writemask)
1381 job->store |= PIPE_CLEAR_DEPTH;
1382 rsc->initialized_buffers |= PIPE_CLEAR_DEPTH;
1383 }
1384
1385 if (v3d->zsa && job->zsbuf && v3d->zsa->base.stencil[0].enabled) {
1386 struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);
1387 if (rsc->separate_stencil)
1388 rsc = rsc->separate_stencil;
1389
1390 v3d_job_add_bo(job, rsc->bo);
1391
1392 job->load |= PIPE_CLEAR_STENCIL & ~no_load_mask;
1393 if (v3d->zsa->base.stencil[0].writemask ||
1394 v3d->zsa->base.stencil[1].writemask) {
1395 job->store |= PIPE_CLEAR_STENCIL;
1396 }
1397 rsc->initialized_buffers |= PIPE_CLEAR_STENCIL;
1398 }
1399
1400
1401 for (int i = 0; i < job->nr_cbufs; i++) {
1402 uint32_t bit = PIPE_CLEAR_COLOR0 << i;
1403 int blend_rt = v3d->blend->base.independent_blend_enable ? i : 0;
1404
1405 if (job->store & bit || !job->cbufs[i])
1406 continue;
1407 struct v3d_resource *rsc = v3d_resource(job->cbufs[i]->texture);
1408
1409 if (rsc->invalidated) {
1410 job->invalidated_load |= bit;
1411 rsc->invalidated = false;
1412 } else {
1413 job->load |= bit & ~no_load_mask;
1414 }
1415 if (v3d->blend->base.rt[blend_rt].colormask)
1416 job->store |= bit;
1417 v3d_job_add_bo(job, rsc->bo);
1418 }
1419
1420 if (job->referenced_size > 768 * 1024 * 1024) {
1421 perf_debug("Flushing job with %dkb to try to free up memory\n",
1422 job->referenced_size / 1024);
1423 v3d_flush(pctx);
1424 }
1425
1426 if (V3D_DBG(ALWAYS_FLUSH))
1427 v3d_flush(pctx);
1428 }
1429
1430 static void
v3d_launch_grid(struct pipe_context * pctx,const struct pipe_grid_info * info)1431 v3d_launch_grid(struct pipe_context *pctx, const struct pipe_grid_info *info)
1432 {
1433 struct v3d_context *v3d = v3d_context(pctx);
1434 struct v3d_screen *screen = v3d->screen;
1435 unsigned i;
1436
1437 v3d_predraw_check_stage_inputs(pctx, PIPE_SHADER_COMPUTE);
1438
1439 v3d_update_compiled_cs(v3d);
1440
1441 if (!v3d->prog.compute->resource) {
1442 static bool warned = false;
1443 if (!warned) {
1444 fprintf(stderr,
1445 "Compute shader failed to compile. "
1446 "Expect corruption.\n");
1447 warned = true;
1448 }
1449 return;
1450 }
1451
1452 /* Some of the units of scale:
1453 *
1454 * - Batches of 16 work items (shader invocations) that will be queued
1455 * to the run on a QPU at once.
1456 *
1457 * - Workgroups composed of work items based on the shader's layout
1458 * declaration.
1459 *
1460 * - Supergroups of 1-16 workgroups. There can only be 16 supergroups
1461 * running at a time on the core, so we want to keep them large to
1462 * keep the QPUs busy, but a whole supergroup will sync at a barrier
1463 * so we want to keep them small if one is present.
1464 */
1465 struct drm_v3d_submit_csd submit = { 0 };
1466 struct v3d_job *job = v3d_job_create(v3d);
1467
1468 /* Set up the actual number of workgroups, synchronously mapping the
1469 * indirect buffer if necessary to get the dimensions.
1470 */
1471 if (info->indirect) {
1472 struct pipe_transfer *transfer;
1473 uint32_t *map = pipe_buffer_map_range(pctx, info->indirect,
1474 info->indirect_offset,
1475 3 * sizeof(uint32_t),
1476 PIPE_MAP_READ,
1477 &transfer);
1478 memcpy(v3d->compute_num_workgroups, map, 3 * sizeof(uint32_t));
1479 pipe_buffer_unmap(pctx, transfer);
1480
1481 if (v3d->compute_num_workgroups[0] == 0 ||
1482 v3d->compute_num_workgroups[1] == 0 ||
1483 v3d->compute_num_workgroups[2] == 0) {
1484 /* Nothing to dispatch, so skip the draw (CSD can't
1485 * handle 0 workgroups).
1486 */
1487 return;
1488 }
1489 } else {
1490 v3d->compute_num_workgroups[0] = info->grid[0];
1491 v3d->compute_num_workgroups[1] = info->grid[1];
1492 v3d->compute_num_workgroups[2] = info->grid[2];
1493 }
1494
1495 uint32_t num_wgs = 1;
1496 for (i = 0; i < 3; i++) {
1497 num_wgs *= v3d->compute_num_workgroups[i];
1498 submit.cfg[i] |= (v3d->compute_num_workgroups[i] <<
1499 V3D_CSD_CFG012_WG_COUNT_SHIFT);
1500 }
1501
1502 memcpy(v3d->compute_workgroup_size, info->block, 3 * sizeof(uint32_t));
1503 uint32_t wg_size = info->block[0] * info->block[1] * info->block[2];
1504
1505 struct v3d_compute_prog_data *compute =
1506 v3d->prog.compute->prog_data.compute;
1507 uint32_t wgs_per_sg =
1508 v3d_csd_choose_workgroups_per_supergroup(
1509 &v3d->screen->devinfo,
1510 compute->has_subgroups,
1511 compute->base.has_control_barrier,
1512 compute->base.threads,
1513 num_wgs, wg_size);
1514
1515 uint32_t batches_per_sg = DIV_ROUND_UP(wgs_per_sg * wg_size, 16);
1516 uint32_t whole_sgs = num_wgs / wgs_per_sg;
1517 uint32_t rem_wgs = num_wgs - whole_sgs * wgs_per_sg;
1518 uint32_t num_batches = batches_per_sg * whole_sgs +
1519 DIV_ROUND_UP(rem_wgs * wg_size, 16);
1520
1521 submit.cfg[3] |= (wgs_per_sg & 0xf) << V3D_CSD_CFG3_WGS_PER_SG_SHIFT;
1522 submit.cfg[3] |=
1523 (batches_per_sg - 1) << V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT;
1524 submit.cfg[3] |= (wg_size & 0xff) << V3D_CSD_CFG3_WG_SIZE_SHIFT;
1525
1526
1527 /* Number of batches the dispatch will invoke.
1528 * V3D 7.1.6 and later don't subtract 1 from the number of batches
1529 */
1530 if (v3d->screen->devinfo.ver < 71 ||
1531 (v3d->screen->devinfo.ver == 71 && v3d->screen->devinfo.rev < 6)) {
1532 submit.cfg[4] = num_batches - 1;
1533 } else {
1534 submit.cfg[4] = num_batches;
1535 }
1536
1537 /* Make sure we didn't accidentally underflow. */
1538 assert(submit.cfg[4] != ~0);
1539
1540 v3d_job_add_bo(job, v3d_resource(v3d->prog.compute->resource)->bo);
1541 submit.cfg[5] = (v3d_resource(v3d->prog.compute->resource)->bo->offset +
1542 v3d->prog.compute->offset);
1543 if (v3d->screen->devinfo.ver < 71)
1544 submit.cfg[5] |= V3D_CSD_CFG5_PROPAGATE_NANS;
1545 if (v3d->prog.compute->prog_data.base->single_seg)
1546 submit.cfg[5] |= V3D_CSD_CFG5_SINGLE_SEG;
1547 if (v3d->prog.compute->prog_data.base->threads == 4)
1548 submit.cfg[5] |= V3D_CSD_CFG5_THREADING;
1549
1550 uint32_t shared_size = v3d->prog.compute->prog_data.compute->shared_size +
1551 info->variable_shared_mem;
1552 if (shared_size) {
1553 v3d->compute_shared_memory =
1554 v3d_bo_alloc(v3d->screen,
1555 shared_size * num_wgs,
1556 "shared_vars");
1557 v3d->shared_memory = shared_size;
1558 }
1559
1560 util_dynarray_foreach(&v3d->global_buffers, struct pipe_resource *, res) {
1561 if (!*res)
1562 continue;
1563 struct v3d_resource *rsc = v3d_resource(*res);
1564 v3d_job_add_bo(job, rsc->bo);
1565 }
1566
1567 struct v3d_cl_reloc uniforms = v3d_write_uniforms(v3d, job,
1568 v3d->prog.compute,
1569 PIPE_SHADER_COMPUTE);
1570 v3d_job_add_bo(job, uniforms.bo);
1571 submit.cfg[6] = uniforms.bo->offset + uniforms.offset;
1572
1573 /* Pull some job state that was stored in a SUBMIT_CL struct out to
1574 * our SUBMIT_CSD struct
1575 */
1576 submit.bo_handles = job->submit.bo_handles;
1577 submit.bo_handle_count = job->submit.bo_handle_count;
1578
1579 /* Serialize this in the rest of our command stream. */
1580 submit.in_sync = v3d->out_sync;
1581 submit.out_sync = v3d->out_sync;
1582
1583 if (v3d->active_perfmon) {
1584 assert(screen->has_perfmon);
1585 submit.perfmon_id = v3d->active_perfmon->kperfmon_id;
1586 }
1587
1588 v3d->last_perfmon = v3d->active_perfmon;
1589
1590 if (!V3D_DBG(NORAST)) {
1591 int ret = v3d_ioctl(screen->fd, DRM_IOCTL_V3D_SUBMIT_CSD,
1592 &submit);
1593 static bool warned = false;
1594 if (ret && !warned) {
1595 fprintf(stderr, "CSD submit call returned %s. "
1596 "Expect corruption.\n", strerror(errno));
1597 warned = true;
1598 } else if (!ret) {
1599 if (v3d->active_perfmon)
1600 v3d->active_perfmon->job_submitted = true;
1601 }
1602 }
1603
1604 v3d_job_free(v3d, job);
1605
1606 /* Mark SSBOs as being written.. we don't actually know which ones are
1607 * read vs written, so just assume the worst
1608 */
1609 BITSET_FOREACH_SET(i, v3d->ssbo[PIPE_SHADER_COMPUTE].enabled_mask,
1610 PIPE_MAX_SHADER_BUFFERS) {
1611 struct v3d_resource *rsc = v3d_resource(
1612 v3d->ssbo[PIPE_SHADER_COMPUTE].sb[i].buffer);
1613 rsc->writes++;
1614 rsc->compute_written = true;
1615 }
1616
1617 BITSET_FOREACH_SET(i,
1618 v3d->shaderimg[PIPE_SHADER_COMPUTE].enabled_mask,
1619 PIPE_MAX_SHADER_IMAGES) {
1620 struct v3d_resource *rsc = v3d_resource(
1621 v3d->shaderimg[PIPE_SHADER_COMPUTE].si[i].base.resource);
1622 rsc->writes++;
1623 rsc->compute_written = true;
1624 }
1625
1626 util_dynarray_foreach(&v3d->global_buffers, struct pipe_resource *, res) {
1627 struct v3d_resource *rsc = v3d_resource(*res);
1628 if (!rsc)
1629 continue;
1630 rsc->writes++;
1631 rsc->compute_written = true;
1632 }
1633
1634 v3d_bo_unreference(&uniforms.bo);
1635 v3d_bo_unreference(&v3d->compute_shared_memory);
1636 }
1637
1638 /**
1639 * Implements gallium's clear() hook (glClear()) by drawing a pair of triangles.
1640 */
1641 static void
v3d_draw_clear(struct v3d_context * v3d,struct v3d_job * job,unsigned buffers,const union pipe_color_union * color,double depth,unsigned stencil)1642 v3d_draw_clear(struct v3d_context *v3d,
1643 struct v3d_job *job,
1644 unsigned buffers,
1645 const union pipe_color_union *color,
1646 double depth, unsigned stencil)
1647 {
1648 /* Flag we are clearing these buffers with a draw call so we can
1649 * skip loads for them. Notice that if we had emitted any draw calls
1650 * before this clear the loads will still happen, since those previous
1651 * draw calls would have flagged them.
1652 */
1653 job->clear_draw |= buffers;
1654
1655 v3d_blitter_save(v3d, V3D_CLEAR_COND);
1656 util_blitter_clear(v3d->blitter,
1657 v3d->framebuffer.width,
1658 v3d->framebuffer.height,
1659 util_framebuffer_get_num_layers(&v3d->framebuffer),
1660 buffers, color, depth, stencil,
1661 util_framebuffer_get_num_samples(&v3d->framebuffer) > 1);
1662 }
1663
1664 /**
1665 * Attempts to perform the GL clear by using the TLB's fast clear at the start
1666 * of the frame.
1667 */
1668 static unsigned
v3d_tlb_clear(struct v3d_job * job,unsigned buffers,const union pipe_color_union * color,double depth,unsigned stencil)1669 v3d_tlb_clear(struct v3d_job *job, unsigned buffers,
1670 const union pipe_color_union *color,
1671 double depth, unsigned stencil)
1672 {
1673 struct v3d_context *v3d = job->v3d;
1674
1675 if (job->draw_calls_queued) {
1676 /* If anything in the CL has drawn using the buffer, then the
1677 * TLB clear we're trying to add now would happen before that
1678 * drawing.
1679 */
1680 buffers &= ~(job->load | job->store);
1681 }
1682
1683 /* GFXH-1461: If we were to emit a load of just depth or just stencil,
1684 * then the clear for the other may get lost. We need to decide now
1685 * if it would be possible to need to emit a load of just one after
1686 * we've set up our TLB clears. This issue is fixed since V3D 4.3.18.
1687 */
1688 if (v3d->screen->devinfo.ver == 42 &&
1689 buffers & PIPE_CLEAR_DEPTHSTENCIL &&
1690 (buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL &&
1691 job->zsbuf &&
1692 util_format_is_depth_and_stencil(job->zsbuf->texture->format)) {
1693 buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
1694 }
1695
1696 for (int i = 0; i < job->nr_cbufs; i++) {
1697 uint32_t bit = PIPE_CLEAR_COLOR0 << i;
1698 if (!(buffers & bit))
1699 continue;
1700
1701 struct pipe_surface *psurf = v3d->framebuffer.cbufs[i];
1702 struct v3d_surface *surf = v3d_surface(psurf);
1703 struct v3d_resource *rsc = v3d_resource(psurf->texture);
1704
1705 union util_color uc;
1706 uint32_t internal_size = 4 << surf->internal_bpp;
1707
1708 /* While hardware supports clamping, this is not applied on
1709 * the clear values, so we need to do it manually.
1710 *
1711 * "Clamping is performed on color values immediately as they
1712 * enter the TLB and after blending. Clamping is not
1713 * performed on the clear color."
1714 */
1715 union pipe_color_union clamped_color =
1716 util_clamp_color(psurf->format, color);
1717
1718 if (v3d->swap_color_rb & (1 << i)) {
1719 union pipe_color_union orig_color = clamped_color;
1720 clamped_color.f[0] = orig_color.f[2];
1721 clamped_color.f[1] = orig_color.f[1];
1722 clamped_color.f[2] = orig_color.f[0];
1723 clamped_color.f[3] = orig_color.f[3];
1724 }
1725
1726 if (util_format_is_alpha(psurf->format))
1727 clamped_color.f[0] = clamped_color.f[3];
1728
1729 switch (surf->internal_type) {
1730 case V3D_INTERNAL_TYPE_8:
1731 util_pack_color(clamped_color.f, PIPE_FORMAT_R8G8B8A8_UNORM,
1732 &uc);
1733 memcpy(job->clear_color[i], uc.ui, internal_size);
1734 break;
1735 case V3D_INTERNAL_TYPE_8I:
1736 case V3D_INTERNAL_TYPE_8UI:
1737 job->clear_color[i][0] = ((clamped_color.ui[0] & 0xff) |
1738 (clamped_color.ui[1] & 0xff) << 8 |
1739 (clamped_color.ui[2] & 0xff) << 16 |
1740 (clamped_color.ui[3] & 0xff) << 24);
1741 break;
1742 case V3D_INTERNAL_TYPE_16F:
1743 util_pack_color(clamped_color.f, PIPE_FORMAT_R16G16B16A16_FLOAT,
1744 &uc);
1745 memcpy(job->clear_color[i], uc.ui, internal_size);
1746 break;
1747 case V3D_INTERNAL_TYPE_16I:
1748 case V3D_INTERNAL_TYPE_16UI:
1749 job->clear_color[i][0] = ((clamped_color.ui[0] & 0xffff) |
1750 clamped_color.ui[1] << 16);
1751 job->clear_color[i][1] = ((clamped_color.ui[2] & 0xffff) |
1752 clamped_color.ui[3] << 16);
1753 break;
1754 case V3D_INTERNAL_TYPE_32F:
1755 case V3D_INTERNAL_TYPE_32I:
1756 case V3D_INTERNAL_TYPE_32UI:
1757 memcpy(job->clear_color[i], clamped_color.ui, internal_size);
1758 break;
1759 }
1760
1761 rsc->initialized_buffers |= bit;
1762 }
1763
1764 unsigned zsclear = buffers & PIPE_CLEAR_DEPTHSTENCIL;
1765 if (zsclear) {
1766 struct v3d_resource *rsc =
1767 v3d_resource(v3d->framebuffer.zsbuf->texture);
1768
1769 if (zsclear & PIPE_CLEAR_DEPTH)
1770 job->clear_z = depth;
1771 if (zsclear & PIPE_CLEAR_STENCIL)
1772 job->clear_s = stencil;
1773
1774 rsc->initialized_buffers |= zsclear;
1775 }
1776
1777 job->draw_min_x = 0;
1778 job->draw_min_y = 0;
1779 job->draw_max_x = v3d->framebuffer.width;
1780 job->draw_max_y = v3d->framebuffer.height;
1781 job->clear_tlb |= buffers;
1782 job->store |= buffers;
1783 job->scissor.disabled = true;
1784
1785 v3d_start_draw(v3d);
1786
1787 return buffers;
1788 }
1789
1790 static void
v3d_clear(struct pipe_context * pctx,unsigned buffers,const struct pipe_scissor_state * scissor_state,const union pipe_color_union * color,double depth,unsigned stencil)1791 v3d_clear(struct pipe_context *pctx, unsigned buffers, const struct pipe_scissor_state *scissor_state,
1792 const union pipe_color_union *color, double depth, unsigned stencil)
1793 {
1794 struct v3d_context *v3d = v3d_context(pctx);
1795 struct v3d_job *job = v3d_get_job_for_fbo(v3d);
1796
1797 buffers &= ~v3d_tlb_clear(job, buffers, color, depth, stencil);
1798
1799 if (!buffers || !v3d_render_condition_check(v3d))
1800 return;
1801
1802 v3d_draw_clear(v3d, job, buffers, color, depth, stencil);
1803 }
1804
1805 static void
v3d_clear_render_target(struct pipe_context * pctx,struct pipe_surface * ps,const union pipe_color_union * color,unsigned x,unsigned y,unsigned w,unsigned h,bool render_condition_enabled)1806 v3d_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,
1807 const union pipe_color_union *color,
1808 unsigned x, unsigned y, unsigned w, unsigned h,
1809 bool render_condition_enabled)
1810 {
1811 struct v3d_context *v3d = v3d_context(pctx);
1812
1813 if (render_condition_enabled && !v3d_render_condition_check(v3d))
1814 return;
1815
1816 v3d_blitter_save(v3d, render_condition_enabled ?
1817 V3D_CLEAR_SURFACE_COND : V3D_CLEAR_SURFACE);
1818 util_blitter_clear_render_target(v3d->blitter, ps, color, x, y, w, h);
1819 }
1820
1821 static void
v3d_clear_depth_stencil(struct pipe_context * pctx,struct pipe_surface * ps,unsigned buffers,double depth,unsigned stencil,unsigned x,unsigned y,unsigned w,unsigned h,bool render_condition_enabled)1822 v3d_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,
1823 unsigned buffers, double depth, unsigned stencil,
1824 unsigned x, unsigned y, unsigned w, unsigned h,
1825 bool render_condition_enabled)
1826 {
1827 struct v3d_context *v3d = v3d_context(pctx);
1828
1829 if (render_condition_enabled && !v3d_render_condition_check(v3d))
1830 return;
1831
1832 v3d_blitter_save(v3d, render_condition_enabled ?
1833 V3D_CLEAR_SURFACE_COND : V3D_CLEAR_SURFACE);
1834 util_blitter_clear_depth_stencil(v3d->blitter, ps, buffers, depth,
1835 stencil, x, y, w, h);
1836 }
1837
1838 static void
v3d_set_global_binding(struct pipe_context * pctx,unsigned first,unsigned count,struct pipe_resource ** resources,uint32_t ** handles)1839 v3d_set_global_binding(struct pipe_context *pctx,
1840 unsigned first, unsigned count,
1841 struct pipe_resource **resources,
1842 uint32_t **handles)
1843 {
1844 struct v3d_context *v3d = v3d_context(pctx);
1845 unsigned old_size = util_dynarray_num_elements(&v3d->global_buffers, *resources);
1846
1847 if (old_size < first + count) {
1848 /* we are screwed no matter what */
1849 if (!util_dynarray_grow(&v3d->global_buffers, *resources, (first + count) - old_size))
1850 unreachable("out of memory");
1851
1852 for (unsigned i = old_size; i < first + count; i++)
1853 *util_dynarray_element(&v3d->global_buffers, struct pipe_resource *, i) = NULL;
1854 }
1855
1856
1857 for (unsigned i = first; i < first + count; ++i) {
1858 struct pipe_resource **res = util_dynarray_element(&v3d->global_buffers, struct pipe_resource *, first + i);
1859 if (resources && resources[i]) {
1860 struct v3d_resource *rsc = v3d_resource(resources[i]);
1861 pipe_resource_reference(res, resources[i]);
1862
1863 /* We have to add the base address as there might be an existing offset */
1864 *handles[i] += rsc->bo->offset;
1865 } else {
1866 pipe_resource_reference(res, NULL);
1867 }
1868 }
1869 }
1870
1871 void
v3dX(draw_init)1872 v3dX(draw_init)(struct pipe_context *pctx)
1873 {
1874 pctx->draw_vbo = v3d_draw_vbo;
1875 pctx->clear = v3d_clear;
1876 pctx->clear_render_target = v3d_clear_render_target;
1877 pctx->clear_depth_stencil = v3d_clear_depth_stencil;
1878 if (v3d_context(pctx)->screen->has_csd) {
1879 pctx->launch_grid = v3d_launch_grid;
1880 pctx->set_global_binding = v3d_set_global_binding;
1881 }
1882 }
1883