D3D11Texture2D.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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/Texture2D.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 Atomic
  36. {
  37. void Texture2D::OnDeviceLost()
  38. {
  39. // No-op on Direct3D11
  40. }
  41. void Texture2D::OnDeviceReset()
  42. {
  43. // No-op on Direct3D11
  44. }
  45. void Texture2D::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, 0);
  53. }
  54. }
  55. if (renderSurface_)
  56. renderSurface_->Release();
  57. ATOMIC_SAFE_RELEASE(object_.ptr_);
  58. ATOMIC_SAFE_RELEASE(shaderResourceView_);
  59. ATOMIC_SAFE_RELEASE(sampler_);
  60. }
  61. bool Texture2D::SetData(unsigned level, int x, int y, int width, int height, const void* data)
  62. {
  63. ATOMIC_PROFILE(SetTextureData);
  64. if (!object_.ptr_)
  65. {
  66. ATOMIC_LOGERROR("No texture created, can not set data");
  67. return false;
  68. }
  69. if (!data)
  70. {
  71. ATOMIC_LOGERROR("Null source for setting data");
  72. return false;
  73. }
  74. if (level >= levels_)
  75. {
  76. ATOMIC_LOGERROR("Illegal mip level for setting data");
  77. return false;
  78. }
  79. int levelWidth = GetLevelWidth(level);
  80. int levelHeight = GetLevelHeight(level);
  81. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  82. {
  83. ATOMIC_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 = 0;
  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. ATOMIC_LOGD3DERROR("Failed to map texture for update", hr);
  114. return false;
  115. }
  116. else
  117. {
  118. for (int row = 0; row < height; ++row)
  119. memcpy((unsigned char*)mappedData.pData + (row + y) * mappedData.RowPitch + rowStart, src + row * rowSize, rowSize);
  120. graphics_->GetImpl()->GetDeviceContext()->Unmap((ID3D11Resource*)object_.ptr_, subResource);
  121. }
  122. }
  123. else
  124. {
  125. D3D11_BOX destBox;
  126. destBox.left = (UINT)x;
  127. destBox.right = (UINT)(x + width);
  128. destBox.top = (UINT)y;
  129. destBox.bottom = (UINT)(y + height);
  130. destBox.front = 0;
  131. destBox.back = 1;
  132. graphics_->GetImpl()->GetDeviceContext()->UpdateSubresource((ID3D11Resource*)object_.ptr_, subResource, &destBox, data,
  133. rowSize, 0);
  134. }
  135. return true;
  136. }
  137. bool Texture2D::SetData(Image* image, bool useAlpha)
  138. {
  139. if (!image)
  140. {
  141. ATOMIC_LOGERROR("Null image, can not load texture");
  142. return false;
  143. }
  144. // Use a shared ptr for managing the temporary mip images created during this function
  145. SharedPtr<Image> mipImage;
  146. unsigned memoryUse = sizeof(Texture2D);
  147. int quality = QUALITY_HIGH;
  148. Renderer* renderer = GetSubsystem<Renderer>();
  149. if (renderer)
  150. quality = renderer->GetTextureQuality();
  151. if (!image->IsCompressed())
  152. {
  153. // Convert unsuitable formats to RGBA
  154. unsigned components = image->GetComponents();
  155. if ((components == 1 && !useAlpha) || components == 2 || components == 3)
  156. {
  157. mipImage = image->ConvertToRGBA(); image = mipImage;
  158. if (!image)
  159. return false;
  160. components = image->GetComponents();
  161. }
  162. unsigned char* levelData = image->GetData();
  163. int levelWidth = image->GetWidth();
  164. int levelHeight = image->GetHeight();
  165. unsigned format = 0;
  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. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  185. if (IsCompressed() && requestedLevels_ > 1)
  186. requestedLevels_ = 0;
  187. SetSize(levelWidth, levelHeight, format);
  188. for (unsigned i = 0; i < levels_; ++i)
  189. {
  190. SetData(i, 0, 0, levelWidth, levelHeight, levelData);
  191. memoryUse += levelWidth * levelHeight * components;
  192. if (i < levels_ - 1)
  193. {
  194. mipImage = image->GetNextLevel(); image = mipImage;
  195. levelData = image->GetData();
  196. levelWidth = image->GetWidth();
  197. levelHeight = image->GetHeight();
  198. }
  199. }
  200. }
  201. else
  202. {
  203. int width = image->GetWidth();
  204. int height = image->GetHeight();
  205. unsigned levels = image->GetNumCompressedLevels();
  206. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  207. bool needDecompress = false;
  208. if (!format)
  209. {
  210. format = Graphics::GetRGBAFormat();
  211. needDecompress = true;
  212. }
  213. unsigned mipsToSkip = mipsToSkip_[quality];
  214. if (mipsToSkip >= levels)
  215. mipsToSkip = levels - 1;
  216. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  217. --mipsToSkip;
  218. width /= (1 << mipsToSkip);
  219. height /= (1 << mipsToSkip);
  220. SetNumLevels(Max((levels - mipsToSkip), 1U));
  221. SetSize(width, height, format);
  222. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  223. {
  224. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  225. if (!needDecompress)
  226. {
  227. SetData(i, 0, 0, level.width_, level.height_, level.data_);
  228. memoryUse += level.rows_ * level.rowSize_;
  229. }
  230. else
  231. {
  232. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  233. level.Decompress(rgbaData);
  234. SetData(i, 0, 0, level.width_, level.height_, rgbaData);
  235. memoryUse += level.width_ * level.height_ * 4;
  236. delete[] rgbaData;
  237. }
  238. }
  239. }
  240. SetMemoryUse(memoryUse);
  241. return true;
  242. }
  243. bool Texture2D::GetData(unsigned level, void* dest) const
  244. {
  245. if (!object_.ptr_)
  246. {
  247. ATOMIC_LOGERROR("No texture created, can not get data");
  248. return false;
  249. }
  250. if (!dest)
  251. {
  252. ATOMIC_LOGERROR("Null destination for getting data");
  253. return false;
  254. }
  255. if (level >= levels_)
  256. {
  257. ATOMIC_LOGERROR("Illegal mip level for getting data");
  258. return false;
  259. }
  260. int levelWidth = GetLevelWidth(level);
  261. int levelHeight = GetLevelHeight(level);
  262. D3D11_TEXTURE2D_DESC textureDesc;
  263. memset(&textureDesc, 0, sizeof textureDesc);
  264. textureDesc.Width = (UINT)levelWidth;
  265. textureDesc.Height = (UINT)levelHeight;
  266. textureDesc.MipLevels = 1;
  267. textureDesc.ArraySize = 1;
  268. textureDesc.Format = (DXGI_FORMAT)format_;
  269. textureDesc.SampleDesc.Count = 1;
  270. textureDesc.SampleDesc.Quality = 0;
  271. textureDesc.Usage = D3D11_USAGE_STAGING;
  272. textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  273. ID3D11Texture2D* stagingTexture = 0;
  274. HRESULT hr = graphics_->GetImpl()->GetDevice()->CreateTexture2D(&textureDesc, 0, &stagingTexture);
  275. if (FAILED(hr))
  276. {
  277. ATOMIC_SAFE_RELEASE(stagingTexture);
  278. ATOMIC_LOGD3DERROR("Failed to create staging texture for GetData", hr);
  279. return false;
  280. }
  281. unsigned srcSubResource = D3D11CalcSubresource(level, 0, levels_);
  282. D3D11_BOX srcBox;
  283. srcBox.left = 0;
  284. srcBox.right = (UINT)levelWidth;
  285. srcBox.top = 0;
  286. srcBox.bottom = (UINT)levelHeight;
  287. srcBox.front = 0;
  288. srcBox.back = 1;
  289. graphics_->GetImpl()->GetDeviceContext()->CopySubresourceRegion(stagingTexture, 0, 0, 0, 0, (ID3D11Resource*)object_.ptr_,
  290. srcSubResource, &srcBox);
  291. D3D11_MAPPED_SUBRESOURCE mappedData;
  292. mappedData.pData = 0;
  293. unsigned rowSize = GetRowDataSize(levelWidth);
  294. unsigned numRows = (unsigned)(IsCompressed() ? (levelHeight + 3) >> 2 : levelHeight);
  295. hr = graphics_->GetImpl()->GetDeviceContext()->Map((ID3D11Resource*)stagingTexture, 0, D3D11_MAP_READ, 0, &mappedData);
  296. if (FAILED(hr) || !mappedData.pData)
  297. {
  298. ATOMIC_LOGD3DERROR("Failed to map staging texture for GetData", hr);
  299. stagingTexture->Release();
  300. return false;
  301. }
  302. else
  303. {
  304. for (unsigned row = 0; row < numRows; ++row)
  305. memcpy((unsigned char*)dest + row * rowSize, (unsigned char*)mappedData.pData + row * mappedData.RowPitch, rowSize);
  306. graphics_->GetImpl()->GetDeviceContext()->Unmap((ID3D11Resource*)stagingTexture, 0);
  307. stagingTexture->Release();
  308. return true;
  309. }
  310. }
  311. bool Texture2D::Create()
  312. {
  313. Release();
  314. if (!graphics_ || !width_ || !height_)
  315. return false;
  316. levels_ = CheckMaxLevels(width_, height_, requestedLevels_);
  317. D3D11_TEXTURE2D_DESC textureDesc;
  318. memset(&textureDesc, 0, sizeof textureDesc);
  319. textureDesc.Width = (UINT)width_;
  320. textureDesc.Height = (UINT)height_;
  321. textureDesc.MipLevels = levels_;
  322. textureDesc.ArraySize = 1;
  323. textureDesc.Format = (DXGI_FORMAT)(sRGB_ ? GetSRGBFormat(format_) : format_);
  324. textureDesc.SampleDesc.Count = 1;
  325. textureDesc.SampleDesc.Quality = 0;
  326. textureDesc.Usage = usage_ == TEXTURE_DYNAMIC ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
  327. textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  328. if (usage_ == TEXTURE_RENDERTARGET)
  329. textureDesc.BindFlags |= D3D11_BIND_RENDER_TARGET;
  330. else if (usage_ == TEXTURE_DEPTHSTENCIL)
  331. textureDesc.BindFlags |= D3D11_BIND_DEPTH_STENCIL;
  332. textureDesc.CPUAccessFlags = usage_ == TEXTURE_DYNAMIC ? D3D11_CPU_ACCESS_WRITE : 0;
  333. HRESULT hr = graphics_->GetImpl()->GetDevice()->CreateTexture2D(&textureDesc, 0, (ID3D11Texture2D**)&object_);
  334. if (FAILED(hr))
  335. {
  336. ATOMIC_SAFE_RELEASE(object_.ptr_);
  337. ATOMIC_LOGD3DERROR("Failed to create texture", hr);
  338. return false;
  339. }
  340. D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
  341. memset(&resourceViewDesc, 0, sizeof resourceViewDesc);
  342. resourceViewDesc.Format = (DXGI_FORMAT)GetSRVFormat(textureDesc.Format);
  343. resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  344. resourceViewDesc.Texture2D.MipLevels = (UINT)levels_;
  345. hr = graphics_->GetImpl()->GetDevice()->CreateShaderResourceView((ID3D11Resource*)object_.ptr_, &resourceViewDesc,
  346. (ID3D11ShaderResourceView**)&shaderResourceView_);
  347. if (FAILED(hr))
  348. {
  349. ATOMIC_SAFE_RELEASE(shaderResourceView_);
  350. ATOMIC_LOGD3DERROR("Failed to create shader resource view for texture", hr);
  351. return false;
  352. }
  353. if (usage_ == TEXTURE_RENDERTARGET)
  354. {
  355. D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
  356. memset(&renderTargetViewDesc, 0, sizeof renderTargetViewDesc);
  357. renderTargetViewDesc.Format = textureDesc.Format;
  358. renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
  359. hr = graphics_->GetImpl()->GetDevice()->CreateRenderTargetView((ID3D11Resource*)object_.ptr_, &renderTargetViewDesc,
  360. (ID3D11RenderTargetView**)&renderSurface_->renderTargetView_);
  361. if (FAILED(hr))
  362. {
  363. ATOMIC_SAFE_RELEASE(renderSurface_->renderTargetView_);
  364. ATOMIC_LOGD3DERROR("Failed to create rendertarget view for texture", hr);
  365. return false;
  366. }
  367. }
  368. else if (usage_ == TEXTURE_DEPTHSTENCIL)
  369. {
  370. D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
  371. memset(&depthStencilViewDesc, 0, sizeof depthStencilViewDesc);
  372. depthStencilViewDesc.Format = (DXGI_FORMAT)GetDSVFormat(textureDesc.Format);
  373. depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  374. hr = graphics_->GetImpl()->GetDevice()->CreateDepthStencilView((ID3D11Resource*)object_.ptr_, &depthStencilViewDesc,
  375. (ID3D11DepthStencilView**)&renderSurface_->renderTargetView_);
  376. if (FAILED(hr))
  377. {
  378. ATOMIC_SAFE_RELEASE(renderSurface_->renderTargetView_);
  379. ATOMIC_LOGD3DERROR("Failed to create depth-stencil view for texture", hr);
  380. return false;
  381. }
  382. // Create also a read-only version of the view for simultaneous depth testing and sampling in shader
  383. depthStencilViewDesc.Flags = D3D11_DSV_READ_ONLY_DEPTH;
  384. hr = graphics_->GetImpl()->GetDevice()->CreateDepthStencilView((ID3D11Resource*)object_.ptr_, &depthStencilViewDesc,
  385. (ID3D11DepthStencilView**)&renderSurface_->readOnlyView_);
  386. if (FAILED(hr))
  387. {
  388. ATOMIC_SAFE_RELEASE(renderSurface_->readOnlyView_);
  389. ATOMIC_LOGD3DERROR("Failed to create read-only depth-stencil view for texture", hr);
  390. }
  391. }
  392. return true;
  393. }
  394. }