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