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