瀏覽代碼

WIP post processing

BearishSun 9 年之前
父節點
當前提交
80aaee5f4d

+ 9 - 9
Documentation/Manuals/Native/manuals.md

@@ -2,28 +2,28 @@ Manuals									{#manuals}
 ===============
 ===============
 [TOC]
 [TOC]
 
 
-Here you will find a list of all manuals relating to Banshee's native code and their short descriptions. The manuals are for the most part unrelated so you can read them in the order that interests you.
+Here you will find a list of all manuals relating to Banshee's native code, primarily focusing on explaining the engine internals instead of the user-facing code. The manuals are roughly ordered in the way they should be read, although if you are interested in a specific topic feel free to skip ahead as manuals often contain cross references to prerequisite manuals.
 
 
 Name                                      | Description
 Name                                      | Description
 ------------------------------------------|-------------
 ------------------------------------------|-------------
 [Getting started](@ref gettingStarted)    | Shows how to perform some basic operations using Banshee's user-facing interface. Allows you to get a general idea of how Banshee works.
 [Getting started](@ref gettingStarted)    | Shows how to perform some basic operations using Banshee's user-facing interface. Allows you to get a general idea of how Banshee works.
 [Architecture](@ref architecture)         | Gives you an overview of the entire architecture of Banshee. Useful starting point for those modifying engine internals as it provides a way to identify major systems.
 [Architecture](@ref architecture)         | Gives you an overview of the entire architecture of Banshee. Useful starting point for those modifying engine internals as it provides a way to identify major systems.
-[Custom GUI elements](@ref customGUI)     | Shows you how to create custom GUI elements, manually render text or modify GUI system in a general way.
-[Custom importers](@ref customImporters)  | Shows you how to create importers that handle conversion of third party resources into engine ready formats.
-[Custom plugins](@ref customPlugins)      | Shows you how to create custom plugins that can be loaded by Banshee.
-[Custom renderer](@ref customRenderer)    | Shows you how to create a custom renderer so you may fully customize the look of your application.
-[Resources](@ref resources)  			  | Explains how resources work, including saving, loading and creating brand new resource types.
+[Utilities](@ref utilities)               | Provides an overview of a variety of utility systems used throughout Banshee.
 [Core thread](@ref coreThread)            | Explains how core (rendering) thread works, how it interacts with simulation thread, what are core objects and how to create your own.
 [Core thread](@ref coreThread)            | Explains how core (rendering) thread works, how it interacts with simulation thread, what are core objects and how to create your own.
-[Game objects](@ref gameObjects)          | Explains what are scene objects and components and how can you use them to create your scene.
+[Resources](@ref resources)  			  | Explains how resources work, including saving, loading and creating brand new resource types.
 [RTTI](@ref rtti)                         | Shows you how to add run-time type information for your objects.
 [RTTI](@ref rtti)                         | Shows you how to add run-time type information for your objects.
-[Scripting](@ref scripting)               | Shows you how to interact with the scripting system, and how to expose C++ objects to the scripting API.
+[Game objects](@ref gameObjects)          | Explains what are scene objects and components and how can you use them to create your scene.
 [Render API](@ref renderAPI)              | Explains how to use the render API to use the graphics pipeline and draw objects. 
 [Render API](@ref renderAPI)              | Explains how to use the render API to use the graphics pipeline and draw objects. 
 [Textures](@ref textures)                 | Shows you how to create, use and manipulate textures.
 [Textures](@ref textures)                 | Shows you how to create, use and manipulate textures.
 [Meshes](@ref meshes)                     | Shows you how to create, use and manipulate meshes.
 [Meshes](@ref meshes)                     | Shows you how to create, use and manipulate meshes.
 [Render targets](@ref renderTargets)	  | Shows you how to create and use render textures and windows.
 [Render targets](@ref renderTargets)	  | Shows you how to create and use render textures and windows.
 [GPU programs](@ref gpuPrograms)		  | Shows you how to create and use GPU programs.
 [GPU programs](@ref gpuPrograms)		  | Shows you how to create and use GPU programs.
 [Materials](@ref materials)				  | Shows you how to create and use materials and shaders.
 [Materials](@ref materials)				  | Shows you how to create and use materials and shaders.
-[Utilities](@ref utilities)               | Provides an overview of a variety of utility systems used throughout Banshee.
+[Scripting](@ref scripting)               | Shows you how to interact with the scripting system, and how to expose C++ objects to the scripting API.
+[Custom GUI elements](@ref customGUI)     | Shows you how to create custom GUI elements, manually render text or modify GUI system in a general way.
+[Custom importers](@ref customImporters)  | Shows you how to create importers that handle conversion of third party resources into engine ready formats.
+[Custom plugins](@ref customPlugins)      | Shows you how to create custom plugins that can be loaded by Banshee.
+[Custom renderer](@ref customRenderer)    | Shows you how to create a custom renderer so you may fully customize the look of your application.
 [Quick reference](@ref quickref)          | Provides a few bits of commonly required information, that might be hard to remember otherwise.
 [Quick reference](@ref quickref)          | Provides a few bits of commonly required information, that might be hard to remember otherwise.
 [Porting](@ref porting)                   | Information about how to go on about porting Banshee to a different operating system.
 [Porting](@ref porting)                   | Information about how to go on about porting Banshee to a different operating system.
 [Code style](@ref style)                  | Information about code style used when writing Banshee.
 [Code style](@ref style)                  | Information about code style used when writing Banshee.

+ 1 - 1
Source/BansheeCore/Include/BsMaterialParam.h

@@ -204,7 +204,7 @@ namespace BansheeEngine
 		TMaterialParamLoadStoreTexture() { }
 		TMaterialParamLoadStoreTexture() { }
 
 
 		/** @copydoc GpuParamLoadStoreTexture::set */
 		/** @copydoc GpuParamLoadStoreTexture::set */
-		void set(const SPtr<TextureCore>& texture, const TextureSurface& surface);
+		void set(const SPtr<TextureCore>& texture, const TextureSurface& surface = TextureSurface());
 
 
 		/** @copydoc GpuParamLoadStoreTexture::get */
 		/** @copydoc GpuParamLoadStoreTexture::get */
 		SPtr<TextureCore> get();
 		SPtr<TextureCore> get();

+ 18 - 16
Source/BansheeCore/Source/BsRenderTexture.cpp

@@ -96,23 +96,25 @@ namespace BansheeEngine
 
 
 		throwIfBuffersDontMatch();
 		throwIfBuffersDontMatch();
 
 
-		assert(mColorSurface != nullptr);
-		assert(mColorSurface->getTexture() != nullptr);
-
-		SPtr<TextureCore> colorTexture = mColorSurface->getTexture();
-		const TextureProperties& texProps = colorTexture->getProperties();
-
-		if ((mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) > texProps.getNumFaces())
-		{
-			BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
-				toString(mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) +
-				". Max num faces: " + toString(texProps.getNumFaces()));
-		}
-
-		if (mColorSurface->getMostDetailedMip() > texProps.getNumMipmaps())
+		if (mColorSurface != nullptr)
 		{
 		{
-			BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
-				toString(mColorSurface->getMostDetailedMip()) + ". Max num mipmaps: " + toString(texProps.getNumMipmaps()));
+			assert(mColorSurface->getTexture() != nullptr);
+
+			SPtr<TextureCore> colorTexture = mColorSurface->getTexture();
+			const TextureProperties& texProps = colorTexture->getProperties();
+
+			if ((mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) > texProps.getNumFaces())
+			{
+				BS_EXCEPT(InvalidParametersException, "Provided number of faces is out of range. Face: " +
+					toString(mColorSurface->getFirstArraySlice() + mColorSurface->getNumArraySlices()) +
+					". Max num faces: " + toString(texProps.getNumFaces()));
+			}
+
+			if (mColorSurface->getMostDetailedMip() > texProps.getNumMipmaps())
+			{
+				BS_EXCEPT(InvalidParametersException, "Provided number of mip maps is out of range. Mip level: " +
+					toString(mColorSurface->getMostDetailedMip()) + ". Max num mipmaps: " + toString(texProps.getNumMipmaps()));
+			}
 		}
 		}
 	}
 	}
 
 

