Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable anisotropy scattering for random walk #1208

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 19 additions & 32 deletions zenovis/xinxinoptix/DeflMatShader.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -878,8 +880,6 @@ extern "C" __global__ void __closesthit__radiance()
prd->diffDepth++;
}



prd->passed = false;
bool inToOut = false;
bool outToIn = false;
Expand All @@ -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;
}
Expand All @@ -912,16 +914,14 @@ 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;

prd->medium = DisneyBSDF::PhaseFunctions::isotropic;

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());
Expand All @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -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){
Expand Down
1 change: 1 addition & 0 deletions zenovis/xinxinoptix/IOMat.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct MatOutput {

vec3 nrm;
vec3 emission;
float vol_anisotropy;
};

struct MatInput {
Expand Down
20 changes: 15 additions & 5 deletions zenovis/xinxinoptix/PTKernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "DisneyBSDF.h"
#include "zxxglslvec.h"

#include "volume.h"
#include <cuda_fp16.h>

extern "C" {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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++;

Expand Down
12 changes: 7 additions & 5 deletions zenovis/xinxinoptix/TraceStuff.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <optix.h>
#include <cuda_fp16.h>

#include "zxxglslvec.h"
#include "optixPathTracer.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -152,16 +154,16 @@ 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));
if(curMatIdx<7 && c > 1e-6f )
{
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;
Expand Down