D3D9TextureCube.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 "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. bool compressed = format_ == D3DFMT_DXT1 || format_ == D3DFMT_DXT3 || format_ == D3DFMT_DXT5;
  154. if (compressed)
  155. {
  156. x &= ~3;
  157. y &= ~3;
  158. }
  159. int levelWidth = GetLevelWidth(level);
  160. int levelHeight = GetLevelHeight(level);
  161. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  162. {
  163. LOGERROR("Illegal dimensions for setting data");
  164. return false;
  165. }
  166. D3DLOCKED_RECT d3dLockedRect;
  167. RECT d3dRect;
  168. d3dRect.left = x;
  169. d3dRect.top = y;
  170. d3dRect.right = x + width;
  171. d3dRect.bottom = y + height;
  172. DWORD flags = 0;
  173. if (level == 0 && x == 0 && y == 0 && width == levelWidth && height == levelHeight && pool_ == D3DPOOL_DEFAULT)
  174. flags |= D3DLOCK_DISCARD;
  175. if (FAILED(((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, &d3dLockedRect, (flags &
  176. D3DLOCK_DISCARD) ? 0 : &d3dRect, flags)))
  177. {
  178. LOGERROR("Could not lock texture");
  179. return false;
  180. }
  181. if (compressed)
  182. {
  183. height = (height + 3) >> 2;
  184. y >>= 2;
  185. }
  186. unsigned char* src = (unsigned char*)data;
  187. unsigned rowSize = GetRowDataSize(width);
  188. unsigned rowOffset = GetRowDataSize(x);
  189. // GetRowDataSize() returns CPU-side (source) data size, so need to convert for X8R8G8B8
  190. if (format_ == D3DFMT_X8R8G8B8)
  191. {
  192. rowSize = rowSize / 3 * 4;
  193. rowOffset = rowOffset / 3 * 4;
  194. }
  195. // Perform conversion from RGB / RGBA as necessary
  196. switch (format_)
  197. {
  198. default:
  199. for (int i = 0; i < height; ++i)
  200. {
  201. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  202. memcpy(dest, src, rowSize);
  203. src += rowSize;
  204. }
  205. break;
  206. case D3DFMT_X8R8G8B8:
  207. for (int i = 0; i < height; ++i)
  208. {
  209. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  210. for (int j = 0; j < width; ++j)
  211. {
  212. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = 255;
  213. src += 3;
  214. }
  215. }
  216. break;
  217. case D3DFMT_A8R8G8B8:
  218. for (int i = 0; i < height; ++i)
  219. {
  220. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  221. for (int j = 0; j < width; ++j)
  222. {
  223. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = src[3];
  224. src += 4;
  225. }
  226. }
  227. break;
  228. }
  229. ((IDirect3DCubeTexture9*)object_)->UnlockRect((D3DCUBEMAP_FACES)face, level);
  230. return true;
  231. }
  232. bool TextureCube::Load(Deserializer& source)
  233. {
  234. PROFILE(LoadTextureCube);
  235. ResourceCache* cache = GetSubsystem<ResourceCache>();
  236. // In headless mode, do not actually load the texture, just return success
  237. Graphics* graphics = GetSubsystem<Graphics>();
  238. if (!graphics)
  239. return true;
  240. // If over the texture budget, see if materials can be freed to allow textures to be freed
  241. CheckTextureBudget(GetTypeStatic());
  242. String texPath, texName, texExt;
  243. SplitPath(GetName(), texPath, texName, texExt);
  244. SharedPtr<XMLFile> xml(new XMLFile(context_));
  245. if (!xml->Load(source))
  246. return false;
  247. LoadParameters(xml);
  248. XMLElement textureElem = xml->GetRoot();
  249. XMLElement faceElem = textureElem.GetChild("face");
  250. unsigned faces = 0;
  251. while (faceElem && faces < MAX_CUBEMAP_FACES)
  252. {
  253. String name = faceElem.GetString("name");
  254. String faceTexPath, faceTexName, faceTexExt;
  255. SplitPath(name, faceTexPath, faceTexName, faceTexExt);
  256. // If path is empty, add the XML file path
  257. if (faceTexPath.Empty())
  258. name = texPath + name;
  259. SharedPtr<Image> image(cache->GetResource<Image>(name));
  260. Load((CubeMapFace)faces, image);
  261. faces++;
  262. faceElem = faceElem.GetNext("face");
  263. }
  264. return true;
  265. }
  266. bool TextureCube::Load(CubeMapFace face, Deserializer& source)
  267. {
  268. PROFILE(LoadTextureCube);
  269. SharedPtr<Image> image(new Image(context_));
  270. if (!image->Load(source))
  271. return false;
  272. return Load(face, image);
  273. }
  274. bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image, bool useAlpha)
  275. {
  276. if (!image)
  277. {
  278. LOGERROR("Null image, can not load texture");
  279. return false;
  280. }
  281. unsigned memoryUse = 0;
  282. int quality = QUALITY_HIGH;
  283. Renderer* renderer = GetSubsystem<Renderer>();
  284. if (renderer)
  285. quality = renderer->GetTextureQuality();
  286. if (!image->IsCompressed())
  287. {
  288. unsigned char* levelData = image->GetData();
  289. int levelWidth = image->GetWidth();
  290. int levelHeight = image->GetHeight();
  291. unsigned components = image->GetComponents();
  292. unsigned format = 0;
  293. if (levelWidth != levelHeight)
  294. {
  295. LOGERROR("Cube texture width not equal to height");
  296. return false;
  297. }
  298. // Discard unnecessary mip levels
  299. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  300. {
  301. image = image->GetNextLevel();
  302. levelWidth = image->GetWidth();
  303. levelHeight = image->GetHeight();
  304. }
  305. switch (components)
  306. {
  307. case 1:
  308. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  309. break;
  310. case 2:
  311. format = Graphics::GetLuminanceAlphaFormat();
  312. break;
  313. case 3:
  314. format = Graphics::GetRGBFormat();
  315. break;
  316. case 4:
  317. format = Graphics::GetRGBAFormat();
  318. break;
  319. }
  320. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  321. if (!face)
  322. SetSize(levelWidth, format);
  323. else
  324. {
  325. if (!object_)
  326. {
  327. LOGERROR("Cube texture face 0 must be loaded first");
  328. return false;
  329. }
  330. if (levelWidth != width_ || format != format_)
  331. {
  332. LOGERROR("Cube texture face does not match size or format of face 0");
  333. return false;
  334. }
  335. }
  336. for (unsigned i = 0; i < levels_; ++i)
  337. {
  338. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  339. memoryUse += levelWidth * levelHeight * components;
  340. if (i < levels_ - 1)
  341. {
  342. image = image->GetNextLevel();
  343. levelData = image->GetData();
  344. levelWidth = image->GetWidth();
  345. levelHeight = image->GetHeight();
  346. }
  347. }
  348. }
  349. else
  350. {
  351. int width = image->GetWidth();
  352. int height = image->GetHeight();
  353. unsigned levels = image->GetNumCompressedLevels();
  354. unsigned format = GetDXTFormat(image->GetCompressedFormat());
  355. if (width != height)
  356. {
  357. LOGERROR("Cube texture width not equal to height");
  358. return false;
  359. }
  360. unsigned mipsToSkip = mipsToSkip_[quality];
  361. if (mipsToSkip >= levels)
  362. mipsToSkip = levels - 1;
  363. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  364. --mipsToSkip;
  365. width /= (1 << mipsToSkip);
  366. height /= (1 << mipsToSkip);
  367. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  368. if (!face)
  369. {
  370. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  371. SetSize(width, format);
  372. }
  373. else
  374. {
  375. if (!object_)
  376. {
  377. LOGERROR("Cube texture face 0 must be loaded first");
  378. return false;
  379. }
  380. if (width != width_ || format != format_)
  381. {
  382. LOGERROR("Cube texture face does not match size or format of face 0");
  383. return false;
  384. }
  385. }
  386. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  387. {
  388. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  389. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  390. memoryUse += level.rows_ * level.rowSize_;
  391. }
  392. }
  393. faceMemoryUse_[face] = memoryUse;
  394. unsigned totalMemoryUse = sizeof(TextureCube);
  395. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  396. totalMemoryUse += faceMemoryUse_[i];
  397. SetMemoryUse(totalMemoryUse);
  398. return true;
  399. }
  400. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  401. {
  402. if (!object_)
  403. {
  404. LOGERROR("No texture created, can not get data");
  405. return false;
  406. }
  407. if (!dest)
  408. {
  409. LOGERROR("Null destination for getting data");
  410. return false;
  411. }
  412. if (level >= levels_)
  413. {
  414. LOGERROR("Illegal mip level for getting data");
  415. return false;
  416. }
  417. bool compressed = format_ == D3DFMT_DXT1 || format_ == D3DFMT_DXT3 || format_ == D3DFMT_DXT5;
  418. int levelWidth = GetLevelWidth(level);
  419. int levelHeight = GetLevelHeight(level);
  420. D3DLOCKED_RECT d3dLockedRect;
  421. RECT d3dRect;
  422. d3dRect.left = 0;
  423. d3dRect.top = 0;
  424. d3dRect.right = levelWidth;
  425. d3dRect.bottom = levelHeight;
  426. if (FAILED(((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, &d3dLockedRect, &d3dRect, D3DLOCK_READONLY)))
  427. {
  428. LOGERROR("Could not lock texture");
  429. return false;
  430. }
  431. int height = levelHeight;
  432. if (compressed)
  433. height = (height + 3) >> 2;
  434. unsigned char* destPtr = (unsigned char*)dest;
  435. unsigned rowSize = GetRowDataSize(levelWidth);
  436. // GetRowDataSize() returns CPU-side (destination) data size, so need to convert for X8R8G8B8
  437. if (format_ == D3DFMT_X8R8G8B8)
  438. rowSize = rowSize / 3 * 4;
  439. // Perform conversion to RGB / RGBA as necessary
  440. switch (format_)
  441. {
  442. default:
  443. for (int i = 0; i < height; ++i)
  444. {
  445. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  446. memcpy(destPtr, src, rowSize);
  447. destPtr += rowSize;
  448. }
  449. break;
  450. case D3DFMT_X8R8G8B8:
  451. for (int i = 0; i < height; ++i)
  452. {
  453. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  454. for (int j = 0; j < levelWidth; ++j)
  455. {
  456. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; ++src;
  457. destPtr += 3;
  458. }
  459. }
  460. break;
  461. case D3DFMT_A8R8G8B8:
  462. for (int i = 0; i < height; ++i)
  463. {
  464. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  465. for (int j = 0; j < levelWidth; ++j)
  466. {
  467. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; destPtr[3] = *src++;
  468. destPtr += 4;
  469. }
  470. }
  471. break;
  472. }
  473. ((IDirect3DCubeTexture9*)object_)->UnlockRect((D3DCUBEMAP_FACES)face, level);
  474. return true;
  475. }
  476. bool TextureCube::Create()
  477. {
  478. Release();
  479. if (!graphics_)
  480. return false;
  481. if (!width_ || !height_)
  482. return false;
  483. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  484. if (!device || FAILED(device->CreateCubeTexture(
  485. width_,
  486. requestedLevels_,
  487. usage_,
  488. (D3DFORMAT)format_,
  489. (D3DPOOL)pool_,
  490. (IDirect3DCubeTexture9**)&object_,
  491. 0)))
  492. {
  493. LOGERROR("Could not create cube texture");
  494. return false;
  495. }
  496. levels_ = ((IDirect3DCubeTexture9*)object_)->GetLevelCount();
  497. if (usage_ & D3DUSAGE_RENDERTARGET)
  498. {
  499. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  500. {
  501. ((IDirect3DCubeTexture9*)object_)->GetCubeMapSurface((D3DCUBEMAP_FACES)i, 0,
  502. (IDirect3DSurface9**)&renderSurfaces_[i]->surface_);
  503. }
  504. }
  505. return true;
  506. }