diff options
Diffstat (limited to 'source')
| -rw-r--r-- | source/slang/core.meta.slang | 55 | ||||
| -rw-r--r-- | source/slang/hlsl.meta.slang | 21 | ||||
| -rw-r--r-- | source/slang/slang-emit-c-like.cpp | 24 | ||||
| -rw-r--r-- | source/slang/slang-ir-redundancy-removal.cpp | 17 | ||||
| -rw-r--r-- | source/slang/slang-ir-util.cpp | 10 |
5 files changed, 121 insertions, 6 deletions
diff --git a/source/slang/core.meta.slang b/source/slang/core.meta.slang index 9da33c755..2507c22dd 100644 --- a/source/slang/core.meta.slang +++ b/source/slang/core.meta.slang @@ -1189,6 +1189,7 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) continue; } + bool isReadOnly = (access == SLANG_RESOURCE_ACCESS_READ); // TODO: any constraints to enforce on what gets to be multisampled? unsigned flavor = baseShape; @@ -1318,6 +1319,7 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) char const* t = isFloat ? "out float " : "out uint "; + sb << "[__readNone]\n"; sb << "void GetDimensions("; if(includeMipInfo) sb << "uint mipLevel, "; @@ -1487,6 +1489,8 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) } } + if (isReadOnly) + sb << "[__readNone]\n"; sb << "T Load("; sb << "int" << loadCoordCount << " location"; if(isMultisample) @@ -1516,8 +1520,8 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) sb << ", $2)$z\")\n"; } - - + if (isReadOnly) + sb << "[__readNone]\n"; sb << "T Load("; sb << "int" << loadCoordCount << " location"; if(isMultisample) @@ -1527,7 +1531,8 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) sb << ", constexpr int" << kBaseTextureTypes[tt].coordCount << " offset"; sb << ");\n"; - + if (isReadOnly) + sb << "[__readNone]\n"; sb << "T Load("; sb << "int" << loadCoordCount << " location"; if(isMultisample) @@ -1632,6 +1637,8 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) } // Output that has get + if (isReadOnly) + sb << "[__readNone]\n"; sb << " get;\n"; // !!!!!!!!!!!!!!!!!!!! set !!!!!!!!!!!!!!!!!!!!!!! @@ -1747,17 +1754,23 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) } } + if (isReadOnly) + sb << "[__readNone]\n"; sb << "T Sample(" << samplerStateParam;; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location);\n"; if( baseShape != TextureFlavor::Shape::ShapeCube ) { sb << "__target_intrinsic(glsl, \"$ctextureOffset($p, $2, $3)$z\")\n"; + if (isReadOnly) + sb << "[__readNone]\n"; sb << "T Sample(" << samplerStateParam;; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; sb << "constexpr int" << kBaseTextureTypes[tt].coordCount << " offset);\n"; } + if (isReadOnly) + sb << "[__readNone]\n"; sb << "T Sample(" << samplerStateParam; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; if( baseShape != TextureFlavor::Shape::ShapeCube ) @@ -1766,6 +1779,8 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) } sb << "float clamp);\n"; + if (isReadOnly) + sb << "[__readNone]\n"; sb << "T Sample(" << samplerStateParam; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; if( baseShape != TextureFlavor::Shape::ShapeCube ) @@ -1776,12 +1791,16 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) // `SampleBias()` sb << "__target_intrinsic(glsl, \"$ctexture($p, $2, $3)$z\")\n"; + if (isReadOnly) + sb << "[__readNone]\n"; sb << "T SampleBias(" << samplerStateParam; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, float bias);\n"; if( baseShape != TextureFlavor::Shape::ShapeCube ) { sb << "__target_intrinsic(glsl, \"$ctextureOffset($p, $2, $3, $4)$z\")\n"; + if (isReadOnly) + sb << "[__readNone]\n"; sb << "T SampleBias(" << samplerStateParam; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, float bias, "; sb << "constexpr int" << kBaseTextureTypes[tt].coordCount << " offset);\n"; @@ -1792,11 +1811,15 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) { // `SampleCmp()` and `SampleCmpLevelZero` sb << "__target_intrinsic(glsl, \"texture($p, vec" << arrCoordCount + 1 << "($2, $3))\")"; + if (isReadOnly) + sb << "[__readNone]\n"; sb << "float SampleCmp(SamplerComparisonState s, "; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; sb << "float compareValue"; sb << ");\n"; sb << "__target_intrinsic(glsl, \"texture($p, vec" << arrCoordCount + 1 << "($2, $3))\")"; + if (isReadOnly) + sb << "[__readNone]\n"; sb << "float SampleCmpLevelZero(SamplerComparisonState s, "; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; sb << "float compareValue"; @@ -1851,12 +1874,15 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) // saying they only exclude `offset` for cube maps (which makes // sense). I'm going to assume the documentation for `SampleCmp` // is just wrong. - + if (isReadOnly) + sb << "[__readNone]\n"; sb << "float SampleCmp(SamplerComparisonState s, "; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; sb << "float compareValue, "; sb << "constexpr int" << kBaseTextureTypes[tt].coordCount << " offset);\n"; + if (isReadOnly) + sb << "[__readNone]\n"; sb << "float SampleCmpLevelZero(SamplerComparisonState s, "; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; sb << "float compareValue, "; @@ -1867,6 +1893,8 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) // the dimension. On CUDA there is texNDGrad, but it always just takes ddx, ddy. // I could just assume 0 for elements not supplied, and ignore z. For now will just leave sb << "__target_intrinsic(glsl, \"$ctextureGrad($p, $2, $3, $4)$z\")\n"; + if (isReadOnly) + sb << "[__readNone]\n"; sb << "T SampleGrad(" << samplerStateParam; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; sb << "float" << kBaseTextureTypes[tt].coordCount << " gradX, "; @@ -1876,6 +1904,8 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) if( baseShape != TextureFlavor::Shape::ShapeCube ) { sb << "__target_intrinsic(glsl, \"$ctextureGradOffset($p, $2, $3, $4, $5)$z\")\n"; + if (isReadOnly) + sb << "[__readNone]\n"; sb << "T SampleGrad(" << samplerStateParam; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; sb << "float" << kBaseTextureTypes[tt].coordCount << " gradX, "; @@ -1884,6 +1914,8 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) sb << "__glsl_extension(GL_ARB_sparse_texture_clamp)"; sb << "__target_intrinsic(glsl, \"$ctextureGradOffsetClampARB($p, $2, $3, $4, $5, $6)$z\")\n"; + if (isReadOnly) + sb << "[__readNone]\n"; sb << "T SampleGrad(" << samplerStateParam; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; sb << "float" << kBaseTextureTypes[tt].coordCount << " gradX, "; @@ -1939,7 +1971,8 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) sb << ", $3)\")\n"; } } - + if (isReadOnly) + sb << "[__readNone]\n"; sb << "T SampleLevel(" << samplerStateParam; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; sb << "float level);\n"; @@ -1947,6 +1980,8 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) if( baseShape != TextureFlavor::Shape::ShapeCube ) { sb << "__target_intrinsic(glsl, \"$ctextureLodOffset($p, $2, $3, $4)$z\")\n"; + if (isReadOnly) + sb << "[__readNone]\n"; sb << "T SampleLevel(" << samplerStateParam; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; sb << "float level, "; @@ -2026,21 +2061,29 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) // "It is based on the base type of DataType except when readMode is equal to cudaReadModeNormalizedFloat (see Texture Reference API), in which case it is always float4." sb << "__target_intrinsic(cuda, \"tex2Dgather<$T0>($0, ($2).x, ($2).y, " << componentIndex << ")\")\n"; } + if (isReadOnly) + sb << "[__readNone]\n"; sb << outputType << " Gather" << componentName << "(" << samplerStateParam; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location);\n"; + if (isReadOnly) + sb << "[__readNone]\n"; EMIT_LINE_DIRECTIVE(); sb << "__target_intrinsic(glsl, \"textureGatherOffset($p, $2, $3, " << componentIndex << ")\")\n"; sb << outputType << " Gather" << componentName << "(" << samplerStateParam; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; sb << "constexpr int" << kBaseTextureTypes[tt].coordCount << " offset);\n"; + if (isReadOnly) + sb << "[__readNone]\n"; EMIT_LINE_DIRECTIVE(); sb << outputType << " Gather" << componentName << "(" << samplerStateParam; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; sb << "constexpr int" << kBaseTextureTypes[tt].coordCount << " offset, "; sb << "out uint status);\n"; + if (isReadOnly) + sb << "[__readNone]\n"; EMIT_LINE_DIRECTIVE(); sb << "__target_intrinsic(glsl, \"textureGatherOffsets($p, $2, int" << kBaseTextureTypes[tt].coordCount << "[]($3, $4, $5, $6), " << componentIndex << ")\")\n"; sb << outputType << " Gather" << componentName << "(" << samplerStateParam; @@ -2050,6 +2093,8 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) sb << "int" << kBaseTextureTypes[tt].coordCount << " offset3, "; sb << "int" << kBaseTextureTypes[tt].coordCount << " offset4);\n"; + if (isReadOnly) + sb << "[__readNone]\n"; EMIT_LINE_DIRECTIVE(); sb << outputType << " Gather" << componentName << "(" << samplerStateParam; sb << "float" << kBaseTextureTypes[tt].coordCount + isArray << " location, "; diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang index 37cdc205e..0b7ca535b 100644 --- a/source/slang/hlsl.meta.slang +++ b/source/slang/hlsl.meta.slang @@ -19,29 +19,39 @@ __intrinsic_type($(kIROp_HLSLByteAddressBufferType)) struct ByteAddressBuffer { __target_intrinsic(glsl, "$1 = $0._data.length() * 4") + [__readNone] void GetDimensions( out uint dim); __target_intrinsic(glsl, "$0._data[$1/4]") + [__readNone] uint Load(int location); + [__readNone] uint Load(int location, out uint status); __target_intrinsic(glsl, "uvec2($0._data[$1/4], $0._data[$1/4+1])") + [__readNone] uint2 Load2(int location); + [__readNone] uint2 Load2(int location, out uint status); __target_intrinsic(glsl, "uvec3($0._data[$1/4], $0._data[$1/4+1], $0._data[$1/4+2])") + [__readNone] uint3 Load3(int location); + [__readNone] uint3 Load3(int location, out uint status); __target_intrinsic(glsl, "uvec4($0._data[$1/4], $0._data[$1/4+1], $0._data[$1/4+2], $0._data[$1/4+3])") + [__readNone] uint4 Load4(int location); + [__readNone] uint4 Load4(int location, out uint status); + [__readNone] T Load<T>(int location) { return __byteAddressBufferLoad<T>(this, location); @@ -216,19 +226,24 @@ __intrinsic_type($(kIROp_HLSLStructuredBufferType)) struct StructuredBuffer { __target_intrinsic(glsl, "$1 = $0._data.length(); $2 = 0") + [__readNone] void GetDimensions( out uint numStructs, out uint stride); __target_intrinsic(glsl, "$0._data[$1]") __target_intrinsic(spirv_direct, "%addr = OpAccessChain resultType*StorageBuffer resultId _0 const(int, 0) _1; OpLoad resultType resultId %addr;") + [__readNone] T Load(int location); + + [__readNone] T Load(int location, out uint status); __subscript(uint index) -> T { __target_intrinsic(glsl, "$0._data[$1]") __target_intrinsic(spirv_direct, "%addr = OpAccessChain resultType*StorageBuffer resultId _0 const(int, 0) _1; OpLoad resultType resultId %addr;") + [__readNone] get; }; }; @@ -4923,6 +4938,7 @@ static const int kBaseBufferAccessLevelCount = sizeof(kBaseBufferAccessLevels) / for (int aa = 0; aa < kBaseBufferAccessLevelCount; ++aa) { auto access = kBaseBufferAccessLevels[aa].access; + bool isReadOnly = (access == SLANG_RESOURCE_ACCESS_READ); auto flavor = TextureFlavor::create(TextureFlavor::Shape::ShapeBuffer, access).flavor; sb << "__generic<T>\n"; sb << "__magic_type(Texture," << int(flavor) << ")\n"; @@ -4930,19 +4946,22 @@ for (int aa = 0; aa < kBaseBufferAccessLevelCount; ++aa) sb << "struct "; sb << kBaseBufferAccessLevels[aa].name; sb << "Buffer {\n"; - + sb << "[__readNone]\n"; sb << "void GetDimensions(out uint dim);\n"; char const* glslLoadFuncName = (access == SLANG_RESOURCE_ACCESS_READ) ? "texelFetch" : "imageLoad"; sb << "__glsl_extension(GL_EXT_samplerless_texture_functions)"; sb << "__target_intrinsic(glsl, \"" << glslLoadFuncName << "($0, $1)$z\")\n"; + if (isReadOnly) sb << "[__readNone]\n"; sb << "T Load(int location);\n"; + if (isReadOnly) sb << "[__readNone]\n"; sb << "T Load(int location, out uint status);\n"; sb << "__subscript(uint index) -> T {\n"; + if (isReadOnly) sb << "[__readNone]\n"; sb << "__glsl_extension(GL_EXT_samplerless_texture_functions)"; sb << "__target_intrinsic(glsl, \"" << glslLoadFuncName << "($0, int($1))$z\") get;\n"; diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp index 7840dc450..a31c16505 100644 --- a/source/slang/slang-emit-c-like.cpp +++ b/source/slang/slang-emit-c-like.cpp @@ -1344,6 +1344,30 @@ bool CLikeSourceEmitter::shouldFoldInstIntoUseSites(IRInst* inst) } } + // If this is a call to a ResourceType's member function, don't fold for readability. + if (auto call = as<IRCall>(inst)) + { + auto callee = getResolvedInstForDecorations(call->getCallee()); + if (callee->findDecoration<IRTargetIntrinsicDecoration>()) + { + auto funcType = as<IRFuncType>(callee->getDataType()); + if (funcType) + { + if (funcType->getParamCount() > 0) + { + auto firstParamType = funcType->getParamType(0); + if (as<IRResourceTypeBase>(firstParamType)) + return false; + if (as<IRHLSLStructuredBufferTypeBase>(firstParamType)) + return false; + if (as<IRUntypedBufferResourceType>(firstParamType)) + return false; + if (as<IRSamplerStateTypeBase>(firstParamType)) + return false; + } + } + } + } // We'd like to figure out if it is safe to fold our instruction into `user` // First, let's make sure they are in the same block/parent: diff --git a/source/slang/slang-ir-redundancy-removal.cpp b/source/slang/slang-ir-redundancy-removal.cpp index 2a2047de9..32b349a4f 100644 --- a/source/slang/slang-ir-redundancy-removal.cpp +++ b/source/slang/slang-ir-redundancy-removal.cpp @@ -28,6 +28,8 @@ struct RedundancyRemovalContext case kIROp_GetElement: case kIROp_GetElementPtr: case kIROp_UpdateElement: + case kIROp_Specialize: + case kIROp_LookupWitness: case kIROp_OptionalHasValue: case kIROp_GetOptionalValue: case kIROp_MakeOptionalValue: @@ -67,6 +69,21 @@ struct RedundancyRemovalContext return true; case kIROp_Call: return isPureFunctionalCall(as<IRCall>(inst)); + case kIROp_Load: + // Load is generally not movable, an exception is loading a global constant buffer. + if (auto load = as<IRLoad>(inst)) + { + auto addrType = load->getPtr()->getDataType(); + switch (addrType->getOp()) + { + case kIROp_ConstantBufferType: + case kIROp_ParameterBlockType: + return true; + default: + break; + } + } + return false; default: return false; } diff --git a/source/slang/slang-ir-util.cpp b/source/slang/slang-ir-util.cpp index 339521f41..13920b011 100644 --- a/source/slang/slang-ir-util.cpp +++ b/source/slang/slang-ir-util.cpp @@ -157,6 +157,7 @@ IRInst* maybeSpecializeWithGeneric(IRBuilder& builder, IRInst* genericToSpecaili return genericToSpecailize; } +// Returns true if is not possible to produce side-effect from a value of `dataType`. bool isValueType(IRInst* dataType) { dataType = getResolvedInstForDecorations(unwrapAttributedType(dataType)); @@ -179,6 +180,15 @@ bool isValueType(IRInst* dataType) case kIROp_FuncType: return true; default: + // Read-only resource handles are considered as Value type. + if (auto resType = as<IRResourceTypeBase>(dataType)) + return (resType->getAccess() == SLANG_RESOURCE_ACCESS_READ); + else if (as<IRSamplerStateTypeBase>(dataType)) + return true; + else if (as<IRHLSLByteAddressBufferType>(dataType)) + return true; + else if (as<IRHLSLStructuredBufferType>(dataType)) + return true; return false; } } |
