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