Skip to content

Commit 1c682b9

Browse files
PBRT method is overkill, I know the relative error of the fetched positions is constant
1 parent 4497482 commit 1c682b9

File tree

4 files changed

+76
-85
lines changed

4 files changed

+76
-85
lines changed

examples_tests/22.RaytracedAO/closestHit.comp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ void main()
6868
const uvec3 indices = get_triangle_indices(batchInstanceGUID,triangleID);
6969

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

examples_tests/22.RaytracedAO/raygen.comp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ void main()
5454

5555
const uint vertex_depth_mod_2 = 0x1u;
5656
// load vertex data
57-
const vec3 last_vx_pos = load_positions(indices,batchInstanceData);
57+
mat2x3 dPdBary_error; vec3 lastVxPos_error;
58+
const vec3 last_vx_pos = load_positions(dPdBary_error,lastVxPos_error,indices,batchInstanceData);
5859
const nbl_glsl_xoroshiro64star_state_t scramble_start_state = load_aux_vertex_attrs(compactBary,indices,batchInstanceData,material,outPixelLocation,vertex_depth_mod_2
5960
#ifdef TEX_PREFETCH_STREAM
6061
,dBarydScreen

examples_tests/22.RaytracedAO/raytraceCommon.glsl

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -118,47 +118,34 @@ vec3 nbl_glsl_MC_getNormalizedWorldSpaceN()
118118
return normalizedN;
119119
}
120120

121-
122-
123-
124-
vec3 nbl_glsl_robust_ray_origin_fastest(in vec3 origin, in vec3 direction, float error, vec3 normal)
125-
{
126-
// flip it in the correct direction
127-
error = uintBitsToFloat(floatBitsToUint(error)^(floatBitsToUint(dot(normal,direction))&0x80000000u));
128-
return origin+normal*error;
129-
}
130-
vec3 nbl_glsl_robust_ray_origin_fast(in vec3 origin, in vec3 direction, in vec3 error, in vec3 normal)
131-
{
132-
// anticipate that the error could have increased from manipulation
133-
return nbl_glsl_robust_ray_origin_fastest(origin,direction,error.x+error.y+error.z,normal);
134-
}
135-
vec3 nbl_glsl_robust_ray_origin(in vec3 origin, in vec3 direction, in vec3 error, in vec3 normal)
136-
{
137-
const float d = dot(abs(normal),error);
138-
// anticipate that the error could have increased from manipulation
139-
return nbl_glsl_robust_ray_origin_fastest(origin,direction,d,normal);
140-
}
141-
142-
143-
144121
#include <nbl/builtin/glsl/barycentric/utils.glsl>
145122
mat2x3 dPdBary;
146-
vec3 load_positions(in uvec3 indices, in nbl_glsl_ext_Mitsuba_Loader_instance_data_t batchInstanceData)
123+
vec3 load_positions(out mat2x3 dPdBary_error, out vec3 lastVxPos_error,in uvec3 indices, in nbl_glsl_ext_Mitsuba_Loader_instance_data_t batchInstanceData)
147124
{
148125
mat3 positions = mat3(
149126
nbl_glsl_fetchVtxPos(indices[0],batchInstanceData),
150127
nbl_glsl_fetchVtxPos(indices[1],batchInstanceData),
151128
nbl_glsl_fetchVtxPos(indices[2],batchInstanceData)
152129
);
153130
const mat4x3 tform = batchInstanceData.tform;
154-
mat3 error;
131+
mat3 positions_error;
155132
// for when we quantize positions to RGB21_UNORM
156-
//const float quantizationError = nbl_glsl_numeric_limits_float_epsilon(1u);
157-
positions = nbl_glsl_mul_with_bounds(error,mat3(tform),positions/*,positions*quantizationError*/);
133+
const float quantizationError = exp2(-16.f);// nbl_glsl_numeric_limits_float_epsilon(1u);
134+
positions = nbl_glsl_mul_with_bounds_wo_gamma(positions_error,mat3(tform),positions,quantizationError);
158135
//
159-
dPdBary = mat2x3(positions[0]-positions[2],positions[1]-positions[2]);
160-
return positions[2]+tform[3];
136+
const float accum_error_factor = nbl_glsl_ieee754_gamma(2u)+nbl_glsl_ieee754_gamma(3u);
137+
for (int i=0; i<2; i++)
138+
{
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;
142+
}
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);
146+
return lastVx;
161147
}
148+
162149
#ifdef TEX_PREFETCH_STREAM
163150
mat2x3 nbl_glsl_perturbNormal_dPdSomething()
164151
{
@@ -173,31 +160,6 @@ mat2 nbl_glsl_perturbNormal_dUVdSomething()
173160
#define _NBL_USER_PROVIDED_MATERIAL_COMPILER_GLSL_BACKEND_FUNCTIONS_
174161
#include <nbl/builtin/glsl/material_compiler/common.glsl>
175162

176-
177-
vec3 rand3d(inout nbl_glsl_xoroshiro64star_state_t scramble_state, in int _sample, in int depth)
178-
{
179-
uvec3 seqVal = texelFetch(sampleSequence,int(_sample)+(depth-1)*MAX_ACCUMULATED_SAMPLES).xyz;
180-
seqVal ^= uvec3(nbl_glsl_xoroshiro64star(scramble_state),nbl_glsl_xoroshiro64star(scramble_state),nbl_glsl_xoroshiro64star(scramble_state));
181-
return vec3(seqVal)*uintBitsToFloat(0x2f800004u);
182-
}
183-
184-
void gen_sample_ray(
185-
out float maxT, out vec3 direction, out vec3 throughput,
186-
inout nbl_glsl_xoroshiro64star_state_t scramble_state, in uint sampleID, in uint depth,
187-
in nbl_glsl_MC_precomputed_t precomp, in nbl_glsl_MC_instr_stream_t gcs, in nbl_glsl_MC_instr_stream_t rnps
188-
)
189-
{
190-
maxT = nbl_glsl_FLT_MAX;
191-
192-
vec3 rand = rand3d(scramble_state,int(sampleID),int(depth));
193-
194-
float pdf;
195-
nbl_glsl_LightSample s;
196-
throughput = nbl_glsl_MC_runGenerateAndRemainderStream(precomp,gcs,rnps,rand,pdf,s);
197-
198-
direction = s.L;
199-
}
200-
201163
nbl_glsl_xoroshiro64star_state_t load_aux_vertex_attrs(
202164
in vec2 compactBary, in uvec3 indices, in nbl_glsl_ext_Mitsuba_Loader_instance_data_t batchInstanceData,
203165
in nbl_glsl_MC_oriented_material_t material,
@@ -244,6 +206,56 @@ nbl_glsl_xoroshiro64star_state_t load_aux_vertex_attrs(
244206
return scramble_start_state;
245207
}
246208

209+
// 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)
211+
{
212+
return triangleEdges*compactBary+origin;
213+
}
214+
// robust ray origins
215+
vec3 nbl_glsl_robust_ray_origin_fastest(in vec3 origin, in vec3 direction, float error, vec3 normal)
216+
{
217+
// 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);
231+
}
232+
233+
234+
vec3 rand3d(inout nbl_glsl_xoroshiro64star_state_t scramble_state, in int _sample, in int depth)
235+
{
236+
uvec3 seqVal = texelFetch(sampleSequence,int(_sample)+(depth-1)*MAX_ACCUMULATED_SAMPLES).xyz;
237+
seqVal ^= uvec3(nbl_glsl_xoroshiro64star(scramble_state),nbl_glsl_xoroshiro64star(scramble_state),nbl_glsl_xoroshiro64star(scramble_state));
238+
return vec3(seqVal)*uintBitsToFloat(0x2f800004u);
239+
}
240+
241+
void gen_sample_ray(
242+
out float maxT, out vec3 direction, out vec3 throughput,
243+
inout nbl_glsl_xoroshiro64star_state_t scramble_state, in uint sampleID, in uint depth,
244+
in nbl_glsl_MC_precomputed_t precomp, in nbl_glsl_MC_instr_stream_t gcs, in nbl_glsl_MC_instr_stream_t rnps
245+
)
246+
{
247+
maxT = nbl_glsl_FLT_MAX;
248+
249+
vec3 rand = rand3d(scramble_state,int(sampleID),int(depth));
250+
251+
float pdf;
252+
nbl_glsl_LightSample s;
253+
throughput = nbl_glsl_MC_runGenerateAndRemainderStream(precomp,gcs,rnps,rand,pdf,s);
254+
255+
direction = s.L;
256+
}
257+
258+
247259
void generate_next_rays(
248260
in uint maxRaysToGen, in nbl_glsl_MC_oriented_material_t material, in bool frontfacing, in uint vertex_depth,
249261
in nbl_glsl_xoroshiro64star_state_t scramble_start_state, in uint sampleID, in uvec2 outPixelLocation,

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

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,32 @@
55
#ifndef _NBL_BUILTIN_GLSL_UTILS_TRANSFORM_INCLUDED_
66
#define _NBL_BUILTIN_GLSL_UTILS_TRANSFORM_INCLUDED_
77

8-
mat3 nbl_glsl_mul_with_bounds_wo_gamma(out mat3 error, in mat3 a, in mat3 b, in mat3 b_error)
8+
mat3 nbl_glsl_mul_with_bounds_wo_gamma(out mat3 error, in mat3 a, in mat3 b, in float b_relative_error)
99
{
1010
mat3 retval;
1111
for (int i=0; i<3; i++)
1212
{
1313
vec3 tmp = a[0]*b[i][0];
1414
retval[i] = tmp;
1515
error[i] = abs(tmp);
16-
vec3 additional_error = abs(a[0]*b_error[i][0]);
1716
for (int j=1; j<3; j++)
1817
{
1918
tmp = a[j]*b[i][j];
2019
retval[i] += tmp;
2120
error[i] += abs(tmp);
22-
additional_error += abs(a[j]*b_error[i][j]);
2321
}
24-
additional_error *= nbl_glsl_ieee754_rcpgamma(2u)+1.f;
25-
error[i] += additional_error;
2622
}
23+
const float error_factor = 1.f+b_relative_error/nbl_glsl_numeric_limits_float_epsilon(2);
24+
error *= error_factor;
2725
return retval;
2826
}
29-
mat3 nbl_glsl_mul_with_bounds_wo_gamma(out mat3 error, in mat3 a, in mat3 b)
27+
mat3 nbl_glsl_mul_with_bounds(out mat3 error, in mat3 a, in mat3 b, in float b_relative_error)
3028
{
31-
mat3 retval;
32-
for (int i=0; i<3; i++)
33-
{
34-
vec3 tmp = a[0]*b[i][0];
35-
retval[i] = tmp;
36-
error[i] = abs(tmp);
37-
for (int j=1; j<3; j++)
38-
{
39-
tmp = a[j]*b[i][j];
40-
retval[i] += tmp;
41-
error[i] += abs(tmp);
42-
}
43-
}
29+
mat3 retval = nbl_glsl_mul_with_bounds_wo_gamma(error,a,b,b_relative_error);
30+
error *= nbl_glsl_ieee754_gamma(2u);
4431
return retval;
4532
}
4633

47-
mat3 nbl_glsl_mul_with_bounds(out mat3 error, in mat3 a, in mat3 b, in mat3 b_error)
48-
{
49-
return nbl_glsl_mul_with_bounds_wo_gamma(error,a,b,b_error);
50-
}
51-
52-
mat3 nbl_glsl_mul_with_bounds(out mat3 error, in mat3 a, in mat3 b)
53-
{
54-
return nbl_glsl_mul_with_bounds_wo_gamma(error,a,b);
55-
}
56-
5734

5835
vec4 nbl_glsl_pseudoMul4x4with3x1(in mat4 m, in vec3 v)
5936
{

0 commit comments

Comments
 (0)