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

Optix update, enable double-side for all non-transparent materiel. #1914

Merged
merged 12 commits into from
Oct 16, 2024
19 changes: 10 additions & 9 deletions zenovis/xinxinoptix/DeflMatShader.cu
Original file line number Diff line number Diff line change
Expand Up @@ -969,18 +969,18 @@ extern "C" __global__ void __closesthit__radiance()
backPos = wldPos;
}

shadowPRD.origin = dot(-ray_dir, wldNorm) > 0 ? frontPos : backPos;
shadowPRD.origin = dot(wi, vec3(prd->geometryNormal)) > 0 ? frontPos : backPos;
//auto shadingP = rtgems::offset_ray(shadowPRD.origin + params.cam.eye, prd->geometryNormal); // world space

shadowPRD.origin = frontPos;
if(mats.subsurface>0 && (mats.thin>0.5 || mats.doubleSide>0.5) && istransmission){
shadowPRD.origin = backPos; //rtgems::offset_ray(P, -prd->geometryNormal);
}
//shadowPRD.origin = frontPos;
//if(mats.subsurface>0 && (mats.thin>0.5 || mats.doubleSide>0.5) && istransmission){
//shadowPRD.origin = backPos; //rtgems::offset_ray(P, -prd->geometryNormal);
//}

auto shadingP = rtgems::offset_ray(P + params.cam.eye, prd->geometryNormal); // world space
if(mats.subsurface>0 && (mats.thin>0.5 || mats.doubleSide>0.5) && istransmission){
shadingP = rtgems::offset_ray(P + params.cam.eye, -prd->geometryNormal);
}
auto shadingP = rtgems::offset_ray(P + params.cam.eye, dot(wi, vec3(prd->geometryNormal)) > 0 ? prd->geometryNormal:-prd->geometryNormal); // world space
//if(mats.subsurface>0 && (mats.thin>0.5 || mats.doubleSide>0.5) && istransmission){
//shadingP = rtgems::offset_ray(P + params.cam.eye, -prd->geometryNormal);
//}

prd->radiance = {};
prd->direction = normalize(wi);
Expand All @@ -994,6 +994,7 @@ extern "C" __global__ void __closesthit__radiance()
}

