OGLTexture.cpp 14 KB

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