TextureCube.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 "Log.h"
  25. #include "Profiler.h"
  26. #include "Renderer.h"
  27. #include "RendererImpl.h"
  28. #include "ResourceCache.h"
  29. #include "StringUtils.h"
  30. #include "TextureCube.h"
  31. #include "XMLFile.h"
  32. #include "DebugNew.h"
  33. #ifdef _MSC_VER
  34. #pragma warning(disable:4355)
  35. #endif
  36. TextureCube::TextureCube(Renderer* renderer, TextureUsage usage, const std::string& name) :
  37. Texture(renderer, name),
  38. mLockedLevel(-1)
  39. {
  40. mUsage = 0;
  41. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  42. mFaceMemoryUse[i] = 0;
  43. if (usage == TEXTURE_DEPTHSTENCIL)
  44. EXCEPTION("Depth stencil usage not supported for cube maps");
  45. if (usage == TEXTURE_RENDERTARGET)
  46. {
  47. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  48. mRenderSurfaces[i] = new RenderSurface(this);
  49. mUsage |= D3DUSAGE_RENDERTARGET;
  50. mPool = D3DPOOL_DEFAULT;
  51. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  52. mAddressMode[COORD_U] = ADDRESS_CLAMP;
  53. mAddressMode[COORD_V] = ADDRESS_CLAMP;
  54. mAddressMode[COORD_W] = ADDRESS_CLAMP;
  55. mFilterMode = FILTER_NEAREST;
  56. mRequestedLevels = 1;
  57. }
  58. else if (usage == TEXTURE_DYNAMIC)
  59. {
  60. mUsage |= D3DUSAGE_DYNAMIC;
  61. mPool = D3DPOOL_DEFAULT;
  62. }
  63. else
  64. mPool = D3DPOOL_MANAGED;
  65. }
  66. TextureCube::~TextureCube()
  67. {
  68. release();
  69. }
  70. void TextureCube::onDeviceLost()
  71. {
  72. if (mPool == D3DPOOL_DEFAULT)
  73. release();
  74. }
  75. void TextureCube::onDeviceReset()
  76. {
  77. if (mPool == D3DPOOL_DEFAULT)
  78. {
  79. create();
  80. mDataLost = true;
  81. }
  82. }
  83. void TextureCube::setSize(int size, unsigned format)
  84. {
  85. if (size <= 0)
  86. EXCEPTION("Zero or negative cube texture size");
  87. mWidth = size;
  88. mHeight = size;
  89. mFormat = format;
  90. create();
  91. }
  92. void TextureCube::load(Deserializer& source, ResourceCache* cache)
  93. {
  94. if (!mRenderer)
  95. return;
  96. PROFILE(TextureCube_Load);
  97. if (!cache)
  98. EXCEPTION("Null resource cache for TextureCube");
  99. // If over the texture budget, see if materials can be freed to allow textures to be freed
  100. checkTextureBudget(getTypeStatic(), cache);
  101. std::string texPath;
  102. std::string texName;
  103. std::string texExt;
  104. splitPath(getName(), texPath, texName, texExt);
  105. XMLFile xml;
  106. xml.load(source, cache);
  107. loadParameters(xml);
  108. XMLElement textureElem = xml.getRootElement();
  109. XMLElement faceElem = textureElem.getChildElement("face");
  110. unsigned faces = 0;
  111. while ((faceElem) && (faces < MAX_CUBEMAP_FACES))
  112. {
  113. std::string name = faceElem.getString("name");
  114. std::string faceTexPath;
  115. std::string faceTexName;
  116. std::string faceTexExt;
  117. splitPath(name, faceTexPath, faceTexName, faceTexExt);
  118. // If path is empty, add the XML file path
  119. if (faceTexPath.empty())
  120. name = texPath + name;
  121. SharedPtr<Image> image(cache->getResource<Image>(name));
  122. load((CubeMapFace)faces, image);
  123. faces++;
  124. faceElem = faceElem.getNextElement("face");
  125. }
  126. }
  127. void TextureCube::load(CubeMapFace face, Deserializer& source)
  128. {
  129. PROFILE(TextureCube_Load);
  130. SharedPtr<Image> image(new Image());
  131. image->load(source);
  132. load(face, image);
  133. }
  134. void TextureCube::load(CubeMapFace face, SharedPtr<Image> image)
  135. {
  136. unsigned memoryUse = 0;
  137. if (!image->isCompressed())
  138. {
  139. unsigned char* levelData = image->getData();
  140. int levelWidth = image->getWidth();
  141. int levelHeight = image->getHeight();
  142. unsigned components = image->getComponents();
  143. D3DFORMAT format = D3DFMT_UNKNOWN;
  144. if (levelWidth != levelHeight)
  145. EXCEPTION("Cube texture width not equal to height");
  146. // Discard unnecessary mip levels
  147. for (unsigned i = 0; i < mMipsToSkip[sQuality]; ++i)
  148. {
  149. image = image->getNextLevel();
  150. levelWidth = image->getWidth();
  151. levelHeight = image->getHeight();
  152. }
  153. switch (components)
  154. {
  155. case 1:
  156. format = D3DFMT_L8;
  157. break;
  158. case 2:
  159. format = D3DFMT_A8L8;
  160. break;
  161. case 3:
  162. format = D3DFMT_X8R8G8B8;
  163. break;
  164. case 4:
  165. format = D3DFMT_A8R8G8B8;
  166. break;
  167. }
  168. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  169. if (!face)
  170. setSize(levelWidth, format);
  171. else
  172. {
  173. if (!mObject)
  174. EXCEPTION("Cube texture face 0 must be loaded first");
  175. if ((levelWidth != mWidth) || (format != mFormat))
  176. EXCEPTION("Cube texture face does not match size or format of face 0");
  177. }
  178. // Make sure manual mip creation is never attempted for D3DPOOL_DEFAULT resources
  179. unsigned manualLevels = (mPool == D3DPOOL_DEFAULT) ? 1 : mLevels;
  180. for (unsigned i = 0; i < manualLevels; ++i)
  181. {
  182. D3DLOCKED_RECT hwRect;
  183. lock(face, i, 0, &hwRect);
  184. memoryUse += levelWidth * levelHeight * components;
  185. for (int y = 0; y < levelHeight; ++y)
  186. {
  187. unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * y;
  188. unsigned char* src = levelData + components * levelWidth * y;
  189. switch (components)
  190. {
  191. case 1:
  192. case 2:
  193. memcpy(dest, src, components * levelWidth);
  194. break;
  195. case 3:
  196. for (int x = 0; x < levelWidth; ++x)
  197. {
  198. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = 255;
  199. src += 3;
  200. }
  201. break;
  202. case 4:
  203. for (int x = 0; x < levelWidth; ++x)
  204. {
  205. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = src[3];
  206. src += 4;
  207. }
  208. break;
  209. }
  210. }
  211. if (i < manualLevels - 1)
  212. {
  213. image = image->getNextLevel();
  214. levelData = image->getData();
  215. levelWidth = image->getWidth();
  216. levelHeight = image->getHeight();
  217. }
  218. unlock();
  219. }
  220. }
  221. else
  222. {
  223. int width = image->getWidth();
  224. int height = image->getHeight();
  225. unsigned levels = image->getNumCompressedLevels();
  226. D3DFORMAT format = (D3DFORMAT)getCompressedD3DFormat(image->getCompressedFormat());
  227. if (width != height)
  228. EXCEPTION("Cube texture width not equal to height");
  229. unsigned mipsToSkip = mMipsToSkip[sQuality];
  230. if (mipsToSkip >= levels)
  231. mipsToSkip = levels - 1;
  232. while ((mipsToSkip) && ((width / (1 << mipsToSkip) < 4) || (height / (1 << mipsToSkip) < 4)))
  233. --mipsToSkip;
  234. width /= (1 << mipsToSkip);
  235. height /= (1 << mipsToSkip);
  236. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  237. if (!face)
  238. {
  239. setNumLevels(max((int)(levels - mipsToSkip), 1));
  240. setSize(width, format);
  241. }
  242. else
  243. {
  244. if (!mObject)
  245. EXCEPTION("Cube texture face 0 must be loaded first");
  246. if ((width != mWidth) || (format != mFormat))
  247. EXCEPTION("Cube texture face does not match size or format of face 0");
  248. }
  249. for (unsigned i = 0; (i < mLevels) && (i < levels - mipsToSkip); ++i)
  250. {
  251. CompressedLevel level = image->getCompressedLevel(i + mipsToSkip);
  252. memoryUse += level.mRows * level.mRowSize;
  253. D3DLOCKED_RECT hwRect;
  254. lock(face, i, 0, &hwRect);
  255. for (unsigned j = 0; j < level.mRows; ++j)
  256. {
  257. unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * j;
  258. unsigned char* src = level.mData + level.mRowSize * j;
  259. memcpy(dest, src, level.mRowSize);
  260. }
  261. unlock();
  262. }
  263. }
  264. mFaceMemoryUse[face] = memoryUse;
  265. unsigned totalMemoryUse = 0;
  266. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  267. totalMemoryUse += mFaceMemoryUse[i];
  268. setMemoryUse(totalMemoryUse);
  269. }
  270. void TextureCube::lock(CubeMapFace face, unsigned level, void* rect, void* hwRect)
  271. {
  272. D3DLOCKED_RECT* lockedRect = (D3DLOCKED_RECT*)hwRect;
  273. if (!lockedRect)
  274. EXCEPTION("Null locked rect structure");
  275. if (!mObject)
  276. EXCEPTION("No texture created, can not lock");
  277. if (mLockedLevel != -1)
  278. EXCEPTION("Texture already locked");
  279. DWORD flags = 0;
  280. if ((!rect) && (mPool == D3DPOOL_DEFAULT))
  281. flags |= D3DLOCK_DISCARD;
  282. if (FAILED(((IDirect3DCubeTexture9*)mObject)->LockRect((D3DCUBEMAP_FACES)face, level, lockedRect, (RECT*)rect, flags)))
  283. EXCEPTION("Could not lock texture");
  284. mLockedLevel = level;
  285. mLockedFace = face;
  286. }
  287. void TextureCube::unlock()
  288. {
  289. if (mLockedLevel != -1)
  290. {
  291. ((IDirect3DCubeTexture9*)mObject)->UnlockRect((D3DCUBEMAP_FACES)mLockedFace, mLockedLevel);
  292. mLockedLevel = -1;
  293. }
  294. }
  295. void TextureCube::create()
  296. {
  297. release();
  298. if (!mRenderer)
  299. return;
  300. if ((!mWidth) || (!mHeight))
  301. return;
  302. // If using a default pool texture, must generate mipmaps automatically
  303. if ((mPool == D3DPOOL_DEFAULT) && (mRequestedLevels != 1))
  304. mUsage |= D3DUSAGE_AUTOGENMIPMAP;
  305. else
  306. mUsage &= ~D3DUSAGE_AUTOGENMIPMAP;
  307. if (FAILED(mRenderer->getImpl()->getDevice()->CreateCubeTexture(
  308. mWidth,
  309. mRequestedLevels,
  310. mUsage,
  311. (D3DFORMAT)mFormat,
  312. (D3DPOOL)mPool,
  313. (IDirect3DCubeTexture9**)&mObject,
  314. 0)))
  315. EXCEPTION("Could not create cube texture");
  316. mLevels = ((IDirect3DCubeTexture9*)mObject)->GetLevelCount();
  317. if (mUsage & D3DUSAGE_RENDERTARGET)
  318. {
  319. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  320. ((IDirect3DCubeTexture9*)mObject)->GetCubeMapSurface((D3DCUBEMAP_FACES)i, 0, (IDirect3DSurface9**)&mRenderSurfaces[i]->mSurface);
  321. }
  322. }
  323. void TextureCube::release()
  324. {
  325. if (mObject)
  326. {
  327. if (!mRenderer)
  328. {
  329. LOGWARNING("Renderer has expired, skipping release of TextureCube");
  330. return;
  331. }
  332. unlock();
  333. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  334. {
  335. if (mRenderer->getTexture(i) == this)
  336. mRenderer->setTexture(i, 0);
  337. }
  338. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  339. {
  340. if (mRenderSurfaces[i])
  341. mRenderSurfaces[i]->release();
  342. }
  343. ((IDirect3DCubeTexture9*)mObject)->Release();
  344. mObject = 0;
  345. }
  346. }