edc528591542e50b3b974b0d341c83636f6ed9c3
[mesa.git] / src / gallium / drivers / vc5 / vc5_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 "vc5_context.h"
33 #include "vc5_resource.h"
34 #include "vc5_cl.h"
35 #include "broadcom/cle/v3d_packet_v33_pack.h"
36 #include "broadcom/compiler/v3d_compiler.h"
37
38 /**
39 * Does the initial bining command list setup for drawing to a given FBO.
40 */
41 static void
42 vc5_start_draw(struct vc5_context *vc5)
43 {
44 struct vc5_job *job = vc5->job;
45
46 if (job->needs_flush)
47 return;
48
49 /* Get space to emit our BCL state, using a branch to jump to a new BO
50 * if necessary.
51 */
52 vc5_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);
53
54 job->submit.bcl_start = job->bcl.bo->offset;
55 vc5_job_add_bo(job, job->bcl.bo);
56
57 job->tile_alloc = vc5_bo_alloc(vc5->screen, 1024 * 1024, "tile alloc");
58 struct vc5_bo *tsda = vc5_bo_alloc(vc5->screen,
59 job->draw_tiles_y *
60 job->draw_tiles_x *
61 64,
62 "TSDA");
63
64 /* "Binning mode lists start with a Tile Binning Mode Configuration
65 * item (120)"
66 *
67 * Part1 signals the end of binning config setup.
68 */
69 cl_emit(&job->bcl, TILE_BINNING_MODE_CONFIGURATION_PART2, config) {
70 config.tile_allocation_memory_address =
71 cl_address(job->tile_alloc, 0);
72 config.tile_allocation_memory_size = job->tile_alloc->size;
73 }
74
75 cl_emit(&job->bcl, TILE_BINNING_MODE_CONFIGURATION_PART1, config) {
76 config.tile_state_data_array_base_address =
77 cl_address(tsda, 0);
78
79 config.width_in_tiles = job->draw_tiles_x;
80 config.height_in_tiles = job->draw_tiles_y;
81
82 /* Must be >= 1 */
83 config.number_of_render_targets =
84 MAX2(vc5->framebuffer.nr_cbufs, 1);
85
86 config.multisample_mode_4x = job->msaa;
87
88 config.maximum_bpp_of_all_render_targets = job->internal_bpp;
89 }
90
91 vc5_bo_unreference(&tsda);
92
93 /* There's definitely nothing in the VCD cache we want. */
94 cl_emit(&job->bcl, FLUSH_VCD_CACHE, bin);
95
96 /* "Binning mode lists must have a Start Tile Binning item (6) after
97 * any prefix state data before the binning list proper starts."
98 */
99 cl_emit(&job->bcl, START_TILE_BINNING, bin);
100
101 cl_emit(&job->bcl, PRIMITIVE_LIST_FORMAT, fmt) {
102 fmt.data_type = LIST_INDEXED;
103 fmt.primitive_type = LIST_TRIANGLES;
104 }
105
106 job->needs_flush = true;
107 job->draw_width = vc5->framebuffer.width;
108 job->draw_height = vc5->framebuffer.height;
109 }
110
111 static void
112 vc5_predraw_check_textures(struct pipe_context *pctx,
113 struct vc5_texture_stateobj *stage_tex)
114 {
115 struct vc5_context *vc5 = vc5_context(pctx);
116
117 for (int i = 0; i < stage_tex->num_textures; i++) {
118 struct pipe_sampler_view *view = stage_tex->textures[i];
119 if (!view)
120 continue;
121
122 vc5_flush_jobs_writing_resource(vc5, view->texture);
123 }
124 }
125
126 static void
127 vc5_emit_gl_shader_state(struct vc5_context *vc5,
128 const struct pipe_draw_info *info)
129 {
130 struct vc5_job *job = vc5->job;
131 /* VC5_DIRTY_VTXSTATE */
132 struct vc5_vertex_stateobj *vtx = vc5->vtx;
133 /* VC5_DIRTY_VTXBUF */
134 struct vc5_vertexbuf_stateobj *vertexbuf = &vc5->vertexbuf;
135
136 /* Upload the uniforms to the indirect CL first */
137 struct vc5_cl_reloc fs_uniforms =
138 vc5_write_uniforms(vc5, vc5->prog.fs,
139 &vc5->constbuf[PIPE_SHADER_FRAGMENT],
140 &vc5->fragtex);
141 struct vc5_cl_reloc vs_uniforms =
142 vc5_write_uniforms(vc5, vc5->prog.vs,
143 &vc5->constbuf[PIPE_SHADER_VERTEX],
144 &vc5->verttex);
145 struct vc5_cl_reloc cs_uniforms =
146 vc5_write_uniforms(vc5, vc5->prog.cs,
147 &vc5->constbuf[PIPE_SHADER_VERTEX],
148 &vc5->verttex);
149
150 /* See GFXH-930 workaround below */
151 uint32_t num_elements_to_emit = MAX2(vtx->num_elements, 1);
152 uint32_t shader_rec_offset =
153 vc5_cl_ensure_space(&job->indirect,
154 cl_packet_length(GL_SHADER_STATE_RECORD) +
155 num_elements_to_emit *
156 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD),
157 32);
158
159 cl_emit(&job->indirect, GL_SHADER_STATE_RECORD, shader) {
160 shader.enable_clipping = true;
161 /* VC5_DIRTY_PRIM_MODE | VC5_DIRTY_RASTERIZER */
162 shader.point_size_in_shaded_vertex_data =
163 (info->mode == PIPE_PRIM_POINTS &&
164 vc5->rasterizer->base.point_size_per_vertex);
165
166 /* Must be set if the shader modifies Z, discards, or modifies
167 * the sample mask. For any of these cases, the fragment
168 * shader needs to write the Z value (even just discards).
169 */
170 shader.fragment_shader_does_z_writes =
171 (vc5->prog.fs->prog_data.fs->writes_z ||
172 vc5->prog.fs->prog_data.fs->discard);
173
174 shader.number_of_varyings_in_fragment_shader =
175 vc5->prog.fs->prog_data.base->num_inputs;
176
177 shader.propagate_nans = true;
178
179 shader.coordinate_shader_code_address =
180 cl_address(vc5->prog.cs->bo, 0);
181 shader.vertex_shader_code_address =
182 cl_address(vc5->prog.vs->bo, 0);
183 shader.fragment_shader_code_address =
184 cl_address(vc5->prog.fs->bo, 0);
185
186 /* XXX: Use combined input/output size flag in the common
187 * case.
188 */
189 shader.coordinate_shader_has_separate_input_and_output_vpm_blocks = true;
190 shader.vertex_shader_has_separate_input_and_output_vpm_blocks = true;
191 shader.coordinate_shader_input_vpm_segment_size =
192 MAX2(vc5->prog.cs->prog_data.vs->vpm_input_size, 1);
193 shader.vertex_shader_input_vpm_segment_size =
194 MAX2(vc5->prog.vs->prog_data.vs->vpm_input_size, 1);
195
196 shader.coordinate_shader_output_vpm_segment_size =
197 vc5->prog.cs->prog_data.vs->vpm_output_size;
198 shader.vertex_shader_output_vpm_segment_size =
199 vc5->prog.vs->prog_data.vs->vpm_output_size;
200
201 shader.coordinate_shader_uniforms_address = cs_uniforms;
202 shader.vertex_shader_uniforms_address = vs_uniforms;
203 shader.fragment_shader_uniforms_address = fs_uniforms;
204
205 shader.vertex_id_read_by_coordinate_shader =
206 vc5->prog.cs->prog_data.vs->uses_vid;
207 shader.instance_id_read_by_coordinate_shader =
208 vc5->prog.cs->prog_data.vs->uses_iid;
209 shader.vertex_id_read_by_vertex_shader =
210 vc5->prog.vs->prog_data.vs->uses_vid;
211 shader.instance_id_read_by_vertex_shader =
212 vc5->prog.vs->prog_data.vs->uses_iid;
213
214 shader.address_of_default_attribute_values =
215 cl_address(vtx->default_attribute_values, 0);
216 }
217
218 for (int i = 0; i < vtx->num_elements; i++) {
219 struct pipe_vertex_element *elem = &vtx->pipe[i];
220 struct pipe_vertex_buffer *vb =
221 &vertexbuf->vb[elem->vertex_buffer_index];
222 struct vc5_resource *rsc = vc5_resource(vb->buffer.resource);
223
224 const uint32_t size =
225 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD);
226 cl_emit_with_prepacked(&job->indirect,
227 GL_SHADER_STATE_ATTRIBUTE_RECORD,
228 &vtx->attrs[i * size], attr) {
229 attr.stride = vb->stride;
230 attr.address = cl_address(rsc->bo,
231 vb->buffer_offset +
232 elem->src_offset);
233 attr.number_of_values_read_by_coordinate_shader =
234 vc5->prog.cs->prog_data.vs->vattr_sizes[i];
235 attr.number_of_values_read_by_vertex_shader =
236 vc5->prog.vs->prog_data.vs->vattr_sizes[i];
237 }
238 }
239
240 if (vtx->num_elements == 0) {
241 /* GFXH-930: At least one attribute must be enabled and read
242 * by CS and VS. If we have no attributes being consumed by
243 * the shader, set up a dummy to be loaded into the VPM.
244 */
245 cl_emit(&job->indirect, GL_SHADER_STATE_ATTRIBUTE_RECORD, attr) {
246 /* Valid address of data whose value will be unused. */
247 attr.address = cl_address(job->indirect.bo, 0);
248
249 attr.type = ATTRIBUTE_FLOAT;
250 attr.stride = 0;
251 attr.vec_size = 1;
252
253 attr.number_of_values_read_by_coordinate_shader = 1;
254 attr.number_of_values_read_by_vertex_shader = 1;
255 }
256 }
257
258 cl_emit(&job->bcl, GL_SHADER_STATE, state) {
259 state.address = cl_address(job->indirect.bo, shader_rec_offset);
260 state.number_of_attribute_arrays = num_elements_to_emit;
261 }
262
263 vc5_bo_unreference(&cs_uniforms.bo);
264 vc5_bo_unreference(&vs_uniforms.bo);
265 vc5_bo_unreference(&fs_uniforms.bo);
266
267 job->shader_rec_count++;
268 }
269
270 static void
271 vc5_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
272 {
273 struct vc5_context *vc5 = vc5_context(pctx);
274
275 if (!info->count_from_stream_output && !info->indirect &&
276 !info->primitive_restart &&
277 !u_trim_pipe_prim(info->mode, (unsigned*)&info->count))
278 return;
279
280 /* Fall back for weird desktop GL primitive restart values. */
281 if (info->primitive_restart &&
282 info->index_size) {
283 uint32_t mask = ~0;
284
285 switch (info->index_size) {
286 case 2:
287 mask = 0xffff;
288 break;
289 case 1:
290 mask = 0xff;
291 break;
292 }
293
294 if (info->restart_index != mask) {
295 util_draw_vbo_without_prim_restart(pctx, info);
296 return;
297 }
298 }
299
300 if (info->mode >= PIPE_PRIM_QUADS) {
301 util_primconvert_save_rasterizer_state(vc5->primconvert, &vc5->rasterizer->base);
302 util_primconvert_draw_vbo(vc5->primconvert, info);
303 perf_debug("Fallback conversion for %d %s vertices\n",
304 info->count, u_prim_name(info->mode));
305 return;
306 }
307
308 /* Before setting up the draw, flush anything writing to the textures
309 * that we read from.
310 */
311 vc5_predraw_check_textures(pctx, &vc5->verttex);
312 vc5_predraw_check_textures(pctx, &vc5->fragtex);
313
314 struct vc5_job *job = vc5_get_job_for_fbo(vc5);
315
316 /* Get space to emit our draw call into the BCL, using a branch to
317 * jump to a new BO if necessary.
318 */
319 vc5_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);
320
321 if (vc5->prim_mode != info->mode) {
322 vc5->prim_mode = info->mode;
323 vc5->dirty |= VC5_DIRTY_PRIM_MODE;
324 }
325
326 vc5_start_draw(vc5);
327 vc5_update_compiled_shaders(vc5, info->mode);
328
329 vc5_emit_state(pctx);
330
331 if (vc5->dirty & (VC5_DIRTY_VTXBUF |
332 VC5_DIRTY_VTXSTATE |
333 VC5_DIRTY_PRIM_MODE |
334 VC5_DIRTY_RASTERIZER |
335 VC5_DIRTY_COMPILED_CS |
336 VC5_DIRTY_COMPILED_VS |
337 VC5_DIRTY_COMPILED_FS |
338 vc5->prog.cs->uniform_dirty_bits |
339 vc5->prog.vs->uniform_dirty_bits |
340 vc5->prog.fs->uniform_dirty_bits)) {
341 vc5_emit_gl_shader_state(vc5, info);
342 }
343
344 vc5->dirty = 0;
345
346 /* The Base Vertex/Base Instance packet sets those values to nonzero
347 * for the next draw call only.
348 */
349 if (info->index_bias || info->start_instance) {
350 cl_emit(&job->bcl, BASE_VERTEX_BASE_INSTANCE, base) {
351 base.base_instance = info->start_instance;
352 base.base_vertex = info->index_bias;
353 }
354 }
355
356 /* The HW only processes transform feedback on primitives with the
357 * flag set.
358 */
359 uint32_t prim_tf_enable = 0;
360 if (vc5->prog.bind_vs->num_tf_outputs)
361 prim_tf_enable = (V3D_PRIM_POINTS_TF - V3D_PRIM_POINTS);
362
363 /* Note that the primitive type fields match with OpenGL/gallium
364 * definitions, up to but not including QUADS.
365 */
366 if (info->index_size) {
367 uint32_t index_size = info->index_size;
368 uint32_t offset = info->start * index_size;
369 struct pipe_resource *prsc;
370 if (info->has_user_indices) {
371 prsc = NULL;
372 u_upload_data(vc5->uploader, 0,
373 info->count * info->index_size, 4,
374 info->index.user,
375 &offset, &prsc);
376 } else {
377 prsc = info->index.resource;
378 }
379 struct vc5_resource *rsc = vc5_resource(prsc);
380
381 if (info->instance_count > 1) {
382 cl_emit(&job->bcl, INDEXED_INSTANCED_PRIMITIVE_LIST, prim) {
383 prim.index_type = ffs(info->index_size) - 1;
384 prim.maximum_index = (1u << 31) - 1; /* XXX */
385 prim.address_of_indices_list =
386 cl_address(rsc->bo, offset);
387 prim.mode = info->mode | prim_tf_enable;
388 prim.enable_primitive_restarts = info->primitive_restart;
389
390 prim.number_of_instances = info->instance_count;
391 prim.instance_length = info->count;
392 }
393 } else {
394 cl_emit(&job->bcl, INDEXED_PRIMITIVE_LIST, prim) {
395 prim.index_type = ffs(info->index_size) - 1;
396 prim.length = info->count;
397 prim.maximum_index = (1u << 31) - 1; /* XXX */
398 prim.address_of_indices_list =
399 cl_address(rsc->bo, offset);
400 prim.mode = info->mode | prim_tf_enable;
401 prim.enable_primitive_restarts = info->primitive_restart;
402 }
403 }
404
405 job->draw_calls_queued++;
406
407 if (info->has_user_indices)
408 pipe_resource_reference(&prsc, NULL);
409 } else {
410 if (info->instance_count > 1) {
411 cl_emit(&job->bcl, VERTEX_ARRAY_INSTANCED_PRIMITIVES, prim) {
412 prim.mode = info->mode | prim_tf_enable;
413 prim.index_of_first_vertex = info->start;
414 prim.number_of_instances = info->instance_count;
415 prim.instance_length = info->count;
416 }
417 } else {
418 cl_emit(&job->bcl, VERTEX_ARRAY_PRIMITIVES, prim) {
419 prim.mode = info->mode | prim_tf_enable;
420 prim.length = info->count;
421 prim.index_of_first_vertex = info->start;
422 }
423 }
424 }
425 job->draw_calls_queued++;
426
427 if (vc5->zsa && job->zsbuf &&
428 (vc5->zsa->base.depth.enabled ||
429 vc5->zsa->base.stencil[0].enabled)) {
430 struct vc5_resource *rsc = vc5_resource(job->zsbuf->texture);
431 vc5_job_add_bo(job, rsc->bo);
432
433 if (vc5->zsa->base.depth.enabled) {
434 job->resolve |= PIPE_CLEAR_DEPTH;
435 rsc->initialized_buffers = PIPE_CLEAR_DEPTH;
436
437 if (vc5->zsa->early_z_enable)
438 job->uses_early_z = true;
439 }
440
441 if (vc5->zsa->base.stencil[0].enabled) {
442 job->resolve |= PIPE_CLEAR_STENCIL;
443 rsc->initialized_buffers |= PIPE_CLEAR_STENCIL;
444 }
445 }
446
447 for (int i = 0; i < VC5_MAX_DRAW_BUFFERS; i++) {
448 uint32_t bit = PIPE_CLEAR_COLOR0 << i;
449
450 if (job->resolve & bit || !job->cbufs[i])
451 continue;
452 struct vc5_resource *rsc = vc5_resource(job->cbufs[i]->texture);
453
454 job->resolve |= bit;
455 vc5_job_add_bo(job, rsc->bo);
456 }
457
458 if (V3D_DEBUG & V3D_DEBUG_ALWAYS_FLUSH)
459 vc5_flush(pctx);
460 }
461
462 static void
463 vc5_clear(struct pipe_context *pctx, unsigned buffers,
464 const union pipe_color_union *color, double depth, unsigned stencil)
465 {
466 struct vc5_context *vc5 = vc5_context(pctx);
467 struct vc5_job *job = vc5_get_job_for_fbo(vc5);
468
469 /* We can't flag new buffers for clearing once we've queued draws. We
470 * could avoid this by using the 3d engine to clear.
471 */
472 if (job->draw_calls_queued) {
473 perf_debug("Flushing rendering to process new clear.\n");
474 vc5_job_submit(vc5, job);
475 job = vc5_get_job_for_fbo(vc5);
476 }
477
478 for (int i = 0; i < VC5_MAX_DRAW_BUFFERS; i++) {
479 uint32_t bit = PIPE_CLEAR_COLOR0 << i;
480 if (!(buffers & bit))
481 continue;
482
483 struct pipe_surface *psurf = vc5->framebuffer.cbufs[i];
484 struct vc5_surface *surf = vc5_surface(psurf);
485 struct vc5_resource *rsc = vc5_resource(psurf->texture);
486
487 union util_color uc;
488 uint32_t internal_size = 4 << surf->internal_bpp;
489
490 switch (surf->internal_type) {
491 case INTERNAL_TYPE_8:
492 if (surf->format == PIPE_FORMAT_B4G4R4A4_UNORM ||
493 surf->format == PIPE_FORMAT_B4G4R4A4_UNORM) {
494 /* Our actual hardware layout is ABGR4444, but
495 * we apply a swizzle when texturing to flip
496 * things back around.
497 */
498 util_pack_color(color->f, PIPE_FORMAT_A8R8G8B8_UNORM,
499 &uc);
500 } else {
501 util_pack_color(color->f, PIPE_FORMAT_R8G8B8A8_UNORM,
502 &uc);
503 }
504 memcpy(job->clear_color[i], uc.ui, internal_size);
505 break;
506 case INTERNAL_TYPE_8I:
507 case INTERNAL_TYPE_8UI:
508 job->clear_color[i][0] = ((uc.ui[0] & 0xff) |
509 (uc.ui[1] & 0xff) << 8 |
510 (uc.ui[2] & 0xff) << 16 |
511 (uc.ui[3] & 0xff) << 24);
512 break;
513 case INTERNAL_TYPE_16F:
514 util_pack_color(color->f, PIPE_FORMAT_R16G16B16A16_FLOAT,
515 &uc);
516 memcpy(job->clear_color[i], uc.ui, internal_size);
517 break;
518 case INTERNAL_TYPE_16I:
519 case INTERNAL_TYPE_16UI:
520 job->clear_color[i][0] = ((uc.ui[0] & 0xffff) |
521 uc.ui[1] << 16);
522 job->clear_color[i][1] = ((uc.ui[2] & 0xffff) |
523 uc.ui[3] << 16);
524 break;
525 case INTERNAL_TYPE_32F:
526 case INTERNAL_TYPE_32I:
527 case INTERNAL_TYPE_32UI:
528 memcpy(job->clear_color[i], color->ui, internal_size);
529 break;
530 }
531
532 rsc->initialized_buffers |= bit;
533 }
534
535 unsigned zsclear = buffers & PIPE_CLEAR_DEPTHSTENCIL;
536 if (zsclear) {
537 struct vc5_resource *rsc =
538 vc5_resource(vc5->framebuffer.zsbuf->texture);
539
540 if (zsclear & PIPE_CLEAR_DEPTH)
541 job->clear_z = depth;
542 if (zsclear & PIPE_CLEAR_STENCIL)
543 job->clear_s = stencil;
544
545 rsc->initialized_buffers |= zsclear;
546 }
547
548 job->draw_min_x = 0;
549 job->draw_min_y = 0;
550 job->draw_max_x = vc5->framebuffer.width;
551 job->draw_max_y = vc5->framebuffer.height;
552 job->cleared |= buffers;
553 job->resolve |= buffers;
554
555 vc5_start_draw(vc5);
556 }
557
558 static void
559 vc5_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,
560 const union pipe_color_union *color,
561 unsigned x, unsigned y, unsigned w, unsigned h,
562 bool render_condition_enabled)
563 {
564 fprintf(stderr, "unimpl: clear RT\n");
565 }
566
567 static void
568 vc5_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,
569 unsigned buffers, double depth, unsigned stencil,
570 unsigned x, unsigned y, unsigned w, unsigned h,
571 bool render_condition_enabled)
572 {
573 fprintf(stderr, "unimpl: clear DS\n");
574 }
575
576 void
577 vc5_draw_init(struct pipe_context *pctx)
578 {
579 pctx->draw_vbo = vc5_draw_vbo;
580 pctx->clear = vc5_clear;
581 pctx->clear_render_target = vc5_clear_render_target;
582 pctx->clear_depth_stencil = vc5_clear_depth_stencil;
583 }