BsD3D11Texture.cpp 22 KB

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