Skip to content

Commit 077d150

Browse files
committed
1st working ver, sort of
1 parent e7d4670 commit 077d150

File tree

5 files changed

+63
-50
lines changed

5 files changed

+63
-50
lines changed

31_HLSLPathTracer/app_resources/glsl/common.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//#define VISUALIZE_HIGH_VARIANCE
88

99
// debug
10-
#define NEE_ONLY 1
10+
//#define NEE_ONLY
1111

1212
layout(set = 2, binding = 0) uniform sampler2D envMap;
1313
layout(set = 2, binding = 1) uniform usamplerBuffer sampleSequence;

31_HLSLPathTracer/app_resources/hlsl/common.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ struct Shape<PST_SPHERE>
268268
}
269269

270270
template<class Aniso>
271-
float32_t3 generate_and_pdf(NBL_REF_ARG(float32_t) pdf, NBL_REF_ARG(float32_t) newRayMaxT, NBL_CONST_REF_ARG(float32_t3) origin, NBL_CONST_REF_ARG(Aniso) interaction, bool isBSDF, float32_t3 xi)
271+
float32_t3 generate_and_pdf(NBL_REF_ARG(float32_t) pdf, NBL_REF_ARG(float32_t) newRayMaxT, NBL_CONST_REF_ARG(float32_t3) origin, NBL_CONST_REF_ARG(Aniso) interaction, bool isBSDF, NBL_CONST_REF_ARG(float32_t3) xi)
272272
{
273273
float32_t3 Z = position - origin;
274274
const float distanceSQ = hlsl::dot<float32_t3>(Z,Z);
@@ -279,7 +279,7 @@ struct Shape<PST_SPHERE>
279279
Z *= rcpDistance;
280280

281281
const float cosThetaMax = hlsl::sqrt<float32_t>(cosThetaMax2);
282-
const float cosTheta = nbl::hlsl::mix<float>(1.0, cosThetaMax, xi.x);
282+
const float cosTheta = hlsl::mix<float>(1.0, cosThetaMax, xi.x);
283283

284284
float32_t3 L = Z * cosTheta;
285285

31_HLSLPathTracer/app_resources/hlsl/material_system.hlsl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,8 @@ struct System
123123

124124
quotient_pdf_type quotient_and_pdf(NBL_CONST_REF_ARG(Material) material, NBL_CONST_REF_ARG(create_params_t) cparams, NBL_CONST_REF_ARG(params_t) params)
125125
{
126-
127-
const bool transmissive = material.type == Material::Type::DIELECTRIC;
128-
const float clampedNdotV = math::conditionalAbsOrMax<float>(transmissive, params.uNdotV, 0.0);
129-
const float clampedNdotL = math::conditionalAbsOrMax<float>(transmissive, params.uNdotL, 0.0);
130-
131126
const float minimumProjVectorLen = 0.00000001;
132-
if (clampedNdotV > minimumProjVectorLen && clampedNdotL > minimumProjVectorLen)
127+
if (params.NdotV > minimumProjVectorLen && params.NdotL > minimumProjVectorLen)
133128
{
134129
switch(material.type)
135130
{

31_HLSLPathTracer/app_resources/hlsl/pathtracer.hlsl

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct Unidirectional
9494
uint32_t address = glsl::bitfieldInsert<uint32_t>(protoDimension, _sample, MAX_DEPTH_LOG2, MAX_SAMPLES_LOG2);
9595
uint32_t3 seqVal = sampleSequence[address + i].xyz;
9696
seqVal ^= randGen();
97-
return vector3_type(seqVal) * asfloat(0x2f800004u);
97+
return vector3_type(seqVal) * bit_cast<scalar_type>(0x2f800004u);
9898
}
9999

100100
scalar_type getLuma(NBL_CONST_REF_ARG(vector3_type) col)
@@ -177,6 +177,7 @@ struct Unidirectional
177177
scene.lights[lightID], intersection, interaction,
178178
isBSDF, eps0, depth, scene.toNextEvent(lightID)
179179
);
180+
//printf("%f %f %f\n", nee_sample.L.direction.x, nee_sample.L.direction.y, nee_sample.L.direction.z);
180181

181182
// We don't allow non watertight transmitters in this renderer
182183
bool validPath = nee_sample.NdotL > numeric_limits<scalar_type>::min;
@@ -195,47 +196,51 @@ struct Unidirectional
195196
{
196197
ext::MaterialSystem::Material material;
197198
material.type = bxdf.materialType;
198-
params_type params;
199+
200+
bxdf::BxDFClampMode _clamp;
201+
_clamp = (bxdf.materialType == ext::MaterialSystem::Material::Type::DIELECTRIC) ? bxdf::BxDFClampMode::BCM_ABS : bxdf::BxDFClampMode::BCM_MAX;
202+
// example only uses isotropic bxdfs
203+
params_type params = params_type::template create<sample_type, isotropic_type, isocache_type>(nee_sample, interaction.isotropic, _cache.iso_cache, _clamp);
199204

200205
// TODO: does not yet account for smooth dielectric
201-
if (!isBSDF && bxdf.materialType == ext::MaterialSystem::Material::DIFFUSE)
202-
{
203-
params = params_type::template create<sample_type, isotropic_type>(nee_sample, iso_interaction, bxdf::BCM_MAX);
204-
}
205-
else if (!isBSDF && bxdf.materialType != ext::MaterialSystem::Material::DIFFUSE)
206-
{
207-
if (bxdf.params.is_aniso)
208-
params = params_type::template create<sample_type, anisotropic_type, anisocache_type>(nee_sample, interaction, _cache, bxdf::BCM_MAX);
209-
else
210-
{
211-
isocache_type isocache = _cache.iso_cache;
212-
params = params_type::template create<sample_type, isotropic_type, isocache_type>(nee_sample, iso_interaction, isocache, bxdf::BCM_MAX);
213-
}
214-
}
215-
else if (isBSDF && bxdf.materialType == ext::MaterialSystem::Material::DIFFUSE)
216-
{
217-
params = params_type::template create<sample_type, isotropic_type>(nee_sample, iso_interaction, bxdf::BCM_ABS);
218-
}
219-
else if (isBSDF && bxdf.materialType != ext::MaterialSystem::Material::DIFFUSE)
220-
{
221-
if (bxdf.params.is_aniso)
222-
params = params_type::template create<sample_type, anisotropic_type, anisocache_type>(nee_sample, interaction, _cache, bxdf::BCM_ABS);
223-
else
224-
{
225-
isocache_type isocache = _cache.iso_cache;
226-
params = params_type::template create<sample_type, isotropic_type, isocache_type>(nee_sample, iso_interaction, isocache, bxdf::BCM_ABS);
227-
}
228-
}
206+
// if (!isBSDF && bxdf.materialType == ext::MaterialSystem::Material::DIFFUSE)
207+
// {
208+
// params = params_type::template create<sample_type, isotropic_type>(nee_sample, interaction.isotropic, bxdf::BCM_MAX);
209+
// }
210+
// else if (!isBSDF && bxdf.materialType != ext::MaterialSystem::Material::DIFFUSE)
211+
// {
212+
// if (bxdf.params.is_aniso)
213+
// params = params_type::template create<sample_type, anisotropic_type, anisocache_type>(nee_sample, interaction, _cache, bxdf::BCM_MAX);
214+
// else
215+
// {
216+
// isocache_type isocache = _cache.iso_cache;
217+
// params = params_type::template create<sample_type, isotropic_type, isocache_type>(nee_sample, interaction.isotropic, _cache.iso_cache, bxdf::BCM_MAX);
218+
// }
219+
// }
220+
// else if (isBSDF && bxdf.materialType == ext::MaterialSystem::Material::DIFFUSE)
221+
// {
222+
// params = params_type::template create<sample_type, isotropic_type>(nee_sample, interaction.isotropic, bxdf::BCM_ABS);
223+
// }
224+
// else if (isBSDF && bxdf.materialType != ext::MaterialSystem::Material::DIFFUSE)
225+
// {
226+
// if (bxdf.params.is_aniso)
227+
// params = params_type::template create<sample_type, anisotropic_type, anisocache_type>(nee_sample, interaction, _cache, bxdf::BCM_ABS);
228+
// else
229+
// {
230+
// isocache_type isocache = _cache.iso_cache;
231+
// params = params_type::template create<sample_type, isotropic_type, isocache_type>(nee_sample, interaction.isotropic, _cache.iso_cache, bxdf::BCM_ABS);
232+
// }
233+
// }
229234

230235
quotient_pdf_type bsdf_quotient_pdf = materialSystem.quotient_and_pdf(material, bxdf.params, params);
231236
bsdf_quotient_pdf.quotient *= bxdf.albedo * throughput;
232237
neeContrib_pdf.quotient *= bsdf_quotient_pdf.quotient;
233238
const scalar_type otherGenOverChoice = bsdf_quotient_pdf.pdf * rcpChoiceProb;
234-
// const scalar_type otherGenOverLightAndChoice = otherGenOverChoice / bsdf_quotient_pdf.pdf;
235-
// neeContrib_pdf.quotient *= otherGenOverChoice / (1.f + otherGenOverLightAndChoice * otherGenOverLightAndChoice); // balance heuristic
239+
const scalar_type otherGenOverLightAndChoice = otherGenOverChoice / bsdf_quotient_pdf.pdf;
240+
neeContrib_pdf.quotient *= otherGenOverChoice / (1.f + otherGenOverLightAndChoice * otherGenOverLightAndChoice); // balance heuristic
236241

237242
// TODO: ifdef NEE only
238-
neeContrib_pdf.quotient *= otherGenOverChoice;
243+
// neeContrib_pdf.quotient *= otherGenOverChoice;
239244

240245
ray_type nee_ray;
241246
nee_ray.origin = intersection + nee_sample.L.direction * t * Tolerance<scalar_type>::getStart(depth);
@@ -247,7 +252,7 @@ struct Unidirectional
247252
}
248253
}
249254

250-
return false; // NEE only
255+
//return false; // NEE only
251256

252257
// sample BSDF
253258
scalar_type bxdfPdf;
@@ -259,12 +264,17 @@ struct Unidirectional
259264
anisocache_type _cache;
260265
sample_type bsdf_sample = materialSystem.generate(material, bxdf.params, interaction, eps1, _cache);
261266

267+
bxdf::BxDFClampMode _clamp;
268+
_clamp = (bxdf.materialType == ext::MaterialSystem::Material::Type::DIELECTRIC) ? bxdf::BxDFClampMode::BCM_ABS : bxdf::BxDFClampMode::BCM_MAX;
269+
// example only uses isotropic bxdfs
270+
params_type params = params_type::template create<sample_type, isotropic_type, isocache_type>(bsdf_sample, interaction.isotropic, _cache.iso_cache, _clamp);
271+
262272
// TODO: does not yet account for smooth dielectric
263-
params_type params;
264-
if (!isBSDF && bxdf.materialType == ext::MaterialSystem::Material::DIFFUSE)
265-
{
266-
params = params_type::template create<sample_type, isotropic_type>(bsdf_sample, iso_interaction, bxdf::BCM_MAX);
267-
}
273+
// params_type params;
274+
// if (!isBSDF && bxdf.materialType == ext::MaterialSystem::Material::DIFFUSE)
275+
// {
276+
// params = params_type::template create<sample_type, isotropic_type>(bsdf_sample, iso_interaction, bxdf::BCM_MAX);
277+
// }
268278
// else if (!isBSDF && bxdf.materialType != ext::MaterialSystem::Material::DIFFUSE)
269279
// {
270280
// if (bxdf.params.is_aniso)
@@ -292,7 +302,8 @@ struct Unidirectional
292302

293303
// the value of the bsdf divided by the probability of the sample being generated
294304
quotient_pdf_type bsdf_quotient_pdf = materialSystem.quotient_and_pdf(material, bxdf.params, params);
295-
throughput *= bsdf_quotient_pdf.quotient;
305+
throughput *= bxdf.albedo * bsdf_quotient_pdf.quotient;
306+
bxdfPdf = bsdf_quotient_pdf.pdf;
296307
bxdfSample = bsdf_sample.L.direction;
297308
}
298309

@@ -351,7 +362,6 @@ struct Unidirectional
351362
hit = ray.objectID.id != -1;
352363
if (hit)
353364
rayAlive = closestHitProgram(1, i, ray, scene);
354-
355365
}
356366
if (!hit)
357367
missProgram(ray);

31_HLSLPathTracer/imgui.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[Window][Debug##Default]
2+
Pos=60,60
3+
Size=400,400
4+
5+
[Window][Controls]
6+
Pos=10,10
7+
Size=320,340
8+

0 commit comments

Comments
 (0)