BsD3D11Texture.cpp 21 KB

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