瀏覽代碼

WIP post processing
- Added wrappers for downsampling and eye adaptation histogram shaders

BearishSun 9 年之前
父節點
當前提交
b56646eb65

+ 2 - 0
Build/VS2015/RenderBeast.vcxproj

@@ -258,6 +258,7 @@
   </ItemDefinitionGroup>
   <ItemGroup>
     <ClInclude Include="..\..\Source\RenderBeast\Include\BsLightRendering.h" />
+    <ClInclude Include="..\..\Source\RenderBeast\Include\BsPostProcessing.h" />
     <ClInclude Include="..\..\Source\RenderBeast\Include\BsStaticRenderableHandler.h" />
     <ClInclude Include="..\..\Source\RenderBeast\Include\BsRenderBeast.h" />
     <ClInclude Include="..\..\Source\RenderBeast\Include\BsRenderBeastFactory.h" />
@@ -269,6 +270,7 @@
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="..\..\Source\RenderBeast\Source\BsLightRendering.cpp" />
+    <ClCompile Include="..\..\Source\RenderBeast\Source\BsPostProcessing.cpp" />
     <ClCompile Include="..\..\Source\RenderBeast\Source\BsStaticRenderableHandler.cpp" />
     <ClCompile Include="..\..\Source\RenderBeast\Source\BsRenderBeast.cpp" />
     <ClCompile Include="..\..\Source\RenderBeast\Source\BsRenderBeastFactory.cpp" />

+ 6 - 0
Build/VS2015/RenderBeast.vcxproj.filters

@@ -38,6 +38,9 @@
     <ClInclude Include="..\..\Source\RenderBeast\Include\BsLightRendering.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\Source\RenderBeast\Include\BsPostProcessing.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="..\..\Source\RenderBeast\Source\BsRenderTexturePool.cpp">
@@ -64,5 +67,8 @@
     <ClCompile Include="..\..\Source\RenderBeast\Source\BsLightRendering.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\Source\RenderBeast\Source\BsPostProcessing.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 93 - 0
Source/RenderBeast/Include/BsPostProcessing.h

@@ -0,0 +1,93 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#pragma once
+
+#include "BsRenderBeastPrerequisites.h"
+#include "BsRendererMaterial.h"
+#include "BsParamBlocks.h"
+
+namespace BansheeEngine
+{
+	/** @addtogroup RenderBeast
+	 *  @{
+	 */
+
+	/** Settings that control the post-process operation. */
+	struct PostProcessSettings
+	{
+		/** 
+		 * Determines minimum luminance value in the eye adaptation histogram. In log2 units (-8 = 1/256). In the range
+		 * [-16, 0]. 
+		 */
+		float histogramLog2Min = -8.0f; 
+
+		/** 
+		 * Determines maximum luminance value in the eye adaptation histogram. In log2 units (4 = 16). In the range
+		 * [0, 16]. 
+		 */
+		float histogramLog2Max = 4.0f;
+	};
+
+	BS_PARAM_BLOCK_BEGIN(DownsampleParams)
+		BS_PARAM_BLOCK_ENTRY(Vector2, gInvTexSize)
+	BS_PARAM_BLOCK_END
+
+	/** Shader that downsamples a texture to half its size. */
+	class DownsampleMat : public RendererMaterial<DownsampleMat>
+	{
+		RMAT_DEF("PPDownsample.bsl");
+
+	public:
+		DownsampleMat();
+
+		/** Updates the parameter buffers used by the material. */
+		void setParameters(const SPtr<RenderTextureCore>& target);
+	private:
+		DownsampleParams mParams;
+		MaterialParamVec2Core mInvTexSize;
+		MaterialParamTextureCore mInputTexture;
+	};
+
+	BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramParams)
+		BS_PARAM_BLOCK_ENTRY(Vector4I, gPixelOffsetAndSize)
+		BS_PARAM_BLOCK_ENTRY(Vector2, gHistogramParams)
+		BS_PARAM_BLOCK_ENTRY(Vector2I, gThreadGroupCount)
+	BS_PARAM_BLOCK_END
+
+	/** Shader that creates a luminance histogram used for eye adaptation. */
+	class EyeAdaptHistogramMat : public RendererMaterial<EyeAdaptHistogramMat>
+	{
+		RMAT_DEF("PPEyeAdaptHistogram.bsl");
+
+	public:
+		EyeAdaptHistogramMat();
+
+		/** Updates the parameter buffers used by the material. */
+		void setParameters(const SPtr<RenderTextureCore>& target, const PostProcessSettings& settings);
+	private:
+		EyeAdaptHistogramParams mParams;
+		MaterialParamTextureCore mSceneColor;
+		MaterialParamLoadStoreTextureCore mOutputTex;
+
+		static const INT32 THREAD_GROUP_SIZE_X = 4;
+		static const INT32 THREAD_GROUP_SIZE_Y = 4;
+		static const INT32 LOOP_COUNT_X = 8;
+		static const INT32 LOOP_COUNT_Y = 8;
+	};
+
+	/**
+	 * Renders post-processing effects for the provided render target.
+	 *
+	 * @note	Core thread only.
+	 */
+	class BS_BSRND_EXPORT PostProcessing
+	{
+	public:
+		/** Renders post-processing effects for the provided render target. */
+		static void postProcess(const SPtr<RenderTextureCore>& target, const PostProcessSettings& settings);
+		
+	private:
+	};
+
+	/** @} */
+}

