shader-slang / slang

Making it easier to work with shaders
MIT License
1.79k stars 160 forks source link

Add reflection API to query interface sizes #3948

Open fknfilewalker opened 2 months ago

fknfilewalker commented 2 months ago

Is it possible to set/fix the index that is used to cast an Interface var to some struct implementing the interface?

Based on the following example: is-as-dynamic.slang In your test case you show that it is possible to set a fixed size for the Interface with [anyValueSize(8)]. Impl1 and Impl2 use indices 0 and 1 for differentiation. When Impl1 is not used, it gets removed and Impl2 gets index 0. Is there a way to fix Impl2 to always use index 1?

csyonghe commented 2 months ago

There is a way to specify the ID by explicitly linking a TypeConformance object to your linked program when you are using the COM API to compile your shader.

See ISession::createTypeConformanceComponentType:

virtual SLANG_NO_THROW SlangResult SLANG_MCALL createTypeConformanceComponentType(
    slang::TypeReflection* type,
    slang::TypeReflection* interfaceType,
    ITypeConformance** outConformance,
    SlangInt conformanceIdOverride,
    ISlangBlob** outDiagnostics) = 0;

This function returns you a IComponentType* that you can include in your program as part of a CompositeComponentType.

csyonghe commented 2 months ago

However, there is no way to specify this ID directly in the source code. Please let us know if the API is sufficient for your needs.

fknfilewalker commented 2 months ago

I think that should work. Is there also a way to get the byte size of the behind the scenes used Interface union using the COM API?

csyonghe commented 2 months ago

We currently don't have this in our reflection API, but should be easy to add.

swoods-nv commented 2 months ago

Lining up the reflection API request for later down the line, as we've some fairly high priority stuff allocated near term already. Please let me know if this issue is a critical blocker for you.

fknfilewalker commented 2 months ago

It would be nice to have but for now I can just track the individual struct sizes to calculate the max size.