D3D11Texture2D.cpp 17 KB

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