OGLTexture.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. if (graphics_->GetAnisotropySupport())
  154. {
  155. glTexParameterf(target_, GL_TEXTURE_MAX_ANISOTROPY_EXT, filterMode_ == FILTER_ANISOTROPIC ?
  156. (float)graphics_->GetTextureAnisotropy() : 1.0f);
  157. }
  158. // Shadow compare
  159. if (shadowCompare_)
  160. {
  161. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
  162. glTexParameteri(target_, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  163. }
  164. else
  165. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  166. glTexParameterfv(target_, GL_TEXTURE_BORDER_COLOR, borderColor_.Data());
  167. #endif
  168. parametersDirty_ = false;
  169. }
  170. bool Texture::IsCompressed() const
  171. {
  172. #ifndef GL_ES_VERSION_2_0
  173. return format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ||
  174. format_ == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  175. #else
  176. return format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_ETC1_RGB8_OES ||
  177. format_ == COMPRESSED_RGB_PVRTC_4BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_4BPPV1_IMG ||
  178. format_ == COMPRESSED_RGB_PVRTC_2BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  179. #endif
  180. }
  181. int Texture::GetLevelWidth(unsigned level) const
  182. {
  183. if (level > levels_)
  184. return 0;
  185. return Max(width_ >> level, 1);
  186. }
  187. int Texture::GetLevelHeight(unsigned level) const
  188. {
  189. if (level > levels_)
  190. return 0;
  191. return Max(height_ >> level, 1);
  192. }
  193. TextureUsage Texture::GetUsage() const
  194. {
  195. /// \todo Check for rendertarget / depth-stencil mode
  196. if (dynamic_)
  197. return TEXTURE_DYNAMIC;
  198. return TEXTURE_STATIC;
  199. }
  200. unsigned Texture::GetDataSize(int width, int height) const
  201. {
  202. if (IsCompressed())
  203. {
  204. if (format_ == COMPRESSED_RGB_PVRTC_4BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_4BPPV1_IMG)
  205. return (Max(width, 8) * Max(height, 8) * 4 + 7) >> 3;
  206. else if (format_ == COMPRESSED_RGB_PVRTC_2BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_2BPPV1_IMG)
  207. return (Max(width, 16) * Max(height, 8) * 2 + 7) >> 3;
  208. else
  209. return GetRowDataSize(width) * ((height + 3) >> 2);
  210. }
  211. else
  212. return GetRowDataSize(width) * height;
  213. }
  214. unsigned Texture::GetRowDataSize(int width) const
  215. {
  216. switch (format_)
  217. {
  218. case GL_ALPHA:
  219. case GL_LUMINANCE:
  220. return width;
  221. case GL_LUMINANCE_ALPHA:
  222. return width * 2;
  223. case GL_RGB:
  224. return width * 3;
  225. case GL_RGBA:
  226. #ifndef GL_ES_VERSION_2_0
  227. case GL_LUMINANCE32F_ARB:
  228. case GL_DEPTH24_STENCIL8_EXT:
  229. #endif
  230. return width * 4;
  231. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  232. return ((width + 3) >> 2) * 8;
  233. #ifndef GL_ES_VERSION_2_0
  234. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  235. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  236. return ((width + 3) >> 2) * 16;
  237. #else
  238. case GL_ETC1_RGB8_OES:
  239. return ((width + 3) >> 2) * 8;
  240. case COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
  241. case COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
  242. return (width * 4 + 7) >> 3;
  243. case COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
  244. case COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
  245. return (width * 2 + 7) >> 3;
  246. #endif
  247. default:
  248. return 0;
  249. }
  250. }
  251. unsigned Texture::GetExternalFormat(unsigned format)
  252. {
  253. #ifndef GL_ES_VERSION_2_0
  254. // For DEPTH_COMPONENTxx textures DEPTH_COMPONENT needs to be returned
  255. if (format == GL_DEPTH_COMPONENT16 || format == GL_DEPTH_COMPONENT24 || format == GL_DEPTH_COMPONENT32)
  256. return GL_DEPTH_COMPONENT;
  257. else if (format == GL_DEPTH24_STENCIL8_EXT)
  258. return GL_DEPTH_STENCIL_EXT;
  259. else if (format == GL_LUMINANCE32F_ARB)
  260. return GL_LUMINANCE;
  261. else
  262. return format;
  263. #else
  264. return format;
  265. #endif
  266. }
  267. unsigned Texture::GetDataType(unsigned format)
  268. {
  269. #ifndef GL_ES_VERSION_2_0
  270. if (format == GL_DEPTH24_STENCIL8_EXT)
  271. return GL_UNSIGNED_INT_24_8_EXT;
  272. else
  273. return GL_UNSIGNED_BYTE;
  274. #else
  275. if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_COMPONENT24_OES)
  276. return GL_UNSIGNED_INT;
  277. else if (format == GL_DEPTH_COMPONENT16)
  278. return GL_UNSIGNED_SHORT;
  279. else
  280. return GL_UNSIGNED_BYTE;
  281. #endif
  282. }
  283. void Texture::LoadParameters()
  284. {
  285. ResourceCache* cache = GetSubsystem<ResourceCache>();
  286. String texPath, texName, texExt;
  287. SplitPath(GetName(), texPath, texName, texExt);
  288. String xmlName = texPath + texName + ".xml";
  289. if (cache->Exists(xmlName))
  290. {
  291. XMLFile* file = cache->GetResource<XMLFile>(xmlName);
  292. LoadParameters(file);
  293. }
  294. }
  295. void Texture::LoadParameters(XMLFile* file)
  296. {
  297. if (!file)
  298. return;
  299. XMLElement rootElem = file->GetRoot();
  300. LoadParameters(rootElem);
  301. }
  302. void Texture::LoadParameters(const XMLElement& elem)
  303. {
  304. XMLElement paramElem = elem.GetChild();
  305. while (paramElem)
  306. {
  307. String name = paramElem.GetName();
  308. if (name == "address")
  309. {
  310. String coord = paramElem.GetAttributeLower("coord");
  311. if (coord.Length() >= 1)
  312. {
  313. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  314. String mode = paramElem.GetAttributeLower("mode");
  315. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode, addressModeNames, ADDRESS_WRAP));
  316. }
  317. }
  318. if (name == "border")
  319. SetBorderColor(paramElem.GetColor("color"));
  320. if (name == "filter")
  321. {
  322. String mode = paramElem.GetAttributeLower("mode");
  323. SetFilterMode((TextureFilterMode)GetStringListIndex(mode, filterModeNames, FILTER_DEFAULT));
  324. }
  325. if (name == "mipmap")
  326. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  327. if (name == "quality")
  328. {
  329. if (paramElem.HasAttribute("low"))
  330. mipsToSkip_[QUALITY_LOW] = Max(paramElem.GetInt("low"), 0);
  331. if (paramElem.HasAttribute("med"))
  332. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("med"), 0);
  333. if (paramElem.HasAttribute("medium"))
  334. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("medium"), 0);
  335. if (paramElem.HasAttribute("high"))
  336. mipsToSkip_[QUALITY_HIGH] = Max(paramElem.GetInt("high"), 0);
  337. // Make sure a higher quality level does not actually skip more mips
  338. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  339. {
  340. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  341. mipsToSkip_[i] = mipsToSkip_[i - 1];
  342. }
  343. }
  344. paramElem = paramElem.GetNext();
  345. }
  346. }
  347. void Texture::CheckTextureBudget(ShortStringHash type)
  348. {
  349. ResourceCache* cache = GetSubsystem<ResourceCache>();
  350. unsigned textureBudget = cache->GetMemoryBudget(type);
  351. unsigned textureUse = cache->GetMemoryUse(type);
  352. if (!textureBudget)
  353. return;
  354. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  355. // Therefore free unused materials first
  356. if (textureUse > textureBudget)
  357. cache->ReleaseResources(Material::GetTypeStatic());
  358. }