CmD3D11Texture.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. #include "CmD3D11Texture.h"
  2. #include "CmD3D11Mappings.h"
  3. #include "CmD3D11Device.h"
  4. #include "CmD3D11RenderSystem.h"
  5. #include "CmD3D11TextureView.h"
  6. #include "CmException.h"
  7. #include "CmAsyncOp.h"
  8. #if CM_DEBUG_MODE
  9. #define THROW_IF_NOT_RENDER_THREAD throwIfNotRenderThread();
  10. #else
  11. #define THROW_IF_NOT_RENDER_THREAD
  12. #endif
  13. namespace CamelotEngine
  14. {
  15. D3D11Texture::D3D11Texture()
  16. : Texture()
  17. , m1DTex(nullptr)
  18. , m2DTex(nullptr)
  19. , m3DTex(nullptr)
  20. , mTex(nullptr)
  21. , mShaderResourceView(nullptr)
  22. , mStagingBuffer(nullptr)
  23. , mLockedSubresourceIdx(-1)
  24. , mLockedForReading(false)
  25. , mStaticBuffer(nullptr)
  26. {
  27. }
  28. D3D11Texture::~D3D11Texture()
  29. {
  30. }
  31. void D3D11Texture::copyImpl(TexturePtr& target)
  32. {
  33. D3D11Texture* other = static_cast<D3D11Texture*>(target.get());
  34. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  35. D3D11Device& device = rs->getPrimaryDevice();
  36. device.getImmediateContext()->CopyResource(other->getDX11Resource(), mTex);
  37. if (device.hasError())
  38. {
  39. String errorDescription = device.getErrorDescription();
  40. CM_EXCEPT(RenderingAPIException, "D3D11 device cannot copy resource\nError Description:" + errorDescription);
  41. }
  42. }
  43. PixelData D3D11Texture::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  44. {
  45. UINT32 mipWidth = mWidth >> mipLevel;
  46. UINT32 mipHeight = mHeight >> mipLevel;
  47. UINT32 mipDepth = mDepth >> mipLevel;
  48. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mFormat);
  49. D3D11_MAP flags = D3D11Mappings::getLockOptions(options);
  50. if(flags == D3D11_MAP_READ || flags == D3D11_MAP_READ_WRITE)
  51. {
  52. lockedArea.data = _mapstagingbuffer(flags, face, mipLevel);
  53. mLockedForReading = true;
  54. }
  55. else
  56. {
  57. if(mUsage == TU_DYNAMIC)
  58. lockedArea.data = _map(mTex, flags, face, mipLevel);
  59. else
  60. lockedArea.data = _mapstaticbuffer(lockedArea, mipLevel, face);
  61. mLockedForReading = false;
  62. }
  63. return lockedArea;
  64. }
  65. void D3D11Texture::unlockImpl()
  66. {
  67. if(mLockedForReading)
  68. _unmapstagingbuffer();
  69. else
  70. {
  71. if(mUsage == TU_DYNAMIC)
  72. _unmap(mTex);
  73. else
  74. _unmapstaticbuffer();
  75. }
  76. }
  77. void D3D11Texture::initialize_internal()
  78. {
  79. THROW_IF_NOT_RENDER_THREAD
  80. Texture::initialize_internal();
  81. // load based on tex.type
  82. switch (getTextureType())
  83. {
  84. case TEX_TYPE_1D:
  85. _create1DTex();
  86. break;
  87. case TEX_TYPE_2D:
  88. case TEX_TYPE_CUBE_MAP:
  89. _create2DTex();
  90. break;
  91. case TEX_TYPE_3D:
  92. _create3DTex();
  93. break;
  94. default:
  95. destroy_internal();
  96. CM_EXCEPT(RenderingAPIException, "Unknown texture type");
  97. }
  98. }
  99. void D3D11Texture::destroy_internal()
  100. {
  101. SAFE_RELEASE(mTex);
  102. SAFE_RELEASE(mShaderResourceView);
  103. SAFE_RELEASE(m1DTex);
  104. SAFE_RELEASE(m2DTex);
  105. SAFE_RELEASE(m3DTex);
  106. SAFE_RELEASE(mStagingBuffer);
  107. clearBufferViews();
  108. Texture::destroy_internal();
  109. }
  110. void D3D11Texture::_create1DTex()
  111. {
  112. // We must have those defined here
  113. assert(mWidth > 0);
  114. // Determine which D3D11 pixel format we'll use
  115. HRESULT hr;
  116. DXGI_FORMAT d3dPF = D3D11Mappings::_getPF(D3D11Mappings::_getClosestSupportedPF(mFormat));
  117. D3D11_TEXTURE1D_DESC desc;
  118. desc.Width = static_cast<UINT32>(mWidth);
  119. desc.ArraySize = 1;
  120. desc.Format = d3dPF;
  121. desc.MiscFlags = 0;
  122. if((mUsage & TU_RENDERTARGET) != 0)
  123. {
  124. desc.Usage = D3D11_USAGE_DEFAULT;
  125. desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
  126. desc.CPUAccessFlags = 0;
  127. desc.MipLevels = 1;
  128. }
  129. else if((mUsage & TU_DEPTHSTENCIL) != 0)
  130. {
  131. desc.Usage = D3D11_USAGE_DEFAULT;
  132. desc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
  133. desc.CPUAccessFlags = 0;
  134. desc.MipLevels = 1;
  135. }
  136. else
  137. {
  138. desc.Usage = D3D11Mappings::_getUsage(mUsage);
  139. desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  140. desc.CPUAccessFlags = D3D11Mappings::_getAccessFlags(mUsage);
  141. // Determine total number of mipmaps including main one (d3d11 convention)
  142. UINT32 numMips = (mNumMipmaps == MIP_UNLIMITED || (1U << mNumMipmaps) > mWidth) ? 0 : mNumMipmaps + 1;
  143. desc.MipLevels = numMips;
  144. }
  145. // Create the texture
  146. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  147. D3D11Device& device = rs->getPrimaryDevice();
  148. hr = device.getD3D11Device()->CreateTexture1D(&desc, nullptr, &m1DTex);
  149. // Check result and except if failed
  150. if (FAILED(hr) || device.hasError())
  151. {
  152. destroy_internal();
  153. String errorDescription = device.getErrorDescription();
  154. CM_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
  155. }
  156. hr = m1DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)&mTex);
  157. if(FAILED(hr) || device.hasError())
  158. {
  159. destroy_internal();
  160. String errorDescription = device.getErrorDescription();
  161. CM_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
  162. }
  163. m1DTex->GetDesc(&desc);
  164. mNumMipmaps = desc.MipLevels - 1;
  165. mDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
  166. // Create texture view
  167. if((mUsage & TU_DEPTHSTENCIL) == 0)
  168. {
  169. ZeroMemory(&mSRVDesc, sizeof(mSRVDesc));
  170. mSRVDesc.Format = desc.Format;
  171. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
  172. mSRVDesc.Texture1D.MipLevels = desc.MipLevels;
  173. hr = device.getD3D11Device()->CreateShaderResourceView(m1DTex, &mSRVDesc, &mShaderResourceView);
  174. if (FAILED(hr) || device.hasError())
  175. {
  176. String errorDescription = device.getErrorDescription();
  177. CM_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
  178. }
  179. }
  180. }
  181. void D3D11Texture::_create2DTex()
  182. {
  183. // We must have those defined here
  184. assert(mWidth > 0 || mHeight > 0);
  185. // Determine which D3D11 pixel format we'll use
  186. HRESULT hr;
  187. DXGI_FORMAT d3dPF = D3D11Mappings::_getPF(D3D11Mappings::_getClosestSupportedPF(mFormat));
  188. D3D11_TEXTURE2D_DESC desc;
  189. desc.Width = static_cast<UINT32>(mWidth);
  190. desc.Height = static_cast<UINT32>(mHeight);
  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. DXGI_SAMPLE_DESC sampleDesc;
  201. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  202. rs->determineFSAASettings(mFSAA, mFSAAHint, d3dPF, &sampleDesc);
  203. desc.SampleDesc = sampleDesc;
  204. if(getTextureType() == TEX_TYPE_CUBE_MAP)
  205. {
  206. CM_EXCEPT(NotImplementedException, "Cube map not yet supported as a render target."); // TODO: Will be once I add proper texture array support
  207. }
  208. }
  209. else if((mUsage & TU_DEPTHSTENCIL) != 0)
  210. {
  211. desc.Usage = D3D11_USAGE_DEFAULT;
  212. desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  213. desc.CPUAccessFlags = 0;
  214. desc.MipLevels = 1;
  215. DXGI_SAMPLE_DESC sampleDesc;
  216. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  217. rs->determineFSAASettings(mFSAA, mFSAAHint, d3dPF, &sampleDesc);
  218. desc.SampleDesc = sampleDesc;
  219. if(getTextureType() == TEX_TYPE_CUBE_MAP)
  220. {
  221. CM_EXCEPT(NotImplementedException, "Cube map not yet supported as a depth stencil target."); // TODO: Will be once I add proper texture array support
  222. }
  223. }
  224. else
  225. {
  226. desc.Usage = D3D11Mappings::_getUsage(mUsage);
  227. desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  228. desc.CPUAccessFlags = D3D11Mappings::_getAccessFlags(mUsage);
  229. // Determine total number of mipmaps including main one (d3d11 convention)
  230. UINT32 numMips = (mNumMipmaps == MIP_UNLIMITED || (1U << mNumMipmaps) > mWidth) ? 0 : mNumMipmaps + 1;
  231. desc.MipLevels = numMips;
  232. DXGI_SAMPLE_DESC sampleDesc;
  233. sampleDesc.Count = 1;
  234. sampleDesc.Quality = 0;
  235. desc.SampleDesc = sampleDesc;
  236. }
  237. if (getTextureType() == TEX_TYPE_CUBE_MAP)
  238. {
  239. desc.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;
  240. desc.ArraySize = 6;
  241. }
  242. // Create the texture
  243. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  244. D3D11Device& device = rs->getPrimaryDevice();
  245. hr = device.getD3D11Device()->CreateTexture2D(&desc, nullptr, &m2DTex);
  246. // Check result and except if failed
  247. if (FAILED(hr) || device.hasError())
  248. {
  249. destroy_internal();
  250. String errorDescription = device.getErrorDescription();
  251. CM_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
  252. }
  253. hr = m2DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)&mTex);
  254. if(FAILED(hr) || device.hasError())
  255. {
  256. destroy_internal();
  257. String errorDescription = device.getErrorDescription();
  258. CM_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
  259. }
  260. m2DTex->GetDesc(&desc);
  261. mNumMipmaps = desc.MipLevels - 1;
  262. mDXGIFormat = desc.Format;
  263. // Create shader texture view
  264. if((mUsage & TU_DEPTHSTENCIL) == 0)
  265. {
  266. ZeroMemory(&mSRVDesc, sizeof(mSRVDesc));
  267. mSRVDesc.Format = desc.Format;
  268. if((mUsage & TU_RENDERTARGET) != 0)
  269. {
  270. if(mFSAA > 0)
  271. {
  272. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;
  273. mSRVDesc.Texture2D.MostDetailedMip = 0;
  274. mSRVDesc.Texture2D.MipLevels = desc.MipLevels;
  275. }
  276. else
  277. {
  278. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  279. mSRVDesc.Texture2D.MostDetailedMip = 0;
  280. mSRVDesc.Texture2D.MipLevels = desc.MipLevels;
  281. }
  282. }
  283. else
  284. {
  285. switch(getTextureType())
  286. {
  287. case TEX_TYPE_CUBE_MAP:
  288. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
  289. mSRVDesc.TextureCube.MipLevels = desc.MipLevels;
  290. mSRVDesc.TextureCube.MostDetailedMip = 0;
  291. break;
  292. case TEX_TYPE_2D:
  293. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  294. mSRVDesc.Texture2D.MostDetailedMip = 0;
  295. mSRVDesc.Texture2D.MipLevels = desc.MipLevels;
  296. break;
  297. }
  298. }
  299. mDimension = mSRVDesc.ViewDimension;
  300. hr = device.getD3D11Device()->CreateShaderResourceView(m2DTex, &mSRVDesc, &mShaderResourceView);
  301. if (FAILED(hr) || device.hasError())
  302. {
  303. String errorDescription = device.getErrorDescription();
  304. CM_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
  305. }
  306. }
  307. else
  308. {
  309. if(mFSAA > 0)
  310. mDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;
  311. else
  312. mDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  313. }
  314. }
  315. void D3D11Texture::_create3DTex()
  316. {
  317. // We must have those defined here
  318. assert(mWidth > 0 && mHeight > 0 && mDepth > 0);
  319. // Determine which D3D11 pixel format we'll use
  320. HRESULT hr;
  321. DXGI_FORMAT d3dPF = D3D11Mappings::_getPF(D3D11Mappings::_getClosestSupportedPF(mFormat));
  322. D3D11_TEXTURE3D_DESC desc;
  323. desc.Width = static_cast<UINT32>(mWidth);
  324. desc.Height = static_cast<UINT32>(mHeight);
  325. desc.Depth = static_cast<UINT32>(mDepth);
  326. desc.Format = d3dPF;
  327. desc.MiscFlags = 0;
  328. if((mUsage & TU_RENDERTARGET) != 0)
  329. {
  330. desc.Usage = D3D11_USAGE_DEFAULT;
  331. desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
  332. desc.CPUAccessFlags = 0;
  333. desc.MipLevels = 1;
  334. }
  335. else if((mUsage & TU_DEPTHSTENCIL) != 0)
  336. {
  337. desc.Usage = D3D11_USAGE_DEFAULT;
  338. desc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
  339. desc.CPUAccessFlags = 0;
  340. desc.MipLevels = 1;
  341. }
  342. else
  343. {
  344. desc.Usage = D3D11Mappings::_getUsage(mUsage);
  345. desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  346. desc.CPUAccessFlags = D3D11Mappings::_getAccessFlags(mUsage);
  347. // Determine total number of mipmaps including main one (d3d11 convention)
  348. UINT numMips = (mNumMipmaps == MIP_UNLIMITED || (1U << mNumMipmaps) > std::max(std::max(mWidth, mHeight), mDepth)) ? 0 : mNumMipmaps + 1;
  349. desc.MipLevels = numMips;
  350. }
  351. // Create the texture
  352. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  353. D3D11Device& device = rs->getPrimaryDevice();
  354. hr = device.getD3D11Device()->CreateTexture3D(&desc, nullptr, &m3DTex);
  355. // Check result and except if failed
  356. if (FAILED(hr) || device.hasError())
  357. {
  358. destroy_internal();
  359. String errorDescription = device.getErrorDescription();
  360. CM_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
  361. }
  362. hr = m3DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)&mTex);
  363. if(FAILED(hr) || device.hasError())
  364. {
  365. destroy_internal();
  366. String errorDescription = device.getErrorDescription();
  367. CM_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
  368. }
  369. // Create texture view
  370. m3DTex->GetDesc(&desc);
  371. mNumMipmaps = desc.MipLevels - 1;
  372. mDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
  373. if((mUsage & TU_DEPTHSTENCIL) == 0)
  374. {
  375. ZeroMemory(&mSRVDesc, sizeof(mSRVDesc));
  376. mSRVDesc.Format = desc.Format;
  377. mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
  378. mSRVDesc.Texture3D.MostDetailedMip = 0;
  379. mSRVDesc.Texture3D.MipLevels = desc.MipLevels;
  380. hr = device.getD3D11Device()->CreateShaderResourceView(m2DTex, &mSRVDesc, &mShaderResourceView);
  381. if (FAILED(hr) || device.hasError())
  382. {
  383. String errorDescription = device.getErrorDescription();
  384. CM_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
  385. }
  386. }
  387. }
  388. void* D3D11Texture::_map(ID3D11Resource* res, D3D11_MAP flags, UINT32 mipLevel, UINT32 face)
  389. {
  390. D3D11_MAPPED_SUBRESOURCE pMappedResource;
  391. pMappedResource.pData = nullptr;
  392. mipLevel = Math::Clamp(mipLevel, (UINT32)mipLevel, getNumMipmaps());
  393. face = Math::Clamp(face, (UINT32)0, mDepth - 1);
  394. if(getTextureType() == TEX_TYPE_3D)
  395. face = 0;
  396. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  397. D3D11Device& device = rs->getPrimaryDevice();
  398. mLockedSubresourceIdx = D3D11CalcSubresource(mipLevel, face, getNumMipmaps()+1);
  399. device.getImmediateContext()->Map(res, mLockedSubresourceIdx, flags, 0, &pMappedResource);
  400. if (device.hasError())
  401. {
  402. String errorDescription = device.getErrorDescription();
  403. CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map texture\nError Description:" + errorDescription);
  404. }
  405. return pMappedResource.pData;
  406. }
  407. void D3D11Texture::_unmap(ID3D11Resource* res)
  408. {
  409. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  410. D3D11Device& device = rs->getPrimaryDevice();
  411. device.getImmediateContext()->Unmap(res, mLockedSubresourceIdx);
  412. if (device.hasError())
  413. {
  414. String errorDescription = device.getErrorDescription();
  415. CM_EXCEPT(RenderingAPIException, "D3D11 device unmap resource\nError Description:" + errorDescription);
  416. }
  417. }
  418. void* D3D11Texture::_mapstagingbuffer(D3D11_MAP flags, UINT32 mipLevel, UINT32 face)
  419. {
  420. if(!mStagingBuffer)
  421. _createStagingBuffer();
  422. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  423. D3D11Device& device = rs->getPrimaryDevice();
  424. device.getImmediateContext()->CopyResource(mStagingBuffer, mTex);
  425. return _map(mStagingBuffer, flags, face, mipLevel);
  426. }
  427. void D3D11Texture::_unmapstagingbuffer()
  428. {
  429. _unmap(mStagingBuffer);
  430. }
  431. void* D3D11Texture::_mapstaticbuffer(PixelData lock, UINT32 mipLevel, UINT32 face)
  432. {
  433. UINT32 sizeOfImage = lock.getConsecutiveSize();
  434. mLockedSubresourceIdx = D3D11CalcSubresource(mipLevel, face, getNumMipmaps()+1);
  435. mStaticBuffer = CM_NEW(PixelData, PoolAlloc) PixelData(lock.getWidth(), lock.getHeight(), lock.getDepth(), lock.getFormat());
  436. return mStaticBuffer->allocData(sizeOfImage);
  437. }
  438. void D3D11Texture::_unmapstaticbuffer()
  439. {
  440. UINT32 rowWidth = D3D11Mappings::_getSizeInBytes(mStaticBuffer->getFormat(), mStaticBuffer->getWidth());
  441. UINT32 sliceWidth = D3D11Mappings::_getSizeInBytes(mStaticBuffer->getFormat(), mStaticBuffer->getWidth(), mStaticBuffer->getHeight());
  442. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  443. D3D11Device& device = rs->getPrimaryDevice();
  444. device.getImmediateContext()->UpdateSubresource(mTex, mLockedSubresourceIdx, nullptr, mStaticBuffer->data, rowWidth, sliceWidth);
  445. if (device.hasError())
  446. {
  447. String errorDescription = device.getErrorDescription();
  448. CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map texture\nError Description:" + errorDescription);
  449. }
  450. if(mStaticBuffer != nullptr)
  451. CM_DELETE(mStaticBuffer, PixelData, PoolAlloc);
  452. }
  453. void D3D11Texture::_createStagingBuffer()
  454. {
  455. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  456. D3D11Device& device = rs->getPrimaryDevice();
  457. switch (getTextureType())
  458. {
  459. case TEX_TYPE_1D:
  460. {
  461. D3D11_TEXTURE1D_DESC desc;
  462. m1DTex->GetDesc(&desc);
  463. desc.BindFlags = 0;
  464. desc.MiscFlags = 0;
  465. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
  466. desc.Usage = D3D11_USAGE_STAGING;
  467. device.getD3D11Device()->CreateTexture1D(&desc, NULL, (ID3D11Texture1D**)(&mStagingBuffer));
  468. }
  469. break;
  470. case TEX_TYPE_2D:
  471. case TEX_TYPE_CUBE_MAP:
  472. {
  473. D3D11_TEXTURE2D_DESC desc;
  474. m2DTex->GetDesc(&desc);
  475. desc.BindFlags = 0;
  476. desc.MiscFlags = 0;
  477. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
  478. desc.Usage = D3D11_USAGE_STAGING;
  479. device.getD3D11Device()->CreateTexture2D(&desc, NULL, (ID3D11Texture2D**)(&mStagingBuffer));
  480. }
  481. break;
  482. case TEX_TYPE_3D:
  483. {
  484. D3D11_TEXTURE3D_DESC desc;
  485. m3DTex->GetDesc(&desc);
  486. desc.BindFlags = 0;
  487. desc.MiscFlags = 0;
  488. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
  489. desc.Usage = D3D11_USAGE_STAGING;
  490. device.getD3D11Device()->CreateTexture3D(&desc, NULL, (ID3D11Texture3D**)(&mStagingBuffer));
  491. }
  492. break;
  493. }
  494. }
  495. TextureViewPtr D3D11Texture::createView()
  496. {
  497. TextureViewPtr viewPtr(CM_NEW(D3D11TextureView, PoolAlloc) D3D11TextureView(), &CoreObject::_deleteDelayed<D3D11TextureView, PoolAlloc>);
  498. viewPtr->setThisPtr(viewPtr);
  499. return viewPtr;
  500. }
  501. }
  502. #undef THROW_IF_NOT_RENDER_THREAD