BsRenderTexture.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsRenderTexture.h"
  4. #include "BsException.h"
  5. #include "BsTexture.h"
  6. #include "BsTextureManager.h"
  7. #include "BsResources.h"
  8. #include "BsCoreThread.h"
  9. #include "BsFrameAlloc.h"
  10. namespace BansheeEngine
  11. {
  12. RenderTextureProperties::RenderTextureProperties(const RENDER_TEXTURE_DESC& desc, bool requiresFlipping)
  13. {
  14. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  15. {
  16. HTexture texture = desc.colorSurfaces[i].texture;
  17. if (texture.isLoaded())
  18. {
  19. const TextureProperties& texProps = texture->getProperties();
  20. construct(&texProps, requiresFlipping);
  21. break;
  22. }
  23. }
  24. }
  25. RenderTextureProperties::RenderTextureProperties(const RENDER_TEXTURE_DESC_CORE& desc, bool requiresFlipping)
  26. {
  27. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  28. {
  29. SPtr<TextureCore> texture = desc.colorSurfaces[i].texture;
  30. if (texture != nullptr)
  31. {
  32. const TextureProperties& texProps = texture->getProperties();
  33. construct(&texProps, requiresFlipping);
  34. break;
  35. }
  36. }
  37. }
  38. void RenderTextureProperties::construct(const TextureProperties* textureProps, bool requiresFlipping)
  39. {
  40. if (textureProps != nullptr)
  41. {
  42. mWidth = textureProps->getWidth();
  43. mHeight = textureProps->getHeight();
  44. mColorDepth = BansheeEngine::PixelUtil::getNumElemBits(textureProps->getFormat());
  45. mHwGamma = textureProps->isHardwareGammaEnabled();
  46. mMultisampleCount = textureProps->getNumSamples();
  47. }
  48. mActive = true;
  49. mIsWindow = false;
  50. mRequiresTextureFlipping = requiresFlipping;
  51. }
  52. RenderTextureCore::RenderTextureCore(const RENDER_TEXTURE_DESC_CORE& desc, GpuDeviceFlags deviceMask)
  53. :mDesc(desc)
  54. { }
  55. RenderTextureCore::~RenderTextureCore()
  56. {
  57. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  58. {
  59. if (mColorSurfaces[i] != nullptr)
  60. TextureCore::releaseView(mColorSurfaces[i]);
  61. }
  62. if (mDepthStencilSurface != nullptr)
  63. TextureCore::releaseView(mDepthStencilSurface);
  64. }
  65. void RenderTextureCore::initialize()
  66. {
  67. RenderTargetCore::initialize();
  68. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  69. {
  70. if (mDesc.colorSurfaces[i].texture != nullptr)
  71. {
  72. SPtr<TextureCore> texture = mDesc.colorSurfaces[i].texture;
  73. if (texture->getProperties().getUsage() != TU_RENDERTARGET)
  74. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with render target usage.");
  75. mColorSurfaces[i] = TextureCore::requestView(texture, mDesc.colorSurfaces[i].mipLevel, 1,
  76. mDesc.colorSurfaces[i].face, mDesc.colorSurfaces[i].numFaces, GVU_RENDERTARGET);
  77. }
  78. }
  79. if (mDesc.depthStencilSurface.texture != nullptr)
  80. {
  81. SPtr<TextureCore> texture = mDesc.depthStencilSurface.texture;
  82. if (texture->getProperties().getUsage() != TU_DEPTHSTENCIL)
  83. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with depth stencil usage.");
  84. mDepthStencilSurface = TextureCore::requestView(texture, mDesc.depthStencilSurface.mipLevel, 1,
  85. mDesc.depthStencilSurface.face, 0, GVU_DEPTHSTENCIL);
  86. }
  87. throwIfBuffersDontMatch();
  88. }
  89. SPtr<RenderTextureCore> RenderTextureCore::create(const RENDER_TEXTURE_DESC_CORE& desc, GpuDeviceFlags deviceMask)
  90. {
  91. return TextureCoreManager::instance().createRenderTexture(desc, deviceMask);
  92. }
  93. void RenderTextureCore::syncToCore(const CoreSyncData& data)
  94. {
  95. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  96. props = data.getData<RenderTextureProperties>();
  97. }
  98. const RenderTextureProperties& RenderTextureCore::getProperties() const
  99. {
  100. return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
  101. }
  102. void RenderTextureCore::throwIfBuffersDontMatch() const
  103. {
  104. SPtr<TextureView> firstSurfaceDesc = nullptr;
  105. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  106. {
  107. if (mColorSurfaces[i] == nullptr)
  108. continue;
  109. if (firstSurfaceDesc == nullptr)
  110. {
  111. firstSurfaceDesc = mColorSurfaces[i];
  112. continue;
  113. }
  114. const TextureProperties& curTexProps = mColorSurfaces[i]->getTexture()->getProperties();
  115. const TextureProperties& firstTexProps = firstSurfaceDesc->getTexture()->getProperties();
  116. UINT32 curMsCount = curTexProps.getNumSamples();
  117. UINT32 firstMsCount = firstTexProps.getNumSamples();
  118. if (curMsCount == 0)
  119. curMsCount = 1;
  120. if (firstMsCount == 0)
  121. firstMsCount = 1;
  122. if (curTexProps.getWidth() != firstTexProps.getWidth() ||
  123. curTexProps.getHeight() != firstTexProps.getHeight() ||
  124. curMsCount != firstMsCount)
  125. {
  126. String errorInfo = "\nWidth: " + toString(curTexProps.getWidth()) + "/" + toString(firstTexProps.getWidth());
  127. errorInfo += "\nHeight: " + toString(curTexProps.getHeight()) + "/" + toString(firstTexProps.getHeight());
  128. errorInfo += "\nMultisample Count: " + toString(curMsCount) + "/" + toString(firstMsCount);
  129. BS_EXCEPT(InvalidParametersException, "Provided color textures don't match!" + errorInfo);
  130. }
  131. }
  132. if (firstSurfaceDesc != nullptr)
  133. {
  134. const TextureProperties& firstTexProps = firstSurfaceDesc->getTexture()->getProperties();
  135. UINT32 numSlices;
  136. if (firstTexProps.getTextureType() == TEX_TYPE_3D)
  137. numSlices = firstTexProps.getDepth();
  138. else
  139. numSlices = firstTexProps.getNumFaces();
  140. if ((firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) > numSlices)
  141. {
  142. BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  143. toString(firstSurfaceDesc->getFirstArraySlice() + firstSurfaceDesc->getNumArraySlices()) + ". Max num faces: " + toString(numSlices));
  144. }
  145. if (firstSurfaceDesc->getMostDetailedMip() > firstTexProps.getNumMipmaps())
  146. {
  147. BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  148. toString(firstSurfaceDesc->getMostDetailedMip()) + ". Max num mipmaps: " + toString(firstTexProps.getNumMipmaps()));
  149. }
  150. if (mDepthStencilSurface == nullptr)
  151. return;
  152. const TextureProperties& depthTexProps = mDepthStencilSurface->getTexture()->getProperties();
  153. UINT32 depthMsCount = depthTexProps.getNumSamples();
  154. UINT32 colorMsCount = firstTexProps.getNumSamples();
  155. if (depthMsCount == 0)
  156. depthMsCount = 1;
  157. if (colorMsCount == 0)
  158. colorMsCount = 1;
  159. if (depthTexProps.getWidth() != firstTexProps.getWidth() ||
  160. depthTexProps.getHeight() != firstTexProps.getHeight() ||
  161. depthMsCount != colorMsCount)
  162. {
  163. String errorInfo = "\nWidth: " + toString(depthTexProps.getWidth()) + "/" + toString(firstTexProps.getWidth());
  164. errorInfo += "\nHeight: " + toString(depthTexProps.getHeight()) + "/" + toString(firstTexProps.getHeight());
  165. errorInfo += "\nMultisample Count: " + toString(depthMsCount) + "/" + toString(colorMsCount);
  166. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  167. }
  168. }
  169. }
  170. SPtr<RenderTexture> RenderTexture::create(const TEXTURE_DESC& desc,
  171. bool createDepth, PixelFormat depthStencilFormat)
  172. {
  173. return TextureManager::instance().createRenderTexture(desc, createDepth, depthStencilFormat);
  174. }
  175. SPtr<RenderTexture> RenderTexture::create(const RENDER_TEXTURE_DESC& desc)
  176. {
  177. return TextureManager::instance().createRenderTexture(desc);
  178. }
  179. SPtr<RenderTextureCore> RenderTexture::getCore() const
  180. {
  181. return std::static_pointer_cast<RenderTextureCore>(mCoreSpecific);
  182. }
  183. RenderTexture::RenderTexture(const RENDER_TEXTURE_DESC& desc)
  184. :mDesc(desc)
  185. {
  186. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  187. {
  188. if (desc.colorSurfaces[i].texture != nullptr)
  189. mBindableColorTex[i] = desc.colorSurfaces[i].texture;
  190. }
  191. if (desc.depthStencilSurface.texture != nullptr)
  192. mBindableDepthStencilTex = desc.depthStencilSurface.texture;
  193. }
  194. SPtr<CoreObjectCore> RenderTexture::createCore() const
  195. {
  196. RENDER_TEXTURE_DESC_CORE coreDesc;
  197. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  198. {
  199. RENDER_SURFACE_DESC_CORE surfaceDesc;
  200. if (mDesc.colorSurfaces[i].texture.isLoaded())
  201. surfaceDesc.texture = mDesc.colorSurfaces[i].texture->getCore();
  202. surfaceDesc.face = mDesc.colorSurfaces[i].face;
  203. surfaceDesc.numFaces = mDesc.colorSurfaces[i].numFaces;
  204. surfaceDesc.mipLevel = mDesc.colorSurfaces[i].mipLevel;
  205. coreDesc.colorSurfaces[i] = surfaceDesc;
  206. }
  207. if (mDesc.depthStencilSurface.texture.isLoaded())
  208. coreDesc.depthStencilSurface.texture = mDesc.depthStencilSurface.texture->getCore();
  209. coreDesc.depthStencilSurface.face = mDesc.depthStencilSurface.face;
  210. coreDesc.depthStencilSurface.numFaces = mDesc.depthStencilSurface.numFaces;
  211. coreDesc.depthStencilSurface.mipLevel = mDesc.depthStencilSurface.mipLevel;
  212. return TextureCoreManager::instance().createRenderTextureInternal(coreDesc);
  213. }
  214. CoreSyncData RenderTexture::syncToCore(FrameAlloc* allocator)
  215. {
  216. UINT32 size = sizeof(RenderTextureProperties);
  217. UINT8* buffer = allocator->alloc(size);
  218. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  219. memcpy(buffer, (void*)&props, size);
  220. return CoreSyncData(buffer, size);
  221. }
  222. const RenderTextureProperties& RenderTexture::getProperties() const
  223. {
  224. return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
  225. }
  226. }