texture index for MSL #1448
-
I'm converting from GLSL to MSL using MoltenVKShaderConverter. In my GLSL I've got: layout(binding = 1) uniform sampler2D previousFrame;
layout(binding = 2) uniform sampler2D a;
layout(binding = 3) uniform sampler2D b;
layout(binding = 4) uniform sampler2D c;
layout(binding = 5) uniform sampler2D d; the generated MSL is: fragment main0_out main0(texture2d<float> d [[texture(0)]], texture2d<float> c [[texture(1)]], texture2d<float> b [[texture(2)]], texture2d<float> a [[texture(3)]], texture2d<float> previousFrame [[texture(4)]], sampler dSmplr [[sampler(0)]], sampler cSmplr [[sampler(1)]], sampler bSmplr [[sampler(2)]], sampler aSmplr [[sampler(3)]], sampler previousFrameSmplr [[sampler(4)]], float4 gl_FragCoord [[position]]) The GLSL binding indices don't seem to make a difference, I always get the same output. And the order is determined by the first use of the Is there a way I have the metal indices correspond to the binding indices? If I were to use Vulkan directly then I wouldn't have to deal with this. I'm just so much more familiar with Metal. Conversion code is: - (NSString*) toMSL:(NSString*) text {
mvk::GLSLToSPIRVConverter toSpirv;
toSpirv.setGLSL(std::string(text.UTF8String));
if(!toSpirv.convert(kMVKGLSLConversionShaderStageFragment, true, true)) {
printf("failed to convert to SPIRV: %s\n", toSpirv.getResultLog().c_str());
return nil;
}
mvk::SPIRVToMSLConverter toMSL;
toMSL.setSPIRV(toSpirv.getSPIRV());
mvk::SPIRVToMSLConversionConfiguration ctx;
if(!toMSL.convert(ctx)) {
printf("failed to convert to MSL: %s\n", toMSL.getResultLog().c_str());
return nil;
}
return [NSString stringWithUTF8String:toMSL.getMSL().c_str()];
} Many thanks for any help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The index-based resource mapping for Metal is different than location-binding mapping used by Vulkan. SPIRV-Cross, which is used by MoltenVK for shader conversion, defaults to automatically incrementing index numbers. At runtime, MoltenVK passes the mappings to SPIRV-Cross, based on how the Vulkan pipeline is configured, so that both the shader and pipeline use the same Metal indexes for the same resources. If you are trying to convert shaders off-line for some other use, it's up to you to define that Vulkan-Metal resource mapping, either by accepting the SPIRV-Cross default approach, or by setting the contents of your To get a better idea of how to do this, have a look at the MoltenVK code to see how |
Beta Was this translation helpful? Give feedback.
The index-based resource mapping for Metal is different than location-binding mapping used by Vulkan. SPIRV-Cross, which is used by MoltenVK for shader conversion, defaults to automatically incrementing index numbers.
At runtime, MoltenVK passes the mappings to SPIRV-Cross, based on how the Vulkan pipeline is configured, so that both the shader and pipeline use the same Metal indexes for the same resources.
If you are trying to convert shaders off-line for some other use, it's up to you to define that Vulkan-Metal resource mapping, either by accepting the SPIRV-Cross default approach, or by setting the contents of your
ctx.resourceBindings
(mvk::SPIRVToMSLConversionConfiguration::resource…