D3D11TextureCube.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. //
  2. // Copyright (c) 2008-2016 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. void TextureCube::OnDeviceLost()
  41. {
  42. // No-op on Direct3D11
  43. }
  44. void TextureCube::OnDeviceReset()
  45. {
  46. // No-op on Direct3D11
  47. }
  48. void TextureCube::Release()
  49. {
  50. if (graphics_)
  51. {
  52. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  53. {
  54. if (graphics_->GetTexture(i) == this)
  55. graphics_->SetTexture(i, 0);
  56. }
  57. }
  58. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  59. {
  60. if (renderSurfaces_[i])
  61. renderSurfaces_[i]->Release();
  62. }
  63. ATOMIC_SAFE_RELEASE(object_.ptr_);
  64. ATOMIC_SAFE_RELEASE(shaderResourceView_);
  65. ATOMIC_SAFE_RELEASE(sampler_);
  66. }
  67. bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data)
  68. {
  69. ATOMIC_PROFILE(SetTextureData);
  70. if (!object_.ptr_)
  71. {
  72. ATOMIC_LOGERROR("No texture created, can not set data");
  73. return false;
  74. }
  75. if (!data)
  76. {
  77. ATOMIC_LOGERROR("Null source for setting data");
  78. return false;
  79. }
  80. if (level >= levels_)
  81. {
  82. ATOMIC_LOGERROR("Illegal mip level for setting data");
  83. return false;
  84. }
  85. int levelWidth = GetLevelWidth(level);
  86. int levelHeight = GetLevelHeight(level);
  87. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  88. {
  89. ATOMIC_LOGERROR("Illegal dimensions for setting data");
  90. return false;
  91. }
  92. // If compressed, align the update region on a block
  93. if (IsCompressed())
  94. {
  95. x &= ~3;
  96. y &= ~3;
  97. width += 3;
  98. width &= 0xfffffffc;
  99. height += 3;
  100. height &= 0xfffffffc;
  101. }
  102. unsigned char* src = (unsigned char*)data;
  103. unsigned rowSize = GetRowDataSize(width);
  104. unsigned rowStart = GetRowDataSize(x);
  105. unsigned subResource = D3D11CalcSubresource(level, face, levels_);
  106. if (usage_ == TEXTURE_DYNAMIC)
  107. {
  108. if (IsCompressed())
  109. {
  110. height = (height + 3) >> 2;
  111. y >>= 2;
  112. }
  113. D3D11_MAPPED_SUBRESOURCE mappedData;
  114. mappedData.pData = 0;
  115. HRESULT hr = graphics_->GetImpl()->GetDeviceContext()->Map((ID3D11Resource*)object_.ptr_, subResource, D3D11_MAP_WRITE_DISCARD, 0,
  116. &mappedData);
  117. if (FAILED(hr) || !mappedData.pData)
  118. {
  119. ATOMIC_LOGD3DERROR("Failed to map texture for update", hr);
  120. return false;
  121. }
  122. else
  123. {
  124. for (int row = 0; row < height; ++row)
  125. memcpy((unsigned char*)mappedData.pData + (row + y) * mappedData.RowPitch + rowStart, src + row * rowSize, rowSize);
  126. graphics_->GetImpl()->GetDeviceContext()->Unmap((ID3D11Resource*)object_.ptr_, subResource);
  127. }
  128. }
  129. else
  130. {
  131. D3D11_BOX destBox;
  132. destBox.left = (UINT)x;
  133. destBox.right = (UINT)(x + width);
  134. destBox.top = (UINT)y;
  135. destBox.bottom = (UINT)(y + height);
  136. destBox.front = 0;
  137. destBox.back = 1;
  138. graphics_->GetImpl()->GetDeviceContext()->UpdateSubresource((ID3D11Resource*)object_.ptr_, subResource, &destBox, data,
  139. rowSize, 0);
  140. }
  141. return true;
  142. }
  143. bool TextureCube::SetData(CubeMapFace face, Deserializer& source)
  144. {
  145. SharedPtr<Image> image(new Image(context_));
  146. if (!image->Load(source))
  147. return false;
  148. return SetData(face, image);
  149. }
  150. bool TextureCube::SetData(CubeMapFace face, Image* image, bool useAlpha)
  151. {
  152. if (!image)
  153. {
  154. ATOMIC_LOGERROR("Null image, can not load texture");
  155. return false;
  156. }
  157. // Use a shared ptr for managing the temporary mip images created during this function
  158. SharedPtr<Image> mipImage;
  159. unsigned memoryUse = 0;
  160. int quality = QUALITY_HIGH;
  161. Renderer* renderer = GetSubsystem<Renderer>();
  162. if (renderer)
  163. quality = renderer->GetTextureQuality();
  164. if (!image->IsCompressed())
  165. {
  166. // Convert unsuitable formats to RGBA
  167. unsigned components = image->GetComponents();
  168. if ((components == 1 && !useAlpha) || components == 2 || components == 3)
  169. {
  170. mipImage = image->ConvertToRGBA(); image = mipImage;
  171. if (!image)
  172. return false;
  173. components = image->GetComponents();
  174. }
  175. unsigned char* levelData = image->GetData();
  176. int levelWidth = image->GetWidth();
  177. int levelHeight = image->GetHeight();
  178. unsigned format = 0;
  179. if (levelWidth != levelHeight)
  180. {
  181. ATOMIC_LOGERROR("Cube texture width not equal to height");
  182. return false;
  183. }
  184. // Discard unnecessary mip levels
  185. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  186. {
  187. mipImage = image->GetNextLevel(); image = mipImage;
  188. levelData = image->GetData();
  189. levelWidth = image->GetWidth();
  190. levelHeight = image->GetHeight();
  191. }
  192. switch (components)
  193. {
  194. case 1:
  195. format = Graphics::GetAlphaFormat();
  196. break;
  197. case 4:
  198. format = Graphics::GetRGBAFormat();
  199. break;
  200. default: break;
  201. }
  202. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  203. if (!face)
  204. {
  205. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  206. if (IsCompressed() && requestedLevels_ > 1)
  207. requestedLevels_ = 0;
  208. SetSize(levelWidth, format);
  209. }
  210. else
  211. {
  212. if (!object_.ptr_)
  213. {
  214. ATOMIC_LOGERROR("Cube texture face 0 must be loaded first");
  215. return false;
  216. }
  217. if (levelWidth != width_ || format != format_)
  218. {
  219. ATOMIC_LOGERROR("Cube texture face does not match size or format of face 0");
  220. return false;
  221. }
  222. }
  223. for (unsigned i = 0; i < levels_; ++i)
  224. {
  225. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  226. memoryUse += levelWidth * levelHeight * components;
  227. if (i < levels_ - 1)
  228. {
  229. mipImage = image->GetNextLevel(); image = mipImage;
  230. levelData = image->GetData();
  231. levelWidth = image->GetWidth();
  232. levelHeight = image->GetHeight();
  233. }
  234. }
  235. }
  236. else
  237. {
  238. int width = image->GetWidth();
  239. int height = image->GetHeight();
  240. unsigned levels = image->GetNumCompressedLevels();
  241. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  242. bool needDecompress = false;
  243. if (width != height)
  244. {
  245. ATOMIC_LOGERROR("Cube texture width not equal to height");
  246. return false;
  247. }
  248. if (!format)
  249. {
  250. format = Graphics::GetRGBAFormat();
  251. needDecompress = true;
  252. }
  253. unsigned mipsToSkip = mipsToSkip_[quality];
  254. if (mipsToSkip >= levels)
  255. mipsToSkip = levels - 1;
  256. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  257. --mipsToSkip;
  258. width /= (1 << mipsToSkip);
  259. height /= (1 << mipsToSkip);
  260. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  261. if (!face)
  262. {
  263. SetNumLevels(Max((levels - mipsToSkip), 1U));
  264. SetSize(width, format);
  265. }
  266. else
  267. {
  268. if (!object_.ptr_)
  269. {
  270. ATOMIC_LOGERROR("Cube texture face 0 must be loaded first");
  271. return false;
  272. }
  273. if (width != width_ || format != format_)
  274. {
  275. ATOMIC_LOGERROR("Cube texture face does not match size or format of face 0");
  276. return false;
  277. }
  278. }
  279. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  280. {
  281. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  282. if (!needDecompress)
  283. {
  284. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  285. memoryUse += level.rows_ * level.rowSize_;
  286. }
  287. else
  288. {
  289. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  290. level.Decompress(rgbaData);
  291. SetData(face, i, 0, 0, level.width_, level.height_, rgbaData);
  292. memoryUse += level.width_ * level.height_ * 4;
  293. delete[] rgbaData;
  294. }
  295. }
  296. }
  297. faceMemoryUse_[face] = memoryUse;
  298. unsigned totalMemoryUse = sizeof(TextureCube);
  299. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  300. totalMemoryUse += faceMemoryUse_[i];
  301. SetMemoryUse(totalMemoryUse);
  302. return true;
  303. }
  304. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  305. {
  306. if (!object_.ptr_)
  307. {
  308. ATOMIC_LOGERROR("No texture created, can not get data");
  309. return false;
  310. }
  311. if (!dest)
  312. {
  313. ATOMIC_LOGERROR("Null destination for getting data");
  314. return false;
  315. }
  316. if (level >= levels_)
  317. {
  318. ATOMIC_LOGERROR("Illegal mip level for getting data");
  319. return false;
  320. }
  321. int levelWidth = GetLevelWidth(level);
  322. int levelHeight = GetLevelHeight(level);
  323. D3D11_TEXTURE2D_DESC textureDesc;
  324. memset(&textureDesc, 0, sizeof textureDesc);
  325. textureDesc.Width = (UINT)levelWidth;
  326. textureDesc.Height = (UINT)levelHeight;
  327. textureDesc.MipLevels = 1;
  328. textureDesc.ArraySize = 1;
  329. textureDesc.Format = (DXGI_FORMAT)format_;
  330. textureDesc.SampleDesc.Count = 1;
  331. textureDesc.SampleDesc.Quality = 0;
  332. textureDesc.Usage = D3D11_USAGE_STAGING;
  333. textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  334. ID3D11Texture2D* stagingTexture = 0;
  335. HRESULT hr = graphics_->GetImpl()->GetDevice()->CreateTexture2D(&textureDesc, 0, &stagingTexture);
  336. if (FAILED(hr))
  337. {
  338. ATOMIC_SAFE_RELEASE(stagingTexture);
  339. ATOMIC_LOGD3DERROR("Failed to create staging texture for GetData", hr);
  340. return false;
  341. }
  342. unsigned srcSubResource = D3D11CalcSubresource(level, face, levels_);
  343. D3D11_BOX srcBox;
  344. srcBox.left = 0;
  345. srcBox.right = (UINT)levelWidth;
  346. srcBox.top = 0;
  347. srcBox.bottom = (UINT)levelHeight;
  348. srcBox.front = 0;
  349. srcBox.back = 1;
  350. graphics_->GetImpl()->GetDeviceContext()->CopySubresourceRegion(stagingTexture, 0, 0, 0, 0, (ID3D11Resource*)object_.ptr_,
  351. srcSubResource, &srcBox);
  352. D3D11_MAPPED_SUBRESOURCE mappedData;
  353. mappedData.pData = 0;
  354. unsigned rowSize = GetRowDataSize(levelWidth);
  355. unsigned numRows = (unsigned)(IsCompressed() ? (levelHeight + 3) >> 2 : levelHeight);
  356. hr = graphics_->GetImpl()->GetDeviceContext()->Map((ID3D11Resource*)stagingTexture, 0, D3D11_MAP_READ, 0, &mappedData);
  357. if (FAILED(hr) || !mappedData.pData)
  358. {
  359. ATOMIC_LOGD3DERROR("Failed to map staging texture for GetData", hr);
  360. stagingTexture->Release();
  361. return false;
  362. }
  363. else
  364. {
  365. for (unsigned row = 0; row < numRows; ++row)
  366. memcpy((unsigned char*)dest + row * rowSize, (unsigned char*)mappedData.pData + row * mappedData.RowPitch, rowSize);
  367. graphics_->GetImpl()->GetDeviceContext()->Unmap((ID3D11Resource*)stagingTexture, 0);
  368. stagingTexture->Release();
  369. return true;
  370. }
  371. }
  372. bool TextureCube::Create()
  373. {
  374. Release();
  375. if (!graphics_ || !width_ || !height_)
  376. return false;
  377. levels_ = CheckMaxLevels(width_, height_, requestedLevels_);
  378. D3D11_TEXTURE2D_DESC textureDesc;
  379. memset(&textureDesc, 0, sizeof textureDesc);
  380. textureDesc.Width = (UINT)width_;
  381. textureDesc.Height = (UINT)height_;
  382. textureDesc.MipLevels = levels_;
  383. textureDesc.ArraySize = MAX_CUBEMAP_FACES;
  384. textureDesc.Format = (DXGI_FORMAT)(sRGB_ ? GetSRGBFormat(format_) : format_);
  385. textureDesc.SampleDesc.Count = 1;
  386. textureDesc.SampleDesc.Quality = 0;
  387. textureDesc.Usage = usage_ == TEXTURE_DYNAMIC ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
  388. textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  389. if (usage_ == TEXTURE_RENDERTARGET)
  390. textureDesc.BindFlags |= D3D11_BIND_RENDER_TARGET;
  391. else if (usage_ == TEXTURE_DEPTHSTENCIL)
  392. textureDesc.BindFlags |= D3D11_BIND_DEPTH_STENCIL;
  393. textureDesc.CPUAccessFlags = usage_ == TEXTURE_DYNAMIC ? D3D11_CPU_ACCESS_WRITE : 0;
  394. textureDesc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
  395. HRESULT hr = graphics_->GetImpl()->GetDevice()->CreateTexture2D(&textureDesc, 0, (ID3D11Texture2D**)&object_.ptr_);
  396. if (FAILED(hr))
  397. {
  398. ATOMIC_SAFE_RELEASE(object_.ptr_);
  399. ATOMIC_LOGD3DERROR("Failed to create texture", hr);
  400. return false;
  401. }
  402. D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
  403. memset(&resourceViewDesc, 0, sizeof resourceViewDesc);
  404. resourceViewDesc.Format = (DXGI_FORMAT)GetSRVFormat(textureDesc.Format);
  405. resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
  406. resourceViewDesc.Texture2D.MipLevels = (UINT)levels_;
  407. hr = graphics_->GetImpl()->GetDevice()->CreateShaderResourceView((ID3D11Resource*)object_.ptr_, &resourceViewDesc,
  408. (ID3D11ShaderResourceView**)&shaderResourceView_);
  409. if (FAILED(hr))
  410. {
  411. ATOMIC_SAFE_RELEASE(shaderResourceView_);
  412. ATOMIC_LOGD3DERROR("Failed to create shader resource view for texture", hr);
  413. return false;
  414. }
  415. if (usage_ == TEXTURE_RENDERTARGET)
  416. {
  417. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  418. {
  419. D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
  420. memset(&renderTargetViewDesc, 0, sizeof renderTargetViewDesc);
  421. renderTargetViewDesc.Format = textureDesc.Format;
  422. renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
  423. renderTargetViewDesc.Texture2DArray.ArraySize = 1;
  424. renderTargetViewDesc.Texture2DArray.FirstArraySlice = i;
  425. renderTargetViewDesc.Texture2DArray.MipSlice = 0;
  426. hr = graphics_->GetImpl()->GetDevice()->CreateRenderTargetView((ID3D11Resource*)object_.ptr_, &renderTargetViewDesc,
  427. (ID3D11RenderTargetView**)&renderSurfaces_[i]->renderTargetView_);
  428. if (FAILED(hr))
  429. {
  430. ATOMIC_SAFE_RELEASE(renderSurfaces_[i]->renderTargetView_);
  431. ATOMIC_LOGD3DERROR("Failed to create rendertarget view for texture", hr);
  432. return false;
  433. }
  434. }
  435. }
  436. return true;
  437. }
  438. }