OGLTexture.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  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. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../../Precompiled.h"
  23. #include "../../Graphics/Graphics.h"
  24. #include "../../Graphics/GraphicsImpl.h"
  25. #include "../../Graphics/Material.h"
  26. #include "../../Graphics/RenderSurface.h"
  27. #include "../../IO/Log.h"
  28. #include "../../Resource/ResourceCache.h"
  29. #include "../../Resource/XMLFile.h"
  30. #include "../../DebugNew.h"
  31. namespace Urho3D
  32. {
  33. static GLenum glWrapModes[] =
  34. {
  35. GL_REPEAT,
  36. GL_MIRRORED_REPEAT,
  37. GL_CLAMP_TO_EDGE,
  38. #ifndef GL_ES_VERSION_2_0
  39. GL_CLAMP
  40. #else
  41. GL_CLAMP_TO_EDGE
  42. #endif
  43. };
  44. #ifndef GL_ES_VERSION_2_0
  45. static GLenum gl3WrapModes[] =
  46. {
  47. GL_REPEAT,
  48. GL_MIRRORED_REPEAT,
  49. GL_CLAMP_TO_EDGE,
  50. GL_CLAMP_TO_BORDER
  51. };
  52. #endif
  53. static GLenum GetWrapMode(TextureAddressMode mode)
  54. {
  55. #ifndef GL_ES_VERSION_2_0
  56. return Graphics::GetGL3Support() ? gl3WrapModes[mode] : glWrapModes[mode];
  57. #else
  58. return glWrapModes[mode];
  59. #endif
  60. }
  61. void Texture::SetSRGB(bool enable)
  62. {
  63. if (graphics_)
  64. enable &= graphics_->GetSRGBSupport();
  65. if (enable != sRGB_)
  66. {
  67. sRGB_ = enable;
  68. // If texture had already been created, must recreate it to set the sRGB texture format
  69. if (object_.name_)
  70. Create();
  71. // If texture in use in the framebuffer, mark it dirty
  72. if (graphics_ && graphics_->GetRenderTarget(0) && graphics_->GetRenderTarget(0)->GetParentTexture() == this)
  73. graphics_->MarkFBODirty();
  74. }
  75. }
  76. void Texture::UpdateParameters()
  77. {
  78. if (!object_.name_ || !graphics_)
  79. return;
  80. // If texture is multisampled, do not attempt to set parameters as it's illegal, just return
  81. #ifndef GL_ES_VERSION_2_0
  82. if (target_ == GL_TEXTURE_2D_MULTISAMPLE)
  83. {
  84. parametersDirty_ = false;
  85. return;
  86. }
  87. #endif
  88. // Wrapping
  89. glTexParameteri(target_, GL_TEXTURE_WRAP_S, GetWrapMode(addressModes_[COORD_U]));
  90. glTexParameteri(target_, GL_TEXTURE_WRAP_T, GetWrapMode(addressModes_[COORD_V]));
  91. #ifndef GL_ES_VERSION_2_0
  92. glTexParameteri(target_, GL_TEXTURE_WRAP_R, GetWrapMode(addressModes_[COORD_W]));
  93. #endif
  94. TextureFilterMode filterMode = filterMode_;
  95. if (filterMode == FILTER_DEFAULT)
  96. filterMode = graphics_->GetDefaultTextureFilterMode();
  97. // Filtering
  98. switch (filterMode)
  99. {
  100. case FILTER_NEAREST:
  101. if (levels_ < 2)
  102. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  103. else
  104. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
  105. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  106. break;
  107. case FILTER_BILINEAR:
  108. if (levels_ < 2)
  109. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  110. else
  111. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  112. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  113. break;
  114. case FILTER_ANISOTROPIC:
  115. case FILTER_TRILINEAR:
  116. if (levels_ < 2)
  117. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  118. else
  119. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  120. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  121. break;
  122. case FILTER_NEAREST_ANISOTROPIC:
  123. if (levels_ < 2)
  124. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  125. else
  126. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
  127. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  128. break;
  129. default:
  130. break;
  131. }
  132. #ifndef GL_ES_VERSION_2_0
  133. // Anisotropy
  134. if (graphics_->GetAnisotropySupport())
  135. {
  136. unsigned maxAnisotropy = anisotropy_ ? anisotropy_ : graphics_->GetDefaultTextureAnisotropy();
  137. glTexParameterf(target_, GL_TEXTURE_MAX_ANISOTROPY_EXT,
  138. (filterMode == FILTER_ANISOTROPIC || filterMode == FILTER_NEAREST_ANISOTROPIC) ? (float)maxAnisotropy : 1.0f);
  139. }
  140. // Shadow compare
  141. if (shadowCompare_)
  142. {
  143. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
  144. glTexParameteri(target_, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  145. }
  146. else
  147. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  148. glTexParameterfv(target_, GL_TEXTURE_BORDER_COLOR, borderColor_.Data());
  149. #endif
  150. parametersDirty_ = false;
  151. }
  152. bool Texture::GetParametersDirty() const
  153. {
  154. return parametersDirty_;
  155. }
  156. bool Texture::IsCompressed() const
  157. {
  158. return format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ||
  159. format_ == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT || format_ == GL_ETC1_RGB8_OES ||
  160. format_ == GL_ETC2_RGB8_OES || format_ == GL_ETC2_RGBA8_OES ||
  161. format_ == COMPRESSED_RGB_PVRTC_4BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_4BPPV1_IMG ||
  162. format_ == COMPRESSED_RGB_PVRTC_2BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  163. }
  164. unsigned Texture::GetRowDataSize(int width) const
  165. {
  166. switch (format_)
  167. {
  168. case GL_ALPHA:
  169. case GL_LUMINANCE:
  170. return (unsigned)width;
  171. case GL_LUMINANCE_ALPHA:
  172. return (unsigned)(width * 2);
  173. case GL_RGB:
  174. return (unsigned)(width * 3);
  175. case GL_RGBA:
  176. #ifndef GL_ES_VERSION_2_0
  177. case GL_DEPTH24_STENCIL8_EXT:
  178. case GL_RG16:
  179. case GL_RG16F:
  180. case GL_R32F:
  181. #endif
  182. return (unsigned)(width * 4);
  183. #ifndef GL_ES_VERSION_2_0
  184. case GL_R8:
  185. return (unsigned)width;
  186. case GL_RG8:
  187. case GL_R16F:
  188. return (unsigned)(width * 2);
  189. case GL_RGBA16:
  190. case GL_RGBA16F_ARB:
  191. return (unsigned)(width * 8);
  192. case GL_RGBA32F_ARB:
  193. return (unsigned)(width * 16);
  194. #endif
  195. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  196. return ((unsigned)(width + 3) >> 2u) * 8;
  197. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  198. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  199. return ((unsigned)(width + 3) >> 2u) * 16;
  200. case GL_ETC1_RGB8_OES:
  201. case GL_ETC2_RGB8_OES:
  202. return ((unsigned)(width + 3) >> 2u) * 8;
  203. case GL_ETC2_RGBA8_OES:
  204. return ((unsigned)(width + 3) >> 2u) * 16;
  205. case COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
  206. case COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
  207. return ((unsigned)(width + 3) >> 2u) * 8;
  208. case COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
  209. case COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
  210. return ((unsigned)(width + 7) >> 3u) * 8;
  211. default:
  212. return 0;
  213. }
  214. }
  215. unsigned Texture::GetExternalFormat(unsigned format)
  216. {
  217. #ifndef GL_ES_VERSION_2_0
  218. if (format == GL_DEPTH_COMPONENT16 || format == GL_DEPTH_COMPONENT24 || format == GL_DEPTH_COMPONENT32)
  219. return GL_DEPTH_COMPONENT;
  220. else if (format == GL_DEPTH24_STENCIL8_EXT)
  221. return GL_DEPTH_STENCIL_EXT;
  222. else if (format == GL_SLUMINANCE_EXT)
  223. return GL_LUMINANCE;
  224. else if (format == GL_SLUMINANCE_ALPHA_EXT)
  225. return GL_LUMINANCE_ALPHA;
  226. else if (format == GL_R8 || format == GL_R16F || format == GL_R32F)
  227. return GL_RED;
  228. else if (format == GL_RG8 || format == GL_RG16 || format == GL_RG16F || format == GL_RG32F)
  229. return GL_RG;
  230. else if (format == GL_RGBA16 || format == GL_RGBA16F_ARB || format == GL_RGBA32F_ARB || format == GL_SRGB_ALPHA_EXT)
  231. return GL_RGBA;
  232. else if (format == GL_SRGB_EXT)
  233. return GL_RGB;
  234. else
  235. return format;
  236. #else
  237. return format;
  238. #endif
  239. }
  240. unsigned Texture::GetDataType(unsigned format)
  241. {
  242. #ifndef GL_ES_VERSION_2_0
  243. if (format == GL_DEPTH24_STENCIL8_EXT)
  244. return GL_UNSIGNED_INT_24_8_EXT;
  245. else if (format == GL_RG16 || format == GL_RGBA16)
  246. return GL_UNSIGNED_SHORT;
  247. else if (format == GL_RGBA32F_ARB || format == GL_RG32F || format == GL_R32F)
  248. return GL_FLOAT;
  249. else if (format == GL_RGBA16F_ARB || format == GL_RG16F || format == GL_R16F)
  250. return GL_HALF_FLOAT_ARB;
  251. else
  252. return GL_UNSIGNED_BYTE;
  253. #else
  254. if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_COMPONENT24_OES)
  255. return GL_UNSIGNED_INT;
  256. else if (format == GL_DEPTH_COMPONENT16)
  257. return GL_UNSIGNED_SHORT;
  258. else
  259. return GL_UNSIGNED_BYTE;
  260. #endif
  261. }
  262. unsigned Texture::GetSRGBFormat(unsigned format)
  263. {
  264. #ifndef GL_ES_VERSION_2_0
  265. if (!graphics_ || !graphics_->GetSRGBSupport())
  266. return format;
  267. switch (format)
  268. {
  269. case GL_RGB:
  270. return GL_SRGB_EXT;
  271. case GL_RGBA:
  272. return GL_SRGB_ALPHA_EXT;
  273. case GL_LUMINANCE:
  274. return GL_SLUMINANCE_EXT;
  275. case GL_LUMINANCE_ALPHA:
  276. return GL_SLUMINANCE_ALPHA_EXT;
  277. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  278. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
  279. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  280. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
  281. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  282. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
  283. default:
  284. return format;
  285. }
  286. #else
  287. return format;
  288. #endif
  289. }
  290. void Texture::RegenerateLevels()
  291. {
  292. if (!object_.name_)
  293. return;
  294. #ifndef GL_ES_VERSION_2_0
  295. if (Graphics::GetGL3Support())
  296. glGenerateMipmap(target_);
  297. else
  298. glGenerateMipmapEXT(target_);
  299. #else
  300. glGenerateMipmap(target_);
  301. #endif
  302. levelsDirty_ = false;
  303. }
  304. }