OGLTexture.cpp 12 KB

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