OGLTexture.cpp 13 KB

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