From ac5d3525f383a95f269fad82f01dc5bd7fbedddb Mon Sep 17 00:00:00 2001 From: Julius Trinkunas Date: Thu, 14 Nov 2024 20:20:47 +0200 Subject: [PATCH] Metal: enable mipmap support --- core/renderer/backend/Types.h | 3 +++ core/renderer/backend/metal/TextureMTL.mm | 23 ++++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/core/renderer/backend/Types.h b/core/renderer/backend/Types.h index a19455f4896b..f1433922e7e0 100644 --- a/core/renderer/backend/Types.h +++ b/core/renderer/backend/Types.h @@ -46,6 +46,9 @@ inline TargetBufferFlags getMRTColorFlag(size_t index) noexcept typedef TargetBufferFlags ClearFlag; +/// @note In Metal, mipmap filter is derived from `magFilter` value: ie `NEAREST_MIPMAP_LINEAR` and +/// `LINEAR_MIPMAP_LINEAR` will select `LINEAR` filter, while `NEAREST_MIPMAP_NEAREST` and +/// `LINEAR_MIPMAP_NEAREST` will select `NEAREST` filter. struct SamplerDescriptor { SamplerFilter magFilter = SamplerFilter::LINEAR; diff --git a/core/renderer/backend/metal/TextureMTL.mm b/core/renderer/backend/metal/TextureMTL.mm index 042d820b3c25..d142cde57450 100644 --- a/core/renderer/backend/metal/TextureMTL.mm +++ b/core/renderer/backend/metal/TextureMTL.mm @@ -32,7 +32,7 @@ of this software and associated documentation files (the "Software"), to deal namespace { -MTLSamplerAddressMode toMTLSamplerAddressMode(SamplerAddressMode mode) +static MTLSamplerAddressMode toMTLSamplerAddressMode(SamplerAddressMode mode) { MTLSamplerAddressMode ret = MTLSamplerAddressModeRepeat; switch (mode) @@ -53,7 +53,7 @@ MTLSamplerAddressMode toMTLSamplerAddressMode(SamplerAddressMode mode) return ret; } -MTLSamplerMinMagFilter toMTLSamplerMinMagFilter(SamplerFilter mode) +static MTLSamplerMinMagFilter toMTLSamplerMinMagFilter(SamplerFilter mode) { switch (mode) { @@ -70,7 +70,22 @@ MTLSamplerMinMagFilter toMTLSamplerMinMagFilter(SamplerFilter mode) } } -bool isColorRenderable(PixelFormat textureFormat) +static MTLSamplerMipFilter toMTLSamplerMipFilter(SamplerFilter mode) +{ + switch (mode) + { + case SamplerFilter::NEAREST_MIPMAP_LINEAR: + case SamplerFilter::LINEAR_MIPMAP_LINEAR: + return MTLSamplerMipFilterLinear; + case SamplerFilter::NEAREST_MIPMAP_NEAREST: + case SamplerFilter::LINEAR_MIPMAP_NEAREST: + return MTLSamplerMipFilterNearest; + default: + return MTLSamplerMipFilterNotMipmapped; + } +} + +static bool isColorRenderable(PixelFormat textureFormat) { switch (textureFormat) { @@ -168,6 +183,8 @@ bool isColorRenderable(PixelFormat textureFormat) descriptor.minFilter == SamplerFilter::DONT_CARE ? _minFilter : toMTLSamplerMinMagFilter(descriptor.minFilter); mtlDescriptor.magFilter = descriptor.magFilter == SamplerFilter::DONT_CARE ? _magFilter : toMTLSamplerMinMagFilter(descriptor.magFilter); + mtlDescriptor.mipFilter = + descriptor.magFilter == SamplerFilter::DONT_CARE ? _mipFilter : toMTLSamplerMipFilter(descriptor.magFilter); if (_mtlSamplerState) {