OGLTexture2D.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 "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. {
  37. target_ = GL_TEXTURE_2D;
  38. }
  39. Texture2D::~Texture2D()
  40. {
  41. Release();
  42. }
  43. void Texture2D::RegisterObject(Context* context)
  44. {
  45. context->RegisterFactory<Texture2D>();
  46. }
  47. bool Texture2D::Load(Deserializer& source)
  48. {
  49. PROFILE(LoadTexture2D);
  50. // In headless mode, do not actually load the texture, just return success
  51. Graphics* graphics = GetSubsystem<Graphics>();
  52. if (!graphics)
  53. return true;
  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::OnDeviceLost()
  64. {
  65. savedLevels_.Clear();
  66. // Check if save data is supported, in that case save data of each mip level
  67. if (GetDataSize(width_, height_))
  68. {
  69. for (unsigned i = 0; i < levels_; ++i)
  70. {
  71. int levelWidth = GetLevelWidth(i);
  72. int levelHeight = GetLevelHeight(i);
  73. SharedArrayPtr<unsigned char> savedLevel(new unsigned char[GetDataSize(levelWidth, levelHeight)]);
  74. GetData(i, savedLevel.Get());
  75. savedLevels_.Push(savedLevel);
  76. }
  77. }
  78. Release();
  79. }
  80. void Texture2D::OnDeviceReset()
  81. {
  82. if (!object_)
  83. {
  84. Create();
  85. // Restore texture from save data if it exists
  86. if (savedLevels_.Size())
  87. {
  88. for (unsigned i = 0; i < savedLevels_.Size(); ++i)
  89. {
  90. int levelWidth = GetLevelWidth(i);
  91. int levelHeight = GetLevelHeight(i);
  92. SetData(i, 0, 0, levelWidth, levelHeight, savedLevels_[i].Get());
  93. }
  94. savedLevels_.Clear();
  95. }
  96. }
  97. }
  98. void Texture2D::Release()
  99. {
  100. if (object_)
  101. {
  102. if (!graphics_)
  103. return;
  104. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  105. {
  106. if (graphics_->GetTexture(i) == this)
  107. graphics_->SetTexture(i, 0);
  108. }
  109. if (renderSurface_)
  110. renderSurface_->Release();
  111. glDeleteTextures(1, &object_);
  112. object_ = 0;
  113. }
  114. else
  115. {
  116. if (renderSurface_)
  117. renderSurface_->Release();
  118. }
  119. }
  120. bool Texture2D::SetSize(int width, int height, unsigned format, TextureUsage usage)
  121. {
  122. // Delete the old rendersurface if any
  123. renderSurface_.Reset();
  124. if (usage >= TEXTURE_RENDERTARGET)
  125. {
  126. renderSurface_ = new RenderSurface(this, GL_TEXTURE_2D);
  127. dynamic_ = true;
  128. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  129. addressMode_[COORD_U] = ADDRESS_CLAMP;
  130. addressMode_[COORD_V] = ADDRESS_CLAMP;
  131. filterMode_ = FILTER_NEAREST;
  132. requestedLevels_ = 1;
  133. }
  134. else if (usage == TEXTURE_DYNAMIC)
  135. dynamic_ = true;
  136. else
  137. dynamic_ = false;
  138. width_ = width;
  139. height_ = height;
  140. format_ = format;
  141. return Create();
  142. }
  143. bool Texture2D::SetData(unsigned level, int x, int y, int width, int height, const void* data)
  144. {
  145. if (!object_ || !graphics_)
  146. {
  147. LOGERROR("No texture created, can not set data");
  148. return false;
  149. }
  150. if (!data)
  151. {
  152. LOGERROR("Null source for setting data");
  153. return false;
  154. }
  155. if (level >= levels_)
  156. {
  157. LOGERROR("Illegal mip level for setting data");
  158. return false;
  159. }
  160. bool compressed = format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ||
  161. format_ == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  162. if (compressed)
  163. {
  164. x &= ~3;
  165. y &= ~3;
  166. }
  167. int levelWidth = GetLevelWidth(level);
  168. int levelHeight = GetLevelHeight(level);
  169. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  170. {
  171. LOGERROR("Illegal dimensions for setting data");
  172. return false;
  173. }
  174. bool wholeLevel = x == 0 && y == 0 && width == levelWidth && height == levelHeight;
  175. // Use Direct3D convention with the vertical coordinates ie. 0 is top
  176. y = levelHeight - (y + height);
  177. graphics_->SetTextureForUpdate(this);
  178. if (!compressed)
  179. {
  180. if (wholeLevel)
  181. glTexImage2D(target_, level, format_, width, height, 0, GetExternalFormat(format_), GetDataType(format_), data);
  182. else
  183. glTexSubImage2D(target_, level, x, y, width, height, GetExternalFormat(format_), GetDataType(format_), data);
  184. }
  185. else
  186. {
  187. if (wholeLevel)
  188. glCompressedTexImage2D(target_, level, format_, width, height, 0, GetDataSize(width, height), data);
  189. else
  190. glCompressedTexSubImage2D(target_, level, x, y, width, height, format_, GetDataSize(width, height), data);
  191. }
  192. graphics_->SetTexture(0, 0);
  193. return true;
  194. }
  195. bool Texture2D::Load(SharedPtr<Image> image, bool useAlpha)
  196. {
  197. if (!image)
  198. {
  199. LOGERROR("Null image, can not load texture");
  200. return false;
  201. }
  202. unsigned memoryUse = sizeof(Texture2D);
  203. int quality = QUALITY_HIGH;
  204. Renderer* renderer = GetSubsystem<Renderer>();
  205. if (renderer)
  206. quality = renderer->GetTextureQuality();
  207. if (!image->IsCompressed())
  208. {
  209. unsigned char* levelData = image->GetData();
  210. int levelWidth = image->GetWidth();
  211. int levelHeight = image->GetHeight();
  212. unsigned components = image->GetComponents();
  213. unsigned format = 0;
  214. // Discard unnecessary mip levels
  215. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  216. {
  217. image = image->GetNextLevel();
  218. levelData = image->GetData();
  219. levelWidth = image->GetWidth();
  220. levelHeight = image->GetHeight();
  221. }
  222. switch (components)
  223. {
  224. case 1:
  225. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  226. break;
  227. case 2:
  228. format = Graphics::GetLuminanceAlphaFormat();
  229. break;
  230. case 3:
  231. format = Graphics::GetRGBFormat();
  232. break;
  233. case 4:
  234. format = Graphics::GetRGBAFormat();
  235. break;
  236. }
  237. SetSize(levelWidth, levelHeight, format);
  238. if (!object_)
  239. return false;
  240. for (unsigned i = 0; i < levels_; ++i)
  241. {
  242. SetData(i, 0, 0, levelWidth, levelHeight, levelData);
  243. memoryUse += levelWidth * levelHeight * components;
  244. if (i < levels_ - 1)
  245. {
  246. image = image->GetNextLevel();
  247. levelData = image->GetData();
  248. levelWidth = image->GetWidth();
  249. levelHeight = image->GetHeight();
  250. }
  251. }
  252. }
  253. else
  254. {
  255. int width = image->GetWidth();
  256. int height = image->GetHeight();
  257. unsigned levels = image->GetNumCompressedLevels();
  258. unsigned format = GetDXTFormat(image->GetCompressedFormat());
  259. bool needDecompress = false;
  260. if (!graphics_->GetCompressedTextureSupport())
  261. {
  262. format = Graphics::GetRGBAFormat();
  263. needDecompress = true;
  264. }
  265. unsigned mipsToSkip = mipsToSkip_[quality];
  266. if (mipsToSkip >= levels)
  267. mipsToSkip = levels - 1;
  268. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  269. --mipsToSkip;
  270. width /= (1 << mipsToSkip);
  271. height /= (1 << mipsToSkip);
  272. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  273. SetSize(width, height, format);
  274. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  275. {
  276. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  277. if (!needDecompress)
  278. {
  279. SetData(i, 0, 0, level.width_, level.height_, level.data_);
  280. memoryUse += level.rows_ * level.rowSize_;
  281. }
  282. else
  283. {
  284. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  285. level.Decompress(rgbaData);
  286. SetData(i, 0, 0, level.width_, level.height_, rgbaData);
  287. memoryUse += level.width_ * level.height_ * 4;
  288. delete[] rgbaData;
  289. }
  290. }
  291. }
  292. SetMemoryUse(memoryUse);
  293. return true;
  294. }
  295. bool Texture2D::GetData(unsigned level, void* dest) const
  296. {
  297. if (!object_ || !graphics_)
  298. {
  299. LOGERROR("No texture created, can not get data");
  300. return false;
  301. }
  302. if (!dest)
  303. {
  304. LOGERROR("Null destination for getting data");
  305. return false;
  306. }
  307. if (level >= levels_)
  308. {
  309. LOGERROR("Illegal mip level for getting data");
  310. return false;
  311. }
  312. bool compressed = format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ||
  313. format_ == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  314. graphics_->SetTextureForUpdate(const_cast<Texture2D*>(this));
  315. if (!compressed)
  316. glGetTexImage(target_, level, GetExternalFormat(format_), GetDataType(format_), dest);
  317. else
  318. glGetCompressedTexImage(target_, level, dest);
  319. graphics_->SetTexture(0, 0);
  320. return true;
  321. }
  322. bool Texture2D::Create()
  323. {
  324. Release();
  325. if (!graphics_)
  326. return false;
  327. if (!width_ || !height_)
  328. return false;
  329. // For packed depth-stencil, create a renderbuffer instead of a texture if depth texture is not properly supported
  330. if (!graphics_->GetHardwareDepthSupport() && format_ == Graphics::GetDepthStencilFormat())
  331. {
  332. if (renderSurface_)
  333. {
  334. renderSurface_->CreateRenderBuffer(width_, height_, format_);
  335. depthBits_ = 24;
  336. return true;
  337. }
  338. else
  339. return false;
  340. }
  341. glGenTextures(1, &object_);
  342. // Ensure that our texture is bound to OpenGL texture unit 0
  343. graphics_->SetTextureForUpdate(this);
  344. // If not compressed, create the initial level 0 texture with null data
  345. unsigned externalFormat = GetExternalFormat(format_);
  346. unsigned dataType = GetDataType(format_);
  347. if (format_ != GL_COMPRESSED_RGBA_S3TC_DXT1_EXT && format_ != GL_COMPRESSED_RGBA_S3TC_DXT3_EXT &&
  348. format_ != GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
  349. glTexImage2D(target_, 0, format_, width_, height_, 0, externalFormat, dataType, 0);
  350. // If depth format, get the depth size
  351. if (externalFormat == GL_DEPTH_COMPONENT)
  352. glGetTexLevelParameteriv(target_, 0, GL_TEXTURE_DEPTH_SIZE, &depthBits_);
  353. // Set mipmapping
  354. levels_ = requestedLevels_;
  355. if (!levels_)
  356. {
  357. unsigned maxSize = Max((int)width_, (int)height_);
  358. while (maxSize)
  359. {
  360. maxSize >>= 1;
  361. ++levels_;
  362. }
  363. }
  364. glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0);
  365. glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  366. // Set initial parameters, then unbind the texture
  367. UpdateParameters();
  368. graphics_->SetTexture(0, 0);
  369. return true;
  370. }