OGLTexture.cpp 14 KB

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