OGLTexture.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. #ifndef GL_ES_VERSION_2_0
  278. case GL_DEPTH24_STENCIL8_EXT:
  279. case GL_RG16:
  280. case GL_R16F:
  281. case GL_R32F:
  282. #endif
  283. return (unsigned)(width * 4);
  284. #ifndef GL_ES_VERSION_2_0
  285. case GL_R8:
  286. return (unsigned)width;
  287. case GL_RG8:
  288. return (unsigned)(width * 2);
  289. case GL_RGBA16:
  290. return (unsigned)(width * 8);
  291. case GL_RGBA16F_ARB:
  292. case GL_RGBA32F_ARB:
  293. return (unsigned)(width * 16);
  294. #endif
  295. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  296. return (unsigned)(((width + 3) >> 2) * 8);
  297. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  298. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  299. return (unsigned)(((width + 3) >> 2) * 16);
  300. case GL_ETC1_RGB8_OES:
  301. return (unsigned)(((width + 3) >> 2) * 8);
  302. case COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
  303. case COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
  304. return (unsigned)((width * 4 + 7) >> 3);
  305. case COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
  306. case COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
  307. return (unsigned)((width * 2 + 7) >> 3);
  308. default:
  309. return 0;
  310. }
  311. }
  312. unsigned Texture::GetComponents() const
  313. {
  314. if (!width_ || IsCompressed())
  315. return 0;
  316. else
  317. return GetRowDataSize(width_) / width_;
  318. }
  319. unsigned Texture::GetExternalFormat(unsigned format)
  320. {
  321. #ifndef GL_ES_VERSION_2_0
  322. if (format == GL_DEPTH_COMPONENT16 || format == GL_DEPTH_COMPONENT24 || format == GL_DEPTH_COMPONENT32)
  323. return GL_DEPTH_COMPONENT;
  324. else if (format == GL_DEPTH24_STENCIL8_EXT)
  325. return GL_DEPTH_STENCIL_EXT;
  326. else if (format == GL_SLUMINANCE_EXT)
  327. return GL_LUMINANCE;
  328. else if (format == GL_SLUMINANCE_ALPHA_EXT)
  329. return GL_LUMINANCE_ALPHA;
  330. else if (format == GL_R8 || format == GL_R16F || format == GL_R32F)
  331. return GL_RED;
  332. else if (format == GL_RG8 || format == GL_RG16 || format == GL_RG16F || format == GL_RG32F)
  333. return GL_RG;
  334. else if (format == GL_RGBA16 || format == GL_RGBA16F_ARB || format == GL_RGBA32F_ARB || format == GL_SRGB_ALPHA_EXT)
  335. return GL_RGBA;
  336. else if (format == GL_SRGB_EXT)
  337. return GL_RGB;
  338. else
  339. return format;
  340. #else
  341. return format;
  342. #endif
  343. }
  344. unsigned Texture::GetDataType(unsigned format)
  345. {
  346. #ifndef GL_ES_VERSION_2_0
  347. if (format == GL_DEPTH24_STENCIL8_EXT)
  348. return GL_UNSIGNED_INT_24_8_EXT;
  349. else if (format == GL_RG16 || format == GL_RGBA16)
  350. return GL_UNSIGNED_SHORT;
  351. else if (format == GL_RGBA16F_ARB || format == GL_RGBA32F_ARB || format == GL_RG16F || format == GL_RG32F ||
  352. format == GL_R16F || format == GL_R32F)
  353. return GL_FLOAT;
  354. else
  355. return GL_UNSIGNED_BYTE;
  356. #else
  357. if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_COMPONENT24_OES)
  358. return GL_UNSIGNED_INT;
  359. else if (format == GL_DEPTH_COMPONENT16)
  360. return GL_UNSIGNED_SHORT;
  361. else
  362. return GL_UNSIGNED_BYTE;
  363. #endif
  364. }
  365. void Texture::SetParameters(XMLFile* file)
  366. {
  367. if (!file)
  368. return;
  369. XMLElement rootElem = file->GetRoot();
  370. SetParameters(rootElem);
  371. }
  372. void Texture::SetParameters(const XMLElement& elem)
  373. {
  374. XMLElement paramElem = elem.GetChild();
  375. while (paramElem)
  376. {
  377. String name = paramElem.GetName();
  378. if (name == "address")
  379. {
  380. String coord = paramElem.GetAttributeLower("coord");
  381. if (coord.Length() >= 1)
  382. {
  383. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  384. String mode = paramElem.GetAttributeLower("mode");
  385. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode.CString(), addressModeNames, ADDRESS_WRAP));
  386. }
  387. }
  388. if (name == "border")
  389. SetBorderColor(paramElem.GetColor("color"));
  390. if (name == "filter")
  391. {
  392. String mode = paramElem.GetAttributeLower("mode");
  393. SetFilterMode((TextureFilterMode)GetStringListIndex(mode.CString(), filterModeNames, FILTER_DEFAULT));
  394. }
  395. if (name == "mipmap")
  396. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  397. if (name == "quality")
  398. {
  399. if (paramElem.HasAttribute("low"))
  400. SetMipsToSkip(QUALITY_LOW, paramElem.GetInt("low"));
  401. if (paramElem.HasAttribute("med"))
  402. SetMipsToSkip(QUALITY_MEDIUM, paramElem.GetInt("med"));
  403. if (paramElem.HasAttribute("medium"))
  404. SetMipsToSkip(QUALITY_MEDIUM, paramElem.GetInt("medium"));
  405. if (paramElem.HasAttribute("high"))
  406. SetMipsToSkip(QUALITY_HIGH, paramElem.GetInt("high"));
  407. }
  408. if (name == "srgb")
  409. SetSRGB(paramElem.GetBool("enable"));
  410. paramElem = paramElem.GetNext();
  411. }
  412. }
  413. unsigned Texture::GetSRGBFormat(unsigned format)
  414. {
  415. #ifndef GL_ES_VERSION_2_0
  416. if (!graphics_ || !graphics_->GetSRGBSupport())
  417. return format;
  418. switch (format)
  419. {
  420. case GL_RGB:
  421. return GL_SRGB_EXT;
  422. case GL_RGBA:
  423. return GL_SRGB_ALPHA_EXT;
  424. case GL_LUMINANCE:
  425. return GL_SLUMINANCE_EXT;
  426. case GL_LUMINANCE_ALPHA:
  427. return GL_SLUMINANCE_ALPHA_EXT;
  428. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  429. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
  430. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  431. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
  432. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  433. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
  434. default:
  435. return format;
  436. }
  437. #else
  438. return format;
  439. #endif
  440. }
  441. void Texture::CheckTextureBudget(StringHash type)
  442. {
  443. ResourceCache* cache = GetSubsystem<ResourceCache>();
  444. unsigned textureBudget = cache->GetMemoryBudget(type);
  445. unsigned textureUse = cache->GetMemoryUse(type);
  446. if (!textureBudget)
  447. return;
  448. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  449. // Therefore free unused materials first
  450. if (textureUse > textureBudget)
  451. cache->ReleaseResources(Material::GetTypeStatic());
  452. }
  453. }