2
0

BsRenderTexture.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "BsRenderTexture.h"
  2. #include "BsException.h"
  3. #include "BsPixelBuffer.h"
  4. #include "BsTexture.h"
  5. #include "BsTextureManager.h"
  6. #include "BsResources.h"
  7. #include "BsCoreThread.h"
  8. namespace BansheeEngine
  9. {
  10. RenderTextureCore::RenderTextureCore(RenderTexture* parent, RenderTextureProperties* properties, const RENDER_SURFACE_DESC& colorSurfaceDesc,
  11. const RENDER_SURFACE_DESC& depthStencilSurfaceDesc)
  12. :RenderTargetCore(parent, properties), mColorSurface(nullptr), mDepthStencilSurface(nullptr)
  13. {
  14. if (colorSurfaceDesc.texture != nullptr)
  15. {
  16. TexturePtr texture = colorSurfaceDesc.texture;
  17. if (texture->getUsage() != TU_RENDERTARGET)
  18. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with render target usage.");
  19. mColorSurface = Texture::requestView(texture, colorSurfaceDesc.mipLevel, 1,
  20. colorSurfaceDesc.face, colorSurfaceDesc.numFaces, GVU_RENDERTARGET);
  21. }
  22. if (depthStencilSurfaceDesc.texture != nullptr)
  23. {
  24. TexturePtr texture = depthStencilSurfaceDesc.texture;
  25. if (texture->getUsage() != TU_DEPTHSTENCIL)
  26. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with depth stencil usage.");
  27. mDepthStencilSurface = Texture::requestView(texture, depthStencilSurfaceDesc.mipLevel, 1,
  28. depthStencilSurfaceDesc.face, depthStencilSurfaceDesc.numFaces, GVU_DEPTHSTENCIL);
  29. }
  30. throwIfBuffersDontMatch();
  31. assert(mColorSurface != nullptr);
  32. assert(mColorSurface->getTexture() != nullptr);
  33. if (mColorSurface->getTexture()->getTextureType() != TEX_TYPE_2D)
  34. BS_EXCEPT(NotImplementedException, "Render textures are currently only implemented for 2D surfaces.");
  35. if ((mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) > mColorSurface->getTexture()->getNumFaces())
  36. {
  37. BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  38. toString(mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) +
  39. ". Max num faces: " + toString(mColorSurface->getTexture()->getNumFaces()));
  40. }
  41. if (mColorSurface->getMostDetailedMip() > mColorSurface->getTexture()->getNumMipmaps())
  42. {
  43. BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  44. toString(mColorSurface->getMostDetailedMip()) + ". Max num mipmaps: " + toString(mColorSurface->getTexture()->getNumMipmaps()));
  45. }
  46. }
  47. RenderTextureCore::~RenderTextureCore()
  48. {
  49. if (mColorSurface != nullptr)
  50. Texture::releaseView(mColorSurface);
  51. if (mDepthStencilSurface != nullptr)
  52. Texture::releaseView(mDepthStencilSurface);
  53. }
  54. void RenderTextureCore::throwIfBuffersDontMatch() const
  55. {
  56. if (mColorSurface == nullptr || mDepthStencilSurface == nullptr)
  57. return;
  58. if (mColorSurface->getTexture()->getWidth() != mDepthStencilSurface->getTexture()->getWidth() ||
  59. mColorSurface->getTexture()->getHeight() != mDepthStencilSurface->getTexture()->getHeight() ||
  60. mColorSurface->getTexture()->getMultisampleCount() != mDepthStencilSurface->getTexture()->getMultisampleCount() ||
  61. mColorSurface->getTexture()->getMultisampleHint() != mDepthStencilSurface->getTexture()->getMultisampleHint())
  62. {
  63. String errorInfo = "\nWidth: " + toString(mColorSurface->getTexture()->getWidth()) + "/" + toString(mDepthStencilSurface->getTexture()->getWidth());
  64. errorInfo += "\nHeight: " + toString(mColorSurface->getTexture()->getHeight()) + "/" + toString(mDepthStencilSurface->getTexture()->getHeight());
  65. errorInfo += "\nMultisample Count: " + toString(mColorSurface->getTexture()->getMultisampleCount()) + "/" + toString(mDepthStencilSurface->getTexture()->getMultisampleCount());
  66. errorInfo += "\nMultisample Hint: " + mColorSurface->getTexture()->getMultisampleHint() + "/" + mDepthStencilSurface->getTexture()->getMultisampleHint();
  67. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  68. }
  69. }
  70. RenderTexture* RenderTextureCore::getNonCore() const
  71. {
  72. return static_cast<RenderTexture*>(mParent);
  73. }
  74. RenderTexturePtr RenderTexture::create(TextureType textureType, UINT32 width, UINT32 height,
  75. PixelFormat format, bool hwGamma, UINT32 multisampleCount, const String& multisampleHint,
  76. bool createDepth, PixelFormat depthStencilFormat)
  77. {
  78. return TextureManager::instance().createRenderTexture(textureType, width, height, format, hwGamma,
  79. multisampleCount, multisampleHint, createDepth, depthStencilFormat);
  80. }
  81. RenderTexturePtr RenderTexture::create(const RENDER_TEXTURE_DESC& desc)
  82. {
  83. return TextureManager::instance().createRenderTexture(desc);
  84. }
  85. const RenderTextureProperties& RenderTexture::getProperties() const
  86. {
  87. THROW_IF_CORE_THREAD;
  88. return static_cast<const RenderTextureProperties&>(RenderTarget::getProperties());
  89. }
  90. RenderTextureCore* RenderTexture::getCore() const
  91. {
  92. return static_cast<RenderTextureCore*>(mCore);
  93. }
  94. void RenderTexture::initialize(const RENDER_TEXTURE_DESC& desc)
  95. {
  96. mColorSurfaceDesc = desc.colorSurface;
  97. mDepthStencilSurfaceDesc = desc.depthStencilSurface;
  98. TexturePtr texture = desc.colorSurface.texture;
  99. mProperties = createProperties();
  100. RenderTextureProperties* properties = static_cast<RenderTextureProperties*>(mProperties);
  101. if (texture != nullptr)
  102. {
  103. properties->mWidth = texture->getWidth();
  104. properties->mHeight = texture->getHeight();
  105. properties->mColorDepth = BansheeEngine::PixelUtil::getNumElemBits(texture->getFormat());
  106. properties->mHwGamma = texture->isHardwareGammaEnabled();
  107. properties->mMultisampleCount = texture->getMultisampleCount();
  108. properties->mMultisampleHint = texture->getMultisampleHint();
  109. }
  110. properties->mActive = true;
  111. properties->mIsWindow = false;
  112. properties->mRequiresTextureFlipping = requiresTextureFlipping();
  113. // Create non-persistent resource handles for the used textures (we only need them because a lot of the code accepts only handles,
  114. // since they're non persistent they don't really have any benefit over shared pointers)
  115. if (desc.colorSurface.texture != nullptr)
  116. mBindableColorTex = static_resource_cast<Texture>(gResources()._createResourceHandle(desc.colorSurface.texture));
  117. if (desc.depthStencilSurface.texture != nullptr)
  118. mBindableDepthStencilTex = static_resource_cast<Texture>(gResources()._createResourceHandle(desc.depthStencilSurface.texture));
  119. RenderTarget::initialize();
  120. }
  121. RenderTargetCore* RenderTexture::createCore()
  122. {
  123. RenderTextureProperties* coreProperties = bs_new<RenderTextureProperties>();
  124. RenderTextureProperties* myProperties = static_cast<RenderTextureProperties*>(mProperties);
  125. *coreProperties = *myProperties;
  126. return createCore(coreProperties, mColorSurfaceDesc, mDepthStencilSurfaceDesc);
  127. }
  128. }