Skip to content

Commit e552021

Browse files
final form of forward error propagation
1 parent 1c682b9 commit e552021

File tree

5 files changed

+76
-38
lines changed

5 files changed

+76
-38
lines changed

examples_tests/22.RaytracedAO/closestHit.comp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void main()
6969

7070
// positions
7171
mat2x3 dPdBary_error; vec3 lastVxPos_error;
72-
const vec3 last_vx_pos = load_positions(dPdBary_error,lastVxPos_error,indices,batchInstanceData);
72+
const vec3 lastVxPos = load_positions(dPdBary_error,lastVxPos_error,indices,batchInstanceData);
7373
const vec3 geomNormal = cross(dPdBary[0],dPdBary[1]);
7474
const bool frontfacing = bool((batchInstanceData.determinantSignBit^floatBitsToUint(dot(geomNormal,normalizedV)))&0x80000000u);
7575

@@ -88,22 +88,29 @@ void main()
8888
,mat2(0.0) // TODO: Covariance Rendering
8989
#endif
9090
);
91-
92-
const vec3 hitWorldPos = dPdBary*compactBary.xy+last_vx_pos;
91+
92+
vec3 origin_error;
93+
const float compactBary_error = 0.f; // TODO:
94+
const vec3 origin = nbl_glsl_interpolate_with_bounds(origin_error,dPdBary,dPdBary_error,lastVxPos,lastVxPos_error,compactBary,compactBary_error);
9395

9496
generate_next_rays(
9597
MAX_RAYS_GENERATED,material,frontfacing,vertex_depth,scramble_start_state,
96-
sampleID,outPixelLocation,hitWorldPos,throughput
98+
sampleID,outPixelLocation,origin,origin_error,throughput
9799
);
98100
}
101+
#if 1
102+
if (vertex_depth!=2u)
103+
emissive = vec3(0.f);
104+
#endif
99105
}
100-
106+
#if 0
101107
// TODO: finish MIS
102108
vec3 acc;
103109
const uvec3 accumulationLocation = uvec3(outPixelLocation,sampleID%staticViewData.samplesPerPixelPerDispatch);
104110
const bool first_accumulating_path_vertex = accumulationLocation.z!=0u||get_path_vertex_depth()==2u;
105111
const bool notFirstFrame = pc.cummon.rcpFramesDispatched!=1.f;
106112
if (record_emission_common(acc,accumulationLocation,emissive*throughput,first_accumulating_path_vertex))
107113
storeAccumulation(acc,accumulationLocation);
114+
#endif
108115
}
109116
}

examples_tests/22.RaytracedAO/raygen.comp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,25 @@ void main()
5555
const uint vertex_depth_mod_2 = 0x1u;
5656
// load vertex data
5757
mat2x3 dPdBary_error; vec3 lastVxPos_error;
58-
const vec3 last_vx_pos = load_positions(dPdBary_error,lastVxPos_error,indices,batchInstanceData);
58+
const vec3 lastVxPos = load_positions(dPdBary_error,lastVxPos_error,indices,batchInstanceData);
5959
const nbl_glsl_xoroshiro64star_state_t scramble_start_state = load_aux_vertex_attrs(compactBary,indices,batchInstanceData,material,outPixelLocation,vertex_depth_mod_2
6060
#ifdef TEX_PREFETCH_STREAM
6161
,dBarydScreen
6262
#endif
6363
);
6464

65-
const vec3 hitWorldPos = dPdBary*compactBary.xy+last_vx_pos;
66-
normalizedV = normalize(pc.cummon.camPos-hitWorldPos);
65+
vec3 origin_error;
66+
const float compactBary_error = exp2(-16.f); // UNORM16 being used for storage
67+
const vec3 origin = nbl_glsl_interpolate_with_bounds(origin_error,dPdBary,dPdBary_error,lastVxPos,lastVxPos_error,compactBary,compactBary_error);
68+
normalizedV = normalize(pc.cummon.camPos-origin);
69+
70+
emissive = vec3(1000.f)*dot(abs(normalizedN),origin_error)*1.03125f;
6771

6872
// generate rays
6973
const uint vertex_depth = 1u;
7074
generate_next_rays(
7175
staticViewData.samplesPerPixelPerDispatch,material,frontfacing,vertex_depth,
72-
scramble_start_state,pc.cummon.samplesComputed,outPixelLocation,hitWorldPos,vec3(1.0)
76+
scramble_start_state,pc.cummon.samplesComputed,outPixelLocation,origin,origin_error,vec3(1.0)
7377
);
7478
}
7579

examples_tests/22.RaytracedAO/raytraceCommon.glsl

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,18 @@ vec3 load_positions(out mat2x3 dPdBary_error, out vec3 lastVxPos_error,in uvec3
129129
);
130130
const mat4x3 tform = batchInstanceData.tform;
131131
mat3 positions_error;
132-
// for when we quantize positions to RGB21_UNORM
133-
const float quantizationError = exp2(-16.f);// nbl_glsl_numeric_limits_float_epsilon(1u);
132+
// when we quantize positions to RGB21_UNORM, change to exp2(-22.f)
133+
const float quantizationError = nbl_glsl_numeric_limits_float_epsilon(1u);
134134
positions = nbl_glsl_mul_with_bounds_wo_gamma(positions_error,mat3(tform),positions,quantizationError);
135+
positions_error *= nbl_glsl_ieee754_gamma(2u);
135136
//
136-
const float accum_error_factor = nbl_glsl_ieee754_gamma(2u)+nbl_glsl_ieee754_gamma(3u);
137137
for (int i=0; i<2; i++)
138138
{
139-
dPdBary[i] = positions[i]-positions[2];
140-
dPdBary_error[i] = abs(dPdBary[i])*nbl_glsl_ieee754_gamma(1u);
141-
dPdBary_error[i] += (positions_error[i]+positions_error[2])*accum_error_factor;
139+
dPdBary[i] = nbl_glsl_ieee754_sub_with_bounds_wo_gamma(dPdBary_error[i],positions[i],positions_error[i],positions[2],positions_error[2]);
140+
dPdBary_error[i] *= nbl_glsl_ieee754_gamma(1u);
142141
}
143-
const vec3 lastVx = positions[2]+tform[3];
144-
lastVxPos_error = abs(positions[2])*quantizationError;
145-
lastVxPos_error += (abs(lastVx)+lastVxPos_error[2])*nbl_glsl_ieee754_gamma(1u);
142+
const vec3 lastVx = nbl_glsl_ieee754_add_with_bounds_wo_gamma(lastVxPos_error,positions[2],positions_error[2],tform[3],vec3(0.f));
143+
lastVxPos_error *= nbl_glsl_ieee754_gamma(1u);
146144
return lastVx;
147145
}
148146

@@ -207,27 +205,30 @@ nbl_glsl_xoroshiro64star_state_t load_aux_vertex_attrs(
207205
}
208206

209207
// TODO MOVE to some other header!
210-
vec3 nbl_glsl_interpolate_with_bounds(out vec3 error, in mat2x3 triangleEdges, in mat2x3 triangleEdges_error, in vec3 origin, in vec3 origin_error, in vec2 compactBary)
208+
vec3 nbl_glsl_interpolate_with_bounds(out vec3 error, in mat2x3 triangleEdges, in mat2x3 triangleEdges_error,
209+
in vec3 origin, in vec3 origin_error, in vec2 compactBary, in float compactBary_error
210+
)
211211
{
212-
return triangleEdges*compactBary+origin;
212+
vec3 error1;
213+
const vec3 col1 = nbl_glsl_ieee754_mul_with_bounds_wo_gamma(error1,triangleEdges[0],triangleEdges_error[0],compactBary.x,compactBary_error);
214+
error1 *= nbl_glsl_ieee754_gamma(1u);
215+
vec3 error2;
216+
const vec3 col2 = nbl_glsl_ieee754_mul_with_bounds_wo_gamma(error2,triangleEdges[1],triangleEdges_error[1],compactBary.y,compactBary_error);
217+
error2 *= nbl_glsl_ieee754_gamma(1u);
218+
vec3 error3;
219+
vec3 retval = nbl_glsl_ieee754_add_with_bounds_wo_gamma(error3,col1,error1,col2,error2);
220+
error3 *= nbl_glsl_ieee754_gamma(1u);
221+
retval = nbl_glsl_ieee754_add_with_bounds_wo_gamma(error,retval,error3,origin,origin_error);
222+
//error *= nbl_glsl_ieee754_gamma(1u);
223+
error *= nbl_glsl_ieee754_gamma(8u); // cause PBRT says so
224+
return retval;
213225
}
214226
// robust ray origins
215-
vec3 nbl_glsl_robust_ray_origin_fastest(in vec3 origin, in vec3 direction, float error, vec3 normal)
227+
vec3 nbl_glsl_robust_ray_origin_impl(in vec3 origin, in vec3 direction, float offset, vec3 normal)
216228
{
217229
// flip it in the correct direction
218-
error = uintBitsToFloat(floatBitsToUint(error)^(floatBitsToUint(dot(normal,direction))&0x80000000u));
219-
return origin+normal*error;
220-
}
221-
vec3 nbl_glsl_robust_ray_origin_fast(in vec3 origin, in vec3 direction, in vec3 error, in vec3 normal)
222-
{
223-
// anticipate that the error could have increased from manipulation
224-
return nbl_glsl_robust_ray_origin_fastest(origin,direction,error.x+error.y+error.z,normal);
225-
}
226-
vec3 nbl_glsl_robust_ray_origin(in vec3 origin, in vec3 direction, in vec3 error, in vec3 normal)
227-
{
228-
const float d = dot(abs(normal),error);
229-
// anticipate that the error could have increased from manipulation
230-
return nbl_glsl_robust_ray_origin_fastest(origin,direction,d,normal);
230+
offset = uintBitsToFloat(floatBitsToUint(offset)^(floatBitsToUint(dot(normal,direction))&0x80000000u));
231+
return origin+normal*offset;
231232
}
232233

233234

@@ -259,7 +260,7 @@ void gen_sample_ray(
259260
void generate_next_rays(
260261
in uint maxRaysToGen, in nbl_glsl_MC_oriented_material_t material, in bool frontfacing, in uint vertex_depth,
261262
in nbl_glsl_xoroshiro64star_state_t scramble_start_state, in uint sampleID, in uvec2 outPixelLocation,
262-
in vec3 origin, in vec3 prevThroughput)
263+
in vec3 origin, in vec3 origin_error, in vec3 prevThroughput)
263264
{
264265
// get material streams as well
265266
const nbl_glsl_MC_instr_stream_t gcs = nbl_glsl_MC_oriented_material_t_getGenChoiceStream(material);
@@ -301,14 +302,14 @@ for (uint i=1u; i!=vertex_depth; i++)
301302
// TODO: investigate workgroup reductions here
302303
const uint baseOutputID = atomicAdd(rayCount[pc.cummon.rayCountWriteIx],raysToAllocate);
303304

304-
// TODO: improve
305-
const vec3 error = vec3(0.01f);// abs(origin)* uintBitsToFloat(0x35480005);
305+
// adjust for the fact that the normal might be too short (inversesqrt precision)
306+
const float ray_offset = dot(abs(normalizedN),origin_error)*1.03125f;
306307
uint offset = 0u;
307308
for (uint i=0u; i<maxRaysToGen; i++)
308309
if (maxT[i]!=0.f)
309310
{
310311
nbl_glsl_ext_RadeonRays_ray newRay;
311-
newRay.origin = nbl_glsl_robust_ray_origin(origin,direction[i],error,normalizedN);
312+
newRay.origin = nbl_glsl_robust_ray_origin_impl(origin,direction[i],ray_offset,normalizedN);
312313
newRay.maxT = maxT[i];
313314
newRay.direction = direction[i];
314315
newRay.time = packOutPixelLocation(outPixelLocation);

include/nbl/builtin/glsl/limits/numeric.glsl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,29 @@ float nbl_glsl_ieee754_rcpgamma(uint n)
9090
return nbl_glsl_ieee754_rcpgamma(float(n));
9191
}
9292

93+
// TODO: move to some other header (maybe ieee754.glsl)
94+
vec3 nbl_glsl_ieee754_add_with_bounds_wo_gamma(out vec3 error, in vec3 a, in vec3 a_error, in vec3 b, in vec3 b_error)
95+
{
96+
error = (a_error+b_error)/nbl_glsl_numeric_limits_float_epsilon(1u);
97+
vec3 sum = a+b;
98+
error += abs(sum);
99+
return sum;
100+
}
101+
vec3 nbl_glsl_ieee754_sub_with_bounds_wo_gamma(out vec3 error, in vec3 a, in vec3 a_error, in vec3 b, in vec3 b_error)
102+
{
103+
error = (a_error+b_error)/nbl_glsl_numeric_limits_float_epsilon(1u);
104+
vec3 sum = a-b;
105+
error += abs(sum);
106+
return sum;
107+
}
108+
vec3 nbl_glsl_ieee754_mul_with_bounds_wo_gamma(out vec3 error, in vec3 a, in vec3 a_error, in float b, in float b_error)
109+
{
110+
vec3 crossCorrelationA = abs(a)*b_error;
111+
vec3 crossCorrelationB = a_error*abs(b);
112+
error = (crossCorrelationB+crossCorrelationA+crossCorrelationB*crossCorrelationA)/nbl_glsl_numeric_limits_float_epsilon(1u);
113+
vec3 product = a*b;
114+
error += abs(product);
115+
return product;
116+
}
117+
93118
#endif

include/nbl/builtin/glsl/utils/transform.glsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef _NBL_BUILTIN_GLSL_UTILS_TRANSFORM_INCLUDED_
66
#define _NBL_BUILTIN_GLSL_UTILS_TRANSFORM_INCLUDED_
77

8+
// move to ieee754 header?
89
mat3 nbl_glsl_mul_with_bounds_wo_gamma(out mat3 error, in mat3 a, in mat3 b, in float b_relative_error)
910
{
1011
mat3 retval;

0 commit comments

Comments
 (0)