| 1 |
#[compute] |
| 2 |
|
| 3 |
#version 450 |
| 4 |
|
| 5 |
#VERSION_DEFINES |
| 6 |
|
| 7 |
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; |
| 8 |
|
| 9 |
#define MAX_CASCADES 8 |
| 10 |
|
| 11 |
layout(set = 0, binding = 1) uniform texture3D sdf_cascades[MAX_CASCADES]; |
| 12 |
layout(set = 0, binding = 2) uniform texture3D light_cascades[MAX_CASCADES]; |
| 13 |
layout(set = 0, binding = 3) uniform texture3D aniso0_cascades[MAX_CASCADES]; |
| 14 |
layout(set = 0, binding = 4) uniform texture3D aniso1_cascades[MAX_CASCADES]; |
| 15 |
layout(set = 0, binding = 5) uniform texture3D occlusion_texture; |
| 16 |
|
| 17 |
layout(set = 0, binding = 8) uniform sampler linear_sampler; |
| 18 |
|
| 19 |
struct CascadeData { |
| 20 |
vec3 offset; //offset of (0,0,0) in world coordinates |
| 21 |
float to_cell; // 1/bounds * grid_size |
| 22 |
ivec3 probe_world_offset; |
| 23 |
uint pad; |
| 24 |
vec4 pad2; |
| 25 |
}; |
| 26 |
|
| 27 |
layout(set = 0, binding = 9, std140) uniform Cascades { |
| 28 |
CascadeData data[MAX_CASCADES]; |
| 29 |
} |
| 30 |
cascades; |
| 31 |
|
| 32 |
layout(rgba16f, set = 0, binding = 10) uniform restrict writeonly image2D screen_buffer; |
| 33 |
|
| 34 |
layout(set = 0, binding = 11) uniform texture2DArray lightprobe_texture; |
| 35 |
|
| 36 |
layout(push_constant, std430) uniform Params { |
| 37 |
vec3 grid_size; |
| 38 |
uint max_cascades; |
| 39 |
|
| 40 |
ivec2 screen_size; |
| 41 |
float y_mult; |
| 42 |
|
| 43 |
float z_near; |
| 44 |
|
| 45 |
mat3x4 inv_projection; |
| 46 |
// We pack these more tightly than mat3 and vec3, which will require some reconstruction trickery. |
| 47 |
float cam_basis[3][3]; |
| 48 |
float cam_origin[3]; |
| 49 |
} |
| 50 |
params; |
| 51 |
|
| 52 |
vec3 linear_to_srgb(vec3 color) { |
| 53 |
//if going to srgb, clamp from 0 to 1. |
| 54 |
color = clamp(color, vec3(0.0), vec3(1.0)); |
| 55 |
const vec3 a = vec3(0.055f); |
| 56 |
return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f))); |
| 57 |
} |
| 58 |
|
| 59 |
vec2 octahedron_wrap(vec2 v) { |
| 60 |
vec2 signVal; |
| 61 |
signVal.x = v.x >= 0.0 ? 1.0 : -1.0; |
| 62 |
signVal.y = v.y >= 0.0 ? 1.0 : -1.0; |
| 63 |
return (1.0 - abs(v.yx)) * signVal; |
| 64 |
} |
| 65 |
|
| 66 |
vec2 octahedron_encode(vec3 n) { |
| 67 |
// https://twitter.com/Stubbesaurus/status/937994790553227264 |
| 68 |
n /= (abs(n.x) + abs(n.y) + abs(n.z)); |
| 69 |
n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy); |
| 70 |
n.xy = n.xy * 0.5 + 0.5; |
| 71 |
return n.xy; |
| 72 |
} |
| 73 |
|
| 74 |
void main() { |
| 75 |
// Pixel being shaded |
| 76 |
ivec2 screen_pos = ivec2(gl_GlobalInvocationID.xy); |
| 77 |
if (any(greaterThanEqual(screen_pos, params.screen_size))) { //too large, do nothing |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
vec3 ray_pos; |
| 82 |
vec3 ray_dir; |
| 83 |
{ |
| 84 |
ray_pos = vec3(params.cam_origin[0], params.cam_origin[1], params.cam_origin[2]); |
| 85 |
|
| 86 |
ray_dir.xy = ((vec2(screen_pos) / vec2(params.screen_size)) * 2.0 - 1.0); |
| 87 |
ray_dir.z = params.z_near; |
| 88 |
|
| 89 |
ray_dir = (vec4(ray_dir, 1.0) * mat4(params.inv_projection)).xyz; |
| 90 |
|
| 91 |
mat3 cam_basis; |
| 92 |
{ |
| 93 |
vec3 c0 = vec3(params.cam_basis[0][0], params.cam_basis[0][1], params.cam_basis[0][2]); |
| 94 |
vec3 c1 = vec3(params.cam_basis[1][0], params.cam_basis[1][1], params.cam_basis[1][2]); |
| 95 |
vec3 c2 = vec3(params.cam_basis[2][0], params.cam_basis[2][1], params.cam_basis[2][2]); |
| 96 |
cam_basis = mat3(c0, c1, c2); |
| 97 |
} |
| 98 |
ray_dir = normalize(cam_basis * ray_dir); |
| 99 |
} |
| 100 |
|
| 101 |
ray_pos.y *= params.y_mult; |
| 102 |
ray_dir.y *= params.y_mult; |
| 103 |
ray_dir = normalize(ray_dir); |
| 104 |
|
| 105 |
vec3 pos_to_uvw = 1.0 / params.grid_size; |
| 106 |
|
| 107 |
vec3 light = vec3(0.0); |
| 108 |
float blend = 0.0; |
| 109 |
|
| 110 |
#if 1 |
| 111 |
// No interpolation |
| 112 |
|
| 113 |
vec3 inv_dir = 1.0 / ray_dir; |
| 114 |
|
| 115 |
float rough = 0.5; |
| 116 |
bool hit = false; |
| 117 |
|
| 118 |
for (uint i = 0; i < params.max_cascades; i++) { |
| 119 |
//convert to local bounds |
| 120 |
vec3 pos = ray_pos - cascades.data[i].offset; |
| 121 |
pos *= cascades.data[i].to_cell; |
| 122 |
|
| 123 |
// Should never happen for debug, since we start mostly at the bounds center, |
| 124 |
// but add anyway. |
| 125 |
//if (any(lessThan(pos,vec3(0.0))) || any(greaterThanEqual(pos,params.grid_size))) { |
| 126 |
// continue; //already past bounds for this cascade, goto next |
| 127 |
//} |
| 128 |
|
| 129 |
//find maximum advance distance (until reaching bounds) |
| 130 |
vec3 t0 = -pos * inv_dir; |
| 131 |
vec3 t1 = (params.grid_size - pos) * inv_dir; |
| 132 |
vec3 tmax = max(t0, t1); |
| 133 |
float max_advance = min(tmax.x, min(tmax.y, tmax.z)); |
| 134 |
|
| 135 |
float advance = 0.0; |
| 136 |
vec3 uvw; |
| 137 |
hit = false; |
| 138 |
|
| 139 |
while (advance < max_advance) { |
| 140 |
//read how much to advance from SDF |
| 141 |
uvw = (pos + ray_dir * advance) * pos_to_uvw; |
| 142 |
|
| 143 |
float distance = texture(sampler3D(sdf_cascades[i], linear_sampler), uvw).r * 255.0 - 1.7; |
| 144 |
|
| 145 |
if (distance < 0.001) { |
| 146 |
//consider hit |
| 147 |
hit = true; |
| 148 |
break; |
| 149 |
} |
| 150 |
|
| 151 |
advance += distance; |
| 152 |
} |
| 153 |
|
| 154 |
if (!hit) { |
| 155 |
pos += ray_dir * min(advance, max_advance); |
| 156 |
pos /= cascades.data[i].to_cell; |
| 157 |
pos += cascades.data[i].offset; |
| 158 |
ray_pos = pos; |
| 159 |
continue; |
| 160 |
} |
| 161 |
|
| 162 |
//compute albedo, emission and normal at hit point |
| 163 |
|
| 164 |
const float EPSILON = 0.001; |
| 165 |
vec3 hit_normal = normalize(vec3( |
| 166 |
texture(sampler3D(sdf_cascades[i], linear_sampler), uvw + vec3(EPSILON, 0.0, 0.0)).r - texture(sampler3D(sdf_cascades[i], linear_sampler), uvw - vec3(EPSILON, 0.0, 0.0)).r, |
| 167 |
texture(sampler3D(sdf_cascades[i], linear_sampler), uvw + vec3(0.0, EPSILON, 0.0)).r - texture(sampler3D(sdf_cascades[i], linear_sampler), uvw - vec3(0.0, EPSILON, 0.0)).r, |
| 168 |
texture(sampler3D(sdf_cascades[i], linear_sampler), uvw + vec3(0.0, 0.0, EPSILON)).r - texture(sampler3D(sdf_cascades[i], linear_sampler), uvw - vec3(0.0, 0.0, EPSILON)).r)); |
| 169 |
|
| 170 |
vec3 hit_light = texture(sampler3D(light_cascades[i], linear_sampler), uvw).rgb; |
| 171 |
vec4 aniso0 = texture(sampler3D(aniso0_cascades[i], linear_sampler), uvw); |
| 172 |
vec3 hit_aniso0 = aniso0.rgb; |
| 173 |
vec3 hit_aniso1 = vec3(aniso0.a, texture(sampler3D(aniso1_cascades[i], linear_sampler), uvw).rg); |
| 174 |
|
| 175 |
hit_light *= (dot(max(vec3(0.0), (hit_normal * hit_aniso0)), vec3(1.0)) + dot(max(vec3(0.0), (-hit_normal * hit_aniso1)), vec3(1.0))); |
| 176 |
|
| 177 |
light = hit_light; |
| 178 |
|
| 179 |
break; |
| 180 |
} |
| 181 |
|
| 182 |
#endif |
| 183 |
|
| 184 |
imageStore(screen_buffer, screen_pos, vec4(linear_to_srgb(light), 1.0)); |
| 185 |
} |