CmD3D11Texture.cpp 15 KB

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