CmD3D11TextureView.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "CmD3D11TextureView.h"
  2. #include "CmD3D11RenderSystem.h"
  3. #include "CmD3D11Device.h"
  4. #include "CmD3D11Texture.h"
  5. #include "CmUtil.h"
  6. #include "CmException.h"
  7. namespace CamelotFramework
  8. {
  9. D3D11TextureView::D3D11TextureView()
  10. :TextureView(), mSRV(nullptr), mUAV(nullptr), mDSV(nullptr), mRTV(nullptr)
  11. {
  12. }
  13. void D3D11TextureView::initialize_internal()
  14. {
  15. D3D11Texture* d3d11Texture = static_cast<D3D11Texture*>(mOwnerTexture.get());
  16. if((mDesc.usage & GVU_RANDOMWRITE) != 0)
  17. mUAV = createUAV(d3d11Texture, mDesc.mostDetailMip, mDesc.numMips, mDesc.firstArraySlice, mDesc.numArraySlices);
  18. else if((mDesc.usage & GVU_RENDERTARGET) != 0)
  19. mRTV = createRTV(d3d11Texture, mDesc.mostDetailMip, mDesc.numMips, mDesc.firstArraySlice, mDesc.numArraySlices);
  20. else if((mDesc.usage & GVU_DEPTHSTENCIL) != 0)
  21. mDSV = createDSV(d3d11Texture, mDesc.mostDetailMip, mDesc.numMips, mDesc.firstArraySlice, mDesc.numArraySlices);
  22. else
  23. mSRV = createSRV(d3d11Texture, mDesc.mostDetailMip, mDesc.numMips, mDesc.firstArraySlice, mDesc.numArraySlices);
  24. TextureView::initialize_internal();
  25. }
  26. void D3D11TextureView::destroy_internal()
  27. {
  28. SAFE_RELEASE(mSRV);
  29. SAFE_RELEASE(mUAV);
  30. SAFE_RELEASE(mDSV);
  31. SAFE_RELEASE(mRTV);
  32. TextureView::destroy_internal();
  33. }
  34. ID3D11ShaderResourceView* D3D11TextureView::createSRV(D3D11Texture* texture,
  35. UINT32 mostDetailMip, UINT32 numMips, UINT32 firstArraySlice, UINT32 numArraySlices)
  36. {
  37. D3D11_SHADER_RESOURCE_VIEW_DESC desc;
  38. ZeroMemory(&desc, sizeof(desc));
  39. switch(texture->getTextureType())
  40. {
  41. case TEX_TYPE_1D:
  42. desc.Texture1D.MipLevels = numMips;
  43. desc.Texture1D.MostDetailedMip = mostDetailMip;
  44. desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
  45. break;
  46. case TEX_TYPE_2D:
  47. desc.Texture2D.MipLevels = numMips;
  48. desc.Texture2D.MostDetailedMip = mostDetailMip;
  49. if(texture->getFSAA() > 0)
  50. desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;
  51. else
  52. desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  53. break;
  54. case TEX_TYPE_3D:
  55. desc.Texture3D.MipLevels = numMips;
  56. desc.Texture3D.MostDetailedMip = mostDetailMip;
  57. desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
  58. break;
  59. case TEX_TYPE_CUBE_MAP:
  60. desc.TextureCube.MipLevels = numMips;
  61. desc.TextureCube.MostDetailedMip = mostDetailMip;
  62. desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
  63. break;
  64. default:
  65. CM_EXCEPT(InvalidParametersException, "Invalid texture type for this view type.");
  66. }
  67. desc.Format = texture->getDXGIFormat();
  68. ID3D11ShaderResourceView* srv = nullptr;
  69. D3D11RenderSystem* d3d11rs = static_cast<D3D11RenderSystem*>(D3D11RenderSystem::instancePtr());
  70. HRESULT hr = d3d11rs->getPrimaryDevice().getD3D11Device()->CreateShaderResourceView(texture->getDX11Resource(), &desc, &srv);
  71. if (FAILED(hr) || d3d11rs->getPrimaryDevice().hasError())
  72. {
  73. String msg = d3d11rs->getPrimaryDevice().getErrorDescription();
  74. CM_EXCEPT(RenderingAPIException, "Cannot create ShaderResourceView: " + msg);
  75. }
  76. return srv;
  77. }
  78. ID3D11RenderTargetView* D3D11TextureView::createRTV(D3D11Texture* texture,
  79. UINT32 mostDetailMip, UINT32 numMips, UINT32 firstArraySlice, UINT32 numArraySlices)
  80. {
  81. D3D11_RENDER_TARGET_VIEW_DESC desc;
  82. ZeroMemory(&desc, sizeof(desc));
  83. switch(texture->getTextureType())
  84. {
  85. case TEX_TYPE_1D:
  86. desc.Texture1D.MipSlice = mostDetailMip;
  87. desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE1D;
  88. break;
  89. case TEX_TYPE_2D:
  90. desc.Texture2D.MipSlice = mostDetailMip;
  91. if(texture->getFSAA() > 0)
  92. desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS;
  93. else
  94. desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
  95. break;
  96. case TEX_TYPE_3D:
  97. desc.Texture3D.MipSlice = mostDetailMip;
  98. desc.Texture3D.FirstWSlice = firstArraySlice;
  99. desc.Texture3D.WSize = numArraySlices;
  100. desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE3D;
  101. break;
  102. default:
  103. CM_EXCEPT(InvalidParametersException, "Invalid texture type for this view type.");
  104. }
  105. desc.Format = texture->getDXGIFormat();
  106. ID3D11RenderTargetView* rtv = nullptr;
  107. D3D11RenderSystem* d3d11rs = static_cast<D3D11RenderSystem*>(D3D11RenderSystem::instancePtr());
  108. HRESULT hr = d3d11rs->getPrimaryDevice().getD3D11Device()->CreateRenderTargetView(texture->getDX11Resource(), &desc, &rtv);
  109. if (FAILED(hr) || d3d11rs->getPrimaryDevice().hasError())
  110. {
  111. String msg = d3d11rs->getPrimaryDevice().getErrorDescription();
  112. CM_EXCEPT(RenderingAPIException, "Cannot create RenderTargetView: " + msg);
  113. }
  114. return rtv;
  115. }
  116. ID3D11UnorderedAccessView* D3D11TextureView::createUAV(D3D11Texture* texture,
  117. UINT32 mostDetailMip, UINT32 numMips, UINT32 firstArraySlice, UINT32 numArraySlices)
  118. {
  119. D3D11_UNORDERED_ACCESS_VIEW_DESC desc;
  120. ZeroMemory(&desc, sizeof(desc));
  121. switch(texture->getTextureType())
  122. {
  123. case TEX_TYPE_1D:
  124. desc.Texture1D.MipSlice = mostDetailMip;
  125. desc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE1D;
  126. break;
  127. case TEX_TYPE_2D:
  128. desc.Texture2D.MipSlice = mostDetailMip;
  129. desc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
  130. break;
  131. case TEX_TYPE_3D:
  132. desc.Texture3D.MipSlice = mostDetailMip;
  133. desc.Texture3D.FirstWSlice = firstArraySlice;
  134. desc.Texture3D.WSize = numArraySlices;
  135. desc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE3D;
  136. break;
  137. default:
  138. CM_EXCEPT(InvalidParametersException, "Invalid texture type for this view type.");
  139. }
  140. desc.Format = texture->getDXGIFormat();
  141. ID3D11UnorderedAccessView* uav = nullptr;
  142. D3D11RenderSystem* d3d11rs = static_cast<D3D11RenderSystem*>(D3D11RenderSystem::instancePtr());
  143. HRESULT hr = d3d11rs->getPrimaryDevice().getD3D11Device()->CreateUnorderedAccessView(texture->getDX11Resource(), &desc, &uav);
  144. if (FAILED(hr) || d3d11rs->getPrimaryDevice().hasError())
  145. {
  146. String msg = d3d11rs->getPrimaryDevice().getErrorDescription();
  147. CM_EXCEPT(RenderingAPIException, "Cannot create UnorderedAccessView: " + msg);
  148. }
  149. return uav;
  150. }
  151. ID3D11DepthStencilView* D3D11TextureView::createDSV(D3D11Texture* texture,
  152. UINT32 mostDetailMip, UINT32 numMips, UINT32 firstArraySlice, UINT32 numArraySlices)
  153. {
  154. D3D11_DEPTH_STENCIL_VIEW_DESC desc;
  155. ZeroMemory(&desc, sizeof(desc));
  156. switch(texture->getTextureType())
  157. {
  158. case TEX_TYPE_1D:
  159. desc.Texture1D.MipSlice = mostDetailMip;
  160. desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE1D;
  161. break;
  162. case TEX_TYPE_2D:
  163. desc.Texture2D.MipSlice = mostDetailMip;
  164. if(texture->getFSAA() > 0)
  165. desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
  166. else
  167. desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  168. break;
  169. default:
  170. CM_EXCEPT(InvalidParametersException, "Invalid texture type for this view type.");
  171. }
  172. desc.Format = texture->getDXGIFormat();
  173. ID3D11DepthStencilView* dsv = nullptr;
  174. D3D11RenderSystem* d3d11rs = static_cast<D3D11RenderSystem*>(D3D11RenderSystem::instancePtr());
  175. HRESULT hr = d3d11rs->getPrimaryDevice().getD3D11Device()->CreateDepthStencilView(texture->getDX11Resource(), &desc, &dsv);
  176. if (FAILED(hr) || d3d11rs->getPrimaryDevice().hasError())
  177. {
  178. String msg = d3d11rs->getPrimaryDevice().getErrorDescription();
  179. CM_EXCEPT(RenderingAPIException, "Cannot create DepthStencilView: " + msg);
  180. }
  181. return dsv;
  182. }
  183. }