D3D11TextureCube.cpp 18 KB

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