CmD3D11Texture.cpp 18 KB

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