v3d: emit geometry shader state commands
[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 #if V3D_VERSION >= 41
332 static void
333 v3d_emit_gs_state_record(struct v3d_job *job,
334 struct v3d_compiled_shader *gs_bin,
335 struct v3d_cl_reloc gs_bin_uniforms,
336 struct v3d_compiled_shader *gs,
337 struct v3d_cl_reloc gs_render_uniforms)
338 {
339 cl_emit(&job->indirect, GEOMETRY_SHADER_STATE_RECORD, shader) {
340 shader.geometry_bin_mode_shader_code_address =
341 cl_address(v3d_resource(gs_bin->resource)->bo,
342 gs_bin->offset);
343 shader.geometry_bin_mode_shader_4_way_threadable =
344 gs_bin->prog_data.gs->base.threads == 4;
345 shader.geometry_bin_mode_shader_start_in_final_thread_section =
346 gs_bin->prog_data.gs->base.single_seg;
347 shader.geometry_bin_mode_shader_propagate_nans = true;
348 shader.geometry_bin_mode_shader_uniforms_address =
349 gs_bin_uniforms;
350
351 shader.geometry_render_mode_shader_code_address =
352 cl_address(v3d_resource(gs->resource)->bo, gs->offset);
353 shader.geometry_render_mode_shader_4_way_threadable =
354 gs->prog_data.gs->base.threads == 4;
355 shader.geometry_render_mode_shader_start_in_final_thread_section =
356 gs->prog_data.gs->base.single_seg;
357 shader.geometry_render_mode_shader_propagate_nans = true;
358 shader.geometry_render_mode_shader_uniforms_address =
359 gs_render_uniforms;
360 }
361 }
362
363 static uint8_t
364 v3d_gs_output_primitive(uint32_t prim_type)
365 {
366 switch (prim_type) {
367 case GL_POINTS:
368 return GEOMETRY_SHADER_POINTS;
369 case GL_LINE_STRIP:
370 return GEOMETRY_SHADER_LINE_STRIP;
371 case GL_TRIANGLE_STRIP:
372 return GEOMETRY_SHADER_TRI_STRIP;
373 default:
374 unreachable("Unsupported primitive type");
375 }
376 }
377
378 static void
379 v3d_emit_tes_gs_common_params(struct v3d_job *job,
380 uint8_t gs_out_prim_type)
381 {
382 /* This, and v3d_emit_tes_gs_shader_params below, fill in default
383 * values for tessellation fields even though we don't support
384 * tessellation yet because our packing functions (and the simulator)
385 * complain if we don't.
386 */
387 cl_emit(&job->indirect, TESSELLATION_GEOMETRY_COMMON_PARAMS, shader) {
388 shader.tessellation_type = TESSELLATION_TYPE_TRIANGLE;
389 shader.tessellation_point_mode = false;
390 shader.tessellation_edge_spacing = TESSELLATION_EDGE_SPACING_EVEN;
391 shader.tessellation_clockwise = true;
392 shader.tessellation_invocations = 1;
393
394 shader.geometry_shader_output_format =
395 v3d_gs_output_primitive(gs_out_prim_type);
396 shader.geometry_shader_instances = 1; /* FIXME */
397 }
398 }
399
400 static void
401 v3d_emit_tes_gs_shader_params(struct v3d_job *job,
402 struct v3d_gs_prog_data *gs)
403 {
404 cl_emit(&job->indirect, TESSELLATION_GEOMETRY_SHADER_PARAMS, shader) {
405 shader.tcs_batch_flush_mode = V3D_TCS_FLUSH_MODE_FULLY_PACKED;
406 shader.per_patch_data_column_depth = 1;
407 shader.tcs_output_segment_size_in_sectors = 1;
408 shader.tcs_output_segment_pack_mode = V3D_PACK_MODE_16_WAY;
409 shader.tes_output_segment_size_in_sectors = 1;
410 shader.tes_output_segment_pack_mode = V3D_PACK_MODE_16_WAY;
411 shader.gs_output_segment_size_in_sectors =
412 gs->vpm_output_size;
413 shader.gs_output_segment_pack_mode = V3D_PACK_MODE_16_WAY; /* FIXME*/
414 shader.tbg_max_patches_per_tcs_batch = 1;
415 shader.tbg_max_extra_vertex_segs_for_patches_after_first = 0;
416 shader.tbg_min_tcs_output_segments_required_in_play = 1;
417 shader.tbg_min_per_patch_data_segments_required_in_play = 1;
418 shader.tpg_max_patches_per_tes_batch = 1;
419 shader.tpg_max_vertex_segments_per_tes_batch = 0;
420 shader.tpg_max_tcs_output_segments_per_tes_batch = 1;
421 shader.tpg_min_tes_output_segments_required_in_play = 1;
422 shader.gbg_max_tes_output_vertex_segments_per_gs_batch = 0;
423 shader.gbg_min_gs_output_segments_required_in_play = 1;
424 }
425 }
426
427 #endif
428
429 static void
430 v3d_emit_gl_shader_state(struct v3d_context *v3d,
431 const struct pipe_draw_info *info)
432 {
433 struct v3d_job *job = v3d->job;
434 /* VC5_DIRTY_VTXSTATE */
435 struct v3d_vertex_stateobj *vtx = v3d->vtx;
436 /* VC5_DIRTY_VTXBUF */
437 struct v3d_vertexbuf_stateobj *vertexbuf = &v3d->vertexbuf;
438
439 /* Upload the uniforms to the indirect CL first */
440 struct v3d_cl_reloc fs_uniforms =
441 v3d_write_uniforms(v3d, job, v3d->prog.fs,
442 PIPE_SHADER_FRAGMENT);
443
444 struct v3d_cl_reloc gs_uniforms = { NULL, 0 };
445 struct v3d_cl_reloc gs_bin_uniforms = { NULL, 0 };
446 if (v3d->prog.gs) {
447 gs_uniforms = v3d_write_uniforms(v3d, job, v3d->prog.gs,
448 PIPE_SHADER_GEOMETRY);
449 }
450 if (v3d->prog.gs_bin) {
451 gs_bin_uniforms = v3d_write_uniforms(v3d, job, v3d->prog.gs_bin,
452 PIPE_SHADER_GEOMETRY);
453 }
454
455 struct v3d_cl_reloc vs_uniforms =
456 v3d_write_uniforms(v3d, job, v3d->prog.vs,
457 PIPE_SHADER_VERTEX);
458 struct v3d_cl_reloc cs_uniforms =
459 v3d_write_uniforms(v3d, job, v3d->prog.cs,
460 PIPE_SHADER_VERTEX);
461
462 /* Update the cache dirty flag based on the shader progs data */
463 job->tmu_dirty_rcl |= v3d->prog.cs->prog_data.vs->base.tmu_dirty_rcl;
464 job->tmu_dirty_rcl |= v3d->prog.vs->prog_data.vs->base.tmu_dirty_rcl;
465 if (v3d->prog.gs_bin) {
466 job->tmu_dirty_rcl |=
467 v3d->prog.gs_bin->prog_data.gs->base.tmu_dirty_rcl;
468 }
469 if (v3d->prog.gs) {
470 job->tmu_dirty_rcl |=
471 v3d->prog.gs->prog_data.gs->base.tmu_dirty_rcl;
472 }
473 job->tmu_dirty_rcl |= v3d->prog.fs->prog_data.fs->base.tmu_dirty_rcl;
474
475 /* See GFXH-930 workaround below */
476 uint32_t num_elements_to_emit = MAX2(vtx->num_elements, 1);
477
478 uint32_t shader_state_record_length =
479 cl_packet_length(GL_SHADER_STATE_RECORD);
480 #if V3D_VERSION >= 41
481 if (v3d->prog.gs) {
482 shader_state_record_length +=
483 cl_packet_length(GEOMETRY_SHADER_STATE_RECORD) +
484 cl_packet_length(TESSELLATION_GEOMETRY_COMMON_PARAMS) +
485 2 * cl_packet_length(TESSELLATION_GEOMETRY_SHADER_PARAMS);
486 }
487 #endif
488
489 uint32_t shader_rec_offset =
490 v3d_cl_ensure_space(&job->indirect,
491 shader_state_record_length +
492 num_elements_to_emit *
493 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD),
494 32);
495
496 /* XXX perf: We should move most of the SHADER_STATE_RECORD setup to
497 * compile time, so that we mostly just have to OR the VS and FS
498 * records together at draw time.
499 */
500 #if V3D_VERSION >= 41
501 if (v3d->prog.gs) {
502 v3d_emit_gs_state_record(v3d->job,
503 v3d->prog.gs_bin, gs_bin_uniforms,
504 v3d->prog.gs, gs_uniforms);
505
506 struct v3d_gs_prog_data *gs = v3d->prog.gs->prog_data.gs;
507 struct v3d_gs_prog_data *gs_bin = v3d->prog.gs_bin->prog_data.gs;
508
509 v3d_emit_tes_gs_common_params(v3d->job, gs->out_prim_type);
510 v3d_emit_tes_gs_shader_params(v3d->job, gs_bin);
511 v3d_emit_tes_gs_shader_params(v3d->job, gs);
512 }
513 #endif
514
515 cl_emit(&job->indirect, GL_SHADER_STATE_RECORD, shader) {
516 shader.enable_clipping = true;
517 /* VC5_DIRTY_PRIM_MODE | VC5_DIRTY_RASTERIZER */
518 shader.point_size_in_shaded_vertex_data =
519 (info->mode == PIPE_PRIM_POINTS &&
520 v3d->rasterizer->base.point_size_per_vertex);
521
522 /* Must be set if the shader modifies Z, discards, or modifies
523 * the sample mask. For any of these cases, the fragment
524 * shader needs to write the Z value (even just discards).
525 */
526 shader.fragment_shader_does_z_writes =
527 v3d->prog.fs->prog_data.fs->writes_z;
528 /* Set if the EZ test must be disabled (due to shader side
529 * effects and the early_z flag not being present in the
530 * shader).
531 */
532 shader.turn_off_early_z_test =
533 v3d->prog.fs->prog_data.fs->disable_ez;
534
535 shader.fragment_shader_uses_real_pixel_centre_w_in_addition_to_centroid_w2 =
536 v3d->prog.fs->prog_data.fs->uses_center_w;
537
538 #if V3D_VERSION >= 41
539 shader.any_shader_reads_hardware_written_primitive_id =
540 v3d->prog.gs ? v3d->prog.gs->prog_data.gs->uses_pid :
541 false;
542 #endif
543
544 #if V3D_VERSION >= 40
545 shader.do_scoreboard_wait_on_first_thread_switch =
546 v3d->prog.fs->prog_data.fs->lock_scoreboard_on_first_thrsw;
547 shader.disable_implicit_point_line_varyings =
548 !v3d->prog.fs->prog_data.fs->uses_implicit_point_line_varyings;
549 #endif
550
551 shader.number_of_varyings_in_fragment_shader =
552 v3d->prog.fs->prog_data.fs->num_inputs;
553
554 shader.coordinate_shader_propagate_nans = true;
555 shader.vertex_shader_propagate_nans = true;
556 shader.fragment_shader_propagate_nans = true;
557
558 shader.coordinate_shader_code_address =
559 cl_address(v3d_resource(v3d->prog.cs->resource)->bo,
560 v3d->prog.cs->offset);
561 shader.vertex_shader_code_address =
562 cl_address(v3d_resource(v3d->prog.vs->resource)->bo,
563 v3d->prog.vs->offset);
564 shader.fragment_shader_code_address =
565 cl_address(v3d_resource(v3d->prog.fs->resource)->bo,
566 v3d->prog.fs->offset);
567
568 /* XXX: Use combined input/output size flag in the common
569 * case.
570 */
571 shader.coordinate_shader_has_separate_input_and_output_vpm_blocks =
572 v3d->prog.cs->prog_data.vs->separate_segments;
573 shader.vertex_shader_has_separate_input_and_output_vpm_blocks =
574 v3d->prog.vs->prog_data.vs->separate_segments;
575
576 shader.coordinate_shader_input_vpm_segment_size =
577 v3d->prog.cs->prog_data.vs->separate_segments ?
578 v3d->prog.cs->prog_data.vs->vpm_input_size : 1;
579 shader.vertex_shader_input_vpm_segment_size =
580 v3d->prog.vs->prog_data.vs->separate_segments ?
581 v3d->prog.vs->prog_data.vs->vpm_input_size : 1;
582
583 shader.coordinate_shader_output_vpm_segment_size =
584 v3d->prog.cs->prog_data.vs->vpm_output_size;
585 shader.vertex_shader_output_vpm_segment_size =
586 v3d->prog.vs->prog_data.vs->vpm_output_size;
587
588 shader.coordinate_shader_uniforms_address = cs_uniforms;
589 shader.vertex_shader_uniforms_address = vs_uniforms;
590 shader.fragment_shader_uniforms_address = fs_uniforms;
591
592 #if V3D_VERSION >= 41
593 shader.min_coord_shader_input_segments_required_in_play = 1;
594 shader.min_vertex_shader_input_segments_required_in_play = 1;
595
596 shader.coordinate_shader_4_way_threadable =
597 v3d->prog.cs->prog_data.vs->base.threads == 4;
598 shader.vertex_shader_4_way_threadable =
599 v3d->prog.vs->prog_data.vs->base.threads == 4;
600 shader.fragment_shader_4_way_threadable =
601 v3d->prog.fs->prog_data.fs->base.threads == 4;
602
603 shader.coordinate_shader_start_in_final_thread_section =
604 v3d->prog.cs->prog_data.vs->base.single_seg;
605 shader.vertex_shader_start_in_final_thread_section =
606 v3d->prog.vs->prog_data.vs->base.single_seg;
607 shader.fragment_shader_start_in_final_thread_section =
608 v3d->prog.fs->prog_data.fs->base.single_seg;
609 #else
610 shader.coordinate_shader_4_way_threadable =
611 v3d->prog.cs->prog_data.vs->base.threads == 4;
612 shader.coordinate_shader_2_way_threadable =
613 v3d->prog.cs->prog_data.vs->base.threads == 2;
614 shader.vertex_shader_4_way_threadable =
615 v3d->prog.vs->prog_data.vs->base.threads == 4;
616 shader.vertex_shader_2_way_threadable =
617 v3d->prog.vs->prog_data.vs->base.threads == 2;
618 shader.fragment_shader_4_way_threadable =
619 v3d->prog.fs->prog_data.fs->base.threads == 4;
620 shader.fragment_shader_2_way_threadable =
621 v3d->prog.fs->prog_data.fs->base.threads == 2;
622 #endif
623
624 shader.vertex_id_read_by_coordinate_shader =
625 v3d->prog.cs->prog_data.vs->uses_vid;
626 shader.instance_id_read_by_coordinate_shader =
627 v3d->prog.cs->prog_data.vs->uses_iid;
628 shader.vertex_id_read_by_vertex_shader =
629 v3d->prog.vs->prog_data.vs->uses_vid;
630 shader.instance_id_read_by_vertex_shader =
631 v3d->prog.vs->prog_data.vs->uses_iid;
632
633 shader.address_of_default_attribute_values =
634 cl_address(v3d_resource(vtx->defaults)->bo,
635 vtx->defaults_offset);
636 }
637
638 bool cs_loaded_any = false;
639 for (int i = 0; i < vtx->num_elements; i++) {
640 struct pipe_vertex_element *elem = &vtx->pipe[i];
641 struct pipe_vertex_buffer *vb =
642 &vertexbuf->vb[elem->vertex_buffer_index];
643 struct v3d_resource *rsc = v3d_resource(vb->buffer.resource);
644
645 const uint32_t size =
646 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD);
647 cl_emit_with_prepacked(&job->indirect,
648 GL_SHADER_STATE_ATTRIBUTE_RECORD,
649 &vtx->attrs[i * size], attr) {
650 attr.stride = vb->stride;
651 attr.address = cl_address(rsc->bo,
652 vb->buffer_offset +
653 elem->src_offset);
654 attr.number_of_values_read_by_coordinate_shader =
655 v3d->prog.cs->prog_data.vs->vattr_sizes[i];
656 attr.number_of_values_read_by_vertex_shader =
657 v3d->prog.vs->prog_data.vs->vattr_sizes[i];
658
659 /* GFXH-930: At least one attribute must be enabled
660 * and read by CS and VS. If we have attributes being
661 * consumed by the VS but not the CS, then set up a
662 * dummy load of the last attribute into the CS's VPM
663 * inputs. (Since CS is just dead-code-elimination
664 * compared to VS, we can't have CS loading but not
665 * VS).
666 */
667 if (v3d->prog.cs->prog_data.vs->vattr_sizes[i])
668 cs_loaded_any = true;
669 if (i == vtx->num_elements - 1 && !cs_loaded_any) {
670 attr.number_of_values_read_by_coordinate_shader = 1;
671 }
672 #if V3D_VERSION >= 41
673 attr.maximum_index = 0xffffff;
674 #endif
675 }
676 STATIC_ASSERT(sizeof(vtx->attrs) >= V3D_MAX_VS_INPUTS / 4 * size);
677 }
678
679 if (vtx->num_elements == 0) {
680 /* GFXH-930: At least one attribute must be enabled and read
681 * by CS and VS. If we have no attributes being consumed by
682 * the shader, set up a dummy to be loaded into the VPM.
683 */
684 cl_emit(&job->indirect, GL_SHADER_STATE_ATTRIBUTE_RECORD, attr) {
685 /* Valid address of data whose value will be unused. */
686 attr.address = cl_address(job->indirect.bo, 0);
687
688 attr.type = ATTRIBUTE_FLOAT;
689 attr.stride = 0;
690 attr.vec_size = 1;
691
692 attr.number_of_values_read_by_coordinate_shader = 1;
693 attr.number_of_values_read_by_vertex_shader = 1;
694 }
695 }
696
697 cl_emit(&job->bcl, VCM_CACHE_SIZE, vcm) {
698 vcm.number_of_16_vertex_batches_for_binning =
699 v3d->prog.cs->prog_data.vs->vcm_cache_size;
700 vcm.number_of_16_vertex_batches_for_rendering =
701 v3d->prog.vs->prog_data.vs->vcm_cache_size;
702 }
703
704 #if V3D_VERSION >= 41
705 if (v3d->prog.gs) {
706 cl_emit(&job->bcl, GL_SHADER_STATE_INCLUDING_GS, state) {
707 state.address = cl_address(job->indirect.bo,
708 shader_rec_offset);
709 state.number_of_attribute_arrays = num_elements_to_emit;
710 }
711 } else {
712 cl_emit(&job->bcl, GL_SHADER_STATE, state) {
713 state.address = cl_address(job->indirect.bo,
714 shader_rec_offset);
715 state.number_of_attribute_arrays = num_elements_to_emit;
716 }
717 }
718 #else
719 assert(!v3d->prog.gs);
720 cl_emit(&job->bcl, GL_SHADER_STATE, state) {
721 state.address = cl_address(job->indirect.bo, shader_rec_offset);
722 state.number_of_attribute_arrays = num_elements_to_emit;
723 }
724 #endif
725
726 v3d_bo_unreference(&cs_uniforms.bo);
727 v3d_bo_unreference(&vs_uniforms.bo);
728 if (gs_uniforms.bo)
729 v3d_bo_unreference(&gs_uniforms.bo);
730 if (gs_bin_uniforms.bo)
731 v3d_bo_unreference(&gs_bin_uniforms.bo);
732 v3d_bo_unreference(&fs_uniforms.bo);
733 }
734
735 /**
736 * Updates the number of primitvies generated from the number of vertices
737 * to draw. We do this here instead of using PRIMITIVE_COUNTS_FEEDBACK because
738 * using the GPU packet for this might require sync waits and this is trivial
739 * to handle in the CPU instead.
740 */
741 static void
742 v3d_update_primitives_generated_counter(struct v3d_context *v3d,
743 const struct pipe_draw_info *info)
744 {
745 if (!v3d->active_queries)
746 return;
747
748 uint32_t prims = u_prims_for_vertices(info->mode, info->count);
749 v3d->prims_generated += prims;
750 }
751
752 static void
753 v3d_update_job_ez(struct v3d_context *v3d, struct v3d_job *job)
754 {
755 switch (v3d->zsa->ez_state) {
756 case VC5_EZ_UNDECIDED:
757 /* If the Z/S state didn't pick a direction but didn't
758 * disable, then go along with the current EZ state. This
759 * allows EZ optimization for Z func == EQUAL or NEVER.
760 */
761 break;
762
763 case VC5_EZ_LT_LE:
764 case VC5_EZ_GT_GE:
765 /* If the Z/S state picked a direction, then it needs to match
766 * the current direction if we've decided on one.
767 */
768 if (job->ez_state == VC5_EZ_UNDECIDED)
769 job->ez_state = v3d->zsa->ez_state;
770 else if (job->ez_state != v3d->zsa->ez_state)
771 job->ez_state = VC5_EZ_DISABLED;
772 break;
773
774 case VC5_EZ_DISABLED:
775 /* If the current Z/S state disables EZ because of a bad Z
776 * func or stencil operation, then we can't do any more EZ in
777 * this frame.
778 */
779 job->ez_state = VC5_EZ_DISABLED;
780 break;
781 }
782
783 /* If the FS affects the Z of the pixels, then it may update against
784 * the chosen EZ direction (though we could use
785 * ARB_conservative_depth's hints to avoid this)
786 */
787 if (v3d->prog.fs->prog_data.fs->writes_z) {
788 job->ez_state = VC5_EZ_DISABLED;
789 }
790
791 if (job->first_ez_state == VC5_EZ_UNDECIDED &&
792 (job->ez_state != VC5_EZ_DISABLED || job->draw_calls_queued == 0))
793 job->first_ez_state = job->ez_state;
794 }
795
796 static void
797 v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
798 {
799 struct v3d_context *v3d = v3d_context(pctx);
800
801 if (!info->count_from_stream_output && !info->indirect &&
802 !info->primitive_restart &&
803 !u_trim_pipe_prim(info->mode, (unsigned*)&info->count))
804 return;
805
806 /* Fall back for weird desktop GL primitive restart values. */
807 if (info->primitive_restart &&
808 info->index_size) {
809 uint32_t mask = ~0;
810
811 switch (info->index_size) {
812 case 2:
813 mask = 0xffff;
814 break;
815 case 1:
816 mask = 0xff;
817 break;
818 }
819
820 if (info->restart_index != mask) {
821 util_draw_vbo_without_prim_restart(pctx, info);
822 return;
823 }
824 }
825
826 if (info->mode >= PIPE_PRIM_QUADS) {
827 util_primconvert_save_rasterizer_state(v3d->primconvert, &v3d->rasterizer->base);
828 util_primconvert_draw_vbo(v3d->primconvert, info);
829 perf_debug("Fallback conversion for %d %s vertices\n",
830 info->count, u_prim_name(info->mode));
831 return;
832 }
833
834 /* Before setting up the draw, flush anything writing to the resources
835 * that we read from or reading from resources we write to.
836 */
837 for (int s = 0; s < PIPE_SHADER_COMPUTE; s++)
838 v3d_predraw_check_stage_inputs(pctx, s);
839
840 if (info->indirect) {
841 v3d_flush_jobs_writing_resource(v3d, info->indirect->buffer,
842 V3D_FLUSH_DEFAULT);
843 }
844
845 v3d_predraw_check_outputs(pctx);
846
847 /* If transform feedback is active and we are switching primitive type
848 * we need to submit the job before drawing and update the vertex count
849 * written to TF based on the primitive type since we will need to
850 * know the exact vertex count if the application decides to call
851 * glDrawTransformFeedback() later.
852 */
853 if (v3d->streamout.num_targets > 0 &&
854 u_base_prim_type(info->mode) != u_base_prim_type(v3d->prim_mode)) {
855 v3d_tf_update_counters(v3d);
856 }
857
858 struct v3d_job *job = v3d_get_job_for_fbo(v3d);
859
860 /* If vertex texturing depends on the output of rendering, we need to
861 * ensure that that rendering is complete before we run a coordinate
862 * shader that depends on it.
863 *
864 * Given that doing that is unusual, for now we just block the binner
865 * on the last submitted render, rather than tracking the last
866 * rendering to each texture's BO.
867 */
868 if (v3d->tex[PIPE_SHADER_VERTEX].num_textures || info->indirect) {
869 perf_debug("Blocking binner on last render "
870 "due to vertex texturing or indirect drawing.\n");
871 job->submit.in_sync_bcl = v3d->out_sync;
872 }
873
874 /* Mark SSBOs and images as being written. We don't actually know
875 * which ones are read vs written, so just assume the worst.
876 */
877 for (int s = 0; s < PIPE_SHADER_COMPUTE; s++) {
878 foreach_bit(i, v3d->ssbo[s].enabled_mask) {
879 v3d_job_add_write_resource(job,
880 v3d->ssbo[s].sb[i].buffer);
881 job->tmu_dirty_rcl = true;
882 }
883
884 foreach_bit(i, v3d->shaderimg[s].enabled_mask) {
885 v3d_job_add_write_resource(job,
886 v3d->shaderimg[s].si[i].base.resource);
887 job->tmu_dirty_rcl = true;
888 }
889 }
890
891 /* Get space to emit our draw call into the BCL, using a branch to
892 * jump to a new BO if necessary.
893 */
894 v3d_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);
895
896 if (v3d->prim_mode != info->mode) {
897 v3d->prim_mode = info->mode;
898 v3d->dirty |= VC5_DIRTY_PRIM_MODE;
899 }
900
901 v3d_start_draw(v3d);
902 v3d_update_compiled_shaders(v3d, info->mode);
903 v3d_update_job_ez(v3d, job);
904
905 /* If this job was writing to transform feedback buffers before this
906 * draw and we are reading from them here, then we need to wait for TF
907 * to complete before we emit this draw.
908 *
909 * Notice this check needs to happen before we emit state for the
910 * current draw call, where we update job->tf_enabled, so we can ensure
911 * that we only check TF writes for prior draws.
912 */
913 v3d_emit_wait_for_tf_if_needed(v3d, job);
914
915 #if V3D_VERSION >= 41
916 v3d41_emit_state(pctx);
917 #else
918 v3d33_emit_state(pctx);
919 #endif
920
921 if (v3d->dirty & (VC5_DIRTY_VTXBUF |
922 VC5_DIRTY_VTXSTATE |
923 VC5_DIRTY_PRIM_MODE |
924 VC5_DIRTY_RASTERIZER |
925 VC5_DIRTY_COMPILED_CS |
926 VC5_DIRTY_COMPILED_VS |
927 VC5_DIRTY_COMPILED_GS_BIN |
928 VC5_DIRTY_COMPILED_GS |
929 VC5_DIRTY_COMPILED_FS |
930 v3d->prog.cs->uniform_dirty_bits |
931 v3d->prog.vs->uniform_dirty_bits |
932 (v3d->prog.gs_bin ?
933 v3d->prog.gs_bin->uniform_dirty_bits : 0) |
934 (v3d->prog.gs ?
935 v3d->prog.gs->uniform_dirty_bits : 0) |
936 v3d->prog.fs->uniform_dirty_bits)) {
937 v3d_emit_gl_shader_state(v3d, info);
938 }
939
940 v3d->dirty = 0;
941
942 /* The Base Vertex/Base Instance packet sets those values to nonzero
943 * for the next draw call only.
944 */
945 if (info->index_bias || info->start_instance) {
946 cl_emit(&job->bcl, BASE_VERTEX_BASE_INSTANCE, base) {
947 base.base_instance = info->start_instance;
948 base.base_vertex = info->index_bias;
949 }
950 }
951
952 uint32_t prim_tf_enable = 0;
953 #if V3D_VERSION < 40
954 /* V3D 3.x: The HW only processes transform feedback on primitives
955 * with the flag set.
956 */
957 if (v3d->streamout.num_targets)
958 prim_tf_enable = (V3D_PRIM_POINTS_TF - V3D_PRIM_POINTS);
959 #endif
960
961 v3d_update_primitives_generated_counter(v3d, info);
962
963 /* Note that the primitive type fields match with OpenGL/gallium
964 * definitions, up to but not including QUADS.
965 */
966 if (info->index_size) {
967 uint32_t index_size = info->index_size;
968 uint32_t offset = info->start * index_size;
969 struct pipe_resource *prsc;
970 if (info->has_user_indices) {
971 prsc = NULL;
972 u_upload_data(v3d->uploader, 0,
973 info->count * info->index_size, 4,
974 info->index.user,
975 &offset, &prsc);
976 } else {
977 prsc = info->index.resource;
978 }
979 struct v3d_resource *rsc = v3d_resource(prsc);
980
981 #if V3D_VERSION >= 40
982 cl_emit(&job->bcl, INDEX_BUFFER_SETUP, ib) {
983 ib.address = cl_address(rsc->bo, 0);
984 ib.size = rsc->bo->size;
985 }
986 #endif
987
988 if (info->indirect) {
989 cl_emit(&job->bcl, INDIRECT_INDEXED_INSTANCED_PRIM_LIST, prim) {
990 prim.index_type = ffs(info->index_size) - 1;
991 #if V3D_VERSION < 40
992 prim.address_of_indices_list =
993 cl_address(rsc->bo, offset);
994 #endif /* V3D_VERSION < 40 */
995 prim.mode = info->mode | prim_tf_enable;
996 prim.enable_primitive_restarts = info->primitive_restart;
997
998 prim.number_of_draw_indirect_indexed_records = info->indirect->draw_count;
999
1000 prim.stride_in_multiples_of_4_bytes = info->indirect->stride >> 2;
1001 prim.address = cl_address(v3d_resource(info->indirect->buffer)->bo,
1002 info->indirect->offset);
1003 }
1004 } else if (info->instance_count > 1) {
1005 cl_emit(&job->bcl, INDEXED_INSTANCED_PRIM_LIST, prim) {
1006 prim.index_type = ffs(info->index_size) - 1;
1007 #if V3D_VERSION >= 40
1008 prim.index_offset = offset;
1009 #else /* V3D_VERSION < 40 */
1010 prim.maximum_index = (1u << 31) - 1; /* XXX */
1011 prim.address_of_indices_list =
1012 cl_address(rsc->bo, offset);
1013 #endif /* V3D_VERSION < 40 */
1014 prim.mode = info->mode | prim_tf_enable;
1015 prim.enable_primitive_restarts = info->primitive_restart;
1016
1017 prim.number_of_instances = info->instance_count;
1018 prim.instance_length = info->count;
1019 }
1020 } else {
1021 cl_emit(&job->bcl, INDEXED_PRIM_LIST, prim) {
1022 prim.index_type = ffs(info->index_size) - 1;
1023 prim.length = info->count;
1024 #if V3D_VERSION >= 40
1025 prim.index_offset = offset;
1026 #else /* V3D_VERSION < 40 */
1027 prim.maximum_index = (1u << 31) - 1; /* XXX */
1028 prim.address_of_indices_list =
1029 cl_address(rsc->bo, offset);
1030 #endif /* V3D_VERSION < 40 */
1031 prim.mode = info->mode | prim_tf_enable;
1032 prim.enable_primitive_restarts = info->primitive_restart;
1033 }
1034 }
1035
1036 if (info->has_user_indices)
1037 pipe_resource_reference(&prsc, NULL);
1038 } else {
1039 if (info->indirect) {
1040 cl_emit(&job->bcl, INDIRECT_VERTEX_ARRAY_INSTANCED_PRIMS, prim) {
1041 prim.mode = info->mode | prim_tf_enable;
1042 prim.number_of_draw_indirect_array_records = info->indirect->draw_count;
1043
1044 prim.stride_in_multiples_of_4_bytes = info->indirect->stride >> 2;
1045 prim.address = cl_address(v3d_resource(info->indirect->buffer)->bo,
1046 info->indirect->offset);
1047 }
1048 } else if (info->instance_count > 1) {
1049 struct pipe_stream_output_target *so =
1050 info->count_from_stream_output;
1051 uint32_t vert_count = so ?
1052 v3d_stream_output_target_get_vertex_count(so) :
1053 info->count;
1054 cl_emit(&job->bcl, VERTEX_ARRAY_INSTANCED_PRIMS, prim) {
1055 prim.mode = info->mode | prim_tf_enable;
1056 prim.index_of_first_vertex = info->start;
1057 prim.number_of_instances = info->instance_count;
1058 prim.instance_length = vert_count;
1059 }
1060 } else {
1061 struct pipe_stream_output_target *so =
1062 info->count_from_stream_output;
1063 uint32_t vert_count = so ?
1064 v3d_stream_output_target_get_vertex_count(so) :
1065 info->count;
1066 cl_emit(&job->bcl, VERTEX_ARRAY_PRIMS, prim) {
1067 prim.mode = info->mode | prim_tf_enable;
1068 prim.length = vert_count;
1069 prim.index_of_first_vertex = info->start;
1070 }
1071 }
1072 }
1073
1074 /* A flush is required in between a TF draw and any following TF specs
1075 * packet, or the GPU may hang. Just flush each time for now.
1076 */
1077 if (v3d->streamout.num_targets)
1078 cl_emit(&job->bcl, TRANSFORM_FEEDBACK_FLUSH_AND_COUNT, flush);
1079
1080 job->draw_calls_queued++;
1081 if (v3d->streamout.num_targets)
1082 job->tf_draw_calls_queued++;
1083
1084 /* Increment the TF offsets by how many verts we wrote. XXX: This
1085 * needs some clamping to the buffer size.
1086 */
1087 for (int i = 0; i < v3d->streamout.num_targets; i++)
1088 v3d->streamout.offsets[i] += info->count;
1089
1090 if (v3d->zsa && job->zsbuf && v3d->zsa->base.depth.enabled) {
1091 struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);
1092 v3d_job_add_bo(job, rsc->bo);
1093
1094 job->load |= PIPE_CLEAR_DEPTH & ~job->clear;
1095 if (v3d->zsa->base.depth.writemask)
1096 job->store |= PIPE_CLEAR_DEPTH;
1097 rsc->initialized_buffers = PIPE_CLEAR_DEPTH;
1098 }
1099
1100 if (v3d->zsa && job->zsbuf && v3d->zsa->base.stencil[0].enabled) {
1101 struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);
1102 if (rsc->separate_stencil)
1103 rsc = rsc->separate_stencil;
1104
1105 v3d_job_add_bo(job, rsc->bo);
1106
1107 job->load |= PIPE_CLEAR_STENCIL & ~job->clear;
1108 if (v3d->zsa->base.stencil[0].writemask ||
1109 v3d->zsa->base.stencil[1].writemask) {
1110 job->store |= PIPE_CLEAR_STENCIL;
1111 }
1112 rsc->initialized_buffers |= PIPE_CLEAR_STENCIL;
1113 }
1114
1115 for (int i = 0; i < V3D_MAX_DRAW_BUFFERS; i++) {
1116 uint32_t bit = PIPE_CLEAR_COLOR0 << i;
1117 int blend_rt = v3d->blend->base.independent_blend_enable ? i : 0;
1118
1119 if (job->store & bit || !job->cbufs[i])
1120 continue;
1121 struct v3d_resource *rsc = v3d_resource(job->cbufs[i]->texture);
1122
1123 job->load |= bit & ~job->clear;
1124 if (v3d->blend->base.rt[blend_rt].colormask)
1125 job->store |= bit;
1126 v3d_job_add_bo(job, rsc->bo);
1127 }
1128
1129 if (job->referenced_size > 768 * 1024 * 1024) {
1130 perf_debug("Flushing job with %dkb to try to free up memory\n",
1131 job->referenced_size / 1024);
1132 v3d_flush(pctx);
1133 }
1134
1135 if (V3D_DEBUG & V3D_DEBUG_ALWAYS_FLUSH)
1136 v3d_flush(pctx);
1137 }
1138
1139 #if V3D_VERSION >= 41
1140 #define V3D_CSD_CFG012_WG_COUNT_SHIFT 16
1141 #define V3D_CSD_CFG012_WG_OFFSET_SHIFT 0
1142 /* Allow this dispatch to start while the last one is still running. */
1143 #define V3D_CSD_CFG3_OVERLAP_WITH_PREV (1 << 26)
1144 /* Maximum supergroup ID. 6 bits. */
1145 #define V3D_CSD_CFG3_MAX_SG_ID_SHIFT 20
1146 /* Batches per supergroup minus 1. 8 bits. */
1147 #define V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT 12
1148 /* Workgroups per supergroup, 0 means 16 */
1149 #define V3D_CSD_CFG3_WGS_PER_SG_SHIFT 8
1150 #define V3D_CSD_CFG3_WG_SIZE_SHIFT 0
1151
1152 #define V3D_CSD_CFG5_PROPAGATE_NANS (1 << 2)
1153 #define V3D_CSD_CFG5_SINGLE_SEG (1 << 1)
1154 #define V3D_CSD_CFG5_THREADING (1 << 0)
1155
1156 static void
1157 v3d_launch_grid(struct pipe_context *pctx, const struct pipe_grid_info *info)
1158 {
1159 struct v3d_context *v3d = v3d_context(pctx);
1160 struct v3d_screen *screen = v3d->screen;
1161
1162 v3d_predraw_check_stage_inputs(pctx, PIPE_SHADER_COMPUTE);
1163
1164 v3d_update_compiled_cs(v3d);
1165
1166 if (!v3d->prog.compute->resource) {
1167 static bool warned = false;
1168 if (!warned) {
1169 fprintf(stderr,
1170 "Compute shader failed to compile. "
1171 "Expect corruption.\n");
1172 warned = true;
1173 }
1174 return;
1175 }
1176
1177 /* Some of the units of scale:
1178 *
1179 * - Batches of 16 work items (shader invocations) that will be queued
1180 * to the run on a QPU at once.
1181 *
1182 * - Workgroups composed of work items based on the shader's layout
1183 * declaration.
1184 *
1185 * - Supergroups of 1-16 workgroups. There can only be 16 supergroups
1186 * running at a time on the core, so we want to keep them large to
1187 * keep the QPUs busy, but a whole supergroup will sync at a barrier
1188 * so we want to keep them small if one is present.
1189 */
1190 struct drm_v3d_submit_csd submit = { 0 };
1191 struct v3d_job *job = v3d_job_create(v3d);
1192
1193 /* Set up the actual number of workgroups, synchronously mapping the
1194 * indirect buffer if necessary to get the dimensions.
1195 */
1196 if (info->indirect) {
1197 struct pipe_transfer *transfer;
1198 uint32_t *map = pipe_buffer_map_range(pctx, info->indirect,
1199 info->indirect_offset,
1200 3 * sizeof(uint32_t),
1201 PIPE_TRANSFER_READ,
1202 &transfer);
1203 memcpy(v3d->compute_num_workgroups, map, 3 * sizeof(uint32_t));
1204 pipe_buffer_unmap(pctx, transfer);
1205
1206 if (v3d->compute_num_workgroups[0] == 0 ||
1207 v3d->compute_num_workgroups[1] == 0 ||
1208 v3d->compute_num_workgroups[2] == 0) {
1209 /* Nothing to dispatch, so skip the draw (CSD can't
1210 * handle 0 workgroups).
1211 */
1212 return;
1213 }
1214 } else {
1215 v3d->compute_num_workgroups[0] = info->grid[0];
1216 v3d->compute_num_workgroups[1] = info->grid[1];
1217 v3d->compute_num_workgroups[2] = info->grid[2];
1218 }
1219
1220 for (int i = 0; i < 3; i++) {
1221 submit.cfg[i] |= (v3d->compute_num_workgroups[i] <<
1222 V3D_CSD_CFG012_WG_COUNT_SHIFT);
1223 }
1224
1225 perf_debug("CSD only using single WG per SG currently, "
1226 "should increase that when possible.");
1227 int wgs_per_sg = 1;
1228 int wg_size = info->block[0] * info->block[1] * info->block[2];
1229 submit.cfg[3] |= wgs_per_sg << V3D_CSD_CFG3_WGS_PER_SG_SHIFT;
1230 submit.cfg[3] |= ((DIV_ROUND_UP(wgs_per_sg * wg_size, 16) - 1) <<
1231 V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT);
1232 submit.cfg[3] |= (wg_size & 0xff) << V3D_CSD_CFG3_WG_SIZE_SHIFT;
1233
1234 int batches_per_wg = DIV_ROUND_UP(wg_size, 16);
1235 /* Number of batches the dispatch will invoke (minus 1). */
1236 submit.cfg[4] = batches_per_wg * (v3d->compute_num_workgroups[0] *
1237 v3d->compute_num_workgroups[1] *
1238 v3d->compute_num_workgroups[2]) - 1;
1239
1240 /* Make sure we didn't accidentally underflow. */
1241 assert(submit.cfg[4] != ~0);
1242
1243 v3d_job_add_bo(job, v3d_resource(v3d->prog.compute->resource)->bo);
1244 submit.cfg[5] = (v3d_resource(v3d->prog.compute->resource)->bo->offset +
1245 v3d->prog.compute->offset);
1246 submit.cfg[5] |= V3D_CSD_CFG5_PROPAGATE_NANS;
1247 if (v3d->prog.compute->prog_data.base->single_seg)
1248 submit.cfg[5] |= V3D_CSD_CFG5_SINGLE_SEG;
1249 if (v3d->prog.compute->prog_data.base->threads == 4)
1250 submit.cfg[5] |= V3D_CSD_CFG5_THREADING;
1251
1252 if (v3d->prog.compute->prog_data.compute->shared_size) {
1253 v3d->compute_shared_memory =
1254 v3d_bo_alloc(v3d->screen,
1255 v3d->prog.compute->prog_data.compute->shared_size *
1256 wgs_per_sg,
1257 "shared_vars");
1258 }
1259
1260 struct v3d_cl_reloc uniforms = v3d_write_uniforms(v3d, job,
1261 v3d->prog.compute,
1262 PIPE_SHADER_COMPUTE);
1263 v3d_job_add_bo(job, uniforms.bo);
1264 submit.cfg[6] = uniforms.bo->offset + uniforms.offset;
1265
1266 /* Pull some job state that was stored in a SUBMIT_CL struct out to
1267 * our SUBMIT_CSD struct
1268 */
1269 submit.bo_handles = job->submit.bo_handles;
1270 submit.bo_handle_count = job->submit.bo_handle_count;
1271
1272 /* Serialize this in the rest of our command stream. */
1273 submit.in_sync = v3d->out_sync;
1274 submit.out_sync = v3d->out_sync;
1275
1276 if (!(V3D_DEBUG & V3D_DEBUG_NORAST)) {
1277 int ret = v3d_ioctl(screen->fd, DRM_IOCTL_V3D_SUBMIT_CSD,
1278 &submit);
1279 static bool warned = false;
1280 if (ret && !warned) {
1281 fprintf(stderr, "CSD submit call returned %s. "
1282 "Expect corruption.\n", strerror(errno));
1283 warned = true;
1284 }
1285 }
1286
1287 v3d_job_free(v3d, job);
1288
1289 /* Mark SSBOs as being written.. we don't actually know which ones are
1290 * read vs written, so just assume the worst
1291 */
1292 foreach_bit(i, v3d->ssbo[PIPE_SHADER_COMPUTE].enabled_mask) {
1293 struct v3d_resource *rsc = v3d_resource(
1294 v3d->ssbo[PIPE_SHADER_COMPUTE].sb[i].buffer);
1295 rsc->writes++; /* XXX */
1296 }
1297
1298 foreach_bit(i, v3d->shaderimg[PIPE_SHADER_COMPUTE].enabled_mask) {
1299 struct v3d_resource *rsc = v3d_resource(
1300 v3d->shaderimg[PIPE_SHADER_COMPUTE].si[i].base.resource);
1301 rsc->writes++;
1302 }
1303
1304 v3d_bo_unreference(&uniforms.bo);
1305 v3d_bo_unreference(&v3d->compute_shared_memory);
1306 }
1307 #endif
1308
1309 /**
1310 * Implements gallium's clear() hook (glClear()) by drawing a pair of triangles.
1311 */
1312 static void
1313 v3d_draw_clear(struct v3d_context *v3d,
1314 unsigned buffers,
1315 const union pipe_color_union *color,
1316 double depth, unsigned stencil)
1317 {
1318 static const union pipe_color_union dummy_color = {};
1319
1320 /* The blitter util dereferences the color regardless, even though the
1321 * gallium clear API may not pass one in when only Z/S are cleared.
1322 */
1323 if (!color)
1324 color = &dummy_color;
1325
1326 v3d_blitter_save(v3d);
1327 util_blitter_clear(v3d->blitter,
1328 v3d->framebuffer.width,
1329 v3d->framebuffer.height,
1330 util_framebuffer_get_num_layers(&v3d->framebuffer),
1331 buffers, color, depth, stencil,
1332 util_framebuffer_get_num_samples(&v3d->framebuffer) > 1);
1333 }
1334
1335 /**
1336 * Attempts to perform the GL clear by using the TLB's fast clear at the start
1337 * of the frame.
1338 */
1339 static unsigned
1340 v3d_tlb_clear(struct v3d_job *job, unsigned buffers,
1341 const union pipe_color_union *color,
1342 double depth, unsigned stencil)
1343 {
1344 struct v3d_context *v3d = job->v3d;
1345
1346 if (job->draw_calls_queued) {
1347 /* If anything in the CL has drawn using the buffer, then the
1348 * TLB clear we're trying to add now would happen before that
1349 * drawing.
1350 */
1351 buffers &= ~(job->load | job->store);
1352 }
1353
1354 /* GFXH-1461: If we were to emit a load of just depth or just stencil,
1355 * then the clear for the other may get lost. We need to decide now
1356 * if it would be possible to need to emit a load of just one after
1357 * we've set up our TLB clears.
1358 */
1359 if (buffers & PIPE_CLEAR_DEPTHSTENCIL &&
1360 (buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL &&
1361 job->zsbuf &&
1362 util_format_is_depth_and_stencil(job->zsbuf->texture->format)) {
1363 buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
1364 }
1365
1366 for (int i = 0; i < V3D_MAX_DRAW_BUFFERS; i++) {
1367 uint32_t bit = PIPE_CLEAR_COLOR0 << i;
1368 if (!(buffers & bit))
1369 continue;
1370
1371 struct pipe_surface *psurf = v3d->framebuffer.cbufs[i];
1372 struct v3d_surface *surf = v3d_surface(psurf);
1373 struct v3d_resource *rsc = v3d_resource(psurf->texture);
1374
1375 union util_color uc;
1376 uint32_t internal_size = 4 << surf->internal_bpp;
1377
1378 static union pipe_color_union swapped_color;
1379 if (v3d->swap_color_rb & (1 << i)) {
1380 swapped_color.f[0] = color->f[2];
1381 swapped_color.f[1] = color->f[1];
1382 swapped_color.f[2] = color->f[0];
1383 swapped_color.f[3] = color->f[3];
1384 color = &swapped_color;
1385 }
1386
1387 switch (surf->internal_type) {
1388 case V3D_INTERNAL_TYPE_8:
1389 util_pack_color(color->f, PIPE_FORMAT_R8G8B8A8_UNORM,
1390 &uc);
1391 memcpy(job->clear_color[i], uc.ui, internal_size);
1392 break;
1393 case V3D_INTERNAL_TYPE_8I:
1394 case V3D_INTERNAL_TYPE_8UI:
1395 job->clear_color[i][0] = ((color->ui[0] & 0xff) |
1396 (color->ui[1] & 0xff) << 8 |
1397 (color->ui[2] & 0xff) << 16 |
1398 (color->ui[3] & 0xff) << 24);
1399 break;
1400 case V3D_INTERNAL_TYPE_16F:
1401 util_pack_color(color->f, PIPE_FORMAT_R16G16B16A16_FLOAT,
1402 &uc);
1403 memcpy(job->clear_color[i], uc.ui, internal_size);
1404 break;
1405 case V3D_INTERNAL_TYPE_16I:
1406 case V3D_INTERNAL_TYPE_16UI:
1407 job->clear_color[i][0] = ((color->ui[0] & 0xffff) |
1408 color->ui[1] << 16);
1409 job->clear_color[i][1] = ((color->ui[2] & 0xffff) |
1410 color->ui[3] << 16);
1411 break;
1412 case V3D_INTERNAL_TYPE_32F:
1413 case V3D_INTERNAL_TYPE_32I:
1414 case V3D_INTERNAL_TYPE_32UI:
1415 memcpy(job->clear_color[i], color->ui, internal_size);
1416 break;
1417 }
1418
1419 rsc->initialized_buffers |= bit;
1420 }
1421
1422 unsigned zsclear = buffers & PIPE_CLEAR_DEPTHSTENCIL;
1423 if (zsclear) {
1424 struct v3d_resource *rsc =
1425 v3d_resource(v3d->framebuffer.zsbuf->texture);
1426
1427 if (zsclear & PIPE_CLEAR_DEPTH)
1428 job->clear_z = depth;
1429 if (zsclear & PIPE_CLEAR_STENCIL)
1430 job->clear_s = stencil;
1431
1432 rsc->initialized_buffers |= zsclear;
1433 }
1434
1435 job->draw_min_x = 0;
1436 job->draw_min_y = 0;
1437 job->draw_max_x = v3d->framebuffer.width;
1438 job->draw_max_y = v3d->framebuffer.height;
1439 job->clear |= buffers;
1440 job->store |= buffers;
1441
1442 v3d_start_draw(v3d);
1443
1444 return buffers;
1445 }
1446
1447 static void
1448 v3d_clear(struct pipe_context *pctx, unsigned buffers,
1449 const union pipe_color_union *color, double depth, unsigned stencil)
1450 {
1451 struct v3d_context *v3d = v3d_context(pctx);
1452 struct v3d_job *job = v3d_get_job_for_fbo(v3d);
1453
1454 buffers &= ~v3d_tlb_clear(job, buffers, color, depth, stencil);
1455
1456 if (buffers)
1457 v3d_draw_clear(v3d, buffers, color, depth, stencil);
1458 }
1459
1460 static void
1461 v3d_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,
1462 const union pipe_color_union *color,
1463 unsigned x, unsigned y, unsigned w, unsigned h,
1464 bool render_condition_enabled)
1465 {
1466 fprintf(stderr, "unimpl: clear RT\n");
1467 }
1468
1469 static void
1470 v3d_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,
1471 unsigned buffers, double depth, unsigned stencil,
1472 unsigned x, unsigned y, unsigned w, unsigned h,
1473 bool render_condition_enabled)
1474 {
1475 fprintf(stderr, "unimpl: clear DS\n");
1476 }
1477
1478 void
1479 v3dX(draw_init)(struct pipe_context *pctx)
1480 {
1481 pctx->draw_vbo = v3d_draw_vbo;
1482 pctx->clear = v3d_clear;
1483 pctx->clear_render_target = v3d_clear_render_target;
1484 pctx->clear_depth_stencil = v3d_clear_depth_stencil;
1485 #if V3D_VERSION >= 41
1486 if (v3d_context(pctx)->screen->has_csd)
1487 pctx->launch_grid = v3d_launch_grid;
1488 #endif
1489 }