prd->lightmask = DefaultMatMask;
shadowPRD.ShadowNormal = dot(wi, vec3(prd->geometryNormal)) > 0 ? prd->geometryNormal:-prd->geometryNormal;
DirectLighting<true>(prd, shadowPRD, shadingP, ray_dir, evalBxDF, &taskAux, dummy_prt);
if(mats.shadowReceiver > 0.5f)
{
Expand Down
4 changes: 2 additions & 2 deletions zenovis/xinxinoptix/DisneyBRDF.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ float DielectricFresnel(float cosThetaI, float eta)
float eta2 = eta * eta;

float cos2t = 1.0f - sin2 / eta2;
if(cos2t < 0) return 1.0f;
if(cos2t < 0.0f) return 1.0f;

float t0 = sqrt(cos2t);
float t1 = eta * t0;
Expand Down Expand Up @@ -417,7 +417,7 @@ vec3 EvalDisneyDiffuse(vec3 baseColor, float subsurface, float roughness, float
float FH = SchlickWeight(LDotH);
vec3 Fsheen = FH * sheen * Csheen;

pdf = L.z * 1.0f / M_PIf;
pdf = abs(L.z) * 1.0f / M_PIf;
return 1.0f / M_PIf * baseColor * (Fd + Fretro) + Fsheen;
}

Expand Down
200 changes: 96 additions & 104 deletions zenovis/xinxinoptix/DisneyBSDF.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,27 @@ namespace DisneyBSDF{
return result;
}

static __inline__ __device__
vec3 EvaluateDiffuse(vec3 baseColor, float subsurface, float roughness, float sheen, vec3 Csheen, vec3 V, vec3 L, vec3 H, float &pdf){
pdf = 0.0f;
if (L.z == 0.0f)
return vec3(0.0f);

float LDotH = abs(dot(L, H));
float F90 = 0.5f + 2.0f * LDotH * LDotH * roughness;
// Diffuse
float FL = BRDFBasics::SchlickWeight(abs(L.z));
float FV = BRDFBasics::SchlickWeight(abs(V.z));
float Fd = mix(1.0f,F90,FL) * mix(1.0f,F90,FV);


// Sheen
float FH = BRDFBasics::SchlickWeight(LDotH);
vec3 Fsheen = FH * sheen * Csheen;

pdf =abs (L.z) * 1.0f / M_PIf;
return 1.0f / M_PIf * baseColor * (Fd + Fsheen);
}

static __inline__ __device__
float3 EvaluateDisney2(
Expand Down Expand Up @@ -351,7 +372,6 @@ namespace DisneyBSDF{

wm = wm.z<0.0f?-wm:wm;
BRDFBasics::TintColors(mix(mat.basecolor, mat.sssColor, mat.subsurface), eta, mat.specularTint, mat.sheenTint, F0, Csheen, Cspec0);
Cspec0 = Cspec0;
//material layer mix weight
float dielectricWt = (1.0 - mat.metallic) * (1.0 - mat.specTrans);
float metalWt = mat.metallic;
Expand Down Expand Up @@ -407,17 +427,18 @@ namespace DisneyBSDF{
f = f + h;
return f * abs(wi.z);
}
if(diffPr > 0.0 && reflect)
{

vec3 d = BRDFBasics::EvalDisneyDiffuse(thin? mat.basecolor : mix(mat.basecolor,mat.sssColor,mat.subsurface), mat.subsurface, mat.roughness, mat.sheen,
Csheen, wo, wi, wm, tmpPdf) * dielectricWt;
if(reflect){

if(diffPr > 0.0f){
//vec3 d = EvaluateDiffuse(thin? mat.basecolor : mix(mat.basecolor,mat.sssColor,mat.subsurface), mat.subsurface, mat.roughness, mat.sheen,Csheen, wo, wi, wm, tmpPdf) * dielectricWt;
vec3 d = BRDFBasics::EvalDisneyDiffuse(thin? mat.basecolor : mix(mat.basecolor,mat.sssColor,mat.subsurface), mat.subsurface, mat.roughness, mat.sheen,Csheen, wo, wi, wm, tmpPdf) * dielectricWt;
dterm = dterm + d;
f = f + d;
fPdf += tmpPdf * diffPr ;
}
if(dielectricPr>0.0 && reflect)
{
}

if(dielectricPr > 0.0f){
float F = BRDFBasics::SchlickDielectic(abs(dot(wm, wo)), mat.ior);
float ax, ay;
BRDFBasics::CalculateAnisotropicParams(mat.roughness,mat.anisotropic,ax,ay);
Expand All @@ -427,9 +448,9 @@ namespace DisneyBSDF{
sterm = sterm + s;
f = f + s;
fPdf += tmpPdf * dielectricPr;
}
if(metalPr>0.0 && reflect)
{
}

if(metalPr>0.0f){
vec3 F = mix(mix(mat.basecolor, mat.diffractColor, mat.diffraction), vec3(1.0), BRDFBasics::SchlickWeight(HoV));
float ax, ay;
BRDFBasics::CalculateAnisotropicParams(mat.roughness,mat.anisotropic,ax,ay);
Expand All @@ -439,12 +460,28 @@ namespace DisneyBSDF{
sterm = sterm + s;
f = f + s;
fPdf += tmpPdf * metalPr;
}

if(clearCtPr>0.0f){
vec3 wm = normalize(wi + wo);
float ax, ay;
BRDFBasics::CalculateAnisotropicParams(mat.clearcoatRoughness,0,ax,ay);
//ior related clearCt
float F = BRDFBasics::SchlickDielectic(abs(dot(wm, wo)), mat.clearcoatIOR);
vec3 s = mix(vec3(0.04f), vec3(1.0f), F) *
BRDFBasics::EvalClearcoat(mat.clearcoatRoughness, wo, wi,
wm, tmpPdf) * 0.25 * mat.clearcoat;
sterm = sterm + s;
f = f + s;
fPdf += tmpPdf * clearCtPr;
}

}
if(glassPr>0.0)

if(glassPr>0.0f)
{
bool entering = wo.z>0?true:false;

//float F = BRDFBasics::DielectricFresnel(, eta);
float ax, ay;
BRDFBasics::CalculateAnisotropicParams(mat.roughness,mat.anisotropic,ax,ay);
if (reflect) {
Expand Down Expand Up @@ -491,22 +528,7 @@ namespace DisneyBSDF{
}

}
if(clearCtPr>0.0 && reflect)
{
vec3 wm = normalize(wi + wo);
float ax, ay;
BRDFBasics::CalculateAnisotropicParams(mat.clearcoatRoughness,0,ax,ay);
//ior related clearCt
float F = BRDFBasics::SchlickDielectic(abs(dot(wm, wo)), mat.clearcoatIOR);
vec3 s = mix(vec3(0.04f), vec3(1.0f), F) *
BRDFBasics::EvalClearcoat(mat.clearcoatRoughness, wo, wi,
wm, tmpPdf) * 0.25 * mat.clearcoat;
sterm = sterm + s;
f = f + s;
fPdf += tmpPdf * clearCtPr;
}
if((sssPr>0.0&&reflectance) || (sssPr>0.0 && dot(wo, N2)<0.0) || (sssPr>0.0 && (thin)))
{
if(sssPr > 0.0f && (reflectance || dot(wo,N2) < 0.0f || thin)){
bool trans = (dot(wi, N2) * dot(wo, N2)<0) && (wi.z * wo.z<0);
float FL = BRDFBasics::SchlickWeight(abs(wi.z));
float FV = BRDFBasics::SchlickWeight(abs(wo.z));
Expand Down Expand Up @@ -555,6 +577,16 @@ namespace DisneyBSDF{
return 1.0f / ( n * n) - (1.0f - c * c);
}

static __inline__ __device__
void SampleSpecular(vec3 wo, vec3& wi, float rough, float aniso, float r1, float r2){
float ax, ay;
BRDFBasics::CalculateAnisotropicParams(rough,aniso,ax,ay);
vec3 vtmp = wo;
vtmp.z = abs(vtmp.z);
vec3 wm = BRDFBasics::SampleGGXVNDF(vtmp, ax, ay, r1, r2);

wi = normalize(reflect(-wo, wm));
}


static __inline__ __device__
Expand Down Expand Up @@ -683,10 +715,6 @@ namespace DisneyBSDF{
isSS = false;
tbn.inverse_transform(wi);
wi = normalize(wi);

if (dot(wi, N2) < 0) {
wi = normalize(wi - 1.01f * dot(wi, N2) * N2);
}
}
else{
//switch between scattering or diffuse reflection
Expand All @@ -695,17 +723,14 @@ namespace DisneyBSDF{
{
prd->fromDiff = true;
wi = BRDFBasics::CosineSampleHemisphere(r1, r2);
if(wo.z<0.0f){
wi.z = -wi.z;
}
isSS = false;
tbn.inverse_transform(wi);
wi = normalize(wi);

if(dot(wi, N2)<0)
{
wi = normalize(wi - 1.01f * dot(wi, N2) * N2);
}
}else
{

//go inside
wi = -BRDFBasics::UniformSampleHemisphere(r1, r2);
wi.z = min(-0.2f, wi.z);
Expand All @@ -724,11 +749,6 @@ namespace DisneyBSDF{
tbn.inverse_transform(wi);
wi = normalize(wi);

bool sameside2 = (dot(wi, N) * dot(wi, N2)) > 0.0f;
if (sameside == false) {
wi = normalize(wi - 1.01f * dot(wi, N2) * N2);
}

}
}

Expand All @@ -744,62 +764,45 @@ namespace DisneyBSDF{
else if(r3<p3)//specular
{
prd->hit_type = SPECULAR_HIT;
float ax, ay;
BRDFBasics::CalculateAnisotropicParams(mat.roughness,mat.anisotropic,ax,ay);

vec3 vtmp = wo;
vtmp.z = wo.z>0?vtmp.z:-vtmp.z;
vec3 wm = BRDFBasics::SampleGGXVNDF(vtmp, ax, ay, r1, r2);

if (wm.z < 0.0)
wm.z = -wm.z;

wm.z = wo.z>0? wm.z:-wm.z;

wi = normalize(reflect(-wo, wm));
SampleSpecular(wo,wi,mat.roughness,mat.anisotropic,r1,r2);
tbn.inverse_transform(wi);
wi = normalize(wi);

if(dot(wi, N2)<0)
{
wi = normalize(wi - 1.01f * dot(wi, N2) * N2);
}
}else if(r3<p4)//glass
{

bool entering = wo.z>0?true:false;
float ax, ay;
BRDFBasics::CalculateAnisotropicParams(mat.roughness,mat.anisotropic,ax,ay);
vec3 swo = wo.z>0?wo:-wo;
vec3 wm = BRDFBasics::SampleGGXVNDF(swo, ax, ay, r1, r2);
wm = wm.z<0?-wm:wm;

wm = entering?wm:-wm;

float F = BRDFBasics::DielectricFresnel(abs(dot(wm, wo)), entering?mat.ior:1.0f/mat.ior);
float p = rnd(seed);
if(p<F)//reflection
{
wi = normalize(reflect(-normalize(wo),wm));
}else //refraction
bool entering = wo.z>0?true:false;
float ax, ay;
BRDFBasics::CalculateAnisotropicParams(mat.roughness,mat.anisotropic,ax,ay);
vec3 swo = wo.z>0?wo:-wo;
vec3 wm = BRDFBasics::SampleGGXVNDF(swo, ax, ay, r1, r2);
wm = wm.z<0?-wm:wm;

wm = entering?wm:-wm;

float F = BRDFBasics::DielectricFresnel(abs(dot(wm, wo)), entering?mat.ior:1.0f/mat.ior);
float p = rnd(seed);
if(p<F)//reflection
{
wi = normalize(reflect(-normalize(wo),wm));
}else //refraction
{
if(thin)
{
if(thin)
{
wi = -wo;
extinction = vec3(0.0f);
}else {
wi = normalize(
refract(wo, wm, entering ? 1.0f / mat.ior : mat.ior));
flag = transmissionEvent;
isTrans = true;
extinction =
CalculateExtinction(mat.transTint, mat.transTintDepth);
extinction = entering ? extinction : vec3(0.0f);
}
wi = -wo;
extinction = vec3(0.0f);
}else {
wi = normalize(
refract(wo, wm, entering ? 1.0f / mat.ior : mat.ior));
flag = transmissionEvent;
isTrans = true;
extinction =
CalculateExtinction(mat.transTint, mat.transTintDepth);
extinction = entering ? extinction : vec3(0.0f);
}
}

tbn.inverse_transform(wi);
wi = normalize(wi);
tbn.inverse_transform(wi);
wi = normalize(wi);
minSpecRough = mat.roughness;
auto woo = wo;
tbn.inverse_transform(woo);
Expand All @@ -814,20 +817,9 @@ namespace DisneyBSDF{
}else if(r3<p5)//cc
{
prd->hit_type = SPECULAR_HIT;
float ax, ay;
BRDFBasics::CalculateAnisotropicParams(mat.clearcoatRoughness,0,ax,ay);
vec3 swo = wo.z>0?wo:-wo;
vec3 wm = BRDFBasics::SampleGGXVNDF(swo, ax, ay, r1, r2);
wm = wm.z<0?-wm:wm;


wi = normalize(reflect(-wo, wm));
SampleSpecular(wo,wi,mat.clearcoatRoughness,0.0f,r1,r2);
tbn.inverse_transform(wi);
wi = normalize(wi);
if(dot(wi, N2)<0)
{
wi = normalize(wi - 1.01f * dot(wi, N2) * N2);
}
}

tbn.inverse_transform(wo);
Expand Down
2 changes: 1 addition & 1 deletion zenovis/xinxinoptix/Light.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void DirectLighting(RadiancePRD *prd, ShadowPRD& shadowPRD, const float3& shadin
UF = (UF - _SKY_PROB_) / lightPickProb;

const Vector3f& SP = reinterpret_cast<const Vector3f&>(shadingP);
const Vector3f& SN = reinterpret_cast<const Vector3f&>(prd->geometryNormal);
const Vector3f& SN = reinterpret_cast<const Vector3f&>(shadowPRD.ShadowNormal);

auto pick = lightTree->sample(UF, SP, SN);
if (pick.prob <= 0.0f) { return; }
Expand Down
1 change: 1 addition & 0 deletions zenovis/xinxinoptix/TraceStuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct ShadowPRD {
uint8_t nonThinTransHit;

VolumePRD vol;
float3 ShadowNormal;

float rndf() {
return rnd(seed);
Expand Down
Loading