BsRenderTexture.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. if (colorProps.getWidth() != depthProps.getWidth() ||
  124. colorProps.getHeight() != depthProps.getHeight() ||
  125. colorProps.getMultisampleCount() != depthProps.getMultisampleCount())
  126. {
  127. String errorInfo = "\nWidth: " + toString(colorProps.getWidth()) + "/" + toString(depthProps.getWidth());
  128. errorInfo += "\nHeight: " + toString(colorProps.getHeight()) + "/" + toString(depthProps.getHeight());
  129. errorInfo += "\nMultisample Count: " + toString(colorProps.getMultisampleCount()) + "/" + toString(depthProps.getMultisampleCount());
  130. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  131. }
  132. }
  133. SPtr<RenderTexture> RenderTexture::create(TextureType textureType, UINT32 width, UINT32 height,
  134. PixelFormat format, bool hwGamma, UINT32 multisampleCount,
  135. bool createDepth, PixelFormat depthStencilFormat)
  136. {
  137. return TextureManager::instance().createRenderTexture(textureType, width, height, format, hwGamma,
  138. multisampleCount, createDepth, depthStencilFormat);
  139. }
  140. SPtr<RenderTexture> RenderTexture::create(const RENDER_TEXTURE_DESC& desc)
  141. {
  142. return TextureManager::instance().createRenderTexture(desc);
  143. }
  144. SPtr<RenderTextureCore> RenderTexture::getCore() const
  145. {
  146. return std::static_pointer_cast<RenderTextureCore>(mCoreSpecific);
  147. }
  148. RenderTexture::RenderTexture(const RENDER_TEXTURE_DESC& desc)
  149. {
  150. mDesc = desc;
  151. if (desc.colorSurface.texture != nullptr)
  152. mBindableColorTex = desc.colorSurface.texture;
  153. if (desc.depthStencilSurface.texture != nullptr)
  154. mBindableDepthStencilTex = desc.depthStencilSurface.texture;
  155. }
  156. SPtr<CoreObjectCore> RenderTexture::createCore() const
  157. {
  158. RENDER_TEXTURE_CORE_DESC coreDesc;
  159. if (mDesc.colorSurface.texture.isLoaded())
  160. coreDesc.colorSurface.texture = mDesc.colorSurface.texture->getCore();
  161. if (mDesc.depthStencilSurface.texture.isLoaded())
  162. coreDesc.depthStencilSurface.texture = mDesc.depthStencilSurface.texture->getCore();
  163. coreDesc.colorSurface.face = mDesc.colorSurface.face;
  164. coreDesc.colorSurface.numFaces = mDesc.colorSurface.numFaces;
  165. coreDesc.colorSurface.mipLevel = mDesc.colorSurface.mipLevel;
  166. coreDesc.depthStencilSurface.face = mDesc.depthStencilSurface.face;
  167. coreDesc.depthStencilSurface.numFaces = mDesc.depthStencilSurface.numFaces;
  168. coreDesc.depthStencilSurface.mipLevel = mDesc.depthStencilSurface.mipLevel;
  169. return TextureCoreManager::instance().createRenderTextureInternal(coreDesc);
  170. }
  171. CoreSyncData RenderTexture::syncToCore(FrameAlloc* allocator)
  172. {
  173. UINT32 size = sizeof(RenderTextureProperties);
  174. UINT8* buffer = allocator->alloc(size);
  175. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  176. memcpy(buffer, (void*)&props, size);
  177. return CoreSyncData(buffer, size);
  178. }
  179. const RenderTextureProperties& RenderTexture::getProperties() const
  180. {
  181. return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
  182. }
  183. }