forked from QianMo/X-PostProcessing-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DualGaussianBlur.cs
166 lines (128 loc) · 6.52 KB
/
DualGaussianBlur.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//----------------------------------------------------------------------------------------------------------
// X-PostProcessing Library
// https://github.com/QianMo/X-PostProcessing-Library
// Copyright (C) 2020 QianMo. All rights reserved.
// Licensed under the MIT License
// You may not use this file except in compliance with the License.You may obtain a copy of the License at
// http://opensource.org/licenses/MIT
//----------------------------------------------------------------------------------------------------------
using System;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.PostProcessing;
namespace XPostProcessing
{
[Serializable]
[PostProcess(typeof(DualGaussianBlurRenderer), PostProcessEvent.AfterStack, "X-PostProcessing/Blur/DualGaussianBlur")]
public class DualGaussianBlur : PostProcessEffectSettings
{
[Range(0.0f, 15.0f)]
public FloatParameter BlurRadius = new FloatParameter { value = 5.0f };
[Range(1.0f, 8.0f)]
public IntParameter Iteration = new IntParameter { value = 4 };
[Range(1, 10)]
public FloatParameter RTDownScaling = new FloatParameter { value = 2 };
}
public sealed class DualGaussianBlurRenderer : PostProcessEffectRenderer<DualGaussianBlur>
{
private const string PROFILER_TAG = "X-DualGaussianBlur";
private Shader shader;
// [down,up]
Level[] m_Pyramid;
const int k_MaxPyramidSize = 16;
public override void Init()
{
shader = Shader.Find("Hidden/X-PostProcessing/DualGaussianBlur");
m_Pyramid = new Level[k_MaxPyramidSize];
for (int i = 0; i < k_MaxPyramidSize; i++)
{
m_Pyramid[i] = new Level
{
down_vertical = Shader.PropertyToID("_BlurMipDownV" + i),
down_horizontal = Shader.PropertyToID("_BlurMipDownH" + i),
up_vertical = Shader.PropertyToID("_BlurMipUpV" + i),
up_horizontal = Shader.PropertyToID("_BlurMipUpH" + i),
};
}
}
public override void Release()
{
base.Release();
}
static class ShaderIDs
{
internal static readonly int BlurOffset = Shader.PropertyToID("_BlurOffset");
internal static readonly int BufferRT1 = Shader.PropertyToID("_BufferRT1");
internal static readonly int BufferRT2 = Shader.PropertyToID("_BufferRT2");
}
struct Level
{
internal int down_vertical;
internal int down_horizontal;
internal int up_horizontal;
internal int up_vertical;
}
public override void Render(PostProcessRenderContext context)
{
CommandBuffer cmd = context.command;
PropertySheet sheet = context.propertySheets.Get(shader);
cmd.BeginSample(PROFILER_TAG);
int tw = (int)(context.screenWidth / settings.RTDownScaling);
int th = (int)(context.screenHeight / settings.RTDownScaling);
Vector4 BlurOffset = new Vector4(settings.BlurRadius / (float)context.screenWidth, settings.BlurRadius / (float)context.screenHeight, 0, 0);
sheet.properties.SetVector(ShaderIDs.BlurOffset, BlurOffset);
// Downsample
RenderTargetIdentifier lastDown = context.source;
for (int i = 0; i < settings.Iteration; i++)
{
int mipDownV = m_Pyramid[i].down_vertical;
int mipDowH = m_Pyramid[i].down_horizontal;
int mipUpV = m_Pyramid[i].up_vertical;
int mipUpH = m_Pyramid[i].up_horizontal;
context.GetScreenSpaceTemporaryRT(cmd, mipDownV, 0, context.sourceFormat, RenderTextureReadWrite.Default, FilterMode.Bilinear, tw, th);
context.GetScreenSpaceTemporaryRT(cmd, mipDowH, 0, context.sourceFormat, RenderTextureReadWrite.Default, FilterMode.Bilinear, tw, th);
context.GetScreenSpaceTemporaryRT(cmd, mipUpV, 0, context.sourceFormat, RenderTextureReadWrite.Default, FilterMode.Bilinear, tw, th);
context.GetScreenSpaceTemporaryRT(cmd, mipUpH, 0, context.sourceFormat, RenderTextureReadWrite.Default, FilterMode.Bilinear, tw, th);
// horizontal blur
sheet.properties.SetVector(ShaderIDs.BlurOffset, new Vector4(settings.BlurRadius / context.screenWidth, 0, 0, 0));
context.command.BlitFullscreenTriangle(lastDown, mipDowH, sheet, 0);
// vertical blur
sheet.properties.SetVector(ShaderIDs.BlurOffset, new Vector4(0, settings.BlurRadius / context.screenHeight, 0, 0));
context.command.BlitFullscreenTriangle(mipDowH, mipDownV, sheet, 0);
lastDown = mipDownV;
tw = Mathf.Max(tw / 2, 1);
th = Mathf.Max(th / 2, 1);
}
// Upsample
int lastUp = m_Pyramid[settings.Iteration - 1].down_vertical;
for (int i = settings.Iteration - 2; i >= 0; i--)
{
int mipUpV = m_Pyramid[i].up_vertical;
int mipUpH = m_Pyramid[i].up_horizontal;
// horizontal blur
sheet.properties.SetVector(ShaderIDs.BlurOffset, new Vector4(settings.BlurRadius / context.screenWidth, 0, 0, 0));
context.command.BlitFullscreenTriangle(lastUp, mipUpH, sheet, 0);
// vertical blur
sheet.properties.SetVector(ShaderIDs.BlurOffset, new Vector4(0, settings.BlurRadius / context.screenHeight, 0, 0));
context.command.BlitFullscreenTriangle(mipUpH, mipUpV, sheet, 0);
lastUp = mipUpV;
}
// Render blurred texture in blend pass
cmd.BlitFullscreenTriangle(lastUp, context.destination, sheet, 1);
// Cleanup
for (int i = 0; i < settings.Iteration; i++)
{
if (m_Pyramid[i].down_vertical != lastUp)
cmd.ReleaseTemporaryRT(m_Pyramid[i].down_vertical);
if (m_Pyramid[i].down_horizontal != lastUp)
cmd.ReleaseTemporaryRT(m_Pyramid[i].down_horizontal);
if (m_Pyramid[i].up_horizontal != lastUp)
cmd.ReleaseTemporaryRT(m_Pyramid[i].up_horizontal);
if (m_Pyramid[i].up_vertical != lastUp)
cmd.ReleaseTemporaryRT(m_Pyramid[i].up_vertical);
}
cmd.EndSample(PROFILER_TAG);
}
}
}