summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/vc5/vc5_draw.c
blob: 02333ede5c5ba11b17245d6edf184dd25dc8b362 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
 * Copyright © 2014-2017 Broadcom
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "util/u_blitter.h"
#include "util/u_prim.h"
#include "util/u_format.h"
#include "util/u_pack_color.h"
#include "util/u_prim_restart.h"
#include "util/u_upload_mgr.h"
#include "indices/u_primconvert.h"

#include "vc5_context.h"
#include "vc5_resource.h"
#include "vc5_cl.h"
#include "broadcom/cle/v3d_packet_v33_pack.h"
#include "broadcom/compiler/v3d_compiler.h"

/**
 * Does the initial bining command list setup for drawing to a given FBO.
 */
static void
vc5_start_draw(struct vc5_context *vc5)
{
        struct vc5_job *job = vc5->job;

        if (job->needs_flush)
                return;

        /* Get space to emit our BCL state, using a branch to jump to a new BO
         * if necessary.
         */
        vc5_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);

        job->submit.bcl_start = job->bcl.bo->offset;
        vc5_job_add_bo(job, job->bcl.bo);

        job->tile_alloc = vc5_bo_alloc(vc5->screen, 1024 * 1024, "tile alloc");
        struct vc5_bo *tsda = vc5_bo_alloc(vc5->screen,
                                           job->draw_tiles_y *
                                           job->draw_tiles_x *
                                           64,
                                           "TSDA");

        /* "Binning mode lists start with a Tile Binning Mode Configuration
         * item (120)"
         *
         * Part1 signals the end of binning config setup.
         */
        cl_emit(&job->bcl, TILE_BINNING_MODE_CONFIGURATION_PART2, config) {
                config.tile_allocation_memory_address =
                        cl_address(job->tile_alloc, 0);
                config.tile_allocation_memory_size = job->tile_alloc->size;
        }

        cl_emit(&job->bcl, TILE_BINNING_MODE_CONFIGURATION_PART1, config) {
                config.tile_state_data_array_base_address =
                        cl_address(tsda, 0);

                config.width_in_tiles = job->draw_tiles_x;
                config.height_in_tiles = job->draw_tiles_y;

                /* Must be >= 1 */
                config.number_of_render_targets =
                        MAX2(vc5->framebuffer.nr_cbufs, 1);

                config.multisample_mode_4x = job->msaa;

                config.maximum_bpp_of_all_render_targets = job->internal_bpp;
        }

        vc5_bo_unreference(&tsda);

        /* There's definitely nothing in the VCD cache we want. */
        cl_emit(&job->bcl, FLUSH_VCD_CACHE, bin);

        /* "Binning mode lists must have a Start Tile Binning item (6) after
         *  any prefix state data before the binning list proper starts."
         */
        cl_emit(&job->bcl, START_TILE_BINNING, bin);

        cl_emit(&job->bcl, PRIMITIVE_LIST_FORMAT, fmt) {
                fmt.data_type = LIST_INDEXED;
                fmt.primitive_type = LIST_TRIANGLES;
        }

        job->needs_flush = true;
        job->draw_width = vc5->framebuffer.width;
        job->draw_height = vc5->framebuffer.height;
}

static void
vc5_predraw_check_textures(struct pipe_context *pctx,
                           struct vc5_texture_stateobj *stage_tex)
{
        struct vc5_context *vc5 = vc5_context(pctx);

        for (int i = 0; i < stage_tex->num_textures; i++) {
                struct pipe_sampler_view *view = stage_tex->textures[i];
                if (!view)
                        continue;

                vc5_flush_jobs_writing_resource(vc5, view->texture);
        }
}

