Untitled
Loading...
Searching...
No Matches
focal_distance.comp
Go to the documentation of this file.
1#version 430 core
2
3layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
4
5
6layout(std140, binding = 0) uniform CameraData {
7 vec4 cam_pos;
8 vec4 cam_vel;
10 mat4 unjittered_projection;
11 mat4 view;
12 mat4 inv_projection;
13 mat4 unjittered_inv_projection;
14 mat4 inv_view;
15};
16
17struct MeshNode
18{
19 vec4 min;
20 vec4 max;
21 vec4 center;
22 vec4 extents;
23 uint instance_count;
24 uint static_mesh;
25 uint lm_offset;
26 uint material;
27 uint first;
28 uint count;
29 uint pad0;
30 uint pad1;
31};
32
33#define MAX_MESH_NODES 1024
34layout(std430, binding = 15) buffer MeshNodeData {
35
36 float focal_distance;
37 uint pad0;
38 uint pad1;
39 uint pad2;
41
42};
43
44uniform sampler2D gDepth;
47uniform float uDT;
48
49vec3 get_view_pos_from_depth(vec2 uv, float depth, mat4 invProjMatrix) {
50 vec4 ndc = vec4(uv * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
51
52 vec4 viewSpacePos = invProjMatrix * ndc;
53
54 return viewSpacePos.xyz / viewSpacePos.w;
55}
56
57
58float linearize(float z)
59{
60 float near = 0.1;
61 float far = 1000.0;
62 return (2.0 * near) / (far + near - z * (far - near));
63}
64
65void main()
66{
67 if (gl_GlobalInvocationID.x > 0) return;
68
69 ivec2 uv = ivec2(uResolution/2.0f);
70 vec2 suv = vec2(0.5, 0.5);
71 float old_focal_distance = focal_distance;
72 float acc = 0.0;
73 for (int y = -4; y <= 4; y++)
74 {
75 for (int x = -4; x <= 4; x++)
76 {
77
78 vec2 tuv = vec2(x, y) * uTexelSize;
79 ivec2 current_uv = uv + ivec2(uTexelSize * ivec2(x, y));
80 float depth = texelFetch(gDepth, current_uv, 0).r;
81 vec3 viewPos = get_view_pos_from_depth(suv + tuv, depth, unjittered_inv_projection);
82
83 float pixelDistance = -(viewPos.z);
84
85 acc += pixelDistance;
86
87 }
88 }
89
90
91 float d = acc / 81;
92
93 float targetDiopter = 1.0 / max(d, 0.05);
94 float currentDiopter = 1.0 / max(focal_distance, 0.05);
95
96 float blend = 1.0 - exp(-5.0 * uDT);
97
98 currentDiopter = mix(currentDiopter, targetDiopter, blend);
99
100 focal_distance = 1.0 / currentDiopter;
101
102
103
104}
layout(local_size_x=16, local_size_y=16) in
void main()
uniform vec2 uTexelSize
Definition bokeh.frag:18
uniform sampler2D gDepth
Definition coc.frag:19
vec3 get_view_pos_from_depth(vec2 uv, float depth, mat4 invProjMatrix)
Definition coc.frag:22
float linearize(float z)
Definition coc.frag:30
#define MAX_MESH_NODES
Maximum MeshNodes in a scene (used for SSBO sizing).
uniform float uDT
uniform vec2 uResolution
in vec2 uv
uint pad1
Definition coc.frag:55
uint first
Definition coc.frag:52
vec4 min
Definition coc.frag:44
uint material
Definition coc.frag:51
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 ...
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:14
Definition types.h:21
Definition types.h:11
Definition types.h:12
Definition types.h:13