D3D9TextureCube.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../../Precompiled.h"
  23. #include "../../Core/Context.h"
  24. #include "../../Core/Profiler.h"
  25. #include "../../Graphics/Graphics.h"
  26. #include "../../Graphics/GraphicsEvents.h"
  27. #include "../../Graphics/GraphicsImpl.h"
  28. #include "../../Graphics/Renderer.h"
  29. #include "../../Graphics/TextureCube.h"
  30. #include "../../IO/FileSystem.h"
  31. #include "../../IO/Log.h"
  32. #include "../../Resource/ResourceCache.h"
  33. #include "../../Resource/XMLFile.h"
  34. #include "../../DebugNew.h"
  35. #ifdef _MSC_VER
  36. #pragma warning(disable:4355)
  37. #endif
  38. namespace Atomic
  39. {
  40. static const char* cubeMapLayoutNames[] = {
  41. "horizontal",
  42. "horizontalnvidia",
  43. "horizontalcross",
  44. "verticalcross",
  45. "blender",
  46. 0
  47. };
  48. static SharedPtr<Image> GetTileImage(Image* src, int tileX, int tileY, int tileWidth, int tileHeight)
  49. {
  50. return SharedPtr<Image>(
  51. src->GetSubimage(IntRect(tileX * tileWidth, tileY * tileHeight, (tileX + 1) * tileWidth, (tileY + 1) * tileHeight)));
  52. }
  53. TextureCube::TextureCube(Context* context) :
  54. Texture(context),
  55. lockedLevel_(-1)
  56. {
  57. // Default to clamp mode addressing
  58. addressMode_[COORD_U] = ADDRESS_CLAMP;
  59. addressMode_[COORD_V] = ADDRESS_CLAMP;
  60. addressMode_[COORD_W] = ADDRESS_CLAMP;
  61. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  62. faceMemoryUse_[i] = 0;
  63. }
  64. TextureCube::~TextureCube()
  65. {
  66. Release();
  67. }
  68. void TextureCube::RegisterObject(Context* context)
  69. {
  70. context->RegisterFactory<TextureCube>();
  71. }
  72. bool TextureCube::BeginLoad(Deserializer& source)
  73. {
  74. ResourceCache* cache = GetSubsystem<ResourceCache>();
  75. // In headless mode, do not actually load the texture, just return success
  76. if (!graphics_)
  77. return true;
  78. // If device is lost, retry later
  79. if (graphics_->IsDeviceLost())
  80. {
  81. LOGWARNING("Texture load while device is lost");
  82. dataPending_ = true;
  83. return true;
  84. }
  85. cache->ResetDependencies(this);
  86. String texPath, texName, texExt;
  87. SplitPath(GetName(), texPath, texName, texExt);
  88. loadParameters_ = (new XMLFile(context_));
  89. if (!loadParameters_->Load(source))
  90. {
  91. loadParameters_.Reset();
  92. return false;
  93. }
  94. loadImages_.Clear();
  95. XMLElement textureElem = loadParameters_->GetRoot();
  96. XMLElement imageElem = textureElem.GetChild("image");
  97. // Single image and multiple faces with layout
  98. if (imageElem)
  99. {
  100. String name = imageElem.GetAttribute("name");
  101. // If path is empty, add the XML file path
  102. if (GetPath(name).Empty())
  103. name = texPath + name;
  104. SharedPtr<Image> image = cache->GetTempResource<Image>(name);
  105. if (!image)
  106. return false;
  107. int faceWidth, faceHeight;
  108. loadImages_.Resize(MAX_CUBEMAP_FACES);
  109. if (image->IsCubemap())
  110. {
  111. loadImages_[FACE_POSITIVE_X] = image;
  112. loadImages_[FACE_NEGATIVE_X] = loadImages_[FACE_POSITIVE_X]->GetNextSibling();
  113. loadImages_[FACE_POSITIVE_Y] = loadImages_[FACE_NEGATIVE_X]->GetNextSibling();
  114. loadImages_[FACE_NEGATIVE_Y] = loadImages_[FACE_POSITIVE_Y]->GetNextSibling();
  115. loadImages_[FACE_POSITIVE_Z] = loadImages_[FACE_NEGATIVE_Y]->GetNextSibling();
  116. loadImages_[FACE_NEGATIVE_Z] = loadImages_[FACE_POSITIVE_Z]->GetNextSibling();
  117. }
  118. else
  119. {
  120. CubeMapLayout layout = (CubeMapLayout)GetStringListIndex(imageElem.GetAttribute("layout").CString(), cubeMapLayoutNames, CML_HORIZONTAL);
  121. switch (layout)
  122. {
  123. case CML_HORIZONTAL:
  124. faceWidth = image->GetWidth() / MAX_CUBEMAP_FACES;
  125. faceHeight = image->GetHeight();
  126. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 0, 0, faceWidth, faceHeight);
  127. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  128. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 2, 0, faceWidth, faceHeight);
  129. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 3, 0, faceWidth, faceHeight);
  130. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 4, 0, faceWidth, faceHeight);
  131. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 5, 0, faceWidth, faceHeight);
  132. break;
  133. case CML_HORIZONTALNVIDIA:
  134. faceWidth = image->GetWidth() / MAX_CUBEMAP_FACES;
  135. faceHeight = image->GetHeight();
  136. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  137. loadImages_[i] = GetTileImage(image, i, 0, faceWidth, faceHeight);
  138. break;
  139. case CML_HORIZONTALCROSS:
  140. faceWidth = image->GetWidth() / 4;
  141. faceHeight = image->GetHeight() / 3;
  142. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  143. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  144. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  145. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  146. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 3, 1, faceWidth, faceHeight);
  147. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 1, 2, faceWidth, faceHeight);
  148. break;
  149. case CML_VERTICALCROSS:
  150. faceWidth = image->GetWidth() / 3;
  151. faceHeight = image->GetHeight() / 4;
  152. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  153. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  154. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  155. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  156. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 1, 2, faceWidth, faceHeight);
  157. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 1, 3, faceWidth, faceHeight);
  158. if (loadImages_[FACE_NEGATIVE_Z])
  159. {
  160. loadImages_[FACE_NEGATIVE_Z]->FlipVertical();
  161. loadImages_[FACE_NEGATIVE_Z]->FlipHorizontal();
  162. }
  163. break;
  164. case CML_BLENDER:
  165. faceWidth = image->GetWidth() / 3;
  166. faceHeight = image->GetHeight() / 2;
  167. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 0, faceWidth, faceHeight);
  168. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  169. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 0, faceWidth, faceHeight);
  170. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  171. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  172. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  173. break;
  174. }
  175. }
  176. }
  177. // Face per image
  178. else
  179. {
  180. XMLElement faceElem = textureElem.GetChild("face");
  181. while (faceElem)
  182. {
  183. String name = faceElem.GetAttribute("name");
  184. // If path is empty, add the XML file path
  185. if (GetPath(name).Empty())
  186. name = texPath + name;
  187. loadImages_.Push(cache->GetTempResource<Image>(name));
  188. cache->StoreResourceDependency(this, name);
  189. faceElem = faceElem.GetNext("face");
  190. }
  191. }
  192. // Precalculate mip levels if async loading
  193. if (GetAsyncLoadState() == ASYNC_LOADING)
  194. {
  195. for (unsigned i = 0; i < loadImages_.Size(); ++i)
  196. {
  197. if (loadImages_[i])
  198. loadImages_[i]->PrecalculateLevels();
  199. }
  200. }
  201. return true;
  202. }
  203. bool TextureCube::EndLoad()
  204. {
  205. // In headless mode, do not actually load the texture, just return success
  206. if (!graphics_ || graphics_->IsDeviceLost())
  207. return true;
  208. // If over the texture budget, see if materials can be freed to allow textures to be freed
  209. CheckTextureBudget(GetTypeStatic());
  210. SetParameters(loadParameters_);
  211. for (unsigned i = 0; i < loadImages_.Size() && i < MAX_CUBEMAP_FACES; ++i)
  212. SetData((CubeMapFace)i, loadImages_[i]);
  213. loadImages_.Clear();
  214. loadParameters_.Reset();
  215. return true;
  216. }
  217. void TextureCube::OnDeviceLost()
  218. {
  219. if (Graphics::IsUnmanagedPool(pool_))
  220. Release();
  221. }
  222. void TextureCube::OnDeviceReset()
  223. {
  224. if (Graphics::IsUnmanagedPool(pool_) || !object_ || dataPending_)
  225. {
  226. // If has a resource file, reload through the resource cache. Otherwise just recreate.
  227. ResourceCache* cache = GetSubsystem<ResourceCache>();
  228. if (cache->Exists(GetName()))
  229. dataLost_ = !cache->ReloadResource(this);
  230. if (!object_)
  231. {
  232. Create();
  233. dataLost_ = true;
  234. }
  235. }
  236. dataPending_ = false;
  237. }
  238. void TextureCube::Release()
  239. {
  240. if (object_)
  241. {
  242. if (!graphics_)
  243. return;
  244. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  245. {
  246. if (graphics_->GetTexture(i) == this)
  247. graphics_->SetTexture(i, 0);
  248. }
  249. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  250. {
  251. if (renderSurfaces_[i])
  252. renderSurfaces_[i]->Release();
  253. }
  254. ((IDirect3DCubeTexture9*)object_)->Release();
  255. object_ = 0;
  256. }
  257. }
  258. bool TextureCube::SetSize(int size, unsigned format, TextureUsage usage)
  259. {
  260. if (size <= 0)
  261. {
  262. LOGERROR("Zero or negative cube texture size");
  263. return false;
  264. }
  265. if (usage == TEXTURE_DEPTHSTENCIL)
  266. {
  267. LOGERROR("Depth-stencil usage not supported for cube maps");
  268. return false;
  269. }
  270. // Delete the old rendersurfaces if any
  271. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  272. {
  273. renderSurfaces_[i].Reset();
  274. faceMemoryUse_[i] = 0;
  275. }
  276. pool_ = Graphics::GetDirect3D9ExEnabled() ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
  277. usage_ = 0;
  278. if (usage == TEXTURE_RENDERTARGET)
  279. {
  280. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  281. renderSurfaces_[i] = new RenderSurface(this);
  282. usage_ |= D3DUSAGE_RENDERTARGET;
  283. pool_ = D3DPOOL_DEFAULT;
  284. // Nearest filtering and mipmaps disabled by default
  285. filterMode_ = FILTER_NEAREST;
  286. requestedLevels_ = 1;
  287. }
  288. else if (usage == TEXTURE_DYNAMIC)
  289. {
  290. usage_ |= D3DUSAGE_DYNAMIC;
  291. pool_ = D3DPOOL_DEFAULT;
  292. }
  293. if (usage == TEXTURE_RENDERTARGET)
  294. SubscribeToEvent(E_RENDERSURFACEUPDATE, HANDLER(TextureCube, HandleRenderSurfaceUpdate));
  295. else
  296. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  297. width_ = size;
  298. height_ = size;
  299. format_ = format;
  300. return Create();
  301. }
  302. bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data)
  303. {
  304. PROFILE(SetTextureData);
  305. if (!object_)
  306. {
  307. LOGERROR("No texture created, can not set data");
  308. return false;
  309. }
  310. if (!data)
  311. {
  312. LOGERROR("Null source for setting data");
  313. return false;
  314. }
  315. if (level >= levels_)
  316. {
  317. LOGERROR("Illegal mip level for setting data");
  318. return false;
  319. }
  320. if (graphics_->IsDeviceLost())
  321. {
  322. LOGWARNING("Texture data assignment while device is lost");
  323. dataPending_ = true;
  324. return true;
  325. }
  326. if (IsCompressed())
  327. {
  328. x &= ~3;
  329. y &= ~3;
  330. }
  331. int levelWidth = GetLevelWidth(level);
  332. int levelHeight = GetLevelHeight(level);
  333. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  334. {
  335. LOGERROR("Illegal dimensions for setting data");
  336. return false;
  337. }
  338. IDirect3DSurface9* offscreenSurface = 0;
  339. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  340. D3DLOCKED_RECT d3dLockedRect;
  341. RECT d3dRect;
  342. d3dRect.left = x;
  343. d3dRect.top = y;
  344. d3dRect.right = x + width;
  345. d3dRect.bottom = y + height;
  346. DWORD flags = 0;
  347. if (!Graphics::GetDirect3D9ExEnabled())
  348. {
  349. if (level == 0 && x == 0 && y == 0 && width == levelWidth && height == levelHeight && pool_ == D3DPOOL_DEFAULT)
  350. flags |= D3DLOCK_DISCARD;
  351. if (FAILED(((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, &d3dLockedRect,
  352. (flags & D3DLOCK_DISCARD) ? 0 : &d3dRect, flags)))
  353. {
  354. LOGERROR("Could not lock texture");
  355. return false;
  356. }
  357. }
  358. else
  359. {
  360. device->CreateOffscreenPlainSurface((UINT)width_, (UINT)height_, (D3DFORMAT)format_, D3DPOOL_DEFAULT, &offscreenSurface, 0);
  361. if (!offscreenSurface)
  362. {
  363. LOGERROR("TextureCube::SetData - Could not create offscreen surface for updating TextureCube");
  364. return false;
  365. }
  366. if (FAILED(offscreenSurface->LockRect(&d3dLockedRect, &d3dRect, D3DLOCK_DISCARD)))
  367. {
  368. LOGERROR("TextureCube::SetData - Could not lock offscreen surface for updating TextureCube");
  369. offscreenSurface->Release();
  370. return false;
  371. }
  372. }
  373. if (IsCompressed())
  374. {
  375. height = (height + 3) >> 2;
  376. y >>= 2;
  377. }
  378. unsigned char* src = (unsigned char*)data;
  379. unsigned rowSize = GetRowDataSize(width);
  380. // GetRowDataSize() returns CPU-side (source) data size, so need to convert for X8R8G8B8
  381. if (format_ == D3DFMT_X8R8G8B8)
  382. rowSize = rowSize / 3 * 4;
  383. // Perform conversion from RGB / RGBA as necessary
  384. switch (format_)
  385. {
  386. default:
  387. for (int i = 0; i < height; ++i)
  388. {
  389. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  390. memcpy(dest, src, rowSize);
  391. src += rowSize;
  392. }
  393. break;
  394. case D3DFMT_X8R8G8B8:
  395. for (int i = 0; i < height; ++i)
  396. {
  397. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  398. for (int j = 0; j < width; ++j)
  399. {
  400. *dest++ = src[2];
  401. *dest++ = src[1];
  402. *dest++ = src[0];
  403. *dest++ = 255;
  404. src += 3;
  405. }
  406. }
  407. break;
  408. case D3DFMT_A8R8G8B8:
  409. for (int i = 0; i < height; ++i)
  410. {
  411. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  412. for (int j = 0; j < width; ++j)
  413. {
  414. *dest++ = src[2];
  415. *dest++ = src[1];
  416. *dest++ = src[0];
  417. *dest++ = src[3];
  418. src += 4;
  419. }
  420. }
  421. break;
  422. }
  423. if (!offscreenSurface)
  424. {
  425. ((IDirect3DCubeTexture9*)object_)->UnlockRect((D3DCUBEMAP_FACES)face, level);
  426. }
  427. else
  428. {
  429. offscreenSurface->UnlockRect();
  430. IDirect3DSurface9* faceSurface = 0;
  431. if (FAILED(((IDirect3DCubeTexture9*)object_)->GetCubeMapSurface((D3DCUBEMAP_FACES)face, level, &faceSurface)))
  432. {
  433. LOGERROR("TextureCube::SetData - Could not get cubemap surface");
  434. offscreenSurface->Release();
  435. return false;
  436. }
  437. if (FAILED(device->StretchRect(offscreenSurface, NULL, faceSurface, NULL, D3DTEXF_NONE)))
  438. {
  439. LOGERROR("TextureCube::SetData - Could not stretch rect");
  440. faceSurface->Release();
  441. offscreenSurface->Release();
  442. return false;
  443. }
  444. faceSurface->Release();
  445. offscreenSurface->Release();
  446. }
  447. return true;
  448. }
  449. bool TextureCube::SetData(CubeMapFace face, Deserializer& source)
  450. {
  451. SharedPtr<Image> image(new Image(context_));
  452. if (!image->Load(source))
  453. return false;
  454. return SetData(face, image);
  455. }
  456. bool TextureCube::SetData(CubeMapFace face, SharedPtr<Image> image, bool useAlpha)
  457. {
  458. if (!image)
  459. {
  460. LOGERROR("Null image, can not load texture");
  461. return false;
  462. }
  463. unsigned memoryUse = 0;
  464. int quality = QUALITY_HIGH;
  465. Renderer* renderer = GetSubsystem<Renderer>();
  466. if (renderer)
  467. quality = renderer->GetTextureQuality();
  468. if (!image->IsCompressed())
  469. {
  470. unsigned char* levelData = image->GetData();
  471. int levelWidth = image->GetWidth();
  472. int levelHeight = image->GetHeight();
  473. unsigned components = image->GetComponents();
  474. unsigned format = 0;
  475. if (levelWidth != levelHeight)
  476. {
  477. LOGERROR("Cube texture width not equal to height");
  478. return false;
  479. }
  480. // Discard unnecessary mip levels
  481. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  482. {
  483. image = image->GetNextLevel();
  484. levelData = image->GetData();
  485. levelWidth = image->GetWidth();
  486. levelHeight = image->GetHeight();
  487. }
  488. switch (components)
  489. {
  490. case 1:
  491. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  492. break;
  493. case 2:
  494. format = Graphics::GetLuminanceAlphaFormat();
  495. break;
  496. case 3:
  497. format = Graphics::GetRGBFormat();
  498. break;
  499. case 4:
  500. format = Graphics::GetRGBAFormat();
  501. break;
  502. default:
  503. assert(false); // Should never reach here
  504. break;
  505. }
  506. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  507. if (!face)
  508. {
  509. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  510. if (IsCompressed() && requestedLevels_ > 1)
  511. requestedLevels_ = 0;
  512. SetSize(levelWidth, format);
  513. }
  514. else
  515. {
  516. if (!object_)
  517. {
  518. LOGERROR("Cube texture face 0 must be loaded first");
  519. return false;
  520. }
  521. if (levelWidth != width_ || format != format_)
  522. {
  523. LOGERROR("Cube texture face does not match size or format of face 0");
  524. return false;
  525. }
  526. }
  527. for (unsigned i = 0; i < levels_; ++i)
  528. {
  529. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  530. memoryUse += levelWidth * levelHeight * components;
  531. if (i < levels_ - 1)
  532. {
  533. image = image->GetNextLevel();
  534. levelData = image->GetData();
  535. levelWidth = image->GetWidth();
  536. levelHeight = image->GetHeight();
  537. }
  538. }
  539. }
  540. else
  541. {
  542. int width = image->GetWidth();
  543. int height = image->GetHeight();
  544. unsigned levels = image->GetNumCompressedLevels();
  545. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  546. bool needDecompress = false;
  547. if (width != height)
  548. {
  549. LOGERROR("Cube texture width not equal to height");
  550. return false;
  551. }
  552. if (!format)
  553. {
  554. format = Graphics::GetRGBAFormat();
  555. needDecompress = true;
  556. }
  557. unsigned mipsToSkip = mipsToSkip_[quality];
  558. if (mipsToSkip >= levels)
  559. mipsToSkip = levels - 1;
  560. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  561. --mipsToSkip;
  562. width /= (1 << mipsToSkip);
  563. height /= (1 << mipsToSkip);
  564. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  565. if (!face)
  566. {
  567. SetNumLevels((unsigned)Max((int)(levels - mipsToSkip), 1));
  568. SetSize(width, format);
  569. }
  570. else
  571. {
  572. if (!object_)
  573. {
  574. LOGERROR("Cube texture face 0 must be loaded first");
  575. return false;
  576. }
  577. if (width != width_ || format != format_)
  578. {
  579. LOGERROR("Cube texture face does not match size or format of face 0");
  580. return false;
  581. }
  582. }
  583. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  584. {
  585. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  586. if (!needDecompress)
  587. {
  588. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  589. memoryUse += level.rows_ * level.rowSize_;
  590. }
  591. else
  592. {
  593. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  594. level.Decompress(rgbaData);
  595. SetData(face, i, 0, 0, level.width_, level.height_, rgbaData);
  596. memoryUse += level.width_ * level.height_ * 4;
  597. delete[] rgbaData;
  598. }
  599. }
  600. }
  601. faceMemoryUse_[face] = memoryUse;
  602. unsigned totalMemoryUse = sizeof(TextureCube);
  603. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  604. totalMemoryUse += faceMemoryUse_[i];
  605. SetMemoryUse(totalMemoryUse);
  606. return true;
  607. }
  608. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  609. {
  610. if (!object_)
  611. {
  612. LOGERROR("No texture created, can not get data");
  613. return false;
  614. }
  615. if (!dest)
  616. {
  617. LOGERROR("Null destination for getting data");
  618. return false;
  619. }
  620. if (level >= levels_)
  621. {
  622. LOGERROR("Illegal mip level for getting data");
  623. return false;
  624. }
  625. if (graphics_->IsDeviceLost())
  626. {
  627. LOGWARNING("Getting texture data while device is lost");
  628. return false;
  629. }
  630. int levelWidth = GetLevelWidth(level);
  631. int levelHeight = GetLevelHeight(level);
  632. D3DLOCKED_RECT d3dLockedRect;
  633. RECT d3dRect;
  634. d3dRect.left = 0;
  635. d3dRect.top = 0;
  636. d3dRect.right = levelWidth;
  637. d3dRect.bottom = levelHeight;
  638. IDirect3DSurface9* offscreenSurface = 0;
  639. // Need to use a offscreen surface & GetRenderTargetData() for rendertargets
  640. if (renderSurfaces_[face])
  641. {
  642. if (level != 0)
  643. {
  644. LOGERROR("Can only get mip level 0 data from a rendertarget");
  645. return false;
  646. }
  647. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  648. device->CreateOffscreenPlainSurface((UINT)width_, (UINT)height_, (D3DFORMAT)format_, D3DPOOL_SYSTEMMEM, &offscreenSurface, 0);
  649. if (!offscreenSurface)
  650. {
  651. LOGERROR("Could not create surface for getting rendertarget data");
  652. return false;
  653. }
  654. device->GetRenderTargetData((IDirect3DSurface9*)renderSurfaces_[face]->GetSurface(), offscreenSurface);
  655. if (FAILED(offscreenSurface->LockRect(&d3dLockedRect, &d3dRect, D3DLOCK_READONLY)))
  656. {
  657. LOGERROR("Could not lock surface for getting rendertarget data");
  658. offscreenSurface->Release();
  659. return false;
  660. }
  661. }
  662. else
  663. {
  664. if (FAILED(
  665. ((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, &d3dLockedRect, &d3dRect, D3DLOCK_READONLY)))
  666. {
  667. LOGERROR("Could not lock texture");
  668. return false;
  669. }
  670. }
  671. int height = levelHeight;
  672. if (IsCompressed())
  673. height = (height + 3) >> 2;
  674. unsigned char* destPtr = (unsigned char*)dest;
  675. unsigned rowSize = GetRowDataSize(levelWidth);
  676. // GetRowDataSize() returns CPU-side (destination) data size, so need to convert for X8R8G8B8
  677. if (format_ == D3DFMT_X8R8G8B8)
  678. rowSize = rowSize / 3 * 4;
  679. // Perform conversion to RGB / RGBA as necessary
  680. switch (format_)
  681. {
  682. default:
  683. for (int i = 0; i < height; ++i)
  684. {
  685. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  686. memcpy(destPtr, src, rowSize);
  687. destPtr += rowSize;
  688. }
  689. break;
  690. case D3DFMT_X8R8G8B8:
  691. for (int i = 0; i < height; ++i)
  692. {
  693. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  694. for (int j = 0; j < levelWidth; ++j)
  695. {
  696. destPtr[2] = *src++;
  697. destPtr[1] = *src++;
  698. destPtr[0] = *src++;
  699. ++src;
  700. destPtr += 3;
  701. }
  702. }
  703. break;
  704. case D3DFMT_A8R8G8B8:
  705. for (int i = 0; i < height; ++i)
  706. {
  707. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  708. for (int j = 0; j < levelWidth; ++j)
  709. {
  710. destPtr[2] = *src++;
  711. destPtr[1] = *src++;
  712. destPtr[0] = *src++;
  713. destPtr[3] = *src++;
  714. destPtr += 4;
  715. }
  716. }
  717. break;
  718. }
  719. if (offscreenSurface)
  720. {
  721. offscreenSurface->UnlockRect();
  722. offscreenSurface->Release();
  723. }
  724. else
  725. ((IDirect3DCubeTexture9*)object_)->UnlockRect((D3DCUBEMAP_FACES)face, level);
  726. return true;
  727. }
  728. bool TextureCube::Create()
  729. {
  730. Release();
  731. if (!graphics_ || !width_ || !height_)
  732. return false;
  733. if (graphics_->IsDeviceLost())
  734. {
  735. LOGWARNING("Texture creation while device is lost");
  736. return true;
  737. }
  738. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  739. if (!device || FAILED(device->CreateCubeTexture(
  740. (UINT)width_,
  741. requestedLevels_,
  742. usage_,
  743. (D3DFORMAT)format_,
  744. (D3DPOOL)pool_,
  745. (IDirect3DCubeTexture9**)&object_,
  746. 0)))
  747. {
  748. LOGERROR("Could not create cube texture");
  749. return false;
  750. }
  751. levels_ = ((IDirect3DCubeTexture9*)object_)->GetLevelCount();
  752. if (usage_ & D3DUSAGE_RENDERTARGET)
  753. {
  754. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  755. {
  756. ((IDirect3DCubeTexture9*)object_)->GetCubeMapSurface((D3DCUBEMAP_FACES)i, 0,
  757. (IDirect3DSurface9**)&renderSurfaces_[i]->surface_);
  758. }
  759. }
  760. return true;
  761. }
  762. void TextureCube::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  763. {
  764. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  765. {
  766. if (renderSurfaces_[i] && renderSurfaces_[i]->GetUpdateMode() == SURFACE_UPDATEALWAYS)
  767. renderSurfaces_[i]->QueueUpdate();
  768. }
  769. }
  770. }