static void
vc5_emit_gl_shader_state(struct vc5_context *vc5,
                         const struct pipe_draw_info *info)
{
        struct vc5_job *job = vc5->job;
        /* VC5_DIRTY_VTXSTATE */
        struct vc5_vertex_stateobj *vtx = vc5->vtx;
        /* VC5_DIRTY_VTXBUF */
        struct vc5_vertexbuf_stateobj *vertexbuf = &vc5->vertexbuf;

        /* Upload the uniforms to the indirect CL first */
        struct vc5_cl_reloc fs_uniforms =
                vc5_write_uniforms(vc5, vc5->prog.fs,
                                   &vc5->constbuf[PIPE_SHADER_FRAGMENT],
                                   &vc5->fragtex);
        struct vc5_cl_reloc vs_uniforms =
                vc5_write_uniforms(vc5, vc5->prog.vs,
                                   &vc5->constbuf[PIPE_SHADER_VERTEX],
                                   &vc5->verttex);
        struct vc5_cl_reloc cs_uniforms =
                vc5_write_uniforms(vc5, vc5->prog.cs,
                                   &vc5->constbuf[PIPE_SHADER_VERTEX],
                                   &vc5->verttex);

        /* See GFXH-930 workaround below */
        uint32_t num_elements_to_emit = MAX2(vtx->num_elements, 1);
        uint32_t shader_rec_offset =
                vc5_cl_ensure_space(&job->indirect,
                                    cl_packet_length(GL_SHADER_STATE_RECORD) +
                                    num_elements_to_emit *
                                    cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD),
                                    32);

        cl_emit(&job->indirect, GL_SHADER_STATE_RECORD, shader) {
                shader.enable_clipping = true;
                /* VC5_DIRTY_PRIM_MODE | VC5_DIRTY_RASTERIZER */
                shader.point_size_in_shaded_vertex_data =
                        (info->mode == PIPE_PRIM_POINTS &&
                         vc5->rasterizer->base.point_size_per_vertex);

                /* Must be set if the shader modifies Z, discards, or modifies
                 * the sample mask.  For any of these cases, the fragment
                 * shader needs to write the Z value (even just discards).
                 */
                shader.fragment_shader_does_z_writes =
                        (vc5->prog.fs->prog_data.fs->writes_z ||
                         vc5->prog.fs->prog_data.fs->discard);

                shader.number_of_varyings_in_fragment_shader =
                        vc5->prog.fs->prog_data.base->num_inputs;

                shader.propagate_nans = true;

                shader.coordinate_shader_code_address =
                        cl_address(vc5->prog.cs->bo, 0);
                shader.vertex_shader_code_address =
                        cl_address(vc5->prog.vs->bo, 0);
                shader.fragment_shader_code_address =
                        cl_address(vc5->prog.fs->bo, 0);

                /* XXX: Use combined input/output size flag in the common
                 * case.
                 */
                shader.coordinate_shader_has_separate_input_and_output_vpm_blocks = true;
                shader.vertex_shader_has_separate_input_and_output_vpm_blocks = true;
                shader.coordinate_shader_input_vpm_segment_size =
                        MAX2(vc5->prog.cs->prog_data.vs->vpm_input_size, 1);
                shader.vertex_shader_input_vpm_segment_size =
                        MAX2(vc5->prog.vs->prog_data.vs->vpm_input_size, 1);

                shader.coordinate_shader_output_vpm_segment_size =
                        vc5->prog.cs->prog_data.vs->vpm_output_size;
                shader.vertex_shader_output_vpm_segment_size =
                        vc5->prog.vs->prog_data.vs->vpm_output_size;

                shader.coordinate_shader_uniforms_address = cs_uniforms;
                shader.vertex_shader_uniforms_address = vs_uniforms;
                shader.fragment_shader_uniforms_address = fs_uniforms;

                shader.vertex_id_read_by_coordinate_shader =
                        vc5->prog.cs->prog_data.vs->uses_vid;
                shader.instance_id_read_by_coordinate_shader =
                        vc5->prog.cs->prog_data.vs->uses_iid;
                shader.vertex_id_read_by_vertex_shader =
                        vc5->prog.vs->prog_data.vs->uses_vid;
                shader.instance_id_read_by_vertex_shader =
                        vc5->prog.vs->prog_data.vs->uses_iid;

                shader.address_of_default_attribute_values =
                        cl_address(vtx->default_attribute_values, 0);
        }

        for (int i = 0; i < vtx->num_elements; i++) {
                struct pipe_vertex_element *elem = &vtx->pipe[i];
                struct pipe_vertex_buffer *vb =
                        &vertexbuf->vb[elem->vertex_buffer_index];
                struct vc5_resource *rsc = vc5_resource(vb->buffer.resource);

                struct V3D33_GL_SHADER_STATE_ATTRIBUTE_RECORD attr_unpacked = {
                        .stride = vb->stride,
                        .address = cl_address(rsc->bo,
                                              vb->buffer_offset +
                                              elem->src_offset),
                        .number_of_values_read_by_coordinate_shader =
                                vc5->prog.cs->prog_data.vs->vattr_sizes[i],
                        .number_of_values_read_by_vertex_shader =
                                vc5->prog.vs->prog_data.vs->vattr_sizes[i],
                };
                const uint32_t size =
                        cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD);
                uint8_t attr_packed[size];
                V3D33_GL_SHADER_STATE_ATTRIBUTE_RECORD_pack(&job->indirect,
                                                            attr_packed,
                                                            &attr_unpacked);
                for (int j = 0; j < size; j++)
                        attr_packed[j] |= vtx->attrs[i * size + j];
                cl_emit_prepacked(&job->indirect, &attr_packed);
        }

        if (vtx->num_elements == 0) {
                /* GFXH-930: At least one attribute must be enabled and read
                 * by CS and VS.  If we have no attributes being consumed by
                 * the shader, set up a dummy to be loaded into the VPM.
                 */
                cl_emit(&job->indirect, GL_SHADER_STATE_ATTRIBUTE_RECORD, attr) {
                        /* Valid address of data whose value will be unused. */
                        attr.address = cl_address(job->indirect.bo, 0);

                        attr.type = ATTRIBUTE_FLOAT;
                        attr.stride = 0;
                        attr.vec_size = 1;

                        attr.number_of_values_read_by_coordinate_shader = 1;
                        attr.number_of_values_read_by_vertex_shader = 1;
                }
        }

        cl_emit(&job->bcl, GL_SHADER_STATE, state) {
                state.address = cl_address(job->indirect.bo, shader_rec_offset);
                state.number_of_attribute_arrays = num_elements_to_emit;
        }

        vc5_bo_unreference(&cs_uniforms.bo);
        vc5_bo_unreference(&vs_uniforms.bo);
        vc5_bo_unreference(&fs_uniforms.bo);

        job->shader_rec_count++;
}

