OGLTextureCube.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 <GLee.h>
  35. #include "DebugNew.h"
  36. #ifdef _MSC_VER
  37. #pragma warning(disable:4355)
  38. #endif
  39. OBJECTTYPESTATIC(TextureCube);
  40. TextureCube::TextureCube(Context* context) :
  41. Texture(context)
  42. {
  43. textureType_ = GL_TEXTURE_CUBE_MAP;
  44. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  45. faceMemoryUse_[i] = 0;
  46. }
  47. TextureCube::~TextureCube()
  48. {
  49. Release();
  50. }
  51. void TextureCube::RegisterObject(Context* context)
  52. {
  53. context->RegisterFactory<TextureCube>();
  54. }
  55. void TextureCube::Release()
  56. {
  57. if (object_)
  58. {
  59. if (!graphics_)
  60. return;
  61. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  62. {
  63. if (graphics_->GetTexture(i) == this)
  64. graphics_->SetTexture(i, 0);
  65. }
  66. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  67. {
  68. if (renderSurfaces_[i])
  69. renderSurfaces_[i]->Release();
  70. }
  71. glDeleteTextures(1, &object_);
  72. object_ = 0;
  73. }
  74. }
  75. bool TextureCube::SetSize(int size, unsigned format, TextureUsage usage)
  76. {
  77. if (size <= 0)
  78. {
  79. LOGERROR("Zero or negative cube texture size");
  80. return false;
  81. }
  82. if (usage == TEXTURE_DEPTHSTENCIL)
  83. {
  84. LOGERROR("Depth stencil usage not supported for cube maps");
  85. return false;
  86. }
  87. // Delete the old rendersurfaces if any
  88. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  89. {
  90. renderSurfaces_[i].Reset();
  91. faceMemoryUse_[i] = 0;
  92. }
  93. if (usage == TEXTURE_RENDERTARGET)
  94. {
  95. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  96. renderSurfaces_[i] = new RenderSurface(this);
  97. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  98. addressMode_[COORD_U] = ADDRESS_CLAMP;
  99. addressMode_[COORD_V] = ADDRESS_CLAMP;
  100. addressMode_[COORD_W] = ADDRESS_CLAMP;
  101. filterMode_ = FILTER_NEAREST;
  102. requestedLevels_ = 1;
  103. dynamic_ = true;
  104. }
  105. else if (usage == TEXTURE_DYNAMIC)
  106. dynamic_ = true;
  107. else
  108. dynamic_ = false;
  109. width_ = size;
  110. height_ = size;
  111. format_ = format;
  112. return Create();
  113. }
  114. bool TextureCube::Load(Deserializer& source)
  115. {
  116. PROFILE(LoadTextureCube);
  117. ResourceCache* cache = GetSubsystem<ResourceCache>();
  118. if (!graphics_)
  119. return false;
  120. // If over the texture budget, see if materials can be freed to allow textures to be freed
  121. CheckTextureBudget(GetTypeStatic());
  122. String texPath, texName, texExt;
  123. SplitPath(GetName(), texPath, texName, texExt);
  124. SharedPtr<XMLFile> xml(new XMLFile(context_));
  125. if (!xml->Load(source))
  126. return false;
  127. LoadParameters(xml);
  128. XMLElement textureElem = xml->GetRootElement();
  129. XMLElement faceElem = textureElem.GetChildElement("face");
  130. unsigned faces = 0;
  131. while ((faceElem) && (faces < MAX_CUBEMAP_FACES))
  132. {
  133. String name = faceElem.GetString("name");
  134. String faceTexPath, faceTexName, faceTexExt;
  135. SplitPath(name, faceTexPath, faceTexName, faceTexExt);
  136. // If path is empty, add the XML file path
  137. if (faceTexPath.Empty())
  138. name = texPath + name;
  139. SharedPtr<Image> image(cache->GetResource<Image>(name));
  140. Load((CubeMapFace)faces, image);
  141. faces++;
  142. faceElem = faceElem.GetNextElement("face");
  143. }
  144. return true;
  145. }
  146. bool TextureCube::Load(CubeMapFace face, Deserializer& source)
  147. {
  148. PROFILE(LoadTextureCube);
  149. SharedPtr<Image> image(new Image(context_));
  150. if (!image->Load(source))
  151. return false;
  152. return Load(face, image);
  153. }
  154. bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image)
  155. {
  156. if (!image)
  157. {
  158. LOGERROR("Null image, can not load texture");
  159. return false;
  160. }
  161. unsigned memoryUse = 0;
  162. int quality = QUALITY_HIGH;
  163. Renderer* renderer = GetSubsystem<Renderer>();
  164. if (renderer)
  165. quality = renderer->GetTextureQuality();
  166. if (!image->IsCompressed())
  167. {
  168. unsigned char* levelData = image->GetData();
  169. int levelWidth = image->GetWidth();
  170. int levelHeight = image->GetHeight();
  171. unsigned components = image->GetComponents();
  172. unsigned format = 0;
  173. if (levelWidth != levelHeight)
  174. {
  175. LOGERROR("Cube texture width not equal to height");
  176. return false;
  177. }
  178. // Discard unnecessary mip levels
  179. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  180. {
  181. image = image->GetNextLevel();
  182. levelWidth = image->GetWidth();
  183. levelHeight = image->GetHeight();
  184. }
  185. switch (components)
  186. {
  187. case 1:
  188. format = Graphics::GetLuminanceFormat();
  189. break;
  190. case 2:
  191. format = Graphics::GetLuminanceAlphaFormat();
  192. break;
  193. case 3:
  194. format = Graphics::GetRGBFormat();
  195. break;
  196. case 4:
  197. format = Graphics::GetRGBAFormat();
  198. break;
  199. }
  200. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  201. if (!face)
  202. SetSize(levelWidth, format);
  203. else
  204. {
  205. if (!object_)
  206. {
  207. LOGERROR("Cube texture face 0 must be loaded first");
  208. return false;
  209. }
  210. if ((levelWidth != width_) || (format != format_))
  211. {
  212. LOGERROR("Cube texture face does not match size or format of face 0");
  213. return false;
  214. }
  215. }
  216. graphics_->SetTextureForUpdate(this);
  217. for (unsigned i = 0; i < levels_; ++i)
  218. {
  219. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, i, format_, levelWidth, levelHeight, 0,
  220. GetExternalFormat(format_), GL_UNSIGNED_BYTE, levelData);
  221. if (i < levels_ - 1)
  222. {
  223. image = image->GetNextLevel();
  224. levelData = image->GetData();
  225. levelWidth = image->GetWidth();
  226. levelHeight = image->GetHeight();
  227. }
  228. memoryUse += levelWidth * levelHeight * components;
  229. }
  230. graphics_->SetTexture(0, 0);
  231. }
  232. else
  233. {
  234. int width = image->GetWidth();
  235. int height = image->GetHeight();
  236. unsigned levels = image->GetNumCompressedLevels();
  237. unsigned format = GetDXTFormat(image->GetCompressedFormat());
  238. if (width != height)
  239. {
  240. LOGERROR("Cube texture width not equal to height");
  241. return false;
  242. }
  243. unsigned mipsToSkip = mipsToSkip_[quality];
  244. if (mipsToSkip >= levels)
  245. mipsToSkip = levels - 1;
  246. while ((mipsToSkip) && ((width / (1 << mipsToSkip) < 4) || (height / (1 << mipsToSkip) < 4)))
  247. --mipsToSkip;
  248. width /= (1 << mipsToSkip);
  249. height /= (1 << mipsToSkip);
  250. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  251. if (!face)
  252. {
  253. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  254. SetSize(width, format);
  255. }
  256. else
  257. {
  258. if (!object_)
  259. {
  260. LOGERROR("Cube texture face 0 must be loaded first");
  261. return false;
  262. }
  263. if ((width != width_) || (format != format_))
  264. {
  265. LOGERROR("Cube texture face does not match size or format of face 0");
  266. return false;
  267. }
  268. }
  269. graphics_->SetTextureForUpdate(this);
  270. for (unsigned i = 0; (i < levels_) && (i < levels - mipsToSkip); ++i)
  271. {
  272. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  273. glCompressedTexImage2D(textureType_, i, format_, level.width_, level.height_, 0, level.dataSize_, level.data_);
  274. memoryUse += level.rows_ * level.rowSize_;
  275. }
  276. graphics_->SetTexture(0, 0);
  277. }
  278. faceMemoryUse_[face] = memoryUse;
  279. unsigned totalMemoryUse = 0;
  280. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  281. totalMemoryUse += faceMemoryUse_[i];
  282. SetMemoryUse(totalMemoryUse);
  283. return true;
  284. }
  285. bool TextureCube::Create()
  286. {
  287. Release();
  288. if (!graphics_)
  289. return false;
  290. if ((!width_) || (!height_))
  291. return false;
  292. glGenTextures(1, &object_);
  293. // Ensure that our texture is bound to OpenGL texture unit 0
  294. graphics_->SetTextureForUpdate(this);
  295. // If not compressed, create the initial level 0 texture with null data
  296. unsigned externalFormat = GetExternalFormat(format_);
  297. unsigned dataType = GetDataType(format_);
  298. if ((format_ != GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) && (format_ != GL_COMPRESSED_RGBA_S3TC_DXT3_EXT) &&
  299. (format_ != GL_COMPRESSED_RGBA_S3TC_DXT5_EXT))
  300. {
  301. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  302. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format_, width_, height_, 0, externalFormat, dataType, 0);
  303. }
  304. // Set mipmapping
  305. levels_ = requestedLevels_;
  306. if (!levels_)
  307. {
  308. unsigned maxSize = max((int)width_, (int)height_);
  309. while (maxSize)
  310. {
  311. maxSize >>= 1;
  312. ++levels_;
  313. }
  314. }
  315. glTexParameteri(textureType_, GL_TEXTURE_BASE_LEVEL, 0);
  316. glTexParameteri(textureType_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  317. // Set initial parameters, then unbind the texture
  318. UpdateParameters();
  319. graphics_->SetTexture(0, 0);
  320. return true;
  321. }