BsRenderTexture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 = (UINT32)-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 == (UINT32)-1)
  22. firstIdx = i;
  23. requiresHwGamma |= texture->getProperties().isHardwareGammaEnabled();
  24. }
  25. if (firstIdx == (UINT32)-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 = (UINT32)-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 == (UINT32)-1)
  53. firstIdx = i;
  54. requiresHwGamma |= texture->getProperties().isHardwareGammaEnabled();
  55. }
  56. if(firstIdx == (UINT32)-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, width, height, numSlices);
  81. numSlices *= numSlices;
  82. multisampleCount = textureProps->getNumSamples();
  83. }
  84. isWindow = false;
  85. requiresTextureFlipping = requiresFlipping;
  86. this->hwGamma = hwGamma;
  87. }
  88. SPtr<RenderTexture> RenderTexture::create(const TEXTURE_DESC& desc,
  89. bool createDepth, PixelFormat depthStencilFormat)
  90. {
  91. return TextureManager::instance().createRenderTexture(desc, createDepth, depthStencilFormat);
  92. }
  93. SPtr<RenderTexture> RenderTexture::create(const RENDER_TEXTURE_DESC& desc)
  94. {
  95. return TextureManager::instance().createRenderTexture(desc);
  96. }
  97. SPtr<ct::RenderTexture> RenderTexture::getCore() const
  98. {
  99. return std::static_pointer_cast<ct::RenderTexture>(mCoreSpecific);
  100. }
  101. RenderTexture::RenderTexture(const RENDER_TEXTURE_DESC& desc)
  102. :mDesc(desc)
  103. {
  104. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  105. {
  106. if (desc.colorSurfaces[i].texture != nullptr)
  107. mBindableColorTex[i] = desc.colorSurfaces[i].texture;
  108. }
  109. if (desc.depthStencilSurface.texture != nullptr)
  110. mBindableDepthStencilTex = desc.depthStencilSurface.texture;
  111. }
  112. SPtr<ct::CoreObject> RenderTexture::createCore() const
  113. {
  114. ct::RENDER_TEXTURE_DESC coreDesc;
  115. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  116. {
  117. ct::RENDER_SURFACE_DESC surfaceDesc;
  118. if (mDesc.colorSurfaces[i].texture.isLoaded())
  119. surfaceDesc.texture = mDesc.colorSurfaces[i].texture->getCore();
  120. surfaceDesc.face = mDesc.colorSurfaces[i].face;
  121. surfaceDesc.numFaces = mDesc.colorSurfaces[i].numFaces;
  122. surfaceDesc.mipLevel = mDesc.colorSurfaces[i].mipLevel;
  123. coreDesc.colorSurfaces[i] = surfaceDesc;
  124. }
  125. if (mDesc.depthStencilSurface.texture.isLoaded())
  126. coreDesc.depthStencilSurface.texture = mDesc.depthStencilSurface.texture->getCore();
  127. coreDesc.depthStencilSurface.face = mDesc.depthStencilSurface.face;
  128. coreDesc.depthStencilSurface.numFaces = mDesc.depthStencilSurface.numFaces;
  129. coreDesc.depthStencilSurface.mipLevel = mDesc.depthStencilSurface.mipLevel;
  130. return ct::TextureManager::instance().createRenderTextureInternal(coreDesc);
  131. }
  132. CoreSyncData RenderTexture::syncToCore(FrameAlloc* allocator)
  133. {
  134. UINT32 size = sizeof(RenderTextureProperties);
  135. UINT8* buffer = allocator->alloc(size);
  136. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  137. memcpy(buffer, (void*)&props, size);
  138. return CoreSyncData(buffer, size);
  139. }
  140. const RenderTextureProperties& RenderTexture::getProperties() const
  141. {
  142. return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
  143. }
  144. namespace ct
  145. {
  146. RenderTexture::RenderTexture(const RENDER_TEXTURE_DESC& desc, UINT32 deviceIdx)
  147. :mDesc(desc)
  148. { }
  149. RenderTexture::~RenderTexture()
  150. { }
  151. void RenderTexture::initialize()
  152. {
  153. RenderTarget::initialize();
  154. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  155. {
  156. if (mDesc.colorSurfaces[i].texture != nullptr)
  157. {
  158. SPtr<Texture> texture = mDesc.colorSurfaces[i].texture;
  159. if ((texture->getProperties().getUsage() & TU_RENDERTARGET) == 0)
  160. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with render target usage.");
  161. mColorSurfaces[i] = texture->requestView(mDesc.colorSurfaces[i].mipLevel, 1,
  162. mDesc.colorSurfaces[i].face, mDesc.colorSurfaces[i].numFaces, GVU_RENDERTARGET);
  163. }
  164. }
  165. if (mDesc.depthStencilSurface.texture != nullptr)
  166. {
  167. SPtr<Texture> texture = mDesc.depthStencilSurface.texture;
  168. if ((texture->getProperties().getUsage() & TU_DEPTHSTENCIL) == 0)
  169. BS_EXCEPT(InvalidParametersException, "Provided texture is not created with depth stencil usage.");
  170. mDepthStencilSurface = texture->requestView(mDesc.depthStencilSurface.mipLevel, 1,
  171. mDesc.depthStencilSurface.face, mDesc.depthStencilSurface.numFaces, GVU_DEPTHSTENCIL);
  172. }
  173. throwIfBuffersDontMatch();
  174. }
  175. SPtr<RenderTexture> RenderTexture::create(const RENDER_TEXTURE_DESC& desc, UINT32 deviceIdx)
  176. {
  177. return TextureManager::instance().createRenderTexture(desc, deviceIdx);
  178. }
  179. void RenderTexture::syncToCore(const CoreSyncData& data)
  180. {
  181. RenderTextureProperties& props = const_cast<RenderTextureProperties&>(getProperties());
  182. props = data.getData<RenderTextureProperties>();
  183. }
  184. const RenderTextureProperties& RenderTexture::getProperties() const
  185. {
  186. return static_cast<const RenderTextureProperties&>(getPropertiesInternal());
  187. }
  188. void RenderTexture::throwIfBuffersDontMatch() const
  189. {
  190. UINT32 firstSurfaceIdx = (UINT32)-1;
  191. for (UINT32 i = 0; i < BS_MAX_MULTIPLE_RENDER_TARGETS; i++)
  192. {
  193. if (mColorSurfaces[i] == nullptr)
  194. continue;
  195. if (firstSurfaceIdx == (UINT32)-1)
  196. {
  197. firstSurfaceIdx = i;
  198. continue;
  199. }
  200. const TextureProperties& curTexProps = mDesc.colorSurfaces[i].texture->getProperties();
  201. const TextureProperties& firstTexProps = mDesc.colorSurfaces[firstSurfaceIdx].texture->getProperties();
  202. UINT32 curMsCount = curTexProps.getNumSamples();
  203. UINT32 firstMsCount = firstTexProps.getNumSamples();
  204. UINT32 curNumSlices = mColorSurfaces[i]->getNumArraySlices();
  205. UINT32 firstNumSlices = mColorSurfaces[firstSurfaceIdx]->getNumArraySlices();
  206. if (curMsCount == 0)
  207. curMsCount = 1;
  208. if (firstMsCount == 0)
  209. firstMsCount = 1;
  210. if (curTexProps.getWidth() != firstTexProps.getWidth() ||
  211. curTexProps.getHeight() != firstTexProps.getHeight() ||
  212. curTexProps.getDepth() != firstTexProps.getDepth() ||
  213. curMsCount != firstMsCount ||
  214. curNumSlices != firstNumSlices)
  215. {
  216. String errorInfo = "\nWidth: " + toString(curTexProps.getWidth()) + "/" + toString(firstTexProps.getWidth());
  217. errorInfo += "\nHeight: " + toString(curTexProps.getHeight()) + "/" + toString(firstTexProps.getHeight());
  218. errorInfo += "\nDepth: " + toString(curTexProps.getDepth()) + "/" + toString(firstTexProps.getDepth());
  219. errorInfo += "\nNum. slices: " + toString(curNumSlices) + "/" + toString(firstNumSlices);
  220. errorInfo += "\nMultisample Count: " + toString(curMsCount) + "/" + toString(firstMsCount);
  221. BS_EXCEPT(InvalidParametersException, "Provided color textures don't match!" + errorInfo);
  222. }
  223. }
  224. if (firstSurfaceIdx != (UINT32)-1)
  225. {
  226. const TextureProperties& firstTexProps = mDesc.colorSurfaces[firstSurfaceIdx].texture->getProperties();
  227. SPtr<TextureView> firstSurfaceView = mColorSurfaces[firstSurfaceIdx];
  228. UINT32 numSlices;
  229. if (firstTexProps.getTextureType() == TEX_TYPE_3D)
  230. numSlices = firstTexProps.getDepth();
  231. else
  232. numSlices = firstTexProps.getNumFaces();
  233. if ((firstSurfaceView->getFirstArraySlice() + firstSurfaceView->getNumArraySlices()) > numSlices)
  234. {
  235. BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
  236. toString(firstSurfaceView->getFirstArraySlice() + firstSurfaceView->getNumArraySlices()) + ". Max num faces: " + toString(numSlices));
  237. }
  238. if (firstSurfaceView->getMostDetailedMip() > firstTexProps.getNumMipmaps())
  239. {
  240. BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
  241. toString(firstSurfaceView->getMostDetailedMip()) + ". Max num mipmaps: " + toString(firstTexProps.getNumMipmaps()));
  242. }
  243. if (mDepthStencilSurface == nullptr)
  244. return;
  245. const TextureProperties& depthTexProps = mDesc.depthStencilSurface.texture->getProperties();
  246. UINT32 depthMsCount = depthTexProps.getNumSamples();
  247. UINT32 colorMsCount = firstTexProps.getNumSamples();
  248. if (depthMsCount == 0)
  249. depthMsCount = 1;
  250. if (colorMsCount == 0)
  251. colorMsCount = 1;
  252. if (depthTexProps.getWidth() != firstTexProps.getWidth() ||
  253. depthTexProps.getHeight() != firstTexProps.getHeight() ||
  254. depthMsCount != colorMsCount)
  255. {
  256. String errorInfo = "\nWidth: " + toString(depthTexProps.getWidth()) + "/" + toString(firstTexProps.getWidth());
  257. errorInfo += "\nHeight: " + toString(depthTexProps.getHeight()) + "/" + toString(firstTexProps.getHeight());
  258. errorInfo += "\nMultisample Count: " + toString(depthMsCount) + "/" + toString(colorMsCount);
  259. BS_EXCEPT(InvalidParametersException, "Provided texture and depth stencil buffer don't match!" + errorInfo);
  260. }
  261. }
  262. }
  263. }
  264. }