TextureCube.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 "StringUtils.h"
  33. #include "TextureCube.h"
  34. #include "XMLFile.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. lockedLevel_(-1)
  43. {
  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::OnDeviceLost()
  56. {
  57. if (pool_ == D3DPOOL_DEFAULT)
  58. Release();
  59. }
  60. void TextureCube::OnDeviceReset()
  61. {
  62. if (pool_ == D3DPOOL_DEFAULT)
  63. {
  64. Create();
  65. dataLost_ = true;
  66. }
  67. }
  68. void TextureCube::Release()
  69. {
  70. if (object_)
  71. {
  72. if (!graphics_)
  73. return;
  74. Unlock();
  75. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  76. {
  77. if (graphics_->GetTexture(i) == this)
  78. graphics_->SetTexture(i, 0);
  79. }
  80. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  81. {
  82. if (renderSurfaces_[i])
  83. renderSurfaces_[i]->Release();
  84. }
  85. ((IDirect3DCubeTexture9*)object_)->Release();
  86. object_ = 0;
  87. }
  88. }
  89. bool TextureCube::SetSize(int size, unsigned format, TextureUsage usage)
  90. {
  91. if (size <= 0)
  92. {
  93. LOGERROR("Zero or negative cube texture size");
  94. return false;
  95. }
  96. if (usage == TEXTURE_DEPTHSTENCIL)
  97. {
  98. LOGERROR("Depth stencil usage not supported for cube maps");
  99. return false;
  100. }
  101. // Delete the old rendersurfaces if any
  102. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  103. {
  104. renderSurfaces_[i].Reset();
  105. faceMemoryUse_[i] = 0;
  106. }
  107. pool_ = D3DPOOL_MANAGED;
  108. usage_ = 0;
  109. if (usage == TEXTURE_RENDERTARGET)
  110. {
  111. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  112. renderSurfaces_[i] = new RenderSurface(this);
  113. usage_ |= D3DUSAGE_RENDERTARGET;
  114. pool_ = D3DPOOL_DEFAULT;
  115. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  116. addressMode_[COORD_U] = ADDRESS_CLAMP;
  117. addressMode_[COORD_V] = ADDRESS_CLAMP;
  118. addressMode_[COORD_W] = ADDRESS_CLAMP;
  119. filterMode_ = FILTER_NEAREST;
  120. requestedLevels_ = 1;
  121. }
  122. else if (usage == TEXTURE_DYNAMIC)
  123. {
  124. usage_ |= D3DUSAGE_DYNAMIC;
  125. pool_ = D3DPOOL_DEFAULT;
  126. }
  127. width_ = size;
  128. height_ = size;
  129. format_ = format;
  130. return Create();
  131. }
  132. bool TextureCube::Load(Deserializer& source)
  133. {
  134. PROFILE(LoadTextureCube);
  135. ResourceCache* cache = GetSubsystem<ResourceCache>();
  136. if (!graphics_)
  137. return false;
  138. // If over the texture budget, see if materials can be freed to allow textures to be freed
  139. CheckTextureBudget(GetTypeStatic());
  140. std::string texPath, texName, texExt;
  141. SplitPath(GetName(), texPath, texName, texExt);
  142. SharedPtr<XMLFile> xml(new XMLFile(context_));
  143. if (!xml->Load(source))
  144. return false;
  145. LoadParameters(xml);
  146. XMLElement textureElem = xml->GetRootElement();
  147. XMLElement faceElem = textureElem.GetChildElement("face");
  148. unsigned faces = 0;
  149. while ((faceElem) && (faces < MAX_CUBEMAP_FACES))
  150. {
  151. std::string name = faceElem.GetString("name");
  152. std::string faceTexPath, faceTexName, faceTexExt;
  153. SplitPath(name, faceTexPath, faceTexName, faceTexExt);
  154. // If path is empty, add the XML file path
  155. if (faceTexPath.empty())
  156. name = texPath + name;
  157. SharedPtr<Image> image(cache->GetResource<Image>(name));
  158. Load((CubeMapFace)faces, image);
  159. faces++;
  160. faceElem = faceElem.GetNextElement("face");
  161. }
  162. return true;
  163. }
  164. bool TextureCube::Load(CubeMapFace face, Deserializer& source)
  165. {
  166. PROFILE(LoadTextureCube);
  167. SharedPtr<Image> image(new Image(context_));
  168. if (!image->Load(source))
  169. return false;
  170. return Load(face, image);
  171. }
  172. bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image)
  173. {
  174. if (!image)
  175. {
  176. LOGERROR("Null image, can not load texture");
  177. return false;
  178. }
  179. unsigned memoryUse = 0;
  180. int quality = QUALITY_HIGH;
  181. Renderer* renderer = GetSubsystem<Renderer>();
  182. if (renderer)
  183. quality = renderer->GetTextureQuality();
  184. if (!image->IsCompressed())
  185. {
  186. unsigned char* levelData = image->GetData();
  187. int levelWidth = image->GetWidth();
  188. int levelHeight = image->GetHeight();
  189. unsigned components = image->GetComponents();
  190. D3DFORMAT format = D3DFMT_UNKNOWN;
  191. if (levelWidth != levelHeight)
  192. {
  193. LOGERROR("Cube texture width not equal to height");
  194. return false;
  195. }
  196. // Discard unnecessary mip levels
  197. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  198. {
  199. image = image->GetNextLevel();
  200. levelWidth = image->GetWidth();
  201. levelHeight = image->GetHeight();
  202. }
  203. switch (components)
  204. {
  205. case 1:
  206. format = D3DFMT_L8;
  207. break;
  208. case 2:
  209. format = D3DFMT_A8L8;
  210. break;
  211. case 3:
  212. format = D3DFMT_X8R8G8B8;
  213. break;
  214. case 4:
  215. format = D3DFMT_A8R8G8B8;
  216. break;
  217. }
  218. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  219. if (!face)
  220. SetSize(levelWidth, format);
  221. else
  222. {
  223. if (!object_)
  224. {
  225. LOGERROR("Cube texture face 0 must be loaded first");
  226. return false;
  227. }
  228. if ((levelWidth != width_) || (format != format_))
  229. {
  230. LOGERROR("Cube texture face does not match size or format of face 0");
  231. return false;
  232. }
  233. }
  234. // Make sure manual mip creation is never attempted for D3DPOOL_DEFAULT resources
  235. unsigned manualLevels = (pool_ == D3DPOOL_DEFAULT) ? 1 : levels_;
  236. for (unsigned i = 0; i < manualLevels; ++i)
  237. {
  238. D3DLOCKED_RECT hwRect;
  239. if (!Lock(face, i, 0, &hwRect))
  240. return false;
  241. memoryUse += levelWidth * levelHeight * components;
  242. for (int y = 0; y < levelHeight; ++y)
  243. {
  244. unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * y;
  245. unsigned char* src = levelData + components * levelWidth * y;
  246. switch (components)
  247. {
  248. case 1:
  249. case 2:
  250. memcpy(dest, src, components * levelWidth);
  251. break;
  252. case 3:
  253. for (int x = 0; x < levelWidth; ++x)
  254. {
  255. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = 255;
  256. src += 3;
  257. }
  258. break;
  259. case 4:
  260. for (int x = 0; x < levelWidth; ++x)
  261. {
  262. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = src[3];
  263. src += 4;
  264. }
  265. break;
  266. }
  267. }
  268. if (i < manualLevels - 1)
  269. {
  270. image = image->GetNextLevel();
  271. levelData = image->GetData();
  272. levelWidth = image->GetWidth();
  273. levelHeight = image->GetHeight();
  274. }
  275. Unlock();
  276. }
  277. }
  278. else
  279. {
  280. int width = image->GetWidth();
  281. int height = image->GetHeight();
  282. unsigned levels = image->GetNumCompressedLevels();
  283. D3DFORMAT format = (D3DFORMAT)GetCompressedD3DFormat(image->GetCompressedFormat());
  284. if (width != height)
  285. {
  286. LOGERROR("Cube texture width not equal to height");
  287. return false;
  288. }
  289. unsigned mipsToSkip = mipsToSkip_[quality];
  290. if (mipsToSkip >= levels)
  291. mipsToSkip = levels - 1;
  292. while ((mipsToSkip) && ((width / (1 << mipsToSkip) < 4) || (height / (1 << mipsToSkip) < 4)))
  293. --mipsToSkip;
  294. width /= (1 << mipsToSkip);
  295. height /= (1 << mipsToSkip);
  296. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  297. if (!face)
  298. {
  299. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  300. SetSize(width, format);
  301. }
  302. else
  303. {
  304. if (!object_)
  305. {
  306. LOGERROR("Cube texture face 0 must be loaded first");
  307. return false;
  308. }
  309. if ((width != 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 < levels - mipsToSkip); ++i)
  316. {
  317. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  318. memoryUse += level.rows_ * level.rowSize_;
  319. D3DLOCKED_RECT hwRect;
  320. if (!Lock(face, i, 0, &hwRect))
  321. return false;
  322. for (unsigned j = 0; j < level.rows_; ++j)
  323. {
  324. unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * j;
  325. unsigned char* src = level.data_ + level.rowSize_ * j;
  326. memcpy(dest, src, level.rowSize_);
  327. }
  328. Unlock();
  329. }
  330. }
  331. faceMemoryUse_[face] = memoryUse;
  332. unsigned totalMemoryUse = 0;
  333. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  334. totalMemoryUse += faceMemoryUse_[i];
  335. SetMemoryUse(totalMemoryUse);
  336. return true;
  337. }
  338. bool TextureCube::Lock(CubeMapFace face, unsigned level, void* rect, void* hwRect)
  339. {
  340. D3DLOCKED_RECT* lockedRect = (D3DLOCKED_RECT*)hwRect;
  341. if (!lockedRect)
  342. {
  343. LOGERROR("Null locked rect structure");
  344. return false;
  345. }
  346. if (!object_)
  347. {
  348. LOGERROR("No texture created, can not lock");
  349. return false;
  350. }
  351. if (lockedLevel_ != -1)
  352. {
  353. LOGERROR("Texture already locked");
  354. return false;
  355. }
  356. DWORD flags = 0;
  357. if ((!rect) && (pool_ == D3DPOOL_DEFAULT))
  358. flags |= D3DLOCK_DISCARD;
  359. if (FAILED(((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, lockedRect, (RECT*)rect, flags)))
  360. {
  361. LOGERROR("Could not lock texture");
  362. return false;
  363. }
  364. lockedLevel_ = level;
  365. lockedFace_ = face;
  366. return true;
  367. }
  368. void TextureCube::Unlock()
  369. {
  370. if (lockedLevel_ != -1)
  371. {
  372. ((IDirect3DCubeTexture9*)object_)->UnlockRect((D3DCUBEMAP_FACES)lockedFace_, lockedLevel_);
  373. lockedLevel_ = -1;
  374. }
  375. }
  376. bool TextureCube::Create()
  377. {
  378. Release();
  379. if (!graphics_)
  380. return false;
  381. if ((!width_) || (!height_))
  382. return false;
  383. // If using a default pool texture, must generate mipmaps automatically
  384. if ((pool_ == D3DPOOL_DEFAULT) && (requestedLevels_ != 1))
  385. usage_ |= D3DUSAGE_AUTOGENMIPMAP;
  386. else
  387. usage_ &= ~D3DUSAGE_AUTOGENMIPMAP;
  388. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  389. if ((!device) || (FAILED(device->CreateCubeTexture(
  390. width_,
  391. requestedLevels_,
  392. usage_,
  393. (D3DFORMAT)format_,
  394. (D3DPOOL)pool_,
  395. (IDirect3DCubeTexture9**)&object_,
  396. 0))))
  397. {
  398. LOGERROR("Could not create cube texture");
  399. return false;
  400. }
  401. levels_ = ((IDirect3DCubeTexture9*)object_)->GetLevelCount();
  402. if (usage_ & D3DUSAGE_RENDERTARGET)
  403. {
  404. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  405. {
  406. ((IDirect3DCubeTexture9*)object_)->GetCubeMapSurface((D3DCUBEMAP_FACES)i, 0,
  407. (IDirect3DSurface9**)&renderSurfaces_[i]->surface_);
  408. }
  409. }
  410. return true;
  411. }