BsMultiRenderTexture.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. CoreSyncData MultiRenderTextureCore::syncFromCore(FrameAlloc* allocator)
  95. {
  96. UINT32 size = sizeof(MultiRenderTextureProperties);
  97. UINT8* buffer = allocator->alloc(size);
  98. MultiRenderTextureProperties& props = const_cast<MultiRenderTextureProperties&>(getProperties());
  99. memcpy(buffer, &props, size);
  100. return CoreSyncData(buffer, size);
  101. }
  102. void MultiRenderTextureCore::syncToCore(const CoreSyncData& data)
  103. {
  104. MultiRenderTextureProperties& props = const_cast<MultiRenderTextureProperties&>(getProperties());
  105. props = data.getData<MultiRenderTextureProperties>();
  106. }
  107. const MultiRenderTextureProperties& MultiRenderTextureCore::getProperties() const
  108. {
  109. return static_cast<const MultiRenderTextureProperties&>(getPropertiesInternal());
  110. }
  111. void MultiRenderTextureCore::throwIfBuffersDontMatch() const
  112. {
  113. TextureViewPtr firstSurfaceDesc = nullptr;
  114. for(size_t i = 0; i < mColorSurfaces.size(); i++)
  115. {
  116. if(mColorSurfaces[i] == nullptr)
  117. continue;
  118. if(firstSurfaceDesc == nullptr)
  119. {
  120. firstSurfaceDesc = mColorSurfaces[i];
  121. continue;
  122. }
  123. const TextureProperties& curTexProps = mColorSurfaces[i]->getTexture()->getProperties();
  124. const TextureProperties& firstTexProps = firstSurfaceDesc->getTexture()->getProperties();
  125. if (curTexProps.getWidth() != firstTexProps.getWidth() ||
  126. curTexProps.getHeight() != firstTexProps.getHeight() ||
  127. curTexProps.getMultisampleCount() != firstTexProps.getMultisampleCount())
  128. {
  129. String errorInfo = "\nWidth: " + toString(curTexProps.getWidth()) + "/" + toString(firstTexProps.getWidth());
  130. errorInfo += "\nHeight: " + toString(curTexProps.getHeight()) + "/" + toString(firstTexProps.getHeight());
  131. errorInfo += "\nMultisample Count: " + toString(curTexProps.getMultisampleCount()) + "/" + toString(firstTexProps.getMultisampleCount());
  132. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  133. }
  134. }
  135. if (firstSurfaceDesc != nullptr)
  136. {
  137. const TextureProperties& firstTexProps = firstSurfaceDesc->getTexture()->getProperties();
  138. if (firstTexProps.getTextureType() != TEX_TYPE_2D)
  139. BS_EXCEPT(NotImplementedException, "Render textures are currently only implemented for 2D surfaces.");
  140. if ((firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) >= firstTexProps.getNumFaces())
  141. {
  142. BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  143. toString(firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) + ". Max num faces: " + toString(firstTexProps.getNumFaces()));
  144. }
  145. if (firstSurfaceDesc->getMostDetailedMip() >= firstTexProps.getNumMipmaps())
  146. {
  147. BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  148. toString(firstSurfaceDesc->getMostDetailedMip()) + ". Max num mipmaps: " + toString(firstTexProps.getNumMipmaps()));
  149. }
  150. if (mDepthStencilSurface == nullptr)
  151. return;
  152. const TextureProperties& depthTexProps = mDepthStencilSurface->getTexture()->getProperties();
  153. if (depthTexProps.getWidth() != firstTexProps.getWidth() ||
  154. depthTexProps.getHeight() != firstTexProps.getHeight() ||
  155. depthTexProps.getMultisampleCount() != firstTexProps.getMultisampleCount())
  156. {
  157. String errorInfo = "\nWidth: " + toString(depthTexProps.getWidth()) + "/" + toString(firstTexProps.getWidth());
  158. errorInfo += "\nHeight: " + toString(depthTexProps.getHeight()) + "/" + toString(firstTexProps.getHeight());
  159. errorInfo += "\nMultisample Count: " + toString(depthTexProps.getMultisampleCount()) + "/" + toString(firstTexProps.getMultisampleCount());
  160. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  161. }
  162. }
  163. }
  164. void MultiRenderTextureCore::copyToMemory(PixelData &dst, FrameBuffer buffer)
  165. {
  166. throw std::exception("The method or operation is not implemented.");
  167. }
  168. MultiRenderTexture::MultiRenderTexture(const MULTI_RENDER_TEXTURE_DESC& desc)
  169. :mDesc(desc)
  170. {
  171. for (UINT32 i = 0; i < (UINT32)desc.colorSurfaces.size(); i++)
  172. {
  173. if (desc.colorSurfaces[i].texture != nullptr)
  174. mBindableColorTex.push_back(desc.colorSurfaces[i].texture);
  175. }
  176. if (desc.depthStencilSurface.texture != nullptr)
  177. mBindableDepthStencilTex = desc.depthStencilSurface.texture;
  178. }
  179. SPtr<CoreObjectCore> MultiRenderTexture::createCore() const
  180. {
  181. MULTI_RENDER_TEXTURE_CORE_DESC coreDesc;
  182. for (auto& colorSurface : mDesc.colorSurfaces)
  183. {
  184. RENDER_SURFACE_CORE_DESC surfaceDesc;
  185. if (colorSurface.texture.isLoaded())
  186. surfaceDesc.texture = colorSurface.texture->getCore();
  187. surfaceDesc.face = colorSurface.face;
  188. surfaceDesc.mipLevel = colorSurface.mipLevel;
  189. coreDesc.colorSurfaces.push_back(surfaceDesc);
  190. }
  191. if (mDesc.depthStencilSurface.texture.isLoaded())
  192. coreDesc.depthStencilSurface.texture = mDesc.depthStencilSurface.texture->getCore();
  193. coreDesc.depthStencilSurface.face = mDesc.depthStencilSurface.face;
  194. coreDesc.depthStencilSurface.mipLevel = mDesc.depthStencilSurface.mipLevel;
  195. return TextureCoreManager::instance().createMultiRenderTextureInternal(coreDesc);
  196. }
  197. SPtr<MultiRenderTextureCore> MultiRenderTexture::getCore() const
  198. {
  199. return std::static_pointer_cast<MultiRenderTextureCore>(mCoreSpecific);
  200. }
  201. MultiRenderTexturePtr MultiRenderTexture::create(const MULTI_RENDER_TEXTURE_DESC& desc)
  202. {
  203. return TextureManager::instance().createMultiRenderTexture(desc);
  204. }
  205. CoreSyncData MultiRenderTexture::syncToCore(FrameAlloc* allocator)
  206. {
  207. UINT32 size = sizeof(MultiRenderTextureProperties);
  208. UINT8* buffer = allocator->alloc(size);
  209. MultiRenderTextureProperties& props = const_cast<MultiRenderTextureProperties&>(getProperties());
  210. memcpy(buffer, &props, size);
  211. return CoreSyncData(buffer, size);
  212. }
  213. void MultiRenderTexture::syncFromCore(const CoreSyncData& data)
  214. {
  215. MultiRenderTextureProperties& props = const_cast<MultiRenderTextureProperties&>(getProperties());
  216. props = data.getData<MultiRenderTextureProperties>();
  217. }
  218. const MultiRenderTextureProperties& MultiRenderTexture::getProperties() const
  219. {
  220. return static_cast<const MultiRenderTextureProperties&>(getPropertiesInternal());
  221. }
  222. UINT32 MultiRenderTexture::getColorSurfaceCount() const
  223. {
  224. return (UINT32)mDesc.colorSurfaces.size();
  225. }
  226. }