D3D11TextureCube.cpp 24 KB

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