+ 23 - 8
Source/RenderBeast/Include/BsPostProcessing.h

@@ -51,17 +51,21 @@ namespace BansheeEngine
 	public:
 	public:
 		DownsampleMat();
 		DownsampleMat();
 
 
-		/** Updates the parameter buffers used by the material. */
-		void setParameters(const SPtr<RenderTextureCore>& target);
+		/** Renders the post-process effect with the provided parameters. */
+		void execute(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo);
 
 
-		/** Renders the post-process effect on the provided target. */
-		void render(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo);
+		/** Releases the output render target. */
+		void release(PostProcessInfo& ppInfo);
+
+		/** Returns the render texture where the output will be written. */
+		SPtr<RenderTextureCore> getOutput() const { return mOutput; }
 	private:
 	private:
 		DownsampleParams mParams;
 		DownsampleParams mParams;
 		MaterialParamVec2Core mInvTexSize;
 		MaterialParamVec2Core mInvTexSize;
 		MaterialParamTextureCore mInputTexture;
 		MaterialParamTextureCore mInputTexture;
 
 
 		POOLED_RENDER_TEXTURE_DESC mOutputDesc;
 		POOLED_RENDER_TEXTURE_DESC mOutputDesc;
+		SPtr<RenderTextureCore> mOutput;
 	};
 	};
 
 
 	BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramParams)
 	BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramParams)
