summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-syntax.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2023-06-28 07:39:31 -0400
committerGitHub <noreply@github.com>2023-06-28 07:39:31 -0400
commit97963c5c119a3445fa6353809669d4553952e66c (patch)
tree321161d206ccc5dfd7fc0fc3261c1ec3847f84a4 /source/slang/slang-syntax.cpp
parent9ddbea318d347f55c81c82e71ee09c45aeb89c59 (diff)
Add support for vk::image_format attribute (#2945)
Diffstat (limited to 'source/slang/slang-syntax.cpp')
-rw-r--r--source/slang/slang-syntax.cpp49
1 files changed, 45 insertions, 4 deletions
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)];