D3D9TextureCube.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "DebugNew.h"
  35. #ifdef _MSC_VER
  36. #pragma warning(disable:4355)
  37. #endif
  38. OBJECTTYPESTATIC(TextureCube);
  39. TextureCube::TextureCube(Context* context) :
  40. Texture(context),
  41. lockedLevel_(-1)
  42. {
  43. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  44. faceMemoryUse_[i] = 0;
  45. }
  46. TextureCube::~TextureCube()
  47. {
  48. Release();
  49. }
  50. void TextureCube::RegisterObject(Context* context)
  51. {
  52. context->RegisterFactory<TextureCube>();
  53. }
  54. void TextureCube::OnDeviceLost()
  55. {
  56. if (pool_ == D3DPOOL_DEFAULT)
  57. Release();
  58. }
  59. void TextureCube::OnDeviceReset()
  60. {
  61. if (pool_ == D3DPOOL_DEFAULT)
  62. {
  63. // If has a file name, reload through the resource cache. Otherwise recreate and mark the data lost
  64. if (!GetName().Trimmed().Empty())
  65. GetSubsystem<ResourceCache>()->ReloadResource(this);
  66. else
  67. {
  68. Create();
  69. dataLost_ = true;
  70. }
  71. }
  72. }
  73. void TextureCube::Release()
  74. {
  75. if (object_)
  76. {
  77. if (!graphics_)
  78. return;
  79. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  80. {
  81. if (graphics_->GetTexture(i) == this)
  82. graphics_->SetTexture(i, 0);
  83. }
  84. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  85. {
  86. if (renderSurfaces_[i])
  87. renderSurfaces_[i]->Release();
  88. }
  89. ((IDirect3DCubeTexture9*)object_)->Release();
  90. object_ = 0;
  91. }
  92. }
  93. bool TextureCube::SetSize(int size, unsigned format, TextureUsage usage)
  94. {
  95. if (size <= 0)
  96. {
  97. LOGERROR("Zero or negative cube texture size");
  98. return false;
  99. }
  100. if (usage == TEXTURE_DEPTHSTENCIL)
  101. {
  102. LOGERROR("Depth-stencil usage not supported for cube maps");
  103. return false;
  104. }
  105. // Delete the old rendersurfaces if any
  106. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  107. {
  108. renderSurfaces_[i].Reset();
  109. faceMemoryUse_[i] = 0;
  110. }
  111. pool_ = D3DPOOL_MANAGED;
  112. usage_ = 0;
  113. if (usage == TEXTURE_RENDERTARGET)
  114. {
  115. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  116. renderSurfaces_[i] = new RenderSurface(this);
  117. usage_ |= D3DUSAGE_RENDERTARGET;
  118. pool_ = D3DPOOL_DEFAULT;
  119. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  120. addressMode_[COORD_U] = ADDRESS_CLAMP;
  121. addressMode_[COORD_V] = ADDRESS_CLAMP;
  122. addressMode_[COORD_W] = ADDRESS_CLAMP;
  123. filterMode_ = FILTER_NEAREST;
  124. requestedLevels_ = 1;
  125. }
  126. else if (usage == TEXTURE_DYNAMIC)
  127. {
  128. usage_ |= D3DUSAGE_DYNAMIC;
  129. pool_ = D3DPOOL_DEFAULT;
  130. }
  131. width_ = size;
  132. height_ = size;
  133. format_ = format;
  134. return Create();
  135. }
  136. bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data)
  137. {
  138. if (!object_)
  139. {
  140. LOGERROR("No texture created, can not set data");
  141. return false;
  142. }
  143. if (!data)
  144. {
  145. LOGERROR("Null source for setting data");
  146. return false;
  147. }
  148. if (level >= levels_)
  149. {
  150. LOGERROR("Illegal mip level for setting data");
  151. return false;
  152. }
  153. if (IsCompressed())
  154. {
  155. x &= ~3;
  156. y &= ~3;
  157. }
  158. int levelWidth = GetLevelWidth(level);
  159. int levelHeight = GetLevelHeight(level);
  160. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  161. {
  162. LOGERROR("Illegal dimensions for setting data");
  163. return false;
  164. }
  165. D3DLOCKED_RECT d3dLockedRect;
  166. RECT d3dRect;
  167. d3dRect.left = x;
  168. d3dRect.top = y;
  169. d3dRect.right = x + width;
  170. d3dRect.bottom = y + height;
  171. DWORD flags = 0;
  172. if (level == 0 && x == 0 && y == 0 && width == levelWidth && height == levelHeight && pool_ == D3DPOOL_DEFAULT)
  173. flags |= D3DLOCK_DISCARD;
  174. if (FAILED(((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, &d3dLockedRect, (flags &
  175. D3DLOCK_DISCARD) ? 0 : &d3dRect, flags)))
  176. {
  177. LOGERROR("Could not lock texture");
  178. return false;
  179. }
  180. if (IsCompressed())
  181. {
  182. height = (height + 3) >> 2;
  183. y >>= 2;
  184. }
  185. unsigned char* src = (unsigned char*)data;
  186. unsigned rowSize = GetRowDataSize(width);
  187. unsigned rowOffset = GetRowDataSize(x);
  188. // GetRowDataSize() returns CPU-side (source) data size, so need to convert for X8R8G8B8
  189. if (format_ == D3DFMT_X8R8G8B8)
  190. {
  191. rowSize = rowSize / 3 * 4;
  192. rowOffset = rowOffset / 3 * 4;
  193. }
  194. // Perform conversion from RGB / RGBA as necessary
  195. switch (format_)
  196. {
  197. default:
  198. for (int i = 0; i < height; ++i)
  199. {
  200. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  201. memcpy(dest, src, rowSize);
  202. src += rowSize;
  203. }
  204. break;
  205. case D3DFMT_X8R8G8B8:
  206. for (int i = 0; i < height; ++i)
  207. {
  208. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  209. for (int j = 0; j < width; ++j)
  210. {
  211. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = 255;
  212. src += 3;
  213. }
  214. }
  215. break;
  216. case D3DFMT_A8R8G8B8:
  217. for (int i = 0; i < height; ++i)
  218. {
  219. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  220. for (int j = 0; j < width; ++j)
  221. {
  222. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = src[3];
  223. src += 4;
  224. }
  225. }
  226. break;
  227. }
  228. ((IDirect3DCubeTexture9*)object_)->UnlockRect((D3DCUBEMAP_FACES)face, level);
  229. return true;
  230. }
  231. bool TextureCube::Load(Deserializer& source)
  232. {
  233. PROFILE(LoadTextureCube);
  234. ResourceCache* cache = GetSubsystem<ResourceCache>();
  235. // In headless mode, do not actually load the texture, just return success
  236. Graphics* graphics = GetSubsystem<Graphics>();
  237. if (!graphics)
  238. return true;
  239. // If over the texture budget, see if materials can be freed to allow textures to be freed
  240. CheckTextureBudget(GetTypeStatic());
  241. String texPath, texName, texExt;
  242. SplitPath(GetName(), texPath, texName, texExt);
  243. SharedPtr<XMLFile> xml(new XMLFile(context_));
  244. if (!xml->Load(source))
  245. return false;
  246. LoadParameters(xml);
  247. XMLElement textureElem = xml->GetRoot();
  248. XMLElement faceElem = textureElem.GetChild("face");
  249. unsigned faces = 0;
  250. while (faceElem && faces < MAX_CUBEMAP_FACES)
  251. {
  252. String name = faceElem.GetAttribute("name");
  253. String faceTexPath, faceTexName, faceTexExt;
  254. SplitPath(name, faceTexPath, faceTexName, faceTexExt);
  255. // If path is empty, add the XML file path
  256. if (faceTexPath.Empty())
  257. name = texPath + name;
  258. SharedPtr<Image> image(cache->GetResource<Image>(name));
  259. Load((CubeMapFace)faces, image);
  260. faces++;
  261. faceElem = faceElem.GetNext("face");
  262. }
  263. return true;
  264. }
  265. bool TextureCube::Load(CubeMapFace face, Deserializer& source)
  266. {
  267. PROFILE(LoadTextureCube);
  268. SharedPtr<Image> image(new Image(context_));
  269. if (!image->Load(source))
  270. return false;
  271. return Load(face, image);
  272. }
  273. bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image, bool useAlpha)
  274. {
  275. if (!image)
  276. {
  277. LOGERROR("Null image, can not load texture");
  278. return false;
  279. }
  280. unsigned memoryUse = 0;
  281. int quality = QUALITY_HIGH;
  282. Renderer* renderer = GetSubsystem<Renderer>();
  283. if (renderer)
  284. quality = renderer->GetTextureQuality();
  285. if (!image->IsCompressed())
  286. {
  287. unsigned char* levelData = image->GetData();
  288. int levelWidth = image->GetWidth();
  289. int levelHeight = image->GetHeight();
  290. unsigned components = image->GetComponents();
  291. unsigned format = 0;
  292. if (levelWidth != levelHeight)
  293. {
  294. LOGERROR("Cube texture width not equal to height");
  295. return false;
  296. }
  297. // Discard unnecessary mip levels
  298. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  299. {
  300. image = image->GetNextLevel();
  301. levelWidth = image->GetWidth();
  302. levelHeight = image->GetHeight();
  303. }
  304. switch (components)
  305. {
  306. case 1:
  307. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  308. break;
  309. case 2:
  310. format = Graphics::GetLuminanceAlphaFormat();
  311. break;
  312. case 3:
  313. format = Graphics::GetRGBFormat();
  314. break;
  315. case 4:
  316. format = Graphics::GetRGBAFormat();
  317. break;
  318. }
  319. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  320. if (!face)
  321. SetSize(levelWidth, format);
  322. else
  323. {
  324. if (!object_)
  325. {
  326. LOGERROR("Cube texture face 0 must be loaded first");
  327. return false;
  328. }
  329. if (levelWidth != width_ || format != format_)
  330. {
  331. LOGERROR("Cube texture face does not match size or format of face 0");
  332. return false;
  333. }
  334. }
  335. for (unsigned i = 0; i < levels_; ++i)
  336. {
  337. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  338. memoryUse += levelWidth * levelHeight * components;
  339. if (i < levels_ - 1)
  340. {
  341. image = image->GetNextLevel();
  342. levelData = image->GetData();
  343. levelWidth = image->GetWidth();
  344. levelHeight = image->GetHeight();
  345. }
  346. }
  347. }
  348. else
  349. {
  350. int width = image->GetWidth();
  351. int height = image->GetHeight();
  352. unsigned levels = image->GetNumCompressedLevels();
  353. unsigned format = GetDXTFormat(image->GetCompressedFormat());
  354. if (width != height)
  355. {
  356. LOGERROR("Cube texture width not equal to height");
  357. return false;
  358. }
  359. unsigned mipsToSkip = mipsToSkip_[quality];
  360. if (mipsToSkip >= levels)
  361. mipsToSkip = levels - 1;
  362. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  363. --mipsToSkip;
  364. width /= (1 << mipsToSkip);
  365. height /= (1 << mipsToSkip);
  366. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  367. if (!face)
  368. {
  369. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  370. SetSize(width, format);
  371. }
  372. else
  373. {
  374. if (!object_)
  375. {
  376. LOGERROR("Cube texture face 0 must be loaded first");
  377. return false;
  378. }
  379. if (width != width_ || format != format_)
  380. {
  381. LOGERROR("Cube texture face does not match size or format of face 0");
  382. return false;
  383. }
  384. }
  385. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  386. {
  387. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  388. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  389. memoryUse += level.rows_ * level.rowSize_;
  390. }
  391. }
  392. faceMemoryUse_[face] = memoryUse;
  393. unsigned totalMemoryUse = sizeof(TextureCube);
  394. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  395. totalMemoryUse += faceMemoryUse_[i];
  396. SetMemoryUse(totalMemoryUse);
  397. return true;
  398. }
  399. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  400. {
  401. if (!object_)
  402. {
  403. LOGERROR("No texture created, can not get data");
  404. return false;
  405. }
  406. if (!dest)
  407. {
  408. LOGERROR("Null destination for getting data");
  409. return false;
  410. }
  411. if (level >= levels_)
  412. {
  413. LOGERROR("Illegal mip level for getting data");
  414. return false;
  415. }
  416. int levelWidth = GetLevelWidth(level);
  417. int levelHeight = GetLevelHeight(level);
  418. D3DLOCKED_RECT d3dLockedRect;
  419. RECT d3dRect;
  420. d3dRect.left = 0;
  421. d3dRect.top = 0;
  422. d3dRect.right = levelWidth;
  423. d3dRect.bottom = levelHeight;
  424. if (FAILED(((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, &d3dLockedRect, &d3dRect, D3DLOCK_READONLY)))
  425. {
  426. LOGERROR("Could not lock texture");
  427. return false;
  428. }
  429. int height = levelHeight;
  430. if (IsCompressed())
  431. height = (height + 3) >> 2;
  432. unsigned char* destPtr = (unsigned char*)dest;
  433. unsigned rowSize = GetRowDataSize(levelWidth);
  434. // GetRowDataSize() returns CPU-side (destination) data size, so need to convert for X8R8G8B8
  435. if (format_ == D3DFMT_X8R8G8B8)
  436. rowSize = rowSize / 3 * 4;
  437. // Perform conversion to RGB / RGBA as necessary
  438. switch (format_)
  439. {
  440. default:
  441. for (int i = 0; i < height; ++i)
  442. {
  443. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  444. memcpy(destPtr, src, rowSize);
  445. destPtr += rowSize;
  446. }
  447. break;
  448. case D3DFMT_X8R8G8B8:
  449. for (int i = 0; i < height; ++i)
  450. {
  451. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  452. for (int j = 0; j < levelWidth; ++j)
  453. {
  454. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; ++src;
  455. destPtr += 3;
  456. }
  457. }
  458. break;
  459. case D3DFMT_A8R8G8B8:
  460. for (int i = 0; i < height; ++i)
  461. {
  462. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  463. for (int j = 0; j < levelWidth; ++j)
  464. {
  465. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; destPtr[3] = *src++;
  466. destPtr += 4;
  467. }
  468. }
  469. break;
  470. }
  471. ((IDirect3DCubeTexture9*)object_)->UnlockRect((D3DCUBEMAP_FACES)face, level);
  472. return true;
  473. }
  474. bool TextureCube::Create()
  475. {
  476. Release();
  477. if (!graphics_)
  478. return false;
  479. if (!width_ || !height_)
  480. return false;
  481. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  482. if (!device || FAILED(device->CreateCubeTexture(
  483. width_,
  484. requestedLevels_,
  485. usage_,
  486. (D3DFORMAT)format_,
  487. (D3DPOOL)pool_,
  488. (IDirect3DCubeTexture9**)&object_,
  489. 0)))
  490. {
  491. LOGERROR("Could not create cube texture");
  492. return false;
  493. }
  494. levels_ = ((IDirect3DCubeTexture9*)object_)->GetLevelCount();
  495. if (usage_ & D3DUSAGE_RENDERTARGET)
  496. {
  497. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  498. {
  499. ((IDirect3DCubeTexture9*)object_)->GetCubeMapSurface((D3DCUBEMAP_FACES)i, 0,
  500. (IDirect3DSurface9**)&renderSurfaces_[i]->surface_);
  501. }
  502. }
  503. return true;
  504. }