2
0

OGLTextureCube.cpp 16 KB

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