I want to incorporate the conditional compilation feature of WESL into an AssetCache. I have a struct called WeslShader that stores the compiled WESL shader source code, with an Asset implementation that allows me to load it from disk. The problem is that when I load the shader, I also want to have a way to pass what shader feature flags to the AssetCache so that my Asset implementation can pass those feature flags to the WESL compiler to enable or disable certain shader features.
To achieve this, I have "abused" the id passed to Asset::load to encode those feature flags. For example, a shader ID would look something like path.to.my_shader.feature1-feature2-.... Inside load, I will strip the last segment, and pass the remaining path.to.my_shader to cache.source().read(..., "wesl"). Based on the feature flags passed in the IDs, the compilation results will be different, but the stripped IDs will all be the same. This allows hot reloading to automatically recompile all shader variants when the shader source file changes.
Would this work properly? From the looks of it, it would, because I am basically lying to the AssetCahce to make it think that those are different files, but in reality they are the same file, just compiled with different feature flags. If so, I think it would be beneficial for AssetCache to directly support this kind of use case, rather than requiring manual implementation.
I want to incorporate the conditional compilation feature of WESL into an
AssetCache. I have a struct calledWeslShaderthat stores the compiled WESL shader source code, with anAssetimplementation that allows me to load it from disk. The problem is that when I load the shader, I also want to have a way to pass what shader feature flags to theAssetCacheso that myAssetimplementation can pass those feature flags to the WESL compiler to enable or disable certain shader features.To achieve this, I have "abused" the
idpassed toAsset::loadto encode those feature flags. For example, a shader ID would look something likepath.to.my_shader.feature1-feature2-.... Insideload, I will strip the last segment, and pass the remainingpath.to.my_shadertocache.source().read(..., "wesl"). Based on the feature flags passed in the IDs, the compilation results will be different, but the stripped IDs will all be the same. This allows hot reloading to automatically recompile all shader variants when the shader source file changes.Would this work properly? From the looks of it, it would, because I am basically lying to the
AssetCahceto make it think that those are different files, but in reality they are the same file, just compiled with different feature flags. If so, I think it would be beneficial forAssetCacheto directly support this kind of use case, rather than requiring manual implementation.