CmD3D11Texture.cpp 18 KB

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