v3d: Stop using spaces in the names of our buffers.
[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 /* Get space to emit our draw call into the BCL, using a branch to
429 * jump to a new BO if necessary.
430 */
431 v3d_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);
432
433 if (v3d->prim_mode != info->mode) {
434 v3d->prim_mode = info->mode;
435 v3d->dirty |= VC5_DIRTY_PRIM_MODE;
436 }
437
438 v3d_start_draw(v3d);
439 v3d_update_compiled_shaders(v3d, info->mode);
440 v3d_update_job_ez(v3d, job);
441
442 #if V3D_VERSION >= 41
443 v3d41_emit_state(pctx);
444 #else
445 v3d33_emit_state(pctx);
446 #endif
447
448 if (v3d->dirty & (VC5_DIRTY_VTXBUF |
449 VC5_DIRTY_VTXSTATE |
450 VC5_DIRTY_PRIM_MODE |
451 VC5_DIRTY_RASTERIZER |
452 VC5_DIRTY_COMPILED_CS |
453 VC5_DIRTY_COMPILED_VS |
454 VC5_DIRTY_COMPILED_FS |
455 v3d->prog.cs->uniform_dirty_bits |
456 v3d->prog.vs->uniform_dirty_bits |
457 v3d->prog.fs->uniform_dirty_bits)) {
458 v3d_emit_gl_shader_state(v3d, info);
459 }
460
461 v3d->dirty = 0;
462
463 /* The Base Vertex/Base Instance packet sets those values to nonzero
464 * for the next draw call only.
465 */
466 if (info->index_bias || info->start_instance) {
467 cl_emit(&job->bcl, BASE_VERTEX_BASE_INSTANCE, base) {
468 base.base_instance = info->start_instance;
469 base.base_vertex = info->index_bias;
470 }
471 }
472
473 uint32_t prim_tf_enable = 0;
474 #if V3D_VERSION < 40
475 /* V3D 3.x: The HW only processes transform feedback on primitives
476 * with the flag set.
477 */
478 if (v3d->streamout.num_targets)
479 prim_tf_enable = (V3D_PRIM_POINTS_TF - V3D_PRIM_POINTS);
480 #endif
481
482 v3d_tf_statistics_record(v3d, info, v3d->streamout.num_targets);
483
484 /* Note that the primitive type fields match with OpenGL/gallium
485 * definitions, up to but not including QUADS.
486 */
487 if (info->index_size) {
488 uint32_t index_size = info->index_size;
489 uint32_t offset = info->start * index_size;
490 struct pipe_resource *prsc;
491 if (info->has_user_indices) {
492 prsc = NULL;
493 u_upload_data(v3d->uploader, 0,
494 info->count * info->index_size, 4,
495 info->index.user,
496 &offset, &prsc);
497 } else {
498 prsc = info->index.resource;
499 }
500 struct v3d_resource *rsc = v3d_resource(prsc);
501
502 #if V3D_VERSION >= 40
503 cl_emit(&job->bcl, INDEX_BUFFER_SETUP, ib) {
504 ib.address = cl_address(rsc->bo, 0);
505 ib.size = rsc->bo->size;
506 }
507 #endif
508
509 if (info->instance_count > 1) {
510 cl_emit(&job->bcl, INDEXED_INSTANCED_PRIMITIVE_LIST, prim) {
511 prim.index_type = ffs(info->index_size) - 1;
512 #if V3D_VERSION >= 40
513 prim.index_offset = offset;
514 #else /* V3D_VERSION < 40 */
515 prim.maximum_index = (1u << 31) - 1; /* XXX */
516 prim.address_of_indices_list =
517 cl_address(rsc->bo, offset);
518 #endif /* V3D_VERSION < 40 */
519 prim.mode = info->mode | prim_tf_enable;
520 prim.enable_primitive_restarts = info->primitive_restart;
521
522 prim.number_of_instances = info->instance_count;
523 prim.instance_length = info->count;
524 }
525 } else {
526 cl_emit(&job->bcl, INDEXED_PRIMITIVE_LIST, prim) {
527 prim.index_type = ffs(info->index_size) - 1;
528 prim.length = info->count;
529 #if V3D_VERSION >= 40
530 prim.index_offset = offset;
531 #else /* V3D_VERSION < 40 */
532 prim.maximum_index = (1u << 31) - 1; /* XXX */
533 prim.address_of_indices_list =
534 cl_address(rsc->bo, offset);
535 #endif /* V3D_VERSION < 40 */
536 prim.mode = info->mode | prim_tf_enable;
537 prim.enable_primitive_restarts = info->primitive_restart;
538 }
539 }
540
541 job->draw_calls_queued++;
542
543 if (info->has_user_indices)
544 pipe_resource_reference(&prsc, NULL);
545 } else {
546 if (info->instance_count > 1) {
547 cl_emit(&job->bcl, VERTEX_ARRAY_INSTANCED_PRIMITIVES, prim) {
548 prim.mode = info->mode | prim_tf_enable;
549 prim.index_of_first_vertex = info->start;
550 prim.number_of_instances = info->instance_count;
551 prim.instance_length = info->count;
552 }
553 } else {
554 cl_emit(&job->bcl, VERTEX_ARRAY_PRIMITIVES, prim) {
555 prim.mode = info->mode | prim_tf_enable;
556 prim.length = info->count;
557 prim.index_of_first_vertex = info->start;
558 }
559 }
560 }
561
562 /* A flush is required in between a TF draw and any following TF specs
563 * packet, or the GPU may hang. Just flush each time for now.
564 */
565 if (v3d->streamout.num_targets)
566 cl_emit(&job->bcl, TRANSFORM_FEEDBACK_FLUSH_AND_COUNT, flush);
567
568 job->draw_calls_queued++;
569
570 /* Increment the TF offsets by how many verts we wrote. XXX: This
571 * needs some clamping to the buffer size.
572 */
573 for (int i = 0; i < v3d->streamout.num_targets; i++)
574 v3d->streamout.offsets[i] += info->count;
575
576 if (v3d->zsa && job->zsbuf && v3d->zsa->base.depth.enabled) {
577 struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);
578 v3d_job_add_bo(job, rsc->bo);
579
580 job->load |= PIPE_CLEAR_DEPTH & ~job->clear;
581 if (v3d->zsa->base.depth.writemask)
582 job->store |= PIPE_CLEAR_DEPTH;
583 rsc->initialized_buffers = PIPE_CLEAR_DEPTH;
584 }
585
586 if (v3d->zsa && job->zsbuf && v3d->zsa->base.stencil[0].enabled) {
587 struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);
588 if (rsc->separate_stencil)
589 rsc = rsc->separate_stencil;
590
591 v3d_job_add_bo(job, rsc->bo);
592
593 job->load |= PIPE_CLEAR_STENCIL & ~job->clear;
594 if (v3d->zsa->base.stencil[0].writemask ||
595 v3d->zsa->base.stencil[1].writemask) {
596 job->store |= PIPE_CLEAR_STENCIL;
597 }
598 rsc->initialized_buffers |= PIPE_CLEAR_STENCIL;
599 }
600
601 for (int i = 0; i < VC5_MAX_DRAW_BUFFERS; i++) {
602 uint32_t bit = PIPE_CLEAR_COLOR0 << i;
603 int blend_rt = v3d->blend->base.independent_blend_enable ? i : 0;
604
605 if (job->store & bit || !job->cbufs[i])
606 continue;
607 struct v3d_resource *rsc = v3d_resource(job->cbufs[i]->texture);
608
609 job->load |= bit & ~job->clear;
610 if (v3d->blend->base.rt[blend_rt].colormask)
611 job->store |= bit;
612 v3d_job_add_bo(job, rsc->bo);
613 }
614
615 if (job->referenced_size > 768 * 1024 * 1024) {
616 perf_debug("Flushing job with %dkb to try to free up memory\n",
617 job->referenced_size / 1024);
618 v3d_flush(pctx);
619 }
620
621 if (V3D_DEBUG & V3D_DEBUG_ALWAYS_FLUSH)
622 v3d_flush(pctx);
623 }
624
625 /**
626 * Implements gallium's clear() hook (glClear()) by drawing a pair of triangles.
627 */
628 static void
629 v3d_draw_clear(struct v3d_context *v3d,
630 unsigned buffers,
631 const union pipe_color_union *color,
632 double depth, unsigned stencil)
633 {
634 static const union pipe_color_union dummy_color = {};
635
636 /* The blitter util dereferences the color regardless, even though the
637 * gallium clear API may not pass one in when only Z/S are cleared.
638 */
639 if (!color)
640 color = &dummy_color;
641
642 v3d_blitter_save(v3d);
643 util_blitter_clear(v3d->blitter,
644 v3d->framebuffer.width,
645 v3d->framebuffer.height,
646 util_framebuffer_get_num_layers(&v3d->framebuffer),
647 buffers, color, depth, stencil);
648 }
649
650 /**
651 * Attempts to perform the GL clear by using the TLB's fast clear at the start
652 * of the frame.
653 */
654 static unsigned
655 v3d_tlb_clear(struct v3d_job *job, unsigned buffers,
656 const union pipe_color_union *color,
657 double depth, unsigned stencil)
658 {
659 struct v3d_context *v3d = job->v3d;
660
661 if (job->draw_calls_queued) {
662 /* If anything in the CL has drawn using the buffer, then the
663 * TLB clear we're trying to add now would happen before that
664 * drawing.
665 */
666 buffers &= ~(job->load | job->store);
667 }
668
669 /* GFXH-1461: If we were to emit a load of just depth or just stencil,
670 * then the clear for the other may get lost. We need to decide now
671 * if it would be possible to need to emit a load of just one after
672 * we've set up our TLB clears.
673 */
674 if (buffers & PIPE_CLEAR_DEPTHSTENCIL &&
675 (buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL &&
676 job->zsbuf &&
677 util_format_is_depth_and_stencil(job->zsbuf->texture->format)) {
678 buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
679 }
680
681 for (int i = 0; i < VC5_MAX_DRAW_BUFFERS; i++) {
682 uint32_t bit = PIPE_CLEAR_COLOR0 << i;
683 if (!(buffers & bit))
684 continue;
685
686 struct pipe_surface *psurf = v3d->framebuffer.cbufs[i];
687 struct v3d_surface *surf = v3d_surface(psurf);
688 struct v3d_resource *rsc = v3d_resource(psurf->texture);
689
690 union util_color uc;
691 uint32_t internal_size = 4 << surf->internal_bpp;
692
693 static union pipe_color_union swapped_color;
694 if (v3d->swap_color_rb & (1 << i)) {
695 swapped_color.f[0] = color->f[2];
696 swapped_color.f[1] = color->f[1];
697 swapped_color.f[2] = color->f[0];
698 swapped_color.f[3] = color->f[3];
699 color = &swapped_color;
700 }
701
702 switch (surf->internal_type) {
703 case V3D_INTERNAL_TYPE_8:
704 util_pack_color(color->f, PIPE_FORMAT_R8G8B8A8_UNORM,
705 &uc);
706 memcpy(job->clear_color[i], uc.ui, internal_size);
707 break;
708 case V3D_INTERNAL_TYPE_8I:
709 case V3D_INTERNAL_TYPE_8UI:
710 job->clear_color[i][0] = ((color->ui[0] & 0xff) |
711 (color->ui[1] & 0xff) << 8 |
712 (color->ui[2] & 0xff) << 16 |
713 (color->ui[3] & 0xff) << 24);
714 break;
715 case V3D_INTERNAL_TYPE_16F:
716 util_pack_color(color->f, PIPE_FORMAT_R16G16B16A16_FLOAT,
717 &uc);
718 memcpy(job->clear_color[i], uc.ui, internal_size);
719 break;
720 case V3D_INTERNAL_TYPE_16I:
721 case V3D_INTERNAL_TYPE_16UI:
722 job->clear_color[i][0] = ((color->ui[0] & 0xffff) |
723 color->ui[1] << 16);
724 job->clear_color[i][1] = ((color->ui[2] & 0xffff) |
725 color->ui[3] << 16);
726 break;
727 case V3D_INTERNAL_TYPE_32F:
728 case V3D_INTERNAL_TYPE_32I:
729 case V3D_INTERNAL_TYPE_32UI:
730 memcpy(job->clear_color[i], color->ui, internal_size);
731 break;
732 }
733
734 rsc->initialized_buffers |= bit;
735 }
736
737 unsigned zsclear = buffers & PIPE_CLEAR_DEPTHSTENCIL;
738 if (zsclear) {
739 struct v3d_resource *rsc =
740 v3d_resource(v3d->framebuffer.zsbuf->texture);
741
742 if (zsclear & PIPE_CLEAR_DEPTH)
743 job->clear_z = depth;
744 if (zsclear & PIPE_CLEAR_STENCIL)
745 job->clear_s = stencil;
746
747 rsc->initialized_buffers |= zsclear;
748 }
749
750 job->draw_min_x = 0;
751 job->draw_min_y = 0;
752 job->draw_max_x = v3d->framebuffer.width;
753 job->draw_max_y = v3d->framebuffer.height;
754 job->clear |= buffers;
755 job->store |= buffers;
756
757 v3d_start_draw(v3d);
758
759 return buffers;
760 }
761
762 static void
763 v3d_clear(struct pipe_context *pctx, unsigned buffers,
764 const union pipe_color_union *color, double depth, unsigned stencil)
765 {
766 struct v3d_context *v3d = v3d_context(pctx);
767 struct v3d_job *job = v3d_get_job_for_fbo(v3d);
768
769 buffers &= ~v3d_tlb_clear(job, buffers, color, depth, stencil);
770
771 if (buffers)
772 v3d_draw_clear(v3d, buffers, color, depth, stencil);
773 }
774
775 static void
776 v3d_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,
777 const union pipe_color_union *color,
778 unsigned x, unsigned y, unsigned w, unsigned h,
779 bool render_condition_enabled)
780 {
781 fprintf(stderr, "unimpl: clear RT\n");
782 }
783
784 static void
785 v3d_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,
786 unsigned buffers, double depth, unsigned stencil,
787 unsigned x, unsigned y, unsigned w, unsigned h,
788 bool render_condition_enabled)
789 {
790 fprintf(stderr, "unimpl: clear DS\n");
791 }
792
793 void
794 v3dX(draw_init)(struct pipe_context *pctx)
795 {
796 pctx->draw_vbo = v3d_draw_vbo;
797 pctx->clear = v3d_clear;
798 pctx->clear_render_target = v3d_clear_render_target;
799 pctx->clear_depth_stencil = v3d_clear_depth_stencil;
800 }