CmD3D11Texture.cpp 19 KB

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