BsMultiRenderTexture.cpp 7.8 KB

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