@@ -78,13 +82,22 @@ namespace BansheeEngine
 	public:
 	public:
 		EyeAdaptHistogramMat();
 		EyeAdaptHistogramMat();
 
 
-		/** Updates the parameter buffers used by the material. */
-		void setParameters(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo);
+		/** Executes the post-process effect with the provided parameters. */
+		void execute(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo);
+
+		/** Releases the output render target. */
+		void release(PostProcessInfo& ppInfo);
+
+		/** Returns the render texture where the output was written. */
+		SPtr<RenderTextureCore> getOutput() const { return mOutput; }
 	private:
 	private:
 		EyeAdaptHistogramParams mParams;
 		EyeAdaptHistogramParams mParams;
 		MaterialParamTextureCore mSceneColor;
 		MaterialParamTextureCore mSceneColor;
 		MaterialParamLoadStoreTextureCore mOutputTex;
 		MaterialParamLoadStoreTextureCore mOutputTex;
 
 
+		POOLED_RENDER_TEXTURE_DESC mOutputDesc;
+		SPtr<RenderTextureCore> mOutput;
+
 		static const INT32 THREAD_GROUP_SIZE_X = 4;
 		static const INT32 THREAD_GROUP_SIZE_X = 4;
 		static const INT32 THREAD_GROUP_SIZE_Y = 4;
 		static const INT32 THREAD_GROUP_SIZE_Y = 4;
 		static const INT32 LOOP_COUNT_X = 8;
 		static const INT32 LOOP_COUNT_X = 8;
@@ -96,13 +109,15 @@ namespace BansheeEngine
 	 *
 	 *
 	 * @note	Core thread only.
 	 * @note	Core thread only.
 	 */
 	 */
-	class BS_BSRND_EXPORT PostProcessing
+	class BS_BSRND_EXPORT PostProcessing : public Module<PostProcessing>
 	{
 	{
 	public:
 	public:
 		/** Renders post-processing effects for the provided render target. */
 		/** Renders post-processing effects for the provided render target. */
-		static void postProcess(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo);
+		void postProcess(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo);
 		
 		
 	private:
 	private:
+		DownsampleMat mDownsample;
+		EyeAdaptHistogramMat mEyeAdaptHistogram;
 	};
 	};
 
 
 	/** @} */
 	/** @} */

