BsPostProcessing.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPostProcessing.h"
  4. #include "BsRenderTexture.h"
  5. #include "BsRenderTexturePool.h"
  6. #include "BsRendererUtility.h"
  7. #include "BsTextureManager.h"
  8. #include "BsCamera.h"
  9. namespace BansheeEngine
  10. {
  11. DownsampleMat::DownsampleMat()
  12. {
  13. mMaterial->setParamBlockBuffer("Input", mParams.getBuffer());
  14. mInputTexture = mMaterial->getParamTexture("gInputTex");
  15. mInvTexSize = mMaterial->getParamVec2("gInvTexSize");
  16. }
  17. void DownsampleMat::_initDefines(ShaderDefines& defines)
  18. {
  19. // Do nothing
  20. }
  21. void DownsampleMat::execute(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo)
  22. {
  23. // Set parameters
  24. SPtr<TextureCore> colorTexture = target->getBindableColorTexture();
  25. mInputTexture.set(colorTexture);
  26. const RenderTextureProperties& rtProps = target->getProperties();
  27. Vector2 invTextureSize(1.0f / rtProps.getWidth(), 1.0f / rtProps.getHeight());
  28. mParams.gInvTexSize.set(invTextureSize);
  29. // Set output
  30. const TextureProperties& colorProps = colorTexture->getProperties();
  31. UINT32 width = std::max(1, Math::ceilToInt(colorProps.getWidth() * 0.5f));
  32. UINT32 height = std::max(1, Math::ceilToInt(colorProps.getHeight() * 0.5f));
  33. mOutputDesc = POOLED_RENDER_TEXTURE_DESC::create2D(colorProps.getFormat(), width, height, TU_RENDERTARGET);
  34. // Render
  35. ppInfo.downsampledSceneTex = RenderTexturePool::instance().get(mOutputDesc);
  36. RenderAPICore& rapi = RenderAPICore::instance();
  37. rapi.setRenderTarget(ppInfo.downsampledSceneTex->renderTexture, true);
  38. gRendererUtility().setPass(mMaterial, 0);
  39. gRendererUtility().drawScreenQuad();
  40. rapi.setRenderTarget(nullptr);
  41. mOutput = ppInfo.downsampledSceneTex->renderTexture;
  42. }
  43. void DownsampleMat::release(PostProcessInfo& ppInfo)
  44. {
  45. RenderTexturePool::instance().release(ppInfo.downsampledSceneTex);
  46. mOutput = nullptr;
  47. }
  48. EyeAdaptHistogramMat::EyeAdaptHistogramMat()
  49. {
  50. mMaterial->setParamBlockBuffer("Input", mParams.getBuffer());
  51. mSceneColor = mMaterial->getParamTexture("gSceneColorTex");
  52. mOutputTex = mMaterial->getParamLoadStoreTexture("gOutputTex");
  53. }
  54. void EyeAdaptHistogramMat::_initDefines(ShaderDefines& defines)
  55. {
  56. defines.set("THREADGROUP_SIZE_X", THREAD_GROUP_SIZE_X);
  57. defines.set("THREADGROUP_SIZE_Y", THREAD_GROUP_SIZE_Y);
  58. defines.set("LOOP_COUNT_X", LOOP_COUNT_X);
  59. defines.set("LOOP_COUNT_Y", LOOP_COUNT_Y);
  60. }
  61. void EyeAdaptHistogramMat::execute(PostProcessInfo& ppInfo)
  62. {
  63. // Set parameters
  64. SPtr<RenderTextureCore> target = ppInfo.downsampledSceneTex->renderTexture;
  65. mSceneColor.set(ppInfo.downsampledSceneTex->texture);
  66. const RenderTextureProperties& props = target->getProperties();
  67. int offsetAndSize[4] = { 0, 0, (INT32)props.getWidth(), (INT32)props.getHeight() };
  68. mParams.gHistogramParams.set(getHistogramScaleOffset(ppInfo));
  69. mParams.gPixelOffsetAndSize.set(Vector4I(offsetAndSize));
  70. Vector2I threadGroupCount = getThreadGroupCount(target);
  71. mParams.gThreadGroupCount.set(threadGroupCount);
  72. // Set output
  73. UINT32 numHistograms = threadGroupCount.x * threadGroupCount.y;
  74. mOutputDesc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT16_RGBA, HISTOGRAM_NUM_TEXELS, numHistograms,
  75. TU_LOADSTORE);
  76. // Dispatch
  77. ppInfo.histogramTex = RenderTexturePool::instance().get(mOutputDesc);
  78. mOutputTex.set(ppInfo.histogramTex->texture);
  79. RenderAPICore& rapi = RenderAPICore::instance();
  80. gRendererUtility().setComputePass(mMaterial);
  81. rapi.dispatchCompute(threadGroupCount.x, threadGroupCount.y);
  82. // Note: This is ugly, add a better way to clear load/store textures?
  83. TextureSurface blankSurface;
  84. rapi.setLoadStoreTexture(GPT_COMPUTE_PROGRAM, 0, false, nullptr, blankSurface);
  85. mOutput = ppInfo.histogramTex->renderTexture;
  86. }
  87. void EyeAdaptHistogramMat::release(PostProcessInfo& ppInfo)
  88. {
  89. RenderTexturePool::instance().release(ppInfo.histogramTex);
  90. mOutput = nullptr;
  91. }
  92. Vector2I EyeAdaptHistogramMat::getThreadGroupCount(const SPtr<RenderTextureCore>& target)
  93. {
  94. const UINT32 texelsPerThreadGroupX = THREAD_GROUP_SIZE_X * LOOP_COUNT_X;
  95. const UINT32 texelsPerThreadGroupY = THREAD_GROUP_SIZE_Y * LOOP_COUNT_Y;
  96. const RenderTextureProperties& props = target->getProperties();
  97. Vector2I threadGroupCount;
  98. threadGroupCount.x = ((INT32)props.getWidth() + texelsPerThreadGroupX - 1) / texelsPerThreadGroupX;
  99. threadGroupCount.y = ((INT32)props.getHeight() + texelsPerThreadGroupY - 1) / texelsPerThreadGroupY;
  100. return threadGroupCount;
  101. }
  102. Vector2 EyeAdaptHistogramMat::getHistogramScaleOffset(const PostProcessInfo& ppInfo)
  103. {
  104. const PostProcessSettings& settings = ppInfo.settings;
  105. float diff = settings.autoExposure.histogramLog2Max - settings.autoExposure.histogramLog2Min;
  106. float scale = 1.0f / diff;
  107. float offset = -settings.autoExposure.histogramLog2Min * scale;
  108. return Vector2(scale, offset);
  109. }
  110. EyeAdaptHistogramReduceMat::EyeAdaptHistogramReduceMat()
  111. {
  112. mMaterial->setParamBlockBuffer("Input", mParams.getBuffer());
  113. mHistogramTex = mMaterial->getParamTexture("gHistogramTex");
  114. mEyeAdaptationTex = mMaterial->getParamTexture("gEyeAdaptationTex");
  115. }
  116. void EyeAdaptHistogramReduceMat::_initDefines(ShaderDefines& defines)
  117. {
  118. // Do nothing
  119. }
  120. void EyeAdaptHistogramReduceMat::execute(PostProcessInfo& ppInfo)
  121. {
  122. // Set parameters
  123. mHistogramTex.set(ppInfo.histogramTex->texture);
  124. SPtr<PooledRenderTexture> eyeAdaptationRT = ppInfo.eyeAdaptationTex[ppInfo.lastEyeAdaptationTex];
  125. SPtr<TextureCore> eyeAdaptationTex;
  126. if (eyeAdaptationRT != nullptr) // Could be that this is the first run
  127. eyeAdaptationTex = eyeAdaptationRT->texture;
  128. else
  129. eyeAdaptationTex = TextureCore::WHITE;
  130. mEyeAdaptationTex.set(eyeAdaptationTex);
  131. Vector2I threadGroupCount = EyeAdaptHistogramMat::getThreadGroupCount(ppInfo.downsampledSceneTex->renderTexture);
  132. UINT32 numHistograms = threadGroupCount.x * threadGroupCount.y;
  133. mParams.gThreadGroupCount.set(numHistograms);
  134. // Set output
  135. mOutputDesc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT16_RGBA, EyeAdaptHistogramMat::HISTOGRAM_NUM_TEXELS, 2,
  136. TU_RENDERTARGET);
  137. // Render
  138. ppInfo.histogramReduceTex = RenderTexturePool::instance().get(mOutputDesc);
  139. RenderAPICore& rapi = RenderAPICore::instance();
  140. rapi.setRenderTarget(ppInfo.histogramReduceTex->renderTexture, true);
  141. gRendererUtility().setPass(mMaterial, 0);
  142. Rect2 drawUV(0.0f, 0.0f, (float)EyeAdaptHistogramMat::HISTOGRAM_NUM_TEXELS, 2.0f);
  143. gRendererUtility().drawScreenQuad(drawUV);
  144. rapi.setRenderTarget(nullptr);
  145. mOutput = ppInfo.histogramReduceTex->renderTexture;
  146. }
  147. void EyeAdaptHistogramReduceMat::release(PostProcessInfo& ppInfo)
  148. {
  149. RenderTexturePool::instance().release(ppInfo.histogramReduceTex);
  150. mOutput = nullptr;
  151. }
  152. EyeAdaptationMat::EyeAdaptationMat()
  153. {
  154. mMaterial->setParamBlockBuffer("Input", mParams.getBuffer());
  155. mReducedHistogramTex = mMaterial->getParamTexture("gHistogramTex");
  156. }
  157. void EyeAdaptationMat::_initDefines(ShaderDefines& defines)
  158. {
  159. defines.set("THREADGROUP_SIZE_X", EyeAdaptHistogramMat::THREAD_GROUP_SIZE_X);
  160. defines.set("THREADGROUP_SIZE_Y", EyeAdaptHistogramMat::THREAD_GROUP_SIZE_Y);
  161. }
  162. void EyeAdaptationMat::execute(PostProcessInfo& ppInfo, float frameDelta)
  163. {
  164. bool texturesInitialized = ppInfo.eyeAdaptationTex[0] != nullptr && ppInfo.eyeAdaptationTex[1] != nullptr;
  165. if(!texturesInitialized)
  166. {
  167. POOLED_RENDER_TEXTURE_DESC outputDesc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT32_R, 1, 1, TU_RENDERTARGET);
  168. ppInfo.eyeAdaptationTex[0] = RenderTexturePool::instance().get(outputDesc);
  169. ppInfo.eyeAdaptationTex[1] = RenderTexturePool::instance().get(outputDesc);
  170. }
  171. ppInfo.lastEyeAdaptationTex = (ppInfo.lastEyeAdaptationTex + 1) % 2; // TODO - Do I really need two targets?
  172. // Set parameters
  173. mReducedHistogramTex.set(ppInfo.histogramReduceTex->texture);
  174. Vector2 histogramScaleAndOffset = EyeAdaptHistogramMat::getHistogramScaleOffset(ppInfo);
  175. const PostProcessSettings& settings = ppInfo.settings;
  176. Vector4 eyeAdaptationParams[3];
  177. eyeAdaptationParams[0].x = histogramScaleAndOffset.x;
  178. eyeAdaptationParams[0].y = histogramScaleAndOffset.y;
  179. float histogramPctHigh = Math::clamp01(settings.autoExposure.histogramPctHigh);
  180. eyeAdaptationParams[0].z = std::min(Math::clamp01(settings.autoExposure.histogramPctLow), histogramPctHigh);
  181. eyeAdaptationParams[0].w = histogramPctHigh;
  182. eyeAdaptationParams[1].x = std::min(settings.autoExposure.minEyeAdaptation, settings.autoExposure.maxEyeAdaptation);
  183. eyeAdaptationParams[1].y = settings.autoExposure.maxEyeAdaptation;
  184. eyeAdaptationParams[1].z = settings.autoExposure.eyeAdaptationSpeedUp;
  185. eyeAdaptationParams[1].w = settings.autoExposure.eyeAdaptationSpeedDown;
  186. eyeAdaptationParams[2].x = Math::pow(2.0f, settings.exposureScale);
  187. eyeAdaptationParams[2].y = frameDelta;
  188. mParams.gEyeAdaptationParams.set(eyeAdaptationParams[0], 0);
  189. mParams.gEyeAdaptationParams.set(eyeAdaptationParams[1], 1);
  190. mParams.gEyeAdaptationParams.set(eyeAdaptationParams[2], 2);
  191. // Render
  192. SPtr<PooledRenderTexture> eyeAdaptationRT = ppInfo.eyeAdaptationTex[ppInfo.lastEyeAdaptationTex];
  193. RenderAPICore& rapi = RenderAPICore::instance();
  194. rapi.setRenderTarget(eyeAdaptationRT->renderTexture, true);
  195. gRendererUtility().setPass(mMaterial, 0);
  196. gRendererUtility().drawScreenQuad();
  197. rapi.setRenderTarget(nullptr);
  198. }
  199. CreateTonemapLUTMat::CreateTonemapLUTMat()
  200. {
  201. mMaterial->setParamBlockBuffer("Input", mParams.getBuffer());
  202. mMaterial->setParamBlockBuffer("WhiteBalanceInput", mWhiteBalanceParams.getBuffer());
  203. }
  204. void CreateTonemapLUTMat::_initDefines(ShaderDefines& defines)
  205. {
  206. defines.set("LUT_SIZE", LUT_SIZE);
  207. }
  208. void CreateTonemapLUTMat::execute(PostProcessInfo& ppInfo)
  209. {
  210. // Set parameters
  211. mParams.gGammaAdjustment.set(2.2f / ppInfo.settings.gamma);
  212. // Note: Assuming sRGB (PC monitor) for now, change to Rec.709 when running on console (value 1), or to raw 2.2
  213. // gamma when running on Mac (value 2)
  214. mParams.gGammaCorrectionType.set(0);
  215. Vector4 tonemapParams[2];
  216. tonemapParams[0].x = ppInfo.settings.tonemapping.filmicCurveShoulderStrength;
  217. tonemapParams[0].y = ppInfo.settings.tonemapping.filmicCurveLinearStrength;
  218. tonemapParams[0].z = ppInfo.settings.tonemapping.filmicCurveLinearAngle;
  219. tonemapParams[0].w = ppInfo.settings.tonemapping.filmicCurveToeStrength;
  220. tonemapParams[1].x = ppInfo.settings.tonemapping.filmicCurveToeNumerator;
  221. tonemapParams[1].y = ppInfo.settings.tonemapping.filmicCurveToeDenominator;
  222. tonemapParams[1].z = ppInfo.settings.tonemapping.filmicCurveLinearWhitePoint;
  223. tonemapParams[1].w = 0.0f; // Unused
  224. mParams.gTonemapParams.set(tonemapParams[0], 0);
  225. mParams.gTonemapParams.set(tonemapParams[1], 1);
  226. // Set color grading params
  227. mParams.gSaturation.set(ppInfo.settings.colorGrading.saturation);
  228. mParams.gContrast.set(ppInfo.settings.colorGrading.contrast);
  229. mParams.gGain.set(ppInfo.settings.colorGrading.gain);
  230. mParams.gOffset.set(ppInfo.settings.colorGrading.offset);
  231. // Set white balance params
  232. mWhiteBalanceParams.gWhiteTemp.set(ppInfo.settings.whiteBalance.temperature);
  233. mWhiteBalanceParams.gWhiteOffset.set(ppInfo.settings.whiteBalance.tint);
  234. // Set output
  235. POOLED_RENDER_TEXTURE_DESC outputDesc = POOLED_RENDER_TEXTURE_DESC::create3D(PF_B8G8R8X8,
  236. LUT_SIZE, LUT_SIZE, LUT_SIZE, TU_RENDERTARGET);
  237. // Render
  238. ppInfo.colorLUT = RenderTexturePool::instance().get(outputDesc);
  239. RenderAPICore& rapi = RenderAPICore::instance();
  240. rapi.setRenderTarget(ppInfo.colorLUT->renderTexture);
  241. gRendererUtility().setPass(mMaterial, 0);
  242. gRendererUtility().drawScreenQuad(LUT_SIZE);
  243. }
  244. void CreateTonemapLUTMat::release(PostProcessInfo& ppInfo)
  245. {
  246. RenderTexturePool::instance().release(ppInfo.colorLUT);
  247. }
  248. template<bool GammaOnly, bool AutoExposure>
  249. TonemappingMat<GammaOnly, AutoExposure>::TonemappingMat()
  250. {
  251. mMaterial->setParamBlockBuffer("Input", mParams.getBuffer());
  252. mInputTex = mMaterial->getParamTexture("gInputTex");
  253. mColorLUT = mMaterial->getParamTexture("gColorLUT");
  254. mEyeAdaptationTex = mMaterial->getParamTexture("gEyeAdaptationTex");
  255. }
  256. template<bool GammaOnly, bool AutoExposure>
  257. void TonemappingMat<GammaOnly, AutoExposure>::_initDefines(ShaderDefines& defines)
  258. {
  259. if(GammaOnly)
  260. defines.set("GAMMA_ONLY", 1);
  261. if (AutoExposure)
  262. defines.set("AUTO_EXPOSURE", 1);
  263. defines.set("LUT_SIZE", CreateTonemapLUTMat::LUT_SIZE);
  264. }
  265. template<bool GammaOnly, bool AutoExposure>
  266. void TonemappingMat<GammaOnly, AutoExposure>::execute(const SPtr<RenderTextureCore>& sceneColor, const SPtr<ViewportCore>& outputViewport,
  267. PostProcessInfo& ppInfo)
  268. {
  269. mParams.gRawGamma.set(1.0f / ppInfo.settings.gamma);
  270. mParams.gManualExposureScale.set(Math::pow(2.0f, ppInfo.settings.exposureScale));
  271. // Set parameters
  272. SPtr<TextureCore> colorTexture = sceneColor->getBindableColorTexture();
  273. mInputTex.set(colorTexture);
  274. SPtr<TextureCore> colorLUT;
  275. if(ppInfo.colorLUT != nullptr)
  276. colorLUT = ppInfo.colorLUT->texture;
  277. mColorLUT.set(colorLUT);
  278. SPtr<TextureCore> eyeAdaptationTexture;
  279. if(ppInfo.eyeAdaptationTex[ppInfo.lastEyeAdaptationTex] != nullptr)
  280. eyeAdaptationTexture = ppInfo.eyeAdaptationTex[ppInfo.lastEyeAdaptationTex]->texture;
  281. mEyeAdaptationTex.set(eyeAdaptationTexture);
  282. // Render
  283. RenderAPICore& rapi = RenderAPICore::instance();
  284. SPtr<RenderTargetCore> target = outputViewport->getTarget();
  285. rapi.setRenderTarget(target);
  286. rapi.setViewport(outputViewport->getNormArea());
  287. gRendererUtility().setPass(mMaterial, 0);
  288. gRendererUtility().drawScreenQuad();
  289. }
  290. template class TonemappingMat<true, true>;
  291. template class TonemappingMat<false, true>;
  292. template class TonemappingMat<true, false>;
  293. template class TonemappingMat<false, false>;
  294. void PostProcessing::postProcess(const SPtr<RenderTextureCore>& sceneColor, const CameraCore* camera,
  295. PostProcessInfo& ppInfo, float frameDelta)
  296. {
  297. SPtr<ViewportCore> outputViewport = camera->getViewport();
  298. bool hdr = camera->getFlags().isSet(CameraFlag::HDR);
  299. if(hdr && ppInfo.settings.enableAutoExposure)
  300. {
  301. mDownsample.execute(sceneColor, ppInfo);
  302. mEyeAdaptHistogram.execute(ppInfo);
  303. mDownsample.release(ppInfo);
  304. mEyeAdaptHistogramReduce.execute(ppInfo);
  305. mEyeAdaptHistogram.release(ppInfo);
  306. mEyeAdaptation.execute(ppInfo, frameDelta);
  307. mEyeAdaptHistogramReduce.release(ppInfo);
  308. }
  309. if (hdr && ppInfo.settings.enableTonemapping)
  310. {
  311. if (ppInfo.settingDirty) // Rebuild LUT if PP settings changed
  312. mCreateLUT.execute(ppInfo);
  313. if (ppInfo.settings.enableAutoExposure)
  314. mTonemapping_AE.execute(sceneColor, outputViewport, ppInfo);
  315. else
  316. mTonemapping.execute(sceneColor, outputViewport, ppInfo);
  317. }
  318. else
  319. {
  320. if (hdr && ppInfo.settings.enableAutoExposure)
  321. mTonemapping_AE_GO.execute(sceneColor, outputViewport, ppInfo);
  322. else
  323. mTonemapping_GO.execute(sceneColor, outputViewport, ppInfo);
  324. }
  325. if (ppInfo.settingDirty)
  326. ppInfo.settingDirty = false;
  327. // TODO - External code depends on the main RT being bound when this exits, make this clearer
  328. }
  329. }