OGLTextureCube.cpp 15 KB

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