2
0

BsRenderTexture.cpp 11 KB

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