summaryrefslogtreecommitdiff
path: root/source/slang/slang-ast-type.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2023-11-16 14:32:33 -0800
committerGitHub <noreply@github.com>2023-11-16 14:32:33 -0800
commit4c78efd0c34442866f20e9d00bbb6908115c9a01 (patch)
tree03ca8584847f0937a926f6b27386dcd982ed7780 /source/slang/slang-ast-type.cpp
parent12f7237e4060388494c549623f4a640327b7ca08 (diff)
Unify stdlib `Texture` types into one generic type. (#3327)
* Unify Texture types in stdlib into 1 generic type. * Fixes. * Fix. * Fixes. * Fix reflection. * Fix binding reflection. * Add gather intrinsics. * Fix gather intrinsics. * Fix texture type toText. * Fix intrinsic. * fix cuda intrinsic. * Fix project files. * cleanup. * Fix. * Fix. * Fix sampler feedback test. * Fix getDimension intrinsics. * Fix spirv sample image intrinsics. * Fix test. * Fix GLSL intrinsic. * Cleanup. --------- Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'source/slang/slang-ast-type.cpp')
-rw-r--r--source/slang/slang-ast-type.cpp192
1 files changed, 188 insertions, 4 deletions
diff --git a/source/slang/slang-ast-type.cpp b/source/slang/slang-ast-type.cpp
index af4f6d124..e8d702062 100644
--- a/source/slang/slang-ast-type.cpp
+++ b/source/slang/slang-ast-type.cpp
@@ -770,10 +770,97 @@ FeedbackType::Kind FeedbackType::getKind() const
return FeedbackType::Kind(magicMod->tag);
}
-TextureFlavor ResourceType::getFlavor() const
+SlangResourceShape ResourceType::getBaseShape()
+{
+ auto shape = _getGenericTypeArg(getDeclRefBase(), 1);
+ if (as<TextureShape1DType>(shape))
+ return SLANG_TEXTURE_1D;
+ else if (as<TextureShape2DType>(shape))
+ return SLANG_TEXTURE_2D;
+ else if (as<TextureShape3DType>(shape))
+ return SLANG_TEXTURE_3D;
+ else if (as<TextureShapeCubeType>(shape))
+ return SLANG_TEXTURE_CUBE;
+ else if (as<TextureShapeBufferType>(shape))
+ return SLANG_TEXTURE_BUFFER;
+
+ return SLANG_RESOURCE_NONE;
+}
+
+SlangResourceShape ResourceType::getShape()
+{
+ auto baseShape = (SlangResourceShape)getBaseShape();
+ if (isArray())
+ baseShape = (SlangResourceShape)((uint32_t)baseShape | SLANG_TEXTURE_ARRAY_FLAG);
+ if (isMultisample())
+ baseShape = (SlangResourceShape)((uint32_t)baseShape | SLANG_TEXTURE_MULTISAMPLE_FLAG);
+ if (isShadow())
+ baseShape = (SlangResourceShape)((uint32_t)baseShape | SLANG_TEXTURE_SHADOW_FLAG);
+ if (isFeedback())
+ baseShape = (SlangResourceShape)((uint32_t)baseShape | SLANG_TEXTURE_FEEDBACK_FLAG);
+ return baseShape;
+}
+
+bool ResourceType::isArray()
+{
+ auto isArray = _getGenericTypeArg(this, kStdlibTextureIsArrayParameterIndex);
+ if (auto constIntVal = as<ConstantIntVal>(isArray))
+ return constIntVal->getValue() != 0;
+ return false;
+}
+
+bool ResourceType::isMultisample()
{
- auto magicMod = getDeclRef().getDecl()->findModifier<MagicTypeModifier>();
- return TextureFlavor(magicMod->tag);
+ auto isMS = _getGenericTypeArg(this, kStdlibTextureIsMultisampleParameterIndex);
+ if (auto constIntVal = as<ConstantIntVal>(isMS))
+ return constIntVal->getValue() != 0;
+ return false;
+}
+
+bool ResourceType::isShadow()
+{
+ auto isShadow = _getGenericTypeArg(this, kStdlibTextureIsShadowParameterIndex);
+ if (auto constIntVal = as<ConstantIntVal>(isShadow))
+ return constIntVal->getValue() != 0;
+ return false;
+}
+
+bool ResourceType::isFeedback()
+{
+ auto access = _getGenericTypeArg(this, kStdlibTextureAccessParameterIndex);
+ if (auto constIntVal = as<ConstantIntVal>(access))
+ return constIntVal->getValue() == kStdlibResourceAccessFeedback;
+ return false;
+}
+
+bool ResourceType::isCombined()
+{
+ auto combined = _getGenericTypeArg(this, kStdlibTextureIsCombinedParameterIndex);
+ if (auto constIntVal = as<ConstantIntVal>(combined))
+ return constIntVal->getValue() != 0;
+ return false;
+}
+
+SlangResourceAccess ResourceType::getAccess()
+{
+ auto access = _getGenericTypeArg(this, kStdlibTextureAccessParameterIndex);
+ if (auto constIntVal = as<ConstantIntVal>(access))
+ {
+ switch (constIntVal->getValue())
+ {
+ case kStdlibResourceAccessReadOnly:
+ return SLANG_RESOURCE_ACCESS_READ;
+ case kStdlibResourceAccessReadWrite:
+ return SLANG_RESOURCE_ACCESS_READ_WRITE;
+ case kStdlibResourceAccessRasterizerOrdered:
+ return SLANG_RESOURCE_ACCESS_RASTER_ORDERED;
+ case kStdlibResourceAccessFeedback:
+ return SLANG_RESOURCE_ACCESS_FEEDBACK;
+ default:
+ break;
+ }
+ }
+ return SLANG_RESOURCE_ACCESS_NONE;
}
SamplerStateFlavor SamplerStateType::getFlavor() const
@@ -792,9 +879,106 @@ Type* ResourceType::getElementType()
return as<Type>(_getGenericTypeArg(this, 0));
}
+void ResourceType::_toTextOverride(StringBuilder& out)
+{
+ auto tryPrintSimpleName = [&](String& outString) -> bool
+ {
+ StringBuilder resultSB;
+ auto access = getAccess();
+ switch (access)
+ {
+ case SLANG_RESOURCE_ACCESS_READ:
+ break;
+ case SLANG_RESOURCE_ACCESS_READ_WRITE:
+ resultSB << "RW";;
+ break;
+ case SLANG_RESOURCE_ACCESS_RASTER_ORDERED:
+ resultSB << "RasterizerOrdered";
+ break;
+ case SLANG_RESOURCE_ACCESS_FEEDBACK:
+ resultSB << "Feedback";
+ break;
+ default:
+ return false;
+ }
+ auto combined = as<ConstantIntVal>(_getGenericTypeArg(this, 7));
+ auto shapeVal = _getGenericTypeArg(this, 1);
+ if (!as<TextureShapeType>(shapeVal))
+ return false;
+ auto shape = getBaseShape();
+ if (!combined)
+ return false;
+ if (combined->getValue() != 0)
+ resultSB << "Sampler";
+ else
+ {
+ if (shape == SLANG_TEXTURE_BUFFER)
+ resultSB << "Buffer";
+ else
+ resultSB << "Texture";
+ }
+ switch (shape)
+ {
+ case SLANG_TEXTURE_1D:
+ resultSB << "1D";
+ break;
+ case SLANG_TEXTURE_2D:
+ resultSB << "2D";
+ break;
+ case SLANG_TEXTURE_3D:
+ resultSB << "3D";
+ break;
+ case SLANG_TEXTURE_CUBE:
+ resultSB << "Cube";
+ break;
+ }
+ auto isArrayVal = as<ConstantIntVal>(_getGenericTypeArg(this, 2));
+ if (!isArrayVal)
+ return false;
+ if (isArray())
+ resultSB << "Array";
+ auto isMultisampleVal = as<ConstantIntVal>(_getGenericTypeArg(this, 3));
+ if (!isMultisampleVal)
+ return false;
+ if (isMultisample())
+ resultSB << "MS";
+ auto isShadowVal = as<ConstantIntVal>(_getGenericTypeArg(this, 6));
+ if (!isShadowVal)
+ return false;
+ if (isShadow())
+ return false;
+ auto elementType = getElementType();
+ if (elementType)
+ {
+ resultSB << "<";
+ resultSB << elementType->toString();
+ auto sampleCount = _getGenericTypeArg(this, 4);
+ if (auto constIntVal = as<ConstantIntVal>(sampleCount))
+ {
+ if (constIntVal->getValue() != 0)
+ resultSB << ", " << constIntVal->getValue();
+ }
+ else
+ {
+ return false;
+ }
+ resultSB << ">";
+ }
+ outString = resultSB.toString();
+ return true;
+ };
+
+ String simpleName;
+
+ if (tryPrintSimpleName(simpleName))
+ out << simpleName;
+ else
+ DeclRefType::_toTextOverride(out);
+}
+
Val* TextureTypeBase::getSampleCount()
{
- return as<Type>(_getGenericTypeArg(this, 1));
+ return as<Type>(_getGenericTypeArg(this, 4));
}
Type* removeParamDirType(Type* type)