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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-09-11 00:10:44 +08:00
|
|
|
#include "UtilsMTL.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "DeviceMTL.h"
|
2020-09-11 00:10:44 +08:00
|
|
|
#include "TextureMTL.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
|
|
|
|
|
|
|
#define COLOR_ATTAHCMENT_PIXEL_FORMAT MTLPixelFormatBGRA8Unorm
|
|
|
|
|
|
|
|
CC_BACKEND_BEGIN
|
|
|
|
|
2020-09-11 00:10:44 +08:00
|
|
|
id<MTLTexture> UtilsMTL::_defaultColorAttachmentTexture = nil;
|
|
|
|
id<MTLTexture> UtilsMTL::_defaultDepthStencilAttachmentTexture = nil;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
MTLPixelFormat getSupportedDepthStencilFormat()
|
|
|
|
{
|
|
|
|
MTLPixelFormat pixelFormat = MTLPixelFormatDepth32Float_Stencil8;
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
|
|
|
bool isDepth24Stencil8PixelFormatSupported = Configuration::getInstance()->supportsOESPackedDepthStencil();
|
|
|
|
if(isDepth24Stencil8PixelFormatSupported)
|
|
|
|
pixelFormat = MTLPixelFormatDepth24Unorm_Stencil8;
|
|
|
|
#endif
|
|
|
|
return pixelFormat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 00:10:44 +08:00
|
|
|
MTLPixelFormat UtilsMTL::getDefaultDepthStencilAttachmentPixelFormat()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return getSupportedDepthStencilFormat();
|
|
|
|
}
|
|
|
|
|
2020-09-11 00:10:44 +08:00
|
|
|
MTLPixelFormat UtilsMTL::getDefaultColorAttachmentPixelFormat()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return COLOR_ATTAHCMENT_PIXEL_FORMAT;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
switch (textureFormat)
|
|
|
|
{
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
case PixelFormat::MTL_ABGR4:
|
|
|
|
return MTLPixelFormatABGR4Unorm;
|
|
|
|
case PixelFormat::MTL_BGR5A1:
|
|
|
|
return MTLPixelFormatBGR5A1Unorm;
|
|
|
|
case PixelFormat::MTL_B5G6R5:
|
|
|
|
return MTLPixelFormatB5G6R5Unorm;
|
|
|
|
case PixelFormat::PVRTC4A:
|
|
|
|
return MTLPixelFormatPVRTC_RGBA_4BPP;
|
|
|
|
case PixelFormat::PVRTC4:
|
|
|
|
return MTLPixelFormatPVRTC_RGB_4BPP;
|
|
|
|
case PixelFormat::PVRTC2A:
|
|
|
|
return MTLPixelFormatPVRTC_RGBA_2BPP;
|
|
|
|
case PixelFormat::PVRTC2:
|
|
|
|
return MTLPixelFormatPVRTC_RGB_2BPP;
|
2020-09-01 11:43:03 +08:00
|
|
|
case PixelFormat::ETC1:
|
2020-08-31 21:04:47 +08:00
|
|
|
case PixelFormat::ETC2_RGB:
|
2019-11-23 20:27:39 +08:00
|
|
|
return MTLPixelFormatETC2_RGB8;
|
2020-08-31 21:04:47 +08:00
|
|
|
case PixelFormat::ETC2_RGBA:
|
|
|
|
return MTLPixelFormatEAC_RGBA8;
|
2020-09-03 18:01:40 +08:00
|
|
|
case PixelFormat::ASTC4x4:
|
2020-02-15 20:51:20 +08:00
|
|
|
return MTLPixelFormatASTC_4x4_LDR;
|
2020-09-03 18:01:40 +08:00
|
|
|
case PixelFormat::ASTC6x6:
|
|
|
|
return MTLPixelFormatASTC_6x6_LDR;
|
|
|
|
case PixelFormat::ASTC8x8:
|
2020-02-15 20:51:20 +08:00
|
|
|
return MTLPixelFormatASTC_8x8_LDR;
|
2019-11-23 20:27:39 +08:00
|
|
|
#else
|
|
|
|
case PixelFormat::S3TC_DXT1:
|
|
|
|
return MTLPixelFormatBC1_RGBA;
|
|
|
|
case PixelFormat::S3TC_DXT3:
|
|
|
|
return MTLPixelFormatBC2_RGBA;
|
|
|
|
case PixelFormat::S3TC_DXT5:
|
|
|
|
return MTLPixelFormatBC3_RGBA;
|
|
|
|
#endif
|
|
|
|
case PixelFormat::RGBA8888:
|
|
|
|
return MTLPixelFormatRGBA8Unorm;
|
|
|
|
// Should transfer the data to match pixel format when updating data.
|
|
|
|
case PixelFormat::RGB888:
|
|
|
|
return MTLPixelFormatRGBA8Unorm;
|
|
|
|
case PixelFormat::A8:
|
|
|
|
return MTLPixelFormatA8Unorm;
|
|
|
|
case PixelFormat::BGRA8888:
|
|
|
|
return MTLPixelFormatBGRA8Unorm;
|
|
|
|
|
|
|
|
//on mac, D24S8 means MTLPixelFormatDepth24Unorm_Stencil8, while on ios it means MTLPixelFormatDepth32Float_Stencil8
|
|
|
|
case PixelFormat::D24S8:
|
|
|
|
return getSupportedDepthStencilFormat();
|
|
|
|
case PixelFormat::DEFAULT:
|
|
|
|
return COLOR_ATTAHCMENT_PIXEL_FORMAT;
|
|
|
|
case PixelFormat::NONE:
|
|
|
|
default:
|
|
|
|
return MTLPixelFormatInvalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
MTLTextureDescriptor* textureDescriptor = [[MTLTextureDescriptor alloc] init];
|
|
|
|
textureDescriptor.width = CAMetalLayer.drawableSize.width;
|
|
|
|
textureDescriptor.height = CAMetalLayer.drawableSize.height;
|
|
|
|
textureDescriptor.pixelFormat = getSupportedDepthStencilFormat();
|
|
|
|
textureDescriptor.resourceOptions = MTLResourceStorageModePrivate;
|
|
|
|
textureDescriptor.usage = MTLTextureUsageRenderTarget;
|
|
|
|
auto ret = [CAMetalLayer.device newTextureWithDescriptor:textureDescriptor];
|
|
|
|
[textureDescriptor release];
|
|
|
|
|
|
|
|
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];
|
|
|
|
id<MTLBlitCommandEncoder> commandEncoder = [commandBuffer blitCommandEncoder];
|
|
|
|
[commandEncoder generateMipmapsForTexture:texture];
|
|
|
|
[commandEncoder endEncoding];
|
|
|
|
[commandBuffer commit];
|
|
|
|
}
|
|
|
|
|
2020-09-11 00:10:44 +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
|
|
|
{
|
|
|
|
if(!image)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_BACKEND_END
|