Skip to content

Commit

Permalink
Specular support in wavefront pt.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbikker committed Dec 20, 2024
1 parent c8c6093 commit f204b7f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
Binary file modified images/wavefront.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 12 additions & 12 deletions tiny_bvh_gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ void Init()
accumulator = new Buffer( N * sizeof( bvhvec4 ) );
raysIn = new Buffer( N * sizeof( bvhvec4 ) * 4 );
raysOut = new Buffer( N * sizeof( bvhvec4 ) * 4 );
connections = new Buffer( N * 2 * sizeof( bvhvec4 ) * 3 );
connections = new Buffer( N * 3 * sizeof( bvhvec4 ) * 3 );
accumulator = new Buffer( N * sizeof( bvhvec4 ) );
pixels = new Buffer( N * sizeof( uint32_t ) );
// load raw vertex data
AddMesh( "./testdata/cryteksponza.bin", 1, bvhvec3( 0 ), 0xffffff );
AddMesh( "./testdata/lucy.bin", 1.1f, bvhvec3( -2, 4.1f, -3 ), 0xaaaaff );
AddMesh( "./testdata/lucy.bin", 1.1f, bvhvec3( -2, 4.1f, -3 ), 0x2ffff88 );
AddQuad( bvhvec3( 0, 30, -1 ), 9, 5, 0x1ffffff ); // hard-coded light source
// build bvh (here: 'compressed wide bvh', for efficient GPU rendering)
bvh.Build( tris, triCount );
Expand All @@ -104,19 +104,19 @@ bool UpdateCamera( float delta_time_s, fenster& f )
{
bvhvec3 right = normalize( cross( bvhvec3( 0, 1, 0 ), rd.view ) ), up = 0.8f * cross( rd.view, right );
// get camera controls.
bool moved = false;
if (f.keys['A'] || f.keys['D']) rd.eye += right * delta_time_s * (f.keys['D'] ? 10 : -10), moved = true;
if (f.keys['W'] || f.keys['S']) rd.eye += rd.view * delta_time_s * (f.keys['W'] ? 10 : -10), moved = true;
if (f.keys['R'] || f.keys['F']) rd.eye += up * delta_time_s * (f.keys['R'] ? 20 : -20), moved = true;
if (f.keys[20]) rd.view = normalize( rd.view + right * -1.0f * delta_time_s ), moved = true;
if (f.keys[19]) rd.view = normalize( rd.view + right * delta_time_s ), moved = true;
if (f.keys[17]) rd.view = normalize( rd.view + up * -1.0f * delta_time_s ), moved = true;
if (f.keys[18]) rd.view = normalize( rd.view + up * delta_time_s ), moved = true;
float moved = 0, spd = 10.0f * delta_time_s;
if (f.keys['A'] || f.keys['D']) rd.eye += right * (f.keys['D'] ? spd : -spd), moved = 1;
if (f.keys['W'] || f.keys['S']) rd.eye += rd.view * (f.keys['W'] ? spd : -spd), moved = 1;
if (f.keys['R'] || f.keys['F']) rd.eye += up * 2.0f * (f.keys['R'] ? spd : -spd), moved = 1;
if (f.keys[20]) rd.view = normalize( rd.view + right * -0.1f * spd ), moved = 1;
if (f.keys[19]) rd.view = normalize( rd.view + right * 0.1f * spd ), moved = 1;
if (f.keys[17]) rd.view = normalize( rd.view + up * -0.1f * spd ), moved = 1;
if (f.keys[18]) rd.view = normalize( rd.view + up * 0.1f * spd ), moved = 1;
// recalculate right, up
right = normalize( cross( bvhvec3( 0, 1, 0 ), rd.view ) ), up = 0.8f * cross( rd.view, right );
bvhvec3 C = rd.eye + 1.2f * rd.view;
rd.p0 = C - right + up, rd.p1 = C + right + up, rd.p2 = C - right - up;
return moved;
return moved > 0;
}

