Path: blob/21.2-virgl/src/gallium/drivers/zink/zink_draw.c
4570 views
#include "zink_compiler.h"1#include "zink_context.h"2#include "zink_program.h"3#include "zink_query.h"4#include "zink_resource.h"5#include "zink_screen.h"6#include "zink_state.h"7#include "zink_surface.h"89#include "tgsi/tgsi_from_mesa.h"10#include "util/hash_table.h"11#include "util/u_debug.h"12#include "util/u_helpers.h"13#include "util/u_inlines.h"14#include "util/u_prim.h"15#include "util/u_prim_restart.h"161718static void19zink_emit_xfb_counter_barrier(struct zink_context *ctx)20{21/* Between the pause and resume there needs to be a memory barrier for the counter buffers22* with a source access of VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_WRITE_BIT_EXT23* at pipeline stage VK_PIPELINE_STAGE_TRANSFORM_FEEDBACK_BIT_EXT24* to a destination access of VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_READ_BIT_EXT25* at pipeline stage VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT.26*27* - from VK_EXT_transform_feedback spec28*/29for (unsigned i = 0; i < ctx->num_so_targets; i++) {30struct zink_so_target *t = zink_so_target(ctx->so_targets[i]);31if (!t)32continue;33struct zink_resource *res = zink_resource(t->counter_buffer);34if (t->counter_buffer_valid)35zink_resource_buffer_barrier(ctx, NULL, res, VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_READ_BIT_EXT,36VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT);37else38zink_resource_buffer_barrier(ctx, NULL, res, VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_WRITE_BIT_EXT,39VK_PIPELINE_STAGE_TRANSFORM_FEEDBACK_BIT_EXT);40}41ctx->xfb_barrier = false;42}4344static void45zink_emit_xfb_vertex_input_barrier(struct zink_context *ctx, struct zink_resource *res)46{47/* A pipeline barrier is required between using the buffers as48* transform feedback buffers and vertex buffers to49* ensure all writes to the transform feedback buffers are visible50* when the data is read as vertex attributes.51* The source access is VK_ACCESS_TRANSFORM_FEEDBACK_WRITE_BIT_EXT52* and the destination access is VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT53* for the pipeline stages VK_PIPELINE_STAGE_TRANSFORM_FEEDBACK_BIT_EXT54* and VK_PIPELINE_STAGE_VERTEX_INPUT_BIT respectively.55*56* - 20.3.1. Drawing Transform Feedback57*/58zink_resource_buffer_barrier(ctx, NULL, res, VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT,59VK_PIPELINE_STAGE_VERTEX_INPUT_BIT);60}6162static void63zink_emit_stream_output_targets(struct pipe_context *pctx)64{65struct zink_context *ctx = zink_context(pctx);66struct zink_screen *screen = zink_screen(pctx->screen);67struct zink_batch *batch = &ctx->batch;68VkBuffer buffers[PIPE_MAX_SO_OUTPUTS] = {0};69VkDeviceSize buffer_offsets[PIPE_MAX_SO_OUTPUTS] = {0};70VkDeviceSize buffer_sizes[PIPE_MAX_SO_OUTPUTS] = {0};7172for (unsigned i = 0; i < ctx->num_so_targets; i++) {73struct zink_so_target *t = (struct zink_so_target *)ctx->so_targets[i];74if (!t) {75/* no need to reference this or anything */76buffers[i] = zink_resource(ctx->dummy_xfb_buffer)->obj->buffer;77buffer_offsets[i] = 0;78buffer_sizes[i] = sizeof(uint8_t);79continue;80}81struct zink_resource *res = zink_resource(t->base.buffer);82if (!(res->bind_history & ZINK_RESOURCE_USAGE_STREAMOUT))83/* resource has been rebound */84t->counter_buffer_valid = false;85buffers[i] = res->obj->buffer;86zink_resource_buffer_barrier(ctx, NULL, zink_resource(t->base.buffer),87VK_ACCESS_TRANSFORM_FEEDBACK_WRITE_BIT_EXT, VK_PIPELINE_STAGE_TRANSFORM_FEEDBACK_BIT_EXT);88zink_batch_reference_resource_rw(batch, res, true);89buffer_offsets[i] = t->base.buffer_offset;90buffer_sizes[i] = t->base.buffer_size;91res->bind_history |= ZINK_RESOURCE_USAGE_STREAMOUT;92util_range_add(t->base.buffer, &res->valid_buffer_range, t->base.buffer_offset,93t->base.buffer_offset + t->base.buffer_size);94}9596screen->vk.CmdBindTransformFeedbackBuffersEXT(batch->state->cmdbuf, 0, ctx->num_so_targets,97buffers, buffer_offsets,98buffer_sizes);99ctx->dirty_so_targets = false;100}101102ALWAYS_INLINE static void103check_buffer_barrier(struct zink_context *ctx, struct pipe_resource *pres, VkAccessFlags flags, VkPipelineStageFlags pipeline)104{105struct zink_resource *res = zink_resource(pres);106zink_resource_buffer_barrier(ctx, NULL, res, flags, pipeline);107}108109ALWAYS_INLINE static void110barrier_draw_buffers(struct zink_context *ctx, const struct pipe_draw_info *dinfo,111const struct pipe_draw_indirect_info *dindirect, struct pipe_resource *index_buffer)112{113if (index_buffer)114check_buffer_barrier(ctx, index_buffer, VK_ACCESS_INDEX_READ_BIT, VK_PIPELINE_STAGE_VERTEX_INPUT_BIT);115if (dindirect && dindirect->buffer) {116check_buffer_barrier(ctx, dindirect->buffer,117VK_ACCESS_INDIRECT_COMMAND_READ_BIT, VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT);118if (dindirect->indirect_draw_count)119check_buffer_barrier(ctx, dindirect->indirect_draw_count,120VK_ACCESS_INDIRECT_COMMAND_READ_BIT, VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT);121}122}123124static void125zink_bind_vertex_buffers(struct zink_batch *batch, struct zink_context *ctx)126{127VkBuffer buffers[PIPE_MAX_ATTRIBS];128VkDeviceSize buffer_offsets[PIPE_MAX_ATTRIBS];129VkDeviceSize buffer_strides[PIPE_MAX_ATTRIBS];130const struct zink_vertex_elements_state *elems = ctx->element_state;131struct zink_screen *screen = zink_screen(ctx->base.screen);132133if (!elems->hw_state.num_bindings)134return;135136for (unsigned i = 0; i < elems->hw_state.num_bindings; i++) {137struct pipe_vertex_buffer *vb = ctx->vertex_buffers + ctx->element_state->binding_map[i];138assert(vb);139if (vb->buffer.resource) {140struct zink_resource *res = zink_resource(vb->buffer.resource);141buffers[i] = res->obj->buffer;142buffer_offsets[i] = vb->buffer_offset;143buffer_strides[i] = vb->stride;144} else {145buffers[i] = zink_resource(ctx->dummy_vertex_buffer)->obj->buffer;146buffer_offsets[i] = 0;147buffer_strides[i] = 0;148}149}150151if (screen->info.have_EXT_extended_dynamic_state)152screen->vk.CmdBindVertexBuffers2EXT(batch->state->cmdbuf, 0,153elems->hw_state.num_bindings,154buffers, buffer_offsets, NULL, buffer_strides);155else156vkCmdBindVertexBuffers(batch->state->cmdbuf, 0,157elems->hw_state.num_bindings,158buffers, buffer_offsets);159ctx->vertex_buffers_dirty = false;160}161162static void163update_compute_program(struct zink_context *ctx)164{165unsigned bits = 1 << PIPE_SHADER_COMPUTE;166if (ctx->dirty_shader_stages & bits) {167struct zink_compute_program *comp = NULL;168struct hash_entry *entry = _mesa_hash_table_search(ctx->compute_program_cache,169ctx->compute_stage);170if (!entry) {171comp = zink_create_compute_program(ctx, ctx->compute_stage);172entry = _mesa_hash_table_insert(ctx->compute_program_cache, comp->shader, comp);173}174comp = (struct zink_compute_program*)(entry ? entry->data : NULL);175if (comp && comp != ctx->curr_compute) {176ctx->compute_pipeline_state.dirty = true;177zink_batch_reference_program(&ctx->batch, &comp->base);178}179ctx->curr_compute = comp;180ctx->dirty_shader_stages &= bits;181}182}183184static void185update_gfx_program(struct zink_context *ctx)186{187if (ctx->last_vertex_stage_dirty) {188if (ctx->gfx_stages[PIPE_SHADER_GEOMETRY])189ctx->dirty_shader_stages |= BITFIELD_BIT(PIPE_SHADER_GEOMETRY);190else if (ctx->gfx_stages[PIPE_SHADER_TESS_EVAL])191ctx->dirty_shader_stages |= BITFIELD_BIT(PIPE_SHADER_TESS_EVAL);192else193ctx->dirty_shader_stages |= BITFIELD_BIT(PIPE_SHADER_VERTEX);194ctx->last_vertex_stage_dirty = false;195}196unsigned bits = u_bit_consecutive(PIPE_SHADER_VERTEX, 5);197if (ctx->dirty_shader_stages & bits) {198struct zink_gfx_program *prog = NULL;199struct hash_entry *entry = _mesa_hash_table_search(ctx->program_cache,200ctx->gfx_stages);201if (entry)202zink_update_gfx_program(ctx, (struct zink_gfx_program*)entry->data);203else {204prog = zink_create_gfx_program(ctx, ctx->gfx_stages);205entry = _mesa_hash_table_insert(ctx->program_cache, prog->shaders, prog);206}207prog = (struct zink_gfx_program*)(entry ? entry->data : NULL);208if (prog && prog != ctx->curr_program) {209ctx->gfx_pipeline_state.combined_dirty = true;210zink_batch_reference_program(&ctx->batch, &prog->base);211}212ctx->curr_program = prog;213ctx->dirty_shader_stages &= ~bits;214}215}216217static bool218line_width_needed(enum pipe_prim_type reduced_prim,219VkPolygonMode polygon_mode)220{221switch (reduced_prim) {222case PIPE_PRIM_POINTS:223return false;224225case PIPE_PRIM_LINES:226return true;227228case PIPE_PRIM_TRIANGLES:229return polygon_mode == VK_POLYGON_MODE_LINE;230231default:232unreachable("unexpected reduced prim");233}234}235236ALWAYS_INLINE static void237update_drawid(struct zink_context *ctx, unsigned draw_id)238{239vkCmdPushConstants(ctx->batch.state->cmdbuf, ctx->curr_program->base.layout, VK_SHADER_STAGE_VERTEX_BIT,240offsetof(struct zink_gfx_push_constant, draw_id), sizeof(unsigned),241&draw_id);242}243244ALWAYS_INLINE static void245draw_indexed_need_index_buffer_unref(struct zink_context *ctx,246const struct pipe_draw_info *dinfo,247const struct pipe_draw_start_count_bias *draws,248unsigned num_draws,249unsigned draw_id,250bool needs_drawid)251{252VkCommandBuffer cmdbuf = ctx->batch.state->cmdbuf;253if (dinfo->increment_draw_id && needs_drawid) {254for (unsigned i = 0; i < num_draws; i++) {255update_drawid(ctx, draw_id);256vkCmdDrawIndexed(cmdbuf,257draws[i].count, dinfo->instance_count,2580, draws[i].index_bias, dinfo->start_instance);259draw_id++;260}261} else {262if (needs_drawid)263update_drawid(ctx, draw_id);264for (unsigned i = 0; i < num_draws; i++)265vkCmdDrawIndexed(cmdbuf,266draws[i].count, dinfo->instance_count,2670, draws[i].index_bias, dinfo->start_instance);268269}270}271272ALWAYS_INLINE static void273draw_indexed(struct zink_context *ctx,274const struct pipe_draw_info *dinfo,275const struct pipe_draw_start_count_bias *draws,276unsigned num_draws,277unsigned draw_id,278bool needs_drawid)279{280VkCommandBuffer cmdbuf = ctx->batch.state->cmdbuf;281if (dinfo->increment_draw_id && needs_drawid) {282for (unsigned i = 0; i < num_draws; i++) {283update_drawid(ctx, draw_id);284vkCmdDrawIndexed(cmdbuf,285draws[i].count, dinfo->instance_count,286draws[i].start, draws[i].index_bias, dinfo->start_instance);287draw_id++;288}289} else {290if (needs_drawid)291update_drawid(ctx, draw_id);292if (zink_screen(ctx->base.screen)->info.have_EXT_multi_draw)293zink_screen(ctx->base.screen)->vk.CmdDrawMultiIndexedEXT(cmdbuf, num_draws, (const VkMultiDrawIndexedInfoEXT*)draws,294dinfo->instance_count,295dinfo->start_instance, sizeof(struct pipe_draw_start_count_bias),296dinfo->index_bias_varies ? NULL : &draws[0].index_bias);297else {298for (unsigned i = 0; i < num_draws; i++)299vkCmdDrawIndexed(cmdbuf,300draws[i].count, dinfo->instance_count,301draws[i].start, draws[i].index_bias, dinfo->start_instance);302}303}304}305306ALWAYS_INLINE static void307draw(struct zink_context *ctx,308const struct pipe_draw_info *dinfo,309const struct pipe_draw_start_count_bias *draws,310unsigned num_draws,311unsigned draw_id,312bool needs_drawid)313{314VkCommandBuffer cmdbuf = ctx->batch.state->cmdbuf;315if (dinfo->increment_draw_id && needs_drawid) {316for (unsigned i = 0; i < num_draws; i++) {317update_drawid(ctx, draw_id);318vkCmdDraw(cmdbuf, draws[i].count, dinfo->instance_count, draws[i].start, dinfo->start_instance);319draw_id++;320}321} else {322if (needs_drawid)323update_drawid(ctx, draw_id);324if (zink_screen(ctx->base.screen)->info.have_EXT_multi_draw)325zink_screen(ctx->base.screen)->vk.CmdDrawMultiEXT(cmdbuf, num_draws, (const VkMultiDrawInfoEXT*)draws,326dinfo->instance_count, dinfo->start_instance,327sizeof(struct pipe_draw_start_count_bias));328else {329for (unsigned i = 0; i < num_draws; i++)330vkCmdDraw(cmdbuf, draws[i].count, dinfo->instance_count, draws[i].start, dinfo->start_instance);331332}333}334}335336static void337update_barriers(struct zink_context *ctx, bool is_compute)338{339if (!ctx->need_barriers[is_compute]->entries)340return;341struct set *need_barriers = ctx->need_barriers[is_compute];342ctx->barrier_set_idx[is_compute] = !ctx->barrier_set_idx[is_compute];343ctx->need_barriers[is_compute] = &ctx->update_barriers[is_compute][ctx->barrier_set_idx[is_compute]];344set_foreach(need_barriers, he) {345struct zink_resource *res = (struct zink_resource *)he->key;346VkPipelineStageFlags pipeline = 0;347VkAccessFlags access = 0;348if (res->bind_count[is_compute]) {349if (res->write_bind_count[is_compute])350access |= VK_ACCESS_SHADER_WRITE_BIT;351if (res->write_bind_count[is_compute] != res->bind_count[is_compute]) {352unsigned bind_count = res->bind_count[is_compute] - res->write_bind_count[is_compute];353if (res->obj->is_buffer) {354if (res->ubo_bind_count[is_compute]) {355access |= VK_ACCESS_UNIFORM_READ_BIT;356bind_count -= res->ubo_bind_count[is_compute];357}358if (!is_compute && res->vbo_bind_count) {359access |= VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;360pipeline |= VK_PIPELINE_STAGE_VERTEX_INPUT_BIT;361bind_count -= res->vbo_bind_count;362}363}364if (bind_count)365access |= VK_ACCESS_SHADER_READ_BIT;366}367if (is_compute)368pipeline = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;369else {370u_foreach_bit(stage, res->bind_history) {371if ((1 << stage) != ZINK_RESOURCE_USAGE_STREAMOUT)372pipeline |= zink_pipeline_flags_from_pipe_stage((enum pipe_shader_type)stage);373}374}375if (res->base.b.target == PIPE_BUFFER)376zink_resource_buffer_barrier(ctx, NULL, res, access, pipeline);377else {378VkImageLayout layout = zink_descriptor_util_image_layout_eval(res, is_compute);379if (layout != res->layout)380zink_resource_image_barrier(ctx, NULL, res, layout, access, pipeline);381}382/* always barrier on draw if this resource has either multiple image write binds or383* image write binds and image read binds384*/385if (res->write_bind_count[is_compute] && res->bind_count[is_compute] > 1)386_mesa_set_add_pre_hashed(ctx->need_barriers[is_compute], he->hash, res);387}388_mesa_set_remove(need_barriers, he);389if (!need_barriers->entries)390break;391}392}393394void395zink_draw_vbo(struct pipe_context *pctx,396const struct pipe_draw_info *dinfo,397unsigned drawid_offset,398const struct pipe_draw_indirect_info *dindirect,399const struct pipe_draw_start_count_bias *draws,400unsigned num_draws)401{402struct zink_context *ctx = zink_context(pctx);403struct zink_screen *screen = zink_screen(pctx->screen);404struct zink_rasterizer_state *rast_state = ctx->rast_state;405struct zink_depth_stencil_alpha_state *dsa_state = ctx->dsa_state;406struct zink_batch *batch = &ctx->batch;407struct zink_so_target *so_target =408dindirect && dindirect->count_from_stream_output ?409zink_so_target(dindirect->count_from_stream_output) : NULL;410VkBuffer counter_buffers[PIPE_MAX_SO_OUTPUTS];411VkDeviceSize counter_buffer_offsets[PIPE_MAX_SO_OUTPUTS];412bool need_index_buffer_unref = false;413bool mode_changed = ctx->gfx_pipeline_state.mode != dinfo->mode;414415update_barriers(ctx, false);416417if (ctx->gfx_pipeline_state.vertices_per_patch != dinfo->vertices_per_patch)418ctx->gfx_pipeline_state.dirty = true;419bool drawid_broken = ctx->drawid_broken;420ctx->drawid_broken = false;421if (!dindirect || !dindirect->buffer)422ctx->drawid_broken = BITSET_TEST(ctx->gfx_stages[PIPE_SHADER_VERTEX]->nir->info.system_values_read, SYSTEM_VALUE_DRAW_ID) &&423(drawid_offset != 0 ||424(!screen->info.have_EXT_multi_draw && num_draws > 1) ||425(screen->info.have_EXT_multi_draw && num_draws > 1 && !dinfo->increment_draw_id));426if (drawid_broken != ctx->drawid_broken)427ctx->dirty_shader_stages |= BITFIELD_BIT(PIPE_SHADER_VERTEX);428ctx->gfx_pipeline_state.vertices_per_patch = dinfo->vertices_per_patch;429if (ctx->rast_state->base.point_quad_rasterization &&430ctx->gfx_prim_mode != dinfo->mode) {431if (ctx->gfx_prim_mode == PIPE_PRIM_POINTS || dinfo->mode == PIPE_PRIM_POINTS)432ctx->dirty_shader_stages |= BITFIELD_BIT(PIPE_SHADER_FRAGMENT);433}434ctx->gfx_prim_mode = dinfo->mode;435update_gfx_program(ctx);436437if (zink_program_has_descriptors(&ctx->curr_program->base))438screen->descriptors_update(ctx, false);439440if (ctx->gfx_pipeline_state.primitive_restart != dinfo->primitive_restart)441ctx->gfx_pipeline_state.dirty = true;442ctx->gfx_pipeline_state.primitive_restart = dinfo->primitive_restart;443444unsigned index_offset = 0;445unsigned index_size = dinfo->index_size;446struct pipe_resource *index_buffer = NULL;447if (index_size > 0) {448if (dinfo->has_user_indices) {449if (!util_upload_index_buffer(pctx, dinfo, &draws[0], &index_buffer, &index_offset, 4)) {450debug_printf("util_upload_index_buffer() failed\n");451return;452}453zink_batch_reference_resource_move(batch, zink_resource(index_buffer));454} else {455index_buffer = dinfo->index.resource;456zink_batch_reference_resource_rw(batch, zink_resource(index_buffer), false);457}458assert(index_size <= 4 && index_size != 3);459assert(index_size != 1 || screen->info.have_EXT_index_type_uint8);460const VkIndexType index_type[3] = {461VK_INDEX_TYPE_UINT8_EXT,462VK_INDEX_TYPE_UINT16,463VK_INDEX_TYPE_UINT32,464};465struct zink_resource *res = zink_resource(index_buffer);466vkCmdBindIndexBuffer(batch->state->cmdbuf, res->obj->buffer, index_offset, index_type[index_size >> 1]);467}468469bool have_streamout = !!ctx->num_so_targets;470if (have_streamout) {471if (ctx->xfb_barrier)472zink_emit_xfb_counter_barrier(ctx);473if (ctx->dirty_so_targets)474zink_emit_stream_output_targets(pctx);475}476477if (so_target)478zink_emit_xfb_vertex_input_barrier(ctx, zink_resource(so_target->base.buffer));479480barrier_draw_buffers(ctx, dinfo, dindirect, index_buffer);481482if (ctx->descriptor_refs_dirty[0])483zink_update_descriptor_refs(ctx, false);484485batch = zink_batch_rp(ctx);486487VkPipeline prev_pipeline = ctx->gfx_pipeline_state.pipeline;488VkPipeline pipeline = zink_get_gfx_pipeline(ctx, ctx->curr_program,489&ctx->gfx_pipeline_state,490dinfo->mode);491bool pipeline_changed = prev_pipeline != pipeline || ctx->pipeline_changed[0];492if (pipeline_changed)493vkCmdBindPipeline(batch->state->cmdbuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);494495if (ctx->vp_state_changed || pipeline_changed) {496VkViewport viewports[PIPE_MAX_VIEWPORTS];497for (unsigned i = 0; i < ctx->vp_state.num_viewports; i++) {498VkViewport viewport = {499ctx->vp_state.viewport_states[i].translate[0] - ctx->vp_state.viewport_states[i].scale[0],500ctx->vp_state.viewport_states[i].translate[1] - ctx->vp_state.viewport_states[i].scale[1],501ctx->vp_state.viewport_states[i].scale[0] * 2,502ctx->vp_state.viewport_states[i].scale[1] * 2,503ctx->rast_state->base.clip_halfz ?504ctx->vp_state.viewport_states[i].translate[2] :505ctx->vp_state.viewport_states[i].translate[2] - ctx->vp_state.viewport_states[i].scale[2],506ctx->vp_state.viewport_states[i].translate[2] + ctx->vp_state.viewport_states[i].scale[2]507};508viewports[i] = viewport;509}510if (screen->info.have_EXT_extended_dynamic_state)511screen->vk.CmdSetViewportWithCountEXT(batch->state->cmdbuf, ctx->vp_state.num_viewports, viewports);512else513vkCmdSetViewport(batch->state->cmdbuf, 0, ctx->vp_state.num_viewports, viewports);514}515if (ctx->scissor_changed || ctx->vp_state_changed || pipeline_changed) {516VkRect2D scissors[PIPE_MAX_VIEWPORTS];517if (ctx->rast_state->base.scissor) {518for (unsigned i = 0; i < ctx->vp_state.num_viewports; i++) {519scissors[i].offset.x = ctx->vp_state.scissor_states[i].minx;520scissors[i].offset.y = ctx->vp_state.scissor_states[i].miny;521scissors[i].extent.width = ctx->vp_state.scissor_states[i].maxx - ctx->vp_state.scissor_states[i].minx;522scissors[i].extent.height = ctx->vp_state.scissor_states[i].maxy - ctx->vp_state.scissor_states[i].miny;523}524} else {525for (unsigned i = 0; i < ctx->vp_state.num_viewports; i++) {526scissors[i].offset.x = 0;527scissors[i].offset.y = 0;528scissors[i].extent.width = ctx->fb_state.width;529scissors[i].extent.height = ctx->fb_state.height;530}531}532if (screen->info.have_EXT_extended_dynamic_state)533screen->vk.CmdSetScissorWithCountEXT(batch->state->cmdbuf, ctx->vp_state.num_viewports, scissors);534else535vkCmdSetScissor(batch->state->cmdbuf, 0, ctx->vp_state.num_viewports, scissors);536}537ctx->vp_state_changed = false;538ctx->scissor_changed = false;539540if (ctx->stencil_ref_changed) {541vkCmdSetStencilReference(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_BIT,542ctx->stencil_ref.ref_value[0]);543vkCmdSetStencilReference(batch->state->cmdbuf, VK_STENCIL_FACE_BACK_BIT,544ctx->stencil_ref.ref_value[1]);545ctx->stencil_ref_changed = false;546}547548if (pipeline_changed || ctx->dsa_state_changed) {549if (screen->info.have_EXT_extended_dynamic_state) {550screen->vk.CmdSetDepthBoundsTestEnableEXT(batch->state->cmdbuf, dsa_state->hw_state.depth_bounds_test);551if (dsa_state->hw_state.depth_bounds_test)552vkCmdSetDepthBounds(batch->state->cmdbuf,553dsa_state->hw_state.min_depth_bounds,554dsa_state->hw_state.max_depth_bounds);555screen->vk.CmdSetDepthTestEnableEXT(batch->state->cmdbuf, dsa_state->hw_state.depth_test);556if (dsa_state->hw_state.depth_test)557screen->vk.CmdSetDepthCompareOpEXT(batch->state->cmdbuf, dsa_state->hw_state.depth_compare_op);558screen->vk.CmdSetDepthWriteEnableEXT(batch->state->cmdbuf, dsa_state->hw_state.depth_write);559screen->vk.CmdSetStencilTestEnableEXT(batch->state->cmdbuf, dsa_state->hw_state.stencil_test);560if (dsa_state->hw_state.stencil_test) {561screen->vk.CmdSetStencilOpEXT(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_BIT,562dsa_state->hw_state.stencil_front.failOp,563dsa_state->hw_state.stencil_front.passOp,564dsa_state->hw_state.stencil_front.depthFailOp,565dsa_state->hw_state.stencil_front.compareOp);566screen->vk.CmdSetStencilOpEXT(batch->state->cmdbuf, VK_STENCIL_FACE_BACK_BIT,567dsa_state->hw_state.stencil_back.failOp,568dsa_state->hw_state.stencil_back.passOp,569dsa_state->hw_state.stencil_back.depthFailOp,570dsa_state->hw_state.stencil_back.compareOp);571}572if (dsa_state->base.stencil[0].enabled) {573if (dsa_state->base.stencil[1].enabled) {574vkCmdSetStencilWriteMask(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_BIT, dsa_state->hw_state.stencil_front.writeMask);575vkCmdSetStencilWriteMask(batch->state->cmdbuf, VK_STENCIL_FACE_BACK_BIT, dsa_state->hw_state.stencil_back.writeMask);576vkCmdSetStencilCompareMask(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_BIT, dsa_state->hw_state.stencil_front.compareMask);577vkCmdSetStencilCompareMask(batch->state->cmdbuf, VK_STENCIL_FACE_BACK_BIT, dsa_state->hw_state.stencil_back.compareMask);578} else {579vkCmdSetStencilWriteMask(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_AND_BACK, dsa_state->hw_state.stencil_front.writeMask);580vkCmdSetStencilCompareMask(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_AND_BACK, dsa_state->hw_state.stencil_front.compareMask);581}582}583}584ctx->dsa_state_changed = false;585}586587bool rast_state_changed = ctx->rast_state_changed;588if (pipeline_changed || rast_state_changed) {589if (screen->info.have_EXT_extended_dynamic_state)590screen->vk.CmdSetFrontFaceEXT(batch->state->cmdbuf, ctx->gfx_pipeline_state.front_face);591}592593if (pipeline_changed || rast_state_changed || mode_changed) {594enum pipe_prim_type reduced_prim = u_reduced_prim(dinfo->mode);595596bool depth_bias = false;597switch (reduced_prim) {598case PIPE_PRIM_POINTS:599depth_bias = rast_state->offset_point;600break;601602case PIPE_PRIM_LINES:603depth_bias = rast_state->offset_line;604break;605606case PIPE_PRIM_TRIANGLES:607depth_bias = rast_state->offset_tri;608break;609610default:611unreachable("unexpected reduced prim");612}613614if (line_width_needed(reduced_prim, rast_state->hw_state.polygon_mode)) {615if (screen->info.feats.features.wideLines || ctx->line_width == 1.0f)616vkCmdSetLineWidth(batch->state->cmdbuf, ctx->line_width);617else618debug_printf("BUG: wide lines not supported, needs fallback!");619}620if (depth_bias)621vkCmdSetDepthBias(batch->state->cmdbuf, rast_state->offset_units, rast_state->offset_clamp, rast_state->offset_scale);622else623vkCmdSetDepthBias(batch->state->cmdbuf, 0.0f, 0.0f, 0.0f);624}625ctx->rast_state_changed = false;626627if (ctx->sample_locations_changed) {628VkSampleLocationsInfoEXT loc;629zink_init_vk_sample_locations(ctx, &loc);630screen->vk.CmdSetSampleLocationsEXT(batch->state->cmdbuf, &loc);631}632ctx->sample_locations_changed = false;633634if (ctx->gfx_pipeline_state.blend_state->need_blend_constants)635vkCmdSetBlendConstants(batch->state->cmdbuf, ctx->blend_constants);636637if (ctx->vertex_buffers_dirty || pipeline_changed)638zink_bind_vertex_buffers(batch, ctx);639640if (BITSET_TEST(ctx->gfx_stages[PIPE_SHADER_VERTEX]->nir->info.system_values_read, SYSTEM_VALUE_BASE_VERTEX)) {641unsigned draw_mode_is_indexed = index_size > 0;642vkCmdPushConstants(batch->state->cmdbuf, ctx->curr_program->base.layout, VK_SHADER_STAGE_VERTEX_BIT,643offsetof(struct zink_gfx_push_constant, draw_mode_is_indexed), sizeof(unsigned),644&draw_mode_is_indexed);645}646if (ctx->curr_program->shaders[PIPE_SHADER_TESS_CTRL] && ctx->curr_program->shaders[PIPE_SHADER_TESS_CTRL]->is_generated)647vkCmdPushConstants(batch->state->cmdbuf, ctx->curr_program->base.layout, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT,648offsetof(struct zink_gfx_push_constant, default_inner_level), sizeof(float) * 6,649&ctx->tess_levels[0]);650651zink_query_update_gs_states(ctx);652653if (have_streamout) {654for (unsigned i = 0; i < ctx->num_so_targets; i++) {655struct zink_so_target *t = zink_so_target(ctx->so_targets[i]);656counter_buffers[i] = VK_NULL_HANDLE;657if (t) {658struct zink_resource *res = zink_resource(t->counter_buffer);659t->stride = ctx->last_vertex_stage->streamout.so_info.stride[i] * sizeof(uint32_t);660zink_batch_reference_resource_rw(batch, res, true);661if (t->counter_buffer_valid) {662counter_buffers[i] = res->obj->buffer;663counter_buffer_offsets[i] = t->counter_buffer_offset;664}665}666}667screen->vk.CmdBeginTransformFeedbackEXT(batch->state->cmdbuf, 0, ctx->num_so_targets, counter_buffers, counter_buffer_offsets);668}669670ctx->pipeline_changed[0] = false;671672unsigned draw_id = drawid_offset;673bool needs_drawid = ctx->drawid_broken;674batch->state->draw_count += num_draws;675if (index_size > 0) {676if (dindirect && dindirect->buffer) {677assert(num_draws == 1);678if (needs_drawid)679update_drawid(ctx, draw_id);680struct zink_resource *indirect = zink_resource(dindirect->buffer);681zink_batch_reference_resource_rw(batch, indirect, false);682if (dindirect->indirect_draw_count) {683struct zink_resource *indirect_draw_count = zink_resource(dindirect->indirect_draw_count);684zink_batch_reference_resource_rw(batch, indirect_draw_count, false);685screen->vk.CmdDrawIndexedIndirectCount(batch->state->cmdbuf, indirect->obj->buffer, dindirect->offset,686indirect_draw_count->obj->buffer, dindirect->indirect_draw_count_offset,687dindirect->draw_count, dindirect->stride);688} else689vkCmdDrawIndexedIndirect(batch->state->cmdbuf, indirect->obj->buffer, dindirect->offset, dindirect->draw_count, dindirect->stride);690} else {691if (need_index_buffer_unref)692draw_indexed_need_index_buffer_unref(ctx, dinfo, draws, num_draws, draw_id, needs_drawid);693else694draw_indexed(ctx, dinfo, draws, num_draws, draw_id, needs_drawid);695}696} else {697if (so_target && screen->info.tf_props.transformFeedbackDraw) {698if (needs_drawid)699update_drawid(ctx, draw_id);700zink_batch_reference_resource_rw(batch, zink_resource(so_target->base.buffer), false);701zink_batch_reference_resource_rw(batch, zink_resource(so_target->counter_buffer), true);702screen->vk.CmdDrawIndirectByteCountEXT(batch->state->cmdbuf, dinfo->instance_count, dinfo->start_instance,703zink_resource(so_target->counter_buffer)->obj->buffer, so_target->counter_buffer_offset, 0,704MIN2(so_target->stride, screen->info.tf_props.maxTransformFeedbackBufferDataStride));705} else if (dindirect && dindirect->buffer) {706assert(num_draws == 1);707if (needs_drawid)708update_drawid(ctx, draw_id);709struct zink_resource *indirect = zink_resource(dindirect->buffer);710zink_batch_reference_resource_rw(batch, indirect, false);711if (dindirect->indirect_draw_count) {712struct zink_resource *indirect_draw_count = zink_resource(dindirect->indirect_draw_count);713zink_batch_reference_resource_rw(batch, indirect_draw_count, false);714screen->vk.CmdDrawIndirectCount(batch->state->cmdbuf, indirect->obj->buffer, dindirect->offset,715indirect_draw_count->obj->buffer, dindirect->indirect_draw_count_offset,716dindirect->draw_count, dindirect->stride);717} else718vkCmdDrawIndirect(batch->state->cmdbuf, indirect->obj->buffer, dindirect->offset, dindirect->draw_count, dindirect->stride);719} else {720draw(ctx, dinfo, draws, num_draws, draw_id, needs_drawid);721}722}723724if (have_streamout) {725for (unsigned i = 0; i < ctx->num_so_targets; i++) {726struct zink_so_target *t = zink_so_target(ctx->so_targets[i]);727if (t) {728counter_buffers[i] = zink_resource(t->counter_buffer)->obj->buffer;729counter_buffer_offsets[i] = t->counter_buffer_offset;730t->counter_buffer_valid = true;731}732}733screen->vk.CmdEndTransformFeedbackEXT(batch->state->cmdbuf, 0, ctx->num_so_targets, counter_buffers, counter_buffer_offsets);734}735batch->has_work = true;736/* check memory usage and flush/stall as needed to avoid oom */737zink_maybe_flush_or_stall(ctx);738}739740void741zink_launch_grid(struct pipe_context *pctx, const struct pipe_grid_info *info)742{743struct zink_context *ctx = zink_context(pctx);744struct zink_screen *screen = zink_screen(pctx->screen);745struct zink_batch *batch = &ctx->batch;746747update_barriers(ctx, true);748749update_compute_program(ctx);750751zink_program_update_compute_pipeline_state(ctx, ctx->curr_compute, info->block);752VkPipeline prev_pipeline = ctx->compute_pipeline_state.pipeline;753VkPipeline pipeline = zink_get_compute_pipeline(screen, ctx->curr_compute,754&ctx->compute_pipeline_state);755756if (zink_program_has_descriptors(&ctx->curr_compute->base))757screen->descriptors_update(ctx, true);758759if (ctx->descriptor_refs_dirty[1])760zink_update_descriptor_refs(ctx, true);761762if (prev_pipeline != pipeline || ctx->pipeline_changed[1])763vkCmdBindPipeline(batch->state->cmdbuf, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);764ctx->pipeline_changed[1] = false;765766if (BITSET_TEST(ctx->curr_compute->shader->nir->info.system_values_read, SYSTEM_VALUE_WORK_DIM))767vkCmdPushConstants(batch->state->cmdbuf, ctx->curr_compute->base.layout, VK_SHADER_STAGE_COMPUTE_BIT,768offsetof(struct zink_cs_push_constant, work_dim), sizeof(uint32_t),769&info->work_dim);770771batch->state->compute_count++;772if (info->indirect) {773vkCmdDispatchIndirect(batch->state->cmdbuf, zink_resource(info->indirect)->obj->buffer, info->indirect_offset);774zink_batch_reference_resource_rw(batch, zink_resource(info->indirect), false);775} else776vkCmdDispatch(batch->state->cmdbuf, info->grid[0], info->grid[1], info->grid[2]);777batch->has_work = true;778/* check memory usage and flush/stall as needed to avoid oom */779zink_maybe_flush_or_stall(ctx);780}781782783