2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
|
2020-09-01 13:24:45 +08:00
|
|
|
Copyright (c) 2020 c4games.com
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2021-12-28 11:05:34 +08:00
|
|
|
|
2020-09-11 00:10:44 +08:00
|
|
|
#include "UtilsMTL.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "DeviceMTL.h"
|
2020-09-25 11:07:56 +08:00
|
|
|
#include "DeviceInfoMTL.h"
|
2020-09-11 00:10:44 +08:00
|
|
|
#include "TextureMTL.h"
|
2020-09-25 11:07:56 +08:00
|
|
|
#include "../TextureUtils.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
|
|
|
|
|
|
|
CC_BACKEND_BEGIN
|
|
|
|
|
2021-12-28 11:05:34 +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
|
|
|
|
2021-12-28 11:05:34 +08:00
|
|
|
namespace {
|
|
|
|
MTLPixelFormat getSupportedDepthStencilFormat()
|
|
|
|
{
|
|
|
|
MTLPixelFormat pixelFormat = MTLPixelFormatDepth32Float_Stencil8;
|
2019-11-23 20:27:39 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
2021-12-28 11:05:34 +08:00
|
|
|
bool isDepth24Stencil8PixelFormatSupported = DeviceInfoMTL::supportD24S8();
|
|
|
|
if(isDepth24Stencil8PixelFormatSupported)
|
|
|
|
pixelFormat = MTLPixelFormatDepth24Unorm_Stencil8;
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
2021-12-28 11:05:34 +08:00
|
|
|
return pixelFormat;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2020-09-25 11:07:56 +08:00
|
|
|
struct GPUTextureFormatInfo
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-09-25 11:07:56 +08:00
|
|
|
MTLPixelFormat fmt;
|
|
|
|
MTLPixelFormat fmtSrgb;
|
|
|
|
};
|
|
|
|
|
2021-12-28 11:05:34 +08:00
|
|
|
static GPUTextureFormatInfo s_textureFormats[] =
|
|
|
|
{
|
2020-09-25 11:07:56 +08:00
|
|
|
/* pvrtc v1 */
|
2021-12-28 11:05:34 +08:00
|
|
|
{ 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
|
|
|
|
|
2020-09-25 11:07:56 +08:00
|
|
|
/* etc */
|
2021-12-28 11:05:34 +08:00
|
|
|
{ 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
|
|
|
|
|
2020-09-25 11:07:56 +08:00
|
|
|
/* s3tc */
|
2021-12-28 11:05:34 +08:00
|
|
|
{ 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
|
|
|
|
|
2020-09-25 11:07:56 +08:00
|
|
|
/* atc */
|
2021-12-28 11:05:34 +08:00
|
|
|
{ MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ATC_RGB
|
|
|
|
{ MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ATC_EXPLICIT_ALPHA
|
|
|
|
{ MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ATC_INTERPOLATED_ALPHA
|
|
|
|
|
|
|
|
/* astc */
|
2020-09-25 11:07:56 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2021-12-28 11:05:34 +08:00
|
|
|
{ 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
|
2020-09-25 11:07:56 +08:00
|
|
|
#else
|
2021-12-28 11:05:34 +08:00
|
|
|
{ MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC4x4
|
|
|
|
{ MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC5x5
|
|
|
|
{ MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC6x6
|
|
|
|
{ MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC8x5
|
|
|
|
{ MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC8x6
|
|
|
|
{ MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC8x8
|
|
|
|
{ MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC10x5
|
2020-09-25 11:07:56 +08:00
|
|
|
#endif
|
2021-12-28 11:05:34 +08:00
|
|
|
|
2020-09-25 11:07:56 +08:00
|
|
|
/* normal */
|
2021-12-28 11:05:34 +08:00
|
|
|
{ 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
|
|
|
|
{ MTLPixelFormatA8Unorm, MTLPixelFormatInvalid }, // A8
|
|
|
|
{ MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // L8
|
|
|
|
{ MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // LA8
|
|
|
|
|
2020-09-25 11:07:56 +08:00
|
|
|
/* depth stencil */
|
2021-12-28 11:05:34 +08:00
|
|
|
{ MTLPixelFormat(255/*Depth24Unorm_Stencil8*/), MTLPixelFormatInvalid }, // D24S8
|
2020-09-25 11:07:56 +08:00
|
|
|
};
|
2021-12-28 11:05:34 +08:00
|
|
|
static_assert(CC_ARRAYSIZE(s_textureFormats) == (int)PixelFormat::COUNT, "The OpenGL GPU texture format info table incomplete!");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-09-25 11:07:56 +08:00
|
|
|
void UtilsMTL::initGPUTextureFormats()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 11:05:34 +08:00
|
|
|
//on mac, D24S8 means MTLPixelFormatDepth24Unorm_Stencil8, while on ios it means MTLPixelFormatDepth32Float_Stencil8
|
2020-09-25 11:07:56 +08:00
|
|
|
auto& info = s_textureFormats[(int)PixelFormat::D24S8];
|
2021-12-28 11:05:34 +08:00
|
|
|
info.fmt = getSupportedDepthStencilFormat();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2020-09-11 00:10:44 +08:00
|
|
|
id<MTLTexture> UtilsMTL::getDefaultDepthStencilTexture()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 11:05:34 +08:00
|
|
|
if (! _defaultDepthStencilAttachmentTexture)
|
2020-09-11 00:10:44 +08:00
|
|
|
_defaultDepthStencilAttachmentTexture = UtilsMTL::createDepthStencilAttachmentTexture();
|
2021-12-28 11:05:34 +08:00
|
|
|
|
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
|
|
|
{
|
2021-12-28 11:05:34 +08:00
|
|
|
if (UTILS_LIKELY(textureFormat < PixelFormat::COUNT)) {
|
2020-09-25 11:07:56 +08:00
|
|
|
return s_textureFormats[(int)textureFormat].fmt;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2020-09-25 11:07:56 +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
|
|
|
{
|
2021-12-28 11:05:34 +08:00
|
|
|
auto CAMetalLayer = DeviceMTL::getCAMetalLayer();
|
2019-11-23 20:27:39 +08:00
|
|
|
MTLTextureDescriptor* textureDescriptor = [[MTLTextureDescriptor alloc] init];
|
2021-12-28 11:05:34 +08:00
|
|
|
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];
|
2021-12-28 11:05:34 +08:00
|
|
|
|
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
|
|
|
{
|
2021-12-28 11:05:34 +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];
|
|
|
|
}
|
|
|
|
|
2021-12-28 11:05:34 +08:00
|
|
|
void UtilsMTL::swizzleImage(unsigned char *image, std::size_t width, std::size_t height, MTLPixelFormat format)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 11:05:34 +08:00
|
|
|
if(!image)
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
2021-12-28 11:05:34 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto len = width * height;
|
2021-12-28 11:05:34 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_BACKEND_END
|