//********************************** Banshee Engine (www.banshee3d.com) **************************************************// //**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************// #pragma once #include "BsRenderBeastPrerequisites.h" #include "Renderer/BsRendererMaterial.h" #include "Renderer/BsParamBlocks.h" #include "BsGpuResourcePool.h" #include "BsLightRendering.h" #include "Renderer/BsRenderSettings.h" namespace bs { namespace ct { struct RendererViewTargetData; /** @addtogroup RenderBeast * @{ */ BS_PARAM_BLOCK_BEGIN(DownsampleParamDef) BS_PARAM_BLOCK_ENTRY_ARRAY(Vector2, gOffsets, 4) BS_PARAM_BLOCK_END extern DownsampleParamDef gDownsampleParamDef; /** Shader that downsamples a texture to half its size. */ class DownsampleMat : public RendererMaterial { RMAT_DEF("PPDownsample.bsl"); public: DownsampleMat(); /** Renders the post-process effect with the provided parameters. */ void execute(const SPtr& input, const SPtr& output); /** Returns the texture descriptor that can be used for initializing the output render target. */ static POOLED_RENDER_TEXTURE_DESC getOutputDesc(const SPtr& target); /** Returns the downsample material variation matching the provided parameters. */ static DownsampleMat* getVariation(UINT32 quality, bool msaa); private: SPtr mParamBuffer; GpuParamTexture mInputTexture; static ShaderVariation VAR_LowQuality_NoMSAA; static ShaderVariation VAR_LowQuality_MSAA; static ShaderVariation VAR_HighQuality_NoMSAA; static ShaderVariation VAR_HighQuality_MSAA; }; BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramParamDef) BS_PARAM_BLOCK_ENTRY(Vector4I, gPixelOffsetAndSize) BS_PARAM_BLOCK_ENTRY(Vector2, gHistogramParams) BS_PARAM_BLOCK_ENTRY(Vector2I, gThreadGroupCount) BS_PARAM_BLOCK_END extern EyeAdaptHistogramParamDef gEyeAdaptHistogramParamDef; /** Shader that creates a luminance histogram used for eye adaptation. */ class EyeAdaptHistogramMat : public RendererMaterial { RMAT_DEF("PPEyeAdaptHistogram.bsl"); public: EyeAdaptHistogramMat(); /** Executes the post-process effect with the provided parameters. */ void execute(const SPtr& input, const SPtr& output, const AutoExposureSettings& settings); /** Returns the texture descriptor that can be used for initializing the output render target. */ static POOLED_RENDER_TEXTURE_DESC getOutputDesc(const SPtr& target); /** Calculates the number of thread groups that need to execute to cover the provided texture. */ static Vector2I getThreadGroupCount(const SPtr& target); /** * Returns a vector containing scale and offset (in that order) that will be applied to luminance values * to determine their position in the histogram. */ static Vector2 getHistogramScaleOffset(const AutoExposureSettings& settings); static const UINT32 THREAD_GROUP_SIZE_X = 8; static const UINT32 THREAD_GROUP_SIZE_Y = 8; static const UINT32 HISTOGRAM_NUM_TEXELS = (THREAD_GROUP_SIZE_X * THREAD_GROUP_SIZE_Y) / 4; private: SPtr mParamBuffer; GpuParamTexture mSceneColor; GpuParamLoadStoreTexture mOutputTex; static const UINT32 LOOP_COUNT_X = 8; static const UINT32 LOOP_COUNT_Y = 8; }; BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramReduceParamDef) BS_PARAM_BLOCK_ENTRY(int, gThreadGroupCount) BS_PARAM_BLOCK_END extern EyeAdaptHistogramReduceParamDef gEyeAdaptHistogramReduceParamDef; /** Shader that reduces the luminance histograms created by EyeAdaptHistogramMat into a single histogram. */ class EyeAdaptHistogramReduceMat : public RendererMaterial { RMAT_DEF("PPEyeAdaptHistogramReduce.bsl"); public: EyeAdaptHistogramReduceMat(); /** Executes the post-process effect with the provided parameters. */ void execute(const SPtr& sceneColor, const SPtr& histogram, const SPtr& prevFrame, const SPtr& output); /** Returns the texture descriptor that can be used for initializing the output render target. */ static POOLED_RENDER_TEXTURE_DESC getOutputDesc(); private: SPtr mParamBuffer; GpuParamTexture mHistogramTex; GpuParamTexture mEyeAdaptationTex; }; BS_PARAM_BLOCK_BEGIN(EyeAdaptationParamDef) BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gEyeAdaptationParams, 3) BS_PARAM_BLOCK_END extern EyeAdaptationParamDef gEyeAdaptationParamDef; /** Shader that computes the eye adaptation value based on scene luminance. */ class EyeAdaptationMat : public RendererMaterial { RMAT_DEF("PPEyeAdaptation.bsl"); public: EyeAdaptationMat(); /** Executes the post-process effect with the provided parameters. */ void execute(const SPtr& reducedHistogram, const SPtr& output, float frameDelta, const AutoExposureSettings& settings, float exposureScale); /** Returns the texture descriptor that can be used for initializing the output render target. */ static POOLED_RENDER_TEXTURE_DESC getOutputDesc(); private: SPtr mParamBuffer; GpuParamTexture mReducedHistogramTex; }; BS_PARAM_BLOCK_BEGIN(CreateTonemapLUTParamDef) BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gTonemapParams, 2) BS_PARAM_BLOCK_ENTRY(float, gGammaAdjustment) BS_PARAM_BLOCK_ENTRY(int, gGammaCorrectionType) BS_PARAM_BLOCK_ENTRY(Vector3, gSaturation) BS_PARAM_BLOCK_ENTRY(Vector3, gContrast) BS_PARAM_BLOCK_ENTRY(Vector3, gGain) BS_PARAM_BLOCK_ENTRY(Vector3, gOffset) BS_PARAM_BLOCK_END extern CreateTonemapLUTParamDef gCreateTonemapLUTParamDef; BS_PARAM_BLOCK_BEGIN(WhiteBalanceParamDef) BS_PARAM_BLOCK_ENTRY(float, gWhiteTemp) BS_PARAM_BLOCK_ENTRY(float, gWhiteOffset) BS_PARAM_BLOCK_END extern WhiteBalanceParamDef gWhiteBalanceParamDef; /** * Shader that creates a 3D lookup texture that is used to apply tonemapping, color grading, white balancing and gamma * correction. */ class CreateTonemapLUTMat : public RendererMaterial { RMAT_DEF("PPCreateTonemapLUT.bsl"); public: CreateTonemapLUTMat(); /** Executes the post-process effect with the provided parameters. */ void execute(const SPtr& output, const RenderSettings& settings); /** Returns the texture descriptor that can be used for initializing the output render target. */ static POOLED_RENDER_TEXTURE_DESC getOutputDesc(); /** Size of the 3D color lookup table. */ static const UINT32 LUT_SIZE = 32; private: SPtr mParamBuffer; SPtr mWhiteBalanceParamBuffer; GpuParamLoadStoreTexture mOutputTex; }; BS_PARAM_BLOCK_BEGIN(TonemappingParamDef) BS_PARAM_BLOCK_ENTRY(float, gRawGamma) BS_PARAM_BLOCK_ENTRY(float, gManualExposureScale) BS_PARAM_BLOCK_ENTRY(int, gNumSamples) BS_PARAM_BLOCK_END extern TonemappingParamDef gTonemappingParamDef; /** Shader that applies tonemapping and converts a HDR image into a LDR image. */ class TonemappingMat : public RendererMaterial { RMAT_DEF("PPTonemapping.bsl"); public: TonemappingMat(); /** Executes the post-process effect with the provided parameters. */ void execute(const SPtr& sceneColor, const SPtr& eyeAdaptation, const SPtr& colorLUT, const SPtr& output, const RenderSettings& settings); /** Returns the material variation matching the provided parameters. */ static TonemappingMat* getVariation(bool gammaOnly, bool autoExposure, bool MSAA); private: SPtr mParamBuffer; GpuParamTexture mInputTex; GpuParamTexture mColorLUT; GpuParamTexture mEyeAdaptationTex; static ShaderVariation VAR_Gamma_AutoExposure_MSAA; static ShaderVariation VAR_Gamma_AutoExposure_NoMSAA; static ShaderVariation VAR_Gamma_NoAutoExposure_MSAA; static ShaderVariation VAR_Gamma_NoAutoExposure_NoMSAA; static ShaderVariation VAR_NoGamma_AutoExposure_MSAA; static ShaderVariation VAR_NoGamma_AutoExposure_NoMSAA; static ShaderVariation VAR_NoGamma_NoAutoExposure_MSAA; static ShaderVariation VAR_NoGamma_NoAutoExposure_NoMSAA; }; const int MAX_BLUR_SAMPLES = 128; BS_PARAM_BLOCK_BEGIN(GaussianBlurParamDef) BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gSampleOffsets, (MAX_BLUR_SAMPLES + 1) / 2) BS_PARAM_BLOCK_ENTRY_ARRAY(Vector4, gSampleWeights, (MAX_BLUR_SAMPLES + 3) / 4) BS_PARAM_BLOCK_ENTRY(int, gNumSamples) BS_PARAM_BLOCK_END extern GaussianBlurParamDef gGaussianBlurParamDef; /** Shader that performs Gaussian blur filtering on the provided texture. */ class GaussianBlurMat : public RendererMaterial { // Direction of the Gaussian filter pass enum Direction { DirVertical, DirHorizontal }; RMAT_DEF("PPGaussianBlur.bsl"); public: GaussianBlurMat(); /** * Renders the post-process effect with the provided parameters. * * @param[in] source Input texture to blur. * @param[in] filterSize Size of the blurring filter, in percent of the source texture. In range [0, 1]. * @param[in] destination Output texture to which to write the blurred image to. */ void execute(const SPtr& source, float filterSize, const SPtr& destination); private: /** Calculates weights and offsets for the standard distribution of the specified filter size. */ static UINT32 calcStdDistribution(float filterRadius, std::array& weights, std::array& offsets); /** Calculates the radius of the blur kernel depending on the source texture size and provided scale. */ static float calcKernelRadius(const SPtr& source, float scale, Direction filterDir); SPtr mParamBuffer; GpuParamTexture mInputTexture; }; BS_PARAM_BLOCK_BEGIN(GaussianDOFParamDef) BS_PARAM_BLOCK_ENTRY(float, gNearBlurPlane) BS_PARAM_BLOCK_ENTRY(float, gFarBlurPlane) BS_PARAM_BLOCK_ENTRY(float, gInvNearBlurRange) BS_PARAM_BLOCK_ENTRY(float, gInvFarBlurRange) BS_PARAM_BLOCK_ENTRY(Vector2, gHalfPixelOffset) BS_PARAM_BLOCK_END extern GaussianDOFParamDef sGaussianDOFParamDef; /** * Shader that masks pixels from the input color texture into one or two output textures. The masking is done by * determining if the pixel falls into near or far unfocused plane, as determined by depth-of-field parameters. User * can pick whether to output pixels just on the near plane, just on the far plane, or both. * */ class GaussianDOFSeparateMat : public RendererMaterial { RMAT_DEF("PPGaussianDOFSeparate.bsl"); public: GaussianDOFSeparateMat(); /** * Renders the post-process effect with the provided parameters. * * @param[in] color Input color texture to process. * @param[in] depth Input depth buffer texture that will be used for determining pixel depth. * @param[in] view View through which the depth of field effect is viewed. * @param[in] settings Settings used to control depth of field rendering. */ void execute(const SPtr& color, const SPtr& depth, const RendererView& view, const DepthOfFieldSettings& settings); /** * Returns the texture generated after the shader was executed. Only valid to call this in-between calls to * execute() & release(), with @p idx value 0 or 1. */ SPtr getOutput(UINT32 idx); /** * Releases the interally allocated output render textures. Must be called after each call to execute(), when the * caller is done using the textures. */ void release(); /** * Returns the material variation matching the provided parameters. * * @param near If true, near plane pixels are output to the first render target. * @param far If true, far plane pixels are output to the first render target. If @p near is also enabled, the * pixels are output to the second render target instead. */ static GaussianDOFSeparateMat* getVariation(bool near, bool far); private: SPtr mParamBuffer; GpuParamTexture mColorTexture; GpuParamTexture mDepthTexture; SPtr mOutput0; SPtr mOutput1; static ShaderVariation VAR_Near_Far; static ShaderVariation VAR_NoNear_Far; static ShaderVariation VAR_Near_NoFar; }; /** * Shader that combines pixels for near unfocused, focused and far unfocused planes, as calculated by * GaussianDOFSeparateMat. Outputs final depth-of-field filtered image. */ class GaussianDOFCombineMat : public RendererMaterial { RMAT_DEF("PPGaussianDOFCombine.bsl"); public: GaussianDOFCombineMat(); /** * Renders the post-process effect with the provided parameters. * * @param[in] focused Input texture containing focused (default) scene color. * @param[in] near Input texture containing filtered (blurred) values for the unfocused foreground area. * Can be null if no near plane needs to be blended. * @param[in] far Input texture containing filtered (blurred) values for the unfocused background area. * Can be null if no far plane needs to be blended. * @param[in] depth Input depth buffer texture that will be used for determining pixel depth. * @param[in] output Texture to output the results to. * @param[in] view View through which the depth of field effect is viewed. * @param[in] settings Settings used to control depth of field rendering. */ void execute(const SPtr& focused, const SPtr& near, const SPtr& far, const SPtr& depth, const SPtr& output, const RendererView& view, const DepthOfFieldSettings& settings); /** * Returns the material variation matching the provided parameters. * * @param near If true, near plane pixels are read from the near plane texture, otherwise near plane is assumed * not to exist. * @param far If true, far plane pixels are read from the far plane texture, otherwise far plane is assumed not * to exist. */ static GaussianDOFCombineMat* getVariation(bool near, bool far); private: SPtr mParamBuffer; GpuParamTexture mFocusedTexture; GpuParamTexture mNearTexture; GpuParamTexture mFarTexture; GpuParamTexture mDepthTexture; static ShaderVariation VAR_Near_Far; static ShaderVariation VAR_NoNear_Far; static ShaderVariation VAR_Near_NoFar; }; /** Shader that calculates a single level of the hierarchical Z mipmap chain. */ class BuildHiZMat : public RendererMaterial { RMAT_DEF("PPBuildHiZ.bsl"); public: BuildHiZMat(); /** * Renders the post-process effect with the provided parameters. * * @param[in] source Input depth texture to use as the source. * @param[in] srcMip Mip level to read from the @p source texture. * @param[in] srcRect Rectangle in normalized coordinates, describing from which portion of the source * texture to read the input. * @param[in] dstRect Destination rectangle to limit the writes to. * @param[in] output Output target to which to write to results. */ void execute(const SPtr& source, UINT32 srcMip, const Rect2& srcRect, const Rect2& dstRect, const SPtr& output); private: GpuParamTexture mInputTexture; }; BS_PARAM_BLOCK_BEGIN(FXAAParamDef) BS_PARAM_BLOCK_ENTRY(Vector2, gInvTexSize) BS_PARAM_BLOCK_END extern FXAAParamDef gFXAAParamDef; /** Shader that performs Fast Approximate anti-aliasing. */ class FXAAMat : public RendererMaterial { RMAT_DEF("PPFXAA.bsl"); public: FXAAMat(); /** * Renders the post-process effect with the provided parameters. * * @param[in] source Input texture to apply FXAA to. * @param[in] destination Output target to which to write the antialiased image to. */ void execute(const SPtr& source, const SPtr& destination); private: SPtr mParamBuffer; GpuParamTexture mInputTexture; }; BS_PARAM_BLOCK_BEGIN(SSAOParamDef) BS_PARAM_BLOCK_ENTRY(float, gSampleRadius) BS_PARAM_BLOCK_ENTRY(float, gWorldSpaceRadiusMask) BS_PARAM_BLOCK_ENTRY(Vector2, gTanHalfFOV) BS_PARAM_BLOCK_ENTRY(Vector2, gRandomTileScale) BS_PARAM_BLOCK_ENTRY(float, gCotHalfFOV) BS_PARAM_BLOCK_ENTRY(float, gBias) BS_PARAM_BLOCK_ENTRY(Vector2, gDownsampledPixelSize) BS_PARAM_BLOCK_ENTRY(Vector2, gFadeMultiplyAdd) BS_PARAM_BLOCK_ENTRY(float, gPower) BS_PARAM_BLOCK_ENTRY(float, gIntensity) BS_PARAM_BLOCK_END extern SSAOParamDef gSSAOParamDef; /** Textures used as input when calculating SSAO. */ struct SSAOTextureInputs { /** Full resolution scene depth. Only used by final SSAO pass. */ SPtr sceneDepth; /** Full resolution buffer containing scene normals. Only used by final SSAO pass. */ SPtr sceneNormals; /** Precalculated texture containing downsampled normals/depth, to be used for AO input. */ SPtr aoSetup; /** Texture containing AO from the previous pass. Only used if upsampling is enabled. */ SPtr aoDownsampled; /** Tileable texture containing random rotations that will be applied to AO samples. */ SPtr randomRotations; }; /** Shader that computes ambient occlusion using screen based methods. */ class SSAOMat : public RendererMaterial { RMAT_DEF("PPSSAO.bsl"); public: SSAOMat(); /** * Renders the post-process effect with the provided parameters. * * @param[in] view Information about the view we're rendering from. * @param[in] textures Set of textures to be used as input. Which textures are used depends on the * template parameters of this class. * @param[in] destination Output texture to which to write the ambient occlusion data to. * @param[in] settings Settings used to control the ambient occlusion effect. */ void execute(const RendererView& view, const SSAOTextureInputs& textures, const SPtr& destination, const AmbientOcclusionSettings& settings); /** * Returns the material variation matching the provided parameters. * * @param upsample If true the shader will blend the calculated AO with AO data from the previous pass. * @param finalPass If true the shader will use the full screen normal/depth information and perform * intensity scaling, as well as distance fade. Otherwise the shader will use the * downsampled AO setup information, with no scaling/fade. * @param quality Integer in range [0, 4] that controls the quality of SSAO sampling. Higher numbers yield * better quality at the cost of performance. */ static SSAOMat* getVariation(bool upsample, bool finalPass, int quality); private: SPtr mParamBuffer; GpuParamTexture mDepthTexture; GpuParamTexture mNormalsTexture; GpuParamTexture mDownsampledAOTexture; GpuParamTexture mSetupAOTexture; GpuParamTexture mRandomTexture; #define VARIATION(QUALITY) \ static ShaderVariation VAR_Upsample_Final_Quality##QUALITY; \ static ShaderVariation VAR_Upsample_NoFinal_Quality##QUALITY; \ static ShaderVariation VAR_NoUpsample_Final_Quality##QUALITY; \ static ShaderVariation VAR_NoUpsample_NoFinal_Quality##QUALITY; \ VARIATION(0) VARIATION(1) VARIATION(2) VARIATION(3) VARIATION(4) #undef VARIATION }; BS_PARAM_BLOCK_BEGIN(SSAODownsampleParamDef) BS_PARAM_BLOCK_ENTRY(Vector2, gPixelSize) BS_PARAM_BLOCK_ENTRY(float, gInvDepthThreshold) BS_PARAM_BLOCK_END extern SSAODownsampleParamDef gSSAODownsampleParamDef; /** * Shader that downsamples the depth & normal buffer and stores their results in a common texture, to be consumed * by SSAOMat. */ class SSAODownsampleMat : public RendererMaterial { RMAT_DEF("PPSSAODownsample.bsl"); public: SSAODownsampleMat(); /** * Renders the post-process effect with the provided parameters. * * @param[in] view Information about the view we're rendering from. * @param[in] sceneDepth Input texture containing scene depth. * @param[in] sceneNormals Input texture containing scene world space normals. * @param[in] destination Output texture to which to write the downsampled data to. * @param[in] depthRange Valid depth range (in view space) within which nearby samples will be averaged. */ void execute(const RendererView& view, const SPtr& sceneDepth, const SPtr& sceneNormals, const SPtr& destination, float depthRange); private: SPtr mParamBuffer; GpuParamTexture mDepthTexture; GpuParamTexture mNormalsTexture; }; BS_PARAM_BLOCK_BEGIN(SSAOBlurParamDef) BS_PARAM_BLOCK_ENTRY(Vector2, gPixelSize) BS_PARAM_BLOCK_ENTRY(Vector2, gPixelOffset) BS_PARAM_BLOCK_ENTRY(float, gInvDepthThreshold) BS_PARAM_BLOCK_END extern SSAOBlurParamDef gSSAOBlurParamDef; /** * Shaders that blurs the ambient occlusion output, in order to hide the noise caused by the randomization texture. */ class SSAOBlurMat : public RendererMaterial { RMAT_DEF("PPSSAOBlur.bsl"); public: SSAOBlurMat(); /** * Renders the post-process effect with the provided parameters. * * @param[in] view Information about the view we're rendering from. * @param[in] ao Input texture containing ambient occlusion data to be blurred. * @param[in] sceneDepth Input texture containing scene depth. * @param[in] destination Output texture to which to write the blurred data to. * @param[in] depthRange Valid depth range (in view space) within which nearby samples will be averaged. */ void execute(const RendererView& view, const SPtr& ao, const SPtr& sceneDepth, const SPtr& destination, float depthRange); /** Returns the material variation matching the provided parameters. */ static SSAOBlurMat* getVariation(bool horizontal); private: SPtr mParamBuffer; GpuParamTexture mAOTexture; GpuParamTexture mDepthTexture; static ShaderVariation VAR_Vertical; static ShaderVariation VAR_Horizontal; }; BS_PARAM_BLOCK_BEGIN(SSRStencilParamDef) BS_PARAM_BLOCK_ENTRY(Vector2, gRoughnessScaleBias) BS_PARAM_BLOCK_END extern SSRStencilParamDef gSSRStencilParamDef; /** Shader used for marking which parts of the screen require screen space reflections. */ class SSRStencilMat : public RendererMaterial { RMAT_DEF("PPSSRStencil.bsl"); public: SSRStencilMat(); /** * Renders the effect with the provided parameters, using the currently bound render target. * * @param[in] view Information about the view we're rendering from. * @param[in] gbuffer GBuffer textures. * @param[in] settings Parameters used for controling the SSR effect. */ void execute(const RendererView& view, GBufferTextures gbuffer, const ScreenSpaceReflectionsSettings& settings); /** * Returns the material variation matching the provided parameters. * * @param[in] msaa True if the shader will operate on a multisampled surface. * @param[in] singleSampleMSAA Only relevant of @p msaa is true. When enabled only the first sample will be * evaluated. Otherwise all samples will be evaluated. * @return Requested variation of the material. */ static SSRStencilMat* getVariation(bool msaa, bool singleSampleMSAA); private: SPtr mParamBuffer; GBufferParams mGBufferParams; static ShaderVariation VAR_FullMSAA; static ShaderVariation VAR_SingleMSAA; static ShaderVariation VAR_NoMSAA; }; BS_PARAM_BLOCK_BEGIN(SSRTraceParamDef) BS_PARAM_BLOCK_ENTRY(Vector4, gNDCToHiZUV) BS_PARAM_BLOCK_ENTRY(Vector2, gHiZUVToScreenUV) BS_PARAM_BLOCK_ENTRY(Vector2I, gHiZSize) BS_PARAM_BLOCK_ENTRY(int, gHiZNumMips) BS_PARAM_BLOCK_ENTRY(float, gIntensity) BS_PARAM_BLOCK_ENTRY(Vector2, gRoughnessScaleBias) BS_PARAM_BLOCK_ENTRY(int, gTemporalJitter) BS_PARAM_BLOCK_END extern SSRTraceParamDef gSSRTraceParamDef; /** Shader used for tracing rays for screen space reflections. */ class SSRTraceMat : public RendererMaterial { RMAT_DEF("PPSSRTrace.bsl"); public: SSRTraceMat(); /** * Renders the effect with the provided parameters. * * @param[in] view Information about the view we're rendering from. * @param[in] gbuffer GBuffer textures. * @param[in] sceneColor Scene color texture. * @param[in] hiZ Hierarchical Z buffer. * @param[in] settings Parameters used for controling the SSR effect. * @param[in] destination Render target to which to write the results to. */ void execute(const RendererView& view, GBufferTextures gbuffer, const SPtr& sceneColor, const SPtr& hiZ, const ScreenSpaceReflectionsSettings& settings, const SPtr& destination); /** * Calculates a scale & bias that is used for transforming roughness into a fade out value. Anything that is below * @p maxRoughness will have the fade value of 1. Values above @p maxRoughness is slowly fade out over a range that * is 1/2 the length of @p maxRoughness. */ static Vector2 calcRoughnessFadeScaleBias(float maxRoughness); /** * Returns the material variation matching the provided parameters. * * @param[in] quality Determines how many rays to trace. In range [0, 4]. * @param[in] msaa True if the shader will operate on a multisampled surface. * @param[in] singleSampleMSAA Only relevant of @p msaa is true. When enabled only the first sample will be * evaluated. Otherwise all samples will be evaluated. * @return Requested variation of the material. */ static SSRTraceMat* getVariation(UINT32 quality, bool msaa, bool singleSampleMSAA = false); private: SPtr mParamBuffer; GBufferParams mGBufferParams; GpuParamTexture mSceneColorTexture; GpuParamTexture mHiZTexture; #define VARIATION(QUALITY) \ static ShaderVariation VAR_NoMSAA_Quality##QUALITY; \ static ShaderVariation VAR_FullMSAA_Quality##QUALITY; \ static ShaderVariation VAR_SingleMSAA_Quality##QUALITY; \ VARIATION(0) VARIATION(1) VARIATION(2) VARIATION(3) VARIATION(4) #undef VARIATION }; BS_PARAM_BLOCK_BEGIN(TemporalResolveParamDef) BS_PARAM_BLOCK_ENTRY_ARRAY(float, gSampleWeights, 9) BS_PARAM_BLOCK_ENTRY_ARRAY(float, gSampleWeightsLowpass, 9) BS_PARAM_BLOCK_END extern TemporalResolveParamDef gTemporalResolveParamDef; BS_PARAM_BLOCK_BEGIN(SSRResolveParamDef) BS_PARAM_BLOCK_ENTRY(Vector2, gSceneDepthTexelSize) BS_PARAM_BLOCK_ENTRY(Vector2, gSceneColorTexelSize) BS_PARAM_BLOCK_ENTRY(float, gManualExposure) BS_PARAM_BLOCK_END extern SSRResolveParamDef gSSRResolveParamDef; /** Shader used for combining SSR information from the previous frame, in order to yield better quality. */ class SSRResolveMat : public RendererMaterial { RMAT_DEF("PPSSRResolve.bsl"); public: SSRResolveMat(); /** * Renders the effect with the provided parameters. * * @param[in] view Information about the view we're rendering from. * @param[in] prevFrame SSR data calculated previous frame. * @param[in] curFrame SSR data calculated this frame. * @param[in] sceneDepth Buffer containing scene depth. * @param[in] destination Render target to which to write the results to. */ void execute(const RendererView& view, const SPtr& prevFrame, const SPtr& curFrame, const SPtr& sceneDepth, const SPtr& destination); /** * Returns the material variation matching the provided parameters. * * @param[in] msaa True if the shader will operate on a multisampled surface. Note that previous * and current frame color textures must be non-MSAA, regardless of this parameter. * @return Requested variation of the material. */ static SSRResolveMat* getVariation(bool msaa); private: SPtr mSSRParamBuffer; SPtr mTemporalParamBuffer; GpuParamTexture mSceneColorTexture; GpuParamTexture mPrevColorTexture; GpuParamTexture mSceneDepthTexture; GpuParamTexture mEyeAdaptationTexture; static ShaderVariation VAR_MSAA; static ShaderVariation VAR_NoMSAA; }; BS_PARAM_BLOCK_BEGIN(EncodeDepthParamDef) BS_PARAM_BLOCK_ENTRY(float, gNear) BS_PARAM_BLOCK_ENTRY(float, gFar) BS_PARAM_BLOCK_END extern EncodeDepthParamDef gEncodeDepthParamDef; /** * Shader that encodes depth from a specified range into [0, 1] range, and writes the result in the alpha channel * of the output texture. */ class EncodeDepthMat : public RendererMaterial { RMAT_DEF("PPEncodeDepth.bsl"); public: EncodeDepthMat(); /** * Renders the post-process effect with the provided parameters. * * @param[in] depth Resolved (non-MSAA) depth texture to encode. * @param[in] near Near range (in view space) to start encoding the depth. Any depth lower than this will * be encoded to 1. * @param[in] far Far range (in view space) to end encoding the depth. Any depth higher than this will * be encoded to 0. * @param[in] output Output texture to write the results in. Results will be written in the alpha channel. */ void execute(const SPtr& depth, float near, float far, const SPtr& output); private: SPtr mParamBuffer; GpuParamTexture mInputTexture; }; /** * Shader that outputs a texture that determines which pixels require per-sample evaluation. Only relevant when * rendering with MSAA enabled. */ class MSAACoverageMat : public RendererMaterial { RMAT_DEF("MSAACoverage.bsl"); public: MSAACoverageMat(); /** * Renders the effect with the provided parameters, using the currently bound render target. * * @param[in] view Information about the view we're rendering from. * @param[in] gbuffer GBuffer textures. */ void execute(const RendererView& view, GBufferTextures gbuffer); /** Returns the material variation matching the provided parameters. */ static MSAACoverageMat* getVariation(UINT32 msaaCount); private: GBufferParams mGBufferParams; static ShaderVariation VAR_2x; static ShaderVariation VAR_4x; static ShaderVariation VAR_8x; }; /** * Converts the coverage texture output by MSAACoverageMat and writes its information in the highest bit of the * currently bound stencil buffer. This allows coverage information to be used by normal (non-compute) rendering * shaders. */ class MSAACoverageStencilMat : public RendererMaterial { RMAT_DEF("MSAACoverageStencil.bsl"); public: MSAACoverageStencilMat(); /** * Renders the effect with the provided parameters, using the currently bound render target. * * @param[in] view Information about the view we're rendering from. * @param[in] coverage Coverage texture as output by MSAACoverageMat. */ void execute(const RendererView& view, const SPtr& coverage); private: GpuParamTexture mCoverageTexParam; }; /** @} */ }}