BsD3D11Texture.cpp 23 KB

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