Fix texture swizzling crash with older Nvidia GPUs (#3779)

This commit is contained in:
Ben Doherty
2021-04-08 10:38:59 -07:00
committed by GitHub
parent a67505bbe2
commit ec339b0282
3 changed files with 21 additions and 6 deletions

View File

@@ -51,6 +51,9 @@ struct MetalContext {
id<MTLCommandBuffer> pendingCommandBuffer = nullptr;
id<MTLRenderCommandEncoder> currentRenderPassEncoder = nullptr;
// Supported features.
bool supportsTextureSwizzling = false;
// Tracks resources used by command buffers.
MetalResourceTracker resourceTracker;

View File

@@ -78,6 +78,19 @@ MetalDriver::MetalDriver(backend::MetalPlatform* platform) noexcept
<< [mContext->device.name cStringUsingEncoding:NSUTF8StringEncoding] << "'"
<< utils::io::endl;
// In order to support texture swizzling, the GPU needs to support it and the system be running
// macOS 10.15+ / iOS 13+.
mContext->supportsTextureSwizzling = false;
if (@available(macOS 10.15, iOS 13, *)) {
#if defined(IOS)
mContext->supportsTextureSwizzling =
[mContext->device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v1];
#else
mContext->supportsTextureSwizzling =
[mContext->device supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily2_v1];
#endif
}
mContext->commandQueue = [mContext->device newCommandQueue];
mContext->commandQueue.label = @"Filament";
mContext->pipelineStateCache.setDevice(mContext->device);
@@ -588,11 +601,7 @@ bool MetalDriver::isTextureFormatSupported(TextureFormat format) {
}
bool MetalDriver::isTextureSwizzleSupported() {
if (@available(macOS 10.15, iOS 13, *)) {
return true;
} else {
return false;
}
return mContext->supportsTextureSwizzling;
}
bool MetalDriver::isTextureFormatMipmappable(TextureFormat format) {

View File

@@ -502,7 +502,10 @@ MetalTexture::MetalTexture(MetalContext& context, SamplerType target, uint8_t le
a == TextureSwizzle::CHANNEL_3;
// If texture is nil, then it must be a SAMPLER_EXTERNAL texture. We'll ignore this case for now.
// TODO: implement swizzling for external textures.
if (!isDefaultSwizzle && texture) {
if (!isDefaultSwizzle && texture && context.supportsTextureSwizzling) {
// Even though we've already checked context.supportsTextureSwizzling, we still need to
// guard these calls with @availability, otherwise the API usage will generate compiler
// warnings.
if (@available(macOS 10.15, iOS 13, *)) {
NSUInteger slices = texture.arrayLength;
if (texture.textureType == MTLTextureTypeCube ||