OGLTexture2D.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 device is lost, retry later
  55. if (graphics_->IsDeviceLost())
  56. {
  57. LOGWARNING("Texture load while device is lost");
  58. dataPending_ = true;
  59. return true;
  60. }
  61. // If over the texture budget, see if materials can be freed to allow textures to be freed
  62. CheckTextureBudget(GetTypeStatic());
  63. SharedPtr<Image> image(new Image(context_));
  64. if (!image->Load(source))
  65. return false;
  66. // Before actually loading the texture, get optional parameters from an XML description file
  67. LoadParameters();
  68. return Load(image);
  69. }
  70. void Texture2D::OnDeviceLost()
  71. {
  72. GPUObject::OnDeviceLost();
  73. if (renderSurface_)
  74. renderSurface_->OnDeviceLost();
  75. }
  76. void Texture2D::OnDeviceReset()
  77. {
  78. // If has a file name, reload through the resource cache. Otherwise just recreate.
  79. if (!object_)
  80. {
  81. if (!GetName().Trimmed().Empty())
  82. dataLost_ = !GetSubsystem<ResourceCache>()->ReloadResource(this);
  83. else
  84. {
  85. Create();
  86. dataLost_ = true;
  87. }
  88. }
  89. else if (dataPending_)
  90. {
  91. if (!GetName().Trimmed().Empty())
  92. dataLost_ = !GetSubsystem<ResourceCache>()->ReloadResource(this);
  93. else
  94. dataLost_ = true;
  95. }
  96. dataPending_ = false;
  97. }
  98. void Texture2D::Release()
  99. {
  100. if (object_)
  101. {
  102. if (!graphics_ || graphics_->IsDeviceLost())
  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. if (graphics_->IsDeviceLost())
  161. {
  162. LOGWARNING("Texture data assignment while device is lost");
  163. dataPending_ = true;
  164. return false;
  165. }
  166. if (IsCompressed())
  167. {
  168. x &= ~3;
  169. y &= ~3;
  170. }
  171. int levelWidth = GetLevelWidth(level);
  172. int levelHeight = GetLevelHeight(level);
  173. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  174. {
  175. LOGERROR("Illegal dimensions for setting data");
  176. return false;
  177. }
  178. bool wholeLevel = x == 0 && y == 0 && width == levelWidth && height == levelHeight;
  179. // Use Direct3D convention with the vertical coordinates ie. 0 is top
  180. y = levelHeight - (y + height);
  181. graphics_->SetTextureForUpdate(this);
  182. if (!IsCompressed())
  183. {
  184. if (wholeLevel)
  185. glTexImage2D(target_, level, format_, width, height, 0, GetExternalFormat(format_), GetDataType(format_), data);
  186. else
  187. glTexSubImage2D(target_, level, x, y, width, height, GetExternalFormat(format_), GetDataType(format_), data);
  188. }
  189. else
  190. {
  191. if (wholeLevel)
  192. glCompressedTexImage2D(target_, level, format_, width, height, 0, GetDataSize(width, height), data);
  193. else
  194. glCompressedTexSubImage2D(target_, level, x, y, width, height, format_, GetDataSize(width, height), data);
  195. }
  196. graphics_->SetTexture(0, 0);
  197. return true;
  198. }
  199. bool Texture2D::Load(SharedPtr<Image> image, bool useAlpha)
  200. {
  201. if (!image)
  202. {
  203. LOGERROR("Null image, can not load texture");
  204. return false;
  205. }
  206. unsigned memoryUse = sizeof(Texture2D);
  207. int quality = QUALITY_HIGH;
  208. Renderer* renderer = GetSubsystem<Renderer>();
  209. if (renderer)
  210. quality = renderer->GetTextureQuality();
  211. if (!image->IsCompressed())
  212. {
  213. unsigned char* levelData = image->GetData();
  214. int levelWidth = image->GetWidth();
  215. int levelHeight = image->GetHeight();
  216. unsigned components = image->GetComponents();
  217. unsigned format = 0;
  218. // Discard unnecessary mip levels
  219. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  220. {
  221. image = image->GetNextLevel();
  222. levelData = image->GetData();
  223. levelWidth = image->GetWidth();
  224. levelHeight = image->GetHeight();
  225. }
  226. switch (components)
  227. {
  228. case 1:
  229. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  230. break;
  231. case 2:
  232. format = Graphics::GetLuminanceAlphaFormat();
  233. break;
  234. case 3:
  235. format = Graphics::GetRGBFormat();
  236. break;
  237. case 4:
  238. format = Graphics::GetRGBAFormat();
  239. break;
  240. }
  241. SetSize(levelWidth, levelHeight, format);
  242. if (!object_)
  243. return false;
  244. for (unsigned i = 0; i < levels_; ++i)
  245. {
  246. SetData(i, 0, 0, levelWidth, levelHeight, levelData);
  247. memoryUse += levelWidth * levelHeight * components;
  248. if (i < levels_ - 1)
  249. {
  250. image = image->GetNextLevel();
  251. levelData = image->GetData();
  252. levelWidth = image->GetWidth();
  253. levelHeight = image->GetHeight();
  254. }
  255. }
  256. }
  257. else
  258. {
  259. int width = image->GetWidth();
  260. int height = image->GetHeight();
  261. unsigned levels = image->GetNumCompressedLevels();
  262. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  263. bool needDecompress = false;
  264. if (!format)
  265. {
  266. format = Graphics::GetRGBAFormat();
  267. needDecompress = true;
  268. }
  269. unsigned mipsToSkip = mipsToSkip_[quality];
  270. if (mipsToSkip >= levels)
  271. mipsToSkip = levels - 1;
  272. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  273. --mipsToSkip;
  274. width /= (1 << mipsToSkip);
  275. height /= (1 << mipsToSkip);
  276. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  277. SetSize(width, height, format);
  278. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  279. {
  280. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  281. if (!needDecompress)
  282. {
  283. SetData(i, 0, 0, level.width_, level.height_, level.data_);
  284. memoryUse += level.rows_ * level.rowSize_;
  285. }
  286. else
  287. {
  288. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  289. level.Decompress(rgbaData);
  290. SetData(i, 0, 0, level.width_, level.height_, rgbaData);
  291. memoryUse += level.width_ * level.height_ * 4;
  292. delete[] rgbaData;
  293. }
  294. }
  295. }
  296. SetMemoryUse(memoryUse);
  297. return true;
  298. }
  299. bool Texture2D::GetData(unsigned level, void* dest) const
  300. {
  301. #ifndef GL_ES_VERSION_2_0
  302. if (!object_ || !graphics_)
  303. {
  304. LOGERROR("No texture created, can not get data");
  305. return false;
  306. }
  307. if (!dest)
  308. {
  309. LOGERROR("Null destination for getting data");
  310. return false;
  311. }
  312. if (level >= levels_)
  313. {
  314. LOGERROR("Illegal mip level for getting data");
  315. return false;
  316. }
  317. if (graphics_->IsDeviceLost())
  318. {
  319. LOGWARNING("Getting texture data while device is lost");
  320. return false;
  321. }
  322. graphics_->SetTextureForUpdate(const_cast<Texture2D*>(this));
  323. if (!IsCompressed())
  324. glGetTexImage(target_, level, GetExternalFormat(format_), GetDataType(format_), dest);
  325. else
  326. glGetCompressedTexImage(target_, level, dest);
  327. graphics_->SetTexture(0, 0);
  328. return true;
  329. #else
  330. LOGERROR("Getting texture data not supported");
  331. return false;
  332. #endif
  333. }
  334. bool Texture2D::Create()
  335. {
  336. Release();
  337. if (!graphics_ || !width_ || !height_)
  338. return false;
  339. if (graphics_->IsDeviceLost())
  340. {
  341. LOGWARNING("Texture creation while device is lost");
  342. return false;
  343. }
  344. unsigned externalFormat = GetExternalFormat(format_);
  345. unsigned dataType = GetDataType(format_);
  346. // Create a renderbuffer instead of a texture if depth texture is not properly supported
  347. #ifndef GL_ES_VERSION_2_0
  348. if (!graphics_->GetHardwareDepthSupport() && format_ == Graphics::GetDepthStencilFormat())
  349. #else
  350. if (!graphics_->GetHardwareDepthSupport() && externalFormat == GL_DEPTH_COMPONENT)
  351. #endif
  352. {
  353. if (renderSurface_)
  354. {
  355. renderSurface_->CreateRenderBuffer(width_, height_, format_);
  356. return true;
  357. }
  358. else
  359. return false;
  360. }
  361. glGenTextures(1, &object_);
  362. // Ensure that our texture is bound to OpenGL texture unit 0
  363. graphics_->SetTextureForUpdate(this);
  364. // If not compressed, create the initial level 0 texture with null data
  365. bool success = true;
  366. if (!IsCompressed())
  367. {
  368. glGetError();
  369. glTexImage2D(target_, 0, format_, width_, height_, 0, externalFormat, dataType, 0);
  370. if (glGetError())
  371. {
  372. LOGERROR("Failed to create texture");
  373. success = false;
  374. }
  375. }
  376. // Set mipmapping
  377. levels_ = requestedLevels_;
  378. if (!levels_)
  379. {
  380. unsigned maxSize = Max((int)width_, (int)height_);
  381. while (maxSize)
  382. {
  383. maxSize >>= 1;
  384. ++levels_;
  385. }
  386. }
  387. #ifndef GL_ES_VERSION_2_0
  388. glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0);
  389. glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  390. #endif
  391. // Set initial parameters, then unbind the texture
  392. UpdateParameters();
  393. graphics_->SetTexture(0, 0);
  394. return success;
  395. }