Skip to content

Commit 6abb635

Browse files
committed
fixed nan and accumulation going black problem
1 parent 077d150 commit 6abb635

File tree

6 files changed

+61
-45
lines changed

6 files changed

+61
-45
lines changed

31_HLSLPathTracer/app_resources/hlsl/common.hlsl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace hlsl
1818
namespace ext
1919
{
2020

21-
template<typename T>
21+
template<typename T> // TODO make type T Spectrum
2222
struct Payload
2323
{
2424
using this_t = Payload<T>;
@@ -85,6 +85,14 @@ struct Light
8585

8686
NBL_CONSTEXPR_STATIC_INLINE uint32_t INVALID_ID = 0xffffu;
8787

88+
static Light<spectral_type> create(NBL_CONST_REF_ARG(spectral_type) radiance, uint32_t objId, uint32_t mode, ProceduralShapeType shapeType)
89+
{
90+
Light<spectral_type> retval;
91+
retval.radiance = radiance;
92+
retval.objectID = ObjectID::create(objId, mode, shapeType);
93+
return retval;
94+
}
95+
8896
static Light<spectral_type> create(NBL_CONST_REF_ARG(spectral_type) radiance, NBL_CONST_REF_ARG(ObjectID) objectID)
8997
{
9098
Light<spectral_type> retval;

31_HLSLPathTracer/app_resources/hlsl/next_event_estimator.hlsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ struct Estimator
8989

9090
static spectral_type deferredEvalAndPdf(NBL_REF_ARG(scalar_type) pdf, NBL_CONST_REF_ARG(light_type) light, NBL_CONST_REF_ARG(ray_type) ray, NBL_CONST_REF_ARG(Event) event)
9191
{
92-
const uint32_t mode = event.mode;
92+
const IntersectMode mode = (IntersectMode)event.mode;
9393
switch (mode)
9494
{
9595
case IM_RAY_QUERY:
@@ -192,7 +192,7 @@ struct Estimator
192192
}
193193

194194
newRayMaxT *= Tolerance<scalar_type>::getEnd(depth);
195-
pdf *= 1.0 / lightCount;
195+
pdf *= 1.0 / scalar_type(lightCount);
196196
spectral_type quo = light.radiance / pdf;
197197
quotient_pdf = quotient_pdf_type::create(quo, pdf);
198198

@@ -201,7 +201,7 @@ struct Estimator
201201

202202
static sample_type generate_and_quotient_and_pdf(NBL_REF_ARG(quotient_pdf_type) quotient_pdf, NBL_REF_ARG(scalar_type) newRayMaxT, NBL_CONST_REF_ARG(light_type) light, NBL_CONST_REF_ARG(vector3_type) origin, NBL_CONST_REF_ARG(interaction_type) interaction, bool isBSDF, NBL_CONST_REF_ARG(vector3_type) xi, uint32_t depth, NBL_CONST_REF_ARG(Event) event)
203203
{
204-
const uint32_t mode = event.mode;
204+
const IntersectMode mode = (IntersectMode)event.mode;
205205
sample_type L;
206206
switch (mode)
207207
{

31_HLSLPathTracer/app_resources/hlsl/pathtracer.hlsl

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,12 @@ struct Unidirectional
7979
// NextEventEstimator nee)
8080
// {}
8181

82-
static this_t create(NBL_CONST_REF_ARG(PathTracerCreationParams<create_params_type, scalar_type>) params, Buffer<uint3> sampleSequence)
82+
static this_t create(NBL_CONST_REF_ARG(PathTracerCreationParams<create_params_type, scalar_type>) params)
8383
{
8484
this_t retval;
8585
retval.randGen = randgen_type::create(params.rngState);
8686
retval.rayGen = raygen_type::create(params.pixOffsetParam, params.camPos, params.NDC, params.invMVP);
8787
retval.materialSystem = material_system_type::create(params.diffuseParams, params.conductorParams, params.dielectricParams);
88-
retval.sampleSequence = sampleSequence;
8988
return retval;
9089
}
9190

@@ -170,14 +169,14 @@ struct Unidirectional
170169
scalar_type rcpChoiceProb;
171170
if (!math::partitionRandVariable(neeProbability, eps0.z, rcpChoiceProb) && depth < 2u)
172171
{
172+
uint32_t randLightID = uint32_t(float32_t(randGen().x) / numeric_limits<uint32_t>::max) * scene.lightCount;
173173
quotient_pdf_type neeContrib_pdf;
174174
scalar_type t;
175175
sample_type nee_sample = nee.generate_and_quotient_and_pdf(
176176
neeContrib_pdf, t,
177-
scene.lights[lightID], intersection, interaction,
178-
isBSDF, eps0, depth, scene.toNextEvent(lightID)
177+
scene.lights[randLightID], intersection, interaction,
178+
isBSDF, eps0, depth, scene.toNextEvent(randLightID)
179179
);
180-
//printf("%f %f %f\n", nee_sample.L.direction.x, nee_sample.L.direction.y, nee_sample.L.direction.z);
181180

182181
// We don't allow non watertight transmitters in this renderer
183182
bool validPath = nee_sample.NdotL > numeric_limits<scalar_type>::min;
@@ -233,8 +232,7 @@ struct Unidirectional
233232
// }
234233

235234
quotient_pdf_type bsdf_quotient_pdf = materialSystem.quotient_and_pdf(material, bxdf.params, params);
236-
bsdf_quotient_pdf.quotient *= bxdf.albedo * throughput;
237-
neeContrib_pdf.quotient *= bsdf_quotient_pdf.quotient;
235+
neeContrib_pdf.quotient *= bxdf.albedo * throughput * bsdf_quotient_pdf.quotient;
238236
const scalar_type otherGenOverChoice = bsdf_quotient_pdf.pdf * rcpChoiceProb;
239237
const scalar_type otherGenOverLightAndChoice = otherGenOverChoice / bsdf_quotient_pdf.pdf;
240238
neeContrib_pdf.quotient *= otherGenOverChoice / (1.f + otherGenOverLightAndChoice * otherGenOverLightAndChoice); // balance heuristic
@@ -252,7 +250,7 @@ struct Unidirectional
252250
}
253251
}
254252

255-
//return false; // NEE only
253+
// return false; // NEE only
256254

257255
// sample BSDF
258256
scalar_type bxdfPdf;
@@ -312,8 +310,8 @@ struct Unidirectional
312310
if (bxdfPdf > bxdfPdfThreshold && getLuma(throughput) > lumaThroughputThreshold)
313311
{
314312
ray.payload.throughput = throughput;
315-
ray.payload.otherTechniqueHeuristic = neeProbability / bxdfPdf; // numerically stable, don't touch
316-
ray.payload.otherTechniqueHeuristic *= ray.payload.otherTechniqueHeuristic;
313+
scalar_type otherTechniqueHeuristic = neeProbability / bxdfPdf; // numerically stable, don't touch
314+
ray.payload.otherTechniqueHeuristic = otherTechniqueHeuristic * otherTechniqueHeuristic;
317315

318316
// trace new ray
319317
ray.origin = intersection + bxdfSample * (1.0/*kSceneSize*/) * Tolerance<scalar_type>::getStart(depth);
@@ -354,7 +352,7 @@ struct Unidirectional
354352
// bounces
355353
bool hit = true;
356354
bool rayAlive = true;
357-
for (int d = 1; d <= depth && hit && rayAlive; d += 2)
355+
for (int d = 1; (d <= depth) && hit && rayAlive; d += 2)
358356
{
359357
ray.intersectionT = numeric_limits<scalar_type>::max;
360358
ray.objectID = intersector_type::traceRay(ray, scene);
@@ -385,8 +383,6 @@ struct Unidirectional
385383
raygen_type rayGen;
386384
material_system_type materialSystem;
387385
nee_type nee;
388-
389-
Buffer<uint3> sampleSequence;
390386
};
391387

392388
}

31_HLSLPathTracer/app_resources/hlsl/render.comp.hlsl

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "nbl/builtin/hlsl/bxdf/reflection.hlsl"
77
#include "nbl/builtin/hlsl/bxdf/transmission.hlsl"
88

9+
#include "render_common.hlsl"
910
#include "pathtracer.hlsl"
1011

1112
// add these defines (one at a time) using -D argument to dxc
@@ -26,25 +27,6 @@ NBL_CONSTEXPR uint32_t WorkgroupSize = 32;
2627
NBL_CONSTEXPR uint32_t MAX_DEPTH_LOG2 = 4;
2728
NBL_CONSTEXPR uint32_t MAX_SAMPLES_LOG2 = 10;
2829

29-
struct SPushConstants
30-
{
31-
float32_t4x4 invMVP;
32-
int sampleCount;
33-
int depth;
34-
};
35-
36-
[[vk::push_constant]] SPushConstants pc;
37-
38-
[[vk::combinedImageSampler]][[vk::binding(0, 2)]] Texture2D<float3> envMap; // unused
39-
[[vk::combinedImageSampler]][[vk::binding(0, 2)]] SamplerState envSampler;
40-
41-
[[vk::binding(1, 2)]] Buffer<uint3> sampleSequence;
42-
43-
[[vk::combinedImageSampler]][[vk::binding(2, 2)]] Texture2D<uint2> scramblebuf; // unused
44-
[[vk::combinedImageSampler]][[vk::binding(2, 2)]] SamplerState scrambleSampler;
45-
46-
[[vk::image_format("rgba16f")]][[vk::binding(0, 0)]] RWTexture2D<float32_t4> outImage;
47-
4830
int32_t2 getCoordinates()
4931
{
5032
return int32_t2(glsl::gl_GlobalInvocationID().xy);
@@ -115,7 +97,7 @@ static const ext::Shape<ext::PST_RECTANGLE> rectangles[RECTANGLE_COUNT] = {
11597

11698
#define LIGHT_COUNT 1
11799
static const light_type lights[LIGHT_COUNT] = {
118-
light_type::create(spectral_t(30.0,25.0,15.0), ext::ObjectID::create(8u, ext::IntersectMode::IM_PROCEDURAL, LIGHT_TYPE))
100+
light_type::create(spectral_t(30.0,25.0,15.0), 8u, ext::IntersectMode::IM_PROCEDURAL, LIGHT_TYPE)
119101
};
120102

121103
#define BXDF_COUNT 7
@@ -154,7 +136,7 @@ void main(uint32_t3 threadID : SV_DispatchThreadID)
154136

155137
// set up path tracer
156138
ext::PathTracer::PathTracerCreationParams<create_params_t, float> ptCreateParams;
157-
ptCreateParams.rngState = pcg();
139+
ptCreateParams.rngState = scramblebuf[coords].rg;
158140

159141
uint2 scrambleDim;
160142
scramblebuf.GetDimensions(scrambleDim.x, scrambleDim.y);
@@ -174,7 +156,7 @@ void main(uint32_t3 threadID : SV_DispatchThreadID)
174156
ptCreateParams.conductorParams = bxdfs[3].params;
175157
ptCreateParams.dielectricParams = bxdfs[6].params;
176158

177-
pathtracer_type pathtracer = pathtracer_type::create(ptCreateParams, sampleSequence);
159+
pathtracer_type pathtracer = pathtracer_type::create(ptCreateParams);
178160

179161
// set up scene (can do as global var?)
180162
ext::Scene<light_type, bxdfnode_type> scene;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef _NBL_HLSL_PATHTRACER_RENDER_COMMON_INCLUDED_
2+
#define _NBL_HLSL_PATHTRACER_RENDER_COMMON_INCLUDED_
3+
4+
struct SPushConstants
5+
{
6+
float32_t4x4 invMVP;
7+
int sampleCount;
8+
int depth;
9+
};
10+
11+
[[vk::push_constant]] SPushConstants pc;
12+
13+
[[vk::combinedImageSampler]][[vk::binding(0, 2)]] Texture2D<float3> envMap; // unused
14+
[[vk::combinedImageSampler]][[vk::binding(0, 2)]] SamplerState envSampler;
15+
16+
[[vk::binding(1, 2)]] Buffer<uint3> sampleSequence;
17+
18+
[[vk::combinedImageSampler]][[vk::binding(2, 2)]] Texture2D<uint2> scramblebuf; // unused
19+
[[vk::combinedImageSampler]][[vk::binding(2, 2)]] SamplerState scrambleSampler;
20+
21+
[[vk::image_format("rgba16f")]][[vk::binding(0, 0)]] RWTexture2D<float32_t4> outImage;
22+
23+
#endif

31_HLSLPathTracer/main.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ class HLSLComputePathtracer final : public examples::SimpleWindowedApplication,
7474

7575
inline bool isComputeOnly() const override { return false; }
7676

77+
//inline video::IAPIConnection::SFeatures getAPIFeaturesToEnable() override
78+
//{
79+
// auto retval = device_base_t::getAPIFeaturesToEnable();
80+
// retval.synchronizationValidation = true;
81+
// return retval;
82+
//}
83+
7784
inline core::vector<video::SPhysicalDeviceFilter::SurfaceCompatibility> getSurfaces() const override
7885
{
7986
if (!m_surface)
@@ -359,11 +366,11 @@ class HLSLComputePathtracer final : public examples::SimpleWindowedApplication,
359366
options.stage = IShader::E_SHADER_STAGE::ESS_COMPUTE; // should be compute
360367
options.targetSpirvVersion = m_device->getPhysicalDevice()->getLimits().spirvVersion;
361368
options.spirvOptimizer = nullptr;
362-
#ifndef _NBL_DEBUG
363-
ISPIRVOptimizer::E_OPTIMIZER_PASS optPasses = ISPIRVOptimizer::EOP_STRIP_DEBUG_INFO;
364-
auto opt = make_smart_refctd_ptr<ISPIRVOptimizer>(std::span<ISPIRVOptimizer::E_OPTIMIZER_PASS>(&optPasses, 1));
365-
options.spirvOptimizer = opt.get();
366-
#endif
369+
//#ifndef _NBL_DEBUG
370+
// ISPIRVOptimizer::E_OPTIMIZER_PASS optPasses = ISPIRVOptimizer::EOP_STRIP_DEBUG_INFO;
371+
// auto opt = make_smart_refctd_ptr<ISPIRVOptimizer>(std::span<ISPIRVOptimizer::E_OPTIMIZER_PASS>(&optPasses, 1));
372+
// options.spirvOptimizer = opt.get();
373+
//#endif
367374
options.debugInfoFlags |= IShaderCompiler::E_DEBUG_INFO_FLAGS::EDIF_SOURCE_BIT;
368375
options.preprocessorOptions.sourceIdentifier = source->getFilepathHint();
369376
options.preprocessorOptions.logger = m_logger.get();
@@ -1343,7 +1350,7 @@ class HLSLComputePathtracer final : public examples::SimpleWindowedApplication,
13431350
int PTPipline = E_LIGHT_GEOMETRY::ELG_SPHERE;
13441351
int renderMode = E_RENDER_MODE::ERM_HLSL;
13451352
int spp = 32;
1346-
int depth = 3;
1353+
int depth = 1;
13471354

13481355
bool m_firstFrame = true;
13491356
IGPUCommandBuffer::SClearColorValue clearColor = { .float32 = {0.f,0.f,0.f,1.f} };

0 commit comments

Comments
 (0)