bkaradzic / bgfx

Cross-platform, graphics API agnostic, "Bring Your Own Engine/Framework" style rendering library.
https://bkaradzic.github.io/bgfx/overview.html
BSD 2-Clause "Simplified" License
15.01k stars 1.94k forks source link

Shaderc crash compiling gl_PrimitiveID shader to Metal #2792

Open Ravbug opened 2 years ago

Ravbug commented 2 years ago

Describe the bug This shader

#include <bgfx_shader.sh>

void main()
{
    gl_FragColor = vec4(gl_PrimitiveID,0,0,1);
}

compiled to the metal backend with the following flags results in Abort trap: 6: shaderc -f shader.fsh -o fragment.bin -i bgfx/src --type fragment --platform osx --varyingdef varying.def.sc --profile metal --debug

To Reproduce Steps to reproduce the behavior:

  1. Compile the shader above using the command line
  2. Observe the crash
  3. Retry for the DirectX or Vulkan backends - the compile will succeed

Expected behavior shaderc should print an error message containing the compile error, because this shader uses a feature (gl_PrimitiveID) that is not supported on the default shaderc metal version of 1.0.

Additional context shaderc does not crash if the --preprocess flag is passed.

When compiling shaderc in debug mode and attaching a debugger, the crash occurs when executing shaderc_metal.cpp:629

std::string source = msl.compile();

The error occurs inside spirv-cross (spirv_cross_error_handling.hpp:57):

image
#ifdef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
#if !defined(_MSC_VER) || defined(__clang__)
[[noreturn]]
#elif defined(_MSC_VER)
__declspec(noreturn)
#endif
inline void
report_and_abort(const std::string &msg)
{
#ifdef NDEBUG
    (void)msg;
#else
    fprintf(stderr, "There was a compiler error: %s\n", msg.c_str());
#endif
    fflush(stderr);
    abort();        // error here
}

It prints There was a compiler error: PrimitiveId on macOS requires MSL 2.2. but only if shaderc is compiled in debug mode. In release, you only get Abort trap: 6.

One possible fix is to disable the SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS define, which means that even in release if shaderc crashes in spirv-cross, the error message will print: libc++abi: terminating with uncaught exception of type spirv_cross::CompilerError: PrimitiveId on macOS requires MSL 2.2.

alemuntoni commented 9 months ago

I am experiencing the same issue on my MacBook Air M1, which supports Metal 3.

I was wondering if there is a way to specify the Metal version for shader compilation using shaderc, as it currently defaults to version 1.0. In my application, I rely on gl_PrimitiveID, so setting the target version to 2 would be sufficient for my requirements.

I have searched through the documentation, as well as other related issues and Discord channels, but I couldn't find any information on how to achieve this.