OGLTextureCube.cpp 16 KB

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