BsRenderTexture.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsRenderTexture.h"
  4. #include "BsException.h"
  5. #include "BsPixelBuffer.h"
  6. #include "BsTexture.h"
  7. #include "BsTextureManager.h"
  8. #include "BsResources.h"
  9. #include "BsCoreThread.h"
  10. #include "BsFrameAlloc.h"
  11. namespace BansheeEngine
  12. {
  13. RenderTextureProperties::RenderTextureProperties(const RENDER_TEXTURE_DESC& desc, bool requiresFlipping)
  14. {
  15. HTexture texture = desc.colorSurface.texture;
  16. if (texture.isLoaded())
  17. {
  18. const TextureProperties& props = texture->getProperties();
  19. construct(&props, requiresFlipping);
  20. }
  21. else
  22. construct(nullptr, requiresFlipping);
  23. }
  24. RenderTextureProperties::RenderTextureProperties(const RENDER_TEXTURE_CORE_DESC& desc, bool requiresFlipping)
  25. {
  26. SPtr<TextureCore> texture = desc.colorSurface.texture;
  27. if (texture != nullptr)
  28. {
  29. const TextureProperties& props = texture->getProperties();
  30. construct(&props, requiresFlipping);
  31. }
  32. else
  33. construct(nullptr, requiresFlipping);
  34. }
  35. void RenderTextureProperties::construct(const TextureProperties* textureProps, bool requiresFlipping)
  36. {
  37. if (textureProps != nullptr)
  38. {
  39. mWidth = textureProps->getWidth();
  40. mHeight = textureProps->getHeight();
  41. mColorDepth = BansheeEngine::PixelUtil::getNumElemBits(textureProps->getFormat());
  42. mHwGamma = textureProps->isHardwareGammaEnabled();
  43. mMultisampleCount = textureProps->getMultisampleCount();
  44. }
  45. mActive = true;
  46. mIsWindow = false;
  47. mRequiresTextureFlipping = requiresFlipping;
  48. }
  49. RenderTextureCore::RenderTextureCore(const RENDER_TEXTURE_CORE_DESC& desc)
  50. :mColorSurface(nullptr), mDepthStencilSurface(nullptr), mDesc(desc)
  51. { }
  52. RenderTextureCore::~RenderTextureCore()
  53. {
  54. if (mColorSurface != nullptr)
  55. TextureCore::releaseView(mColorSurface);
  56. if (mDepthStencilSurface != nullptr)
  57. TextureCore::releaseView(mDepthStencilSurface);
  58. }
  59. void RenderTextureCore::initialize()
  60. {
  61. RenderTargetCore::initialize();
  62. const RENDER_SURFACE_CORE_DESC& colorSurface = mDesc.colorSurface;
  63. if (colorSurface.texture != nullptr)
  64. {
  65. SPtr<TextureCore> texture = colorSurface.texture;
  66. if (texture->getProperties().getUsage() != TU_RENDERTARGET)
  67. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with render target usage.");
  68. mColorSurface = TextureCore::requestView(texture, colorSurface.mipLevel, 1,
  69. colorSurface.face, 1, GVU_RENDERTARGET);
  70. }
  71. const RENDER_SURFACE_CORE_DESC& depthStencilSurface = mDesc.depthStencilSurface;
  72. if (depthStencilSurface.texture != nullptr)
  73. {
  74. SPtr<TextureCore> texture = depthStencilSurface.texture;
  75. if (texture->getProperties().getUsage() != TU_DEPTHSTENCIL)
  76. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with depth stencil usage.");
  77. mDepthStencilSurface = TextureCore::requestView(texture, depthStencilSurface.mipLevel, 1,
  78. depthStencilSurface.face, 1, GVU_DEPTHSTENCIL);
  79. }
  80. throwIfBuffersDontMatch();
  81. assert(mColorSurface != nullptr);
  82. assert(mColorSurface->getTexture() != nullptr);
  83. SPtr<TextureCore> colorTexture = mColorSurface->getTexture();
  84. const TextureProperties& texProps = colorTexture->getProperties();
  85. if ((mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) > texProps.getNumFaces())
  86. {
  87. BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  88. toString(mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) +
  89. ". Max num faces: " + toString(texProps.getNumFaces()));
  90. }
  91. if (mColorSurface->getMostDetailedMip() > texProps.getNumMipmaps())
  92. {
  93. BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  94. toString(mColorSurface->getMostDetailedMip()) + ". Max num mipmaps: " + toString(texProps.getNumMipmaps()));
  95. }
  96. }
  97. SPtr<RenderTextureCore> RenderTextureCore::create(const RENDER_TEXTURE_CORE_DESC& desc)
  98. {
  99. return TextureCoreManager::instance().createRenderTexture(desc);
  100. }
  101. void RenderTextureCore::syncToCore(const CoreSyncData& data)
  102. {
  103. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  104. props = data.getData<RenderTextureProperties>();
  105. }
  106. const RenderTextureProperties& RenderTextureCore::getProperties() const
  107. {
  108. return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
  109. }
  110. void RenderTextureCore::throwIfBuffersDontMatch() const
  111. {
  112. if (mColorSurface == nullptr || mDepthStencilSurface == nullptr)
  113. return;
  114. const TextureProperties& colorProps = mColorSurface->getTexture()->getProperties();
  115. const TextureProperties& depthProps = mDepthStencilSurface->getTexture()->getProperties();
  116. if (colorProps.getWidth() != depthProps.getWidth() ||
  117. colorProps.getHeight() != depthProps.getHeight() ||
  118. colorProps.getMultisampleCount() != depthProps.getMultisampleCount())
  119. {
  120. String errorInfo = "\nWidth: " + toString(colorProps.getWidth()) + "/" + toString(depthProps.getWidth());
  121. errorInfo += "\nHeight: " + toString(colorProps.getHeight()) + "/" + toString(depthProps.getHeight());
  122. errorInfo += "\nMultisample Count: " + toString(colorProps.getMultisampleCount()) + "/" + toString(depthProps.getMultisampleCount());
  123. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  124. }
  125. }
  126. RenderTexturePtr RenderTexture::create(TextureType textureType, UINT32 width, UINT32 height,
  127. PixelFormat format, bool hwGamma, UINT32 multisampleCount,
  128. bool createDepth, PixelFormat depthStencilFormat)
  129. {
  130. return TextureManager::instance().createRenderTexture(textureType, width, height, format, hwGamma,
  131. multisampleCount, createDepth, depthStencilFormat);
  132. }
  133. RenderTexturePtr RenderTexture::create(const RENDER_TEXTURE_DESC& desc)
  134. {
  135. return TextureManager::instance().createRenderTexture(desc);
  136. }
  137. SPtr<RenderTextureCore> RenderTexture::getCore() const
  138. {
  139. return std::static_pointer_cast<RenderTextureCore>(mCoreSpecific);
  140. }
  141. RenderTexture::RenderTexture(const RENDER_TEXTURE_DESC& desc)
  142. {
  143. mDesc = desc;
  144. if (desc.colorSurface.texture != nullptr)
  145. mBindableColorTex = desc.colorSurface.texture;
  146. if (desc.depthStencilSurface.texture != nullptr)
  147. mBindableDepthStencilTex = desc.depthStencilSurface.texture;
  148. }
  149. SPtr<CoreObjectCore> RenderTexture::createCore() const
  150. {
  151. RENDER_TEXTURE_CORE_DESC coreDesc;
  152. if (mDesc.colorSurface.texture.isLoaded())
  153. coreDesc.colorSurface.texture = mDesc.colorSurface.texture->getCore();
  154. if (mDesc.depthStencilSurface.texture.isLoaded())
  155. coreDesc.depthStencilSurface.texture = mDesc.depthStencilSurface.texture->getCore();
  156. coreDesc.colorSurface.face = mDesc.colorSurface.face;
  157. coreDesc.colorSurface.mipLevel = mDesc.colorSurface.mipLevel;
  158. coreDesc.depthStencilSurface.face = mDesc.depthStencilSurface.face;
  159. coreDesc.depthStencilSurface.mipLevel = mDesc.depthStencilSurface.mipLevel;
  160. return TextureCoreManager::instance().createRenderTextureInternal(coreDesc);
  161. }
  162. CoreSyncData RenderTexture::syncToCore(FrameAlloc* allocator)
  163. {
  164. UINT32 size = sizeof(RenderTextureProperties);
  165. UINT8* buffer = allocator->alloc(size);
  166. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  167. memcpy(buffer, &props, size);
  168. return CoreSyncData(buffer, size);
  169. }
  170. const RenderTextureProperties& RenderTexture::getProperties() const
  171. {
  172. return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
  173. }
  174. }