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

210 lines
8.5 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) 2019-present Axmol Engine contributors (see AUTHORS.md).
2019-11-23 20:27:39 +08:00
https://axmol.dev/
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"
#include "DriverMTL.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 = DriverMTL::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)
{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
{
Release 2.1.5 (#2076) * Fix unexpected libpng used * Fix string format incorrect for tests * Fix #1751, use coroutine control AutoTest flow * Update CHANGELOG.md * Added OpenType font (.otf) to the noCompress list. (#2077) * Update 1k & copyright notice in some sources * Move doctest to axmol 3rdparty * Fix ci * Update 1kdist to v90 * Update 1kiss.ps1 * DrawNodeV2 0.95.1 (#2079) * Rename remaining legacy engine related spells and improve code style * Update 3rdparty README.md * Fix checkReallySupportsASTC does not work on ios device reported by @BIGCATDOG in https://github.com/axmolengine/axmol/issues/2078 * Fix ci * FastRNG: add missing include for AXASSERT (#2081) * Delete unused files * Improve FileUtils - Rename FileUtils::createDirectory to FileUtils::createDirectories - Use splitpath_cb to optimize FileUtils::createDirectories - Rename FileUtils::getFileShortName to FileUtils::getPathBaseName - Rename FileUtils::getFileExtension to FileUtils::getPathExtension - Add FileUtils::getPathDirName - Add FileUtils::getPathBaseNameNoExtension - Mark all renamed FileUtils stubs old name deprecated - Mark all FileUtils offthread APIs deprecated * Update box2d to v2.4.2 * Disable /sdl checks explicitly for winuwp For axmol deprecated policy, we need disable /sdl checks explicitly to avoid compiler traits invoking deprecated functions as error * Update cppwinrt to 2.0.240405.15 * Update simdjson to 3.10.0 * Fix box2d testbed compile error * Improve file path to url * Fix FileUtils::createDirectories unix logic * axmol-cmdline: remove arch suffix for host build output directory * Update CHANGELOG.md * Update lua bindings --------- Co-authored-by: Dani Alias <danielgutierrezalias@gmail.com> Co-authored-by: aismann <icesoft@freenet.de> Co-authored-by: smilediver <smilediver@outlook.com>
2024-08-11 21:11:35 +08:00
if (AX_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::DriverMTL::getCAMetalLayer() setDrawableSize:CGSizeMake(width, height)];
2019-11-23 20:27:39 +08:00
[_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 = DriverMTL::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<DriverMTL*>(DriverMTL::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