From cedf3598b48546eb6809ad74e5686c98f1bec7c6 Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Wed, 12 Jun 2019 20:05:59 +0200 Subject: [PATCH] zink: ensure textures are transitioned properly Acked-by: Jordan Justen --- src/gallium/drivers/zink/zink_context.c | 77 +++++++++++++++---------- 1 file changed, 47 insertions(+), 30 deletions(-) diff --git a/src/gallium/drivers/zink/zink_context.c b/src/gallium/drivers/zink/zink_context.c index 795dffb36a6..9e6b434f47d 100644 --- a/src/gallium/drivers/zink/zink_context.c +++ b/src/gallium/drivers/zink/zink_context.c @@ -886,39 +886,14 @@ zink_draw_vbo(struct pipe_context *pctx, if (!cmdbuf) return; - begin_render_pass(screen, cmdbuf, ctx->gfx_pipeline_state.render_pass, - ctx->framebuffer, - ctx->fb_state.width, ctx->fb_state.height); - - vkCmdSetViewport(cmdbuf->cmdbuf, 0, ctx->num_viewports, ctx->viewports); - - if (ctx->num_scissors) - vkCmdSetScissor(cmdbuf->cmdbuf, 0, ctx->num_scissors, ctx->scissors); - else if (ctx->fb_state.width && ctx->fb_state.height) { - VkRect2D fb_scissor = {}; - fb_scissor.extent.width = ctx->fb_state.width; - fb_scissor.extent.height = ctx->fb_state.height; - vkCmdSetScissor(cmdbuf->cmdbuf, 0, 1, &fb_scissor); - } - - vkCmdSetStencilReference(cmdbuf->cmdbuf, VK_STENCIL_FACE_FRONT_BIT, ctx->stencil_ref[0]); - vkCmdSetStencilReference(cmdbuf->cmdbuf, VK_STENCIL_FACE_BACK_BIT, ctx->stencil_ref[1]); - - if (depth_bias) - vkCmdSetDepthBias(cmdbuf->cmdbuf, rast_state->offset_units, rast_state->offset_clamp, rast_state->offset_scale); - else - vkCmdSetDepthBias(cmdbuf->cmdbuf, 0.0f, 0.0f, 0.0f); - - if (ctx->gfx_pipeline_state.blend_state->need_blend_constants) - vkCmdSetBlendConstants(cmdbuf->cmdbuf, ctx->blend_constants); - - VkDescriptorSet desc_set = allocate_descriptor_set(ctx, gfx_program->dsl); - VkWriteDescriptorSet wds[PIPE_SHADER_TYPES * PIPE_MAX_CONSTANT_BUFFERS + PIPE_SHADER_TYPES * PIPE_MAX_SHADER_SAMPLER_VIEWS]; VkDescriptorBufferInfo buffer_infos[PIPE_SHADER_TYPES * PIPE_MAX_CONSTANT_BUFFERS]; VkDescriptorImageInfo image_infos[PIPE_SHADER_TYPES * PIPE_MAX_SHADER_SAMPLER_VIEWS]; int num_wds = 0, num_buffer_info = 0, num_image_info = 0; + struct zink_resource *transitions[PIPE_SHADER_TYPES * PIPE_MAX_SHADER_SAMPLER_VIEWS]; + int num_transitions = 0; + for (int i = 0; i < ARRAY_SIZE(ctx->gfx_stages); i++) { struct zink_shader *shader = ctx->gfx_stages[i]; if (!shader) @@ -941,7 +916,14 @@ zink_draw_vbo(struct pipe_context *pctx, assert(psampler_view); struct zink_sampler_view *sampler_view = (struct zink_sampler_view *)psampler_view; struct zink_resource *res = zink_resource(psampler_view->texture); - image_infos[num_image_info].imageLayout = res->layout; + VkImageLayout layout = res->layout; + if (layout != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL && + layout != VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL && + layout != VK_IMAGE_LAYOUT_GENERAL) { + transitions[num_transitions++] = res; + layout = VK_IMAGE_LAYOUT_GENERAL; + } + image_infos[num_image_info].imageLayout = layout; image_infos[num_image_info].imageView = sampler_view->image_view; image_infos[num_image_info].sampler = ctx->samplers[i][index]; wds[num_wds].pImageInfo = image_infos + num_image_info; @@ -951,7 +933,6 @@ zink_draw_vbo(struct pipe_context *pctx, wds[num_wds].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; wds[num_wds].pNext = NULL; - wds[num_wds].dstSet = desc_set; wds[num_wds].dstBinding = shader->bindings[j].binding; wds[num_wds].dstArrayElement = 0; wds[num_wds].descriptorCount = 1; @@ -960,6 +941,42 @@ zink_draw_vbo(struct pipe_context *pctx, } } + for (int i = 0; i < num_transitions; ++i) + zink_resource_barrier(cmdbuf->cmdbuf, transitions[i], + transitions[i]->aspect, + VK_IMAGE_LAYOUT_GENERAL); + + begin_render_pass(screen, cmdbuf, ctx->gfx_pipeline_state.render_pass, + ctx->framebuffer, + ctx->fb_state.width, ctx->fb_state.height); + + vkCmdSetViewport(cmdbuf->cmdbuf, 0, ctx->num_viewports, ctx->viewports); + + if (ctx->num_scissors) + vkCmdSetScissor(cmdbuf->cmdbuf, 0, ctx->num_scissors, ctx->scissors); + else if (ctx->fb_state.width && ctx->fb_state.height) { + VkRect2D fb_scissor = {}; + fb_scissor.extent.width = ctx->fb_state.width; + fb_scissor.extent.height = ctx->fb_state.height; + vkCmdSetScissor(cmdbuf->cmdbuf, 0, 1, &fb_scissor); + } + + vkCmdSetStencilReference(cmdbuf->cmdbuf, VK_STENCIL_FACE_FRONT_BIT, ctx->stencil_ref[0]); + vkCmdSetStencilReference(cmdbuf->cmdbuf, VK_STENCIL_FACE_BACK_BIT, ctx->stencil_ref[1]); + + if (depth_bias) + vkCmdSetDepthBias(cmdbuf->cmdbuf, rast_state->offset_units, rast_state->offset_clamp, rast_state->offset_scale); + else + vkCmdSetDepthBias(cmdbuf->cmdbuf, 0.0f, 0.0f, 0.0f); + + if (ctx->gfx_pipeline_state.blend_state->need_blend_constants) + vkCmdSetBlendConstants(cmdbuf->cmdbuf, ctx->blend_constants); + + VkDescriptorSet desc_set = allocate_descriptor_set(ctx, gfx_program->dsl); + + for (int i = 0; i < num_wds; ++i) + wds[i].dstSet = desc_set; + vkUpdateDescriptorSets(screen->dev, num_wds, wds, 0, NULL); vkCmdBindPipeline(cmdbuf->cmdbuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); -- 2.30.2