OGLTexture.cpp 14 KB

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