BsRenderTexture.cpp 9.8 KB

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