BsMultiRenderTexture.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsMultiRenderTexture.h"
  4. #include "BsTexture.h"
  5. #include "BsException.h"
  6. #include "BsDebug.h"
  7. #include "BsCoreThread.h"
  8. #include "BsTextureManager.h"
  9. #include "BsFrameAlloc.h"
  10. #include "BsResources.h"
  11. namespace BansheeEngine
  12. {
  13. MultiRenderTextureProperties::MultiRenderTextureProperties(const MULTI_RENDER_TEXTURE_DESC& desc)
  14. {
  15. for (size_t i = 0; i < desc.colorSurfaces.size(); i++)
  16. {
  17. HTexture texture = desc.colorSurfaces[i].texture;
  18. if (texture != nullptr)
  19. {
  20. const TextureProperties& texProps = texture->getProperties();
  21. construct(&texProps);
  22. break;
  23. }
  24. }
  25. }
  26. MultiRenderTextureProperties::MultiRenderTextureProperties(const MULTI_RENDER_TEXTURE_CORE_DESC& desc)
  27. {
  28. for (size_t i = 0; i < desc.colorSurfaces.size(); i++)
  29. {
  30. SPtr<TextureCore> texture = desc.colorSurfaces[i].texture;
  31. if (texture != nullptr)
  32. {
  33. const TextureProperties& texProps = texture->getProperties();
  34. construct(&texProps);
  35. break;
  36. }
  37. }
  38. }
  39. void MultiRenderTextureProperties::construct(const TextureProperties* props)
  40. {
  41. if (props == nullptr)
  42. return;
  43. mWidth = props->getWidth();
  44. mHeight = props->getHeight();
  45. mColorDepth = PixelUtil::getNumElemBits(props->getFormat());
  46. mActive = true;
  47. mHwGamma = props->isHardwareGammaEnabled();
  48. mMultisampleCount = props->getMultisampleCount();
  49. mIsWindow = false;
  50. mRequiresTextureFlipping = requiresTextureFlipping();
  51. }
  52. MultiRenderTextureCore::MultiRenderTextureCore(const MULTI_RENDER_TEXTURE_CORE_DESC& desc)
  53. :mDesc(desc)
  54. { }
  55. MultiRenderTextureCore::~MultiRenderTextureCore()
  56. {
  57. for (auto iter = mColorSurfaces.begin(); iter != mColorSurfaces.end(); ++iter)
  58. {
  59. if (*iter != nullptr)
  60. TextureCore::releaseView(*iter);
  61. }
  62. if (mDepthStencilSurface != nullptr)
  63. TextureCore::releaseView(mDepthStencilSurface);
  64. }
  65. void MultiRenderTextureCore::initialize()
  66. {
  67. RenderTargetCore::initialize();
  68. mColorSurfaces.resize(BS_MAX_MULTIPLE_RENDER_TARGETS);
  69. for (size_t i = 0; i < mDesc.colorSurfaces.size(); i++)
  70. {
  71. if (mDesc.colorSurfaces[i].texture != nullptr)
  72. {
  73. if (i >= BS_MAX_MULTIPLE_RENDER_TARGETS)
  74. {
  75. LOGWRN("Render texture index is larger than the maximum number of supported render targets. Index: " + toString((int)i) +
  76. ". Max. number of render targets: " + toString(BS_MAX_MULTIPLE_RENDER_TARGETS));
  77. continue;
  78. }
  79. SPtr<TextureCore> texture = mDesc.colorSurfaces[i].texture;
  80. if (texture->getProperties().getUsage() != TU_RENDERTARGET)
  81. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with render target usage.");
  82. mColorSurfaces[i] = TextureCore::requestView(texture, mDesc.colorSurfaces[i].mipLevel, 1,
  83. mDesc.colorSurfaces[i].face, 1, GVU_RENDERTARGET);
  84. }
  85. }
  86. if (mDesc.depthStencilSurface.texture != nullptr)
  87. {
  88. SPtr<TextureCore> texture = mDesc.depthStencilSurface.texture;
  89. if (texture->getProperties().getUsage() != TU_DEPTHSTENCIL)
  90. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with depth stencil usage.");
  91. mDepthStencilSurface = TextureCore::requestView(texture, mDesc.depthStencilSurface.mipLevel, 1,
  92. mDesc.depthStencilSurface.face, 1, GVU_DEPTHSTENCIL);
  93. }
  94. throwIfBuffersDontMatch();
  95. }
  96. void MultiRenderTextureCore::syncToCore(const CoreSyncData& data)
  97. {
  98. MultiRenderTextureProperties& props = const_cast<MultiRenderTextureProperties&>(getProperties());
  99. props = data.getData<MultiRenderTextureProperties>();
  100. }
  101. const MultiRenderTextureProperties& MultiRenderTextureCore::getProperties() const
  102. {
  103. return static_cast<const MultiRenderTextureProperties&>(getPropertiesInternal());
  104. }
  105. void MultiRenderTextureCore::throwIfBuffersDontMatch() const
  106. {
  107. TextureViewPtr firstSurfaceDesc = nullptr;
  108. for(size_t i = 0; i < mColorSurfaces.size(); i++)
  109. {
  110. if(mColorSurfaces[i] == nullptr)
  111. continue;
  112. if(firstSurfaceDesc == nullptr)
  113. {
  114. firstSurfaceDesc = mColorSurfaces[i];
  115. continue;
  116. }
  117. const TextureProperties& curTexProps = mColorSurfaces[i]->getTexture()->getProperties();
  118. const TextureProperties& firstTexProps = firstSurfaceDesc->getTexture()->getProperties();
  119. if (curTexProps.getWidth() != firstTexProps.getWidth() ||
  120. curTexProps.getHeight() != firstTexProps.getHeight() ||
  121. curTexProps.getMultisampleCount() != firstTexProps.getMultisampleCount())
  122. {
  123. String errorInfo = "\nWidth: " + toString(curTexProps.getWidth()) + "/" + toString(firstTexProps.getWidth());
  124. errorInfo += "\nHeight: " + toString(curTexProps.getHeight()) + "/" + toString(firstTexProps.getHeight());
  125. errorInfo += "\nMultisample Count: " + toString(curTexProps.getMultisampleCount()) + "/" + toString(firstTexProps.getMultisampleCount());
  126. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  127. }
  128. }
  129. if (firstSurfaceDesc != nullptr)
  130. {
  131. const TextureProperties& firstTexProps = firstSurfaceDesc->getTexture()->getProperties();
  132. if (firstTexProps.getTextureType() != TEX_TYPE_2D)
  133. BS_EXCEPT(NotImplementedException, "Render textures are currently only implemented for 2D surfaces.");
  134. if ((firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) > firstTexProps.getNumFaces())
  135. {
  136. BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  137. toString(firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) + ". Max num faces: " + toString(firstTexProps.getNumFaces()));
  138. }
  139. if (firstSurfaceDesc->getMostDetailedMip() > firstTexProps.getNumMipmaps())
  140. {
  141. BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  142. toString(firstSurfaceDesc->getMostDetailedMip()) + ". Max num mipmaps: " + toString(firstTexProps.getNumMipmaps()));
  143. }
  144. if (mDepthStencilSurface == nullptr)
  145. return;
  146. const TextureProperties& depthTexProps = mDepthStencilSurface->getTexture()->getProperties();
  147. if (depthTexProps.getWidth() != firstTexProps.getWidth() ||
  148. depthTexProps.getHeight() != firstTexProps.getHeight() ||
  149. depthTexProps.getMultisampleCount() != firstTexProps.getMultisampleCount())
  150. {
  151. String errorInfo = "\nWidth: " + toString(depthTexProps.getWidth()) + "/" + toString(firstTexProps.getWidth());
  152. errorInfo += "\nHeight: " + toString(depthTexProps.getHeight()) + "/" + toString(firstTexProps.getHeight());
  153. errorInfo += "\nMultisample Count: " + toString(depthTexProps.getMultisampleCount()) + "/" + toString(firstTexProps.getMultisampleCount());
  154. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  155. }
  156. }
  157. }
  158. void MultiRenderTextureCore::copyToMemory(PixelData &dst, FrameBuffer buffer)
  159. {
  160. throw std::exception("The method or operation is not implemented.");
  161. }
  162. MultiRenderTexture::MultiRenderTexture(const MULTI_RENDER_TEXTURE_DESC& desc)
  163. :mDesc(desc)
  164. {
  165. for (UINT32 i = 0; i < (UINT32)desc.colorSurfaces.size(); i++)
  166. {
  167. if (desc.colorSurfaces[i].texture != nullptr)
  168. mBindableColorTex.push_back(desc.colorSurfaces[i].texture);
  169. }
  170. if (desc.depthStencilSurface.texture != nullptr)
  171. mBindableDepthStencilTex = desc.depthStencilSurface.texture;
  172. }
  173. SPtr<CoreObjectCore> MultiRenderTexture::createCore() const
  174. {
  175. MULTI_RENDER_TEXTURE_CORE_DESC coreDesc;
  176. for (auto& colorSurface : mDesc.colorSurfaces)
  177. {
  178. RENDER_SURFACE_CORE_DESC surfaceDesc;
  179. if (colorSurface.texture.isLoaded())
  180. surfaceDesc.texture = colorSurface.texture->getCore();
  181. surfaceDesc.face = colorSurface.face;
  182. surfaceDesc.mipLevel = colorSurface.mipLevel;
  183. coreDesc.colorSurfaces.push_back(surfaceDesc);
  184. }
  185. if (mDesc.depthStencilSurface.texture.isLoaded())
  186. coreDesc.depthStencilSurface.texture = mDesc.depthStencilSurface.texture->getCore();
  187. coreDesc.depthStencilSurface.face = mDesc.depthStencilSurface.face;
  188. coreDesc.depthStencilSurface.mipLevel = mDesc.depthStencilSurface.mipLevel;
  189. return TextureCoreManager::instance().createMultiRenderTextureInternal(coreDesc);
  190. }
  191. SPtr<MultiRenderTextureCore> MultiRenderTexture::getCore() const
  192. {
  193. return std::static_pointer_cast<MultiRenderTextureCore>(mCoreSpecific);
  194. }
  195. MultiRenderTexturePtr MultiRenderTexture::create(const MULTI_RENDER_TEXTURE_DESC& desc)
  196. {
  197. return TextureManager::instance().createMultiRenderTexture(desc);
  198. }
  199. CoreSyncData MultiRenderTexture::syncToCore(FrameAlloc* allocator)
  200. {
  201. UINT32 size = sizeof(MultiRenderTextureProperties);
  202. UINT8* buffer = allocator->alloc(size);
  203. MultiRenderTextureProperties& props = const_cast<MultiRenderTextureProperties&>(getProperties());
  204. memcpy(buffer, &props, size);
  205. return CoreSyncData(buffer, size);
  206. }
  207. const MultiRenderTextureProperties& MultiRenderTexture::getProperties() const
  208. {
  209. return static_cast<const MultiRenderTextureProperties&>(getPropertiesInternal());
  210. }
  211. UINT32 MultiRenderTexture::getColorSurfaceCount() const
  212. {
  213. return (UINT32)mDesc.colorSurfaces.size();
  214. }
  215. }