CmD3D11Texture.cpp 18 KB

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