OGLTexture.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // Copyright (c) 2008-2016 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 Atomic
  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. // Wrapping
  81. glTexParameteri(target_, GL_TEXTURE_WRAP_S, GetWrapMode(addressMode_[COORD_U]));
  82. glTexParameteri(target_, GL_TEXTURE_WRAP_T, GetWrapMode(addressMode_[COORD_V]));
  83. #ifndef GL_ES_VERSION_2_0
  84. glTexParameteri(target_, GL_TEXTURE_WRAP_R, GetWrapMode(addressMode_[COORD_W]));
  85. #endif
  86. TextureFilterMode filterMode = filterMode_;
  87. if (filterMode == FILTER_DEFAULT)
  88. filterMode = graphics_->GetDefaultTextureFilterMode();
  89. // Filtering
  90. switch (filterMode)
  91. {
  92. case FILTER_NEAREST:
  93. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  94. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  95. break;
  96. case FILTER_BILINEAR:
  97. if (levels_ < 2)
  98. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  99. else
  100. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  101. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  102. break;
  103. case FILTER_ANISOTROPIC:
  104. case FILTER_TRILINEAR:
  105. if (levels_ < 2)
  106. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  107. else
  108. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  109. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  110. break;
  111. default:
  112. break;
  113. }
  114. #ifndef GL_ES_VERSION_2_0
  115. // Anisotropy
  116. if (graphics_->GetAnisotropySupport())
  117. {
  118. unsigned maxAnisotropy = anisotropy_ ? anisotropy_ : graphics_->GetDefaultTextureAnisotropy();
  119. glTexParameterf(target_, GL_TEXTURE_MAX_ANISOTROPY_EXT,
  120. filterMode == FILTER_ANISOTROPIC ? (float)maxAnisotropy : 1.0f);
  121. }
  122. // Shadow compare
  123. if (shadowCompare_)
  124. {
  125. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
  126. glTexParameteri(target_, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  127. }
  128. else
  129. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  130. glTexParameterfv(target_, GL_TEXTURE_BORDER_COLOR, borderColor_.Data());
  131. #endif
  132. parametersDirty_ = false;
  133. }
  134. bool Texture::GetParametersDirty() const
  135. {
  136. return parametersDirty_;
  137. }
  138. bool Texture::IsCompressed() const
  139. {
  140. return format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ||
  141. format_ == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT || format_ == GL_ETC1_RGB8_OES ||
  142. format_ == COMPRESSED_RGB_PVRTC_4BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_4BPPV1_IMG ||
  143. format_ == COMPRESSED_RGB_PVRTC_2BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  144. }
  145. unsigned Texture::GetRowDataSize(int width) const
  146. {
  147. switch (format_)
  148. {
  149. case GL_ALPHA:
  150. case GL_LUMINANCE:
  151. return (unsigned)width;
  152. case GL_LUMINANCE_ALPHA:
  153. return (unsigned)(width * 2);
  154. case GL_RGB:
  155. return (unsigned)(width * 3);
  156. case GL_RGBA:
  157. #ifndef GL_ES_VERSION_2_0
  158. case GL_DEPTH24_STENCIL8_EXT:
  159. case GL_RG16:
  160. case GL_RG16F:
  161. case GL_R32F:
  162. #endif
  163. return (unsigned)(width * 4);
  164. #ifndef GL_ES_VERSION_2_0
  165. case GL_R8:
  166. return (unsigned)width;
  167. case GL_RG8:
  168. case GL_R16F:
  169. return (unsigned)(width * 2);
  170. case GL_RGBA16:
  171. case GL_RGBA16F_ARB:
  172. return (unsigned)(width * 8);
  173. case GL_RGBA32F_ARB:
  174. return (unsigned)(width * 16);
  175. #endif
  176. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  177. return (unsigned)(((width + 3) >> 2) * 8);
  178. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  179. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  180. return (unsigned)(((width + 3) >> 2) * 16);
  181. case GL_ETC1_RGB8_OES:
  182. return (unsigned)(((width + 3) >> 2) * 8);
  183. case COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
  184. case COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
  185. return (unsigned)((width * 4 + 7) >> 3);
  186. case COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
  187. case COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
  188. return (unsigned)((width * 2 + 7) >> 3);
  189. default:
  190. return 0;
  191. }
  192. }
  193. unsigned Texture::GetExternalFormat(unsigned format)
  194. {
  195. #ifndef GL_ES_VERSION_2_0
  196. if (format == GL_DEPTH_COMPONENT16 || format == GL_DEPTH_COMPONENT24 || format == GL_DEPTH_COMPONENT32)
  197. return GL_DEPTH_COMPONENT;
  198. else if (format == GL_DEPTH24_STENCIL8_EXT)
  199. return GL_DEPTH_STENCIL_EXT;
  200. else if (format == GL_SLUMINANCE_EXT)
  201. return GL_LUMINANCE;
  202. else if (format == GL_SLUMINANCE_ALPHA_EXT)
  203. return GL_LUMINANCE_ALPHA;
  204. else if (format == GL_R8 || format == GL_R16F || format == GL_R32F)
  205. return GL_RED;
  206. else if (format == GL_RG8 || format == GL_RG16 || format == GL_RG16F || format == GL_RG32F)
  207. return GL_RG;
  208. else if (format == GL_RGBA16 || format == GL_RGBA16F_ARB || format == GL_RGBA32F_ARB || format == GL_SRGB_ALPHA_EXT)
  209. return GL_RGBA;
  210. else if (format == GL_SRGB_EXT)
  211. return GL_RGB;
  212. else
  213. return format;
  214. #else
  215. return format;
  216. #endif
  217. }
  218. unsigned Texture::GetDataType(unsigned format)
  219. {
  220. #ifndef GL_ES_VERSION_2_0
  221. if (format == GL_DEPTH24_STENCIL8_EXT)
  222. return GL_UNSIGNED_INT_24_8_EXT;
  223. else if (format == GL_RG16 || format == GL_RGBA16)
  224. return GL_UNSIGNED_SHORT;
  225. else if (format == GL_RGBA32F_ARB || format == GL_RG32F || format == GL_R32F)
  226. return GL_FLOAT;
  227. else if (format == GL_RGBA16F_ARB || format == GL_RG16F || format == GL_R16F)
  228. return GL_HALF_FLOAT_ARB;
  229. else
  230. return GL_UNSIGNED_BYTE;
  231. #else
  232. if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_COMPONENT24_OES)
  233. return GL_UNSIGNED_INT;
  234. else if (format == GL_DEPTH_COMPONENT16)
  235. return GL_UNSIGNED_SHORT;
  236. else
  237. return GL_UNSIGNED_BYTE;
  238. #endif
  239. }
  240. unsigned Texture::GetSRGBFormat(unsigned format)
  241. {
  242. #ifndef GL_ES_VERSION_2_0
  243. if (!graphics_ || !graphics_->GetSRGBSupport())
  244. return format;
  245. switch (format)
  246. {
  247. case GL_RGB:
  248. return GL_SRGB_EXT;
  249. case GL_RGBA:
  250. return GL_SRGB_ALPHA_EXT;
  251. case GL_LUMINANCE:
  252. return GL_SLUMINANCE_EXT;
  253. case GL_LUMINANCE_ALPHA:
  254. return GL_SLUMINANCE_ALPHA_EXT;
  255. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  256. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
  257. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  258. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
  259. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  260. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
  261. default:
  262. return format;
  263. }
  264. #else
  265. return format;
  266. #endif
  267. }
  268. // ATOMIC BEGIN
  269. // Only used on D3D11, here to satisfy script binding linking
  270. unsigned Texture::GetSRVFormat(unsigned format)
  271. {
  272. return 0;
  273. }
  274. // Only used on D3D11, here to satisfy script binding linking
  275. unsigned Texture::GetDSVFormat(unsigned format)
  276. {
  277. return 0;
  278. }
  279. // ATOMIC END
  280. }