OGLTexture2D.cpp 9.6 KB

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