v3d: Fix shaders using pixel center W but no varyings.
[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_minus_1 = v3d->framebuffer.width - 1;
82 config.height_in_pixels_minus_1 = v3d->framebuffer.height - 1;
83 config.number_of_render_targets_minus_1 =
84 MAX2(v3d->framebuffer.nr_cbufs, 1) - 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.coordinate_shader_4_way_threadable =
218 v3d->prog.cs->prog_data.vs->base.threads == 4;
219 shader.vertex_shader_4_way_threadable =
220 v3d->prog.vs->prog_data.vs->base.threads == 4;
221 shader.fragment_shader_4_way_threadable =
222 v3d->prog.fs->prog_data.fs->base.threads == 4;
223
224 shader.coordinate_shader_start_in_final_thread_section =
225 v3d->prog.cs->prog_data.vs->base.single_seg;
226 shader.vertex_shader_start_in_final_thread_section =
227 v3d->prog.vs->prog_data.vs->base.single_seg;
228 shader.fragment_shader_start_in_final_thread_section =
229 v3d->prog.fs->prog_data.fs->base.single_seg;
230 #else
231 shader.coordinate_shader_4_way_threadable =
232 v3d->prog.cs->prog_data.vs->base.threads == 4;
233 shader.coordinate_shader_2_way_threadable =
234 v3d->prog.cs->prog_data.vs->base.threads == 2;
235 shader.vertex_shader_4_way_threadable =
236 v3d->prog.vs->prog_data.vs->base.threads == 4;
237 shader.vertex_shader_2_way_threadable =
238 v3d->prog.vs->prog_data.vs->base.threads == 2;
239 shader.fragment_shader_4_way_threadable =
240 v3d->prog.fs->prog_data.fs->base.threads == 4;
241 shader.fragment_shader_2_way_threadable =
242 v3d->prog.fs->prog_data.fs->base.threads == 2;
243 #endif
244
245 shader.vertex_id_read_by_coordinate_shader =
246 v3d->prog.cs->prog_data.vs->uses_vid;
247 shader.instance_id_read_by_coordinate_shader =
248 v3d->prog.cs->prog_data.vs->uses_iid;
249 shader.vertex_id_read_by_vertex_shader =
250 v3d->prog.vs->prog_data.vs->uses_vid;
251 shader.instance_id_read_by_vertex_shader =
252 v3d->prog.vs->prog_data.vs->uses_iid;
253
254 shader.address_of_default_attribute_values =
255 cl_address(vtx->default_attribute_values, 0);
256 }
257
258 for (int i = 0; i < vtx->num_elements; i++) {
259 struct pipe_vertex_element *elem = &vtx->pipe[i];
260 struct pipe_vertex_buffer *vb =
261 &vertexbuf->vb[elem->vertex_buffer_index];
262 struct v3d_resource *rsc = v3d_resource(vb->buffer.resource);
263
264 const uint32_t size =
265 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD);
266 cl_emit_with_prepacked(&job->indirect,
267 GL_SHADER_STATE_ATTRIBUTE_RECORD,
268 &vtx->attrs[i * size], attr) {
269 attr.stride = vb->stride;
270 attr.address = cl_address(rsc->bo,
271 vb->buffer_offset +
272 elem->src_offset);
273 attr.number_of_values_read_by_coordinate_shader =
274 v3d->prog.cs->prog_data.vs->vattr_sizes[i];
275 attr.number_of_values_read_by_vertex_shader =
276 v3d->prog.vs->prog_data.vs->vattr_sizes[i];
277 #if V3D_VERSION >= 41
278 attr.maximum_index = 0xffffff;
279 #endif
280 }
281 STATIC_ASSERT(sizeof(vtx->attrs) >= VC5_MAX_ATTRIBUTES * size);
282 }
283
284 if (vtx->num_elements == 0) {
285 /* GFXH-930: At least one attribute must be enabled and read
286 * by CS and VS. If we have no attributes being consumed by
287 * the shader, set up a dummy to be loaded into the VPM.
288 */
289 cl_emit(&job->indirect, GL_SHADER_STATE_ATTRIBUTE_RECORD, attr) {
290 /* Valid address of data whose value will be unused. */
291 attr.address = cl_address(job->indirect.bo, 0);
292
293 attr.type = ATTRIBUTE_FLOAT;
294 attr.stride = 0;
295 attr.vec_size = 1;
296
297 attr.number_of_values_read_by_coordinate_shader = 1;
298 attr.number_of_values_read_by_vertex_shader = 1;
299 }
300 }
301
302 cl_emit(&job->bcl, GL_SHADER_STATE, state) {
303 state.address = cl_address(job->indirect.bo, shader_rec_offset);
304 state.number_of_attribute_arrays = num_elements_to_emit;
305 }
306
307 v3d_bo_unreference(&cs_uniforms.bo);
308 v3d_bo_unreference(&vs_uniforms.bo);
309 v3d_bo_unreference(&fs_uniforms.bo);
310
311 job->shader_rec_count++;
312 }
313
314 /**
315 * Computes the various transform feedback statistics, since they can't be
316 * recorded by CL packets.
317 */
318 static void
319 v3d_tf_statistics_record(struct v3d_context *v3d,
320 const struct pipe_draw_info *info,
321 bool prim_tf)
322 {
323 if (!v3d->active_queries)
324 return;
325
326 uint32_t prims = u_prims_for_vertices(info->mode, info->count);
327 v3d->prims_generated += prims;
328
329 if (prim_tf) {
330 /* XXX: Only count if we didn't overflow. */
331 v3d->tf_prims_generated += prims;
332 }
333 }
334
335 static void
336 v3d_update_job_ez(struct v3d_context *v3d, struct v3d_job *job)
337 {
338 switch (v3d->zsa->ez_state) {
339 case VC5_EZ_UNDECIDED:
340 /* If the Z/S state didn't pick a direction but didn't
341 * disable, then go along with the current EZ state. This
342 * allows EZ optimization for Z func == EQUAL or NEVER.
343 */
344 break;
345
346 case VC5_EZ_LT_LE:
347 case VC5_EZ_GT_GE:
348 /* If the Z/S state picked a direction, then it needs to match
349 * the current direction if we've decided on one.
350 */
351 if (job->ez_state == VC5_EZ_UNDECIDED)
352 job->ez_state = v3d->zsa->ez_state;
353 else if (job->ez_state != v3d->zsa->ez_state)
354 job->ez_state = VC5_EZ_DISABLED;
355 break;
356
357 case VC5_EZ_DISABLED:
358 /* If the current Z/S state disables EZ because of a bad Z
359 * func or stencil operation, then we can't do any more EZ in
360 * this frame.
361 */
362 job->ez_state = VC5_EZ_DISABLED;
363 break;
364 }
365
366 /* If the FS affects the Z of the pixels, then it may update against
367 * the chosen EZ direction (though we could use
368 * ARB_conservative_depth's hints to avoid this)
369 */
370 if (v3d->prog.fs->prog_data.fs->writes_z) {
371 job->ez_state = VC5_EZ_DISABLED;
372 }
373
374 if (job->first_ez_state == VC5_EZ_UNDECIDED &&
375 (job->ez_state != VC5_EZ_DISABLED || job->draw_calls_queued == 0))
376 job->first_ez_state = job->ez_state;
377 }
378
379 static void
380 v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
381 {
382 struct v3d_context *v3d = v3d_context(pctx);
383
384 if (!info->count_from_stream_output && !info->indirect &&
385 !info->primitive_restart &&
386 !u_trim_pipe_prim(info->mode, (unsigned*)&info->count))
387 return;
388
389 /* Fall back for weird desktop GL primitive restart values. */
390 if (info->primitive_restart &&
391 info->index_size) {
392 uint32_t mask = ~0;
393
394 switch (info->index_size) {
395 case 2:
396 mask = 0xffff;
397 break;
398 case 1:
399 mask = 0xff;
400 break;
401 }
402
403 if (info->restart_index != mask) {
404 util_draw_vbo_without_prim_restart(pctx, info);
405 return;
406 }
407 }
408
409 if (info->mode >= PIPE_PRIM_QUADS) {
410 util_primconvert_save_rasterizer_state(v3d->primconvert, &v3d->rasterizer->base);
411 util_primconvert_draw_vbo(v3d->primconvert, info);
412 perf_debug("Fallback conversion for %d %s vertices\n",
413 info->count, u_prim_name(info->mode));
414 return;
415 }
416
417 /* Before setting up the draw, flush anything writing to the textures
418 * that we read from.
419 */
420 v3d_predraw_check_textures(pctx, &v3d->verttex);
421 v3d_predraw_check_textures(pctx, &v3d->fragtex);
422
423 struct v3d_job *job = v3d_get_job_for_fbo(v3d);
424
425 /* Get space to emit our draw call into the BCL, using a branch to
426 * jump to a new BO if necessary.
427 */
428 v3d_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);
429
430 if (v3d->prim_mode != info->mode) {
431 v3d->prim_mode = info->mode;
432 v3d->dirty |= VC5_DIRTY_PRIM_MODE;
433 }
434
435 v3d_start_draw(v3d);
436 v3d_update_compiled_shaders(v3d, info->mode);
437 v3d_update_job_ez(v3d, job);
438
439 #if V3D_VERSION >= 41
440 v3d41_emit_state(pctx);
441 #else
442 v3d33_emit_state(pctx);
443 #endif
444
445 if (v3d->dirty & (VC5_DIRTY_VTXBUF |
446 VC5_DIRTY_VTXSTATE |
447 VC5_DIRTY_PRIM_MODE |
448 VC5_DIRTY_RASTERIZER |
449 VC5_DIRTY_COMPILED_CS |
450 VC5_DIRTY_COMPILED_VS |
451 VC5_DIRTY_COMPILED_FS |
452 v3d->prog.cs->uniform_dirty_bits |
453 v3d->prog.vs->uniform_dirty_bits |
454 v3d->prog.fs->uniform_dirty_bits)) {
455 v3d_emit_gl_shader_state(v3d, info);
456 }
457
458 v3d->dirty = 0;
459
460 /* The Base Vertex/Base Instance packet sets those values to nonzero
461 * for the next draw call only.
462 */
463 if (info->index_bias || info->start_instance) {
464 cl_emit(&job->bcl, BASE_VERTEX_BASE_INSTANCE, base) {
465 base.base_instance = info->start_instance;
466 base.base_vertex = info->index_bias;
467 }
468 }
469
470 uint32_t prim_tf_enable = 0;
471 #if V3D_VERSION < 40
472 /* V3D 3.x: The HW only processes transform feedback on primitives
473 * with the flag set.
474 */
475 if (v3d->streamout.num_targets)
476 prim_tf_enable = (V3D_PRIM_POINTS_TF - V3D_PRIM_POINTS);
477 #endif
478
479 v3d_tf_statistics_record(v3d, info, v3d->streamout.num_targets);
480
481 /* Note that the primitive type fields match with OpenGL/gallium
482 * definitions, up to but not including QUADS.
483 */
484 if (info->index_size) {
485 uint32_t index_size = info->index_size;
486 uint32_t offset = info->start * index_size;
487 struct pipe_resource *prsc;
488 if (info->has_user_indices) {
489 prsc = NULL;
490 u_upload_data(v3d->uploader, 0,
491 info->count * info->index_size, 4,
492 info->index.user,
493 &offset, &prsc);
494 } else {
495 prsc = info->index.resource;
496 }
497 struct v3d_resource *rsc = v3d_resource(prsc);
498
499 #if V3D_VERSION >= 40
500 cl_emit(&job->bcl, INDEX_BUFFER_SETUP, ib) {
501 ib.address = cl_address(rsc->bo, 0);
502 ib.size = rsc->bo->size;
503 }
504 #endif
505
506 if (info->instance_count > 1) {
507 cl_emit(&job->bcl, INDEXED_INSTANCED_PRIMITIVE_LIST, prim) {
508 prim.index_type = ffs(info->index_size) - 1;
509 #if V3D_VERSION >= 40
510 prim.index_offset = offset;
511 #else /* V3D_VERSION < 40 */
512 prim.maximum_index = (1u << 31) - 1; /* XXX */
513 prim.address_of_indices_list =
514 cl_address(rsc->bo, offset);
515 #endif /* V3D_VERSION < 40 */
516 prim.mode = info->mode | prim_tf_enable;
517 prim.enable_primitive_restarts = info->primitive_restart;
518
519 prim.number_of_instances = info->instance_count;
520 prim.instance_length = info->count;
521 }
522 } else {
523 cl_emit(&job->bcl, INDEXED_PRIMITIVE_LIST, prim) {
524 prim.index_type = ffs(info->index_size) - 1;
525 prim.length = info->count;
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 }
537
538 job->draw_calls_queued++;
539
540 if (info->has_user_indices)
541 pipe_resource_reference(&prsc, NULL);
542 } else {
543 if (info->instance_count > 1) {
544 cl_emit(&job->bcl, VERTEX_ARRAY_INSTANCED_PRIMITIVES, prim) {
545 prim.mode = info->mode | prim_tf_enable;
546 prim.index_of_first_vertex = info->start;
547 prim.number_of_instances = info->instance_count;
548 prim.instance_length = info->count;
549 }
550 } else {
551 cl_emit(&job->bcl, VERTEX_ARRAY_PRIMITIVES, prim) {
552 prim.mode = info->mode | prim_tf_enable;
553 prim.length = info->count;
554 prim.index_of_first_vertex = info->start;
555 }
556 }
557 }
558 job->draw_calls_queued++;
559
560 if (v3d->zsa && job->zsbuf &&
561 (v3d->zsa->base.depth.enabled ||
562 v3d->zsa->base.stencil[0].enabled)) {
563 struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);
564 v3d_job_add_bo(job, rsc->bo);
565
566 if (v3d->zsa->base.depth.enabled) {
567 job->resolve |= PIPE_CLEAR_DEPTH;
568 rsc->initialized_buffers = PIPE_CLEAR_DEPTH;
569 }
570
571 if (v3d->zsa->base.stencil[0].enabled) {
572 job->resolve |= PIPE_CLEAR_STENCIL;
573 rsc->initialized_buffers |= PIPE_CLEAR_STENCIL;
574 }
575 }
576
577 for (int i = 0; i < VC5_MAX_DRAW_BUFFERS; i++) {
578 uint32_t bit = PIPE_CLEAR_COLOR0 << i;
579
580 if (job->resolve & bit || !job->cbufs[i])
581 continue;
582 struct v3d_resource *rsc = v3d_resource(job->cbufs[i]->texture);
583
584 job->resolve |= bit;
585 v3d_job_add_bo(job, rsc->bo);
586 }
587
588 if (job->referenced_size > 768 * 1024 * 1024) {
589 perf_debug("Flushing job with %dkb to try to free up memory\n",
590 job->referenced_size / 1024);
591 v3d_flush(pctx);
592 }
593
594 if (V3D_DEBUG & V3D_DEBUG_ALWAYS_FLUSH)
595 v3d_flush(pctx);
596 }
597
598 static void
599 v3d_clear(struct pipe_context *pctx, unsigned buffers,
600 const union pipe_color_union *color, double depth, unsigned stencil)
601 {
602 struct v3d_context *v3d = v3d_context(pctx);
603 struct v3d_job *job = v3d_get_job_for_fbo(v3d);
604
605 /* We can't flag new buffers for clearing once we've queued draws. We
606 * could avoid this by using the 3d engine to clear.
607 */
608 if (job->draw_calls_queued) {
609 perf_debug("Flushing rendering to process new clear.\n");
610 v3d_job_submit(v3d, job);
611 job = v3d_get_job_for_fbo(v3d);
612 }
613
614 for (int i = 0; i < VC5_MAX_DRAW_BUFFERS; i++) {
615 uint32_t bit = PIPE_CLEAR_COLOR0 << i;
616 if (!(buffers & bit))
617 continue;
618
619 struct pipe_surface *psurf = v3d->framebuffer.cbufs[i];
620 struct v3d_surface *surf = v3d_surface(psurf);
621 struct v3d_resource *rsc = v3d_resource(psurf->texture);
622
623 union util_color uc;
624 uint32_t internal_size = 4 << surf->internal_bpp;
625
626 static union pipe_color_union swapped_color;
627 if (v3d->swap_color_rb & (1 << i)) {
628 swapped_color.f[0] = color->f[2];
629 swapped_color.f[1] = color->f[1];
630 swapped_color.f[2] = color->f[0];
631 swapped_color.f[3] = color->f[3];
632 color = &swapped_color;
633 }
634
635 switch (surf->internal_type) {
636 case V3D_INTERNAL_TYPE_8:
637 util_pack_color(color->f, PIPE_FORMAT_R8G8B8A8_UNORM,
638 &uc);
639 memcpy(job->clear_color[i], uc.ui, internal_size);
640 break;
641 case V3D_INTERNAL_TYPE_8I:
642 case V3D_INTERNAL_TYPE_8UI:
643 job->clear_color[i][0] = ((color->ui[0] & 0xff) |
644 (color->ui[1] & 0xff) << 8 |
645 (color->ui[2] & 0xff) << 16 |
646 (color->ui[3] & 0xff) << 24);
647 break;
648 case V3D_INTERNAL_TYPE_16F:
649 util_pack_color(color->f, PIPE_FORMAT_R16G16B16A16_FLOAT,
650 &uc);
651 memcpy(job->clear_color[i], uc.ui, internal_size);
652 break;
653 case V3D_INTERNAL_TYPE_16I:
654 case V3D_INTERNAL_TYPE_16UI:
655 job->clear_color[i][0] = ((color->ui[0] & 0xffff) |
656 color->ui[1] << 16);
657 job->clear_color[i][1] = ((color->ui[2] & 0xffff) |
658 color->ui[3] << 16);
659 break;
660 case V3D_INTERNAL_TYPE_32F:
661 case V3D_INTERNAL_TYPE_32I:
662 case V3D_INTERNAL_TYPE_32UI:
663 memcpy(job->clear_color[i], color->ui, internal_size);
664 break;
665 }
666
667 rsc->initialized_buffers |= bit;
668 }
669
670 unsigned zsclear = buffers & PIPE_CLEAR_DEPTHSTENCIL;
671 if (zsclear) {
672 struct v3d_resource *rsc =
673 v3d_resource(v3d->framebuffer.zsbuf->texture);
674
675 if (zsclear & PIPE_CLEAR_DEPTH)
676 job->clear_z = depth;
677 if (zsclear & PIPE_CLEAR_STENCIL)
678 job->clear_s = stencil;
679
680 rsc->initialized_buffers |= zsclear;
681 }
682
683 job->draw_min_x = 0;
684 job->draw_min_y = 0;
685 job->draw_max_x = v3d->framebuffer.width;
686 job->draw_max_y = v3d->framebuffer.height;
687 job->cleared |= buffers;
688 job->resolve |= buffers;
689
690 v3d_start_draw(v3d);
691 }
692
693 static void
694 v3d_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,
695 const union pipe_color_union *color,
696 unsigned x, unsigned y, unsigned w, unsigned h,
697 bool render_condition_enabled)
698 {
699 fprintf(stderr, "unimpl: clear RT\n");
700 }
701
702 static void
703 v3d_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,
704 unsigned buffers, double depth, unsigned stencil,
705 unsigned x, unsigned y, unsigned w, unsigned h,
706 bool render_condition_enabled)
707 {
708 fprintf(stderr, "unimpl: clear DS\n");
709 }
710
711 void
712 v3dX(draw_init)(struct pipe_context *pctx)
713 {
714 pctx->draw_vbo = v3d_draw_vbo;
715 pctx->clear = v3d_clear;
716 pctx->clear_render_target = v3d_clear_render_target;
717 pctx->clear_depth_stencil = v3d_clear_depth_stencil;
718 }