D3D11TextureCube.cpp 24 KB

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