You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I’ve been working with Slang’s DescriptorHandle and implementing a custom getDescriptorFromHandle function, but I’ve encountered some challenges with Slang’s generics, which seem to behave differently compared to C++. The goal is to safely check properties of a generic type T—for example, determining if it’s a ConstantBuffer<T, L>—without needing to know the concrete types of T and L, or to determine if a Texture is read-only, and etc. Below, I’ll outline the problem, my attempts to solve it, and my specific question.
The Problem
I need to perform type checks on generic types in a way that’s agnostic to their specific template arguments. For instance, I’d like to check if T is a ConstantBuffer<T, L> regardless of what T and L are. However, Slang doesn’t allow direct type checks like this:
if (T is ConstantBuffer) // Error: ConstantBuffer requires template arguments
This restriction makes sense because ConstantBuffer is a template that expects non-default arguments (T). In contrast, some types like RWTexture1D, which have no non-default template arguments, can be checked directly:
if (T is RWTexture1D) // This works
This approach seems promising for categorizing types, so I extended it to handle multiple texture types:
if (T is RWTexture1D || T is RWTexture1DArray || T is RWTexture2D || T is RWTexture2DArray ||
T is RWTexture2DMS || T is RWTexture2DMSArray || T is RWTexture3D)
{
// Treat as a storage image
}
However, I’m concerned about its safety. If T is specialized with format or texel types (e.g., RWTexture2D), the is check might fail, even though I’m only interested in the general “kind” of the type (e.g., that it’s an RWTexture2D variant). This makes me question whether this method is reliable for broader use.
Attempted Solution
Ideally, I’d define an interface or trait to categorize types and constrain T accordingly, but getDescriptorFromHandle must adhere to a specific signature, so adding generic constraints isn’t an option. As a workaround, I tried extending Slang’s core types with a helper struct to perform the check. Here’s an example:
export T getDescriptorFromHandle<T>(DescriptorHandle<T> handle) where T : IOpaqueDescriptor
{
__target_switch
{
case spirv:
let x = ((uint2)handle).x;
if (T.kind == DescriptorKind.Buffer)
{
if (IsUniformBuffer<T>.ok())
{
return UniformBufferHeap[x].asOpaqueDescriptor<T>();
}
else
{
return StorageBufferHeap[x].asOpaqueDescriptor<T>();
}
}
// ... other cases for Sampler, Texture, etc.
default:
return defaultGetDescriptorFromHandle(handle);
}
}
This approach seems to work, but I’m not confident it’s robust. It relies on Slang’s generic specialization resolving IsUniformBuffer correctly at the point of the check. Since T isn’t known until getDescriptorFromHandle is called, I’m unsure if specialization happens when I expect it to—or if it’s guaranteed to work consistently, especially compared to C++’s template system. Also, i observed that it makes function call when optimizations are disabled so it may not entirely compile time for all scenerios, which is a problem since getDescriptorFromHandle naturally a compile time construct to decide where to fetch the resource.
My Concerns
Reliability of is Checks: Using T is RWTexture1D feels fragile when T might include additional template parameters I don’t care about.
Extension Approach: The IsUniformBuffer struct feels like a workaround. Does it depend on optimization or timing quirks in Slang’s compilation process?
Timing of Specialization: In getDescriptorFromHandle, T is a generic parameter resolved at call time. Can I trust that type checks or extensions will behave predictably, which is more of a general question than getDescriptorFromHandle in this case to understand slang's template system better.
The Question
What’s the correct and safe way to trait or check core generic types (like ConstantBuffer<T, L>) in Slang without relying on concrete specializations? Is there a recommended pattern or language feature for this kind of type introspection or categorization? I’d love guidance on how to handle this cleanly, especially in the context of functions like getDescriptorFromHandle with fixed signatures.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I’ve been working with Slang’s DescriptorHandle and implementing a custom getDescriptorFromHandle function, but I’ve encountered some challenges with Slang’s generics, which seem to behave differently compared to C++. The goal is to safely check properties of a generic type T—for example, determining if it’s a ConstantBuffer<T, L>—without needing to know the concrete types of T and L, or to determine if a Texture is read-only, and etc. Below, I’ll outline the problem, my attempts to solve it, and my specific question.
The Problem
I need to perform type checks on generic types in a way that’s agnostic to their specific template arguments. For instance, I’d like to check if T is a ConstantBuffer<T, L> regardless of what T and L are. However, Slang doesn’t allow direct type checks like this:
if (T is ConstantBuffer) // Error: ConstantBuffer requires template arguments
This restriction makes sense because ConstantBuffer is a template that expects non-default arguments (T). In contrast, some types like RWTexture1D, which have no non-default template arguments, can be checked directly:
if (T is RWTexture1D) // This works
This approach seems promising for categorizing types, so I extended it to handle multiple texture types:
However, I’m concerned about its safety. If T is specialized with format or texel types (e.g., RWTexture2D), the is check might fail, even though I’m only interested in the general “kind” of the type (e.g., that it’s an RWTexture2D variant). This makes me question whether this method is reliable for broader use.
Attempted Solution
Ideally, I’d define an interface or trait to categorize types and constrain T accordingly, but getDescriptorFromHandle must adhere to a specific signature, so adding generic constraints isn’t an option. As a workaround, I tried extending Slang’s core types with a helper struct to perform the check. Here’s an example:
I then used this in getDescriptorFromHandle:
This approach seems to work, but I’m not confident it’s robust. It relies on Slang’s generic specialization resolving IsUniformBuffer correctly at the point of the check. Since T isn’t known until getDescriptorFromHandle is called, I’m unsure if specialization happens when I expect it to—or if it’s guaranteed to work consistently, especially compared to C++’s template system. Also, i observed that it makes function call when optimizations are disabled so it may not entirely compile time for all scenerios, which is a problem since getDescriptorFromHandle naturally a compile time construct to decide where to fetch the resource.
My Concerns
Reliability of is Checks: Using T is RWTexture1D feels fragile when T might include additional template parameters I don’t care about.
Extension Approach: The IsUniformBuffer struct feels like a workaround. Does it depend on optimization or timing quirks in Slang’s compilation process?
Timing of Specialization: In getDescriptorFromHandle, T is a generic parameter resolved at call time. Can I trust that type checks or extensions will behave predictably, which is more of a general question than getDescriptorFromHandle in this case to understand slang's template system better.
The Question
What’s the correct and safe way to trait or check core generic types (like ConstantBuffer<T, L>) in Slang without relying on concrete specializations? Is there a recommended pattern or language feature for this kind of type introspection or categorization? I’d love guidance on how to handle this cleanly, especially in the context of functions like getDescriptorFromHandle with fixed signatures.
Thanks in advance.
Best!
Beta Was this translation helpful? Give feedback.
All reactions