D3D11Texture2DArray.cpp 17 KB

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