static void
vc5_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
{
        struct vc5_context *vc5 = vc5_context(pctx);

        if (!info->count_from_stream_output && !info->indirect &&
            !info->primitive_restart &&
            !u_trim_pipe_prim(info->mode, (unsigned*)&info->count))
                return;

        /* Fall back for weird desktop GL primitive restart values. */
        if (info->primitive_restart &&
            info->index_size) {
                uint32_t mask = ~0;

                switch (info->index_size) {
                case 2:
                        mask = 0xffff;
                        break;
                case 1:
                        mask = 0xff;
                        break;
                }

                if (info->restart_index != mask) {
                        util_draw_vbo_without_prim_restart(pctx, info);
                        return;
                }
        }

        if (info->mode >= PIPE_PRIM_QUADS) {
                util_primconvert_save_rasterizer_state(vc5->primconvert, &vc5->rasterizer->base);
                util_primconvert_draw_vbo(vc5->primconvert, info);
                perf_debug("Fallback conversion for %d %s vertices\n",
                           info->count, u_prim_name(info->mode));
                return;
        }

        /* Before setting up the draw, flush anything writing to the textures
         * that we read from.
         */
        vc5_predraw_check_textures(pctx, &vc5->verttex);
        vc5_predraw_check_textures(pctx, &vc5->fragtex);

        struct vc5_job *job = vc5_get_job_for_fbo(vc5);

        /* Get space to emit our draw call into the BCL, using a branch to
         * jump to a new BO if necessary.
         */
        vc5_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);

        if (vc5->prim_mode != info->mode) {
                vc5->prim_mode = info->mode;
                vc5->dirty |= VC5_DIRTY_PRIM_MODE;
        }

        vc5_start_draw(vc5);
        vc5_update_compiled_shaders(vc5, info->mode);

        vc5_emit_state(pctx);

        if (vc5->dirty & (VC5_DIRTY_VTXBUF |
                          VC5_DIRTY_VTXSTATE |
                          VC5_DIRTY_PRIM_MODE |
                          VC5_DIRTY_RASTERIZER |
                          VC5_DIRTY_COMPILED_CS |
                          VC5_DIRTY_COMPILED_VS |
                          VC5_DIRTY_COMPILED_FS |
                          vc5->prog.cs->uniform_dirty_bits |
                          vc5->prog.vs->uniform_dirty_bits |
                          vc5->prog.fs->uniform_dirty_bits)) {
                vc5_emit_gl_shader_state(vc5, info);
        }

        vc5->dirty = 0;

        /* The Base Vertex/Base Instance packet sets those values to nonzero
         * for the next draw call only.
         */
        if (info->index_bias || info->start_instance) {
                cl_emit(&job->bcl, BASE_VERTEX_BASE_INSTANCE, base) {
                        base.base_instance = info->start_instance;
                        base.base_vertex = info->index_bias;
                }
        }

        /* The HW only processes transform feedback on primitives with the
         * flag set.
         */
        uint32_t prim_tf_enable = 0;
        if (vc5->prog.bind_vs->num_tf_outputs)
                prim_tf_enable = (V3D_PRIM_POINTS_TF - V3D_PRIM_POINTS);

        /* Note that the primitive type fields match with OpenGL/gallium
         * definitions, up to but not including QUADS.
         */
        if (info->index_size) {
                uint32_t index_size = info->index_size;
                uint32_t offset = info->start * index_size;
                struct pipe_resource *prsc;
                if (info->has_user_indices) {
                        prsc = NULL;
                        u_upload_data(vc5->uploader, 0,
                                      info->count * info->index_size, 4,
                                      info->index.user,
                                      &offset, &prsc);
                } else {
                        prsc = info->index.resource;
                }
                struct vc5_resource *rsc = vc5_resource(prsc);

                if (info->instance_count > 1) {
                        cl_emit(&job->bcl, INDEXED_INSTANCED_PRIMITIVE_LIST, prim) {
                                prim.index_type = ffs(info->index_size) - 1;
                                prim.maximum_index = (1u << 31) - 1; /* XXX */
                                prim.address_of_indices_list =
                                        cl_address(rsc->bo, offset);
                                prim.mode = info->mode | prim_tf_enable;
                                prim.enable_primitive_restarts = info->primitive_restart;

                                prim.number_of_instances = info->instance_count;
                                prim.instance_length = info->count;
                        }
                } else {
                        cl_emit(&job->bcl, INDEXED_PRIMITIVE_LIST, prim) {
                                prim.index_type = ffs(info->index_size) - 1;
                                prim.length = info->count;
                                prim.maximum_index = (1u << 31) - 1; /* XXX */
                                prim.address_of_indices_list =
                                        cl_address(rsc->bo, offset);
                                prim.mode = info->mode | prim_tf_enable;
                                prim.enable_primitive_restarts = info->primitive_restart;
                        }
                }

                job->draw_calls_queued++;

                if (info->has_user_indices)
                        pipe_resource_reference(&prsc, NULL);
        } else {
                if (info->instance_count > 1) {
                        cl_emit(&job->bcl, VERTEX_ARRAY_INSTANCED_PRIMITIVES, prim) {
                                prim.mode = info->mode | prim_tf_enable;
                                prim.index_of_first_vertex = info->start;
                                prim.number_of_instances = info->instance_count;
                                prim.instance_length = info->count;
                        }
                } else {
                        cl_emit(&job->bcl, VERTEX_ARRAY_PRIMITIVES, prim) {
                                prim.mode = info->mode | prim_tf_enable;
                                prim.length = info->count;
                                prim.index_of_first_vertex = info->start;
                        }
                }
        }
        job->draw_calls_queued++;

        if (vc5->zsa && job->zsbuf &&
            (vc5->zsa->base.depth.enabled ||
             vc5->zsa->base.stencil[0].enabled)) {
                struct vc5_resource *rsc = vc5_resource(job->zsbuf->texture);
                vc5_job_add_bo(job, rsc->bo);

                if (vc5->zsa->base.depth.enabled) {
                        job->resolve |= PIPE_CLEAR_DEPTH;
                        rsc->initialized_buffers = PIPE_CLEAR_DEPTH;

                        if (vc5->zsa->early_z_enable)
                                job->uses_early_z = true;
                }

                if (vc5->zsa->base.stencil[0].enabled) {
                        job->resolve |= PIPE_CLEAR_STENCIL;
                        rsc->initialized_buffers |= PIPE_CLEAR_STENCIL;
                }
        }

        for (int i = 0; i < VC5_MAX_DRAW_BUFFERS; i++) {
                uint32_t bit = PIPE_CLEAR_COLOR0 << i;

                if (job->resolve & bit || !job->cbufs[i])
                        continue;
                struct vc5_resource *rsc = vc5_resource(job->cbufs[i]->texture);

                job->resolve |= bit;
                vc5_job_add_bo(job, rsc->bo);
        }

        if (V3D_DEBUG & V3D_DEBUG_ALWAYS_FLUSH)
                vc5_flush(pctx);
}

