OGLTextureCube.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. graphics_->SetTextureForUpdate(this);
  186. if (!compressed)
  187. {
  188. if (wholeLevel)
  189. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, format_, width, height, 0, GetExternalFormat(format_),
  190. GetDataType(format_), data);
  191. else
  192. glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, x, y, width, height, GetExternalFormat(format_),
  193. GetDataType(format_), data);
  194. }
  195. else
  196. {
  197. if (wholeLevel)
  198. glCompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, format_, width, height, 0,
  199. GetDataSize(width, height), data);
  200. else
  201. glCompressedTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, x, y, width, height, format_,
  202. GetDataSize(width, height), data);
  203. }
  204. graphics_->SetTexture(0, 0);
  205. return true;
  206. }
  207. bool TextureCube::Load(Deserializer& source)
  208. {
  209. PROFILE(LoadTextureCube);
  210. ResourceCache* cache = GetSubsystem<ResourceCache>();
  211. // In headless mode, do not actually load the texture, just return success
  212. Graphics* graphics = GetSubsystem<Graphics>();
  213. if (!graphics)
  214. return true;
  215. // If over the texture budget, see if materials can be freed to allow textures to be freed
  216. CheckTextureBudget(GetTypeStatic());
  217. String texPath, texName, texExt;
  218. SplitPath(GetName(), texPath, texName, texExt);
  219. SharedPtr<XMLFile> xml(new XMLFile(context_));
  220. if (!xml->Load(source))
  221. return false;
  222. LoadParameters(xml);
  223. XMLElement textureElem = xml->GetRoot();
  224. XMLElement faceElem = textureElem.GetChild("face");
  225. unsigned faces = 0;
  226. while (faceElem && faces < MAX_CUBEMAP_FACES)
  227. {
  228. String name = faceElem.GetString("name");
  229. String faceTexPath, faceTexName, faceTexExt;
  230. SplitPath(name, faceTexPath, faceTexName, faceTexExt);
  231. // If path is empty, add the XML file path
  232. if (faceTexPath.Empty())
  233. name = texPath + name;
  234. SharedPtr<Image> image(cache->GetResource<Image>(name));
  235. Load((CubeMapFace)faces, image);
  236. faces++;
  237. faceElem = faceElem.GetNext("face");
  238. }
  239. return true;
  240. }
  241. bool TextureCube::Load(CubeMapFace face, Deserializer& source)
  242. {
  243. PROFILE(LoadTextureCube);
  244. SharedPtr<Image> image(new Image(context_));
  245. if (!image->Load(source))
  246. return false;
  247. return Load(face, image);
  248. }
  249. bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image, bool useAlpha)
  250. {
  251. if (!image)
  252. {
  253. LOGERROR("Null image, can not load texture");
  254. return false;
  255. }
  256. unsigned memoryUse = 0;
  257. int quality = QUALITY_HIGH;
  258. Renderer* renderer = GetSubsystem<Renderer>();
  259. if (renderer)
  260. quality = renderer->GetTextureQuality();
  261. if (!image->IsCompressed())
  262. {
  263. unsigned char* levelData = image->GetData();
  264. int levelWidth = image->GetWidth();
  265. int levelHeight = image->GetHeight();
  266. unsigned components = image->GetComponents();
  267. unsigned format = 0;
  268. if (levelWidth != levelHeight)
  269. {
  270. LOGERROR("Cube texture width not equal to height");
  271. return false;
  272. }
  273. // Discard unnecessary mip levels
  274. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  275. {
  276. image = image->GetNextLevel();
  277. levelWidth = image->GetWidth();
  278. levelHeight = image->GetHeight();
  279. }
  280. switch (components)
  281. {
  282. case 1:
  283. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  284. break;
  285. case 2:
  286. format = Graphics::GetLuminanceAlphaFormat();
  287. break;
  288. case 3:
  289. format = Graphics::GetRGBFormat();
  290. break;
  291. case 4:
  292. format = Graphics::GetRGBAFormat();
  293. break;
  294. }
  295. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  296. if (!face)
  297. SetSize(levelWidth, format);
  298. else
  299. {
  300. if (!object_)
  301. {
  302. LOGERROR("Cube texture face 0 must be loaded first");
  303. return false;
  304. }
  305. if (levelWidth != width_ || format != format_)
  306. {
  307. LOGERROR("Cube texture face does not match size or format of face 0");
  308. return false;
  309. }
  310. }
  311. for (unsigned i = 0; i < levels_; ++i)
  312. {
  313. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  314. memoryUse += levelWidth * levelHeight * components;
  315. if (i < levels_ - 1)
  316. {
  317. image = image->GetNextLevel();
  318. levelData = image->GetData();
  319. levelWidth = image->GetWidth();
  320. levelHeight = image->GetHeight();
  321. }
  322. }
  323. }
  324. else
  325. {
  326. int width = image->GetWidth();
  327. int height = image->GetHeight();
  328. unsigned levels = image->GetNumCompressedLevels();
  329. unsigned format = GetDXTFormat(image->GetCompressedFormat());
  330. if (width != height)
  331. {
  332. LOGERROR("Cube texture width not equal to height");
  333. return false;
  334. }
  335. unsigned mipsToSkip = mipsToSkip_[quality];
  336. if (mipsToSkip >= levels)
  337. mipsToSkip = levels - 1;
  338. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  339. --mipsToSkip;
  340. width /= (1 << mipsToSkip);
  341. height /= (1 << mipsToSkip);
  342. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  343. if (!face)
  344. {
  345. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  346. SetSize(width, format);
  347. }
  348. else
  349. {
  350. if (!object_)
  351. {
  352. LOGERROR("Cube texture face 0 must be loaded first");
  353. return false;
  354. }
  355. if (width != width_ || format != format_)
  356. {
  357. LOGERROR("Cube texture face does not match size or format of face 0");
  358. return false;
  359. }
  360. }
  361. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  362. {
  363. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  364. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  365. memoryUse += level.rows_ * level.rowSize_;
  366. }
  367. }
  368. faceMemoryUse_[face] = memoryUse;
  369. unsigned totalMemoryUse = sizeof(TextureCube);
  370. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  371. totalMemoryUse += faceMemoryUse_[i];
  372. SetMemoryUse(totalMemoryUse);
  373. return true;
  374. }
  375. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  376. {
  377. if (!object_ || !graphics_)
  378. {
  379. LOGERROR("No texture created, can not get data");
  380. return false;
  381. }
  382. if (!dest)
  383. {
  384. LOGERROR("Null destination for getting data");
  385. return false;
  386. }
  387. if (level >= levels_)
  388. {
  389. LOGERROR("Illegal mip level for getting data");
  390. return false;
  391. }
  392. bool compressed = format_ == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format_ == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ||
  393. format_ == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  394. graphics_->SetTextureForUpdate(const_cast<TextureCube*>(this));
  395. if (!compressed)
  396. glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, GetExternalFormat(format_), GetDataType(format_), dest);
  397. else
  398. glGetCompressedTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, dest);
  399. graphics_->SetTexture(0, 0);
  400. return true;
  401. }
  402. bool TextureCube::Create()
  403. {
  404. Release();
  405. if (!graphics_)
  406. return false;
  407. if (!width_ || !height_)
  408. return false;
  409. glGenTextures(1, &object_);
  410. // Ensure that our texture is bound to OpenGL texture unit 0
  411. graphics_->SetTextureForUpdate(this);
  412. // If not compressed, create the initial level 0 texture with null data
  413. unsigned externalFormat = GetExternalFormat(format_);
  414. unsigned dataType = GetDataType(format_);
  415. if (format_ != GL_COMPRESSED_RGBA_S3TC_DXT1_EXT && format_ != GL_COMPRESSED_RGBA_S3TC_DXT3_EXT &&
  416. format_ != GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
  417. {
  418. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  419. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format_, width_, height_, 0, externalFormat, dataType, 0);
  420. }
  421. // Set mipmapping
  422. levels_ = requestedLevels_;
  423. if (!levels_)
  424. {
  425. unsigned maxSize = Max((int)width_, (int)height_);
  426. while (maxSize)
  427. {
  428. maxSize >>= 1;
  429. ++levels_;
  430. }
  431. }
  432. glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0);
  433. glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  434. // Set initial parameters, then unbind the texture
  435. UpdateParameters();
  436. graphics_->SetTexture(0, 0);
  437. return true;
  438. }