Skip to content

Commit

Permalink
[mtl] make non-srgb texture view when binding as storage image
Browse files Browse the repository at this point in the history
  • Loading branch information
dBagrat authored Feb 3, 2024
1 parent b89147d commit d26b801
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion source/ngf-mtl/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,10 @@ struct ngf_sampler_t {

struct ngf_image_t {
ngf_id<MTL::Texture> texture = nullptr;

// Workaround for binding srgb images as writeable storage images.
ngf_id<MTL::Texture> non_srgb_view = nullptr;

ngf_image_format format;
uint32_t usage_flags = 0u;
};
Expand Down Expand Up @@ -2264,6 +2268,16 @@ void ngf_cmd_bind_resources(
}
}

static std::optional<ngf_image_format> 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,
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit d26b801

Please sign in to comment.