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