Texture.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #pragma once
  21. #include "../Macro.h"
  22. #include "../Types.h"
  23. #include "GraphicsHandle.h"
  24. #include "RenderTarget.h"
  25. #include "base/CCData.h"
  26. #include <vector>
  27. RENDERER_BEGIN
  28. class DeviceGraphics;
  29. /**
  30. * @addtogroup gfx
  31. * @{
  32. */
  33. /**
  34. * The base texture class
  35. */
  36. class Texture : public RenderTarget
  37. {
  38. public:
  39. /**
  40. * @enum Filter
  41. * Texture filter modes
  42. */
  43. enum class Filter : int8_t
  44. {
  45. NONE = -1,
  46. NEAREST = 0,
  47. LINEAR = 1
  48. };
  49. /**
  50. * @enum Filter
  51. * Texture wrap modes
  52. */
  53. enum class WrapMode : uint16_t
  54. {
  55. REPEAT = GL_REPEAT,
  56. CLAMP = GL_CLAMP_TO_EDGE,
  57. MIRROR = GL_MIRRORED_REPEAT
  58. };
  59. /**
  60. * @enum Format
  61. * Texture formats
  62. */
  63. enum class Format : uint8_t
  64. {
  65. BEGIN = 0,
  66. // compress formats
  67. RGB_DXT1 = 0,
  68. RGBA_DXT1 = 1,
  69. RGBA_DXT3 = 2,
  70. RGBA_DXT5 = 3,
  71. RGB_ETC1 = 4,
  72. RGB_PVRTC_2BPPV1 = 5,
  73. RGBA_PVRTC_2BPPV1 = 6,
  74. RGB_PVRTC_4BPPV1 = 7,
  75. RGBA_PVRTC_4BPPV1 = 8,
  76. //
  77. // normal formats
  78. A8 = 9,
  79. L8 = 10,
  80. L8_A8 = 11,
  81. R5_G6_B5 = 12,
  82. R5_G5_B5_A1 = 13,
  83. R4_G4_B4_A4 = 14,
  84. RGB8 = 15, // each channel has 8 bits
  85. RGBA8 = 16, // each channel has 8 bits
  86. RGB16F = 17, // each channel has 16 bits
  87. RGBA16F = 18, // each channel has 16 bits
  88. RGB32F = 19, // each channel has 32 bits
  89. RGBA32F = 20, // each channel has 32 bits
  90. R32F = 21,
  91. _111110F = 22,
  92. SRGB = 23,
  93. SRGBA = 24,
  94. //
  95. // depth formats
  96. D16 = 25,
  97. END = 25
  98. //
  99. };
  100. /**
  101. * @struct Image
  102. * Raw image data
  103. */
  104. struct Image
  105. {
  106. uint8_t* data = nullptr;
  107. size_t length = 0;
  108. };
  109. /**
  110. * @struct Options
  111. * Texture setting options including format, width, height, wrap mode, filter etc.
  112. */
  113. struct Options
  114. {
  115. /**
  116. * Image mipmaps
  117. */
  118. std::vector<Image> images;
  119. /**
  120. * The maximum anisotropy for the texture
  121. */
  122. int32_t anisotropy = 1;
  123. /**
  124. * The internal format specifying the color components in the texture
  125. */
  126. GLenum glInternalFormat = GL_RGBA;
  127. /**
  128. * The pixel format of the texel data
  129. */
  130. GLenum glFormat = GL_RGB;
  131. /**
  132. * The data type of the texel data
  133. */
  134. GLenum glType = GL_UNSIGNED_BYTE;
  135. /**
  136. * The width of the texture
  137. */
  138. uint16_t width = 4;
  139. /**
  140. * The height of the texture
  141. */
  142. uint16_t height = 4;
  143. uint8_t bpp = 0;
  144. /**
  145. * The wrapping function for texture coordinate s
  146. */
  147. WrapMode wrapS = WrapMode::CLAMP;
  148. /**
  149. * The wrapping function for texture coordinate t
  150. */
  151. WrapMode wrapT = WrapMode::CLAMP;
  152. /**
  153. * The texture minification filter
  154. */
  155. Filter minFilter = Filter::LINEAR;
  156. /**
  157. * The texture magnification filter
  158. */
  159. Filter magFilter = Filter::LINEAR;
  160. /**
  161. * The texture filter for mipmaps
  162. */
  163. Filter mipFilter = Filter::LINEAR;
  164. /**
  165. * Specifies whether the texture have mipmaps
  166. */
  167. bool hasMipmap = false;
  168. /**
  169. * Specifies whether the texture if flipped vertically
  170. */
  171. bool flipY = false;
  172. /**
  173. * Specifies whether the texture have alpha premultiplied
  174. */
  175. bool premultiplyAlpha = false;
  176. /**
  177. * Specifies whether the texture is compressed
  178. */
  179. bool compressed = false;
  180. };
  181. /**
  182. * @struct ImageOption
  183. * The informations of Image which indicates the mipmap level, width, height, filpY and premultiplyAlpha
  184. */
  185. struct ImageOption
  186. {
  187. Image image;
  188. int32_t level = 0;
  189. uint16_t width = 4;
  190. uint16_t height = 4;
  191. bool flipY = false;
  192. bool premultiplyAlpha = false;
  193. };
  194. /**
  195. * @struct SubImageOption
  196. * The informations of sub image which indicates the area to update, mipmap level, filpY and premultiplyAlpha
  197. */
  198. struct SubImageOption
  199. {
  200. SubImageOption(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t level, bool flipY, bool premultiplyAlpha)
  201. : x(x)
  202. , y(y)
  203. , width(width)
  204. , height(height)
  205. , level(level)
  206. , flipY(flipY)
  207. , premultiplyAlpha(premultiplyAlpha)
  208. {}
  209. SubImageOption() {}
  210. uint32_t imageDataLength = 0;
  211. uint16_t x = 0;
  212. uint16_t y = 0;
  213. uint16_t width = 0;
  214. uint16_t height = 0;
  215. uint8_t* imageData = nullptr;
  216. uint8_t level = 0;
  217. bool flipY = false;
  218. bool premultiplyAlpha = false;
  219. };
  220. /**
  221. * Gets the target gl location
  222. */
  223. inline GLuint getTarget() const { return _target; }
  224. /**
  225. * Gets the width of texture
  226. */
  227. inline uint16_t getWidth() const { return _width; }
  228. /**
  229. * Gets the height of texture
  230. */
  231. inline uint16_t getHeight() const { return _height; }
  232. inline void setAlphaAtlas(bool value) { _useAlphaAtlas = value; }
  233. inline bool isAlphaAtlas() const { return _useAlphaAtlas; }
  234. protected:
  235. static GLenum glFilter(Filter filter, Filter mipFilter = Filter::NONE);
  236. static bool isPow2(int32_t v) {
  237. return !(v & (v - 1)) && (!!v);
  238. }
  239. Texture();
  240. virtual ~Texture();
  241. bool init(DeviceGraphics* device);
  242. DeviceGraphics* _device;
  243. GLint _anisotropy;
  244. GLuint _target;
  245. WrapMode _wrapS;
  246. WrapMode _wrapT;
  247. uint16_t _width;
  248. uint16_t _height;
  249. uint8_t _bpp = 0;
  250. Filter _minFilter;
  251. Filter _magFilter;
  252. Filter _mipFilter;
  253. GLenum _glInternalFormat;
  254. GLenum _glFormat;
  255. GLenum _glType;
  256. bool _hasMipmap;
  257. bool _compressed;
  258. bool _useAlphaAtlas = false;
  259. };
  260. // end of gfx group
  261. /// @}
  262. RENDERER_END