BsD3D11Texture.cpp 25 KB

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