v3d: don't try to render if shaders failed to compile
[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 uint32_t
1010 v3d_hw_prim_type(enum pipe_prim_type prim_type)
1011 {
1012 switch (prim_type) {
1013 case PIPE_PRIM_POINTS:
1014 case PIPE_PRIM_LINES:
1015 case PIPE_PRIM_LINE_LOOP:
1016 case PIPE_PRIM_LINE_STRIP:
1017 case PIPE_PRIM_TRIANGLES:
1018 case PIPE_PRIM_TRIANGLE_STRIP:
1019 case PIPE_PRIM_TRIANGLE_FAN:
1020 return prim_type;
1021
1022 case PIPE_PRIM_LINES_ADJACENCY:
1023 case PIPE_PRIM_LINE_STRIP_ADJACENCY:
1024 case PIPE_PRIM_TRIANGLES_ADJACENCY:
1025 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
1026 return 8 + (prim_type - PIPE_PRIM_LINES_ADJACENCY);
1027
1028 default:
1029 unreachable("Unsupported primitive type");
1030 }
1031 }
1032
1033 static bool
1034 v3d_check_compiled_shaders(struct v3d_context *v3d)
1035 {
1036 static bool warned[5] = { 0 };
1037
1038 uint32_t failed_stage = MESA_SHADER_NONE;
1039 if (!v3d->prog.vs->resource || !v3d->prog.cs->resource) {
1040 failed_stage = MESA_SHADER_VERTEX;
1041 } else if ((v3d->prog.gs_bin && !v3d->prog.gs_bin->resource) ||
1042 (v3d->prog.gs && !v3d->prog.gs->resource)) {
1043 failed_stage = MESA_SHADER_GEOMETRY;
1044 } else if (v3d->prog.fs && !v3d->prog.fs->resource) {
1045 failed_stage = MESA_SHADER_FRAGMENT;
1046 }
1047
1048 if (likely(failed_stage == MESA_SHADER_NONE))
1049 return true;
1050
1051 if (!warned[failed_stage]) {
1052 fprintf(stderr,
1053 "%s shader failed to compile. Expect corruption.\n",
1054 _mesa_shader_stage_to_string(failed_stage));
1055 warned[failed_stage] = true;
1056 }
1057 return false;
1058 }
1059
1060 static void
1061 v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
1062 {
1063 struct v3d_context *v3d = v3d_context(pctx);
1064
1065 if (!info->count_from_stream_output && !info->indirect &&
1066 !info->primitive_restart &&
1067 !u_trim_pipe_prim(info->mode, (unsigned*)&info->count))
1068 return;
1069
1070 /* Fall back for weird desktop GL primitive restart values. */
1071 if (info->primitive_restart &&
1072 info->index_size) {
1073 uint32_t mask = ~0;
1074
1075 switch (info->index_size) {
1076 case 2:
1077 mask = 0xffff;
1078 break;
1079 case 1:
1080 mask = 0xff;
1081 break;
1082 }
1083
1084 if (info->restart_index != mask) {
1085 util_draw_vbo_without_prim_restart(pctx, info);
1086 return;
1087 }
1088 }
1089
1090 if (info->mode >= PIPE_PRIM_QUADS && info->mode <= PIPE_PRIM_POLYGON) {
1091 util_primconvert_save_rasterizer_state(v3d->primconvert, &v3d->rasterizer->base);
1092 util_primconvert_draw_vbo(v3d->primconvert, info);
1093 perf_debug("Fallback conversion for %d %s vertices\n",
1094 info->count, u_prim_name(info->mode));
1095 return;
1096 }
1097
1098 /* Before setting up the draw, flush anything writing to the resources
1099 * that we read from or reading from resources we write to.
1100 */
1101 for (int s = 0; s < PIPE_SHADER_COMPUTE; s++)
1102 v3d_predraw_check_stage_inputs(pctx, s);
1103
1104 if (info->indirect) {
1105 v3d_flush_jobs_writing_resource(v3d, info->indirect->buffer,
1106 V3D_FLUSH_DEFAULT);
1107 }
1108
1109 v3d_predraw_check_outputs(pctx);
1110
1111 /* If transform feedback is active and we are switching primitive type
1112 * we need to submit the job before drawing and update the vertex count
1113 * written to TF based on the primitive type since we will need to
1114 * know the exact vertex count if the application decides to call
1115 * glDrawTransformFeedback() later.
1116 */
1117 if (v3d->streamout.num_targets > 0 &&
1118 u_base_prim_type(info->mode) != u_base_prim_type(v3d->prim_mode)) {
1119 v3d_tf_update_counters(v3d);
1120 }
1121
1122 struct v3d_job *job = v3d_get_job_for_fbo(v3d);
1123
1124 /* If vertex texturing depends on the output of rendering, we need to
1125 * ensure that that rendering is complete before we run a coordinate
1126 * shader that depends on it.
1127 *
1128 * Given that doing that is unusual, for now we just block the binner
1129 * on the last submitted render, rather than tracking the last
1130 * rendering to each texture's BO.
1131 */
1132 if (v3d->tex[PIPE_SHADER_VERTEX].num_textures || info->indirect) {
1133 perf_debug("Blocking binner on last render "
1134 "due to vertex texturing or indirect drawing.\n");
1135 job->submit.in_sync_bcl = v3d->out_sync;
1136 }
1137
1138 /* Mark SSBOs and images as being written. We don't actually know
1139 * which ones are read vs written, so just assume the worst.
1140 */
1141 for (int s = 0; s < PIPE_SHADER_COMPUTE; s++) {
1142 foreach_bit(i, v3d->ssbo[s].enabled_mask) {
1143 v3d_job_add_write_resource(job,
1144 v3d->ssbo[s].sb[i].buffer);
1145 job->tmu_dirty_rcl = true;
1146 }
1147
1148 foreach_bit(i, v3d->shaderimg[s].enabled_mask) {
1149 v3d_job_add_write_resource(job,
1150 v3d->shaderimg[s].si[i].base.resource);
1151 job->tmu_dirty_rcl = true;
1152 }
1153 }
1154
1155 /* Get space to emit our draw call into the BCL, using a branch to
1156 * jump to a new BO if necessary.
1157 */
1158 v3d_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);
1159
1160 if (v3d->prim_mode != info->mode) {
1161 v3d->prim_mode = info->mode;
1162 v3d->dirty |= VC5_DIRTY_PRIM_MODE;
1163 }
1164
1165 v3d_start_draw(v3d);
1166 v3d_update_compiled_shaders(v3d, info->mode);
1167 if (!v3d_check_compiled_shaders(v3d))
1168 return;
1169 v3d_update_job_ez(v3d, job);
1170
1171 /* If this job was writing to transform feedback buffers before this
1172 * draw and we are reading from them here, then we need to wait for TF
1173 * to complete before we emit this draw.
1174 *
1175 * Notice this check needs to happen before we emit state for the
1176 * current draw call, where we update job->tf_enabled, so we can ensure
1177 * that we only check TF writes for prior draws.
1178 */
1179 v3d_emit_wait_for_tf_if_needed(v3d, job);
1180
1181 #if V3D_VERSION >= 41
1182 v3d41_emit_state(pctx);
1183 #else
1184 v3d33_emit_state(pctx);
1185 #endif
1186
1187 if (v3d->dirty & (VC5_DIRTY_VTXBUF |
1188 VC5_DIRTY_VTXSTATE |
1189 VC5_DIRTY_PRIM_MODE |
1190 VC5_DIRTY_RASTERIZER |
1191 VC5_DIRTY_COMPILED_CS |
1192 VC5_DIRTY_COMPILED_VS |
1193 VC5_DIRTY_COMPILED_GS_BIN |
1194 VC5_DIRTY_COMPILED_GS |
1195 VC5_DIRTY_COMPILED_FS |
1196 v3d->prog.cs->uniform_dirty_bits |
1197 v3d->prog.vs->uniform_dirty_bits |
1198 (v3d->prog.gs_bin ?
1199 v3d->prog.gs_bin->uniform_dirty_bits : 0) |
1200 (v3d->prog.gs ?
1201 v3d->prog.gs->uniform_dirty_bits : 0) |
1202 v3d->prog.fs->uniform_dirty_bits)) {
1203 v3d_emit_gl_shader_state(v3d, info);
1204 }
1205
1206 v3d->dirty = 0;
1207
1208 /* The Base Vertex/Base Instance packet sets those values to nonzero
1209 * for the next draw call only.
1210 */
1211 if (info->index_bias || info->start_instance) {
1212 cl_emit(&job->bcl, BASE_VERTEX_BASE_INSTANCE, base) {
1213 base.base_instance = info->start_instance;
1214 base.base_vertex = info->index_bias;
1215 }
1216 }
1217
1218 uint32_t prim_tf_enable = 0;
1219 #if V3D_VERSION < 40
1220 /* V3D 3.x: The HW only processes transform feedback on primitives
1221 * with the flag set.
1222 */
1223 if (v3d->streamout.num_targets)
1224 prim_tf_enable = (V3D_PRIM_POINTS_TF - V3D_PRIM_POINTS);
1225 #endif
1226
1227 v3d_update_primitives_generated_counter(v3d, info);
1228
1229 uint32_t hw_prim_type = v3d_hw_prim_type(info->mode);
1230 if (info->index_size) {
1231 uint32_t index_size = info->index_size;
1232 uint32_t offset = info->start * index_size;
1233 struct pipe_resource *prsc;
1234 if (info->has_user_indices) {
1235 prsc = NULL;
1236 u_upload_data(v3d->uploader, 0,
1237 info->count * info->index_size, 4,
1238 info->index.user,
1239 &offset, &prsc);
1240 } else {
1241 prsc = info->index.resource;
1242 }
1243 struct v3d_resource *rsc = v3d_resource(prsc);
1244
1245 #if V3D_VERSION >= 40
1246 cl_emit(&job->bcl, INDEX_BUFFER_SETUP, ib) {
1247 ib.address = cl_address(rsc->bo, 0);
1248 ib.size = rsc->bo->size;
1249 }
1250 #endif
1251
1252 if (info->indirect) {
1253 cl_emit(&job->bcl, INDIRECT_INDEXED_INSTANCED_PRIM_LIST, prim) {
1254 prim.index_type = ffs(info->index_size) - 1;
1255 #if V3D_VERSION < 40
1256 prim.address_of_indices_list =
1257 cl_address(rsc->bo, offset);
1258 #endif /* V3D_VERSION < 40 */
1259 prim.mode = hw_prim_type | prim_tf_enable;
1260 prim.enable_primitive_restarts = info->primitive_restart;
1261
1262 prim.number_of_draw_indirect_indexed_records = info->indirect->draw_count;
1263
1264 prim.stride_in_multiples_of_4_bytes = info->indirect->stride >> 2;
1265 prim.address = cl_address(v3d_resource(info->indirect->buffer)->bo,
1266 info->indirect->offset);
1267 }
1268 } else if (info->instance_count > 1) {
1269 cl_emit(&job->bcl, INDEXED_INSTANCED_PRIM_LIST, prim) {
1270 prim.index_type = ffs(info->index_size) - 1;
1271 #if V3D_VERSION >= 40
1272 prim.index_offset = offset;
1273 #else /* V3D_VERSION < 40 */
1274 prim.maximum_index = (1u << 31) - 1; /* XXX */
1275 prim.address_of_indices_list =
1276 cl_address(rsc->bo, offset);
1277 #endif /* V3D_VERSION < 40 */
1278 prim.mode = hw_prim_type | prim_tf_enable;
1279 prim.enable_primitive_restarts = info->primitive_restart;
1280
1281 prim.number_of_instances = info->instance_count;
1282 prim.instance_length = info->count;
1283 }
1284 } else {
1285 cl_emit(&job->bcl, INDEXED_PRIM_LIST, prim) {
1286 prim.index_type = ffs(info->index_size) - 1;
1287 prim.length = info->count;
1288 #if V3D_VERSION >= 40
1289 prim.index_offset = offset;
1290 #else /* V3D_VERSION < 40 */
1291 prim.maximum_index = (1u << 31) - 1; /* XXX */
1292 prim.address_of_indices_list =
1293 cl_address(rsc->bo, offset);
1294 #endif /* V3D_VERSION < 40 */
1295 prim.mode = hw_prim_type | prim_tf_enable;
1296 prim.enable_primitive_restarts = info->primitive_restart;
1297 }
1298 }
1299
1300 if (info->has_user_indices)
1301 pipe_resource_reference(&prsc, NULL);
1302 } else {
1303 if (info->indirect) {
1304 cl_emit(&job->bcl, INDIRECT_VERTEX_ARRAY_INSTANCED_PRIMS, prim) {
1305 prim.mode = hw_prim_type | prim_tf_enable;
1306 prim.number_of_draw_indirect_array_records = info->indirect->draw_count;
1307
1308 prim.stride_in_multiples_of_4_bytes = info->indirect->stride >> 2;
1309 prim.address = cl_address(v3d_resource(info->indirect->buffer)->bo,
1310 info->indirect->offset);
1311 }
1312 } else if (info->instance_count > 1) {
1313 struct pipe_stream_output_target *so =
1314 info->count_from_stream_output;
1315 uint32_t vert_count = so ?
1316 v3d_stream_output_target_get_vertex_count(so) :
1317 info->count;
1318 cl_emit(&job->bcl, VERTEX_ARRAY_INSTANCED_PRIMS, prim) {
1319 prim.mode = hw_prim_type | prim_tf_enable;
1320 prim.index_of_first_vertex = info->start;
1321 prim.number_of_instances = info->instance_count;
1322 prim.instance_length = vert_count;
1323 }
1324 } else {
1325 struct pipe_stream_output_target *so =
1326 info->count_from_stream_output;
1327 uint32_t vert_count = so ?
1328 v3d_stream_output_target_get_vertex_count(so) :
1329 info->count;
1330 cl_emit(&job->bcl, VERTEX_ARRAY_PRIMS, prim) {
1331 prim.mode = hw_prim_type | prim_tf_enable;
1332 prim.length = vert_count;
1333 prim.index_of_first_vertex = info->start;
1334 }
1335 }
1336 }
1337
1338 /* A flush is required in between a TF draw and any following TF specs
1339 * packet, or the GPU may hang. Just flush each time for now.
1340 */
1341 if (v3d->streamout.num_targets)
1342 cl_emit(&job->bcl, TRANSFORM_FEEDBACK_FLUSH_AND_COUNT, flush);
1343
1344 job->draw_calls_queued++;
1345 if (v3d->streamout.num_targets)
1346 job->tf_draw_calls_queued++;
1347
1348 /* Increment the TF offsets by how many verts we wrote. XXX: This
1349 * needs some clamping to the buffer size.
1350 */
1351 for (int i = 0; i < v3d->streamout.num_targets; i++)
1352 v3d->streamout.offsets[i] += info->count;
1353
1354 if (v3d->zsa && job->zsbuf && v3d->zsa->base.depth.enabled) {
1355 struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);
1356 v3d_job_add_bo(job, rsc->bo);
1357
1358 job->load |= PIPE_CLEAR_DEPTH & ~job->clear;
1359 if (v3d->zsa->base.depth.writemask)
1360 job->store |= PIPE_CLEAR_DEPTH;
1361 rsc->initialized_buffers = PIPE_CLEAR_DEPTH;
1362 }
1363
1364 if (v3d->zsa && job->zsbuf && v3d->zsa->base.stencil[0].enabled) {
1365 struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);
1366 if (rsc->separate_stencil)
1367 rsc = rsc->separate_stencil;
1368
1369 v3d_job_add_bo(job, rsc->bo);
1370
1371 job->load |= PIPE_CLEAR_STENCIL & ~job->clear;
1372 if (v3d->zsa->base.stencil[0].writemask ||
1373 v3d->zsa->base.stencil[1].writemask) {
1374 job->store |= PIPE_CLEAR_STENCIL;
1375 }
1376 rsc->initialized_buffers |= PIPE_CLEAR_STENCIL;
1377 }
1378
1379 for (int i = 0; i < V3D_MAX_DRAW_BUFFERS; i++) {
1380 uint32_t bit = PIPE_CLEAR_COLOR0 << i;
1381 int blend_rt = v3d->blend->base.independent_blend_enable ? i : 0;
1382
1383 if (job->store & bit || !job->cbufs[i])
1384 continue;
1385 struct v3d_resource *rsc = v3d_resource(job->cbufs[i]->texture);
1386
1387 job->load |= bit & ~job->clear;
1388 if (v3d->blend->base.rt[blend_rt].colormask)
1389 job->store |= bit;
1390 v3d_job_add_bo(job, rsc->bo);
1391 }
1392
1393 if (job->referenced_size > 768 * 1024 * 1024) {
1394 perf_debug("Flushing job with %dkb to try to free up memory\n",
1395 job->referenced_size / 1024);
1396 v3d_flush(pctx);
1397 }
1398
1399 if (V3D_DEBUG & V3D_DEBUG_ALWAYS_FLUSH)
1400 v3d_flush(pctx);
1401 }
1402
1403 #if V3D_VERSION >= 41
1404 #define V3D_CSD_CFG012_WG_COUNT_SHIFT 16
1405 #define V3D_CSD_CFG012_WG_OFFSET_SHIFT 0
1406 /* Allow this dispatch to start while the last one is still running. */
1407 #define V3D_CSD_CFG3_OVERLAP_WITH_PREV (1 << 26)
1408 /* Maximum supergroup ID. 6 bits. */
1409 #define V3D_CSD_CFG3_MAX_SG_ID_SHIFT 20
1410 /* Batches per supergroup minus 1. 8 bits. */
1411 #define V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT 12
1412 /* Workgroups per supergroup, 0 means 16 */
1413 #define V3D_CSD_CFG3_WGS_PER_SG_SHIFT 8
1414 #define V3D_CSD_CFG3_WG_SIZE_SHIFT 0
1415
1416 #define V3D_CSD_CFG5_PROPAGATE_NANS (1 << 2)
1417 #define V3D_CSD_CFG5_SINGLE_SEG (1 << 1)
1418 #define V3D_CSD_CFG5_THREADING (1 << 0)
1419
1420 static void
1421 v3d_launch_grid(struct pipe_context *pctx, const struct pipe_grid_info *info)
1422 {
1423 struct v3d_context *v3d = v3d_context(pctx);
1424 struct v3d_screen *screen = v3d->screen;
1425
1426 v3d_predraw_check_stage_inputs(pctx, PIPE_SHADER_COMPUTE);
1427
1428 v3d_update_compiled_cs(v3d);
1429
1430 if (!v3d->prog.compute->resource) {
1431 static bool warned = false;
1432 if (!warned) {
1433 fprintf(stderr,
1434 "Compute shader failed to compile. "
1435 "Expect corruption.\n");
1436 warned = true;
1437 }
1438 return;
1439 }
1440
1441 /* Some of the units of scale:
1442 *
1443 * - Batches of 16 work items (shader invocations) that will be queued
1444 * to the run on a QPU at once.
1445 *
1446 * - Workgroups composed of work items based on the shader's layout
1447 * declaration.
1448 *
1449 * - Supergroups of 1-16 workgroups. There can only be 16 supergroups
1450 * running at a time on the core, so we want to keep them large to
1451 * keep the QPUs busy, but a whole supergroup will sync at a barrier
1452 * so we want to keep them small if one is present.
1453 */
1454 struct drm_v3d_submit_csd submit = { 0 };
1455 struct v3d_job *job = v3d_job_create(v3d);
1456
1457 /* Set up the actual number of workgroups, synchronously mapping the
1458 * indirect buffer if necessary to get the dimensions.
1459 */
1460 if (info->indirect) {
1461 struct pipe_transfer *transfer;
1462 uint32_t *map = pipe_buffer_map_range(pctx, info->indirect,
1463 info->indirect_offset,
1464 3 * sizeof(uint32_t),
1465 PIPE_TRANSFER_READ,
1466 &transfer);
1467 memcpy(v3d->compute_num_workgroups, map, 3 * sizeof(uint32_t));
1468 pipe_buffer_unmap(pctx, transfer);
1469
1470 if (v3d->compute_num_workgroups[0] == 0 ||
1471 v3d->compute_num_workgroups[1] == 0 ||
1472 v3d->compute_num_workgroups[2] == 0) {
1473 /* Nothing to dispatch, so skip the draw (CSD can't
1474 * handle 0 workgroups).
1475 */
1476 return;
1477 }
1478 } else {
1479 v3d->compute_num_workgroups[0] = info->grid[0];
1480 v3d->compute_num_workgroups[1] = info->grid[1];
1481 v3d->compute_num_workgroups[2] = info->grid[2];
1482 }
1483
1484 for (int i = 0; i < 3; i++) {
1485 submit.cfg[i] |= (v3d->compute_num_workgroups[i] <<
1486 V3D_CSD_CFG012_WG_COUNT_SHIFT);
1487 }
1488
1489 perf_debug("CSD only using single WG per SG currently, "
1490 "should increase that when possible.");
1491 int wgs_per_sg = 1;
1492 int wg_size = info->block[0] * info->block[1] * info->block[2];
1493 submit.cfg[3] |= wgs_per_sg << V3D_CSD_CFG3_WGS_PER_SG_SHIFT;
1494 submit.cfg[3] |= ((DIV_ROUND_UP(wgs_per_sg * wg_size, 16) - 1) <<
1495 V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT);
1496 submit.cfg[3] |= (wg_size & 0xff) << V3D_CSD_CFG3_WG_SIZE_SHIFT;
1497
1498 int batches_per_wg = DIV_ROUND_UP(wg_size, 16);
1499 /* Number of batches the dispatch will invoke (minus 1). */
1500 submit.cfg[4] = batches_per_wg * (v3d->compute_num_workgroups[0] *
1501 v3d->compute_num_workgroups[1] *
1502 v3d->compute_num_workgroups[2]) - 1;
1503
1504 /* Make sure we didn't accidentally underflow. */
1505 assert(submit.cfg[4] != ~0);
1506
1507 v3d_job_add_bo(job, v3d_resource(v3d->prog.compute->resource)->bo);
1508 submit.cfg[5] = (v3d_resource(v3d->prog.compute->resource)->bo->offset +
1509 v3d->prog.compute->offset);
1510 submit.cfg[5] |= V3D_CSD_CFG5_PROPAGATE_NANS;
1511 if (v3d->prog.compute->prog_data.base->single_seg)
1512 submit.cfg[5] |= V3D_CSD_CFG5_SINGLE_SEG;
1513 if (v3d->prog.compute->prog_data.base->threads == 4)
1514 submit.cfg[5] |= V3D_CSD_CFG5_THREADING;
1515
1516 if (v3d->prog.compute->prog_data.compute->shared_size) {
1517 v3d->compute_shared_memory =
1518 v3d_bo_alloc(v3d->screen,
1519 v3d->prog.compute->prog_data.compute->shared_size *
1520 wgs_per_sg,
1521 "shared_vars");
1522 }
1523
1524 struct v3d_cl_reloc uniforms = v3d_write_uniforms(v3d, job,
1525 v3d->prog.compute,
1526 PIPE_SHADER_COMPUTE);
1527 v3d_job_add_bo(job, uniforms.bo);
1528 submit.cfg[6] = uniforms.bo->offset + uniforms.offset;
1529
1530 /* Pull some job state that was stored in a SUBMIT_CL struct out to
1531 * our SUBMIT_CSD struct
1532 */
1533 submit.bo_handles = job->submit.bo_handles;
1534 submit.bo_handle_count = job->submit.bo_handle_count;
1535
1536 /* Serialize this in the rest of our command stream. */
1537 submit.in_sync = v3d->out_sync;
1538 submit.out_sync = v3d->out_sync;
1539
1540 if (!(V3D_DEBUG & V3D_DEBUG_NORAST)) {
1541 int ret = v3d_ioctl(screen->fd, DRM_IOCTL_V3D_SUBMIT_CSD,
1542 &submit);
1543 static bool warned = false;
1544 if (ret && !warned) {
1545 fprintf(stderr, "CSD submit call returned %s. "
1546 "Expect corruption.\n", strerror(errno));
1547 warned = true;
1548 }
1549 }
1550
1551 v3d_job_free(v3d, job);
1552
1553 /* Mark SSBOs as being written.. we don't actually know which ones are
1554 * read vs written, so just assume the worst
1555 */
1556 foreach_bit(i, v3d->ssbo[PIPE_SHADER_COMPUTE].enabled_mask) {
1557 struct v3d_resource *rsc = v3d_resource(
1558 v3d->ssbo[PIPE_SHADER_COMPUTE].sb[i].buffer);
1559 rsc->writes++; /* XXX */
1560 }
1561
1562 foreach_bit(i, v3d->shaderimg[PIPE_SHADER_COMPUTE].enabled_mask) {
1563 struct v3d_resource *rsc = v3d_resource(
1564 v3d->shaderimg[PIPE_SHADER_COMPUTE].si[i].base.resource);
1565 rsc->writes++;
1566 }
1567
1568 v3d_bo_unreference(&uniforms.bo);
1569 v3d_bo_unreference(&v3d->compute_shared_memory);
1570 }
1571 #endif
1572
1573 /**
1574 * Implements gallium's clear() hook (glClear()) by drawing a pair of triangles.
1575 */
1576 static void
1577 v3d_draw_clear(struct v3d_context *v3d,
1578 unsigned buffers,
1579 const union pipe_color_union *color,
1580 double depth, unsigned stencil)
1581 {
1582 static const union pipe_color_union dummy_color = {};
1583
1584 /* The blitter util dereferences the color regardless, even though the
1585 * gallium clear API may not pass one in when only Z/S are cleared.
1586 */
1587 if (!color)
1588 color = &dummy_color;
1589
1590 v3d_blitter_save(v3d);
1591 util_blitter_clear(v3d->blitter,
1592 v3d->framebuffer.width,
1593 v3d->framebuffer.height,
1594 util_framebuffer_get_num_layers(&v3d->framebuffer),
1595 buffers, color, depth, stencil,
1596 util_framebuffer_get_num_samples(&v3d->framebuffer) > 1);
1597 }
1598
1599 /**
1600 * Attempts to perform the GL clear by using the TLB's fast clear at the start
1601 * of the frame.
1602 */
1603 static unsigned
1604 v3d_tlb_clear(struct v3d_job *job, unsigned buffers,
1605 const union pipe_color_union *color,
1606 double depth, unsigned stencil)
1607 {
1608 struct v3d_context *v3d = job->v3d;
1609
1610 if (job->draw_calls_queued) {
1611 /* If anything in the CL has drawn using the buffer, then the
1612 * TLB clear we're trying to add now would happen before that
1613 * drawing.
1614 */
1615 buffers &= ~(job->load | job->store);
1616 }
1617
1618 /* GFXH-1461: If we were to emit a load of just depth or just stencil,
1619 * then the clear for the other may get lost. We need to decide now
1620 * if it would be possible to need to emit a load of just one after
1621 * we've set up our TLB clears.
1622 */
1623 if (buffers & PIPE_CLEAR_DEPTHSTENCIL &&
1624 (buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL &&
1625 job->zsbuf &&
1626 util_format_is_depth_and_stencil(job->zsbuf->texture->format)) {
1627 buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
1628 }
1629
1630 for (int i = 0; i < V3D_MAX_DRAW_BUFFERS; i++) {
1631 uint32_t bit = PIPE_CLEAR_COLOR0 << i;
1632 if (!(buffers & bit))
1633 continue;
1634
1635 struct pipe_surface *psurf = v3d->framebuffer.cbufs[i];
1636 struct v3d_surface *surf = v3d_surface(psurf);
1637 struct v3d_resource *rsc = v3d_resource(psurf->texture);
1638
1639 union util_color uc;
1640 uint32_t internal_size = 4 << surf->internal_bpp;
1641
1642 static union pipe_color_union swapped_color;
1643 if (v3d->swap_color_rb & (1 << i)) {
1644 swapped_color.f[0] = color->f[2];
1645 swapped_color.f[1] = color->f[1];
1646 swapped_color.f[2] = color->f[0];
1647 swapped_color.f[3] = color->f[3];
1648 color = &swapped_color;
1649 }
1650
1651 switch (surf->internal_type) {
1652 case V3D_INTERNAL_TYPE_8:
1653 util_pack_color(color->f, PIPE_FORMAT_R8G8B8A8_UNORM,
1654 &uc);
1655 memcpy(job->clear_color[i], uc.ui, internal_size);
1656 break;
1657 case V3D_INTERNAL_TYPE_8I:
1658 case V3D_INTERNAL_TYPE_8UI:
1659 job->clear_color[i][0] = ((color->ui[0] & 0xff) |
1660 (color->ui[1] & 0xff) << 8 |
1661 (color->ui[2] & 0xff) << 16 |
1662 (color->ui[3] & 0xff) << 24);
1663 break;
1664 case V3D_INTERNAL_TYPE_16F:
1665 util_pack_color(color->f, PIPE_FORMAT_R16G16B16A16_FLOAT,
1666 &uc);
1667 memcpy(job->clear_color[i], uc.ui, internal_size);
1668 break;
1669 case V3D_INTERNAL_TYPE_16I:
1670 case V3D_INTERNAL_TYPE_16UI:
1671 job->clear_color[i][0] = ((color->ui[0] & 0xffff) |
1672 color->ui[1] << 16);
1673 job->clear_color[i][1] = ((color->ui[2] & 0xffff) |
1674 color->ui[3] << 16);
1675 break;
1676 case V3D_INTERNAL_TYPE_32F:
1677 case V3D_INTERNAL_TYPE_32I:
1678 case V3D_INTERNAL_TYPE_32UI:
1679 memcpy(job->clear_color[i], color->ui, internal_size);
1680 break;
1681 }
1682
1683 rsc->initialized_buffers |= bit;
1684 }
1685
1686 unsigned zsclear = buffers & PIPE_CLEAR_DEPTHSTENCIL;
1687 if (zsclear) {
1688 struct v3d_resource *rsc =
1689 v3d_resource(v3d->framebuffer.zsbuf->texture);
1690
1691 if (zsclear & PIPE_CLEAR_DEPTH)
1692 job->clear_z = depth;
1693 if (zsclear & PIPE_CLEAR_STENCIL)
1694 job->clear_s = stencil;
1695
1696 rsc->initialized_buffers |= zsclear;
1697 }
1698
1699 job->draw_min_x = 0;
1700 job->draw_min_y = 0;
1701 job->draw_max_x = v3d->framebuffer.width;
1702 job->draw_max_y = v3d->framebuffer.height;
1703 job->clear |= buffers;
1704 job->store |= buffers;
1705
1706 v3d_start_draw(v3d);
1707
1708 return buffers;
1709 }
1710
1711 static void
1712 v3d_clear(struct pipe_context *pctx, unsigned buffers,
1713 const union pipe_color_union *color, double depth, unsigned stencil)
1714 {
1715 struct v3d_context *v3d = v3d_context(pctx);
1716 struct v3d_job *job = v3d_get_job_for_fbo(v3d);
1717
1718 buffers &= ~v3d_tlb_clear(job, buffers, color, depth, stencil);
1719
1720 if (buffers)
1721 v3d_draw_clear(v3d, buffers, color, depth, stencil);
1722 }
1723
1724 static void
1725 v3d_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,
1726 const union pipe_color_union *color,
1727 unsigned x, unsigned y, unsigned w, unsigned h,
1728 bool render_condition_enabled)
1729 {
1730 fprintf(stderr, "unimpl: clear RT\n");
1731 }
1732
1733 static void
1734 v3d_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,
1735 unsigned buffers, double depth, unsigned stencil,
1736 unsigned x, unsigned y, unsigned w, unsigned h,
1737 bool render_condition_enabled)
1738 {
1739 fprintf(stderr, "unimpl: clear DS\n");
1740 }
1741
1742 void
1743 v3dX(draw_init)(struct pipe_context *pctx)
1744 {
1745 pctx->draw_vbo = v3d_draw_vbo;
1746 pctx->clear = v3d_clear;
1747 pctx->clear_render_target = v3d_clear_render_target;
1748 pctx->clear_depth_stencil = v3d_clear_depth_stencil;
1749 #if V3D_VERSION >= 41
1750 if (v3d_context(pctx)->screen->has_csd)
1751 pctx->launch_grid = v3d_launch_grid;
1752 #endif
1753 }