OGLTexture.cpp 15 KB

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