+ 1 - 0
Source/RenderBeast/Include/BsRenderTexturePool.h

@@ -23,6 +23,7 @@ namespace BansheeEngine
 		~PooledRenderTexture();
 		~PooledRenderTexture();
 
 
 		SPtr<TextureCore> texture;
 		SPtr<TextureCore> texture;
+		SPtr<RenderTextureCore> renderTexture;
 
 
 	private:
 	private:
 		friend class RenderTexturePool;
 		friend class RenderTexturePool;

+ 47 - 12
Source/RenderBeast/Source/BsPostProcessing.cpp

@@ -3,6 +3,7 @@
 #include "BsPostProcessing.h"
 #include "BsPostProcessing.h"
 #include "BsRenderTexture.h"
 #include "BsRenderTexture.h"
 #include "BsRenderTexturePool.h"
 #include "BsRenderTexturePool.h"
+#include "BsRendererUtility.h"
 
 
 namespace BansheeEngine
 namespace BansheeEngine
 {
 {
@@ -19,8 +20,9 @@ namespace BansheeEngine
 		// Do nothing
 		// Do nothing
 	}
 	}
 
 
-	void DownsampleMat::setParameters(const SPtr<RenderTextureCore>& target)
+	void DownsampleMat::execute(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo)
 	{
 	{
+		// Set parameters
 		SPtr<TextureCore> colorTexture = target->getBindableColorTexture();
 		SPtr<TextureCore> colorTexture = target->getBindableColorTexture();
 		mInputTexture.set(colorTexture);
 		mInputTexture.set(colorTexture);
 
 
@@ -29,23 +31,30 @@ namespace BansheeEngine
 
 
 		mParams.gInvTexSize.set(invTextureSize);
 		mParams.gInvTexSize.set(invTextureSize);
 
 
+		// Set output
 		const TextureProperties& colorProps = colorTexture->getProperties();
 		const TextureProperties& colorProps = colorTexture->getProperties();
 
 
 		UINT32 width = std::max(1, Math::ceilToInt(colorProps.getWidth() * 0.5f));
 		UINT32 width = std::max(1, Math::ceilToInt(colorProps.getWidth() * 0.5f));
 		UINT32 height = std::max(1, Math::ceilToInt(colorProps.getHeight() * 0.5f));
 		UINT32 height = std::max(1, Math::ceilToInt(colorProps.getHeight() * 0.5f));
 
 
 		mOutputDesc = POOLED_RENDER_TEXTURE_DESC::create2D(colorProps.getFormat(), width, height, TU_RENDERTARGET);
 		mOutputDesc = POOLED_RENDER_TEXTURE_DESC::create2D(colorProps.getFormat(), width, height, TU_RENDERTARGET);
-		
-		// TODO - Actually send params to GPU.
-	}
 
 
-	void DownsampleMat::render(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo)
-	{
+		// Render
 		ppInfo.downsampledSceneTex = RenderTexturePool::instance().get(mOutputDesc);
 		ppInfo.downsampledSceneTex = RenderTexturePool::instance().get(mOutputDesc);
 
 
-		// TODO - Render
+		RenderAPICore& rapi = RenderAPICore::instance();
+		rapi.setRenderTarget(ppInfo.downsampledSceneTex->renderTexture, true);
+
+		gRendererUtility().setPass(mMaterial, 0);
+		gRendererUtility().drawScreenQuad();
+
+		mOutput = ppInfo.downsampledSceneTex->renderTexture;
+	}
 
 
+	void DownsampleMat::release(PostProcessInfo& ppInfo)
+	{
 		RenderTexturePool::instance().release(ppInfo.downsampledSceneTex);
 		RenderTexturePool::instance().release(ppInfo.downsampledSceneTex);
+		mOutput = nullptr;
 	}
 	}
 
 
 	EyeAdaptHistogramMat::EyeAdaptHistogramMat()
 	EyeAdaptHistogramMat::EyeAdaptHistogramMat()