static void
vc5_clear(struct pipe_context *pctx, unsigned buffers,
          const union pipe_color_union *color, double depth, unsigned stencil)
{
        struct vc5_context *vc5 = vc5_context(pctx);
        struct vc5_job *job = vc5_get_job_for_fbo(vc5);

        /* We can't flag new buffers for clearing once we've queued draws.  We
         * could avoid this by using the 3d engine to clear.
         */
        if (job->draw_calls_queued) {
                perf_debug("Flushing rendering to process new clear.\n");
                vc5_job_submit(vc5, job);
                job = vc5_get_job_for_fbo(vc5);
        }

        for (int i = 0; i < VC5_MAX_DRAW_BUFFERS; i++) {
                uint32_t bit = PIPE_CLEAR_COLOR0 << i;
                if (!(buffers & bit))
                        continue;

                struct pipe_surface *cbuf = vc5->framebuffer.cbufs[i];
                struct vc5_resource *rsc =
                        vc5_resource(cbuf->texture);

                union util_color uc;
                util_pack_color(color->f, cbuf->format, &uc);

                memcpy(job->clear_color[i], uc.ui,
                       util_format_get_blocksize(cbuf->format));

                rsc->initialized_buffers |= bit;
        }

        unsigned zsclear = buffers & PIPE_CLEAR_DEPTHSTENCIL;
        if (zsclear) {
                struct vc5_resource *rsc =
                        vc5_resource(vc5->framebuffer.zsbuf->texture);

                if (zsclear & PIPE_CLEAR_DEPTH)
                        job->clear_z = depth;
                if (zsclear & PIPE_CLEAR_STENCIL)
                        job->clear_s = stencil;

                rsc->initialized_buffers |= zsclear;
        }

        job->draw_min_x = 0;
        job->draw_min_y = 0;
        job->draw_max_x = vc5->framebuffer.width;
        job->draw_max_y = vc5->framebuffer.height;
        job->cleared |= buffers;
        job->resolve |= buffers;

        vc5_start_draw(vc5);
}

static void
vc5_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,
                        const union pipe_color_union *color,
                        unsigned x, unsigned y, unsigned w, unsigned h,
                        bool render_condition_enabled)
{
        fprintf(stderr, "unimpl: clear RT\n");
}

static void
vc5_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,
                        unsigned buffers, double depth, unsigned stencil,
                        unsigned x, unsigned y, unsigned w, unsigned h,
                        bool render_condition_enabled)
{
        fprintf(stderr, "unimpl: clear DS\n");
}

void
vc5_draw_init(struct pipe_context *pctx)
{
        pctx->draw_vbo = vc5_draw_vbo;
        pctx->clear = vc5_clear;
        pctx->clear_render_target = vc5_clear_render_target;
        pctx->clear_depth_stencil = vc5_clear_depth_stencil;
}