BsMultiRenderTexture.cpp 7.9 KB

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