OGLTexture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //
  2. // Copyright (c) 2008-2017 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. // 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(addressMode_[COORD_U]));
  90. glTexParameteri(target_, GL_TEXTURE_WRAP_T, GetWrapMode(addressMode_[COORD_V]));
  91. #ifndef GL_ES_VERSION_2_0
  92. glTexParameteri(target_, GL_TEXTURE_WRAP_R, GetWrapMode(addressMode_[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_ == COMPRESSED_RGB_PVRTC_4BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_4BPPV1_IMG ||
  161. format_ == COMPRESSED_RGB_PVRTC_2BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  162. }
  163. unsigned Texture::GetRowDataSize(int width) const
  164. {
  165. switch (format_)
  166. {
  167. case GL_ALPHA:
  168. case GL_LUMINANCE:
  169. return (unsigned)width;
  170. case GL_LUMINANCE_ALPHA:
  171. return (unsigned)(width * 2);
  172. case GL_RGB:
  173. return (unsigned)(width * 3);
  174. case GL_RGBA:
  175. #ifndef GL_ES_VERSION_2_0
  176. case GL_DEPTH24_STENCIL8_EXT:
  177. case GL_RG16:
  178. case GL_RG16F:
  179. case GL_R32F:
  180. #endif
  181. return (unsigned)(width * 4);
  182. #ifndef GL_ES_VERSION_2_0
  183. case GL_R8:
  184. return (unsigned)width;
  185. case GL_RG8:
  186. case GL_R16F:
  187. return (unsigned)(width * 2);
  188. case GL_RGBA16:
  189. case GL_RGBA16F_ARB:
  190. return (unsigned)(width * 8);
  191. case GL_RGBA32F_ARB:
  192. return (unsigned)(width * 16);
  193. #endif
  194. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  195. return (unsigned)(((width + 3) >> 2) * 8);
  196. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  197. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  198. return (unsigned)(((width + 3) >> 2) * 16);
  199. case GL_ETC1_RGB8_OES:
  200. return (unsigned)(((width + 3) >> 2) * 8);
  201. case COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
  202. case COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
  203. return (unsigned)((width * 4 + 7) >> 3);
  204. case COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
  205. case COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
  206. return (unsigned)((width * 2 + 7) >> 3);
  207. default:
  208. return 0;
  209. }
  210. }
  211. unsigned Texture::GetExternalFormat(unsigned format)
  212. {
  213. #ifndef GL_ES_VERSION_2_0
  214. if (format == GL_DEPTH_COMPONENT16 || format == GL_DEPTH_COMPONENT24 || format == GL_DEPTH_COMPONENT32)
  215. return GL_DEPTH_COMPONENT;
  216. else if (format == GL_DEPTH24_STENCIL8_EXT)
  217. return GL_DEPTH_STENCIL_EXT;
  218. else if (format == GL_SLUMINANCE_EXT)
  219. return GL_LUMINANCE;
  220. else if (format == GL_SLUMINANCE_ALPHA_EXT)
  221. return GL_LUMINANCE_ALPHA;
  222. else if (format == GL_R8 || format == GL_R16F || format == GL_R32F)
  223. return GL_RED;
  224. else if (format == GL_RG8 || format == GL_RG16 || format == GL_RG16F || format == GL_RG32F)
  225. return GL_RG;
  226. else if (format == GL_RGBA16 || format == GL_RGBA16F_ARB || format == GL_RGBA32F_ARB || format == GL_SRGB_ALPHA_EXT)
  227. return GL_RGBA;
  228. else if (format == GL_SRGB_EXT)
  229. return GL_RGB;
  230. else
  231. return format;
  232. #else
  233. return format;
  234. #endif
  235. }
  236. unsigned Texture::GetDataType(unsigned format)
  237. {
  238. #ifndef GL_ES_VERSION_2_0
  239. if (format == GL_DEPTH24_STENCIL8_EXT)
  240. return GL_UNSIGNED_INT_24_8_EXT;
  241. else if (format == GL_RG16 || format == GL_RGBA16)
  242. return GL_UNSIGNED_SHORT;
  243. else if (format == GL_RGBA32F_ARB || format == GL_RG32F || format == GL_R32F)
  244. return GL_FLOAT;
  245. else if (format == GL_RGBA16F_ARB || format == GL_RG16F || format == GL_R16F)
  246. return GL_HALF_FLOAT_ARB;
  247. else
  248. return GL_UNSIGNED_BYTE;
  249. #else
  250. if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_COMPONENT24_OES)
  251. return GL_UNSIGNED_INT;
  252. else if (format == GL_DEPTH_COMPONENT16)
  253. return GL_UNSIGNED_SHORT;
  254. else
  255. return GL_UNSIGNED_BYTE;
  256. #endif
  257. }
  258. unsigned Texture::GetSRGBFormat(unsigned format)
  259. {
  260. #ifndef GL_ES_VERSION_2_0
  261. if (!graphics_ || !graphics_->GetSRGBSupport())
  262. return format;
  263. switch (format)
  264. {
  265. case GL_RGB:
  266. return GL_SRGB_EXT;
  267. case GL_RGBA:
  268. return GL_SRGB_ALPHA_EXT;
  269. case GL_LUMINANCE:
  270. return GL_SLUMINANCE_EXT;
  271. case GL_LUMINANCE_ALPHA:
  272. return GL_SLUMINANCE_ALPHA_EXT;
  273. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  274. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
  275. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  276. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
  277. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  278. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
  279. default:
  280. return format;
  281. }
  282. #else
  283. return format;
  284. #endif
  285. }
  286. void Texture::RegenerateLevels()
  287. {
  288. if (!object_.name_)
  289. return;
  290. #ifndef GL_ES_VERSION_2_0
  291. if (Graphics::GetGL3Support())
  292. glGenerateMipmap(target_);
  293. else
  294. glGenerateMipmapEXT(target_);
  295. #else
  296. glGenerateMipmap(target_);
  297. #endif
  298. levelsDirty_ = false;
  299. }
  300. // ATOMIC BEGIN
  301. // Only used on D3D11, here to satisfy script binding linking
  302. unsigned Texture::GetSRVFormat(unsigned format)
  303. {
  304. return 0;
  305. }
  306. // Only used on D3D11, here to satisfy script binding linking
  307. unsigned Texture::GetDSVFormat(unsigned format)
  308. {
  309. return 0;
  310. }
  311. // ATOMIC END
  312. }