2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
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-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../Texture.h"
|
|
|
|
#include "DeviceMTL.h"
|
|
|
|
#import <Metal/Metal.h>
|
|
|
|
#include <array>
|
|
|
|
|
|
|
|
CC_BACKEND_BEGIN
|
|
|
|
/**
|
|
|
|
* @addtogroup _metal
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
enum
|
|
|
|
{
|
2020-02-15 02:36:02 +08:00
|
|
|
MTL_TEXTURE_2D = 1,
|
|
|
|
MTL_TEXTURE_CUBE,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TextureInfoMTL
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
TextureInfoMTL(id<MTLDevice> mtlDevice)
|
|
|
|
{
|
2020-02-15 02:36:02 +08:00
|
|
|
_mtlDevice = mtlDevice;
|
|
|
|
_mtlTextures.fill(nil);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
~TextureInfoMTL() { destroy(); }
|
2020-02-15 02:36:02 +08:00
|
|
|
|
|
|
|
id<MTLTexture> ensure(int index, int target);
|
|
|
|
void destroy();
|
|
|
|
|
2020-02-15 03:10:01 +08:00
|
|
|
id<MTLTexture> createTexture(id<MTLDevice> mtlDevice, const TextureDescriptor& descriptor, int target);
|
2021-12-25 10:04:45 +08:00
|
|
|
void recreateSampler(const SamplerDescriptor& descriptor);
|
2020-02-15 02:36:02 +08:00
|
|
|
|
|
|
|
MTLSamplerAddressMode _sAddressMode;
|
|
|
|
MTLSamplerAddressMode _tAddressMode;
|
|
|
|
MTLSamplerMinMagFilter _minFilter;
|
|
|
|
MTLSamplerMinMagFilter _magFilter;
|
|
|
|
MTLSamplerMipFilter _mipFilter;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-02-15 02:36:02 +08:00
|
|
|
TextureDescriptor _descriptor;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-02-15 02:36:02 +08:00
|
|
|
id<MTLDevice> _mtlDevice;
|
|
|
|
std::array<id<MTLTexture>, CC_META_TEXTURES + 1> _mtlTextures;
|
|
|
|
int _maxIdx = -1;
|
|
|
|
|
|
|
|
id<MTLSamplerState> _mtlSamplerState = nil;
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned int _bytesPerRow = 0;
|
2020-02-15 02:36:02 +08:00
|
|
|
};
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* A 2D texture
|
|
|
|
*/
|
|
|
|
class TextureMTL : public backend::Texture2DBackend
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param mtlDevice The device for which MTLTexture and MTLSamplerState object was created.
|
|
|
|
* @param descriptor Specify texture and sampler description.
|
|
|
|
*/
|
|
|
|
TextureMTL(id<MTLDevice> mtlDevice, const TextureDescriptor& descriptor);
|
|
|
|
~TextureMTL();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Update a two-dimensional texture image
|
|
|
|
* @param data Specifies a pointer to the image data in memory.
|
|
|
|
* @param width Specifies the width of the texture image.
|
|
|
|
* @param height Specifies the height of the texture image.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @param level Specifies the level-of-detail number. Level 0 is the base image level. Level n is the nth mipmap
|
|
|
|
* reduction image.
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void updateData(uint8_t* data,
|
|
|
|
std::size_t width,
|
|
|
|
std::size_t height,
|
|
|
|
std::size_t level,
|
|
|
|
int index = 0) override;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Update a two-dimensional texture image in a compressed format
|
|
|
|
* @param data Specifies a pointer to the compressed image data in memory.
|
|
|
|
* @param width Specifies the width of the texture image.
|
|
|
|
* @param height Specifies the height of the texture image.
|
|
|
|
* @param dataLen Specifies the totoal size of compressed image in bytes.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @param level Specifies the level-of-detail number. Level 0 is the base image level. Level n is the nth mipmap
|
|
|
|
* reduction image.
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void updateCompressedData(uint8_t* data,
|
|
|
|
std::size_t width,
|
|
|
|
std::size_t height,
|
|
|
|
std::size_t dataLen,
|
|
|
|
std::size_t level,
|
|
|
|
int index = 0) override;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Update a two-dimensional texture subimage
|
|
|
|
* @param xoffset Specifies a texel offset in the x direction within the texture array.
|
|
|
|
* @param yoffset Specifies a texel offset in the y direction within the texture array.
|
|
|
|
* @param width Specifies the width of the texture subimage.
|
|
|
|
* @param height Specifies the height of the texture subimage.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @param level Specifies the level-of-detail number. Level 0 is the base image level. Level n is the nth mipmap
|
|
|
|
* reduction image.
|
2019-11-23 20:27:39 +08:00
|
|
|
* @param data Specifies a pointer to the image data in memory.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void 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 = 0) override;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Update a two-dimensional texture subimage in a compressed format
|
|
|
|
* @param xoffset Specifies a texel offset in the x direction within the texture array.
|
|
|
|
* @param yoffset Specifies a texel offset in the y direction within the texture array.
|
|
|
|
* @param width Specifies the width of the texture subimage.
|
|
|
|
* @param height Specifies the height of the texture subimage.
|
|
|
|
* @param dataLen Specifies the totoal size of compressed subimage in bytes.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @param level Specifies the level-of-detail number. Level 0 is the base image level. Level n is the nth mipmap
|
|
|
|
* reduction image.
|
2019-11-23 20:27:39 +08:00
|
|
|
* @param data Specifies a pointer to the compressed image data in memory.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void 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 = 0) override;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Update sampler
|
|
|
|
* @param sampler Specifies the sampler descriptor.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void updateSamplerDescriptor(const SamplerDescriptor& sampler) override;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Generate mipmaps.
|
|
|
|
*/
|
|
|
|
virtual void generateMipmaps() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Update texture description.
|
|
|
|
* @param descriptor Specifies texture and sampler descriptor.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void updateTextureDescriptor(const cocos2d::backend::TextureDescriptor& descriptor, int index = 0) override;
|
|
|
|
|
2020-02-15 02:36:02 +08:00
|
|
|
int getCount() const override { return _textureInfo._maxIdx + 1; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2020-09-21 22:10:50 +08:00
|
|
|
* Get MTLTexture object. reinterpret_cast<id<MTLTexture>>(handler);
|
2019-11-23 20:27:39 +08:00
|
|
|
* @return A MTLTexture object.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
uintptr_t getHandler(int index = 0) const override
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
return reinterpret_cast<uintptr_t>((void*)_textureInfo._mtlTextures[index]);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Get MTLSamplerState object
|
|
|
|
* @return A MTLSamplerState object.
|
|
|
|
*/
|
2020-02-15 03:10:01 +08:00
|
|
|
inline id<MTLSamplerState> getMTLSamplerState() const { return _textureInfo._mtlSamplerState; }
|
2020-02-14 23:42:05 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
private:
|
2020-02-15 02:36:02 +08:00
|
|
|
TextureInfoMTL _textureInfo;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A texture cube
|
|
|
|
*/
|
|
|
|
class TextureCubeMTL : public backend::TextureCubemapBackend
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param mtlDevice The device for which MTLTexture and MTLSamplerState object was created.
|
|
|
|
* @param descriptor Specify texture and sampler description.
|
|
|
|
*/
|
|
|
|
TextureCubeMTL(id<MTLDevice> mtlDevice, const TextureDescriptor& descriptor);
|
|
|
|
~TextureCubeMTL();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Update sampler
|
|
|
|
* @param sampler Specifies the sampler descriptor.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void updateSamplerDescriptor(const SamplerDescriptor& sampler) override;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Update texutre cube data in give slice side.
|
|
|
|
* @param side Specifies which slice texture of cube to be update.
|
|
|
|
* @param data Specifies a pointer to the image data in memory.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void updateFaceData(TextureCubeFace side, void* data, int index = 0) override;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Generate mipmaps.
|
|
|
|
*/
|
|
|
|
virtual void generateMipmaps() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Update texture description.
|
|
|
|
* @param descriptor Specifies texture and sampler descriptor.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void updateTextureDescriptor(const cocos2d::backend::TextureDescriptor& descriptor, int index = 0) override;
|
|
|
|
|
2020-02-15 02:36:02 +08:00
|
|
|
int getCount() const override { return _textureInfo._maxIdx + 1; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
uintptr_t getHandler(int index = 0) const override
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
return reinterpret_cast<uintptr_t>((void*)_textureInfo._mtlTextures[index]);
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Get MTLSamplerState object
|
|
|
|
* @return A MTLSamplerState object.
|
|
|
|
*/
|
2020-02-15 03:10:01 +08:00
|
|
|
inline id<MTLSamplerState> getMTLSamplerState() const { return _textureInfo._mtlSamplerState; }
|
2020-02-15 02:36:02 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
private:
|
2020-02-15 02:36:02 +08:00
|
|
|
TextureInfoMTL _textureInfo;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
MTLRegion _region;
|
|
|
|
std::size_t _bytesPerImage = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// end of _metal group
|
|
|
|
/// @}
|
|
|
|
CC_BACKEND_END
|