Texture2D.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #include "Texture2D.h"
  21. #include "DeviceGraphics.h"
  22. #include "GFXUtils.h"
  23. #include "base/CCGLUtils.h"
  24. RENDERER_BEGIN
  25. Texture2D::Texture2D()
  26. {
  27. // RENDERER_LOGD("Construct Texture2D: %p", this);
  28. }
  29. Texture2D::~Texture2D()
  30. {
  31. // RENDERER_LOGD("Destruct Texture2D: %p", this);
  32. }
  33. bool Texture2D::init(DeviceGraphics* device, Options& options)
  34. {
  35. bool ok = Texture::init(device);
  36. if (ok)
  37. {
  38. _target = GL_TEXTURE_2D;
  39. GL_CHECK(glGenTextures(1, &_glID));
  40. if (options.images.empty())
  41. options.images.push_back(Image());
  42. update(options);
  43. }
  44. return ok;
  45. }
  46. void Texture2D::update(const Options& options)
  47. {
  48. bool genMipmap = _hasMipmap;
  49. _width = options.width;
  50. _height = options.height;
  51. _anisotropy = options.anisotropy;
  52. _minFilter = options.minFilter;
  53. _magFilter = options.magFilter;
  54. _mipFilter = options.mipFilter;
  55. _wrapS = options.wrapS;
  56. _wrapT = options.wrapT;
  57. _glFormat = options.glFormat;
  58. _glInternalFormat = options.glInternalFormat;
  59. _glType = options.glType;
  60. _compressed = options.compressed;
  61. _bpp = options.bpp;
  62. // check if generate mipmap
  63. _hasMipmap = options.hasMipmap;
  64. genMipmap = options.hasMipmap;
  65. if (options.images.size() > 1)
  66. {
  67. genMipmap = false; //REFINE: is it true here?
  68. uint16_t maxLength = options.width > options.height ? options.width : options.height;
  69. if (maxLength >> (options.images.size() - 1) != 1)
  70. RENDERER_LOGE("texture-2d mipmap is invalid, should have a 1x1 mipmap.");
  71. }
  72. // NOTE: get pot after _width, _height has been assigned.
  73. bool pot = isPow2(_width) && isPow2(_height);
  74. if (!pot)
  75. genMipmap = false;
  76. GL_CHECK(glActiveTexture(GL_TEXTURE0));
  77. GL_CHECK(glBindTexture(GL_TEXTURE_2D, _glID));
  78. if (!options.images.empty())
  79. setMipmap(options.images, options.flipY, options.premultiplyAlpha);
  80. setTexInfo();
  81. if (genMipmap)
  82. {
  83. GL_CHECK(glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST));
  84. GL_CHECK(glGenerateMipmap(GL_TEXTURE_2D));
  85. }
  86. _device->restoreTexture(0);
  87. }
  88. void Texture2D::updateSubImage(const SubImageOption& option)
  89. {
  90. GL_CHECK(glActiveTexture(GL_TEXTURE0));
  91. GL_CHECK(glBindTexture(GL_TEXTURE_2D, _glID));
  92. setSubImage(option);
  93. _device->restoreTexture(0);
  94. }
  95. void Texture2D::updateImage(const ImageOption& option)
  96. {
  97. GL_CHECK(glActiveTexture(GL_TEXTURE0));
  98. GL_CHECK(glBindTexture(GL_TEXTURE_2D, _glID));
  99. setImage(option);
  100. _device->restoreTexture(0);
  101. }
  102. // Private methods:
  103. void Texture2D::setSubImage(const SubImageOption& option)
  104. {
  105. bool flipY = option.flipY;
  106. bool premultiplyAlpha = option.premultiplyAlpha;
  107. //Set the row align only when mipmapsNum == 1 and the data is uncompressed
  108. GLint aligment = 1;
  109. if (!_hasMipmap && !_compressed && _bpp > 0)
  110. {
  111. unsigned int bytesPerRow = option.width * _bpp / 8;
  112. if (bytesPerRow % 8 == 0)
  113. aligment = 8;
  114. else if (bytesPerRow % 4 == 0)
  115. aligment = 4;
  116. else if (bytesPerRow % 2 == 0)
  117. aligment = 2;
  118. else
  119. aligment = 1;
  120. }
  121. GL_CHECK(ccPixelStorei(GL_UNPACK_ALIGNMENT, aligment));
  122. GL_CHECK(ccPixelStorei(GL_UNPACK_FLIP_Y_WEBGL, flipY));
  123. GL_CHECK(ccPixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha));
  124. ccFlipYOrPremultiptyAlphaIfNeeded(_glFormat, option.width, option.height, (uint32_t)option.imageDataLength, option.imageData);
  125. if (_compressed)
  126. {
  127. glCompressedTexSubImage2D(GL_TEXTURE_2D,
  128. option.level,
  129. option.x,
  130. option.y,
  131. option.width,
  132. option.height,
  133. _glFormat,
  134. option.imageDataLength,
  135. option.imageData);
  136. }
  137. else
  138. {
  139. GL_CHECK(glTexSubImage2D(GL_TEXTURE_2D,
  140. option.level,
  141. option.x,
  142. option.y,
  143. option.width,
  144. option.height,
  145. _glFormat,
  146. _glType, option.imageData));
  147. }
  148. }
  149. void Texture2D::setImage(const ImageOption& option)
  150. {
  151. const auto& img = option.image;
  152. bool flipY = option.flipY;
  153. bool premultiplyAlpha = option.premultiplyAlpha;
  154. //Set the row align only when mipmapsNum == 1 and the data is uncompressed
  155. GLint aligment = 1;
  156. unsigned int bytesPerRow = option.width * _bpp / 8;
  157. if (_hasMipmap && !_compressed && _bpp > 0)
  158. {
  159. if (bytesPerRow % 8 == 0)
  160. aligment = 8;
  161. else if (bytesPerRow % 4 == 0)
  162. aligment = 4;
  163. else if (bytesPerRow % 2 == 0)
  164. aligment = 2;
  165. else
  166. aligment = 1;
  167. }
  168. GL_CHECK(ccPixelStorei(GL_UNPACK_ALIGNMENT, aligment));
  169. GL_CHECK(ccPixelStorei(GL_UNPACK_FLIP_Y_WEBGL, flipY));
  170. GL_CHECK(ccPixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha));
  171. uint32_t pixelBytes = (uint32_t)img.length;
  172. switch(_glType)
  173. {
  174. case GL_FLOAT:
  175. {
  176. pixelBytes /= sizeof(float);
  177. break;
  178. }
  179. }
  180. ccFlipYOrPremultiptyAlphaIfNeeded(_glFormat, option.width, option.height, pixelBytes, img.data);
  181. if (_compressed)
  182. {
  183. glCompressedTexImage2D(GL_TEXTURE_2D,
  184. option.level,
  185. _glInternalFormat,
  186. option.width,
  187. option.height,
  188. 0,
  189. (GLsizei)img.length,
  190. img.data);
  191. }
  192. else
  193. {
  194. GL_CHECK(glTexImage2D(GL_TEXTURE_2D,
  195. option.level,
  196. _glInternalFormat,
  197. option.width,
  198. option.height,
  199. 0,
  200. _glFormat,
  201. _glType,
  202. img.data));
  203. }
  204. }
  205. void Texture2D::setMipmap(const std::vector<Image>& images, bool isFlipY, bool isPremultiplyAlpha)
  206. {
  207. ImageOption options;
  208. options.width = _width;
  209. options.height = _height;
  210. options.flipY = isFlipY;
  211. options.premultiplyAlpha = isPremultiplyAlpha;
  212. options.level = 0;
  213. for (size_t i = 0, len = images.size(); i < len; ++i)
  214. {
  215. options.level = (GLint)i;
  216. options.width = _width >> i;
  217. options.height = _height >> i;
  218. options.image = images[i];
  219. setImage(options);
  220. }
  221. }
  222. void Texture2D::setTexInfo()
  223. {
  224. bool pot = isPow2(_width) && isPow2(_height);
  225. // WebGL1 doesn't support all wrap modes with NPOT textures
  226. if (!pot && (_wrapS != WrapMode::CLAMP || _wrapT != WrapMode::CLAMP))
  227. {
  228. RENDERER_LOGW("WebGL1 doesn\'t support all wrap modes with NPOT textures");
  229. _wrapS = WrapMode::CLAMP;
  230. _wrapT = WrapMode::CLAMP;
  231. }
  232. Filter mipFilter = _hasMipmap ? _mipFilter : Filter::NONE;
  233. if (!pot && mipFilter != Filter::NONE)
  234. {
  235. RENDERER_LOGW("NPOT textures do not support mipmap filter");
  236. mipFilter = Filter::NONE;
  237. }
  238. GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, glFilter(_minFilter, mipFilter)));
  239. GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, glFilter(_magFilter, Filter::NONE)));
  240. GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLint)_wrapS));
  241. GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLint)_wrapT));
  242. //REFINE: let ext = this._device.ext('EXT_texture_filter_anisotropic');
  243. // if (ext) {
  244. // GL_CHECK(glTexParameteri(GL_TEXTURE_2D, ext.TEXTURE_MAX_ANISOTROPY_EXT, this._anisotropy));
  245. // }
  246. }
  247. RENDERER_END