BsMultiRenderTexture.cpp 8.8 KB

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