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.05k stars 1.94k forks source link

Metal doesn't work on iOS devices based on A7 and A8 CPU #1964

Open BorisShir opened 4 years ago

BorisShir commented 4 years ago

Application crashes on iOS devices based on A7 and A8 CPU (iPhone 5S, iPhone 6, iPad Mini 2, iPad Mini 4 and so on).

The error code which I’m seeing is

Error Domain=AGXMetalA7 Code=1 "sampler argument for sample_compare must be known at compile time" UserInfo={NSLocalizedDescription=sampler argument for sample_compare must be known at compile time}

This happens in bgfx⁩/src⁩/renderer_mtl.h in method id<MTLRenderPipelineState> newRenderPipelineStateWithDescriptor as a result app crashes in void setRenderPipelineState(id<MTLRenderPipelineState> _pipelineState)

I’ve tried to google and it appears that this message means that some features which require MTLFeatureSet_iOS_GPUFamily3_v1 support are being used.

attilaz commented 4 years ago

Yes, you need MTLFeatureSet_iOS_GPUFamily3_v1 to set compare sampler support in the objective-c api. https://developer.apple.com/documentation/metal/mtlsamplerdescriptor/1516001-comparefunction?language=objc

You can workaround this limitation by not using compare sampler and doing compare manually in shader (https://github.com/bkaradzic/bgfx/blob/master/examples/15-shadowmaps-simple/fs_sms_shadow.sh#L34)

or using constant sampler inside the shader. constexpr sampler s(address::clamp_to_zero, filter::linear, compare_func::less); I don't know if it is possible to generate this kind of sampler with shaderc (glslang/spirv-cross).

One thing that could be changed in renderer_mtl.mm to set BGFX_CAPS_TEXTURE_COMPARE_ALL and BGFX_CAPS_TEXTURE_COMPARE_LEQUAL caps only when api side setting is possible. @bkaradzic https://github.com/bkaradzic/bgfx/blob/master/src/renderer_mtl.mm#L490

BorisShir commented 4 years ago

I've tried to comment out both BGFX_CAPS_TEXTURE_COMPARE_ALL and BGFX_CAPS_TEXTURE_COMPARE_LEQUAL caps and this doesn't help. I'm still seeing the same error message and crash.

attilaz commented 4 years ago

Commenting out caps does nothing inside bgfx. It is helpful for user to get info about what supported using bgfx::getCaps().

As these features are not supported by the metal api (for that feature level), there is no magic solution inside bgfx. You have to modify your shaders or at least shaderc to create a constexpr sampler. This could help: https://github.com/KhronosGroup/SPIRV-Cross/issues/533 It would require modification to shaderc and I am not sure how to pass info about to make a sampler constexpr, maybe some command line flag or naming convention?