BsRenderTexture.cpp 7.6 KB

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