D3D11Texture3D.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. //
  2. // Copyright (c) 2008-2020 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/Texture3D.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. namespace Urho3D
  36. {
  37. void Texture3D::OnDeviceLost()
  38. {
  39. // No-op on Direct3D11
  40. }
  41. void Texture3D::OnDeviceReset()
  42. {
  43. // No-op on Direct3D11
  44. }
  45. void Texture3D::Release()
  46. {
  47. if (graphics_ && object_.ptr_)
  48. {
  49. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  50. {
  51. if (graphics_->GetTexture(i) == this)
  52. graphics_->SetTexture(i, nullptr);
  53. }
  54. }
  55. URHO3D_SAFE_RELEASE(object_.ptr_);
  56. URHO3D_SAFE_RELEASE(shaderResourceView_);
  57. URHO3D_SAFE_RELEASE(sampler_);
  58. }
  59. bool Texture3D::SetData(unsigned level, int x, int y, int z, int width, int height, int depth, const void* data)
  60. {
  61. URHO3D_PROFILE(SetTextureData);
  62. if (!object_.ptr_)
  63. {
  64. URHO3D_LOGERROR("No texture created, can not set data");
  65. return false;
  66. }
  67. if (!data)
  68. {
  69. URHO3D_LOGERROR("Null source for setting data");
  70. return false;
  71. }
  72. if (level >= levels_)
  73. {
  74. URHO3D_LOGERROR("Illegal mip level for setting data");
  75. return false;
  76. }
  77. int levelWidth = GetLevelWidth(level);
  78. int levelHeight = GetLevelHeight(level);
  79. int levelDepth = GetLevelDepth(level);
  80. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || z < 0 || z + depth > levelDepth || width <= 0 ||
  81. height <= 0 || depth <= 0)
  82. {
  83. URHO3D_LOGERROR("Illegal dimensions for setting data");
  84. return false;
  85. }
  86. // If compressed, align the update region on a block
  87. if (IsCompressed())
  88. {
  89. x &= ~3;
  90. y &= ~3;
  91. width += 3;
  92. width &= 0xfffffffc;
  93. height += 3;
  94. height &= 0xfffffffc;
  95. }
  96. unsigned char* src = (unsigned char*)data;
  97. unsigned rowSize = GetRowDataSize(width);
  98. unsigned rowStart = GetRowDataSize(x);
  99. unsigned subResource = D3D11CalcSubresource(level, 0, levels_);
  100. if (usage_ == TEXTURE_DYNAMIC)
  101. {
  102. if (IsCompressed())
  103. {
  104. height = (height + 3) >> 2;
  105. y >>= 2;
  106. }
  107. D3D11_MAPPED_SUBRESOURCE mappedData;
  108. mappedData.pData = nullptr;
  109. HRESULT hr = graphics_->GetImpl()->GetDeviceContext()->Map((ID3D11Resource*)object_.ptr_, subResource, D3D11_MAP_WRITE_DISCARD, 0,
  110. &mappedData);
  111. if (FAILED(hr) || !mappedData.pData)
  112. {
  113. URHO3D_LOGD3DERROR("Failed to map texture for update", hr);
  114. return false;
  115. }
  116. else
  117. {
  118. for (int page = 0; page < depth; ++page)
  119. {
  120. for (int row = 0; row < height; ++row)
  121. {
  122. memcpy((unsigned char*)mappedData.pData + (page + z) * mappedData.DepthPitch + (row + y) * mappedData.RowPitch +
  123. rowStart, src + row * rowSize, rowSize);
  124. }
  125. }
  126. graphics_->GetImpl()->GetDeviceContext()->Unmap((ID3D11Resource*)object_.ptr_, subResource);
  127. }
  128. }
  129. else
  130. {
  131. if (IsCompressed())
  132. levelHeight = (levelHeight + 3) >> 2;
  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 = (UINT)z;
  139. destBox.back = (UINT)(z + depth);
  140. graphics_->GetImpl()->GetDeviceContext()->UpdateSubresource((ID3D11Resource*)object_.ptr_, subResource, &destBox, data,
  141. rowSize, levelHeight * rowSize);
  142. }
  143. return true;
  144. }
  145. bool Texture3D::SetData(Image* image, bool useAlpha)
  146. {
  147. if (!image)
  148. {
  149. URHO3D_LOGERROR("Null image, can not load texture");
  150. return false;
  151. }
  152. // Use a shared ptr for managing the temporary mip images created during this function
  153. SharedPtr<Image> mipImage;
  154. unsigned memoryUse = sizeof(Texture3D);
  155. MaterialQuality quality = QUALITY_HIGH;
  156. Renderer* renderer = GetSubsystem<Renderer>();
  157. if (renderer)
  158. quality = renderer->GetTextureQuality();
  159. if (!image->IsCompressed())
  160. {
  161. // Convert unsuitable formats to RGBA
  162. unsigned components = image->GetComponents();
  163. if ((components == 1 && !useAlpha) || components == 2 || components == 3)
  164. {
  165. mipImage = image->ConvertToRGBA(); image = mipImage;
  166. if (!image)
  167. return false;
  168. components = image->GetComponents();
  169. }
  170. unsigned char* levelData = image->GetData();
  171. int levelWidth = image->GetWidth();
  172. int levelHeight = image->GetHeight();
  173. int levelDepth = image->GetDepth();
  174. unsigned format = 0;
  175. // Discard unnecessary mip levels
  176. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  177. {
  178. mipImage = image->GetNextLevel(); image = mipImage;
  179. levelData = image->GetData();
  180. levelWidth = image->GetWidth();
  181. levelHeight = image->GetHeight();
  182. levelDepth = image->GetDepth();
  183. }
  184. switch (components)
  185. {
  186. case 1:
  187. format = Graphics::GetAlphaFormat();
  188. break;
  189. case 4:
  190. format = Graphics::GetRGBAFormat();
  191. break;
  192. default: break;
  193. }
  194. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  195. if (IsCompressed() && requestedLevels_ > 1)
  196. requestedLevels_ = 0;
  197. SetSize(levelWidth, levelHeight, levelDepth, format);
  198. for (unsigned i = 0; i < levels_; ++i)
  199. {
  200. SetData(i, 0, 0, 0, levelWidth, levelHeight, levelDepth, levelData);
  201. memoryUse += levelWidth * levelHeight * levelDepth * components;
  202. if (i < levels_ - 1)
  203. {
  204. mipImage = image->GetNextLevel(); image = mipImage;
  205. levelData = image->GetData();
  206. levelWidth = image->GetWidth();
  207. levelHeight = image->GetHeight();
  208. levelDepth = image->GetDepth();
  209. }
  210. }
  211. }
  212. else
  213. {
  214. int width = image->GetWidth();
  215. int height = image->GetHeight();
  216. int depth = image->GetDepth();
  217. unsigned levels = image->GetNumCompressedLevels();
  218. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  219. bool needDecompress = false;
  220. if (!format)
  221. {
  222. format = Graphics::GetRGBAFormat();
  223. needDecompress = true;
  224. }
  225. unsigned mipsToSkip = mipsToSkip_[quality];
  226. if (mipsToSkip >= levels)
  227. mipsToSkip = levels - 1;
  228. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4 || depth / (1 << mipsToSkip) < 4))
  229. --mipsToSkip;
  230. width /= (1 << mipsToSkip);
  231. height /= (1 << mipsToSkip);
  232. depth /= (1 << mipsToSkip);
  233. SetNumLevels(Max((levels - mipsToSkip), 1U));
  234. SetSize(width, height, depth, format);
  235. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  236. {
  237. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  238. if (!needDecompress)
  239. {
  240. SetData(i, 0, 0, 0, level.width_, level.height_, level.depth_, level.data_);
  241. memoryUse += level.depth_ * level.rows_ * level.rowSize_;
  242. }
  243. else
  244. {
  245. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * level.depth_ * 4];
  246. level.Decompress(rgbaData);
  247. SetData(i, 0, 0, 0, level.width_, level.height_, level.depth_, rgbaData);
  248. memoryUse += level.width_ * level.height_ * level.depth_ * 4;
  249. delete[] rgbaData;
  250. }
  251. }
  252. }
  253. SetMemoryUse(memoryUse);
  254. return true;
  255. }
  256. bool Texture3D::GetData(unsigned level, void* dest) const
  257. {
  258. if (!object_.ptr_)
  259. {
  260. URHO3D_LOGERROR("No texture created, can not get data");
  261. return false;
  262. }
  263. if (!dest)
  264. {
  265. URHO3D_LOGERROR("Null destination for getting data");
  266. return false;
  267. }
  268. if (level >= levels_)
  269. {
  270. URHO3D_LOGERROR("Illegal mip level for getting data");
  271. return false;
  272. }
  273. int levelWidth = GetLevelWidth(level);
  274. int levelHeight = GetLevelHeight(level);
  275. int levelDepth = GetLevelDepth(level);
  276. D3D11_TEXTURE3D_DESC textureDesc;
  277. memset(&textureDesc, 0, sizeof textureDesc);
  278. textureDesc.Width = (UINT)levelWidth;
  279. textureDesc.Height = (UINT)levelHeight;
  280. textureDesc.Depth = (UINT)levelDepth;
  281. textureDesc.MipLevels = 1;
  282. textureDesc.Format = (DXGI_FORMAT)format_;
  283. textureDesc.Usage = D3D11_USAGE_STAGING;
  284. textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  285. ID3D11Texture3D* stagingTexture = nullptr;
  286. HRESULT hr = graphics_->GetImpl()->GetDevice()->CreateTexture3D(&textureDesc, nullptr, &stagingTexture);
  287. if (FAILED(hr))
  288. {
  289. URHO3D_LOGD3DERROR("Failed to create staging texture for GetData", hr);
  290. URHO3D_SAFE_RELEASE(stagingTexture);
  291. return false;
  292. }
  293. unsigned srcSubResource = D3D11CalcSubresource(level, 0, levels_);
  294. D3D11_BOX srcBox;
  295. srcBox.left = 0;
  296. srcBox.right = (UINT)levelWidth;
  297. srcBox.top = 0;
  298. srcBox.bottom = (UINT)levelHeight;
  299. srcBox.front = 0;
  300. srcBox.back = (UINT)levelDepth;
  301. graphics_->GetImpl()->GetDeviceContext()->CopySubresourceRegion(stagingTexture, 0, 0, 0, 0, (ID3D11Resource*)object_.ptr_,
  302. srcSubResource, &srcBox);
  303. D3D11_MAPPED_SUBRESOURCE mappedData;
  304. mappedData.pData = nullptr;
  305. unsigned rowSize = GetRowDataSize(levelWidth);
  306. unsigned numRows = (unsigned)(IsCompressed() ? (levelHeight + 3) >> 2 : levelHeight);
  307. hr = graphics_->GetImpl()->GetDeviceContext()->Map((ID3D11Resource*)stagingTexture, 0, D3D11_MAP_READ, 0, &mappedData);
  308. if (FAILED(hr) || !mappedData.pData)
  309. {
  310. URHO3D_LOGD3DERROR("Failed to map staging texture for GetData", hr);
  311. URHO3D_SAFE_RELEASE(stagingTexture);
  312. return false;
  313. }
  314. else
  315. {
  316. for (int page = 0; page < levelDepth; ++page)
  317. {
  318. for (unsigned row = 0; row < numRows; ++row)
  319. {
  320. memcpy((unsigned char*)dest + (page * numRows + row) * rowSize,
  321. (unsigned char*)mappedData.pData + page * mappedData.DepthPitch + row * mappedData.RowPitch, rowSize);
  322. }
  323. }
  324. graphics_->GetImpl()->GetDeviceContext()->Unmap((ID3D11Resource*)stagingTexture, 0);
  325. URHO3D_SAFE_RELEASE(stagingTexture);
  326. return true;
  327. }
  328. }
  329. bool Texture3D::Create()
  330. {
  331. Release();
  332. if (!graphics_ || !width_ || !height_ || !depth_)
  333. return false;
  334. levels_ = CheckMaxLevels(width_, height_, depth_, requestedLevels_);
  335. D3D11_TEXTURE3D_DESC textureDesc;
  336. memset(&textureDesc, 0, sizeof textureDesc);
  337. textureDesc.Width = (UINT)width_;
  338. textureDesc.Height = (UINT)height_;
  339. textureDesc.Depth = (UINT)depth_;
  340. textureDesc.MipLevels = usage_ != TEXTURE_DYNAMIC ? levels_ : 1;
  341. textureDesc.Format = (DXGI_FORMAT)(sRGB_ ? GetSRGBFormat(format_) : format_);
  342. textureDesc.Usage = usage_ == TEXTURE_DYNAMIC ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
  343. textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  344. textureDesc.CPUAccessFlags = usage_ == TEXTURE_DYNAMIC ? D3D11_CPU_ACCESS_WRITE : 0;
  345. HRESULT hr = graphics_->GetImpl()->GetDevice()->CreateTexture3D(&textureDesc, nullptr, (ID3D11Texture3D**)&object_.ptr_);
  346. if (FAILED(hr))
  347. {
  348. URHO3D_LOGD3DERROR("Failed to create texture", hr);
  349. URHO3D_SAFE_RELEASE(object_.ptr_);
  350. return false;
  351. }
  352. D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
  353. memset(&resourceViewDesc, 0, sizeof resourceViewDesc);
  354. resourceViewDesc.Format = (DXGI_FORMAT)GetSRVFormat(textureDesc.Format);
  355. resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
  356. resourceViewDesc.Texture3D.MipLevels = usage_ != TEXTURE_DYNAMIC ? (UINT)levels_ : 1;
  357. hr = graphics_->GetImpl()->GetDevice()->CreateShaderResourceView((ID3D11Resource*)object_.ptr_, &resourceViewDesc,
  358. (ID3D11ShaderResourceView**)&shaderResourceView_);
  359. if (FAILED(hr))
  360. {
  361. URHO3D_LOGD3DERROR("Failed to create shader resource view for texture", hr);
  362. URHO3D_SAFE_RELEASE(shaderResourceView_);
  363. return false;
  364. }
  365. return true;
  366. }
  367. }