BsD3D11Texture.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsD3D11Texture.h"
  5. #include "BsD3D11Mappings.h"
  6. #include "BsD3D11Device.h"
  7. #include "BsD3D11RenderSystem.h"
  8. #include "BsD3D11TextureView.h"
  9. #include "BsCoreThread.h"
  10. #include "BsException.h"
  11. #include "BsAsyncOp.h"
  12. #include "BsRenderStats.h"
  13. #include "BsDebug.h"
  14. namespace BansheeEngine
  15. {
  16. D3D11Texture::D3D11Texture()
  17. : Texture(), m1DTex(nullptr), m2DTex(nullptr), m3DTex(nullptr),
  18. mTex(nullptr), mShaderResourceView(nullptr), mStagingBuffer(nullptr),
  19. mLockedSubresourceIdx(-1), mLockedForReading(false), mStaticBuffer(nullptr)
  20. { }
  21. D3D11Texture::~D3D11Texture()
  22. { }
  23. void D3D11Texture::initialize_internal()
  24. {
  25. THROW_IF_NOT_CORE_THREAD;
  26. switch (getTextureType())
  27. {
  28. case TEX_TYPE_1D:
  29. create1DTex();
  30. break;
  31. case TEX_TYPE_2D:
  32. case TEX_TYPE_CUBE_MAP:
  33. create2DTex();
  34. break;
  35. case TEX_TYPE_3D:
  36. create3DTex();
  37. break;
  38. default:
  39. destroy_internal();
  40. BS_EXCEPT(RenderingAPIException, "Unknown texture type");
  41. }
  42. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Texture);
  43. Texture::initialize_internal();
  44. }
  45. void D3D11Texture::destroy_internal()
  46. {
  47. SAFE_RELEASE(mTex);
  48. SAFE_RELEASE(mShaderResourceView);
  49. SAFE_RELEASE(m1DTex);
  50. SAFE_RELEASE(m2DTex);
  51. SAFE_RELEASE(m3DTex);
  52. SAFE_RELEASE(mStagingBuffer);
  53. clearBufferViews();
  54. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Texture);
  55. Texture::destroy_internal();
  56. }
  57. void D3D11Texture::copyImpl(TexturePtr& target)
  58. {
  59. D3D11Texture* other = static_cast<D3D11Texture*>(target.get());
  60. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  61. D3D11Device& device = rs->getPrimaryDevice();
  62. device.getImmediateContext()->CopyResource(other->getDX11Resource(), mTex);
  63. if (device.hasError())
  64. {
  65. String errorDescription = device.getErrorDescription();
  66. BS_EXCEPT(RenderingAPIException, "D3D11 device cannot copy resource\nError Description:" + errorDescription);
  67. }
  68. }
  69. PixelData D3D11Texture::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  70. {
  71. #if BS_PROFILING_ENABLED
  72. if (options == GBL_READ_ONLY || options == GBL_READ_WRITE)
  73. {
  74. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_Texture);
  75. }
  76. if (options == GBL_READ_WRITE || options == GBL_WRITE_ONLY || options == GBL_WRITE_ONLY_DISCARD || options == GBL_WRITE_ONLY_NO_OVERWRITE)
  77. {
  78. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_Texture);
  79. }
  80. #endif
  81. UINT32 mipWidth = mWidth >> mipLevel;
  82. UINT32 mipHeight = mHeight >> mipLevel;
  83. UINT32 mipDepth = mDepth >> mipLevel;
  84. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mFormat);
  85. D3D11_MAP flags = D3D11Mappings::getLockOptions(options);
  86. UINT32 rowPitch, slicePitch;
  87. if(flags == D3D11_MAP_READ || flags == D3D11_MAP_READ_WRITE)
  88. {
  89. UINT8* data = (UINT8*)mapstagingbuffer(flags, face, mipLevel, rowPitch, slicePitch);
  90. lockedArea.setExternalBuffer(data);
  91. lockedArea.setRowPitch(rowPitch);
  92. lockedArea.setSlicePitch(slicePitch);
  93. mLockedForReading = true;
  94. }
  95. else
  96. {
  97. if(mUsage == TU_DYNAMIC)
  98. {
  99. UINT8* data = (UINT8*)map(mTex, flags, face, mipLevel, rowPitch, slicePitch);
  100. lockedArea.setExternalBuffer(data);
  101. lockedArea.setRowPitch(rowPitch);
  102. lockedArea.setSlicePitch(slicePitch);
  103. }
  104. else
  105. lockedArea.setExternalBuffer((UINT8*)mapstaticbuffer(lockedArea, mipLevel, face));
  106. mLockedForReading = false;
  107. }
  108. return lockedArea;
  109. }
  110. void D3D11Texture::unlockImpl()
  111. {
  112. if(mLockedForReading)
  113. unmapstagingbuffer();
  114. else
  115. {
  116. if(mUsage == TU_DYNAMIC)
  117. unmap(mTex);
  118. else
  119. unmapstaticbuffer();
  120. }
  121. }
  122. void D3D11Texture::readData(PixelData& dest, UINT32 mipLevel, UINT32 face)
  123. {
  124. PixelData myData = lock(GBL_READ_ONLY, mipLevel, face);
  125. #if BS_DEBUG_MODE
  126. if(dest.getConsecutiveSize() != myData.getConsecutiveSize())
  127. {
  128. unlock();
  129. BS_EXCEPT(InternalErrorException, "Buffer sizes don't match");
  130. }
  131. #endif
  132. PixelUtil::bulkPixelConversion(myData, dest);
  133. unlock();
  134. }
  135. void D3D11Texture::writeData(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer)
  136. {
  137. if(mUsage == TU_DYNAMIC)
  138. {
  139. PixelData myData = lock(discardWholeBuffer ? GBL_WRITE_ONLY_DISCARD : GBL_WRITE_ONLY, mipLevel, face);
  140. PixelUtil::bulkPixelConversion(src, myData);
  141. unlock();
  142. }
  143. else if(mUsage == TU_STATIC)
  144. {
  145. mipLevel = Math::clamp(mipLevel, (UINT32)mipLevel, getNumMipmaps());
  146. face = Math::clamp(face, (UINT32)0, mDepth - 1);
  147. if(getTextureType() == TEX_TYPE_3D)
  148. face = 0;
  149. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  150. D3D11Device& device = rs->getPrimaryDevice();
  151. UINT subresourceIdx = D3D11CalcSubresource(mipLevel, face, getNumMipmaps()+1);
  152. UINT32 rowWidth = D3D11Mappings::getSizeInBytes(mFormat, src.getWidth());
  153. UINT32 sliceWidth = D3D11Mappings::getSizeInBytes(mFormat, src.getWidth(), src.getHeight());
  154. device.getImmediateContext()->UpdateSubresource(mTex, subresourceIdx, nullptr, src.getData(), rowWidth, sliceWidth);
  155. if (device.hasError())
  156. {
  157. String errorDescription = device.getErrorDescription();
  158. BS_EXCEPT(RenderingAPIException, "D3D11 device cannot map texture\nError Description:" + errorDescription);
  159. }
  160. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_Texture);
  161. }
  162. else
  163. {
  164. BS_EXCEPT(RenderingAPIException, "Trying to write into a buffer with unsupported usage: " + toString(mUsage));
  165. }
  166. }
  167. void D3D11Texture::create1DTex()
  168. {
  169. // We must have those defined here
  170. assert(mWidth > 0);
  171. // Determine which D3D11 pixel format we'll use
  172. HRESULT hr;
  173. DXGI_FORMAT d3dPF = D3D11Mappings::getPF(D3D11Mappings::getClosestSupportedPF(mFormat, mHwGamma), mHwGamma);
  174. if (mFormat != D3D11Mappings::getPF(d3dPF))
  175. {
  176. BS_EXCEPT(RenderingAPIException, "Provided pixel format is not supported by the driver: " + toString(mFormat));
  177. }
  178. D3D11_TEXTURE1D_DESC desc;
  179. desc.Width = static_cast<UINT32>(mWidth);
  180. desc.ArraySize = 1;
  181. desc.Format = d3dPF;
  182. desc.MiscFlags = 0;
  183. if((mUsage & TU_RENDERTARGET) != 0)
  184. {
  185. desc.Usage = D3D11_USAGE_DEFAULT;
  186. desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
  187. desc.CPUAccessFlags = 0;
  188. desc.MipLevels = 1;
  189. }
  190. else if((mUsage & TU_DEPTHSTENCIL) != 0)
  191. {
  192. desc.Usage = D3D11_USAGE_DEFAULT;
  193. desc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
  194. desc.CPUAccessFlags = 0;
  195. desc.MipLevels = 1;
  196. }
  197. else
  198. {
  199. desc.Usage = D3D11Mappings::getUsage((GpuBufferUsage)mUsage);
  200. desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  201. desc.CPUAccessFlags = D3D11Mappings::getAccessFlags((GpuBufferUsage)mUsage);
  202. // Determine total number of mipmaps including main one (d3d11 convention)
  203. UINT32 numMips = (mNumMipmaps == MIP_UNLIMITED || (1U << mNumMipmaps) > mWidth) ? 0 : mNumMipmaps + 1;
  204. desc.MipLevels = numMips;
  205. }
  206. // Create the texture
  207. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  208. D3D11Device& device = rs->getPrimaryDevice();
  209. hr = device.getD3D11Device()->CreateTexture1D(&desc, nullptr, &m1DTex);
  210. // Check result and except if failed
  211. if (FAILED(hr) || device.hasError())
  212. {
  213. destroy_internal();
  214. String errorDescription = device.getErrorDescription();
  215. BS_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
  216. }
  217. hr = m1DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)&mTex);
  218. if(FAILED(hr) || device.hasError())
  219. {
  220. destroy_internal();
  221. String errorDescription = device.getErrorDescription();
  222. BS_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
  223. }
  224. m1DTex->GetDesc(&desc);
  225. if(mNumMipmaps != (desc.MipLevels - 1))
  226. {
  227. BS_EXCEPT(RenderingAPIException, "Driver returned different number of mip maps than requested. " \
  228. "Requested: " + toString(mNumMipmaps) + ". Got: " + toString(desc.MipLevels - 1) + ".");
  229. }
  230. mDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
  231. // Create texture view
  232. if((mUsage & TU_DEPTHSTENCIL) == 0)
  233. {
  234. ZeroMemory(&mSRVDesc, sizeof(mSRVDesc));
  235. mSRVDesc.Format = desc.Format;
  236. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
  237. mSRVDesc.Texture1D.MipLevels = desc.MipLevels;
  238. hr = device.getD3D11Device()->CreateShaderResourceView(m1DTex, &mSRVDesc, &mShaderResourceView);
  239. if (FAILED(hr) || device.hasError())
  240. {
  241. String errorDescription = device.getErrorDescription();
  242. BS_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
  243. }
  244. }
  245. }
  246. void D3D11Texture::create2DTex()
  247. {
  248. // We must have those defined here
  249. assert(mWidth > 0 || mHeight > 0);
  250. // Determine which D3D11 pixel format we'll use
  251. HRESULT hr;
  252. DXGI_FORMAT d3dPF = D3D11Mappings::getPF(D3D11Mappings::getClosestSupportedPF(mFormat, mHwGamma), mHwGamma);
  253. if (mFormat != D3D11Mappings::getPF(d3dPF))
  254. {
  255. BS_EXCEPT(RenderingAPIException, "Provided pixel format is not supported by the driver: " + toString(mFormat));
  256. }
  257. D3D11_TEXTURE2D_DESC desc;
  258. desc.Width = static_cast<UINT32>(mWidth);
  259. desc.Height = static_cast<UINT32>(mHeight);
  260. desc.ArraySize = 1;
  261. desc.Format = d3dPF;
  262. desc.MiscFlags = 0;
  263. if((mUsage & TU_RENDERTARGET) != 0)
  264. {
  265. desc.Usage = D3D11_USAGE_DEFAULT;
  266. desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
  267. desc.CPUAccessFlags = 0;
  268. desc.MipLevels = 1;
  269. DXGI_SAMPLE_DESC sampleDesc;
  270. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  271. rs->determineMultisampleSettings(mMultisampleCount, mMultisampleHint, d3dPF, &sampleDesc);
  272. desc.SampleDesc = sampleDesc;
  273. if(getTextureType() == TEX_TYPE_CUBE_MAP)
  274. {
  275. BS_EXCEPT(NotImplementedException, "Cube map not yet supported as a render target."); // TODO: Will be once I add proper texture array support
  276. }
  277. }
  278. else if((mUsage & TU_DEPTHSTENCIL) != 0)
  279. {
  280. desc.Usage = D3D11_USAGE_DEFAULT;
  281. desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  282. desc.CPUAccessFlags = 0;
  283. desc.MipLevels = 1;
  284. DXGI_SAMPLE_DESC sampleDesc;
  285. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  286. rs->determineMultisampleSettings(mMultisampleCount, mMultisampleHint, d3dPF, &sampleDesc);
  287. desc.SampleDesc = sampleDesc;
  288. if(getTextureType() == TEX_TYPE_CUBE_MAP)
  289. {
  290. BS_EXCEPT(NotImplementedException, "Cube map not yet supported as a depth stencil target."); // TODO: Will be once I add proper texture array support
  291. }
  292. }
  293. else
  294. {
  295. desc.Usage = D3D11Mappings::getUsage((GpuBufferUsage)mUsage);
  296. desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  297. desc.CPUAccessFlags = D3D11Mappings::getAccessFlags((GpuBufferUsage)mUsage);
  298. // Determine total number of mipmaps including main one (d3d11 convention)
  299. UINT32 numMips = (mNumMipmaps == MIP_UNLIMITED || (1U << mNumMipmaps) > mWidth) ? 0 : mNumMipmaps + 1;
  300. desc.MipLevels = numMips;
  301. DXGI_SAMPLE_DESC sampleDesc;
  302. sampleDesc.Count = 1;
  303. sampleDesc.Quality = 0;
  304. desc.SampleDesc = sampleDesc;
  305. }
  306. if (getTextureType() == TEX_TYPE_CUBE_MAP)
  307. {
  308. desc.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;
  309. desc.ArraySize = 6;
  310. }
  311. // Create the texture
  312. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  313. D3D11Device& device = rs->getPrimaryDevice();
  314. hr = device.getD3D11Device()->CreateTexture2D(&desc, nullptr, &m2DTex);
  315. // Check result and except if failed
  316. if (FAILED(hr) || device.hasError())
  317. {
  318. destroy_internal();
  319. String errorDescription = device.getErrorDescription();
  320. BS_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
  321. }
  322. hr = m2DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)&mTex);
  323. if(FAILED(hr) || device.hasError())
  324. {
  325. destroy_internal();
  326. String errorDescription = device.getErrorDescription();
  327. BS_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
  328. }
  329. m2DTex->GetDesc(&desc);
  330. if(mNumMipmaps != (desc.MipLevels - 1))
  331. {
  332. BS_EXCEPT(RenderingAPIException, "Driver returned different number of mip maps than requested. " \
  333. "Requested: " + toString(mNumMipmaps) + ". Got: " + toString(desc.MipLevels - 1) + ".");
  334. }
  335. mDXGIFormat = desc.Format;
  336. // Create shader texture view
  337. if((mUsage & TU_DEPTHSTENCIL) == 0)
  338. {
  339. ZeroMemory(&mSRVDesc, sizeof(mSRVDesc));
  340. mSRVDesc.Format = desc.Format;
  341. if((mUsage & TU_RENDERTARGET) != 0)
  342. {
  343. if(mMultisampleCount > 0)
  344. {
  345. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;
  346. mSRVDesc.Texture2D.MostDetailedMip = 0;
  347. mSRVDesc.Texture2D.MipLevels = desc.MipLevels;
  348. }
  349. else
  350. {
  351. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  352. mSRVDesc.Texture2D.MostDetailedMip = 0;
  353. mSRVDesc.Texture2D.MipLevels = desc.MipLevels;
  354. }
  355. }
  356. else
  357. {
  358. switch(getTextureType())
  359. {
  360. case TEX_TYPE_CUBE_MAP:
  361. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
  362. mSRVDesc.TextureCube.MipLevels = desc.MipLevels;
  363. mSRVDesc.TextureCube.MostDetailedMip = 0;
  364. break;
  365. case TEX_TYPE_2D:
  366. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  367. mSRVDesc.Texture2D.MostDetailedMip = 0;
  368. mSRVDesc.Texture2D.MipLevels = desc.MipLevels;
  369. break;
  370. }
  371. }
  372. mDimension = mSRVDesc.ViewDimension;
  373. hr = device.getD3D11Device()->CreateShaderResourceView(m2DTex, &mSRVDesc, &mShaderResourceView);
  374. if (FAILED(hr) || device.hasError())
  375. {
  376. String errorDescription = device.getErrorDescription();
  377. BS_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
  378. }
  379. }
  380. else
  381. {
  382. if(mMultisampleCount > 0)
  383. mDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;
  384. else
  385. mDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  386. }
  387. }
  388. void D3D11Texture::create3DTex()
  389. {
  390. // We must have those defined here
  391. assert(mWidth > 0 && mHeight > 0 && mDepth > 0);
  392. // Determine which D3D11 pixel format we'll use
  393. HRESULT hr;
  394. DXGI_FORMAT d3dPF = D3D11Mappings::getPF(D3D11Mappings::getClosestSupportedPF(mFormat, mHwGamma), mHwGamma);
  395. if (mFormat != D3D11Mappings::getPF(d3dPF))
  396. {
  397. BS_EXCEPT(RenderingAPIException, "Provided pixel format is not supported by the driver: " + toString(mFormat));
  398. }
  399. D3D11_TEXTURE3D_DESC desc;
  400. desc.Width = static_cast<UINT32>(mWidth);
  401. desc.Height = static_cast<UINT32>(mHeight);
  402. desc.Depth = static_cast<UINT32>(mDepth);
  403. desc.Format = d3dPF;
  404. desc.MiscFlags = 0;
  405. if((mUsage & TU_RENDERTARGET) != 0)
  406. {
  407. desc.Usage = D3D11_USAGE_DEFAULT;
  408. desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
  409. desc.CPUAccessFlags = 0;
  410. desc.MipLevels = 1;
  411. }
  412. else if((mUsage & TU_DEPTHSTENCIL) != 0)
  413. {
  414. desc.Usage = D3D11_USAGE_DEFAULT;
  415. desc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
  416. desc.CPUAccessFlags = 0;
  417. desc.MipLevels = 1;
  418. }
  419. else
  420. {
  421. desc.Usage = D3D11Mappings::getUsage((GpuBufferUsage)mUsage);
  422. desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  423. desc.CPUAccessFlags = D3D11Mappings::getAccessFlags((GpuBufferUsage)mUsage);
  424. // Determine total number of mipmaps including main one (d3d11 convention)
  425. UINT numMips = (mNumMipmaps == MIP_UNLIMITED || (1U << mNumMipmaps) > std::max(std::max(mWidth, mHeight), mDepth)) ? 0 : mNumMipmaps + 1;
  426. desc.MipLevels = numMips;
  427. }
  428. // Create the texture
  429. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  430. D3D11Device& device = rs->getPrimaryDevice();
  431. hr = device.getD3D11Device()->CreateTexture3D(&desc, nullptr, &m3DTex);
  432. // Check result and except if failed
  433. if (FAILED(hr) || device.hasError())
  434. {
  435. destroy_internal();
  436. String errorDescription = device.getErrorDescription();
  437. BS_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
  438. }
  439. hr = m3DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)&mTex);
  440. if(FAILED(hr) || device.hasError())
  441. {
  442. destroy_internal();
  443. String errorDescription = device.getErrorDescription();
  444. BS_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
  445. }
  446. // Create texture view
  447. m3DTex->GetDesc(&desc);
  448. if(mNumMipmaps != (desc.MipLevels - 1))
  449. {
  450. BS_EXCEPT(RenderingAPIException, "Driver returned different number of mip maps than requested. " \
  451. "Requested: " + toString(mNumMipmaps) + ". Got: " + toString(desc.MipLevels - 1) + ".");
  452. }
  453. mDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
  454. if((mUsage & TU_DEPTHSTENCIL) == 0)
  455. {
  456. ZeroMemory(&mSRVDesc, sizeof(mSRVDesc));
  457. mSRVDesc.Format = desc.Format;
  458. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
  459. mSRVDesc.Texture3D.MostDetailedMip = 0;
  460. mSRVDesc.Texture3D.MipLevels = desc.MipLevels;
  461. hr = device.getD3D11Device()->CreateShaderResourceView(m2DTex, &mSRVDesc, &mShaderResourceView);
  462. if (FAILED(hr) || device.hasError())
  463. {
  464. String errorDescription = device.getErrorDescription();
  465. BS_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
  466. }
  467. }
  468. }
  469. void* D3D11Texture::map(ID3D11Resource* res, D3D11_MAP flags, UINT32 mipLevel, UINT32 face, UINT32& rowPitch, UINT32& slicePitch)
  470. {
  471. D3D11_MAPPED_SUBRESOURCE pMappedResource;
  472. pMappedResource.pData = nullptr;
  473. mipLevel = Math::clamp(mipLevel, (UINT32)mipLevel, getNumMipmaps());
  474. face = Math::clamp(face, (UINT32)0, mDepth - 1);
  475. if(getTextureType() == TEX_TYPE_3D)
  476. face = 0;
  477. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  478. D3D11Device& device = rs->getPrimaryDevice();
  479. mLockedSubresourceIdx = D3D11CalcSubresource(mipLevel, face, getNumMipmaps()+1);
  480. device.getImmediateContext()->Map(res, mLockedSubresourceIdx, flags, 0, &pMappedResource);
  481. if (device.hasError())
  482. {
  483. String errorDescription = device.getErrorDescription();
  484. BS_EXCEPT(RenderingAPIException, "D3D11 device cannot map texture\nError Description:" + errorDescription);
  485. }
  486. UINT32 bytesPerPixel = PixelUtil::getNumElemBytes(getFormat());
  487. rowPitch = pMappedResource.RowPitch / bytesPerPixel;
  488. slicePitch = pMappedResource.DepthPitch / bytesPerPixel;
  489. return pMappedResource.pData;
  490. }
  491. void D3D11Texture::unmap(ID3D11Resource* res)
  492. {
  493. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  494. D3D11Device& device = rs->getPrimaryDevice();
  495. device.getImmediateContext()->Unmap(res, mLockedSubresourceIdx);
  496. if (device.hasError())
  497. {
  498. String errorDescription = device.getErrorDescription();
  499. BS_EXCEPT(RenderingAPIException, "D3D11 device unmap resource\nError Description:" + errorDescription);
  500. }
  501. }
  502. void* D3D11Texture::mapstagingbuffer(D3D11_MAP flags, UINT32 mipLevel, UINT32 face, UINT32& rowPitch, UINT32& slicePitch)
  503. {
  504. if(!mStagingBuffer)
  505. createStagingBuffer();
  506. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  507. D3D11Device& device = rs->getPrimaryDevice();
  508. device.getImmediateContext()->CopyResource(mStagingBuffer, mTex);
  509. return map(mStagingBuffer, flags, face, mipLevel, rowPitch, slicePitch);
  510. }
  511. void D3D11Texture::unmapstagingbuffer()
  512. {
  513. unmap(mStagingBuffer);
  514. }
  515. void* D3D11Texture::mapstaticbuffer(PixelData lock, UINT32 mipLevel, UINT32 face)
  516. {
  517. UINT32 sizeOfImage = lock.getConsecutiveSize();
  518. mLockedSubresourceIdx = D3D11CalcSubresource(mipLevel, face, getNumMipmaps()+1);
  519. mStaticBuffer = bs_new<PixelData, PoolAlloc>(lock.getWidth(), lock.getHeight(), lock.getDepth(), lock.getFormat());
  520. mStaticBuffer->allocateInternalBuffer();
  521. return mStaticBuffer->getData();
  522. }
  523. void D3D11Texture::unmapstaticbuffer()
  524. {
  525. UINT32 rowWidth = D3D11Mappings::getSizeInBytes(mStaticBuffer->getFormat(), mStaticBuffer->getWidth());
  526. UINT32 sliceWidth = D3D11Mappings::getSizeInBytes(mStaticBuffer->getFormat(), mStaticBuffer->getWidth(), mStaticBuffer->getHeight());
  527. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  528. D3D11Device& device = rs->getPrimaryDevice();
  529. device.getImmediateContext()->UpdateSubresource(mTex, mLockedSubresourceIdx, nullptr, mStaticBuffer->getData(), rowWidth, sliceWidth);
  530. if (device.hasError())
  531. {
  532. String errorDescription = device.getErrorDescription();
  533. BS_EXCEPT(RenderingAPIException, "D3D11 device cannot map texture\nError Description:" + errorDescription);
  534. }
  535. if(mStaticBuffer != nullptr)
  536. bs_delete<PoolAlloc>(mStaticBuffer);
  537. }
  538. void D3D11Texture::createStagingBuffer()
  539. {
  540. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  541. D3D11Device& device = rs->getPrimaryDevice();
  542. switch (getTextureType())
  543. {
  544. case TEX_TYPE_1D:
  545. {
  546. D3D11_TEXTURE1D_DESC desc;
  547. m1DTex->GetDesc(&desc);
  548. desc.BindFlags = 0;
  549. desc.MiscFlags = 0;
  550. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
  551. desc.Usage = D3D11_USAGE_STAGING;
  552. device.getD3D11Device()->CreateTexture1D(&desc, NULL, (ID3D11Texture1D**)(&mStagingBuffer));
  553. }
  554. break;
  555. case TEX_TYPE_2D:
  556. case TEX_TYPE_CUBE_MAP:
  557. {
  558. D3D11_TEXTURE2D_DESC desc;
  559. m2DTex->GetDesc(&desc);
  560. desc.BindFlags = 0;
  561. desc.MiscFlags = 0;
  562. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
  563. desc.Usage = D3D11_USAGE_STAGING;
  564. device.getD3D11Device()->CreateTexture2D(&desc, NULL, (ID3D11Texture2D**)(&mStagingBuffer));
  565. }
  566. break;
  567. case TEX_TYPE_3D:
  568. {
  569. D3D11_TEXTURE3D_DESC desc;
  570. m3DTex->GetDesc(&desc);
  571. desc.BindFlags = 0;
  572. desc.MiscFlags = 0;
  573. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
  574. desc.Usage = D3D11_USAGE_STAGING;
  575. device.getD3D11Device()->CreateTexture3D(&desc, NULL, (ID3D11Texture3D**)(&mStagingBuffer));
  576. }
  577. break;
  578. }
  579. }
  580. TextureViewPtr D3D11Texture::createView()
  581. {
  582. TextureViewPtr viewPtr = bs_core_ptr<D3D11TextureView, PoolAlloc>(new (bs_alloc<D3D11TextureView, PoolAlloc>()) D3D11TextureView());
  583. viewPtr->_setThisPtr(viewPtr);
  584. return viewPtr;
  585. }
  586. }