+ 83 - 0
Source/RenderBeast/Source/BsPostProcessing.cpp

@@ -0,0 +1,83 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#include "BsPostProcessing.h"
+#include "BsRenderTexture.h"
+
+namespace BansheeEngine
+{
+	DownsampleMat::DownsampleMat()
+	{
+		mMaterial->setParamBlockBuffer("Input", mParams.getBuffer());
+
+		mInputTexture = mMaterial->getParamTexture("gInputTex");
+		mInvTexSize = mMaterial->getParamVec2("gInvTexSize");
+	}
+
+	void DownsampleMat::_initDefines(ShaderDefines& defines)
+	{
+		// Do nothing
+	}
+
+	void DownsampleMat::setParameters(const SPtr<RenderTextureCore>& target)
+	{
+		mInputTexture.set(target->getBindableColorTexture());
+
+		const RenderTextureProperties& props = target->getProperties();
+		Vector2 invTextureSize(1.0f / props.getWidth(), 1.0f / props.getHeight());
+
+		mParams.gInvTexSize.set(invTextureSize);
+
+		// TODO - Output
+	}
+
+	EyeAdaptHistogramMat::EyeAdaptHistogramMat()
+	{
+		mMaterial->setParamBlockBuffer("Input", mParams.getBuffer());
+
+		mSceneColor = mMaterial->getParamTexture("gSceneColorTex");
+		mOutputTex = mMaterial->getParamLoadStoreTexture("gOutputTex");
+	}
+
+	void EyeAdaptHistogramMat::_initDefines(ShaderDefines& defines)
+	{
+		defines.set("THREADGROUP_SIZE_X", THREAD_GROUP_SIZE_X);
+		defines.set("THREADGROUP_SIZE_Y", THREAD_GROUP_SIZE_Y);
+		defines.set("LOOP_COUNT_X", LOOP_COUNT_X);
+		defines.set("LOOP_COUNT_Y", LOOP_COUNT_Y);
+	}
+
+	void EyeAdaptHistogramMat::setParameters(const SPtr<RenderTextureCore>& target, const PostProcessSettings& settings)
+	{
+		mSceneColor.set(target->getBindableColorTexture());
+
+		float diff = settings.histogramLog2Max - settings.histogramLog2Min;
+		float scale = 1.0f / diff;
+		float offset = -settings.histogramLog2Min * scale;
+
+		const RenderTextureProperties& props = target->getProperties();
+		int offsetAndSize[4] = { 0, 0, (INT32)props.getWidth(), (INT32)props.getHeight() };
+
+		mParams.gHistogramParams.set(Vector2(scale, offset));
+		mParams.gPixelOffsetAndSize.set(Vector4I(offsetAndSize));
+
+		const UINT32 texelsPerThreadGroupX = THREAD_GROUP_SIZE_X * LOOP_COUNT_X;
+		const UINT32 texelsPerThreadGroupY = THREAD_GROUP_SIZE_Y * LOOP_COUNT_Y;
+
+		Vector2I threadGroupCount;
+		threadGroupCount.x = ((INT32)props.getWidth() + texelsPerThreadGroupX - 1) / texelsPerThreadGroupX;
+		threadGroupCount.y = ((INT32)props.getHeight() + texelsPerThreadGroupY - 1) / texelsPerThreadGroupY;
+
+		mParams.gThreadGroupCount.set(threadGroupCount);
+
+		// TODO - Output
+	}
+
+	void PostProcessing::postProcess(const SPtr<RenderTextureCore>& target, const PostProcessSettings& settings)
+	{
+		RenderAPICore& rapi = RenderAPICore::instance();
+
+
+
+		// TODO - Downsample
+	}
+}