Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/v3d/v3dx_draw.c
4570 views
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_draw.h"
26
#include "util/u_prim.h"
27
#include "util/format/u_format.h"
28
#include "util/u_pack_color.h"
29
#include "util/u_prim_restart.h"
30
#include "util/u_upload_mgr.h"
31
#include "indices/u_primconvert.h"
32
33
#include "v3d_context.h"
34
#include "v3d_resource.h"
35
#include "v3d_cl.h"
36
#include "broadcom/compiler/v3d_compiler.h"
37
#include "broadcom/common/v3d_macros.h"
38
#include "broadcom/common/v3d_util.h"
39
#include "broadcom/cle/v3dx_pack.h"
40
41
static void
42
v3d_start_binning(struct v3d_context *v3d, struct v3d_job *job)
43
{
44
assert(job->needs_flush);
45
46
/* Get space to emit our BCL state, using a branch to jump to a new BO
47
* if necessary.
48
*/
49
50
v3d_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);
51
52
job->submit.bcl_start = job->bcl.bo->offset;
53
v3d_job_add_bo(job, job->bcl.bo);
54
55
/* The PTB will request the tile alloc initial size per tile at start
56
* of tile binning.
57
*/
58
uint32_t tile_alloc_size =
59
MAX2(job->num_layers, 1) * job->draw_tiles_x * job->draw_tiles_y * 64;
60
61
/* The PTB allocates in aligned 4k chunks after the initial setup. */
62
tile_alloc_size = align(tile_alloc_size, 4096);
63
64
/* Include the first two chunk allocations that the PTB does so that
65
* we definitely clear the OOM condition before triggering one (the HW
66
* won't trigger OOM during the first allocations).
67
*/
68
tile_alloc_size += 8192;
69
70
/* For performance, allocate some extra initial memory after the PTB's
71
* minimal allocations, so that we hopefully don't have to block the
72
* GPU on the kernel handling an OOM signal.
73
*/
74
tile_alloc_size += 512 * 1024;
75
76
job->tile_alloc = v3d_bo_alloc(v3d->screen, tile_alloc_size,
77
"tile_alloc");
78
uint32_t tsda_per_tile_size = v3d->screen->devinfo.ver >= 40 ? 256 : 64;
79
job->tile_state = v3d_bo_alloc(v3d->screen,
80
MAX2(job->num_layers, 1) *
81
job->draw_tiles_y *
82
job->draw_tiles_x *
83
tsda_per_tile_size,
84
"TSDA");
85
86
#if V3D_VERSION >= 41
87
/* This must go before the binning mode configuration. It is
88
* required for layered framebuffers to work.
89
*/
90
if (job->num_layers > 0) {
91
cl_emit(&job->bcl, NUMBER_OF_LAYERS, config) {
92
config.number_of_layers = job->num_layers;
93
}
94
}
95
#endif
96
97
#if V3D_VERSION >= 40
98
cl_emit(&job->bcl, TILE_BINNING_MODE_CFG, config) {
99
config.width_in_pixels = job->draw_width;
100
config.height_in_pixels = job->draw_height;
101
config.number_of_render_targets =
102
MAX2(job->nr_cbufs, 1);
103
104
config.multisample_mode_4x = job->msaa;
105
106
config.maximum_bpp_of_all_render_targets = job->internal_bpp;
107
}
108
#else /* V3D_VERSION < 40 */
109
/* "Binning mode lists start with a Tile Binning Mode Configuration
110
* item (120)"
111
*
112
* Part1 signals the end of binning config setup.
113
*/
114
cl_emit(&job->bcl, TILE_BINNING_MODE_CFG_PART2, config) {
115
config.tile_allocation_memory_address =
116
cl_address(job->tile_alloc, 0);
117
config.tile_allocation_memory_size = job->tile_alloc->size;
118
}
119
120
cl_emit(&job->bcl, TILE_BINNING_MODE_CFG_PART1, config) {
121
config.tile_state_data_array_base_address =
122
cl_address(job->tile_state, 0);
123
124
config.width_in_tiles = job->draw_tiles_x;
125
config.height_in_tiles = job->draw_tiles_y;
126
/* Must be >= 1 */
127
config.number_of_render_targets =
128
MAX2(job->nr_cbufs, 1);
129
130
config.multisample_mode_4x = job->msaa;
131
132
config.maximum_bpp_of_all_render_targets = job->internal_bpp;
133
}
134
#endif /* V3D_VERSION < 40 */
135
136
/* There's definitely nothing in the VCD cache we want. */
137
cl_emit(&job->bcl, FLUSH_VCD_CACHE, bin);
138
139
/* Disable any leftover OQ state from another job. */
140
cl_emit(&job->bcl, OCCLUSION_QUERY_COUNTER, counter);
141
142
/* "Binning mode lists must have a Start Tile Binning item (6) after
143
* any prefix state data before the binning list proper starts."
144
*/
145
cl_emit(&job->bcl, START_TILE_BINNING, bin);
146
}
147
/**
148
* Does the initial bining command list setup for drawing to a given FBO.
149
*/
150
static void
151
v3d_start_draw(struct v3d_context *v3d)
152
{
153
struct v3d_job *job = v3d->job;
154
155
if (job->needs_flush)
156
return;
157
158
job->needs_flush = true;
159
job->draw_width = v3d->framebuffer.width;
160
job->draw_height = v3d->framebuffer.height;
161
job->num_layers = util_framebuffer_get_num_layers(&v3d->framebuffer);
162
163
v3d_start_binning(v3d, job);
164
}
165
166
static void
167
v3d_predraw_check_stage_inputs(struct pipe_context *pctx,
168
enum pipe_shader_type s)
169
{
170
struct v3d_context *v3d = v3d_context(pctx);
171
172
/* Flush writes to textures we're sampling. */
173
for (int i = 0; i < v3d->tex[s].num_textures; i++) {
174
struct pipe_sampler_view *pview = v3d->tex[s].textures[i];
175
if (!pview)
176
continue;
177
struct v3d_sampler_view *view = v3d_sampler_view(pview);
178
179
if (view->texture != view->base.texture &&
180
view->base.format != PIPE_FORMAT_X32_S8X24_UINT)
181
v3d_update_shadow_texture(pctx, &view->base);
182
183
v3d_flush_jobs_writing_resource(v3d, view->texture,
184
V3D_FLUSH_DEFAULT,
185
s == PIPE_SHADER_COMPUTE);
186
}
187
188
/* Flush writes to UBOs. */
189
u_foreach_bit(i, v3d->constbuf[s].enabled_mask) {
190
struct pipe_constant_buffer *cb = &v3d->constbuf[s].cb[i];
191
if (cb->buffer) {
192
v3d_flush_jobs_writing_resource(v3d, cb->buffer,
193
V3D_FLUSH_DEFAULT,
194
s == PIPE_SHADER_COMPUTE);
195
}
196
}
197
198
/* Flush reads/writes to our SSBOs */
199
u_foreach_bit(i, v3d->ssbo[s].enabled_mask) {
200
struct pipe_shader_buffer *sb = &v3d->ssbo[s].sb[i];
201
if (sb->buffer) {
202
v3d_flush_jobs_reading_resource(v3d, sb->buffer,
203
V3D_FLUSH_NOT_CURRENT_JOB,
204
s == PIPE_SHADER_COMPUTE);
205
}
206
}
207
208
/* Flush reads/writes to our image views */
209
u_foreach_bit(i, v3d->shaderimg[s].enabled_mask) {
210
struct v3d_image_view *view = &v3d->shaderimg[s].si[i];
211
212
v3d_flush_jobs_reading_resource(v3d, view->base.resource,
213
V3D_FLUSH_NOT_CURRENT_JOB,
214
s == PIPE_SHADER_COMPUTE);
215
}
216
217
/* Flush writes to our vertex buffers (i.e. from transform feedback) */
218
if (s == PIPE_SHADER_VERTEX) {
219
u_foreach_bit(i, v3d->vertexbuf.enabled_mask) {
220
struct pipe_vertex_buffer *vb = &v3d->vertexbuf.vb[i];
221
222
v3d_flush_jobs_writing_resource(v3d, vb->buffer.resource,
223
V3D_FLUSH_DEFAULT,
224
false);
225
}
226
}
227
}
228
229
static void
230
v3d_predraw_check_outputs(struct pipe_context *pctx)
231
{
232
struct v3d_context *v3d = v3d_context(pctx);
233
234
/* Flush jobs reading from TF buffers that we are about to write. */
235
if (v3d_transform_feedback_enabled(v3d)) {
236
struct v3d_streamout_stateobj *so = &v3d->streamout;
237
238
for (int i = 0; i < so->num_targets; i++) {
239
if (!so->targets[i])
240
continue;
241
242
const struct pipe_stream_output_target *target =
243
so->targets[i];
244
v3d_flush_jobs_reading_resource(v3d, target->buffer,
245
V3D_FLUSH_DEFAULT,
246
false);
247
}
248
}
249
}
250
251
/**
252
* Checks if the state for the current draw reads a particular resource in
253
* in the given shader stage.
254
*/
255
static bool
256
v3d_state_reads_resource(struct v3d_context *v3d,
257
struct pipe_resource *prsc,
258
enum pipe_shader_type s)
259
{
260
struct v3d_resource *rsc = v3d_resource(prsc);
261
262
/* Vertex buffers */
263
if (s == PIPE_SHADER_VERTEX) {
264
u_foreach_bit(i, v3d->vertexbuf.enabled_mask) {
265
struct pipe_vertex_buffer *vb = &v3d->vertexbuf.vb[i];
266
if (!vb->buffer.resource)
267
continue;
268
269
struct v3d_resource *vb_rsc =
270
v3d_resource(vb->buffer.resource);
271
if (rsc->bo == vb_rsc->bo)
272
return true;
273
}
274
}
275
276
/* Constant buffers */
277
u_foreach_bit(i, v3d->constbuf[s].enabled_mask) {
278
struct pipe_constant_buffer *cb = &v3d->constbuf[s].cb[i];
279
if (!cb->buffer)
280
continue;
281
282
struct v3d_resource *cb_rsc = v3d_resource(cb->buffer);
283
if (rsc->bo == cb_rsc->bo)
284
return true;
285
}
286
287
/* Shader storage buffers */
288
u_foreach_bit(i, v3d->ssbo[s].enabled_mask) {
289
struct pipe_shader_buffer *sb = &v3d->ssbo[s].sb[i];
290
if (!sb->buffer)
291
continue;
292
293
struct v3d_resource *sb_rsc = v3d_resource(sb->buffer);
294
if (rsc->bo == sb_rsc->bo)
295
return true;
296
}
297
298
/* Textures */
299
for (int i = 0; i < v3d->tex[s].num_textures; i++) {
300
struct pipe_sampler_view *pview = v3d->tex[s].textures[i];
301
if (!pview)
302
continue;
303
304
struct v3d_sampler_view *view = v3d_sampler_view(pview);
305
struct v3d_resource *v_rsc = v3d_resource(view->texture);
306
if (rsc->bo == v_rsc->bo)
307
return true;
308
}
309
310
return false;
311
}
312
313
static void
314
v3d_emit_wait_for_tf(struct v3d_job *job)
315
{
316
/* XXX: we might be able to skip this in some cases, for now we
317
* always emit it.
318
*/
319
cl_emit(&job->bcl, FLUSH_TRANSFORM_FEEDBACK_DATA, flush);
320
321
cl_emit(&job->bcl, WAIT_FOR_TRANSFORM_FEEDBACK, wait) {
322
/* XXX: Wait for all outstanding writes... maybe we can do
323
* better in some cases.
324
*/
325
wait.block_count = 255;
326
}
327
328
/* We have just flushed all our outstanding TF work in this job so make
329
* sure we don't emit TF flushes again for any of it again.
330
*/
331
_mesa_set_clear(job->tf_write_prscs, NULL);
332
}
333
334
static void
335
v3d_emit_wait_for_tf_if_needed(struct v3d_context *v3d, struct v3d_job *job)
336
{
337
if (!job->tf_enabled)
338
return;
339
340
set_foreach(job->tf_write_prscs, entry) {
341
struct pipe_resource *prsc = (struct pipe_resource *)entry->key;
342
for (int s = 0; s < PIPE_SHADER_COMPUTE; s++) {
343
/* Fragment shaders can only start executing after all
344
* binning (and thus TF) is complete.
345
*
346
* XXX: For VS/GS/TES, if the binning shader does not
347
* read the resource then we could also avoid emitting
348
* the wait.
349
*/
350
if (s == PIPE_SHADER_FRAGMENT)
351
continue;
352
353
if (v3d_state_reads_resource(v3d, prsc, s)) {
354
v3d_emit_wait_for_tf(job);
355
return;
356
}
357
}
358
}
359
}
360
361
#if V3D_VERSION >= 41
362
static void
363
v3d_emit_gs_state_record(struct v3d_job *job,
364
struct v3d_compiled_shader *gs_bin,
365
struct v3d_cl_reloc gs_bin_uniforms,
366
struct v3d_compiled_shader *gs,
367
struct v3d_cl_reloc gs_render_uniforms)
368
{
369
cl_emit(&job->indirect, GEOMETRY_SHADER_STATE_RECORD, shader) {
370
shader.geometry_bin_mode_shader_code_address =
371
cl_address(v3d_resource(gs_bin->resource)->bo,
372
gs_bin->offset);
373
shader.geometry_bin_mode_shader_4_way_threadable =
374
gs_bin->prog_data.gs->base.threads == 4;
375
shader.geometry_bin_mode_shader_start_in_final_thread_section =
376
gs_bin->prog_data.gs->base.single_seg;
377
shader.geometry_bin_mode_shader_propagate_nans = true;
378
shader.geometry_bin_mode_shader_uniforms_address =
379
gs_bin_uniforms;
380
381
shader.geometry_render_mode_shader_code_address =
382
cl_address(v3d_resource(gs->resource)->bo, gs->offset);
383
shader.geometry_render_mode_shader_4_way_threadable =
384
gs->prog_data.gs->base.threads == 4;
385
shader.geometry_render_mode_shader_start_in_final_thread_section =
386
gs->prog_data.gs->base.single_seg;
387
shader.geometry_render_mode_shader_propagate_nans = true;
388
shader.geometry_render_mode_shader_uniforms_address =
389
gs_render_uniforms;
390
}
391
}
392
393
static uint8_t
394
v3d_gs_output_primitive(uint32_t prim_type)
395
{
396
switch (prim_type) {
397
case GL_POINTS:
398
return GEOMETRY_SHADER_POINTS;
399
case GL_LINE_STRIP:
400
return GEOMETRY_SHADER_LINE_STRIP;
401
case GL_TRIANGLE_STRIP:
402
return GEOMETRY_SHADER_TRI_STRIP;
403
default:
404
unreachable("Unsupported primitive type");
405
}
406
}
407
408
static void
409
v3d_emit_tes_gs_common_params(struct v3d_job *job,
410
uint8_t gs_out_prim_type,
411
uint8_t gs_num_invocations)
412
{
413
/* This, and v3d_emit_tes_gs_shader_params below, fill in default
414
* values for tessellation fields even though we don't support
415
* tessellation yet because our packing functions (and the simulator)
416
* complain if we don't.
417
*/
418
cl_emit(&job->indirect, TESSELLATION_GEOMETRY_COMMON_PARAMS, shader) {
419
shader.tessellation_type = TESSELLATION_TYPE_TRIANGLE;
420
shader.tessellation_point_mode = false;
421
shader.tessellation_edge_spacing = TESSELLATION_EDGE_SPACING_EVEN;
422
shader.tessellation_clockwise = true;
423
shader.tessellation_invocations = 1;
424
425
shader.geometry_shader_output_format =
426
v3d_gs_output_primitive(gs_out_prim_type);
427
shader.geometry_shader_instances = gs_num_invocations & 0x1F;
428
}
429
}
430
431
static uint8_t
432
simd_width_to_gs_pack_mode(uint32_t width)
433
{
434
switch (width) {
435
case 16:
436
return V3D_PACK_MODE_16_WAY;
437
case 8:
438
return V3D_PACK_MODE_8_WAY;
439
case 4:
440
return V3D_PACK_MODE_4_WAY;
441
case 1:
442
return V3D_PACK_MODE_1_WAY;
443
default:
444
unreachable("Invalid SIMD width");
445
};
446
}
447
448
static void
449
v3d_emit_tes_gs_shader_params(struct v3d_job *job,
450
uint32_t gs_simd,
451
uint32_t gs_vpm_output_size,
452
uint32_t gs_max_vpm_input_size_per_batch)
453
{
454
cl_emit(&job->indirect, TESSELLATION_GEOMETRY_SHADER_PARAMS, shader) {
455
shader.tcs_batch_flush_mode = V3D_TCS_FLUSH_MODE_FULLY_PACKED;
456
shader.per_patch_data_column_depth = 1;
457
shader.tcs_output_segment_size_in_sectors = 1;
458
shader.tcs_output_segment_pack_mode = V3D_PACK_MODE_16_WAY;
459
shader.tes_output_segment_size_in_sectors = 1;
460
shader.tes_output_segment_pack_mode = V3D_PACK_MODE_16_WAY;
461
shader.gs_output_segment_size_in_sectors = gs_vpm_output_size;
462
shader.gs_output_segment_pack_mode =
463
simd_width_to_gs_pack_mode(gs_simd);
464
shader.tbg_max_patches_per_tcs_batch = 1;
465
shader.tbg_max_extra_vertex_segs_for_patches_after_first = 0;
466
shader.tbg_min_tcs_output_segments_required_in_play = 1;
467
shader.tbg_min_per_patch_data_segments_required_in_play = 1;
468
shader.tpg_max_patches_per_tes_batch = 1;
469
shader.tpg_max_vertex_segments_per_tes_batch = 0;
470
shader.tpg_max_tcs_output_segments_per_tes_batch = 1;
471
shader.tpg_min_tes_output_segments_required_in_play = 1;
472
shader.gbg_max_tes_output_vertex_segments_per_gs_batch =
473
gs_max_vpm_input_size_per_batch;
474
shader.gbg_min_gs_output_segments_required_in_play = 1;
475
}
476
}
477
#endif
478
479
static void
480
v3d_emit_gl_shader_state(struct v3d_context *v3d,
481
const struct pipe_draw_info *info)
482
{
483
struct v3d_job *job = v3d->job;
484
/* V3D_DIRTY_VTXSTATE */
485
struct v3d_vertex_stateobj *vtx = v3d->vtx;
486
/* V3D_DIRTY_VTXBUF */
487
struct v3d_vertexbuf_stateobj *vertexbuf = &v3d->vertexbuf;
488
489
/* Upload the uniforms to the indirect CL first */
490
struct v3d_cl_reloc fs_uniforms =
491
v3d_write_uniforms(v3d, job, v3d->prog.fs,
492
PIPE_SHADER_FRAGMENT);
493
494
struct v3d_cl_reloc gs_uniforms = { NULL, 0 };
495
struct v3d_cl_reloc gs_bin_uniforms = { NULL, 0 };
496
if (v3d->prog.gs) {
497
gs_uniforms = v3d_write_uniforms(v3d, job, v3d->prog.gs,
498
PIPE_SHADER_GEOMETRY);
499
}
500
if (v3d->prog.gs_bin) {
501
gs_bin_uniforms = v3d_write_uniforms(v3d, job, v3d->prog.gs_bin,
502
PIPE_SHADER_GEOMETRY);
503
}
504
505
struct v3d_cl_reloc vs_uniforms =
506
v3d_write_uniforms(v3d, job, v3d->prog.vs,
507
PIPE_SHADER_VERTEX);
508
struct v3d_cl_reloc cs_uniforms =
509
v3d_write_uniforms(v3d, job, v3d->prog.cs,
510
PIPE_SHADER_VERTEX);
511
512
/* Update the cache dirty flag based on the shader progs data */
513
job->tmu_dirty_rcl |= v3d->prog.cs->prog_data.vs->base.tmu_dirty_rcl;
514
job->tmu_dirty_rcl |= v3d->prog.vs->prog_data.vs->base.tmu_dirty_rcl;
515
if (v3d->prog.gs_bin) {
516
job->tmu_dirty_rcl |=
517
v3d->prog.gs_bin->prog_data.gs->base.tmu_dirty_rcl;
518
}
519
if (v3d->prog.gs) {
520
job->tmu_dirty_rcl |=
521
v3d->prog.gs->prog_data.gs->base.tmu_dirty_rcl;
522
}
523
job->tmu_dirty_rcl |= v3d->prog.fs->prog_data.fs->base.tmu_dirty_rcl;
524
525
uint32_t num_elements_to_emit = 0;
526
for (int i = 0; i < vtx->num_elements; i++) {
527
struct pipe_vertex_element *elem = &vtx->pipe[i];
528
struct pipe_vertex_buffer *vb =
529
&vertexbuf->vb[elem->vertex_buffer_index];
530
if (vb->buffer.resource)
531
num_elements_to_emit++;
532
}
533
534
uint32_t shader_state_record_length =
535
cl_packet_length(GL_SHADER_STATE_RECORD);
536
#if V3D_VERSION >= 41
537
if (v3d->prog.gs) {
538
shader_state_record_length +=
539
cl_packet_length(GEOMETRY_SHADER_STATE_RECORD) +
540
cl_packet_length(TESSELLATION_GEOMETRY_COMMON_PARAMS) +
541
2 * cl_packet_length(TESSELLATION_GEOMETRY_SHADER_PARAMS);
542
}
543
#endif
544
545
/* See GFXH-930 workaround below */
546
uint32_t shader_rec_offset =
547
v3d_cl_ensure_space(&job->indirect,
548
shader_state_record_length +
549
MAX2(num_elements_to_emit, 1) *
550
cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD),
551
32);
552
553
/* XXX perf: We should move most of the SHADER_STATE_RECORD setup to
554
* compile time, so that we mostly just have to OR the VS and FS
555
* records together at draw time.
556
*/
557
558
struct vpm_config vpm_cfg_bin, vpm_cfg;
559
560
assert(v3d->screen->devinfo.ver >= 41 || !v3d->prog.gs);
561
v3d_compute_vpm_config(&v3d->screen->devinfo,
562
v3d->prog.cs->prog_data.vs,
563
v3d->prog.vs->prog_data.vs,
564
v3d->prog.gs ? v3d->prog.gs_bin->prog_data.gs : NULL,
565
v3d->prog.gs ? v3d->prog.gs->prog_data.gs : NULL,
566
&vpm_cfg_bin,
567
&vpm_cfg);
568
569
if (v3d->prog.gs) {
570
#if V3D_VERSION >= 41
571
v3d_emit_gs_state_record(v3d->job,
572
v3d->prog.gs_bin, gs_bin_uniforms,
573
v3d->prog.gs, gs_uniforms);
574
575
struct v3d_gs_prog_data *gs = v3d->prog.gs->prog_data.gs;
576
v3d_emit_tes_gs_common_params(v3d->job,
577
gs->out_prim_type,
578
gs->num_invocations);
579
580
/* Bin Tes/Gs params */
581
v3d_emit_tes_gs_shader_params(v3d->job,
582
vpm_cfg_bin.gs_width,
583
vpm_cfg_bin.Gd,
584
vpm_cfg_bin.Gv);
585
586
/* Render Tes/Gs params */
587
v3d_emit_tes_gs_shader_params(v3d->job,
588
vpm_cfg.gs_width,
589
vpm_cfg.Gd,
590
vpm_cfg.Gv);
591
#else
592
unreachable("No GS support pre-4.1");
593
#endif
594
}
595
596
cl_emit(&job->indirect, GL_SHADER_STATE_RECORD, shader) {
597
shader.enable_clipping = true;
598
/* V3D_DIRTY_PRIM_MODE | V3D_DIRTY_RASTERIZER */
599
shader.point_size_in_shaded_vertex_data =
600
(info->mode == PIPE_PRIM_POINTS &&
601
v3d->rasterizer->base.point_size_per_vertex);
602
603
/* Must be set if the shader modifies Z, discards, or modifies
604
* the sample mask. For any of these cases, the fragment
605
* shader needs to write the Z value (even just discards).
606
*/
607
shader.fragment_shader_does_z_writes =
608
v3d->prog.fs->prog_data.fs->writes_z;
609
/* Set if the EZ test must be disabled (due to shader side
610
* effects and the early_z flag not being present in the
611
* shader).
612
*/
613
shader.turn_off_early_z_test =
614
v3d->prog.fs->prog_data.fs->disable_ez;
615
616
shader.fragment_shader_uses_real_pixel_centre_w_in_addition_to_centroid_w2 =
617
v3d->prog.fs->prog_data.fs->uses_center_w;
618
619
#if V3D_VERSION >= 41
620
shader.any_shader_reads_hardware_written_primitive_id =
621
(v3d->prog.gs && v3d->prog.gs->prog_data.gs->uses_pid) ||
622
v3d->prog.fs->prog_data.fs->uses_pid;
623
shader.insert_primitive_id_as_first_varying_to_fragment_shader =
624
!v3d->prog.gs && v3d->prog.fs->prog_data.fs->uses_pid;
625
#endif
626
627
#if V3D_VERSION >= 40
628
shader.do_scoreboard_wait_on_first_thread_switch =
629
v3d->prog.fs->prog_data.fs->lock_scoreboard_on_first_thrsw;
630
shader.disable_implicit_point_line_varyings =
631
!v3d->prog.fs->prog_data.fs->uses_implicit_point_line_varyings;
632
#endif
633
634
shader.number_of_varyings_in_fragment_shader =
635
v3d->prog.fs->prog_data.fs->num_inputs;
636
637
shader.coordinate_shader_propagate_nans = true;
638
shader.vertex_shader_propagate_nans = true;
639
shader.fragment_shader_propagate_nans = true;
640
641
shader.coordinate_shader_code_address =
642
cl_address(v3d_resource(v3d->prog.cs->resource)->bo,
643
v3d->prog.cs->offset);
644
shader.vertex_shader_code_address =
645
cl_address(v3d_resource(v3d->prog.vs->resource)->bo,
646
v3d->prog.vs->offset);
647
shader.fragment_shader_code_address =
648
cl_address(v3d_resource(v3d->prog.fs->resource)->bo,
649
v3d->prog.fs->offset);
650
651
/* XXX: Use combined input/output size flag in the common
652
* case.
653
*/
654
shader.coordinate_shader_has_separate_input_and_output_vpm_blocks =
655
v3d->prog.cs->prog_data.vs->separate_segments;
656
shader.vertex_shader_has_separate_input_and_output_vpm_blocks =
657
v3d->prog.vs->prog_data.vs->separate_segments;
658
659
shader.coordinate_shader_input_vpm_segment_size =
660
v3d->prog.cs->prog_data.vs->separate_segments ?
661
v3d->prog.cs->prog_data.vs->vpm_input_size : 1;
662
shader.vertex_shader_input_vpm_segment_size =
663
v3d->prog.vs->prog_data.vs->separate_segments ?
664
v3d->prog.vs->prog_data.vs->vpm_input_size : 1;
665
666
shader.coordinate_shader_output_vpm_segment_size =
667
v3d->prog.cs->prog_data.vs->vpm_output_size;
668
shader.vertex_shader_output_vpm_segment_size =
669
v3d->prog.vs->prog_data.vs->vpm_output_size;
670
671
shader.coordinate_shader_uniforms_address = cs_uniforms;
672
shader.vertex_shader_uniforms_address = vs_uniforms;
673
shader.fragment_shader_uniforms_address = fs_uniforms;
674
675
#if V3D_VERSION >= 41
676
shader.min_coord_shader_input_segments_required_in_play =
677
vpm_cfg_bin.As;
678
shader.min_vertex_shader_input_segments_required_in_play =
679
vpm_cfg.As;
680
681
shader.min_coord_shader_output_segments_required_in_play_in_addition_to_vcm_cache_size =
682
vpm_cfg_bin.Ve;
683
shader.min_vertex_shader_output_segments_required_in_play_in_addition_to_vcm_cache_size =
684
vpm_cfg.Ve;
685
686
shader.coordinate_shader_4_way_threadable =
687
v3d->prog.cs->prog_data.vs->base.threads == 4;
688
shader.vertex_shader_4_way_threadable =
689
v3d->prog.vs->prog_data.vs->base.threads == 4;
690
shader.fragment_shader_4_way_threadable =
691
v3d->prog.fs->prog_data.fs->base.threads == 4;
692
693
shader.coordinate_shader_start_in_final_thread_section =
694
v3d->prog.cs->prog_data.vs->base.single_seg;
695
shader.vertex_shader_start_in_final_thread_section =
696
v3d->prog.vs->prog_data.vs->base.single_seg;
697
shader.fragment_shader_start_in_final_thread_section =
698
v3d->prog.fs->prog_data.fs->base.single_seg;
699
#else
700
shader.coordinate_shader_4_way_threadable =
701
v3d->prog.cs->prog_data.vs->base.threads == 4;
702
shader.coordinate_shader_2_way_threadable =
703
v3d->prog.cs->prog_data.vs->base.threads == 2;
704
shader.vertex_shader_4_way_threadable =
705
v3d->prog.vs->prog_data.vs->base.threads == 4;
706
shader.vertex_shader_2_way_threadable =
707
v3d->prog.vs->prog_data.vs->base.threads == 2;
708
shader.fragment_shader_4_way_threadable =
709
v3d->prog.fs->prog_data.fs->base.threads == 4;
710
shader.fragment_shader_2_way_threadable =
711
v3d->prog.fs->prog_data.fs->base.threads == 2;
712
#endif
713
714
shader.vertex_id_read_by_coordinate_shader =
715
v3d->prog.cs->prog_data.vs->uses_vid;
716
shader.instance_id_read_by_coordinate_shader =
717
v3d->prog.cs->prog_data.vs->uses_iid;
718
shader.vertex_id_read_by_vertex_shader =
719
v3d->prog.vs->prog_data.vs->uses_vid;
720
shader.instance_id_read_by_vertex_shader =
721
v3d->prog.vs->prog_data.vs->uses_iid;
722
723
shader.address_of_default_attribute_values =
724
cl_address(v3d_resource(vtx->defaults)->bo,
725
vtx->defaults_offset);
726
}
727
728
bool cs_loaded_any = false;
729
for (int i = 0; i < vtx->num_elements; i++) {
730
struct pipe_vertex_element *elem = &vtx->pipe[i];
731
struct pipe_vertex_buffer *vb =
732
&vertexbuf->vb[elem->vertex_buffer_index];
733
struct v3d_resource *rsc = v3d_resource(vb->buffer.resource);
734
735
if (!rsc)
736
continue;
737
738
const uint32_t size =
739
cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD);
740
cl_emit_with_prepacked(&job->indirect,
741
GL_SHADER_STATE_ATTRIBUTE_RECORD,
742
&vtx->attrs[i * size], attr) {
743
attr.stride = vb->stride;
744
attr.address = cl_address(rsc->bo,
745
vb->buffer_offset +
746
elem->src_offset);
747
attr.number_of_values_read_by_coordinate_shader =
748
v3d->prog.cs->prog_data.vs->vattr_sizes[i];
749
attr.number_of_values_read_by_vertex_shader =
750
v3d->prog.vs->prog_data.vs->vattr_sizes[i];
751
752
/* GFXH-930: At least one attribute must be enabled
753
* and read by CS and VS. If we have attributes being
754
* consumed by the VS but not the CS, then set up a
755
* dummy load of the last attribute into the CS's VPM
756
* inputs. (Since CS is just dead-code-elimination
757
* compared to VS, we can't have CS loading but not
758
* VS).
759
*/
760
if (v3d->prog.cs->prog_data.vs->vattr_sizes[i])
761
cs_loaded_any = true;
762
if (i == vtx->num_elements - 1 && !cs_loaded_any) {
763
attr.number_of_values_read_by_coordinate_shader = 1;
764
}
765
#if V3D_VERSION >= 41
766
attr.maximum_index = 0xffffff;
767
#endif
768
}
769
STATIC_ASSERT(sizeof(vtx->attrs) >= V3D_MAX_VS_INPUTS / 4 * size);
770
}
771
772
if (num_elements_to_emit == 0) {
773
/* GFXH-930: At least one attribute must be enabled and read
774
* by CS and VS. If we have no attributes being consumed by
775
* the shader, set up a dummy to be loaded into the VPM.
776
*/
777
cl_emit(&job->indirect, GL_SHADER_STATE_ATTRIBUTE_RECORD, attr) {
778
/* Valid address of data whose value will be unused. */
779
attr.address = cl_address(job->indirect.bo, 0);
780
781
attr.type = ATTRIBUTE_FLOAT;
782
attr.stride = 0;
783
attr.vec_size = 1;
784
785
attr.number_of_values_read_by_coordinate_shader = 1;
786
attr.number_of_values_read_by_vertex_shader = 1;
787
}
788
num_elements_to_emit = 1;
789
}
790
791
cl_emit(&job->bcl, VCM_CACHE_SIZE, vcm) {
792
vcm.number_of_16_vertex_batches_for_binning = vpm_cfg_bin.Vc;
793
vcm.number_of_16_vertex_batches_for_rendering = vpm_cfg.Vc;
794
}
795
796
#if V3D_VERSION >= 41
797
if (v3d->prog.gs) {
798
cl_emit(&job->bcl, GL_SHADER_STATE_INCLUDING_GS, state) {
799
state.address = cl_address(job->indirect.bo,
800
shader_rec_offset);
801
state.number_of_attribute_arrays = num_elements_to_emit;
802
}
803
} else {
804
cl_emit(&job->bcl, GL_SHADER_STATE, state) {
805
state.address = cl_address(job->indirect.bo,
806
shader_rec_offset);
807
state.number_of_attribute_arrays = num_elements_to_emit;
808
}
809
}
810
#else
811
assert(!v3d->prog.gs);
812
cl_emit(&job->bcl, GL_SHADER_STATE, state) {
813
state.address = cl_address(job->indirect.bo, shader_rec_offset);
814
state.number_of_attribute_arrays = num_elements_to_emit;
815
}
816
#endif
817
818
v3d_bo_unreference(&cs_uniforms.bo);
819
v3d_bo_unreference(&vs_uniforms.bo);
820
if (gs_uniforms.bo)
821
v3d_bo_unreference(&gs_uniforms.bo);
822
if (gs_bin_uniforms.bo)
823
v3d_bo_unreference(&gs_bin_uniforms.bo);
824
v3d_bo_unreference(&fs_uniforms.bo);
825
}
826
827
/**
828
* Updates the number of primitives generated from the number of vertices
829
* to draw. This only works when no GS is present, since otherwise the number
830
* of primitives generated cannot be determined in advance and we need to
831
* use the PRIMITIVE_COUNTS_FEEDBACK command instead, however, that requires
832
* a sync wait for the draw to complete, so we only use that when GS is present.
833
*/
834
static void
835
v3d_update_primitives_generated_counter(struct v3d_context *v3d,
836
const struct pipe_draw_info *info,
837
const struct pipe_draw_start_count_bias *draw)
838
{
839
assert(!v3d->prog.gs);
840
841
if (!v3d->active_queries)
842
return;
843
844
uint32_t prims = u_prims_for_vertices(info->mode, draw->count);
845
v3d->prims_generated += prims;
846
}
847
848
static void
849
v3d_update_job_ez(struct v3d_context *v3d, struct v3d_job *job)
850
{
851
switch (v3d->zsa->ez_state) {
852
case V3D_EZ_UNDECIDED:
853
/* If the Z/S state didn't pick a direction but didn't
854
* disable, then go along with the current EZ state. This
855
* allows EZ optimization for Z func == EQUAL or NEVER.
856
*/
857
break;
858
859
case V3D_EZ_LT_LE:
860
case V3D_EZ_GT_GE:
861
/* If the Z/S state picked a direction, then it needs to match
862
* the current direction if we've decided on one.
863
*/
864
if (job->ez_state == V3D_EZ_UNDECIDED)
865
job->ez_state = v3d->zsa->ez_state;
866
else if (job->ez_state != v3d->zsa->ez_state)
867
job->ez_state = V3D_EZ_DISABLED;
868
break;
869
870
case V3D_EZ_DISABLED:
871
/* If the current Z/S state disables EZ because of a bad Z
872
* func or stencil operation, then we can't do any more EZ in
873
* this frame.
874
*/
875
job->ez_state = V3D_EZ_DISABLED;
876
break;
877
}
878
879
/* If the FS affects the Z of the pixels, then it may update against
880
* the chosen EZ direction (though we could use
881
* ARB_conservative_depth's hints to avoid this)
882
*/
883
if (v3d->prog.fs->prog_data.fs->writes_z) {
884
job->ez_state = V3D_EZ_DISABLED;
885
}
886
887
if (job->first_ez_state == V3D_EZ_UNDECIDED &&
888
(job->ez_state != V3D_EZ_DISABLED || job->draw_calls_queued == 0))
889
job->first_ez_state = job->ez_state;
890
}
891
892
static uint32_t
893
v3d_hw_prim_type(enum pipe_prim_type prim_type)
894
{
895
switch (prim_type) {
896
case PIPE_PRIM_POINTS:
897
case PIPE_PRIM_LINES:
898
case PIPE_PRIM_LINE_LOOP:
899
case PIPE_PRIM_LINE_STRIP:
900
case PIPE_PRIM_TRIANGLES:
901
case PIPE_PRIM_TRIANGLE_STRIP:
902
case PIPE_PRIM_TRIANGLE_FAN:
903
return prim_type;
904
905
case PIPE_PRIM_LINES_ADJACENCY:
906
case PIPE_PRIM_LINE_STRIP_ADJACENCY:
907
case PIPE_PRIM_TRIANGLES_ADJACENCY:
908
case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
909
return 8 + (prim_type - PIPE_PRIM_LINES_ADJACENCY);
910
911
default:
912
unreachable("Unsupported primitive type");
913
}
914
}
915
916
static bool
917
v3d_check_compiled_shaders(struct v3d_context *v3d)
918
{
919
static bool warned[5] = { 0 };
920
921
uint32_t failed_stage = MESA_SHADER_NONE;
922
if (!v3d->prog.vs->resource || !v3d->prog.cs->resource) {
923
failed_stage = MESA_SHADER_VERTEX;
924
} else if ((v3d->prog.gs_bin && !v3d->prog.gs_bin->resource) ||
925
(v3d->prog.gs && !v3d->prog.gs->resource)) {
926
failed_stage = MESA_SHADER_GEOMETRY;
927
} else if (v3d->prog.fs && !v3d->prog.fs->resource) {
928
failed_stage = MESA_SHADER_FRAGMENT;
929
}
930
931
if (likely(failed_stage == MESA_SHADER_NONE))
932
return true;
933
934
if (!warned[failed_stage]) {
935
fprintf(stderr,
936
"%s shader failed to compile. Expect corruption.\n",
937
_mesa_shader_stage_to_string(failed_stage));
938
warned[failed_stage] = true;
939
}
940
return false;
941
}
942
943
static void
944
v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info,
945
unsigned drawid_offset,
946
const struct pipe_draw_indirect_info *indirect,
947
const struct pipe_draw_start_count_bias *draws,
948
unsigned num_draws)
949
{
950
if (num_draws > 1) {
951
util_draw_multi(pctx, info, drawid_offset, indirect, draws, num_draws);
952
return;
953
}
954
955
if (!indirect && (!draws[0].count || !info->instance_count))
956
return;
957
958
struct v3d_context *v3d = v3d_context(pctx);
959
960
if (!indirect &&
961
!info->primitive_restart &&
962
!u_trim_pipe_prim(info->mode, (unsigned*)&draws[0].count))
963
return;
964
965
/* Fall back for weird desktop GL primitive restart values. */
966
if (info->primitive_restart &&
967
info->index_size) {
968
uint32_t mask = util_prim_restart_index_from_size(info->index_size);
969
if (info->restart_index != mask) {
970
util_draw_vbo_without_prim_restart(pctx, info, drawid_offset, indirect, &draws[0]);
971
return;
972
}
973
}
974
975
if (info->mode >= PIPE_PRIM_QUADS && info->mode <= PIPE_PRIM_POLYGON) {
976
util_primconvert_save_rasterizer_state(v3d->primconvert, &v3d->rasterizer->base);
977
util_primconvert_draw_vbo(v3d->primconvert, info, drawid_offset, indirect, draws, num_draws);
978
perf_debug("Fallback conversion for %d %s vertices\n",
979
draws[0].count, u_prim_name(info->mode));
980
return;
981
}
982
983
/* Before setting up the draw, flush anything writing to the resources
984
* that we read from or reading from resources we write to.
985
*/
986
for (int s = 0; s < PIPE_SHADER_COMPUTE; s++)
987
v3d_predraw_check_stage_inputs(pctx, s);
988
989
if (indirect && indirect->buffer) {
990
v3d_flush_jobs_writing_resource(v3d, indirect->buffer,
991
V3D_FLUSH_DEFAULT, false);
992
}
993
994
v3d_predraw_check_outputs(pctx);
995
996
/* If transform feedback is active and we are switching primitive type
997
* we need to submit the job before drawing and update the vertex count
998
* written to TF based on the primitive type since we will need to
999
* know the exact vertex count if the application decides to call
1000
* glDrawTransformFeedback() later.
1001
*/
1002
if (v3d->streamout.num_targets > 0 &&
1003
u_base_prim_type(info->mode) != u_base_prim_type(v3d->prim_mode)) {
1004
v3d_update_primitive_counters(v3d);
1005
}
1006
1007
struct v3d_job *job = v3d_get_job_for_fbo(v3d);
1008
1009
/* If vertex texturing depends on the output of rendering, we need to
1010
* ensure that that rendering is complete before we run a coordinate
1011
* shader that depends on it.
1012
*
1013
* Given that doing that is unusual, for now we just block the binner
1014
* on the last submitted render, rather than tracking the last
1015
* rendering to each texture's BO.
1016
*/
1017
if (v3d->tex[PIPE_SHADER_VERTEX].num_textures || (indirect && indirect->buffer)) {
1018
perf_debug("Blocking binner on last render "
1019
"due to vertex texturing or indirect drawing.\n");
1020
job->submit.in_sync_bcl = v3d->out_sync;
1021
}
1022
1023
/* We also need to ensure that compute is complete when render depends
1024
* on resources written by it.
1025
*/
1026
if (v3d->sync_on_last_compute_job) {
1027
job->submit.in_sync_bcl = v3d->out_sync;
1028
v3d->sync_on_last_compute_job = false;
1029
}
1030
1031
/* Mark SSBOs and images as being written. We don't actually know
1032
* which ones are read vs written, so just assume the worst.
1033
*/
1034
for (int s = 0; s < PIPE_SHADER_COMPUTE; s++) {
1035
u_foreach_bit(i, v3d->ssbo[s].enabled_mask) {
1036
v3d_job_add_write_resource(job,
1037
v3d->ssbo[s].sb[i].buffer);
1038
job->tmu_dirty_rcl = true;
1039
}
1040
1041
u_foreach_bit(i, v3d->shaderimg[s].enabled_mask) {
1042
v3d_job_add_write_resource(job,
1043
v3d->shaderimg[s].si[i].base.resource);
1044
job->tmu_dirty_rcl = true;
1045
}
1046
}
1047
1048
/* Get space to emit our draw call into the BCL, using a branch to
1049
* jump to a new BO if necessary.
1050
*/
1051
v3d_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);
1052
1053
if (v3d->prim_mode != info->mode) {
1054
v3d->prim_mode = info->mode;
1055
v3d->dirty |= V3D_DIRTY_PRIM_MODE;
1056
}
1057
1058
v3d_start_draw(v3d);
1059
v3d_update_compiled_shaders(v3d, info->mode);
1060
if (!v3d_check_compiled_shaders(v3d))
1061
return;
1062
v3d_update_job_ez(v3d, job);
1063
1064
/* If this job was writing to transform feedback buffers before this
1065
* draw and we are reading from them here, then we need to wait for TF
1066
* to complete before we emit this draw.
1067
*
1068
* Notice this check needs to happen before we emit state for the
1069
* current draw call, where we update job->tf_enabled, so we can ensure
1070
* that we only check TF writes for prior draws.
1071
*/
1072
v3d_emit_wait_for_tf_if_needed(v3d, job);
1073
1074
#if V3D_VERSION >= 41
1075
v3d41_emit_state(pctx);
1076
#else
1077
v3d33_emit_state(pctx);
1078
#endif
1079
1080
if (v3d->dirty & (V3D_DIRTY_VTXBUF |
1081
V3D_DIRTY_VTXSTATE |
1082
V3D_DIRTY_PRIM_MODE |
1083
V3D_DIRTY_RASTERIZER |
1084
V3D_DIRTY_COMPILED_CS |
1085
V3D_DIRTY_COMPILED_VS |
1086
V3D_DIRTY_COMPILED_GS_BIN |
1087
V3D_DIRTY_COMPILED_GS |
1088
V3D_DIRTY_COMPILED_FS |
1089
v3d->prog.cs->uniform_dirty_bits |
1090
v3d->prog.vs->uniform_dirty_bits |
1091
(v3d->prog.gs_bin ?
1092
v3d->prog.gs_bin->uniform_dirty_bits : 0) |
1093
(v3d->prog.gs ?
1094
v3d->prog.gs->uniform_dirty_bits : 0) |
1095
v3d->prog.fs->uniform_dirty_bits)) {
1096
v3d_emit_gl_shader_state(v3d, info);
1097
}
1098
1099
v3d->dirty = 0;
1100
1101
/* The Base Vertex/Base Instance packet sets those values to nonzero
1102
* for the next draw call only.
1103
*/
1104
if ((info->index_size && draws->index_bias) || info->start_instance) {
1105
cl_emit(&job->bcl, BASE_VERTEX_BASE_INSTANCE, base) {
1106
base.base_instance = info->start_instance;
1107
base.base_vertex = info->index_size ? draws->index_bias : 0;
1108
}
1109
}
1110
1111
uint32_t prim_tf_enable = 0;
1112
#if V3D_VERSION < 40
1113
/* V3D 3.x: The HW only processes transform feedback on primitives
1114
* with the flag set.
1115
*/
1116
if (v3d->streamout.num_targets)
1117
prim_tf_enable = (V3D_PRIM_POINTS_TF - V3D_PRIM_POINTS);
1118
#endif
1119
1120
if (!v3d->prog.gs)
1121
v3d_update_primitives_generated_counter(v3d, info, &draws[0]);
1122
1123
uint32_t hw_prim_type = v3d_hw_prim_type(info->mode);
1124
if (info->index_size) {
1125
uint32_t index_size = info->index_size;
1126
uint32_t offset = draws[0].start * index_size;
1127
struct pipe_resource *prsc;
1128
if (info->has_user_indices) {
1129
unsigned start_offset = draws[0].start * info->index_size;
1130
prsc = NULL;
1131
u_upload_data(v3d->uploader, start_offset,
1132
draws[0].count * info->index_size, 4,
1133
(char*)info->index.user + start_offset,
1134
&offset, &prsc);
1135
} else {
1136
prsc = info->index.resource;
1137
}
1138
struct v3d_resource *rsc = v3d_resource(prsc);
1139
1140
#if V3D_VERSION >= 40
1141
cl_emit(&job->bcl, INDEX_BUFFER_SETUP, ib) {
1142
ib.address = cl_address(rsc->bo, 0);
1143
ib.size = rsc->bo->size;
1144
}
1145
#endif
1146
1147
if (indirect && indirect->buffer) {
1148
cl_emit(&job->bcl, INDIRECT_INDEXED_INSTANCED_PRIM_LIST, prim) {
1149
prim.index_type = ffs(info->index_size) - 1;
1150
#if V3D_VERSION < 40
1151
prim.address_of_indices_list =
1152
cl_address(rsc->bo, offset);
1153
#endif /* V3D_VERSION < 40 */
1154
prim.mode = hw_prim_type | prim_tf_enable;
1155
prim.enable_primitive_restarts = info->primitive_restart;
1156
1157
prim.number_of_draw_indirect_indexed_records = indirect->draw_count;
1158
1159
prim.stride_in_multiples_of_4_bytes = indirect->stride >> 2;
1160
prim.address = cl_address(v3d_resource(indirect->buffer)->bo,
1161
indirect->offset);
1162
}
1163
} else if (info->instance_count > 1) {
1164
cl_emit(&job->bcl, INDEXED_INSTANCED_PRIM_LIST, prim) {
1165
prim.index_type = ffs(info->index_size) - 1;
1166
#if V3D_VERSION >= 40
1167
prim.index_offset = offset;
1168
#else /* V3D_VERSION < 40 */
1169
prim.maximum_index = (1u << 31) - 1; /* XXX */
1170
prim.address_of_indices_list =
1171
cl_address(rsc->bo, offset);
1172
#endif /* V3D_VERSION < 40 */
1173
prim.mode = hw_prim_type | prim_tf_enable;
1174
prim.enable_primitive_restarts = info->primitive_restart;
1175
1176
prim.number_of_instances = info->instance_count;
1177
prim.instance_length = draws[0].count;
1178
}
1179
} else {
1180
cl_emit(&job->bcl, INDEXED_PRIM_LIST, prim) {
1181
prim.index_type = ffs(info->index_size) - 1;
1182
prim.length = draws[0].count;
1183
#if V3D_VERSION >= 40
1184
prim.index_offset = offset;
1185
#else /* V3D_VERSION < 40 */
1186
prim.maximum_index = (1u << 31) - 1; /* XXX */
1187
prim.address_of_indices_list =
1188
cl_address(rsc->bo, offset);
1189
#endif /* V3D_VERSION < 40 */
1190
prim.mode = hw_prim_type | prim_tf_enable;
1191
prim.enable_primitive_restarts = info->primitive_restart;
1192
}
1193
}
1194
1195
if (info->has_user_indices)
1196
pipe_resource_reference(&prsc, NULL);
1197
} else {
1198
if (indirect && indirect->buffer) {
1199
cl_emit(&job->bcl, INDIRECT_VERTEX_ARRAY_INSTANCED_PRIMS, prim) {
1200
prim.mode = hw_prim_type | prim_tf_enable;
1201
prim.number_of_draw_indirect_array_records = indirect->draw_count;
1202
1203
prim.stride_in_multiples_of_4_bytes = indirect->stride >> 2;
1204
prim.address = cl_address(v3d_resource(indirect->buffer)->bo,
1205
indirect->offset);
1206
}
1207
} else if (info->instance_count > 1) {
1208
struct pipe_stream_output_target *so =
1209
indirect && indirect->count_from_stream_output ?
1210
indirect->count_from_stream_output : NULL;
1211
uint32_t vert_count = so ?
1212
v3d_stream_output_target_get_vertex_count(so) :
1213
draws[0].count;
1214
cl_emit(&job->bcl, VERTEX_ARRAY_INSTANCED_PRIMS, prim) {
1215
prim.mode = hw_prim_type | prim_tf_enable;
1216
prim.index_of_first_vertex = draws[0].start;
1217
prim.number_of_instances = info->instance_count;
1218
prim.instance_length = vert_count;
1219
}
1220
} else {
1221
struct pipe_stream_output_target *so =
1222
indirect && indirect->count_from_stream_output ?
1223
indirect->count_from_stream_output : NULL;
1224
uint32_t vert_count = so ?
1225
v3d_stream_output_target_get_vertex_count(so) :
1226
draws[0].count;
1227
cl_emit(&job->bcl, VERTEX_ARRAY_PRIMS, prim) {
1228
prim.mode = hw_prim_type | prim_tf_enable;
1229
prim.length = vert_count;
1230
prim.index_of_first_vertex = draws[0].start;
1231
}
1232
}
1233
}
1234
1235
/* A flush is required in between a TF draw and any following TF specs
1236
* packet, or the GPU may hang. Just flush each time for now.
1237
*/
1238
if (v3d->streamout.num_targets)
1239
cl_emit(&job->bcl, TRANSFORM_FEEDBACK_FLUSH_AND_COUNT, flush);
1240
1241
job->draw_calls_queued++;
1242
if (v3d->streamout.num_targets)
1243
job->tf_draw_calls_queued++;
1244
1245
/* Increment the TF offsets by how many verts we wrote. XXX: This
1246
* needs some clamping to the buffer size.
1247
*/
1248
for (int i = 0; i < v3d->streamout.num_targets; i++)
1249
v3d->streamout.offsets[i] += draws[0].count;
1250
1251
if (v3d->zsa && job->zsbuf && v3d->zsa->base.depth_enabled) {
1252
struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);
1253
v3d_job_add_bo(job, rsc->bo);
1254
1255
job->load |= PIPE_CLEAR_DEPTH & ~job->clear;
1256
if (v3d->zsa->base.depth_writemask)
1257
job->store |= PIPE_CLEAR_DEPTH;
1258
rsc->initialized_buffers = PIPE_CLEAR_DEPTH;
1259
}
1260
1261
if (v3d->zsa && job->zsbuf && v3d->zsa->base.stencil[0].enabled) {
1262
struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);
1263
if (rsc->separate_stencil)
1264
rsc = rsc->separate_stencil;
1265
1266
v3d_job_add_bo(job, rsc->bo);
1267
1268
job->load |= PIPE_CLEAR_STENCIL & ~job->clear;
1269
if (v3d->zsa->base.stencil[0].writemask ||
1270
v3d->zsa->base.stencil[1].writemask) {
1271
job->store |= PIPE_CLEAR_STENCIL;
1272
}
1273
rsc->initialized_buffers |= PIPE_CLEAR_STENCIL;
1274
}
1275
1276
for (int i = 0; i < job->nr_cbufs; i++) {
1277
uint32_t bit = PIPE_CLEAR_COLOR0 << i;
1278
int blend_rt = v3d->blend->base.independent_blend_enable ? i : 0;
1279
1280
if (job->store & bit || !job->cbufs[i])
1281
continue;
1282
struct v3d_resource *rsc = v3d_resource(job->cbufs[i]->texture);
1283
1284
job->load |= bit & ~job->clear;
1285
if (v3d->blend->base.rt[blend_rt].colormask)
1286
job->store |= bit;
1287
v3d_job_add_bo(job, rsc->bo);
1288
}
1289
1290
if (job->referenced_size > 768 * 1024 * 1024) {
1291
perf_debug("Flushing job with %dkb to try to free up memory\n",
1292
job->referenced_size / 1024);
1293
v3d_flush(pctx);
1294
}
1295
1296
if (V3D_DEBUG & V3D_DEBUG_ALWAYS_FLUSH)
1297
v3d_flush(pctx);
1298
}
1299
1300
#if V3D_VERSION >= 41
1301
#define V3D_CSD_CFG012_WG_COUNT_SHIFT 16
1302
#define V3D_CSD_CFG012_WG_OFFSET_SHIFT 0
1303
/* Allow this dispatch to start while the last one is still running. */
1304
#define V3D_CSD_CFG3_OVERLAP_WITH_PREV (1 << 26)
1305
/* Maximum supergroup ID. 6 bits. */
1306
#define V3D_CSD_CFG3_MAX_SG_ID_SHIFT 20
1307
/* Batches per supergroup minus 1. 8 bits. */
1308
#define V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT 12
1309
/* Workgroups per supergroup, 0 means 16 */
1310
#define V3D_CSD_CFG3_WGS_PER_SG_SHIFT 8
1311
#define V3D_CSD_CFG3_WG_SIZE_SHIFT 0
1312
1313
#define V3D_CSD_CFG5_PROPAGATE_NANS (1 << 2)
1314
#define V3D_CSD_CFG5_SINGLE_SEG (1 << 1)
1315
#define V3D_CSD_CFG5_THREADING (1 << 0)
1316
1317
static void
1318
v3d_launch_grid(struct pipe_context *pctx, const struct pipe_grid_info *info)
1319
{
1320
struct v3d_context *v3d = v3d_context(pctx);
1321
struct v3d_screen *screen = v3d->screen;
1322
1323
v3d_predraw_check_stage_inputs(pctx, PIPE_SHADER_COMPUTE);
1324
1325
v3d_update_compiled_cs(v3d);
1326
1327
if (!v3d->prog.compute->resource) {
1328
static bool warned = false;
1329
if (!warned) {
1330
fprintf(stderr,
1331
"Compute shader failed to compile. "
1332
"Expect corruption.\n");
1333
warned = true;
1334
}
1335
return;
1336
}
1337
1338
/* Some of the units of scale:
1339
*
1340
* - Batches of 16 work items (shader invocations) that will be queued
1341
* to the run on a QPU at once.
1342
*
1343
* - Workgroups composed of work items based on the shader's layout
1344
* declaration.
1345
*
1346
* - Supergroups of 1-16 workgroups. There can only be 16 supergroups
1347
* running at a time on the core, so we want to keep them large to
1348
* keep the QPUs busy, but a whole supergroup will sync at a barrier
1349
* so we want to keep them small if one is present.
1350
*/
1351
struct drm_v3d_submit_csd submit = { 0 };
1352
struct v3d_job *job = v3d_job_create(v3d);
1353
1354
/* Set up the actual number of workgroups, synchronously mapping the
1355
* indirect buffer if necessary to get the dimensions.
1356
*/
1357
if (info->indirect) {
1358
struct pipe_transfer *transfer;
1359
uint32_t *map = pipe_buffer_map_range(pctx, info->indirect,
1360
info->indirect_offset,
1361
3 * sizeof(uint32_t),
1362
PIPE_MAP_READ,
1363
&transfer);
1364
memcpy(v3d->compute_num_workgroups, map, 3 * sizeof(uint32_t));
1365
pipe_buffer_unmap(pctx, transfer);
1366
1367
if (v3d->compute_num_workgroups[0] == 0 ||
1368
v3d->compute_num_workgroups[1] == 0 ||
1369
v3d->compute_num_workgroups[2] == 0) {
1370
/* Nothing to dispatch, so skip the draw (CSD can't
1371
* handle 0 workgroups).
1372
*/
1373
return;
1374
}
1375
} else {
1376
v3d->compute_num_workgroups[0] = info->grid[0];
1377
v3d->compute_num_workgroups[1] = info->grid[1];
1378
v3d->compute_num_workgroups[2] = info->grid[2];
1379
}
1380
1381
uint32_t num_wgs = 1;
1382
for (int i = 0; i < 3; i++) {
1383
num_wgs *= v3d->compute_num_workgroups[i];
1384
submit.cfg[i] |= (v3d->compute_num_workgroups[i] <<
1385
V3D_CSD_CFG012_WG_COUNT_SHIFT);
1386
}
1387
1388
uint32_t wg_size = info->block[0] * info->block[1] * info->block[2];
1389
1390
struct v3d_compute_prog_data *compute =
1391
v3d->prog.compute->prog_data.compute;
1392
uint32_t wgs_per_sg =
1393
v3d_csd_choose_workgroups_per_supergroup(
1394
&v3d->screen->devinfo,
1395
compute->has_subgroups,
1396
compute->base.has_control_barrier,
1397
compute->base.threads,
1398
num_wgs, wg_size);
1399
1400
uint32_t batches_per_sg = DIV_ROUND_UP(wgs_per_sg * wg_size, 16);
1401
uint32_t whole_sgs = num_wgs / wgs_per_sg;
1402
uint32_t rem_wgs = num_wgs - whole_sgs * wgs_per_sg;
1403
uint32_t num_batches = batches_per_sg * whole_sgs +
1404
DIV_ROUND_UP(rem_wgs * wg_size, 16);
1405
1406
submit.cfg[3] |= (wgs_per_sg & 0xf) << V3D_CSD_CFG3_WGS_PER_SG_SHIFT;
1407
submit.cfg[3] |=
1408
(batches_per_sg - 1) << V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT;
1409
submit.cfg[3] |= (wg_size & 0xff) << V3D_CSD_CFG3_WG_SIZE_SHIFT;
1410
1411
1412
/* Number of batches the dispatch will invoke (minus 1). */
1413
submit.cfg[4] = num_batches - 1;
1414
1415
/* Make sure we didn't accidentally underflow. */
1416
assert(submit.cfg[4] != ~0);
1417
1418
v3d_job_add_bo(job, v3d_resource(v3d->prog.compute->resource)->bo);
1419
submit.cfg[5] = (v3d_resource(v3d->prog.compute->resource)->bo->offset +
1420
v3d->prog.compute->offset);
1421
submit.cfg[5] |= V3D_CSD_CFG5_PROPAGATE_NANS;
1422
if (v3d->prog.compute->prog_data.base->single_seg)
1423
submit.cfg[5] |= V3D_CSD_CFG5_SINGLE_SEG;
1424
if (v3d->prog.compute->prog_data.base->threads == 4)
1425
submit.cfg[5] |= V3D_CSD_CFG5_THREADING;
1426
1427
if (v3d->prog.compute->prog_data.compute->shared_size) {
1428
v3d->compute_shared_memory =
1429
v3d_bo_alloc(v3d->screen,
1430
v3d->prog.compute->prog_data.compute->shared_size *
1431
wgs_per_sg,
1432
"shared_vars");
1433
}
1434
1435
struct v3d_cl_reloc uniforms = v3d_write_uniforms(v3d, job,
1436
v3d->prog.compute,
1437
PIPE_SHADER_COMPUTE);
1438
v3d_job_add_bo(job, uniforms.bo);
1439
submit.cfg[6] = uniforms.bo->offset + uniforms.offset;
1440
1441
/* Pull some job state that was stored in a SUBMIT_CL struct out to
1442
* our SUBMIT_CSD struct
1443
*/
1444
submit.bo_handles = job->submit.bo_handles;
1445
submit.bo_handle_count = job->submit.bo_handle_count;
1446
1447
/* Serialize this in the rest of our command stream. */
1448
submit.in_sync = v3d->out_sync;
1449
submit.out_sync = v3d->out_sync;
1450
1451
if (!(V3D_DEBUG & V3D_DEBUG_NORAST)) {
1452
int ret = v3d_ioctl(screen->fd, DRM_IOCTL_V3D_SUBMIT_CSD,
1453
&submit);
1454
static bool warned = false;
1455
if (ret && !warned) {
1456
fprintf(stderr, "CSD submit call returned %s. "
1457
"Expect corruption.\n", strerror(errno));
1458
warned = true;
1459
}
1460
}
1461
1462
v3d_job_free(v3d, job);
1463
1464
/* Mark SSBOs as being written.. we don't actually know which ones are
1465
* read vs written, so just assume the worst
1466
*/
1467
u_foreach_bit(i, v3d->ssbo[PIPE_SHADER_COMPUTE].enabled_mask) {
1468
struct v3d_resource *rsc = v3d_resource(
1469
v3d->ssbo[PIPE_SHADER_COMPUTE].sb[i].buffer);
1470
rsc->writes++;
1471
rsc->compute_written = true;
1472
}
1473
1474
u_foreach_bit(i, v3d->shaderimg[PIPE_SHADER_COMPUTE].enabled_mask) {
1475
struct v3d_resource *rsc = v3d_resource(
1476
v3d->shaderimg[PIPE_SHADER_COMPUTE].si[i].base.resource);
1477
rsc->writes++;
1478
rsc->compute_written = true;
1479
}
1480
1481
v3d_bo_unreference(&uniforms.bo);
1482
v3d_bo_unreference(&v3d->compute_shared_memory);
1483
}
1484
#endif
1485
1486
/**
1487
* Implements gallium's clear() hook (glClear()) by drawing a pair of triangles.
1488
*/
1489
static void
1490
v3d_draw_clear(struct v3d_context *v3d,
1491
unsigned buffers,
1492
const union pipe_color_union *color,
1493
double depth, unsigned stencil)
1494
{
1495
static const union pipe_color_union dummy_color = {};
1496
1497
/* The blitter util dereferences the color regardless, even though the
1498
* gallium clear API may not pass one in when only Z/S are cleared.
1499
*/
1500
if (!color)
1501
color = &dummy_color;
1502
1503
v3d_blitter_save(v3d);
1504
util_blitter_clear(v3d->blitter,
1505
v3d->framebuffer.width,
1506
v3d->framebuffer.height,
1507
util_framebuffer_get_num_layers(&v3d->framebuffer),
1508
buffers, color, depth, stencil,
1509
util_framebuffer_get_num_samples(&v3d->framebuffer) > 1);
1510
}
1511
1512
/**
1513
* Attempts to perform the GL clear by using the TLB's fast clear at the start
1514
* of the frame.
1515
*/
1516
static unsigned
1517
v3d_tlb_clear(struct v3d_job *job, unsigned buffers,
1518
const union pipe_color_union *color,
1519
double depth, unsigned stencil)
1520
{
1521
struct v3d_context *v3d = job->v3d;
1522
1523
if (job->draw_calls_queued) {
1524
/* If anything in the CL has drawn using the buffer, then the
1525
* TLB clear we're trying to add now would happen before that
1526
* drawing.
1527
*/
1528
buffers &= ~(job->load | job->store);
1529
}
1530
1531
/* GFXH-1461: If we were to emit a load of just depth or just stencil,
1532
* then the clear for the other may get lost. We need to decide now
1533
* if it would be possible to need to emit a load of just one after
1534
* we've set up our TLB clears.
1535
*/
1536
if (buffers & PIPE_CLEAR_DEPTHSTENCIL &&
1537
(buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL &&
1538
job->zsbuf &&
1539
util_format_is_depth_and_stencil(job->zsbuf->texture->format)) {
1540
buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
1541
}
1542
1543
for (int i = 0; i < job->nr_cbufs; i++) {
1544
uint32_t bit = PIPE_CLEAR_COLOR0 << i;
1545
if (!(buffers & bit))
1546
continue;
1547
1548
struct pipe_surface *psurf = v3d->framebuffer.cbufs[i];
1549
struct v3d_surface *surf = v3d_surface(psurf);
1550
struct v3d_resource *rsc = v3d_resource(psurf->texture);
1551
1552
union util_color uc;
1553
uint32_t internal_size = 4 << surf->internal_bpp;
1554
1555
static union pipe_color_union swapped_color;
1556
if (v3d->swap_color_rb & (1 << i)) {
1557
swapped_color.f[0] = color->f[2];
1558
swapped_color.f[1] = color->f[1];
1559
swapped_color.f[2] = color->f[0];
1560
swapped_color.f[3] = color->f[3];
1561
color = &swapped_color;
1562
}
1563
1564
switch (surf->internal_type) {
1565
case V3D_INTERNAL_TYPE_8:
1566
util_pack_color(color->f, PIPE_FORMAT_R8G8B8A8_UNORM,
1567
&uc);
1568
memcpy(job->clear_color[i], uc.ui, internal_size);
1569
break;
1570
case V3D_INTERNAL_TYPE_8I:
1571
case V3D_INTERNAL_TYPE_8UI:
1572
job->clear_color[i][0] = ((color->ui[0] & 0xff) |
1573
(color->ui[1] & 0xff) << 8 |
1574
(color->ui[2] & 0xff) << 16 |
1575
(color->ui[3] & 0xff) << 24);
1576
break;
1577
case V3D_INTERNAL_TYPE_16F:
1578
util_pack_color(color->f, PIPE_FORMAT_R16G16B16A16_FLOAT,
1579
&uc);
1580
memcpy(job->clear_color[i], uc.ui, internal_size);
1581
break;
1582
case V3D_INTERNAL_TYPE_16I:
1583
case V3D_INTERNAL_TYPE_16UI:
1584
job->clear_color[i][0] = ((color->ui[0] & 0xffff) |
1585
color->ui[1] << 16);
1586
job->clear_color[i][1] = ((color->ui[2] & 0xffff) |
1587
color->ui[3] << 16);
1588
break;
1589
case V3D_INTERNAL_TYPE_32F:
1590
case V3D_INTERNAL_TYPE_32I:
1591
case V3D_INTERNAL_TYPE_32UI:
1592
memcpy(job->clear_color[i], color->ui, internal_size);
1593
break;
1594
}
1595
1596
rsc->initialized_buffers |= bit;
1597
}
1598
1599
unsigned zsclear = buffers & PIPE_CLEAR_DEPTHSTENCIL;
1600
if (zsclear) {
1601
struct v3d_resource *rsc =
1602
v3d_resource(v3d->framebuffer.zsbuf->texture);
1603
1604
if (zsclear & PIPE_CLEAR_DEPTH)
1605
job->clear_z = depth;
1606
if (zsclear & PIPE_CLEAR_STENCIL)
1607
job->clear_s = stencil;
1608
1609
rsc->initialized_buffers |= zsclear;
1610
}
1611
1612
job->draw_min_x = 0;
1613
job->draw_min_y = 0;
1614
job->draw_max_x = v3d->framebuffer.width;
1615
job->draw_max_y = v3d->framebuffer.height;
1616
job->clear |= buffers;
1617
job->store |= buffers;
1618
job->scissor.disabled = true;
1619
1620
v3d_start_draw(v3d);
1621
1622
return buffers;
1623
}
1624
1625
static void
1626
v3d_clear(struct pipe_context *pctx, unsigned buffers, const struct pipe_scissor_state *scissor_state,
1627
const union pipe_color_union *color, double depth, unsigned stencil)
1628
{
1629
struct v3d_context *v3d = v3d_context(pctx);
1630
struct v3d_job *job = v3d_get_job_for_fbo(v3d);
1631
1632
buffers &= ~v3d_tlb_clear(job, buffers, color, depth, stencil);
1633
1634
if (buffers)
1635
v3d_draw_clear(v3d, buffers, color, depth, stencil);
1636
}
1637
1638
static void
1639
v3d_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,
1640
const union pipe_color_union *color,
1641
unsigned x, unsigned y, unsigned w, unsigned h,
1642
bool render_condition_enabled)
1643
{
1644
fprintf(stderr, "unimpl: clear RT\n");
1645
}
1646
1647
static void
1648
v3d_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,
1649
unsigned buffers, double depth, unsigned stencil,
1650
unsigned x, unsigned y, unsigned w, unsigned h,
1651
bool render_condition_enabled)
1652
{
1653
fprintf(stderr, "unimpl: clear DS\n");
1654
}
1655
1656
void
1657
v3dX(start_binning)(struct v3d_context *v3d, struct v3d_job *job)
1658
{
1659
v3d_start_binning(v3d, job);
1660
}
1661
1662
void
1663
v3dX(draw_init)(struct pipe_context *pctx)
1664
{
1665
pctx->draw_vbo = v3d_draw_vbo;
1666
pctx->clear = v3d_clear;
1667
pctx->clear_render_target = v3d_clear_render_target;
1668
pctx->clear_depth_stencil = v3d_clear_depth_stencil;
1669
#if V3D_VERSION >= 41
1670
if (v3d_context(pctx)->screen->has_csd)
1671
pctx->launch_grid = v3d_launch_grid;
1672
#endif
1673
}
1674
1675