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