BsRenderTexture.cpp 6.8 KB

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