OGLTexture.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 "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. TextureFilterMode filterMode = filterMode_;
  122. if (filterMode == FILTER_DEFAULT)
  123. filterMode = graphics_->GetDefaultTextureFilterMode();
  124. // Filtering
  125. switch (filterMode)
  126. {
  127. case FILTER_NEAREST:
  128. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  129. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  130. break;
  131. case FILTER_BILINEAR:
  132. if (levels_ < 2)
  133. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  134. else
  135. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  136. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  137. break;
  138. case FILTER_ANISOTROPIC:
  139. case FILTER_TRILINEAR:
  140. if (levels_ < 2)
  141. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  142. else
  143. glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  144. glTexParameteri(target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  145. break;
  146. }
  147. // Anisotropy
  148. glTexParameterf(target_, GL_TEXTURE_MAX_ANISOTROPY_EXT, filterMode_ == FILTER_ANISOTROPIC ?
  149. (float)graphics_->GetTextureAnisotropy() : 1.0f);
  150. // Shadow compare
  151. if (shadowCompare_)
  152. {
  153. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
  154. glTexParameteri(target_, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  155. }
  156. else
  157. glTexParameteri(target_, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  158. glTexParameterfv(target_, GL_TEXTURE_BORDER_COLOR, borderColor_.GetData());
  159. parametersDirty_ = false;
  160. }
  161. int Texture::GetLevelWidth(unsigned level) const
  162. {
  163. if (level > levels_)
  164. return 0;
  165. return Max(width_ >> level, 1);
  166. }
  167. int Texture::GetLevelHeight(unsigned level) const
  168. {
  169. if (level > levels_)
  170. return 0;
  171. return Max(height_ >> level, 1);
  172. }
  173. TextureUsage Texture::GetUsage() const
  174. {
  175. /// \todo Check for render target / depth stencil mode
  176. if (dynamic_)
  177. return TEXTURE_DYNAMIC;
  178. return TEXTURE_STATIC;
  179. }
  180. unsigned Texture::GetDataSize(int width, int height) const
  181. {
  182. if (format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ||
  183. format_ == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
  184. return GetRowDataSize(width) * ((height + 3) >> 2);
  185. else
  186. return GetRowDataSize(width) * height;
  187. }
  188. unsigned Texture::GetRowDataSize(int width) const
  189. {
  190. switch (format_)
  191. {
  192. case GL_ALPHA:
  193. case GL_LUMINANCE:
  194. return width;
  195. case GL_LUMINANCE_ALPHA:
  196. return width * 2;
  197. case GL_RGB:
  198. return width * 3;
  199. case GL_RGBA:
  200. case GL_DEPTH24_STENCIL8_EXT:
  201. return width * 4;
  202. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  203. return ((width + 3) >> 2) * 8;
  204. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  205. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  206. return ((width + 3) >> 2) * 16;
  207. default:
  208. return 0;
  209. }
  210. }
  211. unsigned Texture::GetDXTFormat(CompressedFormat format)
  212. {
  213. switch (format)
  214. {
  215. case CF_DXT1:
  216. return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  217. case CF_DXT3:
  218. return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  219. case CF_DXT5:
  220. return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  221. }
  222. return 0;
  223. }
  224. unsigned Texture::GetExternalFormat(unsigned format)
  225. {
  226. // For DEPTH_COMPONENTxx textures DEPTH_COMPONENT needs to be returned
  227. if (format == GL_DEPTH_COMPONENT16 || format == GL_DEPTH_COMPONENT24 || format == GL_DEPTH_COMPONENT32)
  228. return GL_DEPTH_COMPONENT;
  229. else if (format == GL_DEPTH24_STENCIL8_EXT)
  230. return GL_DEPTH_STENCIL_EXT;
  231. else
  232. return format;
  233. }
  234. unsigned Texture::GetDataType(unsigned format)
  235. {
  236. if (format == GL_DEPTH24_STENCIL8_EXT)
  237. return GL_UNSIGNED_INT_24_8_EXT;
  238. else
  239. return GL_UNSIGNED_BYTE;
  240. }
  241. void Texture::LoadParameters()
  242. {
  243. ResourceCache* cache = GetSubsystem<ResourceCache>();
  244. String texPath, texName, texExt;
  245. SplitPath(GetName(), texPath, texName, texExt);
  246. String xmlName = texPath + texName + ".xml";
  247. if (cache->Exists(xmlName))
  248. {
  249. XMLFile* file = cache->GetResource<XMLFile>(xmlName);
  250. LoadParameters(file);
  251. }
  252. }
  253. void Texture::LoadParameters(XMLFile* file)
  254. {
  255. if (!file)
  256. return;
  257. XMLElement rootElem = file->GetRoot();
  258. XMLElement paramElem = rootElem.GetChild("");
  259. while (paramElem)
  260. {
  261. String name = paramElem.GetName();
  262. if (name == "address")
  263. {
  264. String coord = paramElem.GetStringLower("coord");
  265. if (coord.Length() >= 1)
  266. {
  267. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  268. String mode = paramElem.GetStringLower("mode");
  269. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode, addressModeNames, ADDRESS_WRAP));
  270. }
  271. }
  272. if (name == "border")
  273. SetBorderColor(paramElem.GetColor("color"));
  274. if (name == "filter")
  275. {
  276. String mode = paramElem.GetStringLower("mode");
  277. SetFilterMode((TextureFilterMode)GetStringListIndex(mode, filterModeNames, FILTER_DEFAULT));
  278. }
  279. if (name == "mipmap")
  280. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  281. if (name == "quality")
  282. {
  283. if (paramElem.HasAttribute("low"))
  284. mipsToSkip_[QUALITY_LOW] = Max(paramElem.GetInt("low"), 0);
  285. if (paramElem.HasAttribute("med"))
  286. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("med"), 0);
  287. if (paramElem.HasAttribute("medium"))
  288. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("medium"), 0);
  289. if (paramElem.HasAttribute("high"))
  290. mipsToSkip_[QUALITY_HIGH] = Max(paramElem.GetInt("high"), 0);
  291. // Make sure a higher quality level does not actually skip more mips
  292. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  293. {
  294. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  295. mipsToSkip_[i] = mipsToSkip_[i - 1];
  296. }
  297. }
  298. paramElem = paramElem.GetNext();
  299. }
  300. }
  301. void Texture::CheckTextureBudget(ShortStringHash type)
  302. {
  303. ResourceCache* cache = GetSubsystem<ResourceCache>();
  304. unsigned textureBudget = cache->GetMemoryBudget(type);
  305. unsigned textureUse = cache->GetMemoryUse(type);
  306. if (!textureBudget)
  307. return;
  308. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  309. // Therefore free unused materials first
  310. if (textureUse > textureBudget)
  311. cache->ReleaseResources(Material::GetTypeStatic());
  312. }