Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mtl] make non-srgb texture view when binding as storage image #202

Merged
merged 3 commits into from
Feb 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading