OGLTexture.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "FileSystem.h"
  25. #include "Graphics.h"
  26. #include "GraphicsImpl.h"
  27. #include "Log.h"
  28. #include "Material.h"
  29. #include "Profiler.h"
  30. #include "ResourceCache.h"
  31. #include "StringUtils.h"
  32. #include "Texture.h"
  33. #include "XMLFile.h"
  34. #include "DebugNew.h"
  35. GLenum glWrapModes[] =
  36. {
  37. GL_REPEAT,
  38. GL_MIRRORED_REPEAT,
  39. GL_CLAMP_TO_EDGE,
  40. #ifndef GL_ES_VERSION_2_0
  41. GL_CLAMP
  42. #else
  43. GL_CLAMP_TO_EDGE
  44. #endif
  45. };
  46. static const String addressModeNames[] =
  47. {
  48. "wrap",
  49. "mirror",
  50. "clamp",
  51. "border",
  52. ""
  53. };
  54. static const String filterModeNames[] =
  55. {
  56. "nearest",
  57. "bilinear",
  58. "trilinear",
  59. "anisotropic",
  60. "default",
  61. ""
  62. };
  63. Texture::Texture(Context* context) :
  64. Resource(context),
  65. GPUObject(GetSubsystem<Graphics>()),
  66. levels_(0),
  67. requestedLevels_(0),
  68. width_(0),
  69. height_(0),
  70. dynamic_(false),
  71. shadowCompare_(false),
  72. parametersDirty_(true),
  73. dataLost_(false),
  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::ClearDataLost()
  117. {
  118. dataLost_ = false;
  119. }
  120. void Texture::UpdateParameters()
  121. {
  122. if (!object_ || !graphics_)
  123. return;
  124. // Wrapping
  125. glTexParameteri(target_, GL_TEXTURE_WRAP_S, glWrapModes[addressMode_[0]]);
  126. glTexParameteri(target_, GL_TEXTURE_WRAP_T, glWrapModes[addressMode_[1]]);
  127. #ifndef GL_ES_VERSION_2_0
  128. glTexParameteri(target_, GL_TEXTURE_WRAP_R, glWrapModes[addressMode_[2]]);
  129. #endif
  130. TextureFilterMode filterMode = filterMode_;
  131. if (filterMode == FILTER_DEFAULT)
  132. filterMode = graphics_->GetDefaultTextureFilterMode();
  133. // Filtering
  134. switch (filterMode)
  135. {
  136. case FILTER_NEAREST:
  137. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  138. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  139. break;
  140. case FILTER_BILINEAR:
  141. if (levels_ < 2)
  142. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  143. else
  144. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  145. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  146. break;
  147. case FILTER_ANISOTROPIC:
  148. case FILTER_TRILINEAR:
  149. if (levels_ < 2)
  150. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  151. else
  152. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  153. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  154. break;
  155. }
  156. #ifndef GL_ES_VERSION_2_0
  157. // Anisotropy
  158. glTexParameterf(target_, GL_TEXTURE_MAX_ANISOTROPY_EXT, filterMode_ == FILTER_ANISOTROPIC ?
  159. (float)graphics_->GetTextureAnisotropy() : 1.0f);
  160. // Shadow compare
  161. if (shadowCompare_)
  162. {
  163. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
  164. glTexParameteri(target_, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  165. }
  166. else
  167. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  168. glTexParameterfv(target_, GL_TEXTURE_BORDER_COLOR, borderColor_.Data());
  169. #endif
  170. parametersDirty_ = false;
  171. }
  172. bool Texture::IsCompressed() const
  173. {
  174. #ifndef GL_ES_VERSION_2_0
  175. return format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ||
  176. format_ == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  177. #else
  178. #ifdef ANDROID
  179. return format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_ETC1_RGB8_OES;
  180. #else
  181. return format_ == GL_ETC1_RGB8_OES;
  182. #endif
  183. #endif
  184. }
  185. int Texture::GetLevelWidth(unsigned level) const
  186. {
  187. if (level > levels_)
  188. return 0;
  189. return Max(width_ >> level, 1);
  190. }
  191. int Texture::GetLevelHeight(unsigned level) const
  192. {
  193. if (level > levels_)
  194. return 0;
  195. return Max(height_ >> level, 1);
  196. }
  197. TextureUsage Texture::GetUsage() const
  198. {
  199. /// \todo Check for rendertarget / depth-stencil mode
  200. if (dynamic_)
  201. return TEXTURE_DYNAMIC;
  202. return TEXTURE_STATIC;
  203. }
  204. unsigned Texture::GetDataSize(int width, int height) const
  205. {
  206. if (IsCompressed())
  207. {
  208. if (format_ == COMPRESSED_RGB_PVRTC_4BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_4BPPV1_IMG)
  209. return (Max(width_, 8) * Max(height_, 8) * 4 + 7) >> 3;
  210. else if (format_ == COMPRESSED_RGB_PVRTC_2BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_2BPPV1_IMG)
  211. return (Max(width_, 8) * Max(height_, 8) * 2 + 7) >> 3;
  212. else
  213. return GetRowDataSize(width) * ((height + 3) >> 2);
  214. }
  215. else
  216. return GetRowDataSize(width) * height;
  217. }
  218. unsigned Texture::GetRowDataSize(int width) const
  219. {
  220. switch (format_)
  221. {
  222. case GL_ALPHA:
  223. case GL_LUMINANCE:
  224. return width;
  225. case GL_LUMINANCE_ALPHA:
  226. return width * 2;
  227. case GL_RGB:
  228. return width * 3;
  229. case GL_RGBA:
  230. #ifndef GL_ES_VERSION_2_0
  231. case GL_LUMINANCE32F_ARB:
  232. case GL_DEPTH24_STENCIL8_EXT:
  233. #endif
  234. return width * 4;
  235. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  236. return ((width + 3) >> 2) * 8;
  237. #ifndef GL_ES_VERSION_2_0
  238. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  239. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  240. return ((width + 3) >> 2) * 16;
  241. #else
  242. case GL_ETC1_RGB8_OES:
  243. return ((width + 3) >> 2) * 8;
  244. case COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
  245. case COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
  246. return (width * 4 + 7) >> 3;
  247. case COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
  248. case COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
  249. return (width * 2 + 7) >> 3;
  250. #endif
  251. default:
  252. return 0;
  253. }
  254. }
  255. unsigned Texture::GetExternalFormat(unsigned format)
  256. {
  257. #ifndef GL_ES_VERSION_2_0
  258. // For DEPTH_COMPONENTxx textures DEPTH_COMPONENT needs to be returned
  259. if (format == GL_DEPTH_COMPONENT16 || format == GL_DEPTH_COMPONENT24 || format == GL_DEPTH_COMPONENT32)
  260. return GL_DEPTH_COMPONENT;
  261. else if (format == GL_DEPTH24_STENCIL8_EXT)
  262. return GL_DEPTH_STENCIL_EXT;
  263. else if (format == GL_LUMINANCE32F_ARB)
  264. return GL_LUMINANCE;
  265. else
  266. return format;
  267. #else
  268. return format;
  269. #endif
  270. }
  271. unsigned Texture::GetDataType(unsigned format)
  272. {
  273. #ifndef GL_ES_VERSION_2_0
  274. if (format == GL_DEPTH24_STENCIL8_EXT)
  275. return GL_UNSIGNED_INT_24_8_EXT;
  276. else
  277. return GL_UNSIGNED_BYTE;
  278. #else
  279. if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_COMPONENT24_OES)
  280. return GL_UNSIGNED_INT;
  281. else if (format == GL_DEPTH_COMPONENT16)
  282. return GL_UNSIGNED_SHORT;
  283. else
  284. return GL_UNSIGNED_BYTE;
  285. #endif
  286. }
  287. void Texture::LoadParameters()
  288. {
  289. ResourceCache* cache = GetSubsystem<ResourceCache>();
  290. String texPath, texName, texExt;
  291. SplitPath(GetName(), texPath, texName, texExt);
  292. String xmlName = texPath + texName + ".xml";
  293. if (cache->Exists(xmlName))
  294. {
  295. XMLFile* file = cache->GetResource<XMLFile>(xmlName);
  296. LoadParameters(file);
  297. }
  298. }
  299. void Texture::LoadParameters(XMLFile* file)
  300. {
  301. if (!file)
  302. return;
  303. XMLElement rootElem = file->GetRoot();
  304. LoadParameters(rootElem);
  305. }
  306. void Texture::LoadParameters(const XMLElement& elem)
  307. {
  308. XMLElement paramElem = elem.GetChild();
  309. while (paramElem)
  310. {
  311. String name = paramElem.GetName();
  312. if (name == "address")
  313. {
  314. String coord = paramElem.GetAttributeLower("coord");
  315. if (coord.Length() >= 1)
  316. {
  317. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  318. String mode = paramElem.GetAttributeLower("mode");
  319. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode, addressModeNames, ADDRESS_WRAP));
  320. }
  321. }
  322. if (name == "border")
  323. SetBorderColor(paramElem.GetColor("color"));
  324. if (name == "filter")
  325. {
  326. String mode = paramElem.GetAttributeLower("mode");
  327. SetFilterMode((TextureFilterMode)GetStringListIndex(mode, filterModeNames, FILTER_DEFAULT));
  328. }
  329. if (name == "mipmap")
  330. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  331. if (name == "quality")
  332. {
  333. if (paramElem.HasAttribute("low"))
  334. mipsToSkip_[QUALITY_LOW] = Max(paramElem.GetInt("low"), 0);
  335. if (paramElem.HasAttribute("med"))
  336. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("med"), 0);
  337. if (paramElem.HasAttribute("medium"))
  338. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("medium"), 0);
  339. if (paramElem.HasAttribute("high"))
  340. mipsToSkip_[QUALITY_HIGH] = Max(paramElem.GetInt("high"), 0);
  341. // Make sure a higher quality level does not actually skip more mips
  342. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  343. {
  344. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  345. mipsToSkip_[i] = mipsToSkip_[i - 1];
  346. }
  347. }
  348. paramElem = paramElem.GetNext();
  349. }
  350. }
  351. void Texture::CheckTextureBudget(ShortStringHash type)
  352. {
  353. ResourceCache* cache = GetSubsystem<ResourceCache>();
  354. unsigned textureBudget = cache->GetMemoryBudget(type);
  355. unsigned textureUse = cache->GetMemoryUse(type);
  356. if (!textureBudget)
  357. return;
  358. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  359. // Therefore free unused materials first
  360. if (textureUse > textureBudget)
  361. cache->ReleaseResources(Material::GetTypeStatic());
  362. }