BsD3D11Texture.cpp 25 KB

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