From 4c78efd0c34442866f20e9d00bbb6908115c9a01 Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 16 Nov 2023 14:32:33 -0800 Subject: 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 --- source/slang/slang-ast-type.cpp | 192 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 188 insertions(+), 4 deletions(-) (limited to 'source/slang/slang-ast-type.cpp') 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(shape)) + return SLANG_TEXTURE_1D; + else if (as(shape)) + return SLANG_TEXTURE_2D; + else if (as(shape)) + return SLANG_TEXTURE_3D; + else if (as(shape)) + return SLANG_TEXTURE_CUBE; + else if (as(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(isArray)) + return constIntVal->getValue() != 0; + return false; +} + +bool ResourceType::isMultisample() { - auto magicMod = getDeclRef().getDecl()->findModifier(); - return TextureFlavor(magicMod->tag); + auto isMS = _getGenericTypeArg(this, kStdlibTextureIsMultisampleParameterIndex); + if (auto constIntVal = as(isMS)) + return constIntVal->getValue() != 0; + return false; +} + +bool ResourceType::isShadow() +{ + auto isShadow = _getGenericTypeArg(this, kStdlibTextureIsShadowParameterIndex); + if (auto constIntVal = as(isShadow)) + return constIntVal->getValue() != 0; + return false; +} + +bool ResourceType::isFeedback() +{ + auto access = _getGenericTypeArg(this, kStdlibTextureAccessParameterIndex); + if (auto constIntVal = as(access)) + return constIntVal->getValue() == kStdlibResourceAccessFeedback; + return false; +} + +bool ResourceType::isCombined() +{ + auto combined = _getGenericTypeArg(this, kStdlibTextureIsCombinedParameterIndex); + if (auto constIntVal = as(combined)) + return constIntVal->getValue() != 0; + return false; +} + +SlangResourceAccess ResourceType::getAccess() +{ + auto access = _getGenericTypeArg(this, kStdlibTextureAccessParameterIndex); + if (auto constIntVal = as(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(_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(_getGenericTypeArg(this, 7)); + auto shapeVal = _getGenericTypeArg(this, 1); + if (!as(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(_getGenericTypeArg(this, 2)); + if (!isArrayVal) + return false; + if (isArray()) + resultSB << "Array"; + auto isMultisampleVal = as(_getGenericTypeArg(this, 3)); + if (!isMultisampleVal) + return false; + if (isMultisample()) + resultSB << "MS"; + auto isShadowVal = as(_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(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(_getGenericTypeArg(this, 1)); + return as(_getGenericTypeArg(this, 4)); } Type* removeParamDirType(Type* type) -- cgit v1.2.3