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