D3D11Texture3D.cpp 13 KB

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