From 97963c5c119a3445fa6353809669d4553952e66c Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 28 Jun 2023 07:39:31 -0400 Subject: Add support for vk::image_format attribute (#2945) --- source/slang/slang-syntax.cpp | 49 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) (limited to 'source/slang/slang-syntax.cpp') diff --git a/source/slang/slang-syntax.cpp b/source/slang/slang-syntax.cpp index 606c77096..53e77e646 100644 --- a/source/slang/slang-syntax.cpp +++ b/source/slang/slang-syntax.cpp @@ -1434,10 +1434,8 @@ static const ImageFormatInfo kImageFormatInfos[] = #undef SLANG_IMAGE_FORMAT_INFO }; -bool findImageFormatByName(char const* inName, ImageFormat* outFormat) +bool findImageFormatByName(const UnownedStringSlice& name, ImageFormat* outFormat) { - const UnownedStringSlice name(inName); - for (Index i = 0; i < SLANG_COUNT_OF(kImageFormatInfos); ++i) { const auto& info = kImageFormatInfos[i]; @@ -1450,12 +1448,55 @@ bool findImageFormatByName(char const* inName, ImageFormat* outFormat) return false; } +// https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#id71 +#define SLANG_VK_TO_IMAGE_FORMAT(x) \ + x(r11g11b10f, r11f_g11f_b10f) \ + x(rgb10a2, rgb10_a2) \ + x(rgb10a2ui, rgb10_a2ui) + +struct VkImageFormatInfo +{ + UnownedStringSlice name; + ImageFormat format; +}; +static const VkImageFormatInfo kVkImageFormatInfos[] = +{ +#define SLANG_VK_IMAGE_FORMAT_INFO(name, format) { toSlice(#name), ImageFormat::format }, + SLANG_VK_TO_IMAGE_FORMAT(SLANG_VK_IMAGE_FORMAT_INFO) +}; + +static const auto kSNorm = UnownedStringSlice::fromLiteral("snorm"); + +bool findVkImageFormatByName(const UnownedStringSlice& name, ImageFormat* outFormat) +{ + // Handle names ending in snorm + if (name.endsWith(kSNorm)) + { + StringBuilder buf; + // format names end with snormal after a '_', so replace with that + buf << name.head(name.getLength() - kSNorm.getLength()) << "_" << kSNorm; + return findImageFormatByName(buf.getUnownedSlice(), outFormat); + } + + // Handle the special cases + for (const auto& vkInfo : kVkImageFormatInfos) + { + if (vkInfo.name == name) + { + *outFormat = vkInfo.format; + return true; + } + } + + // Default to the regular lookup mechanism for everything else + return findImageFormatByName(name, outFormat); +} + char const* getGLSLNameForImageFormat(ImageFormat format) { return kImageFormatInfos[Index(format)].name.begin(); } - const ImageFormatInfo& getImageFormatInfo(ImageFormat format) { return kImageFormatInfos[Index(format)]; -- cgit v1.2.3