OGLTexture2D.cpp 13 KB

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