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