@@ -64,8 +73,9 @@ namespace BansheeEngine
 		defines.set("LOOP_COUNT_Y", LOOP_COUNT_Y);
 		defines.set("LOOP_COUNT_Y", LOOP_COUNT_Y);
 	}
 	}
 
 
-	void EyeAdaptHistogramMat::setParameters(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo)
+	void EyeAdaptHistogramMat::execute(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo)
 	{
 	{
+		// Set parameters
 		mSceneColor.set(target->getBindableColorTexture());
 		mSceneColor.set(target->getBindableColorTexture());
 
 
 		const PostProcessSettings& settings = ppInfo.settings;
 		const PostProcessSettings& settings = ppInfo.settings;
@@ -89,15 +99,40 @@ namespace BansheeEngine
 
 
 		mParams.gThreadGroupCount.set(threadGroupCount);
 		mParams.gThreadGroupCount.set(threadGroupCount);
 
 
-		// TODO - Output
+		// Set output
+		UINT32 histogramSize = THREAD_GROUP_SIZE_X * THREAD_GROUP_SIZE_Y;
+		UINT32 histogramTexelCount = histogramSize / 4; // 4 entries per texel
+		UINT32 numHistograms = threadGroupCount.x * threadGroupCount.y;
+
+		mOutputDesc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_FLOAT32_RGBA, histogramTexelCount, numHistograms, 
+			TU_LOADSTORE);
+
+		// Dispatch
+		ppInfo.histogramTex = RenderTexturePool::instance().get(mOutputDesc);
+
+		mOutputTex.set(ppInfo.histogramTex->texture);
+
+		// TODO - Clear downsampled scene texture as render target before dispatch?
+		RenderAPICore& rapi = RenderAPICore::instance();
+		rapi.dispatchCompute(threadGroupCount.x, threadGroupCount.y);
+
+		mOutput = ppInfo.histogramTex->renderTexture;
+	}
+
+	void EyeAdaptHistogramMat::release(PostProcessInfo& ppInfo)
+	{
+		RenderTexturePool::instance().release(ppInfo.histogramTex);
+		mOutput = nullptr;
 	}
 	}
 
 
 	void PostProcessing::postProcess(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo)
 	void PostProcessing::postProcess(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo)
 	{
 	{
-		RenderAPICore& rapi = RenderAPICore::instance();
+		mDownsample.execute(target, ppInfo);
+		mEyeAdaptHistogram.execute(mDownsample.getOutput(), ppInfo);
 
 
-		// TODO - Use plain white for initial eye adaptation tex needed for histogram reduce
+		mDownsample.release(ppInfo);
+		mEyeAdaptHistogram.release(ppInfo);
 
 
-		// TODO - Downsample
+		// TODO - Use plain white for initial eye adaptation tex needed for histogram reduce
 	}
 	}
 }
 }

+ 3 - 1
Source/RenderBeast/Source/BsRenderBeast.cpp

@@ -76,6 +76,7 @@ namespace BansheeEngine
 		mDirLightMat = bs_new<DirectionalLightMat>();
 		mDirLightMat = bs_new<DirectionalLightMat>();
 
 
 		RenderTexturePool::startUp();
 		RenderTexturePool::startUp();
+		PostProcessing::startUp();
 	}
 	}
 
 
 	void RenderBeast::destroyCore()
 	void RenderBeast::destroyCore()
@@ -87,6 +88,7 @@ namespace BansheeEngine
 		mCameraData.clear();
 		mCameraData.clear();
 		mRenderables.clear();
 		mRenderables.clear();
 
 
+		PostProcessing::shutDown();
 		RenderTexturePool::shutDown();
 		RenderTexturePool::shutDown();
 
 
 		bs_delete(mDefaultMaterial);
 		bs_delete(mDefaultMaterial);
