BsMultiRenderTexture.cpp 8.6 KB

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