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