KhronosGroup / MoltenVK

MoltenVK is a Vulkan Portability implementation. It layers a subset of the high-performance, industry-standard Vulkan graphics and compute API over Apple's Metal graphics framework, enabling Vulkan applications to run on macOS, iOS and tvOS.
Apache License 2.0
4.63k stars 402 forks source link

MoltenVK : VK_ERROR_INITIALIZATION_FAILED: Shader library compile failed (Error code 3): #2230

Open metarutaiga opened 2 months ago

metarutaiga commented 2 months ago
[Vulkan] MoltenVK : VK_ERROR_INITIALIZATION_FAILED: Shader library compile failed (Error code 3):
program_source:37:25: error: no member named 'sample' in 'metal::sampler'
    color *= samDiffuse.sample(samDiffuseSmplr, float2(vary.UV0.x, 0.5));
             ~~~~~~~~~~ ^
program_source:37:32: error: use of undeclared identifier 'samDiffuseSmplr'
    color *= samDiffuse.sample(samDiffuseSmplr, float2(vary.UV0.x, 0.5));
                               ^
.
[Vulkan] MoltenVK : VK_ERROR_INVALID_SHADER_NV: Fragment shader function could not be compiled into pipeline. See previous logged error.

How to check what code generated ? I used glslang to compile HLSL into my project.

Here is my repo : https://github.com/metarutaiga/Minamoto To select xxGraphic and Vulkan

billhollings commented 2 months ago

How to check what code generated ?

If you set the MoltenVK configuration parameters:

MVK_CONFIG_LOG_LEVEL=3
MVK_CONFIG_DEBUG=1

MoltenVK will log the incoming SPIR-V and outgoing MSL.

The Configuring MoltenVK section of the Docs/MoltenVK_Runtime_UserGuide.md document explains how to set MoltenVK configuration parameters.

metarutaiga commented 2 months ago

It seems be not supported for DX9 HLSL style.

Converted MSL:
#pragma clang diagnostic ignored "-Wmissing-prototypes"

#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct Varying
{
    float4 Position;
    float4 Color;
    float2 UV0;
};

struct main0_out
{
    float4 _entryPointOutput [[color(0)]];
};

struct main0_in
{
    float4 vary_Color [[user(locn0)]];
    float2 vary_UV0 [[user(locn1)]];
};

static inline __attribute__((always_inline))
float4 _main(thread const Varying& vary, sampler samDiffuse)
{
    float4 varyColor = vary.Color;
    float4 color = float4(1.0);
    color = varyColor;
    color *= samDiffuse.sample(samDiffuseSmplr, float2(vary.UV0.x, 0.5));
    return color;
}

fragment main0_out main0(main0_in in [[stage_in]], sampler samDiffuse [[sampler(0)]], float4 gl_FragCoord [[position]])
{
    main0_out out = {};
    Varying vary;
    vary.Position = gl_FragCoord;
    vary.Color = in.vary_Color;
    vary.UV0 = in.vary_UV0;
    Varying param = vary;
    out._entryPointOutput = _main(param, samDiffuse);
    return out;
}

End MSL
Estimated original GLSL:
#version 450

struct Varying
{
    vec4 Position;
    vec4 Color;
    vec2 UV0;
};

layout(set = 0, binding = 0) uniform sampler samDiffuse;

layout(location = 0) in vec4 vary_Color;
layout(location = 1) in vec2 vary_UV0;
layout(location = 0) out vec4 _entryPointOutput;

vec4 _main(Varying vary)
{
    vec4 varyColor = vary.Color;
    vec4 color = vec4(1.0);
    color = varyColor;
    color *= texture(samDiffuse, vary.UV0.x);
    return color;
}

void main()
{
    Varying vary;
    vary.Position = gl_FragCoord;
    vary.Color = vary_Color;
    vary.UV0 = vary_UV0;
    Varying param = vary;
    _entryPointOutput = _main(param);
}

End GLSL
cdavis5e commented 2 months ago

Based on the shaders you posted, it looks like you're using a standalone sampler, which in SPIR-V must be combined with an image to be able to sample a texture, but you're using it as though it were a combined image-sampler. Your SPIR-V is likely invalid.

cdavis5e commented 2 months ago

Based on your initial post, this may be a bug in glslang's HLSL support.

cdavis5e commented 2 months ago

What does the original HLSL look like?

metarutaiga commented 2 months ago

I used HLSL10 style instead of HLSL style, and it works.

https://github.com/metarutaiga/xxGraphic/blob/61947a6a49911fc7eeae58e5d53bf334d3d04b2d/utility/xxMaterial.cpp#L789

cdavis5e commented 2 months ago

Yeah, that's almost certainly a bug in glslang. You should file a bug with them.