BsD3D11Texture.cpp 22 KB

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