2
0

BsMultiRenderTexture.cpp 7.9 KB

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