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