BsRenderTexture.cpp 11 KB

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