BsRenderTexture.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. {
  67. String errorInfo = "\nWidth: " + toString(mColorSurface->getTexture()->getWidth()) + "/" + toString(mDepthStencilSurface->getTexture()->getWidth());
  68. errorInfo += "\nHeight: " + toString(mColorSurface->getTexture()->getHeight()) + "/" + toString(mDepthStencilSurface->getTexture()->getHeight());
  69. errorInfo += "\nMultisample Count: " + toString(mColorSurface->getTexture()->getMultisampleCount()) + "/" + toString(mDepthStencilSurface->getTexture()->getMultisampleCount());
  70. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  71. }
  72. }
  73. RenderTexture* RenderTextureCore::getNonCore() const
  74. {
  75. return static_cast<RenderTexture*>(mParent);
  76. }
  77. RenderTexturePtr RenderTexture::create(TextureType textureType, UINT32 width, UINT32 height,
  78. PixelFormat format, bool hwGamma, UINT32 multisampleCount,
  79. bool createDepth, PixelFormat depthStencilFormat)
  80. {
  81. return TextureManager::instance().createRenderTexture(textureType, width, height, format, hwGamma,
  82. multisampleCount, createDepth, depthStencilFormat);
  83. }
  84. RenderTexturePtr RenderTexture::create(const RENDER_TEXTURE_DESC& desc)
  85. {
  86. return TextureManager::instance().createRenderTexture(desc);
  87. }
  88. const RenderTextureProperties& RenderTexture::getProperties() const
  89. {
  90. THROW_IF_CORE_THREAD;
  91. return static_cast<const RenderTextureProperties&>(RenderTarget::getProperties());
  92. }
  93. RenderTextureCore* RenderTexture::getCore() const
  94. {
  95. return static_cast<RenderTextureCore*>(mCore);
  96. }
  97. void RenderTexture::initialize(const RENDER_TEXTURE_DESC& desc)
  98. {
  99. mColorSurfaceDesc = desc.colorSurface;
  100. mDepthStencilSurfaceDesc = desc.depthStencilSurface;
  101. TexturePtr texture = desc.colorSurface.texture;
  102. mProperties = createProperties();
  103. RenderTextureProperties* properties = static_cast<RenderTextureProperties*>(mProperties);
  104. if (texture != nullptr)
  105. {
  106. properties->mWidth = texture->getWidth();
  107. properties->mHeight = texture->getHeight();
  108. properties->mColorDepth = BansheeEngine::PixelUtil::getNumElemBits(texture->getFormat());
  109. properties->mHwGamma = texture->isHardwareGammaEnabled();
  110. properties->mMultisampleCount = texture->getMultisampleCount();
  111. }
  112. properties->mActive = true;
  113. properties->mIsWindow = false;
  114. properties->mRequiresTextureFlipping = requiresTextureFlipping();
  115. // Create non-persistent resource handles for the used textures (we only need them because a lot of the code accepts only handles,
  116. // since they're non persistent they don't really have any benefit over shared pointers)
  117. if (desc.colorSurface.texture != nullptr)
  118. mBindableColorTex = static_resource_cast<Texture>(gResources()._createResourceHandle(desc.colorSurface.texture));
  119. if (desc.depthStencilSurface.texture != nullptr)
  120. mBindableDepthStencilTex = static_resource_cast<Texture>(gResources()._createResourceHandle(desc.depthStencilSurface.texture));
  121. RenderTarget::initialize();
  122. }
  123. RenderTargetCore* RenderTexture::createCore()
  124. {
  125. RenderTextureProperties* coreProperties = bs_new<RenderTextureProperties>();
  126. RenderTextureProperties* myProperties = static_cast<RenderTextureProperties*>(mProperties);
  127. *coreProperties = *myProperties;
  128. return createCore(coreProperties, mColorSurfaceDesc, mDepthStencilSurfaceDesc);
  129. }
  130. }