Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/zink/zink_draw.c
4570 views
1
#include "zink_compiler.h"
2
#include "zink_context.h"
3
#include "zink_program.h"
4
#include "zink_query.h"
5
#include "zink_resource.h"
6
#include "zink_screen.h"
7
#include "zink_state.h"
8
#include "zink_surface.h"
9
10
#include "tgsi/tgsi_from_mesa.h"
11
#include "util/hash_table.h"
12
#include "util/u_debug.h"
13
#include "util/u_helpers.h"
14
#include "util/u_inlines.h"
15
#include "util/u_prim.h"
16
#include "util/u_prim_restart.h"
17
18
19
static void
20
zink_emit_xfb_counter_barrier(struct zink_context *ctx)
21
{
22
/* Between the pause and resume there needs to be a memory barrier for the counter buffers
23
* with a source access of VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_WRITE_BIT_EXT
24
* at pipeline stage VK_PIPELINE_STAGE_TRANSFORM_FEEDBACK_BIT_EXT
25
* to a destination access of VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_READ_BIT_EXT
26
* at pipeline stage VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT.
27
*
28
* - from VK_EXT_transform_feedback spec
29
*/
30
for (unsigned i = 0; i < ctx->num_so_targets; i++) {
31
struct zink_so_target *t = zink_so_target(ctx->so_targets[i]);
32
if (!t)
33
continue;
34
struct zink_resource *res = zink_resource(t->counter_buffer);
35
if (t->counter_buffer_valid)
36
zink_resource_buffer_barrier(ctx, NULL, res, VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_READ_BIT_EXT,
37
VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT);
38
else
39
zink_resource_buffer_barrier(ctx, NULL, res, VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_WRITE_BIT_EXT,
40
VK_PIPELINE_STAGE_TRANSFORM_FEEDBACK_BIT_EXT);
41
}
42
ctx->xfb_barrier = false;
43
}
44
45
static void
46
zink_emit_xfb_vertex_input_barrier(struct zink_context *ctx, struct zink_resource *res)
47
{
48
/* A pipeline barrier is required between using the buffers as
49
* transform feedback buffers and vertex buffers to
50
* ensure all writes to the transform feedback buffers are visible
51
* when the data is read as vertex attributes.
52
* The source access is VK_ACCESS_TRANSFORM_FEEDBACK_WRITE_BIT_EXT
53
* and the destination access is VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT
54
* for the pipeline stages VK_PIPELINE_STAGE_TRANSFORM_FEEDBACK_BIT_EXT
55
* and VK_PIPELINE_STAGE_VERTEX_INPUT_BIT respectively.
56
*
57
* - 20.3.1. Drawing Transform Feedback
58
*/
59
zink_resource_buffer_barrier(ctx, NULL, res, VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT,
60
VK_PIPELINE_STAGE_VERTEX_INPUT_BIT);
61
}
62
63
static void
64
zink_emit_stream_output_targets(struct pipe_context *pctx)
65
{
66
struct zink_context *ctx = zink_context(pctx);
67
struct zink_screen *screen = zink_screen(pctx->screen);
68
struct zink_batch *batch = &ctx->batch;
69
VkBuffer buffers[PIPE_MAX_SO_OUTPUTS] = {0};
70
VkDeviceSize buffer_offsets[PIPE_MAX_SO_OUTPUTS] = {0};
71
VkDeviceSize buffer_sizes[PIPE_MAX_SO_OUTPUTS] = {0};
72
73
for (unsigned i = 0; i < ctx->num_so_targets; i++) {
74
struct zink_so_target *t = (struct zink_so_target *)ctx->so_targets[i];
75
if (!t) {
76
/* no need to reference this or anything */
77
buffers[i] = zink_resource(ctx->dummy_xfb_buffer)->obj->buffer;
78
buffer_offsets[i] = 0;
79
buffer_sizes[i] = sizeof(uint8_t);
80
continue;
81
}
82
struct zink_resource *res = zink_resource(t->base.buffer);
83
if (!(res->bind_history & ZINK_RESOURCE_USAGE_STREAMOUT))
84
/* resource has been rebound */
85
t->counter_buffer_valid = false;
86
buffers[i] = res->obj->buffer;
87
zink_resource_buffer_barrier(ctx, NULL, zink_resource(t->base.buffer),
88
VK_ACCESS_TRANSFORM_FEEDBACK_WRITE_BIT_EXT, VK_PIPELINE_STAGE_TRANSFORM_FEEDBACK_BIT_EXT);
89
zink_batch_reference_resource_rw(batch, res, true);
90
buffer_offsets[i] = t->base.buffer_offset;
91
buffer_sizes[i] = t->base.buffer_size;
92
res->bind_history |= ZINK_RESOURCE_USAGE_STREAMOUT;
93
util_range_add(t->base.buffer, &res->valid_buffer_range, t->base.buffer_offset,
94
t->base.buffer_offset + t->base.buffer_size);
95
}
96
97
screen->vk.CmdBindTransformFeedbackBuffersEXT(batch->state->cmdbuf, 0, ctx->num_so_targets,
98
buffers, buffer_offsets,
99
buffer_sizes);
100
ctx->dirty_so_targets = false;
101
}
102
103
ALWAYS_INLINE static void
104
check_buffer_barrier(struct zink_context *ctx, struct pipe_resource *pres, VkAccessFlags flags, VkPipelineStageFlags pipeline)
105
{
106
struct zink_resource *res = zink_resource(pres);
107
zink_resource_buffer_barrier(ctx, NULL, res, flags, pipeline);
108
}
109
110
ALWAYS_INLINE static void
111
barrier_draw_buffers(struct zink_context *ctx, const struct pipe_draw_info *dinfo,
112
const struct pipe_draw_indirect_info *dindirect, struct pipe_resource *index_buffer)
113
{
114
if (index_buffer)
115
check_buffer_barrier(ctx, index_buffer, VK_ACCESS_INDEX_READ_BIT, VK_PIPELINE_STAGE_VERTEX_INPUT_BIT);
116
if (dindirect && dindirect->buffer) {
117
check_buffer_barrier(ctx, dindirect->buffer,
118
VK_ACCESS_INDIRECT_COMMAND_READ_BIT, VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT);
119
if (dindirect->indirect_draw_count)
120
check_buffer_barrier(ctx, dindirect->indirect_draw_count,
121
VK_ACCESS_INDIRECT_COMMAND_READ_BIT, VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT);
122
}
123
}
124
125
static void
126
zink_bind_vertex_buffers(struct zink_batch *batch, struct zink_context *ctx)
127
{
128
VkBuffer buffers[PIPE_MAX_ATTRIBS];
129
VkDeviceSize buffer_offsets[PIPE_MAX_ATTRIBS];
130
VkDeviceSize buffer_strides[PIPE_MAX_ATTRIBS];
131
const struct zink_vertex_elements_state *elems = ctx->element_state;
132
struct zink_screen *screen = zink_screen(ctx->base.screen);
133
134
if (!elems->hw_state.num_bindings)
135
return;
136
137
for (unsigned i = 0; i < elems->hw_state.num_bindings; i++) {
138
struct pipe_vertex_buffer *vb = ctx->vertex_buffers + ctx->element_state->binding_map[i];
139
assert(vb);
140
if (vb->buffer.resource) {
141
struct zink_resource *res = zink_resource(vb->buffer.resource);
142
buffers[i] = res->obj->buffer;
143
buffer_offsets[i] = vb->buffer_offset;
144
buffer_strides[i] = vb->stride;
145
} else {
146
buffers[i] = zink_resource(ctx->dummy_vertex_buffer)->obj->buffer;
147
buffer_offsets[i] = 0;
148
buffer_strides[i] = 0;
149
}
150
}
151
152
if (screen->info.have_EXT_extended_dynamic_state)
153
screen->vk.CmdBindVertexBuffers2EXT(batch->state->cmdbuf, 0,
154
elems->hw_state.num_bindings,
155
buffers, buffer_offsets, NULL, buffer_strides);
156
else
157
vkCmdBindVertexBuffers(batch->state->cmdbuf, 0,
158
elems->hw_state.num_bindings,
159
buffers, buffer_offsets);
160
ctx->vertex_buffers_dirty = false;
161
}
162
163
static void
164
update_compute_program(struct zink_context *ctx)
165
{
166
unsigned bits = 1 << PIPE_SHADER_COMPUTE;
167
if (ctx->dirty_shader_stages & bits) {
168
struct zink_compute_program *comp = NULL;
169
struct hash_entry *entry = _mesa_hash_table_search(ctx->compute_program_cache,
170
ctx->compute_stage);
171
if (!entry) {
172
comp = zink_create_compute_program(ctx, ctx->compute_stage);
173
entry = _mesa_hash_table_insert(ctx->compute_program_cache, comp->shader, comp);
174
}
175
comp = (struct zink_compute_program*)(entry ? entry->data : NULL);
176
if (comp && comp != ctx->curr_compute) {
177
ctx->compute_pipeline_state.dirty = true;
178
zink_batch_reference_program(&ctx->batch, &comp->base);
179
}
180
ctx->curr_compute = comp;
181
ctx->dirty_shader_stages &= bits;
182
}
183
}
184
185
static void
186
update_gfx_program(struct zink_context *ctx)
187
{
188
if (ctx->last_vertex_stage_dirty) {
189
if (ctx->gfx_stages[PIPE_SHADER_GEOMETRY])
190
ctx->dirty_shader_stages |= BITFIELD_BIT(PIPE_SHADER_GEOMETRY);
191
else if (ctx->gfx_stages[PIPE_SHADER_TESS_EVAL])
192
ctx->dirty_shader_stages |= BITFIELD_BIT(PIPE_SHADER_TESS_EVAL);
193
else
194
ctx->dirty_shader_stages |= BITFIELD_BIT(PIPE_SHADER_VERTEX);
195
ctx->last_vertex_stage_dirty = false;
196
}
197
unsigned bits = u_bit_consecutive(PIPE_SHADER_VERTEX, 5);
198
if (ctx->dirty_shader_stages & bits) {
199
struct zink_gfx_program *prog = NULL;
200
struct hash_entry *entry = _mesa_hash_table_search(ctx->program_cache,
201
ctx->gfx_stages);
202
if (entry)
203
zink_update_gfx_program(ctx, (struct zink_gfx_program*)entry->data);
204
else {
205
prog = zink_create_gfx_program(ctx, ctx->gfx_stages);
206
entry = _mesa_hash_table_insert(ctx->program_cache, prog->shaders, prog);
207
}
208
prog = (struct zink_gfx_program*)(entry ? entry->data : NULL);
209
if (prog && prog != ctx->curr_program) {
210
ctx->gfx_pipeline_state.combined_dirty = true;
211
zink_batch_reference_program(&ctx->batch, &prog->base);
212
}
213
ctx->curr_program = prog;
214
ctx->dirty_shader_stages &= ~bits;
215
}
216
}
217
218
static bool
219
line_width_needed(enum pipe_prim_type reduced_prim,
220
VkPolygonMode polygon_mode)
221
{
222
switch (reduced_prim) {
223
case PIPE_PRIM_POINTS:
224
return false;
225
226
case PIPE_PRIM_LINES:
227
return true;
228
229
case PIPE_PRIM_TRIANGLES:
230
return polygon_mode == VK_POLYGON_MODE_LINE;
231
232
default:
233
unreachable("unexpected reduced prim");
234
}
235
}
236
237
ALWAYS_INLINE static void
238
update_drawid(struct zink_context *ctx, unsigned draw_id)
239
{
240
vkCmdPushConstants(ctx->batch.state->cmdbuf, ctx->curr_program->base.layout, VK_SHADER_STAGE_VERTEX_BIT,
241
offsetof(struct zink_gfx_push_constant, draw_id), sizeof(unsigned),
242
&draw_id);
243
}
244
245
ALWAYS_INLINE static void
246
draw_indexed_need_index_buffer_unref(struct zink_context *ctx,
247
const struct pipe_draw_info *dinfo,
248
const struct pipe_draw_start_count_bias *draws,
249
unsigned num_draws,
250
unsigned draw_id,
251
bool needs_drawid)
252
{
253
VkCommandBuffer cmdbuf = ctx->batch.state->cmdbuf;
254
if (dinfo->increment_draw_id && needs_drawid) {
255
for (unsigned i = 0; i < num_draws; i++) {
256
update_drawid(ctx, draw_id);
257
vkCmdDrawIndexed(cmdbuf,
258
draws[i].count, dinfo->instance_count,
259
0, draws[i].index_bias, dinfo->start_instance);
260
draw_id++;
261
}
262
} else {
263
if (needs_drawid)
264
update_drawid(ctx, draw_id);
265
for (unsigned i = 0; i < num_draws; i++)
266
vkCmdDrawIndexed(cmdbuf,
267
draws[i].count, dinfo->instance_count,
268
0, draws[i].index_bias, dinfo->start_instance);
269
270
}
271
}
272
273
ALWAYS_INLINE static void
274
draw_indexed(struct zink_context *ctx,
275
const struct pipe_draw_info *dinfo,
276
const struct pipe_draw_start_count_bias *draws,
277
unsigned num_draws,
278
unsigned draw_id,
279
bool needs_drawid)
280
{
281
VkCommandBuffer cmdbuf = ctx->batch.state->cmdbuf;
282
if (dinfo->increment_draw_id && needs_drawid) {
283
for (unsigned i = 0; i < num_draws; i++) {
284
update_drawid(ctx, draw_id);
285
vkCmdDrawIndexed(cmdbuf,
286
draws[i].count, dinfo->instance_count,
287
draws[i].start, draws[i].index_bias, dinfo->start_instance);
288
draw_id++;
289
}
290
} else {
291
if (needs_drawid)
292
update_drawid(ctx, draw_id);
293
if (zink_screen(ctx->base.screen)->info.have_EXT_multi_draw)
294
zink_screen(ctx->base.screen)->vk.CmdDrawMultiIndexedEXT(cmdbuf, num_draws, (const VkMultiDrawIndexedInfoEXT*)draws,
295
dinfo->instance_count,
296
dinfo->start_instance, sizeof(struct pipe_draw_start_count_bias),
297
dinfo->index_bias_varies ? NULL : &draws[0].index_bias);
298
else {
299
for (unsigned i = 0; i < num_draws; i++)
300
vkCmdDrawIndexed(cmdbuf,
301
draws[i].count, dinfo->instance_count,
302
draws[i].start, draws[i].index_bias, dinfo->start_instance);
303
}
304
}
305
}
306
307
ALWAYS_INLINE static void
308
draw(struct zink_context *ctx,
309
const struct pipe_draw_info *dinfo,
310
const struct pipe_draw_start_count_bias *draws,
311
unsigned num_draws,
312
unsigned draw_id,
313
bool needs_drawid)
314
{
315
VkCommandBuffer cmdbuf = ctx->batch.state->cmdbuf;
316
if (dinfo->increment_draw_id && needs_drawid) {
317
for (unsigned i = 0; i < num_draws; i++) {
318
update_drawid(ctx, draw_id);
319
vkCmdDraw(cmdbuf, draws[i].count, dinfo->instance_count, draws[i].start, dinfo->start_instance);
320
draw_id++;
321
}
322
} else {
323
if (needs_drawid)
324
update_drawid(ctx, draw_id);
325
if (zink_screen(ctx->base.screen)->info.have_EXT_multi_draw)
326
zink_screen(ctx->base.screen)->vk.CmdDrawMultiEXT(cmdbuf, num_draws, (const VkMultiDrawInfoEXT*)draws,
327
dinfo->instance_count, dinfo->start_instance,
328
sizeof(struct pipe_draw_start_count_bias));
329
else {
330
for (unsigned i = 0; i < num_draws; i++)
331
vkCmdDraw(cmdbuf, draws[i].count, dinfo->instance_count, draws[i].start, dinfo->start_instance);
332
333
}
334
}
335
}
336
337
static void
338
update_barriers(struct zink_context *ctx, bool is_compute)
339
{
340
if (!ctx->need_barriers[is_compute]->entries)
341
return;
342
struct set *need_barriers = ctx->need_barriers[is_compute];
343
ctx->barrier_set_idx[is_compute] = !ctx->barrier_set_idx[is_compute];
344
ctx->need_barriers[is_compute] = &ctx->update_barriers[is_compute][ctx->barrier_set_idx[is_compute]];
345
set_foreach(need_barriers, he) {
346
struct zink_resource *res = (struct zink_resource *)he->key;
347
VkPipelineStageFlags pipeline = 0;
348
VkAccessFlags access = 0;
349
if (res->bind_count[is_compute]) {
350
if (res->write_bind_count[is_compute])
351
access |= VK_ACCESS_SHADER_WRITE_BIT;
352
if (res->write_bind_count[is_compute] != res->bind_count[is_compute]) {
353
unsigned bind_count = res->bind_count[is_compute] - res->write_bind_count[is_compute];
354
if (res->obj->is_buffer) {
355
if (res->ubo_bind_count[is_compute]) {
356
access |= VK_ACCESS_UNIFORM_READ_BIT;
357
bind_count -= res->ubo_bind_count[is_compute];
358
}
359
if (!is_compute && res->vbo_bind_count) {
360
access |= VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
361
pipeline |= VK_PIPELINE_STAGE_VERTEX_INPUT_BIT;
362
bind_count -= res->vbo_bind_count;
363
}
364
}
365
if (bind_count)
366
access |= VK_ACCESS_SHADER_READ_BIT;
367
}
368
if (is_compute)
369
pipeline = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
370
else {
371
u_foreach_bit(stage, res->bind_history) {
372
if ((1 << stage) != ZINK_RESOURCE_USAGE_STREAMOUT)
373
pipeline |= zink_pipeline_flags_from_pipe_stage((enum pipe_shader_type)stage);
374
}
375
}
376
if (res->base.b.target == PIPE_BUFFER)
377
zink_resource_buffer_barrier(ctx, NULL, res, access, pipeline);
378
else {
379
VkImageLayout layout = zink_descriptor_util_image_layout_eval(res, is_compute);
380
if (layout != res->layout)
381
zink_resource_image_barrier(ctx, NULL, res, layout, access, pipeline);
382
}
383
/* always barrier on draw if this resource has either multiple image write binds or
384
* image write binds and image read binds
385
*/
386
if (res->write_bind_count[is_compute] && res->bind_count[is_compute] > 1)
387
_mesa_set_add_pre_hashed(ctx->need_barriers[is_compute], he->hash, res);
388
}
389
_mesa_set_remove(need_barriers, he);
390
if (!need_barriers->entries)
391
break;
392
}
393
}
394
395
void
396
zink_draw_vbo(struct pipe_context *pctx,
397
const struct pipe_draw_info *dinfo,
398
unsigned drawid_offset,
399
const struct pipe_draw_indirect_info *dindirect,
400
const struct pipe_draw_start_count_bias *draws,
401
unsigned num_draws)
402
{
403
struct zink_context *ctx = zink_context(pctx);
404
struct zink_screen *screen = zink_screen(pctx->screen);
405
struct zink_rasterizer_state *rast_state = ctx->rast_state;
406
struct zink_depth_stencil_alpha_state *dsa_state = ctx->dsa_state;
407
struct zink_batch *batch = &ctx->batch;
408
struct zink_so_target *so_target =
409
dindirect && dindirect->count_from_stream_output ?
410
zink_so_target(dindirect->count_from_stream_output) : NULL;
411
VkBuffer counter_buffers[PIPE_MAX_SO_OUTPUTS];
412
VkDeviceSize counter_buffer_offsets[PIPE_MAX_SO_OUTPUTS];
413
bool need_index_buffer_unref = false;
414
bool mode_changed = ctx->gfx_pipeline_state.mode != dinfo->mode;
415
416
update_barriers(ctx, false);
417
418
if (ctx->gfx_pipeline_state.vertices_per_patch != dinfo->vertices_per_patch)
419
ctx->gfx_pipeline_state.dirty = true;
420
bool drawid_broken = ctx->drawid_broken;
421
ctx->drawid_broken = false;
422
if (!dindirect || !dindirect->buffer)
423
ctx->drawid_broken = BITSET_TEST(ctx->gfx_stages[PIPE_SHADER_VERTEX]->nir->info.system_values_read, SYSTEM_VALUE_DRAW_ID) &&
424
(drawid_offset != 0 ||
425
(!screen->info.have_EXT_multi_draw && num_draws > 1) ||
426
(screen->info.have_EXT_multi_draw && num_draws > 1 && !dinfo->increment_draw_id));
427
if (drawid_broken != ctx->drawid_broken)
428
ctx->dirty_shader_stages |= BITFIELD_BIT(PIPE_SHADER_VERTEX);
429
ctx->gfx_pipeline_state.vertices_per_patch = dinfo->vertices_per_patch;
430
if (ctx->rast_state->base.point_quad_rasterization &&
431
ctx->gfx_prim_mode != dinfo->mode) {
432
if (ctx->gfx_prim_mode == PIPE_PRIM_POINTS || dinfo->mode == PIPE_PRIM_POINTS)
433
ctx->dirty_shader_stages |= BITFIELD_BIT(PIPE_SHADER_FRAGMENT);
434
}
435
ctx->gfx_prim_mode = dinfo->mode;
436
update_gfx_program(ctx);
437
438
if (zink_program_has_descriptors(&ctx->curr_program->base))
439
screen->descriptors_update(ctx, false);
440
441
if (ctx->gfx_pipeline_state.primitive_restart != dinfo->primitive_restart)
442
ctx->gfx_pipeline_state.dirty = true;
443
ctx->gfx_pipeline_state.primitive_restart = dinfo->primitive_restart;
444
445
unsigned index_offset = 0;
446
unsigned index_size = dinfo->index_size;
447
struct pipe_resource *index_buffer = NULL;
448
if (index_size > 0) {
449
if (dinfo->has_user_indices) {
450
if (!util_upload_index_buffer(pctx, dinfo, &draws[0], &index_buffer, &index_offset, 4)) {
451
debug_printf("util_upload_index_buffer() failed\n");
452
return;
453
}
454
zink_batch_reference_resource_move(batch, zink_resource(index_buffer));
455
} else {
456
index_buffer = dinfo->index.resource;
457
zink_batch_reference_resource_rw(batch, zink_resource(index_buffer), false);
458
}
459
assert(index_size <= 4 && index_size != 3);
460
assert(index_size != 1 || screen->info.have_EXT_index_type_uint8);
461
const VkIndexType index_type[3] = {
462
VK_INDEX_TYPE_UINT8_EXT,
463
VK_INDEX_TYPE_UINT16,
464
VK_INDEX_TYPE_UINT32,
465
};
466
struct zink_resource *res = zink_resource(index_buffer);
467
vkCmdBindIndexBuffer(batch->state->cmdbuf, res->obj->buffer, index_offset, index_type[index_size >> 1]);
468
}
469
470
bool have_streamout = !!ctx->num_so_targets;
471
if (have_streamout) {
472
if (ctx->xfb_barrier)
473
zink_emit_xfb_counter_barrier(ctx);
474
if (ctx->dirty_so_targets)
475
zink_emit_stream_output_targets(pctx);
476
}
477
478
if (so_target)
479
zink_emit_xfb_vertex_input_barrier(ctx, zink_resource(so_target->base.buffer));
480
481
barrier_draw_buffers(ctx, dinfo, dindirect, index_buffer);
482
483
if (ctx->descriptor_refs_dirty[0])
484
zink_update_descriptor_refs(ctx, false);
485
486
batch = zink_batch_rp(ctx);
487
488
VkPipeline prev_pipeline = ctx->gfx_pipeline_state.pipeline;
489
VkPipeline pipeline = zink_get_gfx_pipeline(ctx, ctx->curr_program,
490
&ctx->gfx_pipeline_state,
491
dinfo->mode);
492
bool pipeline_changed = prev_pipeline != pipeline || ctx->pipeline_changed[0];
493
if (pipeline_changed)
494
vkCmdBindPipeline(batch->state->cmdbuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
495
496
if (ctx->vp_state_changed || pipeline_changed) {
497
VkViewport viewports[PIPE_MAX_VIEWPORTS];
498
for (unsigned i = 0; i < ctx->vp_state.num_viewports; i++) {
499
VkViewport viewport = {
500
ctx->vp_state.viewport_states[i].translate[0] - ctx->vp_state.viewport_states[i].scale[0],
501
ctx->vp_state.viewport_states[i].translate[1] - ctx->vp_state.viewport_states[i].scale[1],
502
ctx->vp_state.viewport_states[i].scale[0] * 2,
503
ctx->vp_state.viewport_states[i].scale[1] * 2,
504
ctx->rast_state->base.clip_halfz ?
505
ctx->vp_state.viewport_states[i].translate[2] :
506
ctx->vp_state.viewport_states[i].translate[2] - ctx->vp_state.viewport_states[i].scale[2],
507
ctx->vp_state.viewport_states[i].translate[2] + ctx->vp_state.viewport_states[i].scale[2]
508
};
509
viewports[i] = viewport;
510
}
511
if (screen->info.have_EXT_extended_dynamic_state)
512
screen->vk.CmdSetViewportWithCountEXT(batch->state->cmdbuf, ctx->vp_state.num_viewports, viewports);
513
else
514
vkCmdSetViewport(batch->state->cmdbuf, 0, ctx->vp_state.num_viewports, viewports);
515
}
516
if (ctx->scissor_changed || ctx->vp_state_changed || pipeline_changed) {
517
VkRect2D scissors[PIPE_MAX_VIEWPORTS];
518
if (ctx->rast_state->base.scissor) {
519
for (unsigned i = 0; i < ctx->vp_state.num_viewports; i++) {
520
scissors[i].offset.x = ctx->vp_state.scissor_states[i].minx;
521
scissors[i].offset.y = ctx->vp_state.scissor_states[i].miny;
522
scissors[i].extent.width = ctx->vp_state.scissor_states[i].maxx - ctx->vp_state.scissor_states[i].minx;
523
scissors[i].extent.height = ctx->vp_state.scissor_states[i].maxy - ctx->vp_state.scissor_states[i].miny;
524
}
525
} else {
526
for (unsigned i = 0; i < ctx->vp_state.num_viewports; i++) {
527
scissors[i].offset.x = 0;
528
scissors[i].offset.y = 0;
529
scissors[i].extent.width = ctx->fb_state.width;
530
scissors[i].extent.height = ctx->fb_state.height;
531
}
532
}
533
if (screen->info.have_EXT_extended_dynamic_state)
534
screen->vk.CmdSetScissorWithCountEXT(batch->state->cmdbuf, ctx->vp_state.num_viewports, scissors);
535
else
536
vkCmdSetScissor(batch->state->cmdbuf, 0, ctx->vp_state.num_viewports, scissors);
537
}
538
ctx->vp_state_changed = false;
539
ctx->scissor_changed = false;
540
541
if (ctx->stencil_ref_changed) {
542
vkCmdSetStencilReference(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_BIT,
543
ctx->stencil_ref.ref_value[0]);
544
vkCmdSetStencilReference(batch->state->cmdbuf, VK_STENCIL_FACE_BACK_BIT,
545
ctx->stencil_ref.ref_value[1]);
546
ctx->stencil_ref_changed = false;
547
}
548
549
if (pipeline_changed || ctx->dsa_state_changed) {
550
if (screen->info.have_EXT_extended_dynamic_state) {
551
screen->vk.CmdSetDepthBoundsTestEnableEXT(batch->state->cmdbuf, dsa_state->hw_state.depth_bounds_test);
552
if (dsa_state->hw_state.depth_bounds_test)
553
vkCmdSetDepthBounds(batch->state->cmdbuf,
554
dsa_state->hw_state.min_depth_bounds,
555
dsa_state->hw_state.max_depth_bounds);
556
screen->vk.CmdSetDepthTestEnableEXT(batch->state->cmdbuf, dsa_state->hw_state.depth_test);
557
if (dsa_state->hw_state.depth_test)
558
screen->vk.CmdSetDepthCompareOpEXT(batch->state->cmdbuf, dsa_state->hw_state.depth_compare_op);
559
screen->vk.CmdSetDepthWriteEnableEXT(batch->state->cmdbuf, dsa_state->hw_state.depth_write);
560
screen->vk.CmdSetStencilTestEnableEXT(batch->state->cmdbuf, dsa_state->hw_state.stencil_test);
561
if (dsa_state->hw_state.stencil_test) {
562
screen->vk.CmdSetStencilOpEXT(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_BIT,
563
dsa_state->hw_state.stencil_front.failOp,
564
dsa_state->hw_state.stencil_front.passOp,
565
dsa_state->hw_state.stencil_front.depthFailOp,
566
dsa_state->hw_state.stencil_front.compareOp);
567
screen->vk.CmdSetStencilOpEXT(batch->state->cmdbuf, VK_STENCIL_FACE_BACK_BIT,
568
dsa_state->hw_state.stencil_back.failOp,
569
dsa_state->hw_state.stencil_back.passOp,
570
dsa_state->hw_state.stencil_back.depthFailOp,
571
dsa_state->hw_state.stencil_back.compareOp);
572
}
573
if (dsa_state->base.stencil[0].enabled) {
574
if (dsa_state->base.stencil[1].enabled) {
575
vkCmdSetStencilWriteMask(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_BIT, dsa_state->hw_state.stencil_front.writeMask);
576
vkCmdSetStencilWriteMask(batch->state->cmdbuf, VK_STENCIL_FACE_BACK_BIT, dsa_state->hw_state.stencil_back.writeMask);
577
vkCmdSetStencilCompareMask(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_BIT, dsa_state->hw_state.stencil_front.compareMask);
578
vkCmdSetStencilCompareMask(batch->state->cmdbuf, VK_STENCIL_FACE_BACK_BIT, dsa_state->hw_state.stencil_back.compareMask);
579
} else {
580
vkCmdSetStencilWriteMask(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_AND_BACK, dsa_state->hw_state.stencil_front.writeMask);
581
vkCmdSetStencilCompareMask(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_AND_BACK, dsa_state->hw_state.stencil_front.compareMask);
582
}
583
}
584
}
585
ctx->dsa_state_changed = false;
586
}
587
588
bool rast_state_changed = ctx->rast_state_changed;
589
if (pipeline_changed || rast_state_changed) {
590
if (screen->info.have_EXT_extended_dynamic_state)
591
screen->vk.CmdSetFrontFaceEXT(batch->state->cmdbuf, ctx->gfx_pipeline_state.front_face);
592
}
593
594
if (pipeline_changed || rast_state_changed || mode_changed) {
595
enum pipe_prim_type reduced_prim = u_reduced_prim(dinfo->mode);
596
597
bool depth_bias = false;
598
switch (reduced_prim) {
599
case PIPE_PRIM_POINTS:
600
depth_bias = rast_state->offset_point;
601
break;
602
603
case PIPE_PRIM_LINES:
604
depth_bias = rast_state->offset_line;
605
break;
606
607
case PIPE_PRIM_TRIANGLES:
608
depth_bias = rast_state->offset_tri;
609
break;
610
611
default:
612
unreachable("unexpected reduced prim");
613
}
614
615
if (line_width_needed(reduced_prim, rast_state->hw_state.polygon_mode)) {
616
if (screen->info.feats.features.wideLines || ctx->line_width == 1.0f)
617
vkCmdSetLineWidth(batch->state->cmdbuf, ctx->line_width);
618
else
619
debug_printf("BUG: wide lines not supported, needs fallback!");
620
}
621
if (depth_bias)
622
vkCmdSetDepthBias(batch->state->cmdbuf, rast_state->offset_units, rast_state->offset_clamp, rast_state->offset_scale);
623
else
624
vkCmdSetDepthBias(batch->state->cmdbuf, 0.0f, 0.0f, 0.0f);
625
}
626
ctx->rast_state_changed = false;
627
628
if (ctx->sample_locations_changed) {
629
VkSampleLocationsInfoEXT loc;
630
zink_init_vk_sample_locations(ctx, &loc);
631
screen->vk.CmdSetSampleLocationsEXT(batch->state->cmdbuf, &loc);
632
}
633
ctx->sample_locations_changed = false;
634
635
if (ctx->gfx_pipeline_state.blend_state->need_blend_constants)
636
vkCmdSetBlendConstants(batch->state->cmdbuf, ctx->blend_constants);
637
638
if (ctx->vertex_buffers_dirty || pipeline_changed)
639
zink_bind_vertex_buffers(batch, ctx);
640
641
if (BITSET_TEST(ctx->gfx_stages[PIPE_SHADER_VERTEX]->nir->info.system_values_read, SYSTEM_VALUE_BASE_VERTEX)) {
642
unsigned draw_mode_is_indexed = index_size > 0;
643
vkCmdPushConstants(batch->state->cmdbuf, ctx->curr_program->base.layout, VK_SHADER_STAGE_VERTEX_BIT,
644
offsetof(struct zink_gfx_push_constant, draw_mode_is_indexed), sizeof(unsigned),
645
&draw_mode_is_indexed);
646
}
647
if (ctx->curr_program->shaders[PIPE_SHADER_TESS_CTRL] && ctx->curr_program->shaders[PIPE_SHADER_TESS_CTRL]->is_generated)
648
vkCmdPushConstants(batch->state->cmdbuf, ctx->curr_program->base.layout, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT,
649
offsetof(struct zink_gfx_push_constant, default_inner_level), sizeof(float) * 6,
650
&ctx->tess_levels[0]);
651
652
zink_query_update_gs_states(ctx);
653
654
if (have_streamout) {
655
for (unsigned i = 0; i < ctx->num_so_targets; i++) {
656
struct zink_so_target *t = zink_so_target(ctx->so_targets[i]);
657
counter_buffers[i] = VK_NULL_HANDLE;
658
if (t) {
659
struct zink_resource *res = zink_resource(t->counter_buffer);
660
t->stride = ctx->last_vertex_stage->streamout.so_info.stride[i] * sizeof(uint32_t);
661
zink_batch_reference_resource_rw(batch, res, true);
662
if (t->counter_buffer_valid) {
663
counter_buffers[i] = res->obj->buffer;
664
counter_buffer_offsets[i] = t->counter_buffer_offset;
665
}
666
}
667
}
668
screen->vk.CmdBeginTransformFeedbackEXT(batch->state->cmdbuf, 0, ctx->num_so_targets, counter_buffers, counter_buffer_offsets);
669
}
670
671
ctx->pipeline_changed[0] = false;
672
673
unsigned draw_id = drawid_offset;
674
bool needs_drawid = ctx->drawid_broken;
675
batch->state->draw_count += num_draws;
676
if (index_size > 0) {
677
if (dindirect && dindirect->buffer) {
678
assert(num_draws == 1);
679
if (needs_drawid)
680
update_drawid(ctx, draw_id);
681
struct zink_resource *indirect = zink_resource(dindirect->buffer);
682
zink_batch_reference_resource_rw(batch, indirect, false);
683
if (dindirect->indirect_draw_count) {
684
struct zink_resource *indirect_draw_count = zink_resource(dindirect->indirect_draw_count);
685
zink_batch_reference_resource_rw(batch, indirect_draw_count, false);
686
screen->vk.CmdDrawIndexedIndirectCount(batch->state->cmdbuf, indirect->obj->buffer, dindirect->offset,
687
indirect_draw_count->obj->buffer, dindirect->indirect_draw_count_offset,
688
dindirect->draw_count, dindirect->stride);
689
} else
690
vkCmdDrawIndexedIndirect(batch->state->cmdbuf, indirect->obj->buffer, dindirect->offset, dindirect->draw_count, dindirect->stride);
691
} else {
692
if (need_index_buffer_unref)
693
draw_indexed_need_index_buffer_unref(ctx, dinfo, draws, num_draws, draw_id, needs_drawid);
694
else
695
draw_indexed(ctx, dinfo, draws, num_draws, draw_id, needs_drawid);
696
}
697
} else {
698
if (so_target && screen->info.tf_props.transformFeedbackDraw) {
699
if (needs_drawid)
700
update_drawid(ctx, draw_id);
701
zink_batch_reference_resource_rw(batch, zink_resource(so_target->base.buffer), false);
702
zink_batch_reference_resource_rw(batch, zink_resource(so_target->counter_buffer), true);
703
screen->vk.CmdDrawIndirectByteCountEXT(batch->state->cmdbuf, dinfo->instance_count, dinfo->start_instance,
704
zink_resource(so_target->counter_buffer)->obj->buffer, so_target->counter_buffer_offset, 0,
705
MIN2(so_target->stride, screen->info.tf_props.maxTransformFeedbackBufferDataStride));
706
} else if (dindirect && dindirect->buffer) {
707
assert(num_draws == 1);
708
if (needs_drawid)
709
update_drawid(ctx, draw_id);
710
struct zink_resource *indirect = zink_resource(dindirect->buffer);
711
zink_batch_reference_resource_rw(batch, indirect, false);
712
if (dindirect->indirect_draw_count) {
713
struct zink_resource *indirect_draw_count = zink_resource(dindirect->indirect_draw_count);
714
zink_batch_reference_resource_rw(batch, indirect_draw_count, false);
715
screen->vk.CmdDrawIndirectCount(batch->state->cmdbuf, indirect->obj->buffer, dindirect->offset,
716
indirect_draw_count->obj->buffer, dindirect->indirect_draw_count_offset,
717
dindirect->draw_count, dindirect->stride);
718
} else
719
vkCmdDrawIndirect(batch->state->cmdbuf, indirect->obj->buffer, dindirect->offset, dindirect->draw_count, dindirect->stride);
720
} else {
721
draw(ctx, dinfo, draws, num_draws, draw_id, needs_drawid);
722
}
723
}
724
725
if (have_streamout) {
726
for (unsigned i = 0; i < ctx->num_so_targets; i++) {
727
struct zink_so_target *t = zink_so_target(ctx->so_targets[i]);
728
if (t) {
729
counter_buffers[i] = zink_resource(t->counter_buffer)->obj->buffer;
730
counter_buffer_offsets[i] = t->counter_buffer_offset;
731
t->counter_buffer_valid = true;
732
}
733
}
734
screen->vk.CmdEndTransformFeedbackEXT(batch->state->cmdbuf, 0, ctx->num_so_targets, counter_buffers, counter_buffer_offsets);
735
}
736
batch->has_work = true;
737
/* check memory usage and flush/stall as needed to avoid oom */
738
zink_maybe_flush_or_stall(ctx);
739
}
740
741
void
742
zink_launch_grid(struct pipe_context *pctx, const struct pipe_grid_info *info)
743
{
744
struct zink_context *ctx = zink_context(pctx);
745
struct zink_screen *screen = zink_screen(pctx->screen);
746
struct zink_batch *batch = &ctx->batch;
747
748
update_barriers(ctx, true);
749
750
update_compute_program(ctx);
751
752
zink_program_update_compute_pipeline_state(ctx, ctx->curr_compute, info->block);
753
VkPipeline prev_pipeline = ctx->compute_pipeline_state.pipeline;
754
VkPipeline pipeline = zink_get_compute_pipeline(screen, ctx->curr_compute,
755
&ctx->compute_pipeline_state);
756
757
if (zink_program_has_descriptors(&ctx->curr_compute->base))
758
screen->descriptors_update(ctx, true);
759
760
if (ctx->descriptor_refs_dirty[1])
761
zink_update_descriptor_refs(ctx, true);
762
763
if (prev_pipeline != pipeline || ctx->pipeline_changed[1])
764
vkCmdBindPipeline(batch->state->cmdbuf, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);
765
ctx->pipeline_changed[1] = false;
766
767
if (BITSET_TEST(ctx->curr_compute->shader->nir->info.system_values_read, SYSTEM_VALUE_WORK_DIM))
768
vkCmdPushConstants(batch->state->cmdbuf, ctx->curr_compute->base.layout, VK_SHADER_STAGE_COMPUTE_BIT,
769
offsetof(struct zink_cs_push_constant, work_dim), sizeof(uint32_t),
770
&info->work_dim);
771
772
batch->state->compute_count++;
773
if (info->indirect) {
774
vkCmdDispatchIndirect(batch->state->cmdbuf, zink_resource(info->indirect)->obj->buffer, info->indirect_offset);
775
zink_batch_reference_resource_rw(batch, zink_resource(info->indirect), false);
776
} else
777
vkCmdDispatch(batch->state->cmdbuf, info->grid[0], info->grid[1], info->grid[2]);
778
batch->has_work = true;
779
/* check memory usage and flush/stall as needed to avoid oom */
780
zink_maybe_flush_or_stall(ctx);
781
}
782
783