OGLTexture.cpp 13 KB

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