OGLTexture.cpp 14 KB

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