Skip to content

Commit

Permalink
Fix SSAO on OpenGL platforms [closes #21]
Browse files Browse the repository at this point in the history
  • Loading branch information
Delt06 committed Mar 12, 2023
1 parent 1379da3 commit f3664dc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
26 changes: 26 additions & 0 deletions Assets/ToonRP/Runtime/GraphicsApiUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using UnityEngine;
using UnityEngine.Rendering;

namespace ToonRP.Runtime
{
public static class GraphicsApiUtils
{
// OpenGL uses the [-1, 1] depth clip space.
// Other APIs use the [0, 1] range.
public static bool OpenGlStyleClipDepth
{
get
{
#if UNITY_WEBGL
return true;
#else // !UNITY_WEBGL
GraphicsDeviceType api = SystemInfo.graphicsDeviceType;
return api is
GraphicsDeviceType.OpenGLCore or
GraphicsDeviceType.OpenGLES2 or
GraphicsDeviceType.OpenGLES3;
#endif // UNITY_WEBGL
}
}
}
}
3 changes: 3 additions & 0 deletions Assets/ToonRP/Runtime/GraphicsApiUtils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion Assets/ToonRP/Runtime/PostProcessing/ToonSsao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ private void RenderMainPass()
_cmd.SetRenderTarget(RtId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);

_cmd.SetGlobalTexture("_NoiseTexture", _noiseTexture);
_cmd.SetGlobalFloat("_Radius", _settings.Radius);

_cmd.SetGlobalFloat("_Radius", GetRadius());
_cmd.SetGlobalFloat("_Power", _settings.Power);
_cmd.SetGlobalVector("_NoiseScale",
new Vector4((float) _width / _noiseTexture.width, (float) _height / _noiseTexture.height)
Expand All @@ -188,6 +189,17 @@ private void RenderMainPass()
Draw(MainPass);
}

private float GetRadius()
{
float radius = _settings.Radius;
if (GraphicsApiUtils.OpenGlStyleClipDepth)
{
radius *= 2;
}

return radius;
}

private void RenderBlur(Vector2 direction, in RenderTargetIdentifier source)
{
_cmd.SetGlobalVector("_Direction", direction);
Expand Down
8 changes: 5 additions & 3 deletions Assets/ToonRP/Shaders/PostProcessing/ToonRPSSAO.shader
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{
ZTest Off
ZWrite Off
Cull Off

HLSLINCLUDE

Expand Down Expand Up @@ -54,14 +55,14 @@

#include "../../ShaderLibrary/DepthNormals.hlsl"

#define SAMPLES_COUNT 64
#define MAX_SAMPLES_COUNT 64
#define NORMAL_BIAS 0.0
#define DEPTH_BIAS 0.01

CBUFFER_START(ToonRpSsao)
float2 _NoiseScale;
int _KernelSize;
float4 _Samples[SAMPLES_COUNT];
float4 _Samples[MAX_SAMPLES_COUNT];
float _Radius;
float _Power;
CBUFFER_END
Expand All @@ -83,7 +84,8 @@
float4 RestorePositionVs(float3 positionNdc, const float4x4 inverseProjection)
{
float4 positionVs = mul(inverseProjection, float4(positionNdc, 1.0));
positionVs /= positionVs.w;
positionVs.xyz /= positionVs.w;
positionVs.w = 1;
return positionVs;
}

Expand Down

0 comments on commit f3664dc

Please sign in to comment.