OGLTexture.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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_[COORD_U]]);
  123. glTexParameteri(target_, GL_TEXTURE_WRAP_T, glWrapModes[addressMode_[COORD_V]]);
  124. #ifndef GL_ES_VERSION_2_0
  125. glTexParameteri(target_, GL_TEXTURE_WRAP_R, glWrapModes[addressMode_[COORD_W]]);
  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. default:
  153. break;
  154. }
  155. #ifndef GL_ES_VERSION_2_0
  156. // Anisotropy
  157. if (graphics_->GetAnisotropySupport())
  158. {
  159. glTexParameterf(target_, GL_TEXTURE_MAX_ANISOTROPY_EXT, filterMode_ == FILTER_ANISOTROPIC ?
  160. (float)graphics_->GetTextureAnisotropy() : 1.0f);
  161. }
  162. // Shadow compare
  163. if (shadowCompare_)
  164. {
  165. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
  166. glTexParameteri(target_, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  167. }
  168. else
  169. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  170. glTexParameterfv(target_, GL_TEXTURE_BORDER_COLOR, borderColor_.Data());
  171. #endif
  172. parametersDirty_ = false;
  173. }
  174. bool Texture::IsCompressed() const
  175. {
  176. #ifndef GL_ES_VERSION_2_0
  177. return format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ||
  178. format_ == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  179. #else
  180. return format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_ETC1_RGB8_OES ||
  181. format_ == COMPRESSED_RGB_PVRTC_4BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_4BPPV1_IMG ||
  182. format_ == COMPRESSED_RGB_PVRTC_2BPPV1_IMG || format_ == COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  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, 16) * 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_LUMINANCE16F_ARB:
  232. case GL_LUMINANCE32F_ARB:
  233. case GL_DEPTH24_STENCIL8_EXT:
  234. case GL_RG16:
  235. #endif
  236. return width * 4;
  237. #ifndef GL_ES_VERSION_2_0
  238. case GL_RGBA16:
  239. return width * 8;
  240. case GL_RGBA16F_ARB:
  241. case GL_RGBA32F_ARB:
  242. return width * 16;
  243. #endif
  244. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  245. return ((width + 3) >> 2) * 8;
  246. #ifndef GL_ES_VERSION_2_0
  247. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  248. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  249. return ((width + 3) >> 2) * 16;
  250. #else
  251. case GL_ETC1_RGB8_OES:
  252. return ((width + 3) >> 2) * 8;
  253. case COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
  254. case COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
  255. return (width * 4 + 7) >> 3;
  256. case COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
  257. case COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
  258. return (width * 2 + 7) >> 3;
  259. #endif
  260. default:
  261. return 0;
  262. }
  263. }
  264. unsigned Texture::GetExternalFormat(unsigned format)
  265. {
  266. #ifndef GL_ES_VERSION_2_0
  267. // For DEPTH_COMPONENTxx textures DEPTH_COMPONENT needs to be returned
  268. if (format == GL_DEPTH_COMPONENT16 || format == GL_DEPTH_COMPONENT24 || format == GL_DEPTH_COMPONENT32)
  269. return GL_DEPTH_COMPONENT;
  270. else if (format == GL_DEPTH24_STENCIL8_EXT)
  271. return GL_DEPTH_STENCIL_EXT;
  272. else if (format == GL_LUMINANCE16F_ARB || format == GL_LUMINANCE32F_ARB)
  273. return GL_LUMINANCE;
  274. else if (format == GL_RG16 || format == GL_RG16F || format == GL_RG32F)
  275. return GL_RG;
  276. else if (format == GL_RGBA16 || format == GL_RGBA16F_ARB || format == GL_RGBA32F_ARB)
  277. return GL_RGBA;
  278. else
  279. return format;
  280. #else
  281. return format;
  282. #endif
  283. }
  284. unsigned Texture::GetDataType(unsigned format)
  285. {
  286. #ifndef GL_ES_VERSION_2_0
  287. if (format == GL_DEPTH24_STENCIL8_EXT)
  288. return GL_UNSIGNED_INT_24_8_EXT;
  289. else if (format == GL_RG16 || format == GL_RGBA16)
  290. return GL_UNSIGNED_SHORT;
  291. else if (format == GL_LUMINANCE16F_ARB || format == GL_LUMINANCE32F_ARB || format == GL_RGBA16F_ARB ||
  292. format == GL_RGBA32F_ARB)
  293. return GL_FLOAT;
  294. else
  295. return GL_UNSIGNED_BYTE;
  296. #else
  297. if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_COMPONENT24_OES)
  298. return GL_UNSIGNED_INT;
  299. else if (format == GL_DEPTH_COMPONENT16)
  300. return GL_UNSIGNED_SHORT;
  301. else
  302. return GL_UNSIGNED_BYTE;
  303. #endif
  304. }
  305. void Texture::LoadParameters()
  306. {
  307. ResourceCache* cache = GetSubsystem<ResourceCache>();
  308. String xmlName = ReplaceExtension(GetName(), ".xml");
  309. if (cache->Exists(xmlName))
  310. {
  311. XMLFile* file = cache->GetResource<XMLFile>(xmlName);
  312. LoadParameters(file);
  313. }
  314. }
  315. void Texture::LoadParameters(XMLFile* file)
  316. {
  317. if (!file)
  318. return;
  319. XMLElement rootElem = file->GetRoot();
  320. LoadParameters(rootElem);
  321. }
  322. void Texture::LoadParameters(const XMLElement& elem)
  323. {
  324. XMLElement paramElem = elem.GetChild();
  325. while (paramElem)
  326. {
  327. String name = paramElem.GetName();
  328. if (name == "address")
  329. {
  330. String coord = paramElem.GetAttributeLower("coord");
  331. if (coord.Length() >= 1)
  332. {
  333. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  334. String mode = paramElem.GetAttributeLower("mode");
  335. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode, addressModeNames, ADDRESS_WRAP));
  336. }
  337. }
  338. if (name == "border")
  339. SetBorderColor(paramElem.GetColor("color"));
  340. if (name == "filter")
  341. {
  342. String mode = paramElem.GetAttributeLower("mode");
  343. SetFilterMode((TextureFilterMode)GetStringListIndex(mode, filterModeNames, FILTER_DEFAULT));
  344. }
  345. if (name == "mipmap")
  346. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  347. if (name == "quality")
  348. {
  349. if (paramElem.HasAttribute("low"))
  350. mipsToSkip_[QUALITY_LOW] = Max(paramElem.GetInt("low"), 0);
  351. if (paramElem.HasAttribute("med"))
  352. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("med"), 0);
  353. if (paramElem.HasAttribute("medium"))
  354. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("medium"), 0);
  355. if (paramElem.HasAttribute("high"))
  356. mipsToSkip_[QUALITY_HIGH] = Max(paramElem.GetInt("high"), 0);
  357. // Make sure a higher quality level does not actually skip more mips
  358. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  359. {
  360. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  361. mipsToSkip_[i] = mipsToSkip_[i - 1];
  362. }
  363. }
  364. paramElem = paramElem.GetNext();
  365. }
  366. }
  367. void Texture::CheckTextureBudget(ShortStringHash type)
  368. {
  369. ResourceCache* cache = GetSubsystem<ResourceCache>();
  370. unsigned textureBudget = cache->GetMemoryBudget(type);
  371. unsigned textureUse = cache->GetMemoryUse(type);
  372. if (!textureBudget)
  373. return;
  374. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  375. // Therefore free unused materials first
  376. if (textureUse > textureBudget)
  377. cache->ReleaseResources(Material::GetTypeStatic());
  378. }
  379. }