OGLTexture2D.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "Context.h"
  25. #include "Graphics.h"
  26. #include "GraphicsImpl.h"
  27. #include "Log.h"
  28. #include "Profiler.h"
  29. #include "Renderer.h"
  30. #include "ResourceCache.h"
  31. #include "Texture2D.h"
  32. #include <GLee.h>
  33. #include "DebugNew.h"
  34. OBJECTTYPESTATIC(Texture2D);
  35. Texture2D::Texture2D(Context* context) :
  36. Texture(context),
  37. followWindowSize_(false)
  38. {
  39. textureType_ = GL_TEXTURE_2D;
  40. }
  41. Texture2D::~Texture2D()
  42. {
  43. Release();
  44. }
  45. void Texture2D::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<Texture2D>();
  48. }
  49. bool Texture2D::Load(Deserializer& source)
  50. {
  51. PROFILE(LoadTexture2D);
  52. if (!graphics_)
  53. return false;
  54. // If over the texture budget, see if materials can be freed to allow textures to be freed
  55. CheckTextureBudget(GetTypeStatic());
  56. SharedPtr<Image> image(new Image(context_));
  57. if (!image->Load(source))
  58. return false;
  59. // Before actually loading the texture, get optional parameters from an XML description file
  60. LoadParameters();
  61. return Load(image);
  62. }
  63. void Texture2D::OnDeviceReset()
  64. {
  65. if (followWindowSize_)
  66. Create();
  67. }
  68. void Texture2D::Release()
  69. {
  70. if (object_)
  71. {
  72. if (!graphics_)
  73. return;
  74. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  75. {
  76. if (graphics_->GetTexture(i) == this)
  77. graphics_->SetTexture(i, 0);
  78. }
  79. if (renderSurface_)
  80. renderSurface_->Release();
  81. glDeleteTextures(1, &object_);
  82. object_ = 0;
  83. }
  84. else
  85. {
  86. if (renderSurface_)
  87. renderSurface_->Release();
  88. }
  89. }
  90. bool Texture2D::SetSize(int width, int height, unsigned format, TextureUsage usage)
  91. {
  92. // Delete the old rendersurface if any
  93. renderSurface_.Reset();
  94. if (usage >= TEXTURE_RENDERTARGET)
  95. {
  96. if (!graphics_->GetRenderTextureSupport())
  97. return false;
  98. renderSurface_ = new RenderSurface(this);
  99. dynamic_ = true;
  100. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  101. addressMode_[COORD_U] = ADDRESS_CLAMP;
  102. addressMode_[COORD_V] = ADDRESS_CLAMP;
  103. filterMode_ = FILTER_NEAREST;
  104. requestedLevels_ = 1;
  105. }
  106. else if (usage == TEXTURE_DYNAMIC)
  107. dynamic_ = true;
  108. else
  109. dynamic_ = false;
  110. if ((width <= 0) || (height <= 0))
  111. followWindowSize_ = true;
  112. else
  113. {
  114. width_ = width;
  115. height_ = height;
  116. followWindowSize_ = false;
  117. }
  118. format_ = format;
  119. return Create();
  120. }
  121. bool Texture2D::Load(SharedPtr<Image> image)
  122. {
  123. if (!image)
  124. {
  125. LOGERROR("Null image, can not load texture");
  126. return false;
  127. }
  128. unsigned memoryUse = 0;
  129. int quality = QUALITY_HIGH;
  130. Renderer* renderer = GetSubsystem<Renderer>();
  131. if (renderer)
  132. quality = renderer->GetTextureQuality();
  133. if (!image->IsCompressed())
  134. {
  135. unsigned char* levelData = image->GetData();
  136. int levelWidth = image->GetWidth();
  137. int levelHeight = image->GetHeight();
  138. unsigned components = image->GetComponents();
  139. unsigned format = 0;
  140. // Discard unnecessary mip levels
  141. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  142. {
  143. image = image->GetNextLevel();
  144. levelData = image->GetData();
  145. levelWidth = image->GetWidth();
  146. levelHeight = image->GetHeight();
  147. }
  148. switch (components)
  149. {
  150. case 1:
  151. format = Graphics::GetLuminanceFormat();
  152. break;
  153. case 2:
  154. format = Graphics::GetLuminanceAlphaFormat();
  155. break;
  156. case 3:
  157. format = Graphics::GetRGBFormat();
  158. break;
  159. case 4:
  160. format = Graphics::GetRGBAFormat();
  161. break;
  162. }
  163. SetSize(levelWidth, levelHeight, format);
  164. if (!object_)
  165. return false;
  166. graphics_->SetTextureForUpdate(this);
  167. for (unsigned i = 0; i < levels_; ++i)
  168. {
  169. glTexImage2D(textureType_, i, format_, levelWidth, levelHeight, 0, GetExternalFormat(format_), GL_UNSIGNED_BYTE,
  170. levelData);
  171. if (i < levels_ - 1)
  172. {
  173. image = image->GetNextLevel();
  174. levelData = image->GetData();
  175. levelWidth = image->GetWidth();
  176. levelHeight = image->GetHeight();
  177. }
  178. memoryUse += levelWidth * levelHeight * components;
  179. }
  180. graphics_->SetTexture(0, 0);
  181. }
  182. else
  183. {
  184. int width = image->GetWidth();
  185. int height = image->GetHeight();
  186. unsigned levels = image->GetNumCompressedLevels();
  187. unsigned format = GetDXTFormat(image->GetCompressedFormat());
  188. unsigned mipsToSkip = mipsToSkip_[quality];
  189. if (mipsToSkip >= levels)
  190. mipsToSkip = levels - 1;
  191. while ((mipsToSkip) && ((width / (1 << mipsToSkip) < 4) || (height / (1 << mipsToSkip) < 4)))
  192. --mipsToSkip;
  193. width /= (1 << mipsToSkip);
  194. height /= (1 << mipsToSkip);
  195. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  196. SetSize(width, height, format);
  197. graphics_->SetTextureForUpdate(this);
  198. for (unsigned i = 0; (i < levels_) && (i < levels - mipsToSkip); ++i)
  199. {
  200. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  201. glCompressedTexImage2D(textureType_, i, format_, level.width_, level.height_, 0, level.dataSize_, level.data_);
  202. memoryUse += level.rows_ * level.rowSize_;
  203. }
  204. graphics_->SetTexture(0, 0);
  205. }
  206. SetMemoryUse(memoryUse);
  207. return true;
  208. }
  209. bool Texture2D::Create()
  210. {
  211. Release();
  212. if (!graphics_)
  213. return false;
  214. if (followWindowSize_)
  215. {
  216. width_ = graphics_->GetWidth();
  217. height_ = graphics_->GetHeight();
  218. }
  219. if ((!width_) || (!height_))
  220. return false;
  221. glGenTextures(1, &object_);
  222. // Ensure that our texture is bound to OpenGL texture unit 0
  223. graphics_->SetTextureForUpdate(this);
  224. // If not compressed, create the initial level 0 texture with null data
  225. unsigned externalFormat = GetExternalFormat(format_);
  226. unsigned dataType = GetDataType(format_);
  227. if ((format_ != GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) && (format_ != GL_COMPRESSED_RGBA_S3TC_DXT3_EXT) &&
  228. (format_ != GL_COMPRESSED_RGBA_S3TC_DXT5_EXT))
  229. glTexImage2D(textureType_, 0, format_, width_, height_, 0, externalFormat, dataType, 0);
  230. // If depth format, get the depth size
  231. if (externalFormat == GL_DEPTH_COMPONENT)
  232. glGetTexLevelParameteriv(textureType_, 0, GL_TEXTURE_DEPTH_SIZE, &depthBits_);
  233. // Set mipmapping
  234. levels_ = requestedLevels_;
  235. if (!levels_)
  236. {
  237. unsigned maxSize = max((int)width_, (int)height_);
  238. while (maxSize)
  239. {
  240. maxSize >>= 1;
  241. ++levels_;
  242. }
  243. }
  244. glTexParameteri(textureType_, GL_TEXTURE_BASE_LEVEL, 0);
  245. glTexParameteri(textureType_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  246. // Set initial parameters, then unbind the texture
  247. UpdateParameters();
  248. graphics_->SetTexture(0, 0);
  249. return true;
  250. }