shocker-0x15 / GfxExp

Sandbox for graphics paper implementation
Other
222 stars 19 forks source link

Getting some NaN in path_tracing sample #13

Closed Hurleyworks closed 9 months ago

Hurleyworks commented 9 months ago

I'm getting some NaN in optix_pathtracing_kernels.cu line 201 RGB colorResult = (1 - curWeight) prevColorResult + curWeight contribution;

I add these lines after line 201 to color the NaN pixels super red

 RGB colorResult = (1 - curWeight) * prevColorResult + curWeight * contribution;
 if (isnan(colorResult.r) || isnan(colorResult.g) || isnan(colorResult.b))
 {
     printf("contribution: %f, %f, %f\n", contribution.r, contribution.g, contribution.b);
     colorResult = RGB(make_float3(1000000.0f, 0.0f, 0.0f)); // super red
 }

And here is the output after rendering for a few minutes. Do you know what the problem might be? It is still so hard to debug OptiX. It seems it gets harder and harder with each new release.

nan

shocker-0x15 commented 9 months ago

Typical NaN in a renderer comes from something in BRDF (e.g. acos(x), |x| > 1), invalid reference frame, etc). Could you check first if your input mesh is valid, particularly like tangent vectors?

Hurleyworks commented 9 months ago

Thanks for the quick reply. I simplified down to a single triangle and checked the tangents and they seem correct. I was using the SimplePBRMaterial and have tried LambertMaterial too and there are NaN's with both.

I am only using environment texture for lighting. Here is my command line -cam-pos -0.753442 0.140257 -0.056083 -cam-yaw 75 -name tester -obj E:/common_content/obj/triangle.obj 0.0551 trad -brightness 1.0 -env-texture C:/Users/Steve/Downloads/resting_place_4k.exr -inst tester -begin-pos 0.362 0.329 -2.0 -begin-pitch -90 -begin-yaw 150 -end-pos -0.719 0.329 -0.442 -end-pitch -90 -end-yaw 30

triangle_nan

shocker-0x15 commented 9 months ago

An environment texture can be another NaN source and my light sampling code possibly misses a proper corner case handling. I'll try to see on my side as well.

shocker-0x15 commented 9 months ago

The cause was IBL importance sampling. It rarely samples theta == 0 and this makes PDF value infinity and leads to NaN in the end. Could you try the following fix?

diff --git a/path_tracing/path_tracing_shared.h b/path_tracing/path_tracing_shared.h
index a01c582..4ab033c 100644
--- a/path_tracing/path_tracing_shared.h
+++ b/path_tracing/path_tracing_shared.h
@@ -231,6 +231,10 @@ CUDA_DEVICE_FUNCTION CUDA_INLINE void sampleLight(
         plp.s->envLightImportanceMap.sample(u0, u1, &u, &v, &uvPDF);
         float phi = 2 * Pi * u;
         float theta = Pi * v;
+        if (theta == 0.0f) {
+          *areaPDensity = 0.0f;
+          return;
+        }

         float posPhi = phi - plp.f->envLightRotation;
         posPhi = posPhi - floorf(posPhi / (2 * Pi)) * 2 * Pi;
Hurleyworks commented 9 months ago

Great. That fixes the NaN problem. Thank you very much for the quick help!

I was hoping this would fix a problem I've having with corruption during denoising but it does not unfortunately. https://forums.developer.nvidia.com/t/black-box-rendering-errors/274226/10

shocker-0x15 commented 9 months ago

It is possibly worth using ptx instead of OptiX-IR if you are using. OptiX-IR used to make isnan() not working a while ago. https://forums.developer.nvidia.com/t/optix-7-5-debuggable-optix-ir-makes-isnan-not-working/228251/5 I'm not sure if this issue has been fixed.

Hurleyworks commented 9 months ago

Thanks for the suggestion. I tried it but it did not help.

On Wed, Nov 29, 2023 at 7:45 AM shocker-0x15 @.***> wrote:

It is possibly worth using ptx instead of OptiX-IR if you are using. OptiX-IR used to make isnan() not working a while ago.

https://forums.developer.nvidia.com/t/optix-7-5-debuggable-optix-ir-makes-isnan-not-working/228251/5 I'm not sure if this issue has been fixed.

— Reply to this email directly, view it on GitHub https://github.com/shocker-0x15/GfxExp/issues/13#issuecomment-1831829734, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB56PP4PLHOWCCPYDGUNJ4DYG4U6PAVCNFSM6AAAAAA76WDP3SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMZRHAZDSNZTGQ . You are receiving this because you authored the thread.Message ID: @.***>

-- -Steve http://berserko.hurleyworks.com/ http://www.hurleyworks.com

shocker-0x15 commented 9 months ago

Unfortunate that ptx didn't help, but let me close this issue since the original question seems resolved.

Hurleyworks commented 9 months ago

Thanks again for the help.

If you are a ChatGPT4 user, I made a custom GPT for OptiX8 by feeding it the Programming and API Guides and pdf versions of your OptiX_Utility library. It's a lot more fun to chat with the manuals than to have to dig around them looking for answers. :) https://chat.openai.com/g/g-xuly7sb4A-optix-pathtracer-pro