BsRenderTexture.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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, colorSurface.numFaces, 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, 0, GVU_DEPTHSTENCIL);
  79. }
  80. throwIfBuffersDontMatch();
  81. if (mColorSurface != nullptr)
  82. {
  83. assert(mColorSurface->getTexture() != nullptr);
  84. SPtr<TextureCore> colorTexture = mColorSurface->getTexture();
  85. const TextureProperties& texProps = colorTexture->getProperties();
  86. UINT32 numSlices;
  87. if (texProps.getTextureType() == TEX_TYPE_3D)
  88. numSlices = texProps.getDepth();
  89. else
  90. numSlices = texProps.getNumFaces();
  91. if ((mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) > numSlices)
  92. {
  93. BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  94. toString(mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) +
  95. ". Max num faces: " + toString(numSlices));
  96. }
  97. if (mColorSurface->getMostDetailedMip() > texProps.getNumMipmaps())
  98. {
  99. BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  100. toString(mColorSurface->getMostDetailedMip()) + ". Max num mipmaps: " + toString(texProps.getNumMipmaps()));
  101. }
  102. }
  103. }
  104. SPtr<RenderTextureCore> RenderTextureCore::create(const RENDER_TEXTURE_CORE_DESC& desc)
  105. {
  106. return TextureCoreManager::instance().createRenderTexture(desc);
  107. }
  108. void RenderTextureCore::syncToCore(const CoreSyncData& data)
  109. {
  110. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  111. props = data.getData<RenderTextureProperties>();
  112. }
  113. const RenderTextureProperties& RenderTextureCore::getProperties() const
  114. {
  115. return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
  116. }
  117. void RenderTextureCore::throwIfBuffersDontMatch() const
  118. {
  119. if (mColorSurface == nullptr || mDepthStencilSurface == nullptr)
  120. return;
  121. const TextureProperties& colorProps = mColorSurface->getTexture()->getProperties();
  122. const TextureProperties& depthProps = mDepthStencilSurface->getTexture()->getProperties();
  123. UINT32 colorMsCount = colorProps.getMultisampleCount();
  124. UINT32 depthMsCount = depthProps.getMultisampleCount();
  125. if (colorMsCount == 0)
  126. colorMsCount = 1;
  127. if (depthMsCount == 0)
  128. depthMsCount = 1;
  129. if (colorProps.getWidth() != depthProps.getWidth() ||
  130. colorProps.getHeight() != depthProps.getHeight() ||
  131. colorMsCount != depthMsCount)
  132. {
  133. String errorInfo = "\nWidth: " + toString(colorProps.getWidth()) + "/" + toString(depthProps.getWidth());
  134. errorInfo += "\nHeight: " + toString(colorProps.getHeight()) + "/" + toString(depthProps.getHeight());
  135. errorInfo += "\nMultisample Count: " + toString(colorMsCount) + "/" + toString(depthMsCount);
  136. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  137. }
  138. }
  139. SPtr<RenderTexture> RenderTexture::create(TextureType textureType, UINT32 width, UINT32 height,
  140. PixelFormat format, bool hwGamma, UINT32 multisampleCount,
  141. bool createDepth, PixelFormat depthStencilFormat)
  142. {
  143. return TextureManager::instance().createRenderTexture(textureType, width, height, format, hwGamma,
  144. multisampleCount, createDepth, depthStencilFormat);
  145. }
  146. SPtr<RenderTexture> RenderTexture::create(const RENDER_TEXTURE_DESC& desc)
  147. {
  148. return TextureManager::instance().createRenderTexture(desc);
  149. }
  150. SPtr<RenderTextureCore> RenderTexture::getCore() const
  151. {
  152. return std::static_pointer_cast<RenderTextureCore>(mCoreSpecific);
  153. }
  154. RenderTexture::RenderTexture(const RENDER_TEXTURE_DESC& desc)
  155. {
  156. mDesc = desc;
  157. if (desc.colorSurface.texture != nullptr)
  158. mBindableColorTex = desc.colorSurface.texture;
  159. if (desc.depthStencilSurface.texture != nullptr)
  160. mBindableDepthStencilTex = desc.depthStencilSurface.texture;
  161. }
  162. SPtr<CoreObjectCore> RenderTexture::createCore() const
  163. {
  164. RENDER_TEXTURE_CORE_DESC coreDesc;
  165. if (mDesc.colorSurface.texture.isLoaded())
  166. coreDesc.colorSurface.texture = mDesc.colorSurface.texture->getCore();
  167. if (mDesc.depthStencilSurface.texture.isLoaded())
  168. coreDesc.depthStencilSurface.texture = mDesc.depthStencilSurface.texture->getCore();
  169. coreDesc.colorSurface.face = mDesc.colorSurface.face;
  170. coreDesc.colorSurface.numFaces = mDesc.colorSurface.numFaces;
  171. coreDesc.colorSurface.mipLevel = mDesc.colorSurface.mipLevel;
  172. coreDesc.depthStencilSurface.face = mDesc.depthStencilSurface.face;
  173. coreDesc.depthStencilSurface.numFaces = mDesc.depthStencilSurface.numFaces;
  174. coreDesc.depthStencilSurface.mipLevel = mDesc.depthStencilSurface.mipLevel;
  175. return TextureCoreManager::instance().createRenderTextureInternal(coreDesc);
  176. }
  177. CoreSyncData RenderTexture::syncToCore(FrameAlloc* allocator)
  178. {
  179. UINT32 size = sizeof(RenderTextureProperties);
  180. UINT8* buffer = allocator->alloc(size);
  181. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  182. memcpy(buffer, (void*)&props, size);
  183. return CoreSyncData(buffer, size);
  184. }
  185. const RenderTextureProperties& RenderTexture::getProperties() const
  186. {
  187. return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
  188. }
  189. }