-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathOnTileUberPost.shader
More file actions
364 lines (303 loc) · 14.9 KB
/
Copy pathOnTileUberPost.shader
File metadata and controls
364 lines (303 loc) · 14.9 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
Shader "OnTileUberPost"
{
HLSLINCLUDE
#pragma multi_compile_local_fragment _ _HDR_GRADING _TONEMAP_ACES _TONEMAP_NEUTRAL
#pragma multi_compile_local_fragment _ _FILM_GRAIN
#pragma multi_compile_local_fragment _ _DITHERING
#pragma multi_compile_local_fragment _ _GAMMA_20 _LINEAR_TO_SRGB_CONVERSION
#pragma multi_compile_local_fragment _ _USE_FAST_SRGB_LINEAR_CONVERSION
#pragma multi_compile_local_fragment _ _ENABLE_ALPHA_OUTPUT
#include_with_pragmas "Packages/com.unity.render-pipelines.core/ShaderLibrary/FoveatedRenderingKeywords.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Filtering.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/ScreenCoordOverride.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/Common.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Debug/DebuggingFullscreen.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/DynamicScalingClamping.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/FoveatedRendering.hlsl"
TEXTURE2D(_InternalLut);
TEXTURE2D(_UserLut);
TEXTURE2D(_BlueNoise_Texture);
TEXTURE2D(_Grain_Texture);
float4 _Lut_Params;
float4 _UserLut_Params;
half4 _Vignette_Params1;
float4 _Vignette_Params2;
#ifdef USING_STEREO_MATRICES
float4 _Vignette_ParamsXR;
#endif
float2 _Grain_Params;
float4 _Grain_TilingParams;
float4 _Dithering_Params;
float4 _HDROutputLuminanceParams;
#define VignetteColor _Vignette_Params1.xyz
#ifdef USING_STEREO_MATRICES
#define VignetteCenterEye0 _Vignette_ParamsXR.xy
#define VignetteCenterEye1 _Vignette_ParamsXR.zw
#else
#define VignetteCenter _Vignette_Params2.xy
#endif
#define VignetteIntensity _Vignette_Params2.z
#define VignetteSmoothness _Vignette_Params2.w
#define VignetteRoundness _Vignette_Params1.w
#define LutParams _Lut_Params.xyz
#define PostExposure _Lut_Params.w
#define UserLutParams _UserLut_Params.xyz
#define UserLutContribution _UserLut_Params.w
#define GrainIntensity _Grain_Params.x
#define GrainResponse _Grain_Params.y
#define GrainScale _Grain_TilingParams.xy
#define GrainOffset _Grain_TilingParams.zw
#define DitheringScale _Dithering_Params.xy
#define DitheringOffset _Dithering_Params.zw
#define AlphaScale 1.0
#define AlphaBias 0.0
#define MinNits _HDROutputLuminanceParams.x
#define MaxNits _HDROutputLuminanceParams.y
#define PaperWhite _HDROutputLuminanceParams.z
#define OneOverPaperWhite _HDROutputLuminanceParams.w
half4 UberPost(half4 inputColor, float2 uv)
{
half3 color = inputColor.rgb;
// Gamma space... Just do the rest of Uber in linear and convert back to sRGB at the end
#if UNITY_COLORSPACE_GAMMA
{
color = GetSRGBToLinear(color);
inputColor = GetSRGBToLinear(inputColor);
}
#endif
// To save on variants we use an uniform branch for vignette. This may have performance impact on lower end platforms
UNITY_BRANCH
if (VignetteIntensity > 0)
{
#ifdef USING_STEREO_MATRICES
// With XR, the views can use asymmetric FOV which will have the center of each
// view be at a different location.
const float2 VignetteCenter = unity_StereoEyeIndex == 0 ? VignetteCenterEye0 : VignetteCenterEye1;
#endif
color = ApplyVignette(color, uv, VignetteCenter, VignetteIntensity, VignetteRoundness, VignetteSmoothness, VignetteColor);
}
// Color grading is always enabled when post-processing/uber is active
{
color = ApplyColorGrading(color, PostExposure, TEXTURE2D_ARGS(_InternalLut, sampler_LinearClamp), LutParams, TEXTURE2D_ARGS(_UserLut, sampler_LinearClamp), UserLutParams, UserLutContribution, PaperWhite, OneOverPaperWhite);
}
#if _FILM_GRAIN
{
color = ApplyGrain(color, uv, TEXTURE2D_ARGS(_Grain_Texture, sampler_LinearRepeat), GrainIntensity, GrainResponse, GrainScale, GrainOffset, OneOverPaperWhite);
}
#endif
// When Unity is configured to use gamma color encoding, we ignore the request to convert to gamma 2.0 and instead fall back to sRGB encoding
#if _GAMMA_20 && !UNITY_COLORSPACE_GAMMA
{
color = LinearToGamma20(color);
inputColor = LinearToGamma20(inputColor);
}
// Back to sRGB
#elif UNITY_COLORSPACE_GAMMA || _LINEAR_TO_SRGB_CONVERSION
{
color = GetLinearToSRGB(color);
inputColor = LinearToSRGB(inputColor);
}
#endif
#if _DITHERING
{
color = ApplyDithering(color, uv, TEXTURE2D_ARGS(_BlueNoise_Texture, sampler_PointRepeat), DitheringScale, DitheringOffset, PaperWhite, OneOverPaperWhite);
}
#endif
#if _ENABLE_ALPHA_OUTPUT
// Saturate is necessary to avoid issues when additive blending pushes the alpha over 1.
return half4(color, saturate(inputColor.a));
#else
return half4(color, 1);
#endif
}
ENDHLSL
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}
Pass
{
Name "OnTileUberPost"
LOD 100
ZTest Always ZWrite Off Cull Off
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment FragUberPost
// Declares the framebuffer input as a texture 2d containing half.
FRAMEBUFFER_INPUT_X_HALF(0);
half4 FragUberPost(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = input.texcoord;
// NOTE: Hlsl specifies missing input.a to fill 1 (0 for .rgb).
// InputColor is a "bottom" layer for alpha output.
half4 inputColor = LOAD_FRAMEBUFFER_X_INPUT(0, input.positionCS.xy);
return UberPost(inputColor, uv);
}
ENDHLSL
}
Pass
{
Name "OnTileUberPostMSSoftware"
LOD 100
ZTest Always ZWrite Off Cull Off
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment FragUberPostMSSoftware
#pragma target 5.0
#pragma multi_compile _ _MSAA_2 _MSAA_4
#if defined(_MSAA_2)
#define MSAA_SAMPLES 2
#elif defined(_MSAA_4)
#define MSAA_SAMPLES 4
#else
#define MSAA_SAMPLES 1
#endif
// Declares the framebuffer input as a texture 2d containing half.
FRAMEBUFFER_INPUT_X_HALF_MS(0);
half4 MSAAShaderResolveInputAttachment0(float2 pos, const int msaaSamples)
{
half4 inputColor = half4(0.0, 0.0, 0.0, 0.0);
UNITY_UNROLL
for (int i = 0; i < msaaSamples; ++i) {
half4 col = LOAD_FRAMEBUFFER_INPUT_X_MS(0, i, pos);
inputColor = inputColor + col;
}
return inputColor / msaaSamples;
}
// Run run at fragment frequency and perform a software resolve of the MSAA
// color samples and then run the post-process shader the average result. Faster but not as correct
// as running it per-sample. The resultant output can either be written to a non MSAA surface as it has
// been resolved or to a MSAA surface where all the samples will end up the same which some hardware
// resolve operations will optimize due to detecting no difference in a fragment.
half4 FragUberPostMSSoftware(Varyings input) : SV_Target0
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = input.texcoord;
// NOTE: Hlsl specifies missing input.a to fill 1 (0 for .rgb).
// InputColor is a "bottom" layer for alpha output.
half4 inputColor = MSAAShaderResolveInputAttachment0(input.positionCS.xy, MSAA_SAMPLES);
return UberPost(inputColor, uv);
}
ENDHLSL
}
Pass
{
Name "OnTileUberPostTextureReadVersion"
LOD 100
ZTest Always ZWrite Off Cull Off
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment FragUberPostTextureReadVersion
#pragma target 5.0
#pragma enable_d3d11_debug_symbols
#pragma debug
// Fallback shader to use when we can't keep things on tile, so usually in the editor when dealing with
// MSAA source targets to a non MSAA destination we can't perform a software resolve in the shader and
// so have to fall back to resolving the color target and reading it in as a texture.
// Where we have a MSAA destination we can avoid this.
half4 FragUberPostTextureReadVersion(Varyings input) : SV_Target0
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = SCREEN_COORD_APPLY_SCALEBIAS(UnityStereoTransformScreenSpaceTex(input.texcoord));
half4 inputColor = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, uv);
return UberPost(inputColor, uv);
}
ENDHLSL
}
// Visibility Mesh Versions ------------------------------------------------------------------------------------
Pass
{
Name "OnTileUberPostVisMesh"
LOD 100
ZTest Always ZWrite Off Cull Off
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.universal/Shaders/XR/XRVisibilityMeshHelper.hlsl"
#pragma vertex VertVisibilityMeshXR
#pragma fragment FragUberPost
// Declares the framebuffer input as a texture 2d containing half.
FRAMEBUFFER_INPUT_X_HALF(0);
half4 FragUberPost(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = input.texcoord;
// NOTE: Hlsl specifies missing input.a to fill 1 (0 for .rgb).
// InputColor is a "bottom" layer for alpha output.
half4 inputColor = LOAD_FRAMEBUFFER_X_INPUT(0, input.positionCS.xy);
return UberPost(inputColor, uv);
}
ENDHLSL
}
Pass
{
Name "OnTileUberPostMSSoftwareVisMesh"
LOD 100
ZTest Always ZWrite Off Cull Off
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.universal/Shaders/XR/XRVisibilityMeshHelper.hlsl"
#pragma vertex VertVisibilityMeshXR
#pragma fragment FragUberPostMSSoftware
#pragma target 5.0
#pragma multi_compile _ _MSAA_2 _MSAA_4
#if defined(_MSAA_2)
#define MSAA_SAMPLES 2
#elif defined(_MSAA_4)
#define MSAA_SAMPLES 4
#else
#define MSAA_SAMPLES 1
#endif
// Declares the framebuffer input as a texture 2d containing half.
FRAMEBUFFER_INPUT_X_HALF_MS(0);
half4 MSAAShaderResolveInputAttachment0(float2 pos, const int msaaSamples)
{
half4 inputColor = half4(0.0, 0.0, 0.0, 0.0);
UNITY_UNROLL
for (int i = 0; i < msaaSamples; ++i) {
half4 col = LOAD_FRAMEBUFFER_INPUT_X_MS(0, i, pos);
inputColor = inputColor + col;
}
return inputColor / msaaSamples;
}
// Run at fragment frequency and perform a software resolve of the MSAA
// color samples and then run the post-process shader the average result. Faster but not as correct
// as running it per-sample. The resultant output can either be written to a non MSAA surface as it has
// been resolved or to a MSAA surface where all the samples will end up the same which some hardware
// resolve operations will optimize due to detecting no difference in a fragment.
half4 FragUberPostMSSoftware(Varyings input) : SV_Target0
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = input.texcoord;
// NOTE: Hlsl specifies missing input.a to fill 1 (0 for .rgb).
// InputColor is a "bottom" layer for alpha output.
half4 inputColor = MSAAShaderResolveInputAttachment0(input.positionCS.xy, MSAA_SAMPLES);
return UberPost(inputColor, uv);
}
ENDHLSL
}
Pass
{
Name "OnTileUberPostTextureReadVersionVisMesh"
LOD 100
ZTest Always ZWrite Off Cull Off
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.universal/Shaders/XR/XRVisibilityMeshHelper.hlsl"
#pragma vertex VertVisibilityMeshXR
#pragma fragment FragUberPostTextureReadVersion
#pragma target 5.0
#pragma debug
// Fallback shader to use when we can't keep things on tile, so usually in the editor when dealing with
// MSAA source targets to a non MSAA destination we can't perform a software resolve in the shader and
// so have to fall back to resolving the color target and reading it in as a texture.
// Where we have a MSAA destination we can avoid this.
half4 FragUberPostTextureReadVersion(Varyings input) : SV_Target0
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = SCREEN_COORD_APPLY_SCALEBIAS(UnityStereoTransformScreenSpaceTex(input.texcoord));
half4 inputColor = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, uv);
return UberPost(inputColor, uv);
}
ENDHLSL
}
}
}