2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-01-04 12:36:20 +08:00
|
|
|
https://adxeproject.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.
|
|
|
|
****************************************************************************/
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "TextureMTL.h"
|
2020-09-11 00:10:44 +08:00
|
|
|
#include "UtilsMTL.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "base/ccMacros.h"
|
2020-09-25 11:07:56 +08:00
|
|
|
#include "../TextureUtils.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
CC_BACKEND_BEGIN
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
MTLSamplerAddressMode toMTLSamplerAddressMode(SamplerAddressMode mode)
|
|
|
|
{
|
|
|
|
MTLSamplerAddressMode ret = MTLSamplerAddressModeRepeat;
|
|
|
|
switch (mode)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
case SamplerAddressMode::REPEAT:
|
|
|
|
ret = MTLSamplerAddressModeRepeat;
|
|
|
|
break;
|
|
|
|
case SamplerAddressMode::MIRROR_REPEAT:
|
|
|
|
ret = MTLSamplerAddressModeMirrorRepeat;
|
|
|
|
break;
|
|
|
|
case SamplerAddressMode::CLAMP_TO_EDGE:
|
|
|
|
ret = MTLSamplerAddressModeClampToEdge;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CCASSERT(false, "Not supported sampler address mode!");
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
MTLSamplerMinMagFilter toMTLSamplerMinMagFilter(SamplerFilter mode)
|
|
|
|
{
|
|
|
|
switch (mode)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
case SamplerFilter::NEAREST:
|
|
|
|
case SamplerFilter::NEAREST_MIPMAP_LINEAR:
|
|
|
|
case SamplerFilter::NEAREST_MIPMAP_NEAREST:
|
|
|
|
return MTLSamplerMinMagFilterNearest;
|
|
|
|
case SamplerFilter::LINEAR:
|
|
|
|
case SamplerFilter::LINEAR_MIPMAP_LINEAR:
|
|
|
|
case SamplerFilter::LINEAR_MIPMAP_NEAREST:
|
|
|
|
return MTLSamplerMinMagFilterLinear;
|
|
|
|
case SamplerFilter::DONT_CARE:
|
|
|
|
return MTLSamplerMinMagFilterNearest;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isColorRenderable(PixelFormat textureFormat)
|
|
|
|
{
|
|
|
|
switch (textureFormat)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
case PixelFormat::RGBA8:
|
|
|
|
case PixelFormat::RGBA4:
|
|
|
|
case PixelFormat::RGB565:
|
|
|
|
case PixelFormat::RGB5A1:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-02-15 02:36:02 +08:00
|
|
|
/// CLASS TextureInfoMTL
|
|
|
|
id<MTLTexture> TextureInfoMTL::ensure(int index, int target)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (index < CC_META_TEXTURES)
|
|
|
|
{
|
2020-02-15 02:36:02 +08:00
|
|
|
id<MTLTexture>& mtlTexture = _mtlTextures[index];
|
2021-12-31 12:12:40 +08:00
|
|
|
if (mtlTexture)
|
|
|
|
return mtlTexture;
|
2020-02-15 02:36:02 +08:00
|
|
|
mtlTexture = createTexture(_mtlDevice, _descriptor, target);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (_maxIdx < index)
|
|
|
|
_maxIdx = index;
|
2020-02-15 02:36:02 +08:00
|
|
|
return mtlTexture;
|
|
|
|
}
|
|
|
|
return nil;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureInfoMTL::destroy()
|
|
|
|
{
|
|
|
|
if (_maxIdx == -1)
|
|
|
|
return;
|
2019-11-23 20:27:39 +08:00
|
|
|
id<MTLTexture> texture;
|
|
|
|
int i = 0;
|
2021-12-31 12:12:40 +08:00
|
|
|
while ((texture = _mtlTextures[i++]))
|
2019-11-23 20:27:39 +08:00
|
|
|
[texture release];
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (_mtlSamplerState)
|
|
|
|
{
|
2020-02-15 02:36:02 +08:00
|
|
|
[_mtlSamplerState release];
|
|
|
|
_mtlSamplerState = nil;
|
|
|
|
}
|
|
|
|
_maxIdx = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
id<MTLTexture> TextureInfoMTL::createTexture(id<MTLDevice> mtlDevice, const TextureDescriptor& descriptor, int target)
|
|
|
|
{
|
2020-09-11 00:10:44 +08:00
|
|
|
MTLPixelFormat pixelFormat = UtilsMTL::toMTLPixelFormat(descriptor.textureFormat);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (pixelFormat == MTLPixelFormatInvalid)
|
2020-02-15 02:36:02 +08:00
|
|
|
return nil;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2020-02-15 02:36:02 +08:00
|
|
|
MTLTextureDescriptor* textureDescriptor = nil;
|
2021-12-31 12:12:40 +08:00
|
|
|
switch (target)
|
|
|
|
{
|
2020-02-15 02:36:02 +08:00
|
|
|
case MTL_TEXTURE_2D:
|
|
|
|
textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pixelFormat
|
2021-12-31 12:12:40 +08:00
|
|
|
width:descriptor.width
|
|
|
|
height:descriptor.height
|
|
|
|
mipmapped:YES];
|
2020-02-15 02:36:02 +08:00
|
|
|
break;
|
|
|
|
case MTL_TEXTURE_CUBE:
|
2021-12-31 12:12:40 +08:00
|
|
|
textureDescriptor = [MTLTextureDescriptor textureCubeDescriptorWithPixelFormat:pixelFormat
|
|
|
|
size:descriptor.width
|
|
|
|
mipmapped:YES];
|
2020-02-15 02:36:02 +08:00
|
|
|
break;
|
2021-12-31 12:12:40 +08:00
|
|
|
default:
|
|
|
|
return nil;
|
2020-02-15 02:36:02 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2020-02-15 02:36:02 +08:00
|
|
|
if (TextureUsage::RENDER_TARGET == descriptor.textureUsage)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
// DepthStencil, and Multisample textures must be allocated with the MTLResourceStorageModePrivate resource
|
|
|
|
// option
|
|
|
|
if (PixelFormat::D24S8 == descriptor.textureFormat && target == MTL_TEXTURE_2D)
|
2020-02-15 02:36:02 +08:00
|
|
|
textureDescriptor.resourceOptions = MTLResourceStorageModePrivate;
|
|
|
|
textureDescriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2020-02-15 02:36:02 +08:00
|
|
|
return [mtlDevice newTextureWithDescriptor:textureDescriptor];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureInfoMTL::recreateSampler(const SamplerDescriptor& descriptor)
|
2020-02-15 02:36:02 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
MTLSamplerDescriptor* mtlDescriptor = [MTLSamplerDescriptor new];
|
|
|
|
mtlDescriptor.sAddressMode = descriptor.sAddressMode == SamplerAddressMode::DONT_CARE
|
|
|
|
? _sAddressMode
|
|
|
|
: toMTLSamplerAddressMode(descriptor.sAddressMode);
|
|
|
|
mtlDescriptor.tAddressMode = descriptor.tAddressMode == SamplerAddressMode::DONT_CARE
|
|
|
|
? _tAddressMode
|
|
|
|
: toMTLSamplerAddressMode(descriptor.tAddressMode);
|
|
|
|
|
|
|
|
mtlDescriptor.minFilter =
|
|
|
|
descriptor.minFilter == SamplerFilter::DONT_CARE ? _minFilter : toMTLSamplerMinMagFilter(descriptor.minFilter);
|
|
|
|
mtlDescriptor.magFilter =
|
|
|
|
descriptor.magFilter == SamplerFilter::DONT_CARE ? _magFilter : toMTLSamplerMinMagFilter(descriptor.magFilter);
|
|
|
|
|
|
|
|
if (_mtlSamplerState)
|
2020-02-15 02:36:02 +08:00
|
|
|
{
|
|
|
|
[_mtlSamplerState release];
|
|
|
|
_mtlSamplerState = nil;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2020-02-15 02:36:02 +08:00
|
|
|
_sAddressMode = mtlDescriptor.sAddressMode;
|
|
|
|
_tAddressMode = mtlDescriptor.tAddressMode;
|
2021-12-31 12:12:40 +08:00
|
|
|
_minFilter = mtlDescriptor.minFilter;
|
|
|
|
_magFilter = mtlDescriptor.magFilter;
|
|
|
|
_mipFilter = mtlDescriptor.mipFilter;
|
|
|
|
|
2020-02-15 02:36:02 +08:00
|
|
|
_mtlSamplerState = [_mtlDevice newSamplerStateWithDescriptor:mtlDescriptor];
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2020-02-15 02:36:02 +08:00
|
|
|
[mtlDescriptor release];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CLASS TextureMTL
|
2021-12-31 12:12:40 +08:00
|
|
|
TextureMTL::TextureMTL(id<MTLDevice> mtlDevice, const TextureDescriptor& descriptor) : _textureInfo(mtlDevice)
|
2020-02-15 02:36:02 +08:00
|
|
|
{
|
|
|
|
updateTextureDescriptor(descriptor);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
TextureMTL::~TextureMTL() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureMTL::updateSamplerDescriptor(const SamplerDescriptor& sampler)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-02-15 02:36:02 +08:00
|
|
|
_textureInfo.recreateSampler(sampler);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureMTL::updateTextureDescriptor(const cocos2d::backend::TextureDescriptor& descriptor, int index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
TextureBackend::updateTextureDescriptor(descriptor, index);
|
2020-02-15 02:58:41 +08:00
|
|
|
|
|
|
|
_textureInfo._descriptor = descriptor;
|
2020-02-15 02:36:02 +08:00
|
|
|
_textureInfo.ensure(index, MTL_TEXTURE_2D);
|
|
|
|
updateSamplerDescriptor(descriptor.samplerDescriptor);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2020-09-25 11:07:56 +08:00
|
|
|
_textureInfo._bytesPerRow = PixelFormatUtils::computeRowPitch(descriptor.textureFormat, descriptor.width);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureMTL::updateData(uint8_t* data, std::size_t width, std::size_t height, std::size_t level, int index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
updateSubData(0, 0, width, height, level, data, index);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureMTL::updateSubData(std::size_t xoffset,
|
|
|
|
std::size_t yoffset,
|
|
|
|
std::size_t width,
|
|
|
|
std::size_t height,
|
|
|
|
std::size_t level,
|
|
|
|
uint8_t* data,
|
|
|
|
int index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-02-15 02:36:02 +08:00
|
|
|
auto mtlTexture = _textureInfo.ensure(index, MTL_TEXTURE_2D);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!mtlTexture)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MTLRegion region = {
|
2019-11-23 20:27:39 +08:00
|
|
|
{xoffset, yoffset, 0}, // MTLOrigin
|
|
|
|
{width, height, 1} // MTLSize
|
|
|
|
};
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2020-09-25 11:07:56 +08:00
|
|
|
auto bytesPerRow = PixelFormatUtils::computeRowPitch(_textureFormat, width);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
[mtlTexture replaceRegion:region mipmapLevel:level withBytes:data bytesPerRow:bytesPerRow];
|
|
|
|
|
|
|
|
if (!_hasMipmaps && level > 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
_hasMipmaps = true;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureMTL::updateCompressedData(uint8_t* data,
|
|
|
|
std::size_t width,
|
|
|
|
std::size_t height,
|
|
|
|
std::size_t dataLen,
|
|
|
|
std::size_t level,
|
|
|
|
int index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2019-12-01 23:26:11 +08:00
|
|
|
updateCompressedSubData(0, 0, width, height, dataLen, level, data, index);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureMTL::updateCompressedSubData(std::size_t xoffset,
|
|
|
|
std::size_t yoffset,
|
|
|
|
std::size_t width,
|
|
|
|
std::size_t height,
|
|
|
|
std::size_t dataLen,
|
|
|
|
std::size_t level,
|
|
|
|
uint8_t* data,
|
|
|
|
int index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2019-12-01 23:26:11 +08:00
|
|
|
updateSubData(xoffset, yoffset, width, height, level, data, index);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureMTL::generateMipmaps()
|
|
|
|
{
|
|
|
|
if (TextureUsage::RENDER_TARGET == _textureUsage || isColorRenderable(_textureFormat) == false)
|
|
|
|
return;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
if (!_hasMipmaps)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_hasMipmaps = true;
|
2020-09-21 22:10:50 +08:00
|
|
|
UtilsMTL::generateMipmaps(reinterpret_cast<id<MTLTexture>>(this->getHandler()));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 02:36:02 +08:00
|
|
|
/// CLASS TextureCubeMTL
|
2021-12-31 12:12:40 +08:00
|
|
|
TextureCubeMTL::TextureCubeMTL(id<MTLDevice> mtlDevice, const TextureDescriptor& descriptor) : _textureInfo(mtlDevice)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
updateTextureDescriptor(descriptor);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
TextureCubeMTL::~TextureCubeMTL() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureCubeMTL::updateTextureDescriptor(const cocos2d::backend::TextureDescriptor& descriptor, int index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-02-15 02:58:41 +08:00
|
|
|
TextureBackend::updateTextureDescriptor(descriptor, index);
|
2020-02-15 02:36:02 +08:00
|
|
|
|
|
|
|
_textureInfo._descriptor = descriptor;
|
|
|
|
_textureInfo.ensure(index, MTL_TEXTURE_CUBE);
|
2019-11-23 20:27:39 +08:00
|
|
|
updateSamplerDescriptor(descriptor.samplerDescriptor);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2020-09-25 11:07:56 +08:00
|
|
|
_textureInfo._bytesPerRow = PixelFormatUtils::computeRowPitch(descriptor.textureFormat, descriptor.width);
|
2021-12-31 12:12:40 +08:00
|
|
|
_bytesPerImage = _textureInfo._bytesPerRow * descriptor.width;
|
|
|
|
_region = MTLRegionMake2D(0, 0, descriptor.width, descriptor.height);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureCubeMTL::updateSamplerDescriptor(const SamplerDescriptor& sampler)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-02-15 02:36:02 +08:00
|
|
|
_textureInfo.recreateSampler(sampler);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void TextureCubeMTL::updateFaceData(TextureCubeFace side, void* data, int index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
NSUInteger slice = static_cast<int>(side);
|
2021-12-31 12:12:40 +08:00
|
|
|
auto mtlTexture = _textureInfo.ensure(index, MTL_TEXTURE_CUBE);
|
|
|
|
if (!mtlTexture)
|
|
|
|
return;
|
2020-02-15 02:36:02 +08:00
|
|
|
|
|
|
|
[mtlTexture replaceRegion:_region
|
2021-12-31 12:12:40 +08:00
|
|
|
mipmapLevel:0
|
|
|
|
slice:slice
|
|
|
|
withBytes:data
|
|
|
|
bytesPerRow:_textureInfo._bytesPerRow
|
|
|
|
bytesPerImage:_bytesPerImage];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCubeMTL::generateMipmaps()
|
|
|
|
{
|
|
|
|
if (TextureUsage::RENDER_TARGET == _textureUsage || isColorRenderable(_textureFormat) == false)
|
|
|
|
return;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
if (!_hasMipmaps)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_hasMipmaps = true;
|
2020-09-21 22:10:50 +08:00
|
|
|
UtilsMTL::generateMipmaps(reinterpret_cast<id<MTLTexture>>(this->getHandler()));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_BACKEND_END
|