@@ -725,7 +727,7 @@ namespace BansheeEngine
 
 
 		if (hasGBuffer)
 		if (hasGBuffer)
 		{
 		{
-			PostProcessing::postProcess(camData.target->getSceneColorRT(), camData.postProcessInfo);
+			//PostProcessing::instance().postProcess(camData.target->getSceneColorRT(), camData.postProcessInfo);
 
 
 			// TODO - Instead of doing a separate resolve here I could potentially perform a resolve directly in some
 			// TODO - Instead of doing a separate resolve here I could potentially perform a resolve directly in some
 			// post-processing pass (e.g. tone mapping). Right now it is just an unnecessary blit.
 			// post-processing pass (e.g. tone mapping). Right now it is just an unnecessary blit.

+ 37 - 16
Source/RenderBeast/Source/BsRenderTexturePool.cpp

@@ -48,6 +48,27 @@ namespace BansheeEngine
 		newTextureData->texture = TextureCoreManager::instance().createTexture(desc.type, desc.width, desc.height, 
 		newTextureData->texture = TextureCoreManager::instance().createTexture(desc.type, desc.width, desc.height, 
 			desc.depth, 0, desc.format, desc.flag, desc.hwGamma, desc.numSamples);
 			desc.depth, 0, desc.format, desc.flag, desc.hwGamma, desc.numSamples);
 		
 		
+		if ((desc.flag & (TU_RENDERTARGET | TU_DEPTHSTENCIL)) != 0)
+		{
+			RENDER_TEXTURE_CORE_DESC rtDesc;
+
+			if ((desc.flag & TU_RENDERTARGET) != 0)
+			{
+				rtDesc.colorSurface.texture = newTextureData->texture;
+				rtDesc.colorSurface.face = 0;
+				rtDesc.colorSurface.mipLevel = 0;
+			}
+
+			if ((desc.flag & TU_DEPTHSTENCIL) != 0)
+			{
+				rtDesc.depthStencilSurface.texture = newTextureData->texture;
+				rtDesc.depthStencilSurface.face = 0;
+				rtDesc.depthStencilSurface.mipLevel = 0;
+			}
+
+			newTextureData->renderTexture = TextureCoreManager::instance().createRenderTexture(rtDesc);
+		}
+
 		return newTextureData;
 		return newTextureData;
 	}
 	}
 
 
@@ -61,22 +82,22 @@ namespace BansheeEngine
 	{
 	{
 		const TextureProperties& texProps = texture->getProperties();
 		const TextureProperties& texProps = texture->getProperties();
 
 
-		bool match = texProps.getTextureType() == desc.type && texProps.getFormat() == desc.format && 
-			texProps.getWidth() == desc.width && texProps.getHeight() == desc.height && texProps.getUsage() == desc.flag;
-
-		if (!match)
-			return false;
-
-		if(desc.type == TEX_TYPE_2D)
-			return texProps.isHardwareGammaEnabled() == desc.hwGamma && texProps.getMultisampleCount() == desc.numSamples;
-
-		if(desc.type == TEX_TYPE_3D)
-			return texProps.getDepth() == desc.depth;
-
-		if (desc.type == TEX_TYPE_CUBE_MAP)
-			return true;
-
-		return false;
+		bool match = texProps.getTextureType() == desc.type 
+			&& texProps.getFormat() == desc.format 
+			&& texProps.getWidth() == desc.width 
+			&& texProps.getHeight() == desc.height
+			&& (texProps.getUsage() & desc.flag) == desc.flag
+			&& (
+				(desc.type == TEX_TYPE_2D 
+					&& texProps.isHardwareGammaEnabled() == desc.hwGamma 
+					&& texProps.getMultisampleCount() == desc.numSamples)
+				|| (desc.type == TEX_TYPE_3D 
+					&& texProps.getDepth() == desc.depth)
+				|| (desc.type == TEX_TYPE_CUBE_MAP)
+				)
+			;
+
+		return match;
 	}
 	}
 
 
 	void RenderTexturePool::_registerTexture(const SPtr<PooledRenderTexture>& texture)
 	void RenderTexturePool::_registerTexture(const SPtr<PooledRenderTexture>& texture)