TextureCube.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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;
  141. std::string texName;
  142. std::string texExt;
  143. SplitPath(GetName(), texPath, texName, texExt);
  144. SharedPtr<XMLFile> xml(new XMLFile(context_));
  145. if (!xml->Load(source))
  146. return false;
  147. LoadParameters(xml);
  148. XMLElement textureElem = xml->GetRootElement();
  149. XMLElement faceElem = textureElem.GetChildElement("face");
  150. unsigned faces = 0;
  151. while ((faceElem) && (faces < MAX_CUBEMAP_FACES))
  152. {
  153. std::string name = faceElem.GetString("name");
  154. std::string faceTexPath;
  155. std::string faceTexName;
  156. std::string faceTexExt;
  157. SplitPath(name, faceTexPath, faceTexName, faceTexExt);
  158. // If path is empty, add the XML file path
  159. if (faceTexPath.empty())
  160. name = texPath + name;
  161. SharedPtr<Image> image(cache->GetResource<Image>(name));
  162. Load((CubeMapFace)faces, image);
  163. faces++;
  164. faceElem = faceElem.GetNextElement("face");
  165. }
  166. return true;
  167. }
  168. bool TextureCube::Load(CubeMapFace face, Deserializer& source)
  169. {
  170. PROFILE(LoadTextureCube);
  171. SharedPtr<Image> image(new Image(context_));
  172. if (!image->Load(source))
  173. return false;
  174. return Load(face, image);
  175. }
  176. bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image)
  177. {
  178. if (!image)
  179. {
  180. LOGERROR("Null image, can not load texture");
  181. return false;
  182. }
  183. unsigned memoryUse = 0;
  184. int quality = QUALITY_HIGH;
  185. Renderer* renderer = GetSubsystem<Renderer>();
  186. if (renderer)
  187. quality = renderer->GetTextureQuality();
  188. if (!image->IsCompressed())
  189. {
  190. unsigned char* levelData = image->GetData();
  191. int levelWidth = image->GetWidth();
  192. int levelHeight = image->GetHeight();
  193. unsigned components = image->GetComponents();
  194. D3DFORMAT format = D3DFMT_UNKNOWN;
  195. if (levelWidth != levelHeight)
  196. {
  197. LOGERROR("Cube texture width not equal to height");
  198. return false;
  199. }
  200. // Discard unnecessary mip levels
  201. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  202. {
  203. image = image->GetNextLevel();
  204. levelWidth = image->GetWidth();
  205. levelHeight = image->GetHeight();
  206. }
  207. switch (components)
  208. {
  209. case 1:
  210. format = D3DFMT_L8;
  211. break;
  212. case 2:
  213. format = D3DFMT_A8L8;
  214. break;
  215. case 3:
  216. format = D3DFMT_X8R8G8B8;
  217. break;
  218. case 4:
  219. format = D3DFMT_A8R8G8B8;
  220. break;
  221. }
  222. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  223. if (!face)
  224. SetSize(levelWidth, format);
  225. else
  226. {
  227. if (!object_)
  228. {
  229. LOGERROR("Cube texture face 0 must be loaded first");
  230. return false;
  231. }
  232. if ((levelWidth != width_) || (format != format_))
  233. {
  234. LOGERROR("Cube texture face does not match size or format of face 0");
  235. return false;
  236. }
  237. }
  238. // Make sure manual mip creation is never attempted for D3DPOOL_DEFAULT resources
  239. unsigned manualLevels = (pool_ == D3DPOOL_DEFAULT) ? 1 : levels_;
  240. for (unsigned i = 0; i < manualLevels; ++i)
  241. {
  242. D3DLOCKED_RECT hwRect;
  243. if (!Lock(face, i, 0, &hwRect))
  244. return false;
  245. memoryUse += levelWidth * levelHeight * components;
  246. for (int y = 0; y < levelHeight; ++y)
  247. {
  248. unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * y;
  249. unsigned char* src = levelData + components * levelWidth * y;
  250. switch (components)
  251. {
  252. case 1:
  253. case 2:
  254. memcpy(dest, src, components * levelWidth);
  255. break;
  256. case 3:
  257. for (int x = 0; x < levelWidth; ++x)
  258. {
  259. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = 255;
  260. src += 3;
  261. }
  262. break;
  263. case 4:
  264. for (int x = 0; x < levelWidth; ++x)
  265. {
  266. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = src[3];
  267. src += 4;
  268. }
  269. break;
  270. }
  271. }
  272. if (i < manualLevels - 1)
  273. {
  274. image = image->GetNextLevel();
  275. levelData = image->GetData();
  276. levelWidth = image->GetWidth();
  277. levelHeight = image->GetHeight();
  278. }
  279. Unlock();
  280. }
  281. }
  282. else
  283. {
  284. int width = image->GetWidth();
  285. int height = image->GetHeight();
  286. unsigned levels = image->GetNumCompressedLevels();
  287. D3DFORMAT format = (D3DFORMAT)GetCompressedD3DFormat(image->GetCompressedFormat());
  288. if (width != height)
  289. {
  290. LOGERROR("Cube texture width not equal to height");
  291. return false;
  292. }
  293. unsigned mipsToSkip = mipsToSkip_[quality];
  294. if (mipsToSkip >= levels)
  295. mipsToSkip = levels - 1;
  296. while ((mipsToSkip) && ((width / (1 << mipsToSkip) < 4) || (height / (1 << mipsToSkip) < 4)))
  297. --mipsToSkip;
  298. width /= (1 << mipsToSkip);
  299. height /= (1 << mipsToSkip);
  300. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  301. if (!face)
  302. {
  303. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  304. SetSize(width, format);
  305. }
  306. else
  307. {
  308. if (!object_)
  309. {
  310. LOGERROR("Cube texture face 0 must be loaded first");
  311. return false;
  312. }
  313. if ((width != width_) || (format != format_))
  314. {
  315. LOGERROR("Cube texture face does not match size or format of face 0");
  316. return false;
  317. }
  318. }
  319. for (unsigned i = 0; (i < levels_) && (i < levels - mipsToSkip); ++i)
  320. {
  321. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  322. memoryUse += level.rows_ * level.rowSize_;
  323. D3DLOCKED_RECT hwRect;
  324. if (!Lock(face, i, 0, &hwRect))
  325. return false;
  326. for (unsigned j = 0; j < level.rows_; ++j)
  327. {
  328. unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * j;
  329. unsigned char* src = level.data_ + level.rowSize_ * j;
  330. memcpy(dest, src, level.rowSize_);
  331. }
  332. Unlock();
  333. }
  334. }
  335. faceMemoryUse_[face] = memoryUse;
  336. unsigned totalMemoryUse = 0;
  337. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  338. totalMemoryUse += faceMemoryUse_[i];
  339. SetMemoryUse(totalMemoryUse);
  340. return true;
  341. }
  342. bool TextureCube::Lock(CubeMapFace face, unsigned level, void* rect, void* hwRect)
  343. {
  344. D3DLOCKED_RECT* lockedRect = (D3DLOCKED_RECT*)hwRect;
  345. if (!lockedRect)
  346. {
  347. LOGERROR("Null locked rect structure");
  348. return false;
  349. }
  350. if (!object_)
  351. {
  352. LOGERROR("No texture created, can not lock");
  353. return false;
  354. }
  355. if (lockedLevel_ != -1)
  356. {
  357. LOGERROR("Texture already locked");
  358. return false;
  359. }
  360. DWORD flags = 0;
  361. if ((!rect) && (pool_ == D3DPOOL_DEFAULT))
  362. flags |= D3DLOCK_DISCARD;
  363. if (FAILED(((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, lockedRect, (RECT*)rect, flags)))
  364. {
  365. LOGERROR("Could not lock texture");
  366. return false;
  367. }
  368. lockedLevel_ = level;
  369. lockedFace_ = face;
  370. return true;
  371. }
  372. void TextureCube::Unlock()
  373. {
  374. if (lockedLevel_ != -1)
  375. {
  376. ((IDirect3DCubeTexture9*)object_)->UnlockRect((D3DCUBEMAP_FACES)lockedFace_, lockedLevel_);
  377. lockedLevel_ = -1;
  378. }
  379. }
  380. bool TextureCube::Create()
  381. {
  382. Release();
  383. if (!graphics_)
  384. return false;
  385. if ((!width_) || (!height_))
  386. return false;
  387. // If using a default pool texture, must generate mipmaps automatically
  388. if ((pool_ == D3DPOOL_DEFAULT) && (requestedLevels_ != 1))
  389. usage_ |= D3DUSAGE_AUTOGENMIPMAP;
  390. else
  391. usage_ &= ~D3DUSAGE_AUTOGENMIPMAP;
  392. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  393. if ((!device) || (FAILED(device->CreateCubeTexture(
  394. width_,
  395. requestedLevels_,
  396. usage_,
  397. (D3DFORMAT)format_,
  398. (D3DPOOL)pool_,
  399. (IDirect3DCubeTexture9**)&object_,
  400. 0))))
  401. {
  402. LOGERROR("Could not create cube texture");
  403. return false;
  404. }
  405. levels_ = ((IDirect3DCubeTexture9*)object_)->GetLevelCount();
  406. if (usage_ & D3DUSAGE_RENDERTARGET)
  407. {
  408. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  409. {
  410. ((IDirect3DCubeTexture9*)object_)->GetCubeMapSurface((D3DCUBEMAP_FACES)i, 0,
  411. (IDirect3DSurface9**)&renderSurfaces_[i]->surface_);
  412. }
  413. }
  414. return true;
  415. }