OGLTexture.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "Material.h"
  27. #include "Profiler.h"
  28. #include "ResourceCache.h"
  29. #include "StringUtils.h"
  30. #include "Texture.h"
  31. #include "XMLFile.h"
  32. #include <GLee.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. dataLost_(false),
  67. dynamic_(false),
  68. shadowCompare_(false),
  69. parametersDirty_(true),
  70. filterMode_(FILTER_DEFAULT)
  71. {
  72. for (int i = 0; i < MAX_COORDS; ++i)
  73. addressMode_[i] = ADDRESS_WRAP;
  74. for (int i = 0; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  75. mipsToSkip_[i] = MAX_TEXTURE_QUALITY_LEVELS - 1 - i;
  76. }
  77. Texture::~Texture()
  78. {
  79. }
  80. void Texture::SetNumLevels(unsigned levels)
  81. {
  82. requestedLevels_ = levels;
  83. }
  84. void Texture::SetFilterMode(TextureFilterMode mode)
  85. {
  86. filterMode_ = mode;
  87. parametersDirty_ = true;
  88. }
  89. void Texture::SetAddressMode(TextureCoordinate coord, TextureAddressMode mode)
  90. {
  91. addressMode_[coord] = mode;
  92. parametersDirty_ = true;
  93. }
  94. void Texture::SetShadowCompare(bool enable)
  95. {
  96. shadowCompare_ = enable;
  97. parametersDirty_ = true;
  98. }
  99. void Texture::SetBorderColor(const Color& color)
  100. {
  101. borderColor_ = color;
  102. parametersDirty_ = true;
  103. }
  104. void Texture::SetBackupTexture(Texture* texture)
  105. {
  106. backupTexture_ = texture;
  107. }
  108. void Texture::SetParametersDirty()
  109. {
  110. parametersDirty_ = true;
  111. }
  112. void Texture::ClearDataLost()
  113. {
  114. dataLost_ = false;
  115. }
  116. void Texture::UpdateParameters()
  117. {
  118. if ((!object_) || (!graphics_))
  119. return;
  120. // Wrapping
  121. glTexParameteri(target_, GL_TEXTURE_WRAP_S, glWrapModes[addressMode_[0]]);
  122. glTexParameteri(target_, GL_TEXTURE_WRAP_T, glWrapModes[addressMode_[1]]);
  123. TextureFilterMode filterMode = filterMode_;
  124. if (filterMode == FILTER_DEFAULT)
  125. filterMode = graphics_->GetDefaultTextureFilterMode();
  126. // Filtering
  127. switch (filterMode)
  128. {
  129. case FILTER_NEAREST:
  130. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  131. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  132. break;
  133. case FILTER_BILINEAR:
  134. if (levels_ < 2)
  135. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  136. else
  137. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  138. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  139. break;
  140. case FILTER_ANISOTROPIC:
  141. case FILTER_TRILINEAR:
  142. if (levels_ < 2)
  143. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  144. else
  145. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  146. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  147. break;
  148. }
  149. // Anisotropy
  150. glTexParameterf(target_, GL_TEXTURE_MAX_ANISOTROPY_EXT, filterMode_ == FILTER_ANISOTROPIC ?
  151. (float)graphics_->GetTextureAnisotropy() : 1.0f);
  152. // Shadow compare
  153. if (shadowCompare_)
  154. {
  155. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
  156. glTexParameteri(target_, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  157. }
  158. else
  159. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  160. glTexParameterfv(target_, GL_TEXTURE_BORDER_COLOR, borderColor_.GetData());
  161. parametersDirty_ = false;
  162. }
  163. TextureUsage Texture::GetUsage() const
  164. {
  165. /// \todo Check for rendertarget / depthstencil mode
  166. if (dynamic_)
  167. return TEXTURE_DYNAMIC;
  168. return TEXTURE_STATIC;
  169. }
  170. unsigned Texture::GetDXTFormat(CompressedFormat format)
  171. {
  172. switch (format)
  173. {
  174. case CF_DXT1:
  175. return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  176. case CF_DXT3:
  177. return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  178. case CF_DXT5:
  179. return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  180. }
  181. return 0;
  182. }
  183. unsigned Texture::GetExternalFormat(unsigned format)
  184. {
  185. // For DEPTH_COMPONENTxx textures DEPTH_COMPONENT needs to be returned
  186. if ((format == GL_DEPTH_COMPONENT16) || (format == GL_DEPTH_COMPONENT24) || (format == GL_DEPTH_COMPONENT32))
  187. return GL_DEPTH_COMPONENT;
  188. else if (format == GL_DEPTH24_STENCIL8_EXT)
  189. return GL_DEPTH_STENCIL_EXT;
  190. else
  191. return format;
  192. }
  193. unsigned Texture::GetDataType(unsigned format)
  194. {
  195. if (format == GL_DEPTH24_STENCIL8_EXT)
  196. return GL_UNSIGNED_INT_24_8_EXT;
  197. else
  198. return GL_UNSIGNED_BYTE;
  199. }
  200. void Texture::LoadParameters()
  201. {
  202. ResourceCache* cache = GetSubsystem<ResourceCache>();
  203. String texPath, texName, texExt;
  204. SplitPath(GetName(), texPath, texName, texExt);
  205. String xmlName = texPath + texName + ".xml";
  206. if (cache->Exists(xmlName))
  207. {
  208. XMLFile* file = cache->GetResource<XMLFile>(xmlName);
  209. LoadParameters(file);
  210. }
  211. }
  212. void Texture::LoadParameters(XMLFile* file)
  213. {
  214. if (!file)
  215. return;
  216. XMLElement rootElem = file->GetRootElement();
  217. XMLElement paramElem = rootElem.GetChildElement("");
  218. while (paramElem)
  219. {
  220. String name = paramElem.GetName();
  221. if (name == "address")
  222. {
  223. String coord = paramElem.GetStringLower("coord");
  224. if (coord.Length() >= 1)
  225. {
  226. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  227. String mode = paramElem.GetStringLower("mode");
  228. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode, addressModeNames, ADDRESS_WRAP));
  229. }
  230. }
  231. if (name == "border")
  232. SetBorderColor(paramElem.GetColor("color"));
  233. if (name == "filter")
  234. {
  235. String mode = paramElem.GetStringLower("mode");
  236. SetFilterMode((TextureFilterMode)GetStringListIndex(mode, filterModeNames, FILTER_DEFAULT));
  237. }
  238. if (name == "mipmap")
  239. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  240. if (name == "quality")
  241. {
  242. if (paramElem.HasAttribute("low"))
  243. mipsToSkip_[QUALITY_LOW] = Max(paramElem.GetInt("low"), 0);
  244. if (paramElem.HasAttribute("med"))
  245. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("med"), 0);
  246. if (paramElem.HasAttribute("medium"))
  247. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("medium"), 0);
  248. if (paramElem.HasAttribute("high"))
  249. mipsToSkip_[QUALITY_HIGH] = Max(paramElem.GetInt("high"), 0);
  250. // Make sure a higher quality level does not actually skip more mips
  251. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  252. {
  253. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  254. mipsToSkip_[i] = mipsToSkip_[i - 1];
  255. }
  256. }
  257. paramElem = paramElem.GetNextElement();
  258. }
  259. }
  260. void Texture::CheckTextureBudget(ShortStringHash type)
  261. {
  262. ResourceCache* cache = GetSubsystem<ResourceCache>();
  263. unsigned textureBudget = cache->GetMemoryBudget(type);
  264. unsigned textureUse = cache->GetMemoryUse(type);
  265. if (!textureBudget)
  266. return;
  267. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  268. // Therefore free unused materials first
  269. if (textureUse > textureBudget)
  270. cache->ReleaseResources(Material::GetTypeStatic());
  271. }