BsRenderTexture.cpp 9.8 KB

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