OGLTextureCube.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 "FileSystem.h"
  26. #include "Graphics.h"
  27. #include "GraphicsImpl.h"
  28. #include "Log.h"
  29. #include "Profiler.h"
  30. #include "Renderer.h"
  31. #include "ResourceCache.h"
  32. #include "TextureCube.h"
  33. #include "XMLFile.h"
  34. #include "DebugNew.h"
  35. #ifdef _MSC_VER
  36. #pragma warning(disable:4355)
  37. #endif
  38. OBJECTTYPESTATIC(TextureCube);
  39. TextureCube::TextureCube(Context* context) :
  40. Texture(context)
  41. {
  42. target_ = GL_TEXTURE_CUBE_MAP;
  43. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  44. faceMemoryUse_[i] = 0;
  45. }
  46. TextureCube::~TextureCube()
  47. {
  48. Release();
  49. }
  50. void TextureCube::RegisterObject(Context* context)
  51. {
  52. context->RegisterFactory<TextureCube>();
  53. }
  54. void TextureCube::OnDeviceLost()
  55. {
  56. GPUObject::OnDeviceLost();
  57. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  58. {
  59. if (renderSurfaces_[i])
  60. renderSurfaces_[i]->OnDeviceLost();
  61. }
  62. dataLost_ = true;
  63. }
  64. void TextureCube::OnDeviceReset()
  65. {
  66. // If has a file name, reload through the resource cache. Otherwise just recreate.
  67. if (!GetName().Trimmed().Empty())
  68. {
  69. if (GetSubsystem<ResourceCache>()->ReloadResource(this))
  70. dataLost_ = false;
  71. }
  72. else
  73. Create();
  74. }
  75. void TextureCube::Release()
  76. {
  77. if (object_)
  78. {
  79. if (!graphics_)
  80. return;
  81. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  82. {
  83. if (graphics_->GetTexture(i) == this)
  84. graphics_->SetTexture(i, 0);
  85. }
  86. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  87. {
  88. if (renderSurfaces_[i])
  89. renderSurfaces_[i]->Release();
  90. }
  91. glDeleteTextures(1, &object_);
  92. object_ = 0;
  93. }
  94. }
  95. bool TextureCube::SetSize(int size, unsigned format, TextureUsage usage)
  96. {
  97. if (size <= 0)
  98. {
  99. LOGERROR("Zero or negative cube texture size");
  100. return false;
  101. }
  102. if (usage == TEXTURE_DEPTHSTENCIL)
  103. {
  104. LOGERROR("Depth-stencil usage not supported for cube maps");
  105. return false;
  106. }
  107. // Delete the old rendersurfaces if any
  108. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  109. {
  110. renderSurfaces_[i].Reset();
  111. faceMemoryUse_[i] = 0;
  112. }
  113. if (usage == TEXTURE_RENDERTARGET)
  114. {
  115. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  116. renderSurfaces_[i] = new RenderSurface(this, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i);
  117. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  118. addressMode_[COORD_U] = ADDRESS_CLAMP;
  119. addressMode_[COORD_V] = ADDRESS_CLAMP;
  120. addressMode_[COORD_W] = ADDRESS_CLAMP;
  121. filterMode_ = FILTER_NEAREST;
  122. requestedLevels_ = 1;
  123. dynamic_ = true;
  124. }
  125. else if (usage == TEXTURE_DYNAMIC)
  126. dynamic_ = true;
  127. else
  128. dynamic_ = false;
  129. width_ = size;
  130. height_ = size;
  131. format_ = format;
  132. return Create();
  133. }
  134. bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data)
  135. {
  136. if (!object_ || !graphics_)
  137. {
  138. LOGERROR("No texture created, can not set data");
  139. return false;
  140. }
  141. if (!data)
  142. {
  143. LOGERROR("Null source for setting data");
  144. return false;
  145. }
  146. if (level >= levels_)
  147. {
  148. LOGERROR("Illegal mip level for setting data");
  149. return false;
  150. }
  151. if (IsCompressed())
  152. {
  153. x &= ~3;
  154. y &= ~3;
  155. }
  156. int levelWidth = GetLevelWidth(level);
  157. int levelHeight = GetLevelHeight(level);
  158. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  159. {
  160. LOGERROR("Illegal dimensions for setting data");
  161. return false;
  162. }
  163. bool wholeLevel = x == 0 && y == 0 && width == levelWidth && height == levelHeight;
  164. // Use Direct3D convention with the vertical coordinates ie. 0 is top
  165. y = levelHeight - (y + height);
  166. graphics_->SetTextureForUpdate(this);
  167. if (!IsCompressed())
  168. {
  169. if (wholeLevel)
  170. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, format_, width, height, 0, GetExternalFormat(format_),
  171. GetDataType(format_), data);
  172. else
  173. glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, x, y, width, height, GetExternalFormat(format_),
  174. GetDataType(format_), data);
  175. }
  176. else
  177. {
  178. if (wholeLevel)
  179. glCompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, format_, width, height, 0,
  180. GetDataSize(width, height), data);
  181. else
  182. glCompressedTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, x, y, width, height, format_,
  183. GetDataSize(width, height), data);
  184. }
  185. graphics_->SetTexture(0, 0);
  186. return true;
  187. }
  188. bool TextureCube::Load(Deserializer& source)
  189. {
  190. PROFILE(LoadTextureCube);
  191. ResourceCache* cache = GetSubsystem<ResourceCache>();
  192. // In headless mode, do not actually load the texture, just return success
  193. Graphics* graphics = GetSubsystem<Graphics>();
  194. if (!graphics)
  195. return true;
  196. // If over the texture budget, see if materials can be freed to allow textures to be freed
  197. CheckTextureBudget(GetTypeStatic());
  198. String texPath, texName, texExt;
  199. SplitPath(GetName(), texPath, texName, texExt);
  200. SharedPtr<XMLFile> xml(new XMLFile(context_));
  201. if (!xml->Load(source))
  202. return false;
  203. LoadParameters(xml);
  204. XMLElement textureElem = xml->GetRoot();
  205. XMLElement faceElem = textureElem.GetChild("face");
  206. unsigned faces = 0;
  207. while (faceElem && faces < MAX_CUBEMAP_FACES)
  208. {
  209. String name = faceElem.GetAttribute("name");
  210. String faceTexPath, faceTexName, faceTexExt;
  211. SplitPath(name, faceTexPath, faceTexName, faceTexExt);
  212. // If path is empty, add the XML file path
  213. if (faceTexPath.Empty())
  214. name = texPath + name;
  215. SharedPtr<Image> image(cache->GetResource<Image>(name));
  216. Load((CubeMapFace)faces, image);
  217. faces++;
  218. faceElem = faceElem.GetNext("face");
  219. }
  220. return true;
  221. }
  222. bool TextureCube::Load(CubeMapFace face, Deserializer& source)
  223. {
  224. PROFILE(LoadTextureCube);
  225. SharedPtr<Image> image(new Image(context_));
  226. if (!image->Load(source))
  227. return false;
  228. return Load(face, image);
  229. }
  230. bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image, bool useAlpha)
  231. {
  232. if (!image)
  233. {
  234. LOGERROR("Null image, can not load texture");
  235. return false;
  236. }
  237. unsigned memoryUse = 0;
  238. int quality = QUALITY_HIGH;
  239. Renderer* renderer = GetSubsystem<Renderer>();
  240. if (renderer)
  241. quality = renderer->GetTextureQuality();
  242. if (!image->IsCompressed())
  243. {
  244. unsigned char* levelData = image->GetData();
  245. int levelWidth = image->GetWidth();
  246. int levelHeight = image->GetHeight();
  247. unsigned components = image->GetComponents();
  248. unsigned format = 0;
  249. if (levelWidth != levelHeight)
  250. {
  251. LOGERROR("Cube texture width not equal to height");
  252. return false;
  253. }
  254. // Discard unnecessary mip levels
  255. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  256. {
  257. image = image->GetNextLevel();
  258. levelWidth = image->GetWidth();
  259. levelHeight = image->GetHeight();
  260. }
  261. switch (components)
  262. {
  263. case 1:
  264. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  265. break;
  266. case 2:
  267. format = Graphics::GetLuminanceAlphaFormat();
  268. break;
  269. case 3:
  270. format = Graphics::GetRGBFormat();
  271. break;
  272. case 4:
  273. format = Graphics::GetRGBAFormat();
  274. break;
  275. }
  276. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  277. if (!face)
  278. SetSize(levelWidth, format);
  279. else
  280. {
  281. if (!object_)
  282. {
  283. LOGERROR("Cube texture face 0 must be loaded first");
  284. return false;
  285. }
  286. if (levelWidth != width_ || format != format_)
  287. {
  288. LOGERROR("Cube texture face does not match size or format of face 0");
  289. return false;
  290. }
  291. }
  292. for (unsigned i = 0; i < levels_; ++i)
  293. {
  294. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  295. memoryUse += levelWidth * levelHeight * components;
  296. if (i < levels_ - 1)
  297. {
  298. image = image->GetNextLevel();
  299. levelData = image->GetData();
  300. levelWidth = image->GetWidth();
  301. levelHeight = image->GetHeight();
  302. }
  303. }
  304. }
  305. else
  306. {
  307. int width = image->GetWidth();
  308. int height = image->GetHeight();
  309. unsigned levels = image->GetNumCompressedLevels();
  310. unsigned format = GetDXTFormat(image->GetCompressedFormat());
  311. bool needDecompress = false;
  312. if (width != height)
  313. {
  314. LOGERROR("Cube texture width not equal to height");
  315. return false;
  316. }
  317. if (!graphics_->GetCompressedTextureSupport() || !format)
  318. {
  319. format = Graphics::GetRGBAFormat();
  320. needDecompress = true;
  321. }
  322. unsigned mipsToSkip = mipsToSkip_[quality];
  323. if (mipsToSkip >= levels)
  324. mipsToSkip = levels - 1;
  325. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  326. --mipsToSkip;
  327. width /= (1 << mipsToSkip);
  328. height /= (1 << mipsToSkip);
  329. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  330. if (!face)
  331. {
  332. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  333. SetSize(width, format);
  334. }
  335. else
  336. {
  337. if (!object_)
  338. {
  339. LOGERROR("Cube texture face 0 must be loaded first");
  340. return false;
  341. }
  342. if (width != width_ || format != format_)
  343. {
  344. LOGERROR("Cube texture face does not match size or format of face 0");
  345. return false;
  346. }
  347. }
  348. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  349. {
  350. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  351. if (!needDecompress)
  352. {
  353. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  354. memoryUse += level.rows_ * level.rowSize_;
  355. }
  356. else
  357. {
  358. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  359. level.Decompress(rgbaData);
  360. SetData(face, i, 0, 0, level.width_, level.height_, rgbaData);
  361. memoryUse += level.width_ * level.height_ * 4;
  362. delete[] rgbaData;
  363. }
  364. }
  365. }
  366. faceMemoryUse_[face] = memoryUse;
  367. unsigned totalMemoryUse = sizeof(TextureCube);
  368. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  369. totalMemoryUse += faceMemoryUse_[i];
  370. SetMemoryUse(totalMemoryUse);
  371. return true;
  372. }
  373. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  374. {
  375. #ifndef GL_ES_VERSION_2_0
  376. if (!object_ || !graphics_)
  377. {
  378. LOGERROR("No texture created, can not get data");
  379. return false;
  380. }
  381. if (!dest)
  382. {
  383. LOGERROR("Null destination for getting data");
  384. return false;
  385. }
  386. if (level >= levels_)
  387. {
  388. LOGERROR("Illegal mip level for getting data");
  389. return false;
  390. }
  391. graphics_->SetTextureForUpdate(const_cast<TextureCube*>(this));
  392. if (!IsCompressed())
  393. glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, GetExternalFormat(format_), GetDataType(format_), dest);
  394. else
  395. glGetCompressedTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, dest);
  396. graphics_->SetTexture(0, 0);
  397. return true;
  398. #else
  399. LOGERROR("Get texture data not supported");
  400. return false;
  401. #endif
  402. }
  403. bool TextureCube::Create()
  404. {
  405. Release();
  406. if (!graphics_)
  407. return false;
  408. if (!width_ || !height_)
  409. return false;
  410. glGenTextures(1, &object_);
  411. // Ensure that our texture is bound to OpenGL texture unit 0
  412. graphics_->SetTextureForUpdate(this);
  413. // If not compressed, create the initial level 0 texture with null data
  414. unsigned externalFormat = GetExternalFormat(format_);
  415. unsigned dataType = GetDataType(format_);
  416. bool success = true;
  417. if (!IsCompressed())
  418. {
  419. glGetError();
  420. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  421. {
  422. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format_, width_, height_, 0, externalFormat, dataType, 0);
  423. if (glGetError())
  424. success = false;
  425. }
  426. }
  427. if (!success)
  428. LOGERROR("Failed to create texture");
  429. // Set mipmapping
  430. levels_ = requestedLevels_;
  431. if (!levels_)
  432. {
  433. unsigned maxSize = Max((int)width_, (int)height_);
  434. while (maxSize)
  435. {
  436. maxSize >>= 1;
  437. ++levels_;
  438. }
  439. }
  440. #ifndef GL_ES_VERSION_2_0
  441. glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0);
  442. glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  443. #endif
  444. // Set initial parameters, then unbind the texture
  445. UpdateParameters();
  446. graphics_->SetTexture(0, 0);
  447. return success;
  448. }