     }
 
     // Raytracing constants
-    const int loop_max = 512;
+    const int loop_max = 128;
     const float ray_epsilon = 1.0005;
     const float hit_bias = 0.001;
 


}

void main() { 

    
    vec3 sslr_result = vec3(0);

    Material m = unpack_material(GBufferDepth, GBuffer0, GBuffer1, GBuffer2);
    vec3 view_dir = normalize(m.position - cameraPosition);
    vec3 reflected_dir = reflect(view_dir, m.normal );

    float scale_factor = 0.1 + saturate(distance(m.position, cameraPosition) / 1000.0) * 10.0;

    vec3 target_pos = m.position + reflected_dir * scale_factor;
    vec4 transformed_pos = currentViewProjMat * vec4(target_pos, 1);
    transformed_pos.xyz /= transformed_pos.w;
    transformed_pos.xyz = transformed_pos.xyz * 0.5 + 0.5;

    float pixel_depth = textureLod(DownscaledDepth, texcoord, 0).x;

    if (distance(m.position, cameraPosition) > 1000) {

    } else {

        vec3 ray_origin = vec3(texcoord, pixel_depth);
        vec3 ray_dest = transformed_pos.xyz;
        vec3 ray_direction = normalize(ray_dest - ray_origin);





        sslr_result = trace_ray_smart(m, ray_origin, ray_direction);

        // vec3 intersection_coord = trace_ray(ray_origin, ray_direction);
        // intersection_coord.z = 0.0;

    return vec3(0);
}





vec3 trace_ray_fast(vec3 ro, vec3 rd) {

    const int mipmap = 3;
    ivec2 work_size = textureSize(DownscaledDepth, mipmap).xy;

    rd = normalize(rd);

    // Compute step size
    vec2 ks = 1.0 / abs(rd.xy) / work_size;
    float kstep = min(ks.x, ks.y) * 1.001;
    vec3 pos = ro;

    int max_iter = 30;

    while (max_iter --> 0) {

        pos += kstep * rd;

        vec2 cell_z = textureLod(DownscaledDepth, pos.xy, mipmap).xy; 

        if (pos.z > cell_z.y) {
            return pos;
        }

    }

    return vec3(0);

}



vec3 trace_ray_smart(Material m, vec3 ro, vec3 rd)
{

    // vec3 intersection = trace_ray(ro, rd);
    vec3 intersection = trace_ray_fast(ro, rd);
    if(length(intersection) > 0.0001 && distance(intersection.xy, texcoord) > 0.001) {

        vec3 intersected_color = texture(ShadedScene, intersection.xy).xyz;
