2
0

BsRenderTexture.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. TexturePtr texture = desc.colorSurface.texture;
  14. if (texture != nullptr)
  15. {
  16. const TextureProperties& props = texture->getProperties();
  17. mWidth = props.getWidth();
  18. mHeight = props.getHeight();
  19. mColorDepth = BansheeEngine::PixelUtil::getNumElemBits(props.getFormat());
  20. mHwGamma = props.isHardwareGammaEnabled();
  21. mMultisampleCount = props.getMultisampleCount();
  22. }
  23. mActive = true;
  24. mIsWindow = false;
  25. mRequiresTextureFlipping = requiresFlipping;
  26. }
  27. RenderTextureCore::RenderTextureCore(const RENDER_TEXTURE_DESC& desc)
  28. :mColorSurface(nullptr), mDepthStencilSurface(nullptr), mDesc(desc)
  29. { }
  30. RenderTextureCore::~RenderTextureCore()
  31. { }
  32. void RenderTextureCore::initialize()
  33. {
  34. RenderTargetCore::initialize();
  35. const RENDER_SURFACE_DESC& colorSurface = mDesc.colorSurface;
  36. if (colorSurface.texture != nullptr)
  37. {
  38. SPtr<TextureCore> texture = colorSurface.texture->getCore();
  39. if (texture->getProperties().getUsage() != TU_RENDERTARGET)
  40. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with render target usage.");
  41. mColorSurface = TextureCore::requestView(texture, colorSurface.mipLevel, 1,
  42. colorSurface.face, 1, GVU_RENDERTARGET);
  43. }
  44. const RENDER_SURFACE_DESC& depthStencilSurface = mDesc.depthStencilSurface;
  45. if (depthStencilSurface.texture != nullptr)
  46. {
  47. SPtr<TextureCore> texture = depthStencilSurface.texture->getCore();
  48. if (texture->getProperties().getUsage() != TU_DEPTHSTENCIL)
  49. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with depth stencil usage.");
  50. mDepthStencilSurface = TextureCore::requestView(texture, depthStencilSurface.mipLevel, 1,
  51. depthStencilSurface.face, 1, GVU_DEPTHSTENCIL);
  52. }
  53. throwIfBuffersDontMatch();
  54. assert(mColorSurface != nullptr);
  55. assert(mColorSurface->getTexture() != nullptr);
  56. SPtr<TextureCore> colorTexture = mColorSurface->getTexture();
  57. const TextureProperties& texProps = colorTexture->getProperties();
  58. if ((mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) > texProps.getNumFaces())
  59. {
  60. BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  61. toString(mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) +
  62. ". Max num faces: " + toString(texProps.getNumFaces()));
  63. }
  64. if (mColorSurface->getMostDetailedMip() > texProps.getNumMipmaps())
  65. {
  66. BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  67. toString(mColorSurface->getMostDetailedMip()) + ". Max num mipmaps: " + toString(texProps.getNumMipmaps()));
  68. }
  69. }
  70. void RenderTextureCore::destroy()
  71. {
  72. if (mColorSurface != nullptr)
  73. TextureCore::releaseView(mColorSurface);
  74. if (mDepthStencilSurface != nullptr)
  75. TextureCore::releaseView(mDepthStencilSurface);
  76. RenderTargetCore::destroy();
  77. }
  78. CoreSyncData RenderTextureCore::syncFromCore(FrameAlloc* allocator)
  79. {
  80. UINT32 size = sizeof(RenderTextureProperties);
  81. UINT8* buffer = allocator->alloc(size);
  82. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  83. memcpy(buffer, &props, size);
  84. return CoreSyncData(buffer, size);
  85. }
  86. void RenderTextureCore::syncToCore(const CoreSyncData& data)
  87. {
  88. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  89. props = data.getData<RenderTextureProperties>();
  90. }
  91. const RenderTextureProperties& RenderTextureCore::getProperties() const
  92. {
  93. return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
  94. }
  95. void RenderTextureCore::throwIfBuffersDontMatch() const
  96. {
  97. if (mColorSurface == nullptr || mDepthStencilSurface == nullptr)
  98. return;
  99. const TextureProperties& colorProps = mColorSurface->getTexture()->getProperties();
  100. const TextureProperties& depthProps = mDepthStencilSurface->getTexture()->getProperties();
  101. if (colorProps.getWidth() != depthProps.getWidth() ||
  102. colorProps.getHeight() != depthProps.getHeight() ||
  103. colorProps.getMultisampleCount() != depthProps.getMultisampleCount())
  104. {
  105. String errorInfo = "\nWidth: " + toString(colorProps.getWidth()) + "/" + toString(depthProps.getWidth());
  106. errorInfo += "\nHeight: " + toString(colorProps.getHeight()) + "/" + toString(depthProps.getHeight());
  107. errorInfo += "\nMultisample Count: " + toString(colorProps.getMultisampleCount()) + "/" + toString(depthProps.getMultisampleCount());
  108. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  109. }
  110. }
  111. RenderTexturePtr RenderTexture::create(TextureType textureType, UINT32 width, UINT32 height,
  112. PixelFormat format, bool hwGamma, UINT32 multisampleCount,
  113. bool createDepth, PixelFormat depthStencilFormat)
  114. {
  115. return TextureManager::instance().createRenderTexture(textureType, width, height, format, hwGamma,
  116. multisampleCount, createDepth, depthStencilFormat);
  117. }
  118. RenderTexturePtr RenderTexture::create(const RENDER_TEXTURE_DESC& desc)
  119. {
  120. return TextureManager::instance().createRenderTexture(desc);
  121. }
  122. SPtr<RenderTextureCore> RenderTexture::getCore() const
  123. {
  124. return std::static_pointer_cast<RenderTextureCore>(mCoreSpecific);
  125. }
  126. RenderTexture::RenderTexture(const RENDER_TEXTURE_DESC& desc)
  127. {
  128. mDesc = desc;
  129. // Create non-persistent resource handles for the used textures (we only need them because a lot of the code accepts only handles,
  130. // since they're non persistent they don't really have any benefit over shared pointers)
  131. if (desc.colorSurface.texture != nullptr)
  132. mBindableColorTex = static_resource_cast<Texture>(gResources()._createResourceHandle(desc.colorSurface.texture));
  133. if (desc.depthStencilSurface.texture != nullptr)
  134. mBindableDepthStencilTex = static_resource_cast<Texture>(gResources()._createResourceHandle(desc.depthStencilSurface.texture));
  135. }
  136. SPtr<CoreObjectCore> RenderTexture::createCore() const
  137. {
  138. return TextureCoreManager::instance().createRenderTextureInternal(mDesc);
  139. }
  140. CoreSyncData RenderTexture::syncToCore(FrameAlloc* allocator)
  141. {
  142. UINT32 size = sizeof(RenderTextureProperties);
  143. UINT8* buffer = allocator->alloc(size);
  144. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  145. memcpy(buffer, &props, size);
  146. return CoreSyncData(buffer, size);
  147. }
  148. void RenderTexture::syncFromCore(const CoreSyncData& data)
  149. {
  150. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  151. props = data.getData<RenderTextureProperties>();
  152. }
  153. const RenderTextureProperties& RenderTexture::getProperties() const
  154. {
  155. return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
  156. }
  157. }