OGLTexture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 "Material.h"
  28. #include "Profiler.h"
  29. #include "ResourceCache.h"
  30. #include "StringUtils.h"
  31. #include "Texture.h"
  32. #include "XMLFile.h"
  33. #include "DebugNew.h"
  34. GLenum glWrapModes[] =
  35. {
  36. GL_REPEAT,
  37. GL_MIRRORED_REPEAT,
  38. GL_CLAMP_TO_EDGE,
  39. GL_CLAMP
  40. };
  41. static const String addressModeNames[] =
  42. {
  43. "wrap",
  44. "mirror",
  45. "clamp",
  46. "border",
  47. ""
  48. };
  49. static const String filterModeNames[] =
  50. {
  51. "nearest",
  52. "bilinear",
  53. "trilinear",
  54. "anisotropic",
  55. "default",
  56. ""
  57. };
  58. Texture::Texture(Context* context) :
  59. Resource(context),
  60. GPUObject(GetSubsystem<Graphics>()),
  61. levels_(0),
  62. requestedLevels_(0),
  63. depthBits_(0),
  64. width_(0),
  65. height_(0),
  66. dynamic_(false),
  67. shadowCompare_(false),
  68. parametersDirty_(true),
  69. filterMode_(FILTER_DEFAULT)
  70. {
  71. for (int i = 0; i < MAX_COORDS; ++i)
  72. addressMode_[i] = ADDRESS_WRAP;
  73. for (int i = 0; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  74. mipsToSkip_[i] = MAX_TEXTURE_QUALITY_LEVELS - 1 - i;
  75. }
  76. Texture::~Texture()
  77. {
  78. }
  79. void Texture::SetNumLevels(unsigned levels)
  80. {
  81. requestedLevels_ = levels;
  82. }
  83. void Texture::SetFilterMode(TextureFilterMode mode)
  84. {
  85. filterMode_ = mode;
  86. parametersDirty_ = true;
  87. }
  88. void Texture::SetAddressMode(TextureCoordinate coord, TextureAddressMode mode)
  89. {
  90. addressMode_[coord] = mode;
  91. parametersDirty_ = true;
  92. }
  93. void Texture::SetShadowCompare(bool enable)
  94. {
  95. shadowCompare_ = enable;
  96. parametersDirty_ = true;
  97. }
  98. void Texture::SetBorderColor(const Color& color)
  99. {
  100. borderColor_ = color;
  101. parametersDirty_ = true;
  102. }
  103. void Texture::SetBackupTexture(Texture* texture)
  104. {
  105. backupTexture_ = texture;
  106. }
  107. void Texture::SetParametersDirty()
  108. {
  109. parametersDirty_ = true;
  110. }
  111. void Texture::ClearDataLost()
  112. {
  113. }
  114. void Texture::UpdateParameters()
  115. {
  116. if (!object_ || !graphics_)
  117. return;
  118. // Wrapping
  119. glTexParameteri(target_, GL_TEXTURE_WRAP_S, glWrapModes[addressMode_[0]]);
  120. glTexParameteri(target_, GL_TEXTURE_WRAP_T, glWrapModes[addressMode_[1]]);
  121. glTexParameteri(target_, GL_TEXTURE_WRAP_R, glWrapModes[addressMode_[2]]);
  122. TextureFilterMode filterMode = filterMode_;
  123. if (filterMode == FILTER_DEFAULT)
  124. filterMode = graphics_->GetDefaultTextureFilterMode();
  125. // Filtering
  126. switch (filterMode)
  127. {
  128. case FILTER_NEAREST:
  129. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  130. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  131. break;
  132. case FILTER_BILINEAR:
  133. if (levels_ < 2)
  134. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  135. else
  136. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  137. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  138. break;
  139. case FILTER_ANISOTROPIC:
  140. case FILTER_TRILINEAR:
  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_LINEAR);
  145. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  146. break;
  147. }
  148. // Anisotropy
  149. glTexParameterf(target_, GL_TEXTURE_MAX_ANISOTROPY_EXT, filterMode_ == FILTER_ANISOTROPIC ?
  150. (float)graphics_->GetTextureAnisotropy() : 1.0f);
  151. // Shadow compare
  152. if (shadowCompare_)
  153. {
  154. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
  155. glTexParameteri(target_, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  156. }
  157. else
  158. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  159. glTexParameterfv(target_, GL_TEXTURE_BORDER_COLOR, borderColor_.Data());
  160. parametersDirty_ = false;
  161. }
  162. int Texture::GetLevelWidth(unsigned level) const
  163. {
  164. if (level > levels_)
  165. return 0;
  166. return Max(width_ >> level, 1);
  167. }
  168. int Texture::GetLevelHeight(unsigned level) const
  169. {
  170. if (level > levels_)
  171. return 0;
  172. return Max(height_ >> level, 1);
  173. }
  174. TextureUsage Texture::GetUsage() const
  175. {
  176. /// \todo Check for rendertarget / depth-stencil mode
  177. if (dynamic_)
  178. return TEXTURE_DYNAMIC;
  179. return TEXTURE_STATIC;
  180. }
  181. unsigned Texture::GetDataSize(int width, int height) const
  182. {
  183. if (format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ||
  184. format_ == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
  185. return GetRowDataSize(width) * ((height + 3) >> 2);
  186. else
  187. return GetRowDataSize(width) * height;
  188. }
  189. unsigned Texture::GetRowDataSize(int width) const
  190. {
  191. switch (format_)
  192. {
  193. case GL_ALPHA:
  194. case GL_LUMINANCE:
  195. return width;
  196. case GL_LUMINANCE_ALPHA:
  197. return width * 2;
  198. case GL_RGB:
  199. return width * 3;
  200. case GL_RGBA:
  201. case GL_LUMINANCE32F_ARB:
  202. case GL_DEPTH24_STENCIL8_EXT:
  203. return width * 4;
  204. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  205. return ((width + 3) >> 2) * 8;
  206. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  207. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  208. return ((width + 3) >> 2) * 16;
  209. default:
  210. return 0;
  211. }
  212. }
  213. unsigned Texture::GetDXTFormat(CompressedFormat format)
  214. {
  215. switch (format)
  216. {
  217. case CF_DXT1:
  218. return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  219. case CF_DXT3:
  220. return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  221. case CF_DXT5:
  222. return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  223. }
  224. return 0;
  225. }
  226. unsigned Texture::GetExternalFormat(unsigned format)
  227. {
  228. // For DEPTH_COMPONENTxx textures DEPTH_COMPONENT needs to be returned
  229. if (format == GL_DEPTH_COMPONENT16 || format == GL_DEPTH_COMPONENT24 || format == GL_DEPTH_COMPONENT32)
  230. return GL_DEPTH_COMPONENT;
  231. else if (format == GL_DEPTH24_STENCIL8_EXT)
  232. return GL_DEPTH_STENCIL_EXT;
  233. else if (format == GL_LUMINANCE32F_ARB)
  234. return GL_LUMINANCE;
  235. else
  236. return format;
  237. }
  238. unsigned Texture::GetDataType(unsigned format)
  239. {
  240. if (format == GL_DEPTH24_STENCIL8_EXT)
  241. return GL_UNSIGNED_INT_24_8_EXT;
  242. else
  243. return GL_UNSIGNED_BYTE;
  244. }
  245. void Texture::LoadParameters()
  246. {
  247. ResourceCache* cache = GetSubsystem<ResourceCache>();
  248. String texPath, texName, texExt;
  249. SplitPath(GetName(), texPath, texName, texExt);
  250. String xmlName = texPath + texName + ".xml";
  251. if (cache->Exists(xmlName))
  252. {
  253. XMLFile* file = cache->GetResource<XMLFile>(xmlName);
  254. LoadParameters(file);
  255. }
  256. }
  257. void Texture::LoadParameters(XMLFile* file)
  258. {
  259. if (!file)
  260. return;
  261. XMLElement rootElem = file->GetRoot();
  262. LoadParameters(rootElem);
  263. }
  264. void Texture::LoadParameters(const XMLElement& elem)
  265. {
  266. XMLElement paramElem = elem.GetChild();
  267. while (paramElem)
  268. {
  269. String name = paramElem.GetName();
  270. if (name == "address")
  271. {
  272. String coord = paramElem.GetAttributeLower("coord");
  273. if (coord.Length() >= 1)
  274. {
  275. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  276. String mode = paramElem.GetAttributeLower("mode");
  277. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode, addressModeNames, ADDRESS_WRAP));
  278. }
  279. }
  280. if (name == "border")
  281. SetBorderColor(paramElem.GetColor("color"));
  282. if (name == "filter")
  283. {
  284. String mode = paramElem.GetAttributeLower("mode");
  285. SetFilterMode((TextureFilterMode)GetStringListIndex(mode, filterModeNames, FILTER_DEFAULT));
  286. }
  287. if (name == "mipmap")
  288. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  289. if (name == "quality")
  290. {
  291. if (paramElem.HasAttribute("low"))
  292. mipsToSkip_[QUALITY_LOW] = Max(paramElem.GetInt("low"), 0);
  293. if (paramElem.HasAttribute("med"))
  294. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("med"), 0);
  295. if (paramElem.HasAttribute("medium"))
  296. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("medium"), 0);
  297. if (paramElem.HasAttribute("high"))
  298. mipsToSkip_[QUALITY_HIGH] = Max(paramElem.GetInt("high"), 0);
  299. // Make sure a higher quality level does not actually skip more mips
  300. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  301. {
  302. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  303. mipsToSkip_[i] = mipsToSkip_[i - 1];
  304. }
  305. }
  306. paramElem = paramElem.GetNext();
  307. }
  308. }
  309. void Texture::CheckTextureBudget(ShortStringHash type)
  310. {
  311. ResourceCache* cache = GetSubsystem<ResourceCache>();
  312. unsigned textureBudget = cache->GetMemoryBudget(type);
  313. unsigned textureUse = cache->GetMemoryUse(type);
  314. if (!textureBudget)
  315. return;
  316. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  317. // Therefore free unused materials first
  318. if (textureUse > textureBudget)
  319. cache->ReleaseResources(Material::GetTypeStatic());
  320. }