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