CmD3D11Texture.cpp 18 KB

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