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