BsRenderTexture.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "BsRenderTexture.h"
  2. #include "BsException.h"
  3. #include "BsPixelBuffer.h"
  4. #include "BsTexture.h"
  5. #include "BsTextureManager.h"
  6. #include "BsResources.h"
  7. #include "BsCoreThread.h"
  8. #include "BsFrameAlloc.h"
  9. namespace BansheeEngine
  10. {
  11. RenderTextureProperties::RenderTextureProperties(const RENDER_TEXTURE_DESC& desc, bool requiresFlipping)
  12. {
  13. HTexture texture = desc.colorSurface.texture;
  14. if (texture.isLoaded())
  15. {
  16. const TextureProperties& props = texture->getProperties();
  17. construct(&props, requiresFlipping);
  18. }
  19. else
  20. construct(nullptr, requiresFlipping);
  21. }
  22. RenderTextureProperties::RenderTextureProperties(const RENDER_TEXTURE_CORE_DESC& desc, bool requiresFlipping)
  23. {
  24. SPtr<TextureCore> texture = desc.colorSurface.texture;
  25. if (texture != nullptr)
  26. {
  27. const TextureProperties& props = texture->getProperties();
  28. construct(&props, requiresFlipping);
  29. }
  30. else
  31. construct(nullptr, requiresFlipping);
  32. }
  33. void RenderTextureProperties::construct(const TextureProperties* textureProps, bool requiresFlipping)
  34. {
  35. if (textureProps != nullptr)
  36. {
  37. mWidth = textureProps->getWidth();
  38. mHeight = textureProps->getHeight();
  39. mColorDepth = BansheeEngine::PixelUtil::getNumElemBits(textureProps->getFormat());
  40. mHwGamma = textureProps->isHardwareGammaEnabled();
  41. mMultisampleCount = textureProps->getMultisampleCount();
  42. }
  43. mActive = true;
  44. mIsWindow = false;
  45. mRequiresTextureFlipping = requiresFlipping;
  46. }
  47. RenderTextureCore::RenderTextureCore(const RENDER_TEXTURE_CORE_DESC& desc)
  48. :mColorSurface(nullptr), mDepthStencilSurface(nullptr), mDesc(desc)
  49. { }
  50. RenderTextureCore::~RenderTextureCore()
  51. {
  52. if (mColorSurface != nullptr)
  53. TextureCore::releaseView(mColorSurface);
  54. if (mDepthStencilSurface != nullptr)
  55. TextureCore::releaseView(mDepthStencilSurface);
  56. }
  57. void RenderTextureCore::initialize()
  58. {
  59. RenderTargetCore::initialize();
  60. const RENDER_SURFACE_CORE_DESC& colorSurface = mDesc.colorSurface;
  61. if (colorSurface.texture != nullptr)
  62. {
  63. SPtr<TextureCore> texture = colorSurface.texture;
  64. if (texture->getProperties().getUsage() != TU_RENDERTARGET)
  65. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with render target usage.");
  66. mColorSurface = TextureCore::requestView(texture, colorSurface.mipLevel, 1,
  67. colorSurface.face, 1, GVU_RENDERTARGET);
  68. }
  69. const RENDER_SURFACE_CORE_DESC& depthStencilSurface = mDesc.depthStencilSurface;
  70. if (depthStencilSurface.texture != nullptr)
  71. {
  72. SPtr<TextureCore> texture = depthStencilSurface.texture;
  73. if (texture->getProperties().getUsage() != TU_DEPTHSTENCIL)
  74. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with depth stencil usage.");
  75. mDepthStencilSurface = TextureCore::requestView(texture, depthStencilSurface.mipLevel, 1,
  76. depthStencilSurface.face, 1, GVU_DEPTHSTENCIL);
  77. }
  78. throwIfBuffersDontMatch();
  79. assert(mColorSurface != nullptr);
  80. assert(mColorSurface->getTexture() != nullptr);
  81. SPtr<TextureCore> colorTexture = mColorSurface->getTexture();
  82. const TextureProperties& texProps = colorTexture->getProperties();
  83. if ((mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) > texProps.getNumFaces())
  84. {
  85. BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  86. toString(mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) +
  87. ". Max num faces: " + toString(texProps.getNumFaces()));
  88. }
  89. if (mColorSurface->getMostDetailedMip() > texProps.getNumMipmaps())
  90. {
  91. BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  92. toString(mColorSurface->getMostDetailedMip()) + ". Max num mipmaps: " + toString(texProps.getNumMipmaps()));
  93. }
  94. }
  95. void RenderTextureCore::syncToCore(const CoreSyncData& data)
  96. {
  97. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  98. props = data.getData<RenderTextureProperties>();
  99. }
  100. const RenderTextureProperties& RenderTextureCore::getProperties() const
  101. {
  102. return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
  103. }
  104. void RenderTextureCore::throwIfBuffersDontMatch() const
  105. {
  106. if (mColorSurface == nullptr || mDepthStencilSurface == nullptr)
  107. return;
  108. const TextureProperties& colorProps = mColorSurface->getTexture()->getProperties();
  109. const TextureProperties& depthProps = mDepthStencilSurface->getTexture()->getProperties();
  110. if (colorProps.getWidth() != depthProps.getWidth() ||
  111. colorProps.getHeight() != depthProps.getHeight() ||
  112. colorProps.getMultisampleCount() != depthProps.getMultisampleCount())
  113. {
  114. String errorInfo = "\nWidth: " + toString(colorProps.getWidth()) + "/" + toString(depthProps.getWidth());
  115. errorInfo += "\nHeight: " + toString(colorProps.getHeight()) + "/" + toString(depthProps.getHeight());
  116. errorInfo += "\nMultisample Count: " + toString(colorProps.getMultisampleCount()) + "/" + toString(depthProps.getMultisampleCount());
  117. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  118. }
  119. }
  120. RenderTexturePtr RenderTexture::create(TextureType textureType, UINT32 width, UINT32 height,
  121. PixelFormat format, bool hwGamma, UINT32 multisampleCount,
  122. bool createDepth, PixelFormat depthStencilFormat)
  123. {
  124. return TextureManager::instance().createRenderTexture(textureType, width, height, format, hwGamma,
  125. multisampleCount, createDepth, depthStencilFormat);
  126. }
  127. RenderTexturePtr RenderTexture::create(const RENDER_TEXTURE_DESC& desc)
  128. {
  129. return TextureManager::instance().createRenderTexture(desc);
  130. }
  131. SPtr<RenderTextureCore> RenderTexture::getCore() const
  132. {
  133. return std::static_pointer_cast<RenderTextureCore>(mCoreSpecific);
  134. }
  135. RenderTexture::RenderTexture(const RENDER_TEXTURE_DESC& desc)
  136. {
  137. mDesc = desc;
  138. if (desc.colorSurface.texture != nullptr)
  139. mBindableColorTex = desc.colorSurface.texture;
  140. if (desc.depthStencilSurface.texture != nullptr)
  141. mBindableDepthStencilTex = desc.depthStencilSurface.texture;
  142. }
  143. SPtr<CoreObjectCore> RenderTexture::createCore() const
  144. {
  145. RENDER_TEXTURE_CORE_DESC coreDesc;
  146. if (mDesc.colorSurface.texture.isLoaded())
  147. coreDesc.colorSurface.texture = mDesc.colorSurface.texture->getCore();
  148. if (mDesc.depthStencilSurface.texture.isLoaded())
  149. coreDesc.depthStencilSurface.texture = mDesc.depthStencilSurface.texture->getCore();
  150. coreDesc.colorSurface.face = mDesc.colorSurface.face;
  151. coreDesc.colorSurface.mipLevel = mDesc.colorSurface.mipLevel;
  152. coreDesc.depthStencilSurface.face = mDesc.depthStencilSurface.face;
  153. coreDesc.depthStencilSurface.mipLevel = mDesc.depthStencilSurface.mipLevel;
  154. return TextureCoreManager::instance().createRenderTextureInternal(coreDesc);
  155. }
  156. CoreSyncData RenderTexture::syncToCore(FrameAlloc* allocator)
  157. {
  158. UINT32 size = sizeof(RenderTextureProperties);
  159. UINT8* buffer = allocator->alloc(size);
  160. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  161. memcpy(buffer, &props, size);
  162. return CoreSyncData(buffer, size);
  163. }
  164. const RenderTextureProperties& RenderTexture::getProperties() const
  165. {
  166. return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
  167. }
  168. }