CmD3D11Texture.cpp 19 KB

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