OGLTexture.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 "../../IO/FileSystem.h"
  24. #include "../../Graphics/Graphics.h"
  25. #include "../../Graphics/GraphicsImpl.h"
  26. #include "../../IO/Log.h"
  27. #include "../../Graphics/Material.h"
  28. #include "../../Core/Profiler.h"
  29. #include "../../Graphics/RenderSurface.h"
  30. #include "../../Resource/ResourceCache.h"
  31. #include "../../Graphics/Texture.h"
  32. #include "../../Resource/XMLFile.h"
  33. #include "../../DebugNew.h"
  34. namespace Atomic
  35. {
  36. static GLenum glWrapModes[] =
  37. {
  38. GL_REPEAT,
  39. GL_MIRRORED_REPEAT,
  40. GL_CLAMP_TO_EDGE,
  41. #ifndef GL_ES_VERSION_2_0
  42. GL_CLAMP
  43. #else
  44. GL_CLAMP_TO_EDGE
  45. #endif
  46. };
  47. #ifndef GL_ES_VERSION_2_0
  48. static GLenum gl3WrapModes[] =
  49. {
  50. GL_REPEAT,
  51. GL_MIRRORED_REPEAT,
  52. GL_CLAMP_TO_EDGE,
  53. GL_CLAMP_TO_BORDER
  54. };
  55. #endif
  56. static const char* addressModeNames[] =
  57. {
  58. "wrap",
  59. "mirror",
  60. "clamp",
  61. "border",
  62. 0
  63. };
  64. static const char* filterModeNames[] =
  65. {
  66. "nearest",
  67. "bilinear",
  68. "trilinear",
  69. "anisotropic",
  70. "default",
  71. 0
  72. };
  73. static GLenum GetWrapMode(TextureAddressMode mode)
  74. {
  75. #ifndef GL_ES_VERSION_2_0
  76. return Graphics::GetGL3Support() ? gl3WrapModes[mode] : glWrapModes[mode];
  77. #else
  78. return glWrapModes[mode];
  79. #endif
  80. }
  81. Texture::Texture(Context* context) :
  82. Resource(context),
  83. GPUObject(GetSubsystem<Graphics>()),
  84. usage_(TEXTURE_STATIC),
  85. format_(0),
  86. levels_(0),
  87. requestedLevels_(0),
  88. width_(0),
  89. height_(0),
  90. depth_(0),
  91. shadowCompare_(false),
  92. parametersDirty_(true),
  93. filterMode_(FILTER_DEFAULT),
  94. sRGB_(false)
  95. {
  96. for (int i = 0; i < MAX_COORDS; ++i)
  97. addressMode_[i] = ADDRESS_WRAP;
  98. for (int i = 0; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  99. mipsToSkip_[i] = MAX_TEXTURE_QUALITY_LEVELS - 1 - i;
  100. }
  101. Texture::~Texture()
  102. {
  103. }
  104. void Texture::SetNumLevels(unsigned levels)
  105. {
  106. requestedLevels_ = levels;
  107. }
  108. void Texture::SetFilterMode(TextureFilterMode mode)
  109. {
  110. filterMode_ = mode;
  111. parametersDirty_ = true;
  112. }
  113. void Texture::SetAddressMode(TextureCoordinate coord, TextureAddressMode mode)
  114. {
  115. addressMode_[coord] = mode;
  116. parametersDirty_ = true;
  117. }
  118. void Texture::SetShadowCompare(bool enable)
  119. {
  120. shadowCompare_ = enable;
  121. parametersDirty_ = true;
  122. }
  123. void Texture::SetBorderColor(const Color& color)
  124. {
  125. borderColor_ = color;
  126. parametersDirty_ = true;
  127. }
  128. void Texture::SetSRGB(bool enable)
  129. {
  130. if (graphics_)
  131. enable &= graphics_->GetSRGBSupport();
  132. if (enable != sRGB_)
  133. {
  134. sRGB_ = enable;
  135. // If texture had already been created, must recreate it to set the sRGB texture format
  136. if (object_)
  137. Create();
  138. // If texture in use in the framebuffer, mark it dirty
  139. if (graphics_ && graphics_->GetRenderTarget(0) && graphics_->GetRenderTarget(0)->GetParentTexture() == this)
  140. graphics_->MarkFBODirty();
  141. }
  142. }
  143. void Texture::SetBackupTexture(Texture* texture)
  144. {
  145. backupTexture_ = texture;
  146. }
  147. void Texture::SetMipsToSkip(int quality, int mips)
  148. {
  149. if (quality >= QUALITY_LOW && quality < MAX_TEXTURE_QUALITY_LEVELS)
  150. {
  151. mipsToSkip_[quality] = mips;
  152. // Make sure a higher quality level does not actually skip more mips
  153. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  154. {
  155. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  156. mipsToSkip_[i] = mipsToSkip_[i - 1];
  157. }
  158. }
  159. }
  160. void Texture::SetParametersDirty()
  161. {
  162. parametersDirty_ = true;
  163. }
  164. void Texture::UpdateParameters()
  165. {
  166. if (!object_ || !graphics_)
  167. return;
  168. // Wrapping
  169. glTexParameteri(target_, GL_TEXTURE_WRAP_S, GetWrapMode(addressMode_[COORD_U]));
  170. glTexParameteri(target_, GL_TEXTURE_WRAP_T, GetWrapMode(addressMode_[COORD_V]));
  171. #ifndef GL_ES_VERSION_2_0
  172. glTexParameteri(target_, GL_TEXTURE_WRAP_R, GetWrapMode(addressMode_[COORD_W]));
  173. #endif
  174. TextureFilterMode filterMode = filterMode_;
  175. if (filterMode == FILTER_DEFAULT)
  176. filterMode = graphics_->GetDefaultTextureFilterMode();
  177. // Filtering
  178. switch (filterMode)
  179. {
  180. case FILTER_NEAREST:
  181. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  182. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  183. break;
  184. case FILTER_BILINEAR:
  185. if (levels_ < 2)
  186. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  187. else
  188. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  189. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  190. break;
  191. case FILTER_ANISOTROPIC:
  192. case FILTER_TRILINEAR:
  193. if (levels_ < 2)
  194. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  195. else
  196. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  197. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  198. break;
  199. default:
  200. break;
  201. }
  202. #ifndef GL_ES_VERSION_2_0
  203. // Anisotropy
  204. if (graphics_->GetAnisotropySupport())
  205. {
  206. glTexParameterf(target_, GL_TEXTURE_MAX_ANISOTROPY_EXT, filterMode == FILTER_ANISOTROPIC ?
  207. (float)graphics_->GetTextureAnisotropy() : 1.0f);
  208. }
  209. // Shadow compare
  210. if (shadowCompare_)
  211. {
  212. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
  213. glTexParameteri(target_, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  214. }
  215. else
  216. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  217. glTexParameterfv(target_, GL_TEXTURE_BORDER_COLOR, borderColor_.Data());
  218. #endif
  219. parametersDirty_ = false;
  220. }
  221. int Texture::GetMipsToSkip(int quality) const
  222. {
  223. return (quality >= QUALITY_LOW && quality < MAX_TEXTURE_QUALITY_LEVELS) ? mipsToSkip_[quality] : 0;
  224. }
  225. bool Texture::IsCompressed() const
  226. {
  227. return format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ||
  228. format_ == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT || format_ == GL_ETC1_RGB8_OES ||
  229. format_ == COMPRESSED_RGB_PVRTC_4BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_4BPPV1_IMG ||
  230. format_ == COMPRESSED_RGB_PVRTC_2BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  231. }
  232. int Texture::GetLevelWidth(unsigned level) const
  233. {
  234. if (level > levels_)
  235. return 0;
  236. return Max(width_ >> level, 1);
  237. }
  238. int Texture::GetLevelHeight(unsigned level) const
  239. {
  240. if (level > levels_)
  241. return 0;
  242. return Max(height_ >> level, 1);
  243. }
  244. int Texture::GetLevelDepth(unsigned level) const
  245. {
  246. if (level > levels_)
  247. return 0;
  248. return Max(depth_ >> level, 1);
  249. }
  250. unsigned Texture::GetDataSize(int width, int height) const
  251. {
  252. if (IsCompressed())
  253. {
  254. if (format_ == COMPRESSED_RGB_PVRTC_4BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_4BPPV1_IMG)
  255. return (Max(width, 8) * Max(height, 8) * 4 + 7) >> 3;
  256. else if (format_ == COMPRESSED_RGB_PVRTC_2BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_2BPPV1_IMG)
  257. return (Max(width, 16) * Max(height, 8) * 2 + 7) >> 3;
  258. else
  259. return GetRowDataSize(width) * ((height + 3) >> 2);
  260. }
  261. else
  262. return GetRowDataSize(width) * height;
  263. }
  264. unsigned Texture::GetDataSize(int width, int height, int depth) const
  265. {
  266. return depth * GetDataSize(width, height);
  267. }
  268. unsigned Texture::GetRowDataSize(int width) const
  269. {
  270. switch (format_)
  271. {
  272. case GL_ALPHA:
  273. case GL_LUMINANCE:
  274. return width;
  275. case GL_LUMINANCE_ALPHA:
  276. return width * 2;
  277. case GL_RGB:
  278. return width * 3;
  279. case GL_RGBA:
  280. #ifndef GL_ES_VERSION_2_0
  281. case GL_DEPTH24_STENCIL8_EXT:
  282. case GL_RG16:
  283. case GL_R16F:
  284. case GL_R32F:
  285. #endif
  286. return width * 4;
  287. #ifndef GL_ES_VERSION_2_0
  288. case GL_R8:
  289. return width;
  290. case GL_RG8:
  291. return width * 2;
  292. case GL_RGBA16:
  293. return width * 8;
  294. case GL_RGBA16F_ARB:
  295. case GL_RGBA32F_ARB:
  296. return width * 16;
  297. #endif
  298. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  299. return ((width + 3) >> 2) * 8;
  300. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  301. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  302. return ((width + 3) >> 2) * 16;
  303. case GL_ETC1_RGB8_OES:
  304. return ((width + 3) >> 2) * 8;
  305. case COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
  306. case COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
  307. return (width * 4 + 7) >> 3;
  308. case COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
  309. case COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
  310. return (width * 2 + 7) >> 3;
  311. default:
  312. return 0;
  313. }
  314. }
  315. unsigned Texture::GetComponents() const
  316. {
  317. if (!width_ || IsCompressed())
  318. return 0;
  319. else
  320. return GetRowDataSize(width_) / width_;
  321. }
  322. unsigned Texture::GetExternalFormat(unsigned format)
  323. {
  324. #ifndef GL_ES_VERSION_2_0
  325. if (format == GL_DEPTH_COMPONENT16 || format == GL_DEPTH_COMPONENT24 || format == GL_DEPTH_COMPONENT32)
  326. return GL_DEPTH_COMPONENT;
  327. else if (format == GL_DEPTH24_STENCIL8_EXT)
  328. return GL_DEPTH_STENCIL_EXT;
  329. else if (format == GL_SLUMINANCE_EXT)
  330. return GL_LUMINANCE;
  331. else if (format == GL_SLUMINANCE_ALPHA_EXT)
  332. return GL_LUMINANCE_ALPHA;
  333. else if (format == GL_R8 || format == GL_R16F || format == GL_R32F)
  334. return GL_RED;
  335. else if (format == GL_RG8 || format == GL_RG16 || format == GL_RG16F || format == GL_RG32F)
  336. return GL_RG;
  337. else if (format == GL_RGBA16 || format == GL_RGBA16F_ARB || format == GL_RGBA32F_ARB || format == GL_SRGB_ALPHA_EXT)
  338. return GL_RGBA;
  339. else if (format == GL_SRGB_EXT)
  340. return GL_RGB;
  341. else
  342. return format;
  343. #else
  344. return format;
  345. #endif
  346. }
  347. unsigned Texture::GetDataType(unsigned format)
  348. {
  349. #ifndef GL_ES_VERSION_2_0
  350. if (format == GL_DEPTH24_STENCIL8_EXT)
  351. return GL_UNSIGNED_INT_24_8_EXT;
  352. else if (format == GL_RG16 || format == GL_RGBA16)
  353. return GL_UNSIGNED_SHORT;
  354. else if (format == GL_RGBA16F_ARB || format == GL_RGBA32F_ARB || format == GL_RG16F || format == GL_RG32F || format == GL_R16F ||
  355. format == GL_R32F)
  356. return GL_FLOAT;
  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. }