axmol/core/renderer/backend/metal/UtilsMTL.mm

215 lines
8.9 KiB
Plaintext
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2020 C4games Ltd.
Copyright (c) 2021-2022 Bytedance Inc.
2019-11-23 20:27:39 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2020-09-11 00:10:44 +08:00
#include "UtilsMTL.h"
2019-11-23 20:27:39 +08:00
#include "DeviceMTL.h"
#include "DeviceInfoMTL.h"
2020-09-11 00:10:44 +08:00
#include "TextureMTL.h"
#include "../PixelFormatUtils.h"
#include "base/Configuration.h"
2019-11-23 20:27:39 +08:00
NS_AX_BACKEND_BEGIN
2019-11-23 20:27:39 +08:00
id<MTLTexture> UtilsMTL::_defaultColorAttachmentTexture = nil;
2020-09-11 00:10:44 +08:00
id<MTLTexture> UtilsMTL::_defaultDepthStencilAttachmentTexture = nil;
2019-11-23 20:27:39 +08:00
namespace
{
MTLPixelFormat getSupportedDepthStencilFormat()
{
MTLPixelFormat pixelFormat = MTLPixelFormatDepth32Float_Stencil8;
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_MAC)
bool isDepth24Stencil8PixelFormatSupported = DeviceInfoMTL::supportD24S8();
if (isDepth24Stencil8PixelFormatSupported)
pixelFormat = MTLPixelFormatDepth24Unorm_Stencil8;
2019-11-23 20:27:39 +08:00
#endif
return pixelFormat;
}
2019-11-23 20:27:39 +08:00
}
struct GPUTextureFormatInfo
2019-11-23 20:27:39 +08:00
{
MTLPixelFormat fmt;
MTLPixelFormat fmtSrgb;
};
static GPUTextureFormatInfo s_textureFormats[] = {
/* pvrtc v1 */
{MTLPixelFormat(162 /*PVRTC_RGB_4BPP*/), MTLPixelFormat(163 /*PVRTC_RGB_4BPP_sRGB*/)}, // PTC14
{MTLPixelFormat(166 /*PVRTC_RGBA_4BPP*/), MTLPixelFormat(167 /*PVRTC_RGBA_4BPP_sRGB*/)}, // PTC14A
{MTLPixelFormat(160 /*PVRTC_RGB_2BPP*/), MTLPixelFormat(161 /*PVRTC_RGB_2BPP_sRGB*/)}, // PTC12
{MTLPixelFormat(164 /*PVRTC_RGBA_2BPP*/), MTLPixelFormat(165 /*PVRTC_RGBA_2BPP_sRGB*/)}, // PTC12A
/* etc */
{MTLPixelFormat(180 /*ETC2_RGB8*/), MTLPixelFormat(181 /*ETC2_RGB8_sRGB*/)}, // ETC1
{MTLPixelFormat(180 /*ETC2_RGB8*/), MTLPixelFormat(181 /*ETC2_RGB8_sRGB*/)}, // ETC2
{MTLPixelFormat(178 /*EAC_RGBA8*/), MTLPixelFormat(179 /*EAC_RGBA8_sRGB*/)}, // ETC2A
/* s3tc */
{MTLPixelFormat(130 /*BC1_RGBA*/), MTLPixelFormat(131 /*BC1_RGBA_sRGB*/)}, // S3TC_DXT1/BC1
{MTLPixelFormat(132 /*BC2_RGBA*/), MTLPixelFormat(133 /*BC2_RGBA_sRGB*/)}, // S3TC_DXT3/BC2
{MTLPixelFormat(134 /*BC3_RGBA*/), MTLPixelFormat(135 /*BC3_RGBA_sRGB*/)}, // S3TC_DXT5/BC3
/* atc */
{MTLPixelFormatInvalid, MTLPixelFormatInvalid}, // ATC_RGB
{MTLPixelFormatInvalid, MTLPixelFormatInvalid}, // ATC_EXPLICIT_ALPHA
{MTLPixelFormatInvalid, MTLPixelFormatInvalid}, // ATC_INTERPOLATED_ALPHA
/* astc */
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_IOS)
{MTLPixelFormatASTC_4x4_LDR, MTLPixelFormatASTC_4x4_sRGB}, // ASTC4x4
{MTLPixelFormatASTC_5x5_LDR, MTLPixelFormatASTC_5x5_sRGB}, // ASTC5x5
{MTLPixelFormatASTC_6x6_LDR, MTLPixelFormatASTC_6x6_sRGB}, // ASTC6x6
{MTLPixelFormatASTC_8x5_LDR, MTLPixelFormatASTC_8x5_sRGB}, // ASTC8x5
{MTLPixelFormatASTC_8x6_LDR, MTLPixelFormatASTC_8x6_sRGB}, // ASTC8x6
{MTLPixelFormatASTC_8x8_LDR, MTLPixelFormatASTC_8x8_sRGB}, // ASTC8x8
{MTLPixelFormatASTC_10x5_LDR, MTLPixelFormatASTC_10x5_sRGB}, // ASTC10x5
#else
{MTLPixelFormatInvalid, MTLPixelFormatInvalid}, // ASTC4x4
{MTLPixelFormatInvalid, MTLPixelFormatInvalid}, // ASTC5x5
{MTLPixelFormatInvalid, MTLPixelFormatInvalid}, // ASTC6x6
{MTLPixelFormatInvalid, MTLPixelFormatInvalid}, // ASTC8x5
{MTLPixelFormatInvalid, MTLPixelFormatInvalid}, // ASTC8x6
{MTLPixelFormatInvalid, MTLPixelFormatInvalid}, // ASTC8x8
{MTLPixelFormatInvalid, MTLPixelFormatInvalid}, // ASTC10x5
#endif
/* normal */
{MTLPixelFormatRGBA8Unorm, MTLPixelFormatRGBA8Unorm_sRGB}, // RGBA8
{MTLPixelFormatBGRA8Unorm, MTLPixelFormatBGRA8Unorm_sRGB}, // BGRA8
{MTLPixelFormatInvalid, MTLPixelFormatInvalid}, // RGB8
{MTLPixelFormat(40 /*B5G6R5Unorm*/), MTLPixelFormatInvalid}, // R5G6B5
{MTLPixelFormat(42 /*ABGR4Unorm*/), MTLPixelFormatInvalid}, // RGBA4
{MTLPixelFormat(41 /*A1BGR5Unorm*/), MTLPixelFormatInvalid}, // RGB5A1
{MTLPixelFormatR8Unorm, MTLPixelFormatInvalid}, // R8 In Shader: (R,0,0,0)
{MTLPixelFormatRG8Unorm, MTLPixelFormatInvalid}, // RG8 In Shader: (R,G,0,0)
{MTLPixelFormatA8Unorm, MTLPixelFormatInvalid}, // A8 In Shader: (0,0,0,A) deprecated
{MTLPixelFormatInvalid, MTLPixelFormatInvalid}, // L8 In Shader: (L,L,L,1) deprecated
{MTLPixelFormatInvalid, MTLPixelFormatInvalid}, // LA8 In Shader: (L,L,L,A) deprecated
{MTLPixelFormatRGBA32Float, MTLPixelFormatInvalid}, // RGBA32Fs
/* depth stencil */
{MTLPixelFormat(255 /*Depth24Unorm_Stencil8*/), MTLPixelFormatInvalid}, // D24S8
};
2022-07-16 10:43:05 +08:00
static_assert(AX_ARRAYSIZE(s_textureFormats) == (int)PixelFormat::COUNT,
"The OpenGL GPU texture format info table incomplete!");
2019-11-23 20:27:39 +08:00
void UtilsMTL::initGPUTextureFormats()
2019-11-23 20:27:39 +08:00
{
// on mac, D24S8 means MTLPixelFormatDepth24Unorm_Stencil8, while on ios it means
// MTLPixelFormatDepth32Float_Stencil8
auto& info = s_textureFormats[(int)PixelFormat::D24S8];
info.fmt = getSupportedDepthStencilFormat();
2019-11-23 20:27:39 +08:00
}
2022-11-01 16:02:13 +08:00
MTLPixelFormat UtilsMTL::getDefaultColorAttachmentPixelFormat()
{
return MTLPixelFormatBGRA8Unorm;
}
MTLPixelFormat UtilsMTL::getDefaultDepthStencilAttachmentPixelFormat()
{
return getSupportedDepthStencilFormat();
}
2020-09-11 00:10:44 +08:00
id<MTLTexture> UtilsMTL::getDefaultDepthStencilTexture()
2019-11-23 20:27:39 +08:00
{
if (!_defaultDepthStencilAttachmentTexture)
2020-09-11 00:10:44 +08:00
_defaultDepthStencilAttachmentTexture = UtilsMTL::createDepthStencilAttachmentTexture();
2019-11-23 20:27:39 +08:00
return _defaultDepthStencilAttachmentTexture;
}
2020-09-11 00:10:44 +08:00
void UtilsMTL::updateDefaultColorAttachmentTexture(id<MTLTexture> texture)
2019-11-23 20:27:39 +08:00
{
2020-09-11 00:10:44 +08:00
UtilsMTL::_defaultColorAttachmentTexture = texture;
2019-11-23 20:27:39 +08:00
}
2020-09-11 00:10:44 +08:00
MTLPixelFormat UtilsMTL::toMTLPixelFormat(PixelFormat textureFormat)
2019-11-23 20:27:39 +08:00
{
if (UTILS_LIKELY(textureFormat < PixelFormat::COUNT))
{
return s_textureFormats[(int)textureFormat].fmt;
2019-11-23 20:27:39 +08:00
}
return MTLPixelFormatInvalid;
2019-11-23 20:27:39 +08:00
}
2020-09-11 00:10:44 +08:00
void UtilsMTL::resizeDefaultAttachmentTexture(std::size_t width, std::size_t height)
2019-11-23 20:27:39 +08:00
{
[backend::DeviceMTL::getCAMetalLayer() setDrawableSize:CGSizeMake(width, height)];
[_defaultDepthStencilAttachmentTexture release];
2020-09-11 00:10:44 +08:00
_defaultDepthStencilAttachmentTexture = UtilsMTL::createDepthStencilAttachmentTexture();
2019-11-23 20:27:39 +08:00
}
2020-09-11 00:10:44 +08:00
id<MTLTexture> UtilsMTL::createDepthStencilAttachmentTexture()
2019-11-23 20:27:39 +08:00
{
auto CAMetalLayer = DeviceMTL::getCAMetalLayer();
2019-11-23 20:27:39 +08:00
MTLTextureDescriptor* textureDescriptor = [[MTLTextureDescriptor alloc] init];
textureDescriptor.width = CAMetalLayer.drawableSize.width;
textureDescriptor.height = CAMetalLayer.drawableSize.height;
textureDescriptor.pixelFormat = s_textureFormats[(int)PixelFormat::D24S8].fmt;
textureDescriptor.resourceOptions = MTLResourceStorageModePrivate;
textureDescriptor.usage = MTLTextureUsageRenderTarget;
auto ret = [CAMetalLayer.device newTextureWithDescriptor:textureDescriptor];
2019-11-23 20:27:39 +08:00
[textureDescriptor release];
2019-11-23 20:27:39 +08:00
return ret;
}
2020-09-11 00:10:44 +08:00
void UtilsMTL::generateMipmaps(id<MTLTexture> texture)
2019-11-23 20:27:39 +08:00
{
auto commandQueue = static_cast<DeviceMTL*>(DeviceMTL::getInstance())->getMTLCommandQueue();
auto commandBuffer = [commandQueue commandBuffer];
2019-11-23 20:27:39 +08:00
id<MTLBlitCommandEncoder> commandEncoder = [commandBuffer blitCommandEncoder];
[commandEncoder generateMipmapsForTexture:texture];
[commandEncoder endEncoding];
[commandBuffer commit];
}
void UtilsMTL::swizzleImage(unsigned char* image, std::size_t width, std::size_t height, MTLPixelFormat format)
2019-11-23 20:27:39 +08:00
{
if (!image)
2019-11-23 20:27:39 +08:00
return;
2019-11-23 20:27:39 +08:00
auto len = width * height;
switch (format)
{
// convert to RGBA
case MTLPixelFormatBGRA8Unorm:
for (int i = 0; i < len; i++)
{
unsigned char temp = image[i * 4];
image[i * 4] = image[i * 4 + 2];
image[i * 4 + 2] = temp;
}
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
}
NS_AX_BACKEND_END