Skip to content

Commit

Permalink
Added support for polygonal apertures.
Browse files Browse the repository at this point in the history
  • Loading branch information
apanteleev committed Dec 4, 2019
1 parent 7e1ab41 commit 559d296
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/refresh/vkpt/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,9 @@ prepare_ubo(refdef_t *fd, mleaf_t* viewleaf, const reference_mode_t* ref_mode, c
ubo->pt_aperture = 0.f;
}

// number of polygon vertices must be an integer
ubo->pt_aperture_type = roundf(ubo->pt_aperture_type);

ubo->temporal_blend_factor = ref_mode->temporal_blend_factor;
ubo->flt_enable = ref_mode->enable_denoiser;
ubo->flt_taa = ubo->flt_taa && ref_mode->enable_denoiser;
Expand Down Expand Up @@ -2905,6 +2908,8 @@ R_Init_RTX(qboolean total)

cvar_pt_dof->changed = dof_cvar_changed;
cvar_pt_aperture->changed = dof_cvar_changed;
cvar_pt_aperture_type->changed = dof_cvar_changed;
cvar_pt_aperture_angle->changed = dof_cvar_changed;
cvar_pt_focus->changed = dof_cvar_changed;

cvar_pt_num_bounce_rays->flags |= CVAR_ARCHIVE;
Expand Down
2 changes: 2 additions & 0 deletions src/refresh/vkpt/shader/global_ubo.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
UBO_CVAR_DO(flt_temporal_lf, 1) \
UBO_CVAR_DO(flt_temporal_spec, 1) \
UBO_CVAR_DO(pt_aperture, 2.0) /* aperture size for the Depth of Field effect, in world units */ \
UBO_CVAR_DO(pt_aperture_angle, 0) /* rotation of the polygonal aperture, [0..1] */ \
UBO_CVAR_DO(pt_aperture_type, 0) /* number of aperture polygon edges, circular if less than 3 */ \
UBO_CVAR_DO(pt_beam_softness, 1.0) /* beam softness */ \
UBO_CVAR_DO(pt_bump_scale, 1.0) /* scale for normal maps [0..1] */ \
UBO_CVAR_DO(pt_cameras, 1) /* switch for security cameras, 0 or 1 */ \
Expand Down
26 changes: 24 additions & 2 deletions src/refresh/vkpt/shader/primary_rays.rgen
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,30 @@ get_primary_ray(vec2 screen_pos)
// Offset the ray origin in the focal plane to simulate aperture.

vec2 uv = vec2(get_rng(RNG_PRIMARY_APERTURE_X), get_rng(RNG_PRIMARY_APERTURE_Y));
vec2 disk = sample_disk(uv) * global_ubo.pt_aperture;
vec3 offset = disk.x * right + disk.y * up;
vec2 planar_offset;

if(global_ubo.pt_aperture_type < 3)
{
planar_offset = sample_disk(uv);
}
else
{
float triangle = uv.x * global_ubo.pt_aperture_type;
uv.x = fract(triangle);
triangle = floor(triangle);

vec3 bary = sample_triangle(uv);
float section_angle = 2 * M_PI / global_ubo.pt_aperture_type;
float a1 = section_angle * (triangle + global_ubo.pt_aperture_angle );
float a2 = section_angle * (triangle + global_ubo.pt_aperture_angle + 1);

vec2 v1 = vec2(cos(a1), sin(a1));
vec2 v2 = vec2(cos(a2), sin(a2));
planar_offset = bary.x * v1 + bary.y * v2; // + bary.z * v0, where v0 is (0, 0)
}

planar_offset *= global_ubo.pt_aperture;
vec3 offset = planar_offset.x * right + planar_offset.y * up;

ray.origin = offset;
ray.direction = normalize(focal_point - ray.origin);
Expand Down

0 comments on commit 559d296

Please sign in to comment.