diff --git a/zenovis/xinxinoptix/DeflMatShader.cu b/zenovis/xinxinoptix/DeflMatShader.cu index 46e942b145..50b4b38df7 100644 --- a/zenovis/xinxinoptix/DeflMatShader.cu +++ b/zenovis/xinxinoptix/DeflMatShader.cu @@ -72,6 +72,7 @@ static __inline__ __device__ MatOutput evalMat(cudaTextureObject_t zenotex[], fl float mat_NoL = 1.0f; float mat_LoV = 1.0f; vec3 mat_reflectance = att_reflectance; + auto vol_sample_anisotropy = 0.0f; //GENERATED_END_MARK /** generated code here end **/ MatOutput mats; @@ -108,6 +109,7 @@ static __inline__ __device__ MatOutput evalMat(cudaTextureObject_t zenotex[], fl mats.sssParam = mat_sssParam; mats.scatterStep = mat_scatterStep; mats.smoothness = mat_smoothness; + mats.vol_anisotropy = clamp(vol_sample_anisotropy, -0.99f, 0.99f); return mats; } } @@ -878,8 +880,6 @@ extern "C" __global__ void __closesthit__radiance() prd->diffDepth++; } - - prd->passed = false; bool inToOut = false; bool outToIn = false; @@ -896,13 +896,15 @@ extern "C" __global__ void __closesthit__radiance() if (prd->curMatIdx > 0) { vec3 sigma_t, ss_alpha; prd->readMat(sigma_t, ss_alpha); + + float3 trans; if (ss_alpha.x<0.0f) { // is inside Glass - prd->attenuation *= DisneyBSDF::Transmission(sigma_t, optixGetRayTmax()); - prd->attenuation2 *= DisneyBSDF::Transmission(sigma_t, optixGetRayTmax()); + trans = DisneyBSDF::Transmission(sigma_t, optixGetRayTmax()); } else { - prd->attenuation *= DisneyBSDF::Transmission2(sigma_t * ss_alpha, sigma_t, prd->channelPDF, optixGetRayTmax(), true); - prd->attenuation2 *= DisneyBSDF::Transmission2(sigma_t * ss_alpha, sigma_t, prd->channelPDF, optixGetRayTmax(), true); + trans = DisneyBSDF::Transmission2(sigma_t * ss_alpha, sigma_t, prd->channelPDF, optixGetRayTmax(), true); } + prd->attenuation *= trans; + prd->attenuation2 *= trans; }else { prd->attenuation *= 1; } @@ -912,8 +914,7 @@ extern "C" __global__ void __closesthit__radiance() //if(flag == DisneyBSDF::transmissionEvent || flag == DisneyBSDF::diracEvent) { if(istransmission || flag == DisneyBSDF::diracEvent) { if(prd->next_ray_is_going_inside){ - if(thin < 0.5f && mats.doubleSide < 0.5f ) - { + outToIn = true; inToOut = false; @@ -921,7 +922,6 @@ extern "C" __global__ void __closesthit__radiance() if (prd->curMatIdx > 0) { vec3 sigma_t, ss_alpha; - //vec3 sigma_t, ss_alpha; prd->readMat(sigma_t, ss_alpha); if (ss_alpha.x < 0.0f) { // is inside Glass prd->attenuation *= DisneyBSDF::Transmission(sigma_t, optixGetRayTmax()); @@ -932,8 +932,10 @@ extern "C" __global__ void __closesthit__radiance() prd->channelPDF = vec3(1.0f/3.0f); if (isTrans) { vec3 channelPDF = vec3(1.0f/3.0f); - prd->maxDistance = scatterStep>0.5f? DisneyBSDF::SampleDistance2(prd->seed, prd->sigma_t, prd->sigma_t, channelPDF) : 1e16f; - prd->pushMat(extinction); + // prd->sigma_t is only pre-calculated for SSS branch, so it's garbage value here, use extinction value instead + prd->maxDistance = scatterStep>0.5f? DisneyBSDF::SampleDistance2(prd->seed, extinction, extinction, channelPDF) : 1e16f; + prd->pushMat(extinction, vec3(-1.0f), mats.vol_anisotropy); + } else { vec3 channelPDF = vec3(1.0f/3.0f); @@ -944,21 +946,15 @@ extern "C" __global__ void __closesthit__radiance() //prd->maxDistance = max(prd->maxDistance, 10/min_sg); //printf("maxdist:%f\n",prd->maxDistance); prd->channelPDF = channelPDF; - // already calculated in BxDF - - // if (idx.x == w/2 && idx.y == h/2) { - // printf("into sss, sigma_t, alpha: %f, %f, %f\n", prd->sigma_t.x, prd->sigma_t.y, prd->sigma_t.z,prd->ss_alpha.x, prd->ss_alpha.y, prd->ss_alpha.z); - // } - - prd->pushMat(prd->sigma_t, prd->ss_alpha); + // prd->sigma_t and prd->ss_alpha should be pre-calculated in BxDF for this SSS branch + prd->pushMat(prd->sigma_t, prd->ss_alpha, mats.vol_anisotropy); } prd->scatterDistance = scatterDistance; prd->scatterStep = scatterStep; - } - - } - else{ + } + else { + outToIn = false; inToOut = true; @@ -991,17 +987,8 @@ extern "C" __global__ void __closesthit__radiance() else { prd->isSS = false; - prd->maxDistance = 1e16; + prd->maxDistance = scatterStep>0.5f? DisneyBSDF::SampleDistance2(prd->seed, sigma_t, sigma_t, prd->channelPDF) : 1e16f; } - - // if (prd->medium != DisneyBSDF::PhaseFunctions::vacuum) { - - // prd->bad = true; - - // printf("%f %f %f %f %f %f %f %f \n matIdx = %d isotropic = %d \n", prd->sigma_t_queue[0].x, prd->sigma_t_queue[1].x, prd->sigma_t_queue[2].x, prd->sigma_t_queue[3].x, prd->sigma_t_queue[4].x, prd->sigma_t_queue[5].x, prd->sigma_t_queue[6].x, prd->sigma_t_queue[7].x, - // prd->curMatIdx, prd->medium); - // printf("matIdx = %d isotropic = %d \n\n", prd->curMatIdx, prd->medium); - // } } }else{ if(prd->medium == DisneyBSDF::PhaseFunctions::isotropic){ diff --git a/zenovis/xinxinoptix/IOMat.h b/zenovis/xinxinoptix/IOMat.h index 07b1f91062..2c4e8ef3dc 100644 --- a/zenovis/xinxinoptix/IOMat.h +++ b/zenovis/xinxinoptix/IOMat.h @@ -33,6 +33,7 @@ struct MatOutput { vec3 nrm; vec3 emission; + float vol_anisotropy; }; struct MatInput { diff --git a/zenovis/xinxinoptix/PTKernel.cu b/zenovis/xinxinoptix/PTKernel.cu index 7dc4016b99..3ec0ec0043 100644 --- a/zenovis/xinxinoptix/PTKernel.cu +++ b/zenovis/xinxinoptix/PTKernel.cu @@ -8,6 +8,7 @@ #include "DisneyBSDF.h" #include "zxxglslvec.h" +#include "volume.h" #include extern "C" { @@ -134,6 +135,7 @@ extern "C" __global__ void __raygen__rg() prd.direction = ray_direction; prd.curMatIdx = 0; prd.test_distance = false; + prd.sigma_t_queue[0] = vec3(0.0f); prd.ss_alpha_queue[0] = vec3(-1.0f); prd.minSpecRough = 0.01; prd.samplePdf = 1.0f; @@ -337,10 +339,8 @@ extern "C" __global__ void __miss__radiance() } vec3 sigma_t, ss_alpha; - //vec3 sigma_t, ss_alpha; prd->readMat(sigma_t, ss_alpha); - vec3 transmittance; if (ss_alpha.x < 0.0f) { // is inside Glass transmittance = DisneyBSDF::Transmission(sigma_t, optixGetRayTmax()); @@ -351,18 +351,28 @@ extern "C" __global__ void __miss__radiance() prd->attenuation *= transmittance;//DisneyBSDF::Transmission(prd->extinction,optixGetRayTmax()); prd->attenuation2 *= transmittance;//DisneyBSDF::Transmission(prd->extinction,optixGetRayTmax()); prd->origin += prd->direction * optixGetRayTmax(); - prd->direction = DisneyBSDF::SampleScatterDirection(prd->seed); + + auto anisotropy = prd->anisotropy_queue[prd->curMatIdx]; + pbrt::HenyeyGreenstein hg{__half2float(anisotropy)}; float3 newdir; + + float2 uu = {rnd(prd->seed), rnd(prd->seed)}; + auto prob = hg.Sample_p(-prd->direction, newdir, uu); + + prd->direction = newdir; //DisneyBSDF::SampleScatterDirection(prd->seed); vec3 channelPDF = vec3(1.0f/3.0f); prd->channelPDF = channelPDF; if (ss_alpha.x < 0.0f) { // is inside Glass prd->maxDistance = DisneyBSDF::SampleDistance2(prd->seed, sigma_t, sigma_t, channelPDF); - } else + prd->isSS = false; + } + else { prd->maxDistance = DisneyBSDF::SampleDistance2(prd->seed, vec3(prd->attenuation) * ss_alpha, sigma_t, channelPDF); prd->channelPDF = channelPDF; - } + prd->isSS = true; + } prd->depth++; diff --git a/zenovis/xinxinoptix/TraceStuff.h b/zenovis/xinxinoptix/TraceStuff.h index 3c84ecaba7..fa77a1d04b 100644 --- a/zenovis/xinxinoptix/TraceStuff.h +++ b/zenovis/xinxinoptix/TraceStuff.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "zxxglslvec.h" #include "optixPathTracer.h" @@ -96,8 +97,9 @@ struct RadiancePRD float3 LP; float3 Ldir; float Lweight; - vec3 sigma_t_queue[8]; - vec3 ss_alpha_queue[8]; + vec3 sigma_t_queue[8]{}; + vec3 ss_alpha_queue[8]{}; + half anisotropy_queue[8]{}; int curMatIdx; float samplePdf; bool fromDiff; @@ -152,7 +154,7 @@ struct RadiancePRD attenuation *= multiplier; } - int pushMat(vec3 extinction, vec3 ss_alpha = vec3(-1.0f)) + int pushMat(vec3 extinction, vec3 ss_alpha = vec3(-1.0f), float aniso = 0.0f) { vec3 d = abs(sigma_t_queue[curMatIdx] - extinction); float c = dot(d, vec3(1,1,1)); @@ -160,8 +162,8 @@ struct RadiancePRD { curMatIdx++; sigma_t_queue[curMatIdx] = extinction; - ss_alpha_queue[curMatIdx] = ss_alpha; - + ss_alpha_queue[curMatIdx] = ss_alpha; + anisotropy_queue[curMatIdx] = __float2half(aniso); } return curMatIdx;