axmol/core/renderer/backend/Texture.h

225 lines
8.0 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
2022-04-25 19:15:46 +08:00
Copyright (c) 2020 C4games Ltd
2019-11-23 20:27:39 +08:00
https://axis-project.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-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#pragma once
#include "Types.h"
#include "base/CCRef.h"
#include <cassert>
#include <functional>
NS_AX_BACKEND_BEGIN
2019-11-23 20:27:39 +08:00
/**
* @addtogroup _backend
* @{
*/
/**
* Store texture description.
*/
struct TextureDescriptor
{
2021-12-25 10:04:45 +08:00
TextureType textureType = TextureType::TEXTURE_2D;
PixelFormat textureFormat = PixelFormat::RGBA8;
2019-11-23 20:27:39 +08:00
TextureUsage textureUsage = TextureUsage::READ;
2021-12-25 10:04:45 +08:00
uint32_t width = 0;
uint32_t height = 0;
uint32_t depth = 0;
2019-11-23 20:27:39 +08:00
SamplerDescriptor samplerDescriptor;
};
/**
* A base texture
*/
class TextureBackend : public Ref
{
public:
/**
* Update sampler
* @param sampler Specifies the sampler descriptor.
*/
2021-12-25 10:04:45 +08:00
virtual void updateSamplerDescriptor(const SamplerDescriptor& sampler) = 0;
2019-11-23 20:27:39 +08:00
/// Generate mipmaps.
virtual void generateMipmaps() = 0;
/**
* Update texture description.
* @param descriptor Specifies texture and sampler descriptor.
*/
virtual void updateTextureDescriptor(const TextureDescriptor& descriptor, int index = 0);
/**
* Get texture format.
* @return Texture format.
*/
inline PixelFormat getTextureFormat() const { return _textureFormat; }
/**
* Get texture usage. Symbolic constant can be READ, WRITE or RENDER_TARGET.
* @return Texture usage.
*/
inline TextureUsage getTextureUsage() const { return _textureUsage; }
/**
* Get texture type. Symbolic constant value can be either TEXTURE_2D or TEXTURE_CUBE.
* @return Texture type.
*/
inline TextureType getTextureType() const { return _textureType; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Check if mipmap had generated before.
* @return true if the mipmap has always generated before, otherwise false.
*/
inline bool hasMipmaps() const { return _hasMipmaps; }
2020-02-15 02:36:02 +08:00
virtual int getCount() const { return 1; };
2019-11-23 20:27:39 +08:00
2020-09-11 01:19:10 +08:00
virtual uintptr_t getHandler(int index = 0) const { return 0; }
2021-12-25 10:04:45 +08:00
2020-09-11 00:10:44 +08:00
int getWidth() const { return _width; }
int getHeight() const { return _height; }
2019-11-23 20:27:39 +08:00
protected:
/**
* @param descriptor Specifies the texture descirptor.
*/
2020-02-15 02:58:41 +08:00
TextureBackend() {}
2019-11-23 20:27:39 +08:00
virtual ~TextureBackend();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/// The bytes of all components.
uint8_t _bitsPerPixel = 0;
2021-12-25 10:04:45 +08:00
bool _hasMipmaps = false;
bool _isCompressed = false;
uint32_t _width = 0;
uint32_t _height = 0;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
TextureType _textureType = TextureType::TEXTURE_2D;
PixelFormat _textureFormat = PixelFormat::RGBA8;
2019-11-23 20:27:39 +08:00
TextureUsage _textureUsage = TextureUsage::READ;
};
/**
* A 2D texture.
*/
class Texture2DBackend : public TextureBackend
{
public:
/**
* 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) = 0;
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) = 0;
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) = 0;
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) = 0;
2019-11-23 20:27:39 +08:00
/**
* Get texture width.
* @return Texture width.
*/
inline std::size_t getWidth() const { return _width; }
/**
* Get texture height.
* @return Texture height.
*/
inline std::size_t getHeight() const { return _height; }
};
/**
* A cubemap texture.
*/
class TextureCubemapBackend : public TextureBackend
{
public:
/**
* 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) = 0;
2019-11-23 20:27:39 +08:00
};
2021-12-25 10:04:45 +08:00
// end of _backend group
2019-11-23 20:27:39 +08:00
/// @}
NS_AX_BACKEND_END