v3d: subclass pipe_streamout_output_target to record TF vertices written
[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/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, false);
160 }
161
162 /* Flush writes to UBOs. */
163 foreach_bit(i, v3d->constbuf[s].enabled_mask) {
164 struct pipe_constant_buffer *cb = &v3d->constbuf[s].cb[i];
165 if (cb->buffer)
166 v3d_flush_jobs_writing_resource(v3d, cb->buffer, false);
167 }
168
169 /* Flush writes to our image views */
170 foreach_bit(i, v3d->shaderimg[s].enabled_mask) {
171 struct v3d_image_view *view = &v3d->shaderimg[s].si[i];
172
173 v3d_flush_jobs_writing_resource(v3d, view->base.resource,
174 false);
175 }
176
177 /* Flush writes to our vertex buffers (i.e. from transform feedback) */
178 if (s == PIPE_SHADER_VERTEX) {
179 foreach_bit(i, v3d->vertexbuf.enabled_mask) {
180 struct pipe_vertex_buffer *vb = &v3d->vertexbuf.vb[i];
181
182 v3d_flush_jobs_writing_resource(v3d, vb->buffer.resource,
183 false);
184 }
185 }
186 }
187
188 static void
189 v3d_predraw_check_outputs(struct pipe_context *pctx)
190 {
191 struct v3d_context *v3d = v3d_context(pctx);
192
193 /* Flush jobs reading from TF buffers that we are about to write. */
194 if (v3d_transform_feedback_enabled(v3d)) {
195 struct v3d_streamout_stateobj *so = &v3d->streamout;
196
197 for (int i = 0; i < so->num_targets; i++) {
198 if (!so->targets[i])
199 continue;
200
201 const struct pipe_stream_output_target *target =
202 so->targets[i];
203 v3d_flush_jobs_reading_resource(v3d, target->buffer);
204 }
205 }
206 }
207
208 /**
209 * Checks if the state for the current draw reads a particular resource in
210 * in the given shader stage.
211 */
212 static bool
213 v3d_state_reads_resource(struct v3d_context *v3d,
214 struct pipe_resource *prsc,
215 enum pipe_shader_type s)
216 {
217 struct v3d_resource *rsc = v3d_resource(prsc);
218
219 /* Vertex buffers */
220 if (s == PIPE_SHADER_VERTEX) {
221 foreach_bit(i, v3d->vertexbuf.enabled_mask) {
222 struct pipe_vertex_buffer *vb = &v3d->vertexbuf.vb[i];
223 if (!vb->buffer.resource)
224 continue;
225
226 struct v3d_resource *vb_rsc =
227 v3d_resource(vb->buffer.resource);
228 if (rsc->bo == vb_rsc->bo)
229 return true;
230 }
231 }
232
233 /* Constant buffers */
234 foreach_bit(i, v3d->constbuf[s].enabled_mask) {
235 struct pipe_constant_buffer *cb = &v3d->constbuf[s].cb[i];
236 if (!cb->buffer)
237 continue;
238
239 struct v3d_resource *cb_rsc = v3d_resource(cb->buffer);
240 if (rsc->bo == cb_rsc->bo)
241 return true;
242 }
243
244 /* Shader storage buffers */
245 foreach_bit(i, v3d->ssbo[s].enabled_mask) {
246 struct pipe_shader_buffer *sb = &v3d->ssbo[s].sb[i];
247 if (!sb->buffer)
248 continue;
249
250 struct v3d_resource *sb_rsc = v3d_resource(sb->buffer);
251 if (rsc->bo == sb_rsc->bo)
252 return true;
253 }
254
255 /* Textures */
256 for (int i = 0; i < v3d->tex[s].num_textures; i++) {
257 struct pipe_sampler_view *pview = v3d->tex[s].textures[i];
258 if (!pview)
259 continue;
260
261 struct v3d_sampler_view *view = v3d_sampler_view(pview);
262 struct v3d_resource *v_rsc = v3d_resource(view->texture);
263 if (rsc->bo == v_rsc->bo)
264 return true;
265 }
266
267 return false;
268 }
269
270 static void
271 v3d_emit_wait_for_tf(struct v3d_job *job)
272 {
273 /* XXX: we might be able to skip this in some cases, for now we
274 * always emit it.
275 */
276 cl_emit(&job->bcl, FLUSH_TRANSFORM_FEEDBACK_DATA, flush);
277
278 cl_emit(&job->bcl, WAIT_FOR_TRANSFORM_FEEDBACK, wait) {
279 /* XXX: Wait for all outstanding writes... maybe we can do
280 * better in some cases.
281 */
282 wait.block_count = 255;
283 }
284
285 /* We have just flushed all our outstanding TF work in this job so make
286 * sure we don't emit TF flushes again for any of it again.
287 */
288 _mesa_set_clear(job->tf_write_prscs, NULL);
289 }
290
291 static void
292 v3d_emit_wait_for_tf_if_needed(struct v3d_context *v3d, struct v3d_job *job)
293 {
294 if (!job->tf_enabled)
295 return;
296
297 set_foreach(job->tf_write_prscs, entry) {
298 struct pipe_resource *prsc = (struct pipe_resource *)entry->key;
299 for (int s = 0; s < PIPE_SHADER_COMPUTE; s++) {
300 /* Fragment shaders can only start executing after all
301 * binning (and thus TF) is complete.
302 *
303 * XXX: For VS/GS/TES, if the binning shader does not
304 * read the resource then we could also avoid emitting
305 * the wait.
306 */
307 if (s == PIPE_SHADER_FRAGMENT)
308 continue;
309
310 if (v3d_state_reads_resource(v3d, prsc, s)) {
311 v3d_emit_wait_for_tf(job);
312 return;
313 }
314 }
315 }
316 }
317
318 static void
319 v3d_emit_gl_shader_state(struct v3d_context *v3d,
320 const struct pipe_draw_info *info)
321 {
322 struct v3d_job *job = v3d->job;
323 /* VC5_DIRTY_VTXSTATE */
324 struct v3d_vertex_stateobj *vtx = v3d->vtx;
325 /* VC5_DIRTY_VTXBUF */
326 struct v3d_vertexbuf_stateobj *vertexbuf = &v3d->vertexbuf;
327
328 /* Upload the uniforms to the indirect CL first */
329 struct v3d_cl_reloc fs_uniforms =
330 v3d_write_uniforms(v3d, v3d->prog.fs,
331 PIPE_SHADER_FRAGMENT);
332 struct v3d_cl_reloc vs_uniforms =
333 v3d_write_uniforms(v3d, v3d->prog.vs,
334 PIPE_SHADER_VERTEX);
335 struct v3d_cl_reloc cs_uniforms =
336 v3d_write_uniforms(v3d, v3d->prog.cs,
337 PIPE_SHADER_VERTEX);
338
339 /* See GFXH-930 workaround below */
340 uint32_t num_elements_to_emit = MAX2(vtx->num_elements, 1);
341 uint32_t shader_rec_offset =
342 v3d_cl_ensure_space(&job->indirect,
343 cl_packet_length(GL_SHADER_STATE_RECORD) +
344 num_elements_to_emit *
345 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD),
346 32);
347
348 /* XXX perf: We should move most of the SHADER_STATE_RECORD setup to
349 * compile time, so that we mostly just have to OR the VS and FS
350 * records together at draw time.
351 */
352 cl_emit(&job->indirect, GL_SHADER_STATE_RECORD, shader) {
353 shader.enable_clipping = true;
354 /* VC5_DIRTY_PRIM_MODE | VC5_DIRTY_RASTERIZER */
355 shader.point_size_in_shaded_vertex_data =
356 (info->mode == PIPE_PRIM_POINTS &&
357 v3d->rasterizer->base.point_size_per_vertex);
358
359 /* Must be set if the shader modifies Z, discards, or modifies
360 * the sample mask. For any of these cases, the fragment
361 * shader needs to write the Z value (even just discards).
362 */
363 shader.fragment_shader_does_z_writes =
364 v3d->prog.fs->prog_data.fs->writes_z;
365 /* Set if the EZ test must be disabled (due to shader side
366 * effects and the early_z flag not being present in the
367 * shader).
368 */
369 shader.turn_off_early_z_test =
370 v3d->prog.fs->prog_data.fs->disable_ez;
371
372 shader.fragment_shader_uses_real_pixel_centre_w_in_addition_to_centroid_w2 =
373 v3d->prog.fs->prog_data.fs->uses_center_w;
374
375 #if V3D_VERSION >= 40
376 shader.do_scoreboard_wait_on_first_thread_switch =
377 v3d->prog.fs->prog_data.fs->lock_scoreboard_on_first_thrsw;
378 shader.disable_implicit_point_line_varyings =
379 !v3d->prog.fs->prog_data.fs->uses_implicit_point_line_varyings;
380 #endif
381
382 shader.number_of_varyings_in_fragment_shader =
383 v3d->prog.fs->prog_data.fs->num_inputs;
384
385 shader.coordinate_shader_propagate_nans = true;
386 shader.vertex_shader_propagate_nans = true;
387 shader.fragment_shader_propagate_nans = true;
388
389 shader.coordinate_shader_code_address =
390 cl_address(v3d_resource(v3d->prog.cs->resource)->bo,
391 v3d->prog.cs->offset);
392 shader.vertex_shader_code_address =
393 cl_address(v3d_resource(v3d->prog.vs->resource)->bo,
394 v3d->prog.vs->offset);
395 shader.fragment_shader_code_address =
396 cl_address(v3d_resource(v3d->prog.fs->resource)->bo,
397 v3d->prog.fs->offset);
398
399 /* XXX: Use combined input/output size flag in the common
400 * case.
401 */
402 shader.coordinate_shader_has_separate_input_and_output_vpm_blocks =
403 v3d->prog.cs->prog_data.vs->separate_segments;
404 shader.vertex_shader_has_separate_input_and_output_vpm_blocks =
405 v3d->prog.vs->prog_data.vs->separate_segments;
406
407 shader.coordinate_shader_input_vpm_segment_size =
408 v3d->prog.cs->prog_data.vs->separate_segments ?
409 v3d->prog.cs->prog_data.vs->vpm_input_size : 1;
410 shader.vertex_shader_input_vpm_segment_size =
411 v3d->prog.vs->prog_data.vs->separate_segments ?
412 v3d->prog.vs->prog_data.vs->vpm_input_size : 1;
413
414 shader.coordinate_shader_output_vpm_segment_size =
415 v3d->prog.cs->prog_data.vs->vpm_output_size;
416 shader.vertex_shader_output_vpm_segment_size =
417 v3d->prog.vs->prog_data.vs->vpm_output_size;
418
419 shader.coordinate_shader_uniforms_address = cs_uniforms;
420 shader.vertex_shader_uniforms_address = vs_uniforms;
421 shader.fragment_shader_uniforms_address = fs_uniforms;
422
423 #if V3D_VERSION >= 41
424 shader.min_coord_shader_input_segments_required_in_play = 1;
425 shader.min_vertex_shader_input_segments_required_in_play = 1;
426
427 shader.coordinate_shader_4_way_threadable =
428 v3d->prog.cs->prog_data.vs->base.threads == 4;
429 shader.vertex_shader_4_way_threadable =
430 v3d->prog.vs->prog_data.vs->base.threads == 4;
431 shader.fragment_shader_4_way_threadable =
432 v3d->prog.fs->prog_data.fs->base.threads == 4;
433
434 shader.coordinate_shader_start_in_final_thread_section =
435 v3d->prog.cs->prog_data.vs->base.single_seg;
436 shader.vertex_shader_start_in_final_thread_section =
437 v3d->prog.vs->prog_data.vs->base.single_seg;
438 shader.fragment_shader_start_in_final_thread_section =
439 v3d->prog.fs->prog_data.fs->base.single_seg;
440 #else
441 shader.coordinate_shader_4_way_threadable =
442 v3d->prog.cs->prog_data.vs->base.threads == 4;
443 shader.coordinate_shader_2_way_threadable =
444 v3d->prog.cs->prog_data.vs->base.threads == 2;
445 shader.vertex_shader_4_way_threadable =
446 v3d->prog.vs->prog_data.vs->base.threads == 4;
447 shader.vertex_shader_2_way_threadable =
448 v3d->prog.vs->prog_data.vs->base.threads == 2;
449 shader.fragment_shader_4_way_threadable =
450 v3d->prog.fs->prog_data.fs->base.threads == 4;
451 shader.fragment_shader_2_way_threadable =
452 v3d->prog.fs->prog_data.fs->base.threads == 2;
453 #endif
454
455 shader.vertex_id_read_by_coordinate_shader =
456 v3d->prog.cs->prog_data.vs->uses_vid;
457 shader.instance_id_read_by_coordinate_shader =
458 v3d->prog.cs->prog_data.vs->uses_iid;
459 shader.vertex_id_read_by_vertex_shader =
460 v3d->prog.vs->prog_data.vs->uses_vid;
461 shader.instance_id_read_by_vertex_shader =
462 v3d->prog.vs->prog_data.vs->uses_iid;
463
464 shader.address_of_default_attribute_values =
465 cl_address(v3d_resource(vtx->defaults)->bo,
466 vtx->defaults_offset);
467 }
468
469 bool cs_loaded_any = false;
470 for (int i = 0; i < vtx->num_elements; i++) {
471 struct pipe_vertex_element *elem = &vtx->pipe[i];
472 struct pipe_vertex_buffer *vb =
473 &vertexbuf->vb[elem->vertex_buffer_index];
474 struct v3d_resource *rsc = v3d_resource(vb->buffer.resource);
475
476 const uint32_t size =
477 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD);
478 cl_emit_with_prepacked(&job->indirect,
479 GL_SHADER_STATE_ATTRIBUTE_RECORD,
480 &vtx->attrs[i * size], attr) {
481 attr.stride = vb->stride;
482 attr.address = cl_address(rsc->bo,
483 vb->buffer_offset +
484 elem->src_offset);
485 attr.number_of_values_read_by_coordinate_shader =
486 v3d->prog.cs->prog_data.vs->vattr_sizes[i];
487 attr.number_of_values_read_by_vertex_shader =
488 v3d->prog.vs->prog_data.vs->vattr_sizes[i];
489
490 /* GFXH-930: At least one attribute must be enabled
491 * and read by CS and VS. If we have attributes being
492 * consumed by the VS but not the CS, then set up a
493 * dummy load of the last attribute into the CS's VPM
494 * inputs. (Since CS is just dead-code-elimination
495 * compared to VS, we can't have CS loading but not
496 * VS).
497 */
498 if (v3d->prog.cs->prog_data.vs->vattr_sizes[i])
499 cs_loaded_any = true;
500 if (i == vtx->num_elements - 1 && !cs_loaded_any) {
501 attr.number_of_values_read_by_coordinate_shader = 1;
502 }
503 #if V3D_VERSION >= 41
504 attr.maximum_index = 0xffffff;
505 #endif
506 }
507 STATIC_ASSERT(sizeof(vtx->attrs) >= V3D_MAX_VS_INPUTS / 4 * size);
508 }
509
510 if (vtx->num_elements == 0) {
511 /* GFXH-930: At least one attribute must be enabled and read
512 * by CS and VS. If we have no attributes being consumed by
513 * the shader, set up a dummy to be loaded into the VPM.
514 */
515 cl_emit(&job->indirect, GL_SHADER_STATE_ATTRIBUTE_RECORD, attr) {
516 /* Valid address of data whose value will be unused. */
517 attr.address = cl_address(job->indirect.bo, 0);
518
519 attr.type = ATTRIBUTE_FLOAT;
520 attr.stride = 0;
521 attr.vec_size = 1;
522
523 attr.number_of_values_read_by_coordinate_shader = 1;
524 attr.number_of_values_read_by_vertex_shader = 1;
525 }
526 }
527
528 cl_emit(&job->bcl, VCM_CACHE_SIZE, vcm) {
529 vcm.number_of_16_vertex_batches_for_binning =
530 v3d->prog.cs->prog_data.vs->vcm_cache_size;
531 vcm.number_of_16_vertex_batches_for_rendering =
532 v3d->prog.vs->prog_data.vs->vcm_cache_size;
533 }
534
535 cl_emit(&job->bcl, GL_SHADER_STATE, state) {
536 state.address = cl_address(job->indirect.bo, shader_rec_offset);
537 state.number_of_attribute_arrays = num_elements_to_emit;
538 }
539
540 v3d_bo_unreference(&cs_uniforms.bo);
541 v3d_bo_unreference(&vs_uniforms.bo);
542 v3d_bo_unreference(&fs_uniforms.bo);
543
544 job->shader_rec_count++;
545 }
546
547 /**
548 * Computes the various transform feedback statistics, since they can't be
549 * recorded by CL packets.
550 */
551 static void
552 v3d_tf_statistics_record(struct v3d_context *v3d,
553 const struct pipe_draw_info *info)
554 {
555 if (!v3d->active_queries)
556 return;
557
558 uint32_t prims = u_prims_for_vertices(info->mode, info->count);
559 v3d->prims_generated += prims;
560
561 if (v3d->streamout.num_targets <= 0)
562 return;
563
564 /* XXX: Only count if we didn't overflow. */
565 v3d->tf_prims_generated += prims;
566 for (int i = 0; i < v3d->streamout.num_targets; i++) {
567 struct v3d_stream_output_target *target =
568 v3d_stream_output_target(v3d->streamout.targets[i]);
569 target->recorded_vertex_count += info->count;
570 }
571 }
572
573 static void
574 v3d_update_job_ez(struct v3d_context *v3d, struct v3d_job *job)
575 {
576 switch (v3d->zsa->ez_state) {
577 case VC5_EZ_UNDECIDED:
578 /* If the Z/S state didn't pick a direction but didn't
579 * disable, then go along with the current EZ state. This
580 * allows EZ optimization for Z func == EQUAL or NEVER.
581 */
582 break;
583
584 case VC5_EZ_LT_LE:
585 case VC5_EZ_GT_GE:
586 /* If the Z/S state picked a direction, then it needs to match
587 * the current direction if we've decided on one.
588 */
589 if (job->ez_state == VC5_EZ_UNDECIDED)
590 job->ez_state = v3d->zsa->ez_state;
591 else if (job->ez_state != v3d->zsa->ez_state)
592 job->ez_state = VC5_EZ_DISABLED;
593 break;
594
595 case VC5_EZ_DISABLED:
596 /* If the current Z/S state disables EZ because of a bad Z
597 * func or stencil operation, then we can't do any more EZ in
598 * this frame.
599 */
600 job->ez_state = VC5_EZ_DISABLED;
601 break;
602 }
603
604 /* If the FS affects the Z of the pixels, then it may update against
605 * the chosen EZ direction (though we could use
606 * ARB_conservative_depth's hints to avoid this)
607 */
608 if (v3d->prog.fs->prog_data.fs->writes_z) {
609 job->ez_state = VC5_EZ_DISABLED;
610 }
611
612 if (job->first_ez_state == VC5_EZ_UNDECIDED &&
613 (job->ez_state != VC5_EZ_DISABLED || job->draw_calls_queued == 0))
614 job->first_ez_state = job->ez_state;
615 }
616
617 static void
618 v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
619 {
620 struct v3d_context *v3d = v3d_context(pctx);
621
622 if (!info->count_from_stream_output && !info->indirect &&
623 !info->primitive_restart &&
624 !u_trim_pipe_prim(info->mode, (unsigned*)&info->count))
625 return;
626
627 /* Fall back for weird desktop GL primitive restart values. */
628 if (info->primitive_restart &&
629 info->index_size) {
630 uint32_t mask = ~0;
631
632 switch (info->index_size) {
633 case 2:
634 mask = 0xffff;
635 break;
636 case 1:
637 mask = 0xff;
638 break;
639 }
640
641 if (info->restart_index != mask) {
642 util_draw_vbo_without_prim_restart(pctx, info);
643 return;
644 }
645 }
646
647 if (info->mode >= PIPE_PRIM_QUADS) {
648 util_primconvert_save_rasterizer_state(v3d->primconvert, &v3d->rasterizer->base);
649 util_primconvert_draw_vbo(v3d->primconvert, info);
650 perf_debug("Fallback conversion for %d %s vertices\n",
651 info->count, u_prim_name(info->mode));
652 return;
653 }
654
655 /* Before setting up the draw, flush anything writing to the resources
656 * that we read from or reading from resources we write to.
657 */
658 for (int s = 0; s < PIPE_SHADER_COMPUTE; s++)
659 v3d_predraw_check_stage_inputs(pctx, s);
660
661 if (info->indirect) {
662 v3d_flush_jobs_writing_resource(v3d, info->indirect->buffer,
663 false);
664 }
665
666 v3d_predraw_check_outputs(pctx);
667
668 struct v3d_job *job = v3d_get_job_for_fbo(v3d);
669
670 /* If vertex texturing depends on the output of rendering, we need to
671 * ensure that that rendering is complete before we run a coordinate
672 * shader that depends on it.
673 *
674 * Given that doing that is unusual, for now we just block the binner
675 * on the last submitted render, rather than tracking the last
676 * rendering to each texture's BO.
677 */
678 if (v3d->tex[PIPE_SHADER_VERTEX].num_textures || info->indirect) {
679 perf_debug("Blocking binner on last render "
680 "due to vertex texturing or indirect drawing.\n");
681 job->submit.in_sync_bcl = v3d->out_sync;
682 }
683
684 /* Mark SSBOs as being written. We don't actually know which ones are
685 * read vs written, so just assume the worst
686 */
687 for (int s = 0; s < PIPE_SHADER_COMPUTE; s++) {
688 foreach_bit(i, v3d->ssbo[s].enabled_mask) {
689 v3d_job_add_write_resource(job,
690 v3d->ssbo[s].sb[i].buffer);
691 job->tmu_dirty_rcl = true;
692 }
693
694 foreach_bit(i, v3d->shaderimg[s].enabled_mask) {
695 v3d_job_add_write_resource(job,
696 v3d->shaderimg[s].si[i].base.resource);
697 job->tmu_dirty_rcl = true;
698 }
699 }
700
701 /* Get space to emit our draw call into the BCL, using a branch to
702 * jump to a new BO if necessary.
703 */
704 v3d_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);
705
706 if (v3d->prim_mode != info->mode) {
707 v3d->prim_mode = info->mode;
708 v3d->dirty |= VC5_DIRTY_PRIM_MODE;
709 }
710
711 v3d_start_draw(v3d);
712 v3d_update_compiled_shaders(v3d, info->mode);
713 v3d_update_job_ez(v3d, job);
714
715 /* If this job was writing to transform feedback buffers before this
716 * draw and we are reading from them here, then we need to wait for TF
717 * to complete before we emit this draw.
718 *
719 * Notice this check needs to happen before we emit state for the
720 * current draw call, where we update job->tf_enabled, so we can ensure
721 * that we only check TF writes for prior draws.
722 */
723 v3d_emit_wait_for_tf_if_needed(v3d, job);
724
725 #if V3D_VERSION >= 41
726 v3d41_emit_state(pctx);
727 #else
728 v3d33_emit_state(pctx);
729 #endif
730
731 if (v3d->dirty & (VC5_DIRTY_VTXBUF |
732 VC5_DIRTY_VTXSTATE |
733 VC5_DIRTY_PRIM_MODE |
734 VC5_DIRTY_RASTERIZER |
735 VC5_DIRTY_COMPILED_CS |
736 VC5_DIRTY_COMPILED_VS |
737 VC5_DIRTY_COMPILED_FS |
738 v3d->prog.cs->uniform_dirty_bits |
739 v3d->prog.vs->uniform_dirty_bits |
740 v3d->prog.fs->uniform_dirty_bits)) {
741 v3d_emit_gl_shader_state(v3d, info);
742 }
743
744 v3d->dirty = 0;
745
746 /* The Base Vertex/Base Instance packet sets those values to nonzero
747 * for the next draw call only.
748 */
749 if (info->index_bias || info->start_instance) {
750 cl_emit(&job->bcl, BASE_VERTEX_BASE_INSTANCE, base) {
751 base.base_instance = info->start_instance;
752 base.base_vertex = info->index_bias;
753 }
754 }
755
756 uint32_t prim_tf_enable = 0;
757 #if V3D_VERSION < 40
758 /* V3D 3.x: The HW only processes transform feedback on primitives
759 * with the flag set.
760 */
761 if (v3d->streamout.num_targets)
762 prim_tf_enable = (V3D_PRIM_POINTS_TF - V3D_PRIM_POINTS);
763 #endif
764
765 v3d_tf_statistics_record(v3d, info);
766
767 /* Note that the primitive type fields match with OpenGL/gallium
768 * definitions, up to but not including QUADS.
769 */
770 if (info->index_size) {
771 uint32_t index_size = info->index_size;
772 uint32_t offset = info->start * index_size;
773 struct pipe_resource *prsc;
774 if (info->has_user_indices) {
775 prsc = NULL;
776 u_upload_data(v3d->uploader, 0,
777 info->count * info->index_size, 4,
778 info->index.user,
779 &offset, &prsc);
780 } else {
781 prsc = info->index.resource;
782 }
783 struct v3d_resource *rsc = v3d_resource(prsc);
784
785 #if V3D_VERSION >= 40
786 cl_emit(&job->bcl, INDEX_BUFFER_SETUP, ib) {
787 ib.address = cl_address(rsc->bo, 0);
788 ib.size = rsc->bo->size;
789 }
790 #endif
791
792 if (info->indirect) {
793 cl_emit(&job->bcl, INDIRECT_INDEXED_INSTANCED_PRIM_LIST, prim) {
794 prim.index_type = ffs(info->index_size) - 1;
795 #if V3D_VERSION < 40
796 prim.address_of_indices_list =
797 cl_address(rsc->bo, offset);
798 #endif /* V3D_VERSION < 40 */
799 prim.mode = info->mode | prim_tf_enable;
800 prim.enable_primitive_restarts = info->primitive_restart;
801
802 prim.number_of_draw_indirect_indexed_records = info->indirect->draw_count;
803
804 prim.stride_in_multiples_of_4_bytes = info->indirect->stride >> 2;
805 prim.address = cl_address(v3d_resource(info->indirect->buffer)->bo,
806 info->indirect->offset);
807 }
808 } else if (info->instance_count > 1) {
809 cl_emit(&job->bcl, INDEXED_INSTANCED_PRIM_LIST, prim) {
810 prim.index_type = ffs(info->index_size) - 1;
811 #if V3D_VERSION >= 40
812 prim.index_offset = offset;
813 #else /* V3D_VERSION < 40 */
814 prim.maximum_index = (1u << 31) - 1; /* XXX */
815 prim.address_of_indices_list =
816 cl_address(rsc->bo, offset);
817 #endif /* V3D_VERSION < 40 */
818 prim.mode = info->mode | prim_tf_enable;
819 prim.enable_primitive_restarts = info->primitive_restart;
820
821 prim.number_of_instances = info->instance_count;
822 prim.instance_length = info->count;
823 }
824 } else {
825 cl_emit(&job->bcl, INDEXED_PRIM_LIST, prim) {
826 prim.index_type = ffs(info->index_size) - 1;
827 prim.length = info->count;
828 #if V3D_VERSION >= 40
829 prim.index_offset = offset;
830 #else /* V3D_VERSION < 40 */
831 prim.maximum_index = (1u << 31) - 1; /* XXX */
832 prim.address_of_indices_list =
833 cl_address(rsc->bo, offset);
834 #endif /* V3D_VERSION < 40 */
835 prim.mode = info->mode | prim_tf_enable;
836 prim.enable_primitive_restarts = info->primitive_restart;
837 }
838 }
839
840 job->draw_calls_queued++;
841
842 if (info->has_user_indices)
843 pipe_resource_reference(&prsc, NULL);
844 } else {
845 if (info->indirect) {
846 cl_emit(&job->bcl, INDIRECT_VERTEX_ARRAY_INSTANCED_PRIMS, prim) {
847 prim.mode = info->mode | prim_tf_enable;
848 prim.number_of_draw_indirect_array_records = info->indirect->draw_count;
849
850 prim.stride_in_multiples_of_4_bytes = info->indirect->stride >> 2;
851 prim.address = cl_address(v3d_resource(info->indirect->buffer)->bo,
852 info->indirect->offset);
853 }
854 } else if (info->instance_count > 1) {
855 cl_emit(&job->bcl, VERTEX_ARRAY_INSTANCED_PRIMS, prim) {
856 prim.mode = info->mode | prim_tf_enable;
857 prim.index_of_first_vertex = info->start;
858 prim.number_of_instances = info->instance_count;
859 prim.instance_length = info->count;
860 }
861 } else {
862 cl_emit(&job->bcl, VERTEX_ARRAY_PRIMS, prim) {
863 prim.mode = info->mode | prim_tf_enable;
864 prim.length = info->count;
865 prim.index_of_first_vertex = info->start;
866 }
867 }
868 }
869
870 /* A flush is required in between a TF draw and any following TF specs
871 * packet, or the GPU may hang. Just flush each time for now.
872 */
873 if (v3d->streamout.num_targets)
874 cl_emit(&job->bcl, TRANSFORM_FEEDBACK_FLUSH_AND_COUNT, flush);
875
876 job->draw_calls_queued++;
877
878 /* Increment the TF offsets by how many verts we wrote. XXX: This
879 * needs some clamping to the buffer size.
880 */
881 for (int i = 0; i < v3d->streamout.num_targets; i++)
882 v3d->streamout.offsets[i] += info->count;
883
884 if (v3d->zsa && job->zsbuf && v3d->zsa->base.depth.enabled) {
885 struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);
886 v3d_job_add_bo(job, rsc->bo);
887
888 job->load |= PIPE_CLEAR_DEPTH & ~job->clear;
889 if (v3d->zsa->base.depth.writemask)
890 job->store |= PIPE_CLEAR_DEPTH;
891 rsc->initialized_buffers = PIPE_CLEAR_DEPTH;
892 }
893
894 if (v3d->zsa && job->zsbuf && v3d->zsa->base.stencil[0].enabled) {
895 struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);
896 if (rsc->separate_stencil)
897 rsc = rsc->separate_stencil;
898
899 v3d_job_add_bo(job, rsc->bo);
900
901 job->load |= PIPE_CLEAR_STENCIL & ~job->clear;
902 if (v3d->zsa->base.stencil[0].writemask ||
903 v3d->zsa->base.stencil[1].writemask) {
904 job->store |= PIPE_CLEAR_STENCIL;
905 }
906 rsc->initialized_buffers |= PIPE_CLEAR_STENCIL;
907 }
908
909 for (int i = 0; i < V3D_MAX_DRAW_BUFFERS; i++) {
910 uint32_t bit = PIPE_CLEAR_COLOR0 << i;
911 int blend_rt = v3d->blend->base.independent_blend_enable ? i : 0;
912
913 if (job->store & bit || !job->cbufs[i])
914 continue;
915 struct v3d_resource *rsc = v3d_resource(job->cbufs[i]->texture);
916
917 job->load |= bit & ~job->clear;
918 if (v3d->blend->base.rt[blend_rt].colormask)
919 job->store |= bit;
920 v3d_job_add_bo(job, rsc->bo);
921 }
922
923 if (job->referenced_size > 768 * 1024 * 1024) {
924 perf_debug("Flushing job with %dkb to try to free up memory\n",
925 job->referenced_size / 1024);
926 v3d_flush(pctx);
927 }
928
929 if (V3D_DEBUG & V3D_DEBUG_ALWAYS_FLUSH)
930 v3d_flush(pctx);
931 }
932
933 /**
934 * Implements gallium's clear() hook (glClear()) by drawing a pair of triangles.
935 */
936 static void
937 v3d_draw_clear(struct v3d_context *v3d,
938 unsigned buffers,
939 const union pipe_color_union *color,
940 double depth, unsigned stencil)
941 {
942 static const union pipe_color_union dummy_color = {};
943
944 /* The blitter util dereferences the color regardless, even though the
945 * gallium clear API may not pass one in when only Z/S are cleared.
946 */
947 if (!color)
948 color = &dummy_color;
949
950 v3d_blitter_save(v3d);
951 util_blitter_clear(v3d->blitter,
952 v3d->framebuffer.width,
953 v3d->framebuffer.height,
954 util_framebuffer_get_num_layers(&v3d->framebuffer),
955 buffers, color, depth, stencil,
956 util_framebuffer_get_num_samples(&v3d->framebuffer) > 1);
957 }
958
959 /**
960 * Attempts to perform the GL clear by using the TLB's fast clear at the start
961 * of the frame.
962 */
963 static unsigned
964 v3d_tlb_clear(struct v3d_job *job, unsigned buffers,
965 const union pipe_color_union *color,
966 double depth, unsigned stencil)
967 {
968 struct v3d_context *v3d = job->v3d;
969
970 if (job->draw_calls_queued) {
971 /* If anything in the CL has drawn using the buffer, then the
972 * TLB clear we're trying to add now would happen before that
973 * drawing.
974 */
975 buffers &= ~(job->load | job->store);
976 }
977
978 /* GFXH-1461: If we were to emit a load of just depth or just stencil,
979 * then the clear for the other may get lost. We need to decide now
980 * if it would be possible to need to emit a load of just one after
981 * we've set up our TLB clears.
982 */
983 if (buffers & PIPE_CLEAR_DEPTHSTENCIL &&
984 (buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL &&
985 job->zsbuf &&
986 util_format_is_depth_and_stencil(job->zsbuf->texture->format)) {
987 buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
988 }
989
990 for (int i = 0; i < V3D_MAX_DRAW_BUFFERS; i++) {
991 uint32_t bit = PIPE_CLEAR_COLOR0 << i;
992 if (!(buffers & bit))
993 continue;
994
995 struct pipe_surface *psurf = v3d->framebuffer.cbufs[i];
996 struct v3d_surface *surf = v3d_surface(psurf);
997 struct v3d_resource *rsc = v3d_resource(psurf->texture);
998
999 union util_color uc;
1000 uint32_t internal_size = 4 << surf->internal_bpp;
1001
1002 static union pipe_color_union swapped_color;
1003 if (v3d->swap_color_rb & (1 << i)) {
1004 swapped_color.f[0] = color->f[2];
1005 swapped_color.f[1] = color->f[1];
1006 swapped_color.f[2] = color->f[0];
1007 swapped_color.f[3] = color->f[3];
1008 color = &swapped_color;
1009 }
1010
1011 switch (surf->internal_type) {
1012 case V3D_INTERNAL_TYPE_8:
1013 util_pack_color(color->f, PIPE_FORMAT_R8G8B8A8_UNORM,
1014 &uc);
1015 memcpy(job->clear_color[i], uc.ui, internal_size);
1016 break;
1017 case V3D_INTERNAL_TYPE_8I:
1018 case V3D_INTERNAL_TYPE_8UI:
1019 job->clear_color[i][0] = ((color->ui[0] & 0xff) |
1020 (color->ui[1] & 0xff) << 8 |
1021 (color->ui[2] & 0xff) << 16 |
1022 (color->ui[3] & 0xff) << 24);
1023 break;
1024 case V3D_INTERNAL_TYPE_16F:
1025 util_pack_color(color->f, PIPE_FORMAT_R16G16B16A16_FLOAT,
1026 &uc);
1027 memcpy(job->clear_color[i], uc.ui, internal_size);
1028 break;
1029 case V3D_INTERNAL_TYPE_16I:
1030 case V3D_INTERNAL_TYPE_16UI:
1031 job->clear_color[i][0] = ((color->ui[0] & 0xffff) |
1032 color->ui[1] << 16);
1033 job->clear_color[i][1] = ((color->ui[2] & 0xffff) |
1034 color->ui[3] << 16);
1035 break;
1036 case V3D_INTERNAL_TYPE_32F:
1037 case V3D_INTERNAL_TYPE_32I:
1038 case V3D_INTERNAL_TYPE_32UI:
1039 memcpy(job->clear_color[i], color->ui, internal_size);
1040 break;
1041 }
1042
1043 rsc->initialized_buffers |= bit;
1044 }
1045
1046 unsigned zsclear = buffers & PIPE_CLEAR_DEPTHSTENCIL;
1047 if (zsclear) {
1048 struct v3d_resource *rsc =
1049 v3d_resource(v3d->framebuffer.zsbuf->texture);
1050
1051 if (zsclear & PIPE_CLEAR_DEPTH)
1052 job->clear_z = depth;
1053 if (zsclear & PIPE_CLEAR_STENCIL)
1054 job->clear_s = stencil;
1055
1056 rsc->initialized_buffers |= zsclear;
1057 }
1058
1059 job->draw_min_x = 0;
1060 job->draw_min_y = 0;
1061 job->draw_max_x = v3d->framebuffer.width;
1062 job->draw_max_y = v3d->framebuffer.height;
1063 job->clear |= buffers;
1064 job->store |= buffers;
1065
1066 v3d_start_draw(v3d);
1067
1068 return buffers;
1069 }
1070
1071 static void
1072 v3d_clear(struct pipe_context *pctx, unsigned buffers,
1073 const union pipe_color_union *color, double depth, unsigned stencil)
1074 {
1075 struct v3d_context *v3d = v3d_context(pctx);
1076 struct v3d_job *job = v3d_get_job_for_fbo(v3d);
1077
1078 buffers &= ~v3d_tlb_clear(job, buffers, color, depth, stencil);
1079
1080 if (buffers)
1081 v3d_draw_clear(v3d, buffers, color, depth, stencil);
1082 }
1083
1084 static void
1085 v3d_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,
1086 const union pipe_color_union *color,
1087 unsigned x, unsigned y, unsigned w, unsigned h,
1088 bool render_condition_enabled)
1089 {
1090 fprintf(stderr, "unimpl: clear RT\n");
1091 }
1092
1093 static void
1094 v3d_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,
1095 unsigned buffers, double depth, unsigned stencil,
1096 unsigned x, unsigned y, unsigned w, unsigned h,
1097 bool render_condition_enabled)
1098 {
1099 fprintf(stderr, "unimpl: clear DS\n");
1100 }
1101
1102 void
1103 v3dX(draw_init)(struct pipe_context *pctx)
1104 {
1105 pctx->draw_vbo = v3d_draw_vbo;
1106 pctx->clear = v3d_clear;
1107 pctx->clear_render_target = v3d_clear_render_target;
1108 pctx->clear_depth_stencil = v3d_clear_depth_stencil;
1109 }