OGLTexture.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. //
  2. // Copyright (c) 2008-2015 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 const char* addressModeNames[] =
  54. {
  55. "wrap",
  56. "mirror",
  57. "clamp",
  58. "border",
  59. 0
  60. };
  61. static const char* filterModeNames[] =
  62. {
  63. "nearest",
  64. "bilinear",
  65. "trilinear",
  66. "anisotropic",
  67. "default",
  68. 0
  69. };
  70. static GLenum GetWrapMode(TextureAddressMode mode)
  71. {
  72. #ifndef GL_ES_VERSION_2_0
  73. return Graphics::GetGL3Support() ? gl3WrapModes[mode] : glWrapModes[mode];
  74. #else
  75. return glWrapModes[mode];
  76. #endif
  77. }
  78. Texture::Texture(Context* context) :
  79. Resource(context),
  80. GPUObject(GetSubsystem<Graphics>()),
  81. usage_(TEXTURE_STATIC),
  82. format_(0),
  83. levels_(0),
  84. requestedLevels_(0),
  85. width_(0),
  86. height_(0),
  87. depth_(0),
  88. shadowCompare_(false),
  89. parametersDirty_(true),
  90. filterMode_(FILTER_DEFAULT),
  91. sRGB_(false)
  92. {
  93. for (int i = 0; i < MAX_COORDS; ++i)
  94. addressMode_[i] = ADDRESS_WRAP;
  95. for (int i = 0; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  96. mipsToSkip_[i] = (unsigned)(MAX_TEXTURE_QUALITY_LEVELS - 1 - i);
  97. }
  98. Texture::~Texture()
  99. {
  100. }
  101. void Texture::SetNumLevels(unsigned levels)
  102. {
  103. requestedLevels_ = levels;
  104. }
  105. void Texture::SetFilterMode(TextureFilterMode mode)
  106. {
  107. filterMode_ = mode;
  108. parametersDirty_ = true;
  109. }
  110. void Texture::SetAddressMode(TextureCoordinate coord, TextureAddressMode mode)
  111. {
  112. addressMode_[coord] = mode;
  113. parametersDirty_ = true;
  114. }
  115. void Texture::SetShadowCompare(bool enable)
  116. {
  117. shadowCompare_ = enable;
  118. parametersDirty_ = true;
  119. }
  120. void Texture::SetBorderColor(const Color& color)
  121. {
  122. borderColor_ = color;
  123. parametersDirty_ = true;
  124. }
  125. void Texture::SetSRGB(bool enable)
  126. {
  127. if (graphics_)
  128. enable &= graphics_->GetSRGBSupport();
  129. if (enable != sRGB_)
  130. {
  131. sRGB_ = enable;
  132. // If texture had already been created, must recreate it to set the sRGB texture format
  133. if (object_)
  134. Create();
  135. // If texture in use in the framebuffer, mark it dirty
  136. if (graphics_ && graphics_->GetRenderTarget(0) && graphics_->GetRenderTarget(0)->GetParentTexture() == this)
  137. graphics_->MarkFBODirty();
  138. }
  139. }
  140. void Texture::SetBackupTexture(Texture* texture)
  141. {
  142. backupTexture_ = texture;
  143. }
  144. void Texture::SetMipsToSkip(int quality, int mips)
  145. {
  146. if (quality >= QUALITY_LOW && quality < MAX_TEXTURE_QUALITY_LEVELS)
  147. {
  148. mipsToSkip_[quality] = (unsigned)mips;
  149. // Make sure a higher quality level does not actually skip more mips
  150. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  151. {
  152. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  153. mipsToSkip_[i] = mipsToSkip_[i - 1];
  154. }
  155. }
  156. }
  157. void Texture::SetParametersDirty()
  158. {
  159. parametersDirty_ = true;
  160. }
  161. void Texture::UpdateParameters()
  162. {
  163. if (!object_ || !graphics_)
  164. return;
  165. // Wrapping
  166. glTexParameteri(target_, GL_TEXTURE_WRAP_S, GetWrapMode(addressMode_[COORD_U]));
  167. glTexParameteri(target_, GL_TEXTURE_WRAP_T, GetWrapMode(addressMode_[COORD_V]));
  168. #ifndef GL_ES_VERSION_2_0
  169. glTexParameteri(target_, GL_TEXTURE_WRAP_R, GetWrapMode(addressMode_[COORD_W]));
  170. #endif
  171. TextureFilterMode filterMode = filterMode_;
  172. if (filterMode == FILTER_DEFAULT)
  173. filterMode = graphics_->GetDefaultTextureFilterMode();
  174. // Filtering
  175. switch (filterMode)
  176. {
  177. case FILTER_NEAREST:
  178. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  179. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  180. break;
  181. case FILTER_BILINEAR:
  182. if (levels_ < 2)
  183. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  184. else
  185. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  186. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  187. break;
  188. case FILTER_ANISOTROPIC:
  189. case FILTER_TRILINEAR:
  190. if (levels_ < 2)
  191. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  192. else
  193. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  194. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  195. break;
  196. default:
  197. break;
  198. }
  199. #ifndef GL_ES_VERSION_2_0
  200. // Anisotropy
  201. if (graphics_->GetAnisotropySupport())
  202. {
  203. glTexParameterf(target_, GL_TEXTURE_MAX_ANISOTROPY_EXT,
  204. filterMode == FILTER_ANISOTROPIC ? (float)graphics_->GetTextureAnisotropy() : 1.0f);
  205. }
  206. // Shadow compare
  207. if (shadowCompare_)
  208. {
  209. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
  210. glTexParameteri(target_, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  211. }
  212. else
  213. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  214. glTexParameterfv(target_, GL_TEXTURE_BORDER_COLOR, borderColor_.Data());
  215. #endif
  216. parametersDirty_ = false;
  217. }
  218. int Texture::GetMipsToSkip(int quality) const
  219. {
  220. return (quality >= QUALITY_LOW && quality < MAX_TEXTURE_QUALITY_LEVELS) ? mipsToSkip_[quality] : 0;
  221. }
  222. bool Texture::IsCompressed() const
  223. {
  224. return format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ||
  225. format_ == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT || format_ == GL_ETC1_RGB8_OES ||
  226. format_ == COMPRESSED_RGB_PVRTC_4BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_4BPPV1_IMG ||
  227. format_ == COMPRESSED_RGB_PVRTC_2BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  228. }
  229. int Texture::GetLevelWidth(unsigned level) const
  230. {
  231. if (level > levels_)
  232. return 0;
  233. return Max(width_ >> level, 1);
  234. }
  235. int Texture::GetLevelHeight(unsigned level) const
  236. {
  237. if (level > levels_)
  238. return 0;
  239. return Max(height_ >> level, 1);
  240. }
  241. int Texture::GetLevelDepth(unsigned level) const
  242. {
  243. if (level > levels_)
  244. return 0;
  245. return Max(depth_ >> level, 1);
  246. }
  247. unsigned Texture::GetDataSize(int width, int height) const
  248. {
  249. if (IsCompressed())
  250. {
  251. if (format_ == COMPRESSED_RGB_PVRTC_4BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_4BPPV1_IMG)
  252. return (unsigned)((Max(width, 8) * Max(height, 8) * 4 + 7) >> 3);
  253. else if (format_ == COMPRESSED_RGB_PVRTC_2BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_2BPPV1_IMG)
  254. return (unsigned)((Max(width, 16) * Max(height, 8) * 2 + 7) >> 3);
  255. else
  256. return GetRowDataSize(width) * ((height + 3) >> 2);
  257. }
  258. else
  259. return GetRowDataSize(width) * height;
  260. }
  261. unsigned Texture::GetDataSize(int width, int height, int depth) const
  262. {
  263. return depth * GetDataSize(width, height);
  264. }
  265. unsigned Texture::GetRowDataSize(int width) const
  266. {
  267. switch (format_)
  268. {
  269. case GL_ALPHA:
  270. case GL_LUMINANCE:
  271. return (unsigned)width;
  272. case GL_LUMINANCE_ALPHA:
  273. return (unsigned)(width * 2);
  274. case GL_RGB:
  275. return (unsigned)(width * 3);
  276. case GL_RGBA:
  277. case GL_BGRA:
  278. #ifndef GL_ES_VERSION_2_0
  279. case GL_DEPTH24_STENCIL8_EXT:
  280. case GL_RG16:
  281. case GL_R16F:
  282. case GL_R32F:
  283. #endif
  284. return (unsigned)(width * 4);
  285. #ifndef GL_ES_VERSION_2_0
  286. case GL_R8:
  287. return (unsigned)width;
  288. case GL_RG8:
  289. return (unsigned)(width * 2);
  290. case GL_RGBA16:
  291. return (unsigned)(width * 8);
  292. case GL_RGBA16F_ARB:
  293. case GL_RGBA32F_ARB:
  294. return (unsigned)(width * 16);
  295. #endif
  296. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  297. return (unsigned)(((width + 3) >> 2) * 8);
  298. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  299. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  300. return (unsigned)(((width + 3) >> 2) * 16);
  301. case GL_ETC1_RGB8_OES:
  302. return (unsigned)(((width + 3) >> 2) * 8);
  303. case COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
  304. case COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
  305. return (unsigned)((width * 4 + 7) >> 3);
  306. case COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
  307. case COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
  308. return (unsigned)((width * 2 + 7) >> 3);
  309. default:
  310. return 0;
  311. }
  312. }
  313. unsigned Texture::GetComponents() const
  314. {
  315. if (!width_ || IsCompressed())
  316. return 0;
  317. else
  318. return GetRowDataSize(width_) / width_;
  319. }
  320. unsigned Texture::GetExternalFormat(unsigned format)
  321. {
  322. #ifndef GL_ES_VERSION_2_0
  323. if (format == GL_DEPTH_COMPONENT16 || format == GL_DEPTH_COMPONENT24 || format == GL_DEPTH_COMPONENT32)
  324. return GL_DEPTH_COMPONENT;
  325. else if (format == GL_DEPTH24_STENCIL8_EXT)
  326. return GL_DEPTH_STENCIL_EXT;
  327. else if (format == GL_SLUMINANCE_EXT)
  328. return GL_LUMINANCE;
  329. else if (format == GL_SLUMINANCE_ALPHA_EXT)
  330. return GL_LUMINANCE_ALPHA;
  331. else if (format == GL_R8 || format == GL_R16F || format == GL_R32F)
  332. return GL_RED;
  333. else if (format == GL_RG8 || format == GL_RG16 || format == GL_RG16F || format == GL_RG32F)
  334. return GL_RG;
  335. else if (format == GL_RGBA16 || format == GL_RGBA16F_ARB || format == GL_RGBA32F_ARB || format == GL_SRGB_ALPHA_EXT)
  336. return GL_RGBA;
  337. else if (format == GL_SRGB_EXT)
  338. return GL_RGB;
  339. else
  340. return format;
  341. #else
  342. return format;
  343. #endif
  344. }
  345. unsigned Texture::GetDataType(unsigned format)
  346. {
  347. #ifndef GL_ES_VERSION_2_0
  348. if (format == GL_DEPTH24_STENCIL8_EXT)
  349. return GL_UNSIGNED_INT_24_8_EXT;
  350. else if (format == GL_RG16 || format == GL_RGBA16)
  351. return GL_UNSIGNED_SHORT;
  352. else if (format == GL_RGBA16F_ARB || format == GL_RGBA32F_ARB || format == GL_RG16F || format == GL_RG32F ||
  353. format == GL_R16F || format == GL_R32F)
  354. return GL_FLOAT;
  355. else if (format == GL_BGRA)
  356. return GL_UNSIGNED_INT_8_8_8_8_REV;
  357. else
  358. return GL_UNSIGNED_BYTE;
  359. #else
  360. if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_COMPONENT24_OES)
  361. return GL_UNSIGNED_INT;
  362. else if (format == GL_DEPTH_COMPONENT16)
  363. return GL_UNSIGNED_SHORT;
  364. else
  365. return GL_UNSIGNED_BYTE;
  366. #endif
  367. }
  368. void Texture::SetParameters(XMLFile* file)
  369. {
  370. if (!file)
  371. return;
  372. XMLElement rootElem = file->GetRoot();
  373. SetParameters(rootElem);
  374. }
  375. void Texture::SetParameters(const XMLElement& elem)
  376. {
  377. XMLElement paramElem = elem.GetChild();
  378. while (paramElem)
  379. {
  380. String name = paramElem.GetName();
  381. if (name == "address")
  382. {
  383. String coord = paramElem.GetAttributeLower("coord");
  384. if (coord.Length() >= 1)
  385. {
  386. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  387. String mode = paramElem.GetAttributeLower("mode");
  388. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode.CString(), addressModeNames, ADDRESS_WRAP));
  389. }
  390. }
  391. if (name == "border")
  392. SetBorderColor(paramElem.GetColor("color"));
  393. if (name == "filter")
  394. {
  395. String mode = paramElem.GetAttributeLower("mode");
  396. SetFilterMode((TextureFilterMode)GetStringListIndex(mode.CString(), filterModeNames, FILTER_DEFAULT));
  397. }
  398. if (name == "mipmap")
  399. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  400. if (name == "quality")
  401. {
  402. if (paramElem.HasAttribute("low"))
  403. SetMipsToSkip(QUALITY_LOW, paramElem.GetInt("low"));
  404. if (paramElem.HasAttribute("med"))
  405. SetMipsToSkip(QUALITY_MEDIUM, paramElem.GetInt("med"));
  406. if (paramElem.HasAttribute("medium"))
  407. SetMipsToSkip(QUALITY_MEDIUM, paramElem.GetInt("medium"));
  408. if (paramElem.HasAttribute("high"))
  409. SetMipsToSkip(QUALITY_HIGH, paramElem.GetInt("high"));
  410. }
  411. if (name == "srgb")
  412. SetSRGB(paramElem.GetBool("enable"));
  413. paramElem = paramElem.GetNext();
  414. }
  415. }
  416. unsigned Texture::GetSRGBFormat(unsigned format)
  417. {
  418. #ifndef GL_ES_VERSION_2_0
  419. if (!graphics_ || !graphics_->GetSRGBSupport())
  420. return format;
  421. switch (format)
  422. {
  423. case GL_RGB:
  424. return GL_SRGB_EXT;
  425. case GL_RGBA:
  426. return GL_SRGB_ALPHA_EXT;
  427. case GL_LUMINANCE:
  428. return GL_SLUMINANCE_EXT;
  429. case GL_LUMINANCE_ALPHA:
  430. return GL_SLUMINANCE_ALPHA_EXT;
  431. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  432. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
  433. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  434. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
  435. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  436. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
  437. default:
  438. return format;
  439. }
  440. #else
  441. return format;
  442. #endif
  443. }
  444. void Texture::CheckTextureBudget(StringHash type)
  445. {
  446. ResourceCache* cache = GetSubsystem<ResourceCache>();
  447. unsigned textureBudget = cache->GetMemoryBudget(type);
  448. unsigned textureUse = cache->GetMemoryUse(type);
  449. if (!textureBudget)
  450. return;
  451. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  452. // Therefore free unused materials first
  453. if (textureUse > textureBudget)
  454. cache->ReleaseResources(Material::GetTypeStatic());
  455. }
  456. }