BsPostProcessing.cpp 15 KB

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