// Application Tick
Expand All @@ -135,7 +135,7 @@ void Tick( float delta_time_s, fenster& f, uint32_t* buf )
init->Run( 1 ); // init atomic counters, set buffer ptrs etc.
generate->SetArguments( raysOut, spp * 19191 );
generate->Run2D( oclint2( SCRWIDTH, SCRHEIGHT ) );
for (int i = 0; i < 2; i++)
for (int i = 0; i < 3; i++)
{
swap( raysOut, raysIn );
extend->SetArguments( raysIn );
Expand Down
29 changes: 24 additions & 5 deletions wavefront.cl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ uint WangHash( uint s ) { s = (s ^ 61) ^ (s >> 16), s *= 9, s = s ^ (s >> 4), s
uint RandomUInt( uint* seed ) { *seed ^= *seed << 13, *seed ^= *seed >> 17, *seed ^= *seed << 5; return *seed; }
float RandomFloat( uint* seed ) { return RandomUInt( seed ) * 2.3283064365387e-10f; }

// Color conversion
float3 rgb32_to_vec3( uint c )
{
return (float3)( (float)((c >> 16) & 255), (float)((c >> 8) & 255), (float)(c & 255)) * 0.00392f;
}

// Specular reflection
float3 Reflect( const float3 D, const float3 N ) { return D - 2.0f * N * dot( N,D ); }

// DiffuseReflection: Uniform random bounce in the hemisphere
float3 DiffuseReflection( float3 N, uint* seed )
{
Expand Down Expand Up @@ -104,7 +113,7 @@ void kernel Generate( global struct PathState* raysOut, uint frameSeed )
const float u = ((float)x + RandomFloat( &seed )) / (float)get_global_size( 0 );
const float v = ((float)y + RandomFloat( &seed )) / (float)get_global_size( 1 );
const float4 P = rd.p0 + u * (rd.p1 - rd.p0) + v * (rd.p2 - rd.p0);
raysOut[id].T = (float4)( 1, 1, 1, 1 /* pdf */ );
raysOut[id].T = (float4)( 1, 1, 1, -1 /* pdf, or -1 for specular vertex */ );
raysOut[id].O = (float4)( rd.eye.xyz, as_float( id << 4 /* low bits: depth */ ) );
raysOut[id].D = (float4)( normalize( P.xyz - rd.eye.xyz ), 1e30f );
raysOut[id].hit = (float4)( 1e30f, 0, 0, as_float( 0 ) );
Expand Down Expand Up @@ -171,21 +180,31 @@ void kernel Shade( global float4* accumulator,
uint mat = as_uint( v0.w ) >> 24;
// end path on light
float3 lightColor = (float3)( 20 );
if (mat == 1)
if (mat == 1 /* light source */)
{
if (depth == 0) accumulator[pixelIdx] += (float4)( T * lightColor, 1 );
if (T4.w == -1) accumulator[pixelIdx] += (float4)( T * lightColor, 1 );
continue;
}
float3 vert0 = v0.xyz, vert1 = verts[vertIdx + 1].xyz, vert2 = verts[vertIdx + 2].xyz;
float3 N = normalize( cross( vert1 - vert0, vert2 - vert0 ) );
float3 D = D4.xyz;
if (dot( N, D ) > 0) N *= -1;
float3 I = O4.xyz + t * D;
float3 diff = rgb32_to_vec3( as_uint( v0.w ) ); // material color
// handle pure specular BRDF
if (mat == 2)
{
uint newRayIdx = atomic_inc( &extendTasks );
float3 R = Reflect( D, N );
raysOut[newRayIdx].T = (float4)( T * diff, -1 /* mark vertex as specular */ );
raysOut[newRayIdx].O = (float4)( I + R * 0.001f, as_float( (pixelIdx << 4) + depth + 1 ) );
raysOut[newRayIdx].D = (float4)( R, 1e30f );
continue;
}
// direct illumination: next event estimation
float3 P = (float3)( RandomFloat( &seed ) * 9 - 4.5f, 30, RandomFloat( &seed ) * 5 - 3.5f );
float3 L = P - I;
float NdotL = dot( N, L );
float3 diff = (float3)(1); // material color; simply white for now
float3 BRDF = diff * INVPI; // lambert BRDF: albedo / pi
if (NdotL > 0)
{
Expand All @@ -198,7 +217,7 @@ void kernel Shade( global float4* accumulator,
shadowOut[newShadowIdx].D = (float4)( L, dist - 0.002f );
}
// indirect illumination: diffuse bounce
if (depth < 2)
if (depth < 3)
{
uint newRayIdx = atomic_inc( &extendTasks );
float3 R = CosWeightedDiffReflection( N, &seed );
Expand Down

0 comments on commit f204b7f

Please sign in to comment.