From d84a75466c9d0df78370675421472afe5663b4e9 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Mon, 1 Jun 2020 10:53:19 -0400 Subject: [PATCH] zink: use u_transfer_helper to split/merge interleaved depth/stencil formats EXT_packed_depth_stencil adds GL_UNSIGNED_INT_24_8_EXT which is an interleaved format, but vulkan spec states that reading/writing the corresponding format provides only the D24 component, which requires that we perform separate operations for each component using separate buffers fixes mesa/mesa#3031 Reviewed-by: Erik Faye-Lund Part-of: --- src/gallium/drivers/zink/zink_batch.c | 9 ++ src/gallium/drivers/zink/zink_format.c | 1 + src/gallium/drivers/zink/zink_resource.c | 117 +++++++++++++++++++---- src/gallium/drivers/zink/zink_resource.h | 7 ++ 4 files changed, 116 insertions(+), 18 deletions(-) diff --git a/src/gallium/drivers/zink/zink_batch.c b/src/gallium/drivers/zink/zink_batch.c index 2b92b1a540a..aad6ef958bb 100644 --- a/src/gallium/drivers/zink/zink_batch.c +++ b/src/gallium/drivers/zink/zink_batch.c @@ -111,6 +111,15 @@ zink_batch_reference_resoure(struct zink_batch *batch, if (!entry) { entry = _mesa_set_add(batch->resources, res); pipe_reference(NULL, &res->base.reference); + + /* u_transfer_helper unrefs the stencil buffer when the depth buffer is unrefed, + * so we add an extra ref here to the stencil buffer to compensate + */ + struct zink_resource *stencil; + + zink_get_depth_stencil_resources((struct pipe_resource*)res, NULL, &stencil); + if (stencil) + pipe_reference(NULL, &stencil->base.reference); } } diff --git a/src/gallium/drivers/zink/zink_format.c b/src/gallium/drivers/zink/zink_format.c index 50f89995a90..556409e0e6f 100644 --- a/src/gallium/drivers/zink/zink_format.c +++ b/src/gallium/drivers/zink/zink_format.c @@ -102,6 +102,7 @@ static const VkFormat formats[PIPE_FORMAT_COUNT] = { [PIPE_FORMAT_Z16_UNORM] = VK_FORMAT_D16_UNORM, [PIPE_FORMAT_Z24X8_UNORM] = VK_FORMAT_X8_D24_UNORM_PACK32, [PIPE_FORMAT_Z24_UNORM_S8_UINT] = VK_FORMAT_D24_UNORM_S8_UINT, + [PIPE_FORMAT_S8_UINT] = VK_FORMAT_S8_UINT, // compressed formats [PIPE_FORMAT_DXT1_RGB] = VK_FORMAT_BC1_RGB_UNORM_BLOCK, diff --git a/src/gallium/drivers/zink/zink_resource.c b/src/gallium/drivers/zink/zink_resource.c index ee38909eab8..3be32a95fdf 100644 --- a/src/gallium/drivers/zink/zink_resource.c +++ b/src/gallium/drivers/zink/zink_resource.c @@ -30,6 +30,7 @@ #include "util/slab.h" #include "util/u_debug.h" #include "util/format/u_format.h" +#include "util/u_transfer_helper.h" #include "util/u_inlines.h" #include "util/u_memory.h" @@ -99,6 +100,8 @@ resource_create(struct pipe_screen *pscreen, VkMemoryRequirements reqs; VkMemoryPropertyFlags flags = 0; + + res->internal_format = templ->format; if (templ->target == PIPE_BUFFER) { VkBufferCreateInfo bci = {}; bci.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; @@ -344,18 +347,6 @@ zink_resource_from_handle(struct pipe_screen *pscreen, return resource_create(pscreen, templ, whandle, usage); } -void -zink_screen_resource_init(struct pipe_screen *pscreen) -{ - pscreen->resource_create = zink_resource_create; - pscreen->resource_destroy = zink_resource_destroy; - - if (zink_screen(pscreen)->have_KHR_external_memory_fd) { - pscreen->resource_get_handle = zink_resource_get_handle; - pscreen->resource_from_handle = zink_resource_from_handle; - } -} - static bool zink_transfer_copy_bufimage(struct zink_context *ctx, struct zink_resource *res, @@ -400,7 +391,19 @@ zink_transfer_copy_bufimage(struct zink_context *ctx, zink_batch_reference_resoure(batch, res); zink_batch_reference_resoure(batch, staging_res); - unsigned aspects = res->aspect; + /* we're using u_transfer_helper_deinterleave, which means we'll be getting PIPE_TRANSFER_* usage + * to indicate whether to copy either the depth or stencil aspects + */ + unsigned aspects = 0; + assert((trans->base.usage & (PIPE_TRANSFER_DEPTH_ONLY | PIPE_TRANSFER_STENCIL_ONLY)) != + (PIPE_TRANSFER_DEPTH_ONLY | PIPE_TRANSFER_STENCIL_ONLY)); + if (trans->base.usage & PIPE_TRANSFER_DEPTH_ONLY) + aspects = VK_IMAGE_ASPECT_DEPTH_BIT; + else if (trans->base.usage & PIPE_TRANSFER_STENCIL_ONLY) + aspects = VK_IMAGE_ASPECT_STENCIL_BIT; + else { + aspects = aspect_from_format(res->base.format); + } while (aspects) { int aspect = 1 << u_bit_scan(&aspects); copyRegion.imageSubresource.aspectMask = aspect; @@ -464,12 +467,18 @@ zink_transfer_map(struct pipe_context *pctx, ptr = ((uint8_t *)ptr) + box->x; } else { if (res->optimial_tiling || ((res->base.usage != PIPE_USAGE_STAGING))) { - trans->base.stride = util_format_get_stride(pres->format, box->width); - trans->base.layer_stride = util_format_get_2d_size(pres->format, + enum pipe_format format = pres->format; + if (usage & PIPE_TRANSFER_DEPTH_ONLY) + format = util_format_get_depth_only(pres->format); + else if (usage & PIPE_TRANSFER_STENCIL_ONLY) + format = PIPE_FORMAT_S8_UINT; + trans->base.stride = util_format_get_stride(format, box->width); + trans->base.layer_stride = util_format_get_2d_size(format, trans->base.stride, box->height); struct pipe_resource templ = *pres; + templ.format = format; templ.usage = PIPE_USAGE_STAGING; templ.target = PIPE_BUFFER; templ.bind = 0; @@ -559,13 +568,85 @@ zink_transfer_unmap(struct pipe_context *pctx, slab_free(&ctx->transfer_pool, ptrans); } +static struct pipe_resource * +zink_resource_get_separate_stencil(struct pipe_resource *pres) +{ + /* For packed depth-stencil, we treat depth as the primary resource + * and store S8 as the "second plane" resource. + */ + if (pres->next && pres->next->format == PIPE_FORMAT_S8_UINT) + return pres->next; + + return NULL; + +} + +void +zink_get_depth_stencil_resources(struct pipe_resource *res, + struct zink_resource **out_z, + struct zink_resource **out_s) +{ + if (!res) { + if (out_z) *out_z = NULL; + if (out_s) *out_s = NULL; + return; + } + + if (res->format != PIPE_FORMAT_S8_UINT) { + if (out_z) *out_z = zink_resource(res); + if (out_s) *out_s = zink_resource(zink_resource_get_separate_stencil(res)); + } else { + if (out_z) *out_z = NULL; + if (out_s) *out_s = zink_resource(res); + } +} + +static void +zink_resource_set_separate_stencil(struct pipe_resource *pres, + struct pipe_resource *stencil) +{ + assert(util_format_has_depth(util_format_description(pres->format))); + pipe_resource_reference(&pres->next, stencil); +} + +static enum pipe_format +zink_resource_get_internal_format(struct pipe_resource *pres) +{ + struct zink_resource *res = zink_resource(pres); + return res->internal_format; +} + +static const struct u_transfer_vtbl transfer_vtbl = { + .resource_create = zink_resource_create, + .resource_destroy = zink_resource_destroy, + .transfer_map = zink_transfer_map, + .transfer_unmap = zink_transfer_unmap, + .transfer_flush_region = u_default_transfer_flush_region, + .get_internal_format = zink_resource_get_internal_format, + .set_stencil = zink_resource_set_separate_stencil, + .get_stencil = zink_resource_get_separate_stencil, +}; + +void +zink_screen_resource_init(struct pipe_screen *pscreen) +{ + pscreen->resource_create = zink_resource_create; + pscreen->resource_destroy = zink_resource_destroy; + pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl, true, true, false, false); + + if (zink_screen(pscreen)->have_KHR_external_memory_fd) { + pscreen->resource_get_handle = zink_resource_get_handle; + pscreen->resource_from_handle = zink_resource_from_handle; + } +} + void zink_context_resource_init(struct pipe_context *pctx) { - pctx->transfer_map = zink_transfer_map; - pctx->transfer_unmap = zink_transfer_unmap; + pctx->transfer_map = u_transfer_helper_deinterleave_transfer_map; + pctx->transfer_unmap = u_transfer_helper_deinterleave_transfer_unmap; - pctx->transfer_flush_region = u_default_transfer_flush_region; + pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region; pctx->buffer_subdata = u_default_buffer_subdata; pctx->texture_subdata = u_default_texture_subdata; } diff --git a/src/gallium/drivers/zink/zink_resource.h b/src/gallium/drivers/zink/zink_resource.h index 9bca4c53a43..b6298b3b91f 100644 --- a/src/gallium/drivers/zink/zink_resource.h +++ b/src/gallium/drivers/zink/zink_resource.h @@ -34,6 +34,8 @@ struct sw_displaytarget; struct zink_resource { struct pipe_resource base; + enum pipe_format internal_format:16; + union { VkBuffer buffer; struct { @@ -70,4 +72,9 @@ zink_screen_resource_init(struct pipe_screen *pscreen); void zink_context_resource_init(struct pipe_context *pctx); +void +zink_get_depth_stencil_resources(struct pipe_resource *res, + struct zink_resource **out_z, + struct zink_resource **out_s); + #endif -- 2.30.2