CmD3D11RenderTexture.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "CmD3D11RenderTexture.h"
  2. #include "CmDepthStencilBuffer.h"
  3. #include "CmD3D11RenderSystem.h"
  4. #include "CmD3D11Device.h"
  5. #include "CmD3D11Texture.h"
  6. #include "CmD3D11Mappings.h"
  7. #include "CmTextureManager.h"
  8. #include "CmException.h"
  9. namespace CamelotEngine
  10. {
  11. D3D11RenderTexture::D3D11RenderTexture(TextureType textureType, UINT32 width, UINT32 height,
  12. PixelFormat format, bool hwGamma, UINT32 fsaa, const String& fsaaHint,
  13. bool createDepth, UINT32 depthBits)
  14. :mRenderTargetView(nullptr)
  15. {
  16. mPriority = CM_REND_TO_TEX_RT_GROUP;
  17. if(textureType != TEX_TYPE_2D)
  18. CM_EXCEPT(NotImplementedException, "Render textures are currently only implemented for 2D surfaces.");
  19. mWidth = width;
  20. mHeight = height;
  21. mType = textureType;
  22. mFormat = format;
  23. mColorDepth = CamelotEngine::PixelUtil::getNumElemBits(format);
  24. mActive = true;
  25. mHwGamma = hwGamma;
  26. mFSAA = fsaa;
  27. mFSAAHint = fsaaHint;
  28. mDepthBits = depthBits;
  29. createTextureBuffer();
  30. if(createDepth)
  31. createDepthStencilBuffer();
  32. createResourceView();
  33. }
  34. D3D11RenderTexture::D3D11RenderTexture(TexturePtr texture, DepthStencilBufferPtr depthStencilbuffer)
  35. :mRenderTargetView(nullptr)
  36. {
  37. setBuffers(texture, depthStencilbuffer);
  38. }
  39. void D3D11RenderTexture::setBuffers(TexturePtr texture, DepthStencilBufferPtr depthStencilBuffer)
  40. {
  41. assert(texture != nullptr);
  42. assert(depthStencilBuffer != nullptr);
  43. SAFE_RELEASE(mRenderTargetView)
  44. mPriority = CM_REND_TO_TEX_RT_GROUP;
  45. if(texture->getTextureType() != TEX_TYPE_2D)
  46. CM_EXCEPT(NotImplementedException, "Render textures are currently only implemented for 2D surfaces.");
  47. if(texture->getWidth() != depthStencilBuffer->getWidth() ||
  48. texture->getHeight() != depthStencilBuffer->getWidth() ||
  49. texture->getFSAA() != depthStencilBuffer->getFsaa() ||
  50. texture->getFSAAHint() != depthStencilBuffer->getFsaaHint())
  51. {
  52. String errorInfo = "\nWidth: " + toString(texture->getWidth()) + "/" + toString(depthStencilBuffer->getWidth());
  53. errorInfo += "\nHeight: " + toString(texture->getHeight()) + "/" + toString(depthStencilBuffer->getHeight());
  54. errorInfo += "\nFSAA: " + toString(texture->getFSAA()) + "/" + toString(depthStencilBuffer->getFsaa());
  55. errorInfo += "\nFSAAHint: " + texture->getFSAAHint() + "/" + depthStencilBuffer->getFsaaHint();
  56. CM_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  57. }
  58. mWidth = texture->getWidth();
  59. mHeight = texture->getWidth();
  60. mColorDepth = CamelotEngine::PixelUtil::getNumElemBits(texture->getFormat());
  61. mActive = true;
  62. mHwGamma = texture->isHardwareGammaEnabled();
  63. mFSAA = texture->getFSAA();
  64. mFSAAHint = texture->getFSAAHint();
  65. mType = texture->getTextureType();
  66. mFormat = texture->getFormat();
  67. mDepthBits = depthStencilBuffer->getBitDepth();
  68. mTexture = texture;
  69. mDepthStencilBuffer = depthStencilBuffer;
  70. createResourceView();
  71. }
  72. D3D11RenderTexture::~D3D11RenderTexture()
  73. {
  74. SAFE_RELEASE(mRenderTargetView);
  75. }
  76. void D3D11RenderTexture::createTextureBuffer()
  77. {
  78. mTexture = TextureManager::instance().createTexture(mType, mWidth, mHeight, 0, mFormat, TU_RENDERTARGET, mHwGamma, mFSAA, mFSAAHint);
  79. }
  80. void D3D11RenderTexture::createDepthStencilBuffer()
  81. {
  82. mDepthStencilBuffer = TextureManager::instance().createDepthStencilBuffer(mDepthBits, mWidth, mHeight, mFSAA, mFSAAHint);
  83. }
  84. void D3D11RenderTexture::createResourceView()
  85. {
  86. D3D11_RENDER_TARGET_VIEW_DESC RTVDesc;
  87. ZeroMemory(&RTVDesc, sizeof(RTVDesc));
  88. D3D11Texture* d3d11Texture = static_cast<D3D11Texture*>(mTexture.get());
  89. D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = d3d11Texture->getSRVDesc();
  90. RTVDesc.Format = SRVDesc.Format;
  91. switch(SRVDesc.ViewDimension)
  92. {
  93. case D3D11_SRV_DIMENSION_BUFFER:
  94. RTVDesc.ViewDimension = D3D11_RTV_DIMENSION_BUFFER;
  95. break;
  96. case D3D11_SRV_DIMENSION_TEXTURE1D:
  97. RTVDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE1D;
  98. break;
  99. case D3D11_SRV_DIMENSION_TEXTURE1DARRAY:
  100. RTVDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE1DARRAY;
  101. break;
  102. case D3D11_SRV_DIMENSION_TEXTURECUBE:
  103. RTVDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
  104. RTVDesc.Texture2DArray.FirstArraySlice = 0;
  105. RTVDesc.Texture2DArray.ArraySize = 6;
  106. RTVDesc.Texture2DArray.MipSlice = 0;
  107. break;
  108. case D3D11_SRV_DIMENSION_TEXTURE2D:
  109. RTVDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
  110. break;
  111. case D3D11_SRV_DIMENSION_TEXTURE2DARRAY:
  112. RTVDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
  113. break;
  114. case D3D11_SRV_DIMENSION_TEXTURE2DMS:
  115. RTVDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS;
  116. break;
  117. case D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY:
  118. RTVDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY;
  119. break;
  120. case D3D11_SRV_DIMENSION_TEXTURE3D:
  121. RTVDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE3D;
  122. break;
  123. default:
  124. assert(false);
  125. }
  126. D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
  127. HRESULT hr = device.getD3D11Device()->CreateRenderTargetView(d3d11Texture->getDX11Resource(), &RTVDesc, &mRenderTargetView);
  128. if (FAILED(hr) || device.hasError())
  129. {
  130. String errorDescription = device.getErrorDescription();
  131. CM_EXCEPT(RenderingAPIException, "Error creating Render Target View\nError Description:" + errorDescription);
  132. }
  133. }
  134. }