From d26b80171736376b240f143794eeec88be1ee64a Mon Sep 17 00:00:00 2001 From: Bagrat Dabaghyan Date: Sat, 3 Feb 2024 05:06:19 +0400 Subject: [PATCH] [mtl] make non-srgb texture view when binding as storage image --- source/ngf-mtl/impl.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/source/ngf-mtl/impl.cpp b/source/ngf-mtl/impl.cpp index 5d40ebde..c2688a33 100644 --- a/source/ngf-mtl/impl.cpp +++ b/source/ngf-mtl/impl.cpp @@ -651,6 +651,10 @@ struct ngf_sampler_t { struct ngf_image_t { ngf_id texture = nullptr; + + // Workaround for binding srgb images as writeable storage images. + ngf_id non_srgb_view = nullptr; + ngf_image_format format; uint32_t usage_flags = 0u; }; @@ -2264,6 +2268,16 @@ void ngf_cmd_bind_resources( } } +static std::optional get_regular_format_from_srgb(const ngf_image_format f) { + switch (f) { + case NGF_IMAGE_FORMAT_SRGB8: return NGF_IMAGE_FORMAT_RGB8; + case NGF_IMAGE_FORMAT_SRGBA8: return NGF_IMAGE_FORMAT_RGBA8; + case NGF_IMAGE_FORMAT_BGR8_SRGB: return NGF_IMAGE_FORMAT_BGR8; + case NGF_IMAGE_FORMAT_BGRA8_SRGB: return NGF_IMAGE_FORMAT_BGRA8; + default: return std::nullopt; + } +} + void ngf_cmd_bind_compute_resources( ngf_compute_encoder enc, const ngf_resource_bind_op* bind_ops, @@ -2317,7 +2331,14 @@ void ngf_cmd_bind_compute_resources( case NGF_DESCRIPTOR_STORAGE_IMAGE: case NGF_DESCRIPTOR_IMAGE: { const ngf_image_sampler_bind_info& img_bind_op = bind_op.info.image_sampler; - cmd_buf->active_cce->setTexture(img_bind_op.image->texture.get(), native_binding); + if (const auto maybe_format = get_regular_format_from_srgb(img_bind_op.image->format) ) { + if (!img_bind_op.image->non_srgb_view) + img_bind_op.image->non_srgb_view = img_bind_op.image->texture.get()->newTextureView( + get_mtl_pixel_format(maybe_format.value()).format); + cmd_buf->active_cce->setTexture(img_bind_op.image->non_srgb_view.get(), native_binding); + } else { + cmd_buf->active_cce->setTexture(img_bind_op.image->texture.get(), native_binding); + } break; } case NGF_DESCRIPTOR_SAMPLER: {