BsRenderTexture.cpp 6.4 KB

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