CmD3D11TextureView.cpp 7.3 KB

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