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