Untitled
Loading...
Searching...
No Matches
postprocess.frag
Go to the documentation of this file.
1
2layout(std140, binding = 0) uniform CameraData {
3 vec4 cam_pos;
4 vec4 cam_vel;
6 mat4 unjittered_projection;
8 mat4 inv_projection;
9 mat4 unjittered_inv_projection;
10 mat4 inv_view;
11};
12
14uniform sampler2D uScene;
15uniform sampler2D uHistory;
16uniform sampler2D uFBM;
17
18uniform sampler2D uDPP;
19uniform sampler2D uText;
20uniform sampler2D uSSR;
21uniform sampler2D uCurrentSSR;
22uniform sampler2D gVel;
23uniform sampler2D uBokeh;
24uniform sampler2D uCOC;
25uniform sampler2D uBloom;
26uniform sampler2D uLM;
27uniform sampler3D uLUT;
28uniform uint uMBLUR;
29uniform uint uDOF;
30uniform float uPhysicsFramerate;
31uniform float uRenderFramerate;
32uniform uint uWireframeToggled;
33
34
35layout(std140, binding = 1) uniform RenderingData {
36 ivec4 resolution;
37};
38
39uniform sampler3D uColorLUT;
40
42 float lutSize = 33.0;
43 vec3 scale = vec3((lutSize - 1.0) / lutSize);
44 vec3 offset = vec3(1.0 / (2.0 * lutSize));
45
46 vec3 lutUVW = color * scale + offset;
47
48 return texture(uLUT, lutUVW).rgb;
49}
50
51
52float calculateExposure(vec3 avgColor){
53 float lum = dot(avgColor, vec3(0.2126, 0.7152, 0.0722));
54
55 lum = max(lum, 0.0001f);
56
57 float middleGray = 0.18f;
58 float acesBoost = 0.01f;
59
60 float minExp = 0.1f;
61 float maxExp = 2.0f;
62
63 float targetExposure = (middleGray / lum);
64
65 return clamp(targetExposure, minExp, maxExp);
66}
67
69 float a = 2.51f;
70 float b = 0.03f;
71 float c = 2.43f;
72 float d = 0.59f;
73 float e = 0.14f;
74 return clamp((x*(a*x+b))/(x*(c*x+d)+e), 0.0, 1.0);
75}
76
77vec3 vignette(vec3 color, float u, float v){
78 vec2 nvuv = vUV - 0.5;
79 float nvdist = length(nvuv);
80 float vignette = smoothstep(u, v, nvdist);
81 color.rgb *= vignette;
82
83 return color;
84}
85
86
88(
89 0.613097, 0.070194, 0.020585,
90 0.339523, 0.916394, 0.109572,
91 0.047379, 0.013412, 0.869843
92);
93
95(
96 1.704859, -0.130258, -0.024003,
97 -0.621714, 1.140733, -0.128963,
98 -0.083145, -0.010475, 1.152966
99);
101{
102
103 vec3 ap1 = srgb_to_ap1 * linearRGB;
104
105 vec3 log_low = 10.5402377416545 * ap1 + 0.0729055341958355;
106 vec3 log_high = (log2(max(ap1, vec3(1e-6))) + 9.72) / 17.52;
107
108 vec3 use_low = vec3(lessThanEqual(ap1, vec3(0.0078125)));
109
110 return mix(log_high, log_low, step(ap1, vec3(0.0078125)));
111}
112
114{
115
116 vec3 linear_t = (graded - 0.0729055341958355) / 10.5402377416545;
117 vec3 log_curve= exp2(graded * 17.52 - 9.72);
118
119 vec3 use_low = vec3(lessThanEqual(graded, vec3(0.155228)));
120 vec3 ap1 = mix(log_curve, linear_t, step(graded, vec3(0.155251141552511)));
121 // vec3 ap1 = mix(log_curve, linear_t, use_low);
122 return ap1_to_srgb * ap1;
123}
124
125
126
127
128
129
130
132{
133 if (x <= 0.0078125)
134 {
135 return (10.5402377416545 * x + 0.0729055341958355);
136 }
137 else
138 {
139 return ((log2(x) + 9.72) / 17.52);
140 }
141}
143{
144 if (x <= 0.155228)
145 {
146 return (x - 0.072905534195) / 10.5402377416;
147 }
148 else
149 {
150 return pow(2.0, x * 17.52 - 9.72);
151 }
152}
153
155{
156
157
158 vec3 aces_cct = linear_to_acescct(color);
159
160 vec3 graded = applyLUT(aces_cct);
161
162
163
164 return acescct_to_linear(graded);
165
166}
167
168
169
170
171
172
173
174uniform float uTime;
176
177const int samples = 16;
178
179
180uniform float uFocalLength;
181uniform float uFStop;
182uniform float uSensorHeight;
183uniform float uDOFMaxBlurRadius;
184struct MeshNode
185{
186 vec4 min;
187 vec4 max;
188 vec4 center;
190 uint instance_count;
191 uint static_mesh;
192 uint lm_offset;
193 uint material;
194 uint first;
195 uint count;
196 uint pad0;
197 uint pad1;
198};
199
200#define MAX_MESH_NODES 1024
201layout(std430, binding = 15) buffer MeshNodeData {
202
203 float focal_distance;
204 uint pad0;
205 uint pad1;
206 uint pad2;
208
209};
210
211vec3 get_view_pos_from_depth(vec2 uv, float depth, mat4 invProjMatrix) {
212 vec4 ndc = vec4(uv * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
213
214 vec4 viewSpacePos = invProjMatrix * ndc;
215
216 return viewSpacePos.xyz / viewSpacePos.w;
217}
218
219void main()
220{
221 vec2 uv = vUV;
222 vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
223
224 vec4 sc = texture(uScene, uv);
225 vec4 text = texture(uText, uv);
226
227 #ifdef DEPTH
228 float s = texture(uDPP, uv).r;
229 fragColor = vec4(s, s, s, 1.0);
230 return;
231 #endif
232
233 #ifdef VEL
234 fragColor = vec4(texture(gVel, uv).rg, 0.0, 1.0);
235 return;
236 #endif
237
238 #ifdef BOKEH
239 vec4 bokeh = texture(uBokeh, uv);
240 fragColor = vec4(bokeh.rgb, 1.0);
241 return;
242 #endif
243 #ifdef LM
244 vec4 lm = texture(uLM, uv);
245 fragColor = vec4(lm.rgb, 1.0);
246 return;
247 #endif
248
249
250
251 float framerate_scale = uRenderFramerate / uPhysicsFramerate;
252 #ifdef MBLUR
253
254 vec2 velocity = texture(gVel, uv).rg;
255
256 velocity *= 0.7;
257
258 vec2 texelStep = velocity / float(samples - 1);
259
260 vec2 currentTexCoords = uv - (velocity * 0.7);
261
262 for (int i = 1; i < samples; ++i) {
263 currentTexCoords += texelStep;
264 sc += textureLod(uSSR, currentTexCoords, 0);
265 }
266
267 sc /= samples;
268 #endif
269
270 #ifdef DOF
271
272 float rawDepth = texture(uDPP, uv).r;
273
274 vec3 viewPos = get_view_pos_from_depth(uv, rawDepth, unjittered_inv_projection);
275 float pixelDist = -viewPos.z;
276
277 float coeff = (uFocalLength * uFocalLength) / (uFStop * (focal_distance - uFocalLength));
278 float cocMeters = coeff * ((pixelDist - focal_distance) / pixelDist);
279 float cocUV = cocMeters / uSensorHeight;
280
281
282 float finalCoC = clamp(cocUV, -uDOFMaxBlurRadius, uDOFMaxBlurRadius);
283
284 float blendFactor = abs(finalCoC) / uDOFMaxBlurRadius;
285
286 blendFactor = smoothstep(0.05, 0.9, blendFactor);
287
288 vec4 bokeh = texture(uBokeh, uv);
289
290 sc.rgb = mix(sc.rgb, bokeh.rgb, blendFactor);
291
292 #endif
293
294
295
296 color.rgb = mix(sc.rgb, text.rgb, text.a);
297
298
299 #ifdef BLOOM
300 vec4 bloom = texture(uBloom, uv);
301 vec3 halationSample = texture(uBloom, uv + vec2(0.003, 0.0)).rgb;
302 vec3 halationColor = vec3(halationSample.r * 1.5, halationSample.g * 0.2, 0.0);
303
304 color.rgb += bloom.rgb * 0.3f;
305 #endif
306 vec2 st = gl_FragCoord.xy / resolution.xy;
307
308 #ifdef NOISE
309
310 float noise = fract(sin(dot(st.xy + vec2(uTime * 0.001, 0), vec2(12.9898, 78.233))) * 43758.5453123);
311 vec3 nc = color.rgb * noise;
312 color.rgb = mix(color.rgb, nc, 0.25);
313
314 #endif
315
316
317
318
319 int lastLOD = textureQueryLevels(uScene) - 1;
320 vec3 avgColor = textureLod(uScene, vec2(0.5, 0.5), lastLOD).rgb;
321
322 float autoExposure = calculateExposure(avgColor);
323
324 vec3 aces = color.rgb;
325
326 #ifdef ACES
327 aces = ACESFilm(color.rgb);
328 #endif
329 #ifdef LUT
330 aces = processLUT(max(color.rgb, 0.00001));
331 #endif
332 float gamma = 2.2;
333
334 aces.rgb = pow(aces, vec3(1.0/gamma));
335 fragColor = vec4(aces.rgb, 1.0);
336}
layout(local_size_x=16, local_size_y=16) in
void main()
uniform sampler2D uScene
Definition bloom.frag:3
out vec4 fragColor
Definition bloom.frag:6
uniform sampler2D uBloom
uniform sampler2D uCOC
Definition bokeh.frag:17
uniform float uFocalLength
Definition coc.frag:37
vec3 get_view_pos_from_depth(vec2 uv, float depth, mat4 invProjMatrix)
Definition coc.frag:22
uniform float uSensorHeight
Definition coc.frag:39
uniform float uFStop
Definition coc.frag:38
uniform sampler2D uFBM
Definition dynamic.frag:226
uniform int material
Definition dynamic.frag:215
uniform sampler2D uSSR
Definition dynamic.frag:229
#define MAX_MESH_NODES
Maximum MeshNodes in a scene (used for SSBO sizing).
in vec2 uv
uint pad1
Definition coc.frag:55
uint first
Definition coc.frag:52
vec4 min
Definition coc.frag:44
vec4 max
Definition coc.frag:45
uint lm_offset
Definition coc.frag:50
uint pad0
Definition coc.frag:54
vec4 center
Definition coc.frag:46
uint instance_count
Definition coc.frag:48
bool static_mesh
If this mesh is static (DrawArrays) or dynamic (DrawElements), used to indicate Lightmap status.
size_t count
Count of vertices pushed into the MAIN_VBO.
vec4 extents
Definition coc.frag:47
MeshNodes that describe the position of Meshes, GLTF nodes for GLTFAnimations, or hold references to ...
in vec2 vUV
Definition lightmap.frag:13
float smoothstep(float a, float b, float c)
Definition mathf.h:27
float clamp(float a, float b, float c)
Definition mathf.h:379
uniform float uRenderFramerate
uniform sampler3D uColorLUT
uniform uint uMBLUR
uniform sampler2D uHistory
vec3 acescct_to_linear(vec3 graded)
uniform sampler2D uBokeh
uniform sampler2D gVel
uniform sampler2D uLM
uniform sampler2D uCurrentSSR
uniform uint uDOF
vec3 vignette(vec3 color, float u, float v)
const int samples
float linear_to_acescct_channel(float x)
vec3 processLUT(vec3 color)
uniform sampler2D uDPP
uniform float uDOFMaxBlurRadius
uniform sampler3D uLUT
const mat3 ap1_to_srgb
vec3 linear_to_acescct(vec3 linearRGB)
uniform float uPhysicsFramerate
float calculateExposure(vec3 avgColor)
uniform float uTime
uniform uint uWireframeToggled
float acescct_to_linear_channel(float x)
vec3 ACESFilm(vec3 x)
vec3 applyLUT(vec3 color)
uniform sampler2D uText
const mat3 srgb_to_ap1
uniform mat4 projection
Definition shadow.vert:15
uniform mat4 view
Definition shadow.vert:16
float w
Definition types.h:13
float z
Definition types.h:12
Definition types.h:16
Definition types.h:20
Definition types.h:21
Definition types.h:11
Definition types.h:12
Definition types.h:13
uniform vec3 color
Definition wireframe.frag:6