Skip to content

Commit 93fc8bd

Browse files
Tiny optimizations and preparation for Schussler et. al 2017
1 parent 73f7b96 commit 93fc8bd

File tree

4 files changed

+47
-37
lines changed

4 files changed

+47
-37
lines changed

examples_tests/22.RaytracedAO/closestHit.comp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,17 @@ void main()
6969
const uvec3 indices = get_triangle_indices(batchInstanceData,triangleID);
7070

7171
// positions
72-
vec3 geomDenormal;
73-
const vec3 lastVxPos = load_positions(geomDenormal,batchInstanceData,indices);
74-
const bool frontfacing = bool((batchInstanceData.determinantSignBit^floatBitsToUint(dot(geomDenormal,normalizedV)))&0x80000000u);
72+
vec3 geomNormal;
73+
const vec3 lastVxPos = load_positions(geomNormal,batchInstanceData,indices);
74+
75+
const bool frontfacing = bool((batchInstanceData.determinantSignBit^floatBitsToUint(dot(normalizedV,geomNormal)))&0x80000000u);
7576

7677
// get material
7778
const nbl_glsl_MC_oriented_material_t material = nbl_glsl_MC_material_data_t_getOriented(batchInstanceData.material,frontfacing);
7879
contrib.color = contrib.albedo = nbl_glsl_MC_oriented_material_t_getEmissive(material);
7980

8081
const uint pathDepth = bitfieldExtract(staticViewData.pathDepth_noRussianRouletteDepth_samplesPerPixelPerDispatch,0,8);
81-
const bool _continue = vertex_depth!=pathDepth && ray.maxT==nbl_glsl_FLT_MAX; // not last vertex and not NEE path
82+
const bool _continue = vertex_depth!=pathDepth && material.genchoice_count!=0u && ray.maxT==nbl_glsl_FLT_MAX; // not last vertex and has a BxDF and not NEE path
8283
if (_continue)
8384
{
8485
// if we ever support spatially varying emissive, we'll need to hoist barycentric computation and UV fetching to the position fetching
@@ -89,7 +90,7 @@ void main()
8990

9091
//
9192
normalizedN = load_normal_and_prefetch_textures(
92-
batchInstanceData,indices,compactBary,geomDenormal,material
93+
batchInstanceData,indices,compactBary,geomNormal,material
9394
#ifdef TEX_PREFETCH_STREAM
9495
,mat2(0.0) // TODO: Covariance Rendering
9596
#endif
@@ -103,7 +104,7 @@ void main()
103104
);
104105
}
105106
else
106-
contrib.worldspaceNormal = normalize(geomDenormal)*nbl_glsl_MC_colorToScalar(contrib.albedo);
107+
contrib.worldspaceNormal = geomNormal*nbl_glsl_MC_colorToScalar(contrib.albedo);
107108
}
108109
else
109110
Contribution_initMiss(contrib,aovThroughputScale);

examples_tests/22.RaytracedAO/raygen.comp

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,38 @@ void main()
7979
// get material while waiting for indices
8080
const nbl_glsl_MC_oriented_material_t material = nbl_glsl_MC_material_data_t_getOriented(batchInstanceData.material,frontfacing);
8181
contrib.color = contrib.albedo = nbl_glsl_MC_oriented_material_t_getEmissive(material);
82-
82+
8383
// load vertex data
84-
vec3 geomDenormal;
85-
const vec3 lastVxPos = load_positions(geomDenormal,batchInstanceData,indices);
86-
87-
// get initial scramble key while waiting for vertex positions
88-
const nbl_glsl_xoroshiro64star_state_t scramble_start_state = texelFetch(scramblebuf,ivec2(outPixelLocation),0).rg;
84+
vec3 geomNormal;
85+
const vec3 lastVxPos = load_positions(geomNormal,batchInstanceData,indices);
8986

90-
//
91-
normalizedN = load_normal_and_prefetch_textures(
92-
batchInstanceData,indices,compactBary,geomDenormal,material
93-
#ifdef TEX_PREFETCH_STREAM
94-
,dBarydScreen
95-
#endif
96-
);
87+
// little optimization for non-twosided materials
88+
if (material.genchoice_count!=0u)
89+
{
90+
// get initial scramble key while waiting for vertex positions
91+
const nbl_glsl_xoroshiro64star_state_t scramble_start_state = texelFetch(scramblebuf,ivec2(outPixelLocation),0).rg;
92+
93+
//
94+
normalizedN = load_normal_and_prefetch_textures(
95+
batchInstanceData,indices,compactBary,geomNormal,material
96+
#ifdef TEX_PREFETCH_STREAM
97+
,dBarydScreen
98+
#endif
99+
);
97100

98-
const vec3 origin = dPdBary*compactBary+lastVxPos;
99-
normalizedV = normalize(pc.cummon.camPos-origin);
101+
const vec3 origin = dPdBary*compactBary+lastVxPos;
102+
normalizedV = normalize(pc.cummon.camPos-origin);
100103

101-
// generate rays
102-
const uint vertex_depth = 1u;
103-
generate_next_rays(
104-
samplesPerPixelPerDispatch,material,frontfacing,vertex_depth,
105-
scramble_start_state,pc.cummon.samplesComputed,outPixelLocation,origin,
106-
vec3(pc.cummon.rcpFramesDispatched),1.f,contrib.albedo,contrib.worldspaceNormal
107-
);
104+
// generate rays
105+
const uint vertex_depth = 1u;
106+
generate_next_rays(
107+
samplesPerPixelPerDispatch,material,frontfacing,vertex_depth,
108+
scramble_start_state,pc.cummon.samplesComputed,outPixelLocation,origin,
109+
vec3(pc.cummon.rcpFramesDispatched),1.f,contrib.albedo,contrib.worldspaceNormal
110+
);
111+
}
112+
else
113+
contrib.worldspaceNormal = geomNormal*nbl_glsl_MC_colorToScalar(contrib.albedo);
108114
}
109115
else
110116
Contribution_initMiss(contrib,1.f);

examples_tests/22.RaytracedAO/raytraceCommon.glsl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ bool has_world_transform(in nbl_glsl_ext_Mitsuba_Loader_instance_data_t batchIns
154154

155155
#include <nbl/builtin/glsl/barycentric/utils.glsl>
156156
mat2x3 dPdBary;
157-
vec3 load_positions(out vec3 geomDenormal, in nbl_glsl_ext_Mitsuba_Loader_instance_data_t batchInstanceData, in uvec3 indices)
157+
vec3 load_positions(out vec3 geomNormal, in nbl_glsl_ext_Mitsuba_Loader_instance_data_t batchInstanceData, in uvec3 indices)
158158
{
159159
mat3 positions = mat3(
160160
nbl_glsl_fetchVtxPos(indices[0],batchInstanceData),
@@ -167,7 +167,7 @@ vec3 load_positions(out vec3 geomDenormal, in nbl_glsl_ext_Mitsuba_Loader_instan
167167
//
168168
for (int i=0; i<2; i++)
169169
dPdBary[i] = positions[i]-positions[2];
170-
geomDenormal = cross(dPdBary[0],dPdBary[1]);
170+
geomNormal = normalize(cross(dPdBary[0],dPdBary[1]));
171171
//
172172
if (tform)
173173
positions[2] += batchInstanceData.tform[3];
@@ -196,7 +196,7 @@ bool needs_texture_prefetch(in nbl_glsl_ext_Mitsuba_Loader_instance_data_t batch
196196

197197
vec3 load_normal_and_prefetch_textures(
198198
in nbl_glsl_ext_Mitsuba_Loader_instance_data_t batchInstanceData,
199-
in uvec3 indices, in vec2 compactBary, in vec3 geomDenormal,
199+
in uvec3 indices, in vec2 compactBary, in vec3 geomNormal,
200200
in nbl_glsl_MC_oriented_material_t material
201201
#ifdef TEX_PREFETCH_STREAM
202202
,in mat2 dBarydScreen
@@ -224,7 +224,6 @@ vec3 load_normal_and_prefetch_textures(
224224
// the rest is always only needed for continuing rays
225225

226226

227-
vec3 normal = geomDenormal;
228227
// while waiting for the scramble state
229228
// TODO: optimize, add loads more flags to control this
230229
const bool needsSmoothNormals = true;
@@ -237,18 +236,20 @@ vec3 load_normal_and_prefetch_textures(
237236
);
238237

239238
// not needed for NEE unless doing Area or Projected Solid Angle Sampling
240-
const vec3 smoothNormal = normals*nbl_glsl_barycentric_expand(compactBary);
241-
// TODO: first check wouldn't be needed if we had `needsSmoothNormals` implemented
242-
if (!isnan(smoothNormal.x) && has_world_transform(batchInstanceData))
239+
vec3 smoothNormal = normals*nbl_glsl_barycentric_expand(compactBary);
240+
if (has_world_transform(batchInstanceData))
243241
{
244-
normal = vec3(
242+
smoothNormal = vec3(
245243
dot(batchInstanceData.normalMatrixRow0,smoothNormal),
246244
dot(batchInstanceData.normalMatrixRow1,smoothNormal),
247245
dot(batchInstanceData.normalMatrixRow2,smoothNormal)
248246
);
249247
}
248+
// TODO: this check wouldn't be needed if we had `needsSmoothNormals` implemented
249+
if (!isnan(smoothNormal.x))
250+
return normalize(smoothNormal);
250251
}
251-
return normalize(normal);
252+
return geomNormal;
252253
}
253254

254255
#include <nbl/builtin/glsl/sampling/quantized_sequence.glsl>

include/nbl/builtin/glsl/material_compiler/common_invariant_declarations.glsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct nbl_glsl_MC_instr_stream_t
3737
// (in case of precomp.NdotV<0.0, currInteraction will be set with -precomp.N)
3838
struct nbl_glsl_MC_precomputed_t
3939
{
40+
// TODO: shadingN and geomN
4041
vec3 N;
4142
vec3 V;
4243
bool frontface;
@@ -82,6 +83,7 @@ void nbl_glsl_MC_finalizeMicrofacet(inout nbl_glsl_MC_microfacet_t mf)
8283
struct nbl_glsl_MC_oriented_material_t
8384
{
8485
uvec2 emissive;
86+
// TODO: derive/define upper bounds for instruction counts and bitpack them!
8587
uint prefetch_offset;
8688
uint prefetch_count;
8789
uint instr_offset;

0 commit comments

Comments
 (0)