D3D11TextureCube.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. cache->ResetDependencies(this);
  79. String texPath, texName, texExt;
  80. SplitPath(GetName(), texPath, texName, texExt);
  81. loadParameters_ = (new XMLFile(context_));
  82. if (!loadParameters_->Load(source))
  83. {
  84. loadParameters_.Reset();
  85. return false;
  86. }
  87. loadImages_.Clear();
  88. XMLElement textureElem = loadParameters_->GetRoot();
  89. XMLElement imageElem = textureElem.GetChild("image");
  90. // Single image and multiple faces with layout
  91. if (imageElem)
  92. {
  93. String name = imageElem.GetAttribute("name");
  94. // If path is empty, add the XML file path
  95. if (GetPath(name).Empty())
  96. name = texPath + name;
  97. SharedPtr<Image> image = cache->GetTempResource<Image>(name);
  98. if (!image)
  99. return false;
  100. int faceWidth, faceHeight;
  101. loadImages_.Resize(MAX_CUBEMAP_FACES);
  102. if (image->IsCubemap())
  103. {
  104. loadImages_[FACE_POSITIVE_X] = image;
  105. loadImages_[FACE_NEGATIVE_X] = loadImages_[FACE_POSITIVE_X]->GetNextSibling();
  106. loadImages_[FACE_POSITIVE_Y] = loadImages_[FACE_NEGATIVE_X]->GetNextSibling();
  107. loadImages_[FACE_NEGATIVE_Y] = loadImages_[FACE_POSITIVE_Y]->GetNextSibling();
  108. loadImages_[FACE_POSITIVE_Z] = loadImages_[FACE_NEGATIVE_Y]->GetNextSibling();
  109. loadImages_[FACE_NEGATIVE_Z] = loadImages_[FACE_POSITIVE_Z]->GetNextSibling();
  110. }
  111. else
  112. {
  113. CubeMapLayout layout =
  114. (CubeMapLayout)GetStringListIndex(imageElem.GetAttribute("layout").CString(), cubeMapLayoutNames, CML_HORIZONTAL);
  115. switch (layout)
  116. {
  117. case CML_HORIZONTAL:
  118. faceWidth = image->GetWidth() / MAX_CUBEMAP_FACES;
  119. faceHeight = image->GetHeight();
  120. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 0, 0, faceWidth, faceHeight);
  121. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  122. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 2, 0, faceWidth, faceHeight);
  123. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 3, 0, faceWidth, faceHeight);
  124. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 4, 0, faceWidth, faceHeight);
  125. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 5, 0, faceWidth, faceHeight);
  126. break;
  127. case CML_HORIZONTALNVIDIA:
  128. faceWidth = image->GetWidth() / MAX_CUBEMAP_FACES;
  129. faceHeight = image->GetHeight();
  130. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  131. loadImages_[i] = GetTileImage(image, i, 0, faceWidth, faceHeight);
  132. break;
  133. case CML_HORIZONTALCROSS:
  134. faceWidth = image->GetWidth() / 4;
  135. faceHeight = image->GetHeight() / 3;
  136. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  137. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  138. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  139. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  140. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 3, 1, faceWidth, faceHeight);
  141. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 1, 2, faceWidth, faceHeight);
  142. break;
  143. case CML_VERTICALCROSS:
  144. faceWidth = image->GetWidth() / 3;
  145. faceHeight = image->GetHeight() / 4;
  146. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  147. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  148. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  149. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  150. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 1, 2, faceWidth, faceHeight);
  151. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 1, 3, faceWidth, faceHeight);
  152. if (loadImages_[FACE_NEGATIVE_Z])
  153. {
  154. loadImages_[FACE_NEGATIVE_Z]->FlipVertical();
  155. loadImages_[FACE_NEGATIVE_Z]->FlipHorizontal();
  156. }
  157. break;
  158. case CML_BLENDER:
  159. faceWidth = image->GetWidth() / 3;
  160. faceHeight = image->GetHeight() / 2;
  161. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 0, faceWidth, faceHeight);
  162. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  163. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 0, faceWidth, faceHeight);
  164. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  165. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  166. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  167. break;
  168. }
  169. }
  170. }
  171. // Face per image
  172. else
  173. {
  174. XMLElement faceElem = textureElem.GetChild("face");
  175. while (faceElem)
  176. {
  177. String name = faceElem.GetAttribute("name");
  178. // If path is empty, add the XML file path
  179. if (GetPath(name).Empty())
  180. name = texPath + name;
  181. loadImages_.Push(cache->GetTempResource<Image>(name));
  182. cache->StoreResourceDependency(this, name);
  183. faceElem = faceElem.GetNext("face");
  184. }
  185. }
  186. // Precalculate mip levels if async loading
  187. if (GetAsyncLoadState() == ASYNC_LOADING)
  188. {
  189. for (unsigned i = 0; i < loadImages_.Size(); ++i)
  190. {
  191. if (loadImages_[i])
  192. loadImages_[i]->PrecalculateLevels();
  193. }
  194. }
  195. return true;
  196. }
  197. bool TextureCube::EndLoad()
  198. {
  199. // In headless mode, do not actually load the texture, just return success
  200. if (!graphics_)
  201. return true;
  202. // If over the texture budget, see if materials can be freed to allow textures to be freed
  203. CheckTextureBudget(GetTypeStatic());
  204. SetParameters(loadParameters_);
  205. for (unsigned i = 0; i < loadImages_.Size() && i < MAX_CUBEMAP_FACES; ++i)
  206. SetData((CubeMapFace)i, loadImages_[i]);
  207. loadImages_.Clear();
  208. loadParameters_.Reset();
  209. return true;
  210. }
  211. void TextureCube::Release()
  212. {
  213. if (object_)
  214. {
  215. if (!graphics_)
  216. return;
  217. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  218. {
  219. if (graphics_->GetTexture(i) == this)
  220. graphics_->SetTexture(i, 0);
  221. }
  222. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  223. {
  224. if (renderSurfaces_[i])
  225. renderSurfaces_[i]->Release();
  226. }
  227. ((ID3D11Resource*)object_)->Release();
  228. object_ = 0;
  229. if (shaderResourceView_)
  230. {
  231. ((ID3D11ShaderResourceView*)shaderResourceView_)->Release();
  232. shaderResourceView_ = 0;
  233. }
  234. if (sampler_)
  235. {
  236. ((ID3D11SamplerState*)sampler_)->Release();
  237. sampler_ = 0;
  238. }
  239. }
  240. }
  241. bool TextureCube::SetSize(int size, unsigned format, TextureUsage usage)
  242. {
  243. if (size <= 0)
  244. {
  245. LOGERROR("Zero or negative cube texture size");
  246. return false;
  247. }
  248. if (usage == TEXTURE_DEPTHSTENCIL)
  249. {
  250. LOGERROR("Depth-stencil usage not supported for cube maps");
  251. return false;
  252. }
  253. // Delete the old rendersurfaces if any
  254. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  255. {
  256. renderSurfaces_[i].Reset();
  257. faceMemoryUse_[i] = 0;
  258. }
  259. usage_ = usage;
  260. if (usage_ == TEXTURE_RENDERTARGET)
  261. {
  262. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  263. renderSurfaces_[i] = new RenderSurface(this);
  264. // Nearest filtering and mipmaps disabled by default
  265. filterMode_ = FILTER_NEAREST;
  266. requestedLevels_ = 1;
  267. }
  268. if (usage_ == TEXTURE_RENDERTARGET)
  269. SubscribeToEvent(E_RENDERSURFACEUPDATE, HANDLER(TextureCube, HandleRenderSurfaceUpdate));
  270. else
  271. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  272. width_ = size;
  273. height_ = size;
  274. format_ = format;
  275. return Create();
  276. }
  277. bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data)
  278. {
  279. PROFILE(SetTextureData);
  280. if (!object_)
  281. {
  282. LOGERROR("No texture created, can not set data");
  283. return false;
  284. }
  285. if (!data)
  286. {
  287. LOGERROR("Null source for setting data");
  288. return false;
  289. }
  290. if (level >= levels_)
  291. {
  292. LOGERROR("Illegal mip level for setting data");
  293. return false;
  294. }
  295. int levelWidth = GetLevelWidth(level);
  296. int levelHeight = GetLevelHeight(level);
  297. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  298. {
  299. LOGERROR("Illegal dimensions for setting data");
  300. return false;
  301. }
  302. // If compressed, align the update region on a block
  303. if (IsCompressed())
  304. {
  305. x &= ~3;
  306. y &= ~3;
  307. width += 3;
  308. width &= 0xfffffffc;
  309. height += 3;
  310. height &= 0xfffffffc;
  311. }
  312. unsigned char* src = (unsigned char*)data;
  313. unsigned rowSize = GetRowDataSize(width);
  314. unsigned rowStart = GetRowDataSize(x);
  315. unsigned subResource = D3D11CalcSubresource(level, face, levels_);
  316. if (usage_ == TEXTURE_DYNAMIC)
  317. {
  318. if (IsCompressed())
  319. {
  320. height = (height + 3) >> 2;
  321. y >>= 2;
  322. }
  323. D3D11_MAPPED_SUBRESOURCE mappedData;
  324. mappedData.pData = 0;
  325. graphics_->GetImpl()->GetDeviceContext()->Map((ID3D11Resource*)object_, subResource, D3D11_MAP_WRITE_DISCARD, 0,
  326. &mappedData);
  327. if (mappedData.pData)
  328. {
  329. for (int row = 0; row < height; ++row)
  330. memcpy((unsigned char*)mappedData.pData + (row + y) * mappedData.RowPitch + rowStart, src + row * rowSize, rowSize);
  331. graphics_->GetImpl()->GetDeviceContext()->Unmap((ID3D11Resource*)object_, subResource);
  332. }
  333. else
  334. {
  335. LOGERROR("Failed to map texture for update");
  336. return false;
  337. }
  338. }
  339. else
  340. {
  341. D3D11_BOX destBox;
  342. destBox.left = (UINT)x;
  343. destBox.right = (UINT)(x + width);
  344. destBox.top = (UINT)y;
  345. destBox.bottom = (UINT)(y + height);
  346. destBox.front = 0;
  347. destBox.back = 1;
  348. graphics_->GetImpl()->GetDeviceContext()->UpdateSubresource((ID3D11Resource*)object_, subResource, &destBox, data,
  349. rowSize, 0);
  350. }
  351. return true;
  352. }
  353. bool TextureCube::SetData(CubeMapFace face, Deserializer& source)
  354. {
  355. SharedPtr<Image> image(new Image(context_));
  356. if (!image->Load(source))
  357. return false;
  358. return SetData(face, image);
  359. }
  360. bool TextureCube::SetData(CubeMapFace face, SharedPtr<Image> image, bool useAlpha)
  361. {
  362. if (!image)
  363. {
  364. LOGERROR("Null image, can not load texture");
  365. return false;
  366. }
  367. unsigned memoryUse = 0;
  368. int quality = QUALITY_HIGH;
  369. Renderer* renderer = GetSubsystem<Renderer>();
  370. if (renderer)
  371. quality = renderer->GetTextureQuality();
  372. if (!image->IsCompressed())
  373. {
  374. // Convert unsuitable formats to RGBA
  375. unsigned components = image->GetComponents();
  376. if ((components == 1 && !useAlpha) || components == 2 || components == 3)
  377. {
  378. image = image->ConvertToRGBA();
  379. if (!image)
  380. return false;
  381. components = image->GetComponents();
  382. }
  383. unsigned char* levelData = image->GetData();
  384. int levelWidth = image->GetWidth();
  385. int levelHeight = image->GetHeight();
  386. unsigned format = 0;
  387. if (levelWidth != levelHeight)
  388. {
  389. LOGERROR("Cube texture width not equal to height");
  390. return false;
  391. }
  392. // Discard unnecessary mip levels
  393. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  394. {
  395. image = image->GetNextLevel();
  396. levelData = image->GetData();
  397. levelWidth = image->GetWidth();
  398. levelHeight = image->GetHeight();
  399. }
  400. switch (components)
  401. {
  402. case 1:
  403. format = Graphics::GetAlphaFormat();
  404. break;
  405. case 4:
  406. format = Graphics::GetRGBAFormat();
  407. break;
  408. default: break;
  409. }
  410. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  411. if (!face)
  412. {
  413. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  414. if (IsCompressed() && requestedLevels_ > 1)
  415. requestedLevels_ = 0;
  416. SetSize(levelWidth, format);
  417. }
  418. else
  419. {
  420. if (!object_)
  421. {
  422. LOGERROR("Cube texture face 0 must be loaded first");
  423. return false;
  424. }
  425. if (levelWidth != width_ || format != format_)
  426. {
  427. LOGERROR("Cube texture face does not match size or format of face 0");
  428. return false;
  429. }
  430. }
  431. for (unsigned i = 0; i < levels_; ++i)
  432. {
  433. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  434. memoryUse += levelWidth * levelHeight * components;
  435. if (i < levels_ - 1)
  436. {
  437. image = image->GetNextLevel();
  438. levelData = image->GetData();
  439. levelWidth = image->GetWidth();
  440. levelHeight = image->GetHeight();
  441. }
  442. }
  443. }
  444. else
  445. {
  446. int width = image->GetWidth();
  447. int height = image->GetHeight();
  448. unsigned levels = image->GetNumCompressedLevels();
  449. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  450. bool needDecompress = false;
  451. if (width != height)
  452. {
  453. LOGERROR("Cube texture width not equal to height");
  454. return false;
  455. }
  456. if (!format)
  457. {
  458. format = Graphics::GetRGBAFormat();
  459. needDecompress = true;
  460. }
  461. unsigned mipsToSkip = mipsToSkip_[quality];
  462. if (mipsToSkip >= levels)
  463. mipsToSkip = levels - 1;
  464. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  465. --mipsToSkip;
  466. width /= (1 << mipsToSkip);
  467. height /= (1 << mipsToSkip);
  468. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  469. if (!face)
  470. {
  471. SetNumLevels((unsigned)Max((int)(levels - mipsToSkip), 1));
  472. SetSize(width, format);
  473. }
  474. else
  475. {
  476. if (!object_)
  477. {
  478. LOGERROR("Cube texture face 0 must be loaded first");
  479. return false;
  480. }
  481. if (width != width_ || format != format_)
  482. {
  483. LOGERROR("Cube texture face does not match size or format of face 0");
  484. return false;
  485. }
  486. }
  487. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  488. {
  489. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  490. if (!needDecompress)
  491. {
  492. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  493. memoryUse += level.rows_ * level.rowSize_;
  494. }
  495. else
  496. {
  497. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  498. level.Decompress(rgbaData);
  499. SetData(face, i, 0, 0, level.width_, level.height_, rgbaData);
  500. memoryUse += level.width_ * level.height_ * 4;
  501. delete[] rgbaData;
  502. }
  503. }
  504. }
  505. faceMemoryUse_[face] = memoryUse;
  506. unsigned totalMemoryUse = sizeof(TextureCube);
  507. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  508. totalMemoryUse += faceMemoryUse_[i];
  509. SetMemoryUse(totalMemoryUse);
  510. return true;
  511. }
  512. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  513. {
  514. if (!object_)
  515. {
  516. LOGERROR("No texture created, can not get data");
  517. return false;
  518. }
  519. if (!dest)
  520. {
  521. LOGERROR("Null destination for getting data");
  522. return false;
  523. }
  524. if (level >= levels_)
  525. {
  526. LOGERROR("Illegal mip level for getting data");
  527. return false;
  528. }
  529. int levelWidth = GetLevelWidth(level);
  530. int levelHeight = GetLevelHeight(level);
  531. D3D11_TEXTURE2D_DESC textureDesc;
  532. memset(&textureDesc, 0, sizeof textureDesc);
  533. textureDesc.Width = (UINT)levelWidth;
  534. textureDesc.Height = (UINT)levelHeight;
  535. textureDesc.MipLevels = 1;
  536. textureDesc.ArraySize = 1;
  537. textureDesc.Format = (DXGI_FORMAT)format_;
  538. textureDesc.SampleDesc.Count = 1;
  539. textureDesc.SampleDesc.Quality = 0;
  540. textureDesc.Usage = D3D11_USAGE_STAGING;
  541. textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  542. ID3D11Texture2D* stagingTexture = 0;
  543. graphics_->GetImpl()->GetDevice()->CreateTexture2D(&textureDesc, 0, &stagingTexture);
  544. if (!stagingTexture)
  545. {
  546. LOGERROR("Failed to create staging texture for GetData");
  547. return false;
  548. }
  549. unsigned srcSubResource = D3D11CalcSubresource(level, face, levels_);
  550. D3D11_BOX srcBox;
  551. srcBox.left = 0;
  552. srcBox.right = (UINT)levelWidth;
  553. srcBox.top = 0;
  554. srcBox.bottom = (UINT)levelHeight;
  555. srcBox.front = 0;
  556. srcBox.back = 1;
  557. graphics_->GetImpl()->GetDeviceContext()->CopySubresourceRegion(stagingTexture, 0, 0, 0, 0, (ID3D11Resource*)object_,
  558. srcSubResource, &srcBox);
  559. D3D11_MAPPED_SUBRESOURCE mappedData;
  560. mappedData.pData = 0;
  561. unsigned rowSize = GetRowDataSize(levelWidth);
  562. unsigned numRows = (unsigned)(IsCompressed() ? (levelHeight + 3) >> 2 : levelHeight);
  563. graphics_->GetImpl()->GetDeviceContext()->Map((ID3D11Resource*)stagingTexture, 0, D3D11_MAP_READ, 0, &mappedData);
  564. if (mappedData.pData)
  565. {
  566. for (unsigned row = 0; row < numRows; ++row)
  567. memcpy((unsigned char*)dest + row * rowSize, (unsigned char*)mappedData.pData + row * mappedData.RowPitch, rowSize);
  568. graphics_->GetImpl()->GetDeviceContext()->Unmap((ID3D11Resource*)stagingTexture, 0);
  569. stagingTexture->Release();
  570. return true;
  571. }
  572. else
  573. {
  574. LOGERROR("Failed to map staging texture for GetData");
  575. stagingTexture->Release();
  576. return false;
  577. }
  578. }
  579. bool TextureCube::Create()
  580. {
  581. Release();
  582. if (!graphics_ || !width_ || !height_)
  583. return false;
  584. levels_ = CheckMaxLevels(width_, height_, requestedLevels_);
  585. D3D11_TEXTURE2D_DESC textureDesc;
  586. memset(&textureDesc, 0, sizeof textureDesc);
  587. textureDesc.Width = (UINT)width_;
  588. textureDesc.Height = (UINT)height_;
  589. textureDesc.MipLevels = levels_;
  590. textureDesc.ArraySize = MAX_CUBEMAP_FACES;
  591. textureDesc.Format = (DXGI_FORMAT)(sRGB_ ? GetSRGBFormat(format_) : format_);
  592. textureDesc.SampleDesc.Count = 1;
  593. textureDesc.SampleDesc.Quality = 0;
  594. textureDesc.Usage = usage_ == TEXTURE_DYNAMIC ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
  595. textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  596. if (usage_ == TEXTURE_RENDERTARGET)
  597. textureDesc.BindFlags |= D3D11_BIND_RENDER_TARGET;
  598. else if (usage_ == TEXTURE_DEPTHSTENCIL)
  599. textureDesc.BindFlags |= D3D11_BIND_DEPTH_STENCIL;
  600. textureDesc.CPUAccessFlags = usage_ == TEXTURE_DYNAMIC ? D3D11_CPU_ACCESS_WRITE : 0;
  601. textureDesc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
  602. graphics_->GetImpl()->GetDevice()->CreateTexture2D(&textureDesc, 0, (ID3D11Texture2D**)&object_);
  603. if (!object_)
  604. {
  605. LOGERROR("Failed to create texture");
  606. return false;
  607. }
  608. D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
  609. memset(&resourceViewDesc, 0, sizeof resourceViewDesc);
  610. resourceViewDesc.Format = (DXGI_FORMAT)GetSRVFormat(textureDesc.Format);
  611. resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
  612. resourceViewDesc.Texture2D.MipLevels = (UINT)levels_;
  613. graphics_->GetImpl()->GetDevice()->CreateShaderResourceView((ID3D11Resource*)object_, &resourceViewDesc,
  614. (ID3D11ShaderResourceView**)&shaderResourceView_);
  615. if (!shaderResourceView_)
  616. {
  617. LOGERROR("Failed to create shader resource view for texture");
  618. return false;
  619. }
  620. if (usage_ == TEXTURE_RENDERTARGET)
  621. {
  622. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  623. {
  624. renderSurfaces_[i] = new RenderSurface(this);
  625. D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
  626. memset(&renderTargetViewDesc, 0, sizeof renderTargetViewDesc);
  627. renderTargetViewDesc.Format = textureDesc.Format;
  628. renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
  629. renderTargetViewDesc.Texture2DArray.ArraySize = 1;
  630. renderTargetViewDesc.Texture2DArray.FirstArraySlice = i;
  631. renderTargetViewDesc.Texture2DArray.MipSlice = 0;
  632. graphics_->GetImpl()->GetDevice()->CreateRenderTargetView((ID3D11Resource*)object_, &renderTargetViewDesc,
  633. (ID3D11RenderTargetView**)&renderSurfaces_[i]->renderTargetView_);
  634. if (!renderSurfaces_[i]->renderTargetView_)
  635. {
  636. LOGERROR("Failed to create rendertarget view for texture");
  637. return false;
  638. }
  639. }
  640. }
  641. return true;
  642. }
  643. void TextureCube::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  644. {
  645. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  646. {
  647. if (renderSurfaces_[i] && renderSurfaces_[i]->GetUpdateMode() == SURFACE_UPDATEALWAYS)
  648. renderSurfaces_[i]->QueueUpdate();
  649. }
  650. }
  651. }