TextureCube.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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) || (!cache))
  95. return;
  96. PROFILE(TextureCube_Load);
  97. // If over the texture budget, see if materials can be freed to allow textures to be freed
  98. checkTextureBudget(getTypeStatic(), cache);
  99. std::string texPath;
  100. std::string texName;
  101. std::string texExt;
  102. splitPath(getName(), texPath, texName, texExt);
  103. XMLFile xml;
  104. xml.load(source, cache);
  105. loadParameters(&xml);
  106. XMLElement textureElem = xml.getRootElement();
  107. XMLElement faceElem = textureElem.getChildElement("face");
  108. unsigned faces = 0;
  109. while ((faceElem) && (faces < MAX_CUBEMAP_FACES))
  110. {
  111. std::string name = faceElem.getString("name");
  112. std::string faceTexPath;
  113. std::string faceTexName;
  114. std::string faceTexExt;
  115. splitPath(name, faceTexPath, faceTexName, faceTexExt);
  116. // If path is empty, add the XML file path
  117. if (faceTexPath.empty())
  118. name = texPath + name;
  119. SharedPtr<Image> image(cache->getResource<Image>(name));
  120. load((CubeMapFace)faces, image);
  121. faces++;
  122. faceElem = faceElem.getNextElement("face");
  123. }
  124. }
  125. void TextureCube::load(CubeMapFace face, Deserializer& source)
  126. {
  127. PROFILE(TextureCube_Load);
  128. SharedPtr<Image> image(new Image());
  129. image->load(source);
  130. load(face, image);
  131. }
  132. void TextureCube::load(CubeMapFace face, SharedPtr<Image> image)
  133. {
  134. unsigned memoryUse = 0;
  135. if (!image->isCompressed())
  136. {
  137. unsigned char* levelData = image->getData();
  138. int levelWidth = image->getWidth();
  139. int levelHeight = image->getHeight();
  140. unsigned components = image->getComponents();
  141. D3DFORMAT format = D3DFMT_UNKNOWN;
  142. if (levelWidth != levelHeight)
  143. EXCEPTION("Cube texture width not equal to height");
  144. // Discard unnecessary mip levels
  145. for (unsigned i = 0; i < mMipsToSkip[sQuality]; ++i)
  146. {
  147. image = image->getNextLevel();
  148. levelWidth = image->getWidth();
  149. levelHeight = image->getHeight();
  150. }
  151. switch (components)
  152. {
  153. case 1:
  154. format = D3DFMT_L8;
  155. break;
  156. case 2:
  157. format = D3DFMT_A8L8;
  158. break;
  159. case 3:
  160. format = D3DFMT_X8R8G8B8;
  161. break;
  162. case 4:
  163. format = D3DFMT_A8R8G8B8;
  164. break;
  165. }
  166. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  167. if (!face)
  168. setSize(levelWidth, format);
  169. else
  170. {
  171. if (!mObject)
  172. EXCEPTION("Cube texture face 0 must be loaded first");
  173. if ((levelWidth != mWidth) || (format != mFormat))
  174. EXCEPTION("Cube texture face does not match size or format of face 0");
  175. }
  176. // Make sure manual mip creation is never attempted for D3DPOOL_DEFAULT resources
  177. unsigned manualLevels = (mPool == D3DPOOL_DEFAULT) ? 1 : mLevels;
  178. for (unsigned i = 0; i < manualLevels; ++i)
  179. {
  180. D3DLOCKED_RECT hwRect;
  181. lock(face, i, 0, &hwRect);
  182. memoryUse += levelWidth * levelHeight * components;
  183. for (int y = 0; y < levelHeight; ++y)
  184. {
  185. unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * y;
  186. unsigned char* src = levelData + components * levelWidth * y;
  187. switch (components)
  188. {
  189. case 1:
  190. case 2:
  191. memcpy(dest, src, components * levelWidth);
  192. break;
  193. case 3:
  194. for (int x = 0; x < levelWidth; ++x)
  195. {
  196. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = 255;
  197. src += 3;
  198. }
  199. break;
  200. case 4:
  201. for (int x = 0; x < levelWidth; ++x)
  202. {
  203. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = src[3];
  204. src += 4;
  205. }
  206. break;
  207. }
  208. }
  209. if (i < manualLevels - 1)
  210. {
  211. image = image->getNextLevel();
  212. levelData = image->getData();
  213. levelWidth = image->getWidth();
  214. levelHeight = image->getHeight();
  215. }
  216. unlock();
  217. }
  218. }
  219. else
  220. {
  221. int width = image->getWidth();
  222. int height = image->getHeight();
  223. unsigned levels = image->getNumCompressedLevels();
  224. D3DFORMAT format = (D3DFORMAT)getCompressedD3DFormat(image->getCompressedFormat());
  225. if (width != height)
  226. EXCEPTION("Cube texture width not equal to height");
  227. unsigned mipsToSkip = mMipsToSkip[sQuality];
  228. if (mipsToSkip >= levels)
  229. mipsToSkip = levels - 1;
  230. while ((mipsToSkip) && ((width / (1 << mipsToSkip) < 4) || (height / (1 << mipsToSkip) < 4)))
  231. --mipsToSkip;
  232. width /= (1 << mipsToSkip);
  233. height /= (1 << mipsToSkip);
  234. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  235. if (!face)
  236. {
  237. setNumLevels(max((int)(levels - mipsToSkip), 1));
  238. setSize(width, format);
  239. }
  240. else
  241. {
  242. if (!mObject)
  243. EXCEPTION("Cube texture face 0 must be loaded first");
  244. if ((width != mWidth) || (format != mFormat))
  245. EXCEPTION("Cube texture face does not match size or format of face 0");
  246. }
  247. for (unsigned i = 0; (i < mLevels) && (i < levels - mipsToSkip); ++i)
  248. {
  249. CompressedLevel level = image->getCompressedLevel(i + mipsToSkip);
  250. memoryUse += level.mRows * level.mRowSize;
  251. D3DLOCKED_RECT hwRect;
  252. lock(face, i, 0, &hwRect);
  253. for (unsigned j = 0; j < level.mRows; ++j)
  254. {
  255. unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * j;
  256. unsigned char* src = level.mData + level.mRowSize * j;
  257. memcpy(dest, src, level.mRowSize);
  258. }
  259. unlock();
  260. }
  261. }
  262. mFaceMemoryUse[face] = memoryUse;
  263. unsigned totalMemoryUse = 0;
  264. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  265. totalMemoryUse += mFaceMemoryUse[i];
  266. setMemoryUse(totalMemoryUse);
  267. }
  268. void TextureCube::lock(CubeMapFace face, unsigned level, void* rect, void* hwRect)
  269. {
  270. D3DLOCKED_RECT* lockedRect = (D3DLOCKED_RECT*)hwRect;
  271. if (!lockedRect)
  272. EXCEPTION("Null locked rect structure");
  273. if (!mObject)
  274. EXCEPTION("No texture created, can not lock");
  275. if (mLockedLevel != -1)
  276. EXCEPTION("Texture already locked");
  277. DWORD flags = 0;
  278. if ((!rect) && (mPool == D3DPOOL_DEFAULT))
  279. flags |= D3DLOCK_DISCARD;
  280. if (FAILED(((IDirect3DCubeTexture9*)mObject)->LockRect((D3DCUBEMAP_FACES)face, level, lockedRect, (RECT*)rect, flags)))
  281. EXCEPTION("Could not lock texture");
  282. mLockedLevel = level;
  283. mLockedFace = face;
  284. }
  285. void TextureCube::unlock()
  286. {
  287. if (mLockedLevel != -1)
  288. {
  289. ((IDirect3DCubeTexture9*)mObject)->UnlockRect((D3DCUBEMAP_FACES)mLockedFace, mLockedLevel);
  290. mLockedLevel = -1;
  291. }
  292. }
  293. void TextureCube::create()
  294. {
  295. release();
  296. if (!mRenderer)
  297. return;
  298. if ((!mWidth) || (!mHeight))
  299. return;
  300. // If using a default pool texture, must generate mipmaps automatically
  301. if ((mPool == D3DPOOL_DEFAULT) && (mRequestedLevels != 1))
  302. mUsage |= D3DUSAGE_AUTOGENMIPMAP;
  303. else
  304. mUsage &= ~D3DUSAGE_AUTOGENMIPMAP;
  305. if (FAILED(mRenderer->getImpl()->getDevice()->CreateCubeTexture(
  306. mWidth,
  307. mRequestedLevels,
  308. mUsage,
  309. (D3DFORMAT)mFormat,
  310. (D3DPOOL)mPool,
  311. (IDirect3DCubeTexture9**)&mObject,
  312. 0)))
  313. EXCEPTION("Could not create cube texture");
  314. mLevels = ((IDirect3DCubeTexture9*)mObject)->GetLevelCount();
  315. if (mUsage & D3DUSAGE_RENDERTARGET)
  316. {
  317. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  318. ((IDirect3DCubeTexture9*)mObject)->GetCubeMapSurface((D3DCUBEMAP_FACES)i, 0, (IDirect3DSurface9**)&mRenderSurfaces[i]->mSurface);
  319. }
  320. }
  321. void TextureCube::release()
  322. {
  323. if (mObject)
  324. {
  325. if (!mRenderer)
  326. {
  327. LOGWARNING("Renderer has expired, skipping release of TextureCube");
  328. return;
  329. }
  330. unlock();
  331. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  332. {
  333. if (mRenderer->getTexture(i) == this)
  334. mRenderer->setTexture(i, 0);
  335. }
  336. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  337. {
  338. if (mRenderSurfaces[i])
  339. mRenderSurfaces[i]->release();
  340. }
  341. ((IDirect3DCubeTexture9*)mObject)->Release();
  342. mObject = 0;
  343. }
  344. }