BsD3D11Texture.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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)
  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_STATIC || mUsage == TU_RENDERTARGET || mUsage == TU_DEPTHSTENCIL)
  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. // Create the texture
  218. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  219. D3D11Device& device = rs->getPrimaryDevice();
  220. hr = device.getD3D11Device()->CreateTexture1D(&desc, nullptr, &m1DTex);
  221. // Check result and except if failed
  222. if (FAILED(hr) || device.hasError())
  223. {
  224. destroy_internal();
  225. String errorDescription = device.getErrorDescription();
  226. BS_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
  227. }
  228. hr = m1DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)&mTex);
  229. if(FAILED(hr) || device.hasError())
  230. {
  231. destroy_internal();
  232. String errorDescription = device.getErrorDescription();
  233. BS_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
  234. }
  235. m1DTex->GetDesc(&desc);
  236. if(mNumMipmaps != (desc.MipLevels - 1))
  237. {
  238. BS_EXCEPT(RenderingAPIException, "Driver returned different number of mip maps than requested. " \
  239. "Requested: " + toString(mNumMipmaps) + ". Got: " + toString(desc.MipLevels - 1) + ".");
  240. }
  241. mDXGIFormat = desc.Format;
  242. mDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
  243. // Create texture view
  244. if((mUsage & TU_DEPTHSTENCIL) == 0)
  245. {
  246. ZeroMemory(&mSRVDesc, sizeof(mSRVDesc));
  247. mSRVDesc.Format = desc.Format;
  248. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
  249. mSRVDesc.Texture1D.MipLevels = desc.MipLevels;
  250. hr = device.getD3D11Device()->CreateShaderResourceView(m1DTex, &mSRVDesc, &mShaderResourceView);
  251. if (FAILED(hr) || device.hasError())
  252. {
  253. String errorDescription = device.getErrorDescription();
  254. BS_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
  255. }
  256. }
  257. }
  258. void D3D11Texture::create2DTex()
  259. {
  260. // We must have those defined here
  261. assert(mWidth > 0 || mHeight > 0);
  262. // Determine which D3D11 pixel format we'll use
  263. HRESULT hr;
  264. DXGI_FORMAT d3dPF = D3D11Mappings::getPF(D3D11Mappings::getClosestSupportedPF(mFormat, mHwGamma), mHwGamma);
  265. if (mFormat != D3D11Mappings::getPF(d3dPF))
  266. {
  267. BS_EXCEPT(RenderingAPIException, "Provided pixel format is not supported by the driver: " + toString(mFormat));
  268. }
  269. D3D11_TEXTURE2D_DESC desc;
  270. desc.Width = static_cast<UINT32>(mWidth);
  271. desc.Height = static_cast<UINT32>(mHeight);
  272. desc.ArraySize = 1;
  273. desc.Format = d3dPF;
  274. desc.MiscFlags = 0;
  275. if((mUsage & TU_RENDERTARGET) != 0)
  276. {
  277. desc.Usage = D3D11_USAGE_DEFAULT;
  278. desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
  279. desc.CPUAccessFlags = 0;
  280. desc.MipLevels = 1;
  281. DXGI_SAMPLE_DESC sampleDesc;
  282. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  283. rs->determineMultisampleSettings(mMultisampleCount, mMultisampleHint, d3dPF, &sampleDesc);
  284. desc.SampleDesc = sampleDesc;
  285. if(getTextureType() == TEX_TYPE_CUBE_MAP)
  286. {
  287. BS_EXCEPT(NotImplementedException, "Cube map not yet supported as a render target."); // TODO: Will be once I add proper texture array support
  288. }
  289. }
  290. else if((mUsage & TU_DEPTHSTENCIL) != 0)
  291. {
  292. desc.Usage = D3D11_USAGE_DEFAULT;
  293. desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  294. desc.CPUAccessFlags = 0;
  295. desc.MipLevels = 1;
  296. DXGI_SAMPLE_DESC sampleDesc;
  297. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  298. rs->determineMultisampleSettings(mMultisampleCount, mMultisampleHint, d3dPF, &sampleDesc);
  299. desc.SampleDesc = sampleDesc;
  300. if(getTextureType() == TEX_TYPE_CUBE_MAP)
  301. {
  302. BS_EXCEPT(NotImplementedException, "Cube map not yet supported as a depth stencil target."); // TODO: Will be once I add proper texture array support
  303. }
  304. }
  305. else
  306. {
  307. desc.Usage = D3D11Mappings::getUsage((GpuBufferUsage)mUsage);
  308. desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  309. desc.CPUAccessFlags = D3D11Mappings::getAccessFlags((GpuBufferUsage)mUsage);
  310. // Determine total number of mipmaps including main one (d3d11 convention)
  311. UINT32 numMips = (mNumMipmaps == MIP_UNLIMITED || (1U << mNumMipmaps) > mWidth) ? 0 : mNumMipmaps + 1;
  312. desc.MipLevels = numMips;
  313. DXGI_SAMPLE_DESC sampleDesc;
  314. sampleDesc.Count = 1;
  315. sampleDesc.Quality = 0;
  316. desc.SampleDesc = sampleDesc;
  317. }
  318. if (getTextureType() == TEX_TYPE_CUBE_MAP)
  319. {
  320. desc.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;
  321. desc.ArraySize = 6;
  322. }
  323. // Create the texture
  324. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  325. D3D11Device& device = rs->getPrimaryDevice();
  326. hr = device.getD3D11Device()->CreateTexture2D(&desc, nullptr, &m2DTex);
  327. // Check result and except if failed
  328. if (FAILED(hr) || device.hasError())
  329. {
  330. destroy_internal();
  331. String errorDescription = device.getErrorDescription();
  332. BS_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
  333. }
  334. hr = m2DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)&mTex);
  335. if(FAILED(hr) || device.hasError())
  336. {
  337. destroy_internal();
  338. String errorDescription = device.getErrorDescription();
  339. BS_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
  340. }
  341. m2DTex->GetDesc(&desc);
  342. if(mNumMipmaps != (desc.MipLevels - 1))
  343. {
  344. BS_EXCEPT(RenderingAPIException, "Driver returned different number of mip maps than requested. " \
  345. "Requested: " + toString(mNumMipmaps) + ". Got: " + toString(desc.MipLevels - 1) + ".");
  346. }
  347. mDXGIFormat = desc.Format;
  348. // Create shader texture view
  349. if((mUsage & TU_DEPTHSTENCIL) == 0)
  350. {
  351. ZeroMemory(&mSRVDesc, sizeof(mSRVDesc));
  352. mSRVDesc.Format = desc.Format;
  353. if((mUsage & TU_RENDERTARGET) != 0)
  354. {
  355. if(mMultisampleCount > 0)
  356. {
  357. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;
  358. mSRVDesc.Texture2D.MostDetailedMip = 0;
  359. mSRVDesc.Texture2D.MipLevels = desc.MipLevels;
  360. }
  361. else
  362. {
  363. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  364. mSRVDesc.Texture2D.MostDetailedMip = 0;
  365. mSRVDesc.Texture2D.MipLevels = desc.MipLevels;
  366. }
  367. }
  368. else
  369. {
  370. switch(getTextureType())
  371. {
  372. case TEX_TYPE_CUBE_MAP:
  373. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
  374. mSRVDesc.TextureCube.MipLevels = desc.MipLevels;
  375. mSRVDesc.TextureCube.MostDetailedMip = 0;
  376. break;
  377. case TEX_TYPE_2D:
  378. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  379. mSRVDesc.Texture2D.MostDetailedMip = 0;
  380. mSRVDesc.Texture2D.MipLevels = desc.MipLevels;
  381. break;
  382. }
  383. }
  384. mDimension = mSRVDesc.ViewDimension;
  385. hr = device.getD3D11Device()->CreateShaderResourceView(m2DTex, &mSRVDesc, &mShaderResourceView);
  386. if (FAILED(hr) || device.hasError())
  387. {
  388. String errorDescription = device.getErrorDescription();
  389. BS_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
  390. }
  391. }
  392. else
  393. {
  394. if(mMultisampleCount > 0)
  395. mDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;
  396. else
  397. mDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  398. }
  399. }
  400. void D3D11Texture::create3DTex()
  401. {
  402. // We must have those defined here
  403. assert(mWidth > 0 && mHeight > 0 && mDepth > 0);
  404. // Determine which D3D11 pixel format we'll use
  405. HRESULT hr;
  406. DXGI_FORMAT d3dPF = D3D11Mappings::getPF(D3D11Mappings::getClosestSupportedPF(mFormat, mHwGamma), mHwGamma);
  407. if (mFormat != D3D11Mappings::getPF(d3dPF))
  408. {
  409. BS_EXCEPT(RenderingAPIException, "Provided pixel format is not supported by the driver: " + toString(mFormat));
  410. }
  411. D3D11_TEXTURE3D_DESC desc;
  412. desc.Width = static_cast<UINT32>(mWidth);
  413. desc.Height = static_cast<UINT32>(mHeight);
  414. desc.Depth = static_cast<UINT32>(mDepth);
  415. desc.Format = d3dPF;
  416. desc.MiscFlags = 0;
  417. if((mUsage & TU_RENDERTARGET) != 0)
  418. {
  419. desc.Usage = D3D11_USAGE_DEFAULT;
  420. desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
  421. desc.CPUAccessFlags = 0;
  422. desc.MipLevels = 1;
  423. }
  424. else if((mUsage & TU_DEPTHSTENCIL) != 0)
  425. {
  426. desc.Usage = D3D11_USAGE_DEFAULT;
  427. desc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
  428. desc.CPUAccessFlags = 0;
  429. desc.MipLevels = 1;
  430. }
  431. else
  432. {
  433. desc.Usage = D3D11Mappings::getUsage((GpuBufferUsage)mUsage);
  434. desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  435. desc.CPUAccessFlags = D3D11Mappings::getAccessFlags((GpuBufferUsage)mUsage);
  436. // Determine total number of mipmaps including main one (d3d11 convention)
  437. UINT numMips = (mNumMipmaps == MIP_UNLIMITED || (1U << mNumMipmaps) > std::max(std::max(mWidth, mHeight), mDepth)) ? 0 : mNumMipmaps + 1;
  438. desc.MipLevels = numMips;
  439. }
  440. // Create the texture
  441. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  442. D3D11Device& device = rs->getPrimaryDevice();
  443. hr = device.getD3D11Device()->CreateTexture3D(&desc, nullptr, &m3DTex);
  444. // Check result and except if failed
  445. if (FAILED(hr) || device.hasError())
  446. {
  447. destroy_internal();
  448. String errorDescription = device.getErrorDescription();
  449. BS_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
  450. }
  451. hr = m3DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)&mTex);
  452. if(FAILED(hr) || device.hasError())
  453. {
  454. destroy_internal();
  455. String errorDescription = device.getErrorDescription();
  456. BS_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
  457. }
  458. // Create texture view
  459. m3DTex->GetDesc(&desc);
  460. if(mNumMipmaps != (desc.MipLevels - 1))
  461. {
  462. BS_EXCEPT(RenderingAPIException, "Driver returned different number of mip maps than requested. " \
  463. "Requested: " + toString(mNumMipmaps) + ". Got: " + toString(desc.MipLevels - 1) + ".");
  464. }
  465. mDXGIFormat = desc.Format;
  466. mDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
  467. if((mUsage & TU_DEPTHSTENCIL) == 0)
  468. {
  469. ZeroMemory(&mSRVDesc, sizeof(mSRVDesc));
  470. mSRVDesc.Format = desc.Format;
  471. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
  472. mSRVDesc.Texture3D.MostDetailedMip = 0;
  473. mSRVDesc.Texture3D.MipLevels = desc.MipLevels;
  474. hr = device.getD3D11Device()->CreateShaderResourceView(m2DTex, &mSRVDesc, &mShaderResourceView);
  475. if (FAILED(hr) || device.hasError())
  476. {
  477. String errorDescription = device.getErrorDescription();
  478. BS_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
  479. }
  480. }
  481. }
  482. void* D3D11Texture::map(ID3D11Resource* res, D3D11_MAP flags, UINT32 mipLevel, UINT32 face, UINT32& rowPitch, UINT32& slicePitch)
  483. {
  484. D3D11_MAPPED_SUBRESOURCE pMappedResource;
  485. pMappedResource.pData = nullptr;
  486. mipLevel = Math::clamp(mipLevel, (UINT32)mipLevel, getNumMipmaps());
  487. face = Math::clamp(face, (UINT32)0, mDepth - 1);
  488. if(getTextureType() == TEX_TYPE_3D)
  489. face = 0;
  490. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  491. D3D11Device& device = rs->getPrimaryDevice();
  492. mLockedSubresourceIdx = D3D11CalcSubresource(mipLevel, face, getNumMipmaps()+1);
  493. device.getImmediateContext()->Map(res, mLockedSubresourceIdx, flags, 0, &pMappedResource);
  494. if (device.hasError())
  495. {
  496. String errorDescription = device.getErrorDescription();
  497. BS_EXCEPT(RenderingAPIException, "D3D11 device cannot map texture\nError Description:" + errorDescription);
  498. }
  499. UINT32 bytesPerPixel = PixelUtil::getNumElemBytes(getFormat());
  500. rowPitch = pMappedResource.RowPitch / bytesPerPixel;
  501. slicePitch = pMappedResource.DepthPitch / bytesPerPixel;
  502. return pMappedResource.pData;
  503. }
  504. void D3D11Texture::unmap(ID3D11Resource* res)
  505. {
  506. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  507. D3D11Device& device = rs->getPrimaryDevice();
  508. device.getImmediateContext()->Unmap(res, mLockedSubresourceIdx);
  509. if (device.hasError())
  510. {
  511. String errorDescription = device.getErrorDescription();
  512. BS_EXCEPT(RenderingAPIException, "D3D11 device unmap resource\nError Description:" + errorDescription);
  513. }
  514. }
  515. void* D3D11Texture::mapstagingbuffer(D3D11_MAP flags, UINT32 mipLevel, UINT32 face, UINT32& rowPitch, UINT32& slicePitch)
  516. {
  517. // Note: I am creating and destroying a staging resource every time a texture is read.
  518. // Consider offering a flag on init that will keep this active all the time (at the cost of double memory).
  519. // Reading is slow operation anyway so I don't believe doing it as we are now will influence it much.
  520. if(!mStagingBuffer)
  521. createStagingBuffer();
  522. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  523. D3D11Device& device = rs->getPrimaryDevice();
  524. device.getImmediateContext()->CopyResource(mStagingBuffer, mTex);
  525. return map(mStagingBuffer, flags, face, mipLevel, rowPitch, slicePitch);
  526. }
  527. void D3D11Texture::unmapstagingbuffer()
  528. {
  529. unmap(mStagingBuffer);
  530. SAFE_RELEASE(mStagingBuffer);
  531. }
  532. void* D3D11Texture::mapstaticbuffer(PixelData lock, UINT32 mipLevel, UINT32 face)
  533. {
  534. UINT32 sizeOfImage = lock.getConsecutiveSize();
  535. mLockedSubresourceIdx = D3D11CalcSubresource(mipLevel, face, getNumMipmaps()+1);
  536. mStaticBuffer = bs_new<PixelData, PoolAlloc>(lock.getWidth(), lock.getHeight(), lock.getDepth(), lock.getFormat());
  537. mStaticBuffer->allocateInternalBuffer();
  538. return mStaticBuffer->getData();
  539. }
  540. void D3D11Texture::unmapstaticbuffer()
  541. {
  542. UINT32 rowWidth = D3D11Mappings::getSizeInBytes(mStaticBuffer->getFormat(), mStaticBuffer->getWidth());
  543. UINT32 sliceWidth = D3D11Mappings::getSizeInBytes(mStaticBuffer->getFormat(), mStaticBuffer->getWidth(), mStaticBuffer->getHeight());
  544. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  545. D3D11Device& device = rs->getPrimaryDevice();
  546. device.getImmediateContext()->UpdateSubresource(mTex, mLockedSubresourceIdx, nullptr, mStaticBuffer->getData(), rowWidth, sliceWidth);
  547. if (device.hasError())
  548. {
  549. String errorDescription = device.getErrorDescription();
  550. BS_EXCEPT(RenderingAPIException, "D3D11 device cannot map texture\nError Description:" + errorDescription);
  551. }
  552. if(mStaticBuffer != nullptr)
  553. bs_delete<PoolAlloc>(mStaticBuffer);
  554. }
  555. void D3D11Texture::createStagingBuffer()
  556. {
  557. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  558. D3D11Device& device = rs->getPrimaryDevice();
  559. switch (getTextureType())
  560. {
  561. case TEX_TYPE_1D:
  562. {
  563. D3D11_TEXTURE1D_DESC desc;
  564. m1DTex->GetDesc(&desc);
  565. desc.BindFlags = 0;
  566. desc.MiscFlags = 0;
  567. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
  568. desc.Usage = D3D11_USAGE_STAGING;
  569. device.getD3D11Device()->CreateTexture1D(&desc, NULL, (ID3D11Texture1D**)(&mStagingBuffer));
  570. }
  571. break;
  572. case TEX_TYPE_2D:
  573. case TEX_TYPE_CUBE_MAP:
  574. {
  575. D3D11_TEXTURE2D_DESC desc;
  576. m2DTex->GetDesc(&desc);
  577. desc.BindFlags = 0;
  578. desc.MiscFlags = 0;
  579. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
  580. desc.Usage = D3D11_USAGE_STAGING;
  581. device.getD3D11Device()->CreateTexture2D(&desc, NULL, (ID3D11Texture2D**)(&mStagingBuffer));
  582. }
  583. break;
  584. case TEX_TYPE_3D:
  585. {
  586. D3D11_TEXTURE3D_DESC desc;
  587. m3DTex->GetDesc(&desc);
  588. desc.BindFlags = 0;
  589. desc.MiscFlags = 0;
  590. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
  591. desc.Usage = D3D11_USAGE_STAGING;
  592. device.getD3D11Device()->CreateTexture3D(&desc, NULL, (ID3D11Texture3D**)(&mStagingBuffer));
  593. }
  594. break;
  595. }
  596. }
  597. TextureViewPtr D3D11Texture::createView()
  598. {
  599. TextureViewPtr viewPtr = bs_core_ptr<D3D11TextureView, PoolAlloc>(new (bs_alloc<D3D11TextureView, PoolAlloc>()) D3D11TextureView());
  600. viewPtr->_setThisPtr(viewPtr);
  601. return viewPtr;
  602. }
  603. }