BsRenderTexture.cpp 6.7 KB

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