Skip to content

Commit

Permalink
Merge pull request #1698 from zenustech/fixBugAndDiffraction
Browse files Browse the repository at this point in the history
Fix bug and diffraction
  • Loading branch information
zhxx1987 authored Jan 7, 2024
2 parents 7d2ce68 + ecb1188 commit 3769fe8
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 40 deletions.
15 changes: 12 additions & 3 deletions zeno/src/nodes/mtl/ShaderFinalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ struct ShaderFinalize : INode {
{3, "mat_transScatterColor"},
{1, "mat_ior"},

{1, "mat_diffraction"},
{3, "mat_diffractColor"},

{1, "mat_flatness"},
{1, "mat_shadowReceiver"},
{1, "mat_thin"},
Expand Down Expand Up @@ -116,11 +119,14 @@ struct ShaderFinalize : INode {
get_input<IObject>("specTrans", std::make_shared<NumericObject>(float(0.0f))),
get_input<IObject>("transColor", std::make_shared<NumericObject>(vec3f(1.0f))),
get_input<IObject>("transTint", std::make_shared<NumericObject>(vec3f(1.0f))),
get_input<IObject>("transTintDepth", std::make_shared<NumericObject>(float(1.0f))),
get_input<IObject>("transTintDepth", std::make_shared<NumericObject>(float(10000.0f))),
get_input<IObject>("transDistance", std::make_shared<NumericObject>(float(1.0f))),
get_input<IObject>("transScatterColor", std::make_shared<NumericObject>(vec3f(1.0f))),
get_input<IObject>("ior", std::make_shared<NumericObject>(float(1.5f))),

get_input<IObject>("diffraction", std::make_shared<NumericObject>(float(0.0f))),
get_input<IObject>("diffractColor", std::make_shared<NumericObject>(vec3f(0.0f))),

get_input<IObject>("flatness", std::make_shared<NumericObject>(float(0.0f))),
get_input<IObject>("shadowReceiver", std::make_shared<NumericObject>(float(0.0f))),
get_input<IObject>("thin", std::make_shared<NumericObject>(float(0.0f))),
Expand Down Expand Up @@ -312,10 +318,13 @@ ZENDEFNODE(ShaderFinalize, {
{"float", "specTrans", "0.0"},
{"vec3f", "transColor", "1.0,1.0,1.0"},
{"vec3f", "transTint", "1.0,1.0,1.0"},
{"float", "transTintDepth", "0.0"},
{"float", "transTintDepth", "10000.0"},
{"float", "transDistance", "10.0"},
{"vec3f", "transScatterColor", "1.0,1.0,1.0"},
{"float", "ior", "1.5"},
{"float", "ior", "1.3"},

{"float", "diffraction", "0.0"},
{"vec3f", "diffractColor", "0.0,0.0,0.0"},

{"float", "flatness", "0.0"},
{"float", "shadowReceiver", "0.0"},
Expand Down
28 changes: 21 additions & 7 deletions zenovis/xinxinoptix/CallableDefault.cu
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ extern "C" __device__ MatOutput __direct_callable__evalmat(cudaTextureObject_t z
auto att_instClr = attrs.instClr;
auto att_instTang = attrs.instTang;
auto att_rayLength = attrs.rayLength;
auto att_NoL = attrs.NoL;
auto att_LoV = attrs.LoV;
auto att_N = attrs.N;
auto att_T = attrs.T;
auto att_L = attrs.L;
auto att_V = attrs.V;
auto att_H = attrs.H;

vec3 b = normalize(cross(attrs.tang, attrs.nrm));
vec3 t = normalize(cross(attrs.nrm, b));
vec3 n = normalize(attrs.nrm);

auto att_N = vec3(0.0f,0.0f,1.0f);
auto att_T = vec3(1.0f,0.0f,0.0f);
auto att_L = normalize(vec3(dot(t, attrs.L), dot(b, attrs.L), dot(n, attrs.L)));
auto att_V = normalize(vec3(dot(t, attrs.V), dot(b, attrs.V), dot(n, attrs.V)));
auto att_H = vec3(0.0f,0.0f,1.0f);
auto att_NoL = att_L.z;
auto att_LoV = dot(att_L, att_V);
auto att_reflectance = attrs.reflectance;
auto att_fresnel = attrs.fresnel;

Expand Down Expand Up @@ -71,6 +76,9 @@ extern "C" __device__ MatOutput __direct_callable__evalmat(cudaTextureObject_t z
vec3 mat_transScatterColor = vec3(1.0f,1.0f,1.0f);
float mat_ior = 1.0f;

float mat_diffraction = 0.0f;
vec3 mat_diffractColor = vec3(0.0f);

float mat_flatness = 0.0f;
float mat_thin = 0.0f;
float mat_doubleSide= 0.0f;
Expand Down Expand Up @@ -124,6 +132,9 @@ extern "C" __device__ MatOutput __direct_callable__evalmat(cudaTextureObject_t z
vec3 mat_transScatterColor = vec3(1.0f,1.0f,1.0f);
float mat_ior = 1.0f;

float mat_diffraction = 0.0f;
vec3 mat_diffractColor = vec3(0.0f);

float mat_flatness = 0.0f;
float mat_thin = 0.0f;
float mat_doubleSide= 0.0f;
Expand Down Expand Up @@ -174,6 +185,9 @@ extern "C" __device__ MatOutput __direct_callable__evalmat(cudaTextureObject_t z
mats.transScatterColor = mat_transScatterColor;
mats.ior = max(0.0f,mat_ior);

mats.diffraction = clamp(mat_diffraction, 0.0f, 1.0f);
mats.diffractColor = clamp(mat_diffractColor, vec3(0.0f), vec3(1.0f));

mats.opacity = mat_opacity;
mats.nrm = mat_normal;
mats.emission = mat_emissionIntensity * mat_emission;
Expand Down
42 changes: 26 additions & 16 deletions zenovis/xinxinoptix/DeflMatShader.cu
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ extern "C" __global__ void __anyhit__shadow_cutout()
}

float nDi = fabs(dot(N,ray_dir));
vec3 fakeTrans = vec3(1)-BRDFBasics::fresnelSchlick(vec3(1)-basecolor,nDi);
vec3 fakeTrans = vec3(1)-BRDFBasics::fresnelSchlick(vec3(1),nDi);
prd->attanuation = prd->attanuation * fakeTrans;

#if (_SPHERE_)
Expand Down Expand Up @@ -383,9 +383,30 @@ extern "C" __global__ void __closesthit__radiance()
attrs.instClr = rt_data->instClr[inst_idx];
attrs.instTang = rt_data->instTang[inst_idx];
attrs.rayLength = optixGetRayTmax();

float3 n0 = normalize( decodeNormal(rt_data->nrm[ vert_idx_offset+0 ]) );
n0 = n0;

float3 n1 = normalize( decodeNormal(rt_data->nrm[ vert_idx_offset+1 ]) );
n1 = n1;

float3 n2 = normalize( decodeNormal(rt_data->nrm[ vert_idx_offset+2 ]) );
n2 = n2;

N_Local = normalize(interp(barys, n0, n1, n2));
N_World = optixTransformNormalFromObjectToWorldSpace(N_Local);

N = N_World;
#endif

attrs.pos = attrs.pos + vec3(params.cam.eye);
if(! (length(attrs.tang)>0.0f) )
{
Onb a(N);
attrs.tang = a.m_tangent;
}
attrs.nrm = N;
attrs.V = -normalize(ray_dir);
//MatOutput mats = evalMaterial(rt_data->textures, rt_data->uniforms, attrs);
MatOutput mats = optixDirectCall<MatOutput, cudaTextureObject_t[], float4*, const MatInput&>( rt_data->dc_index, rt_data->textures, rt_data->uniforms, attrs );

Expand All @@ -399,19 +420,8 @@ extern "C" __global__ void __closesthit__radiance()

#else

float3 n0 = normalize( decodeNormal(rt_data->nrm[ vert_idx_offset+0 ]) );
n0 = dot(n0, N_Local)>(1-mats.smoothness)?n0:N_Local;

float3 n1 = normalize( decodeNormal(rt_data->nrm[ vert_idx_offset+1 ]) );
n1 = dot(n1, N_Local)>(1-mats.smoothness)?n1:N_Local;

float3 n2 = normalize( decodeNormal(rt_data->nrm[ vert_idx_offset+2 ]) );
n2 = dot(n2, N_Local)>(1-mats.smoothness)?n2:N_Local;

N_Local = normalize(interp(barys, n0, n1, n2));
N_World = optixTransformNormalFromObjectToWorldSpace(N_Local);

N = N_World;

if(mats.doubleSide>0.5f||mats.thin>0.5f){
N = faceforward( N_World, -ray_dir, N_World );
Expand All @@ -433,10 +443,10 @@ extern "C" __global__ void __closesthit__radiance()
attrs.tang = cross(attrs.nrm, b);
N = mats.nrm.x * attrs.tang + mats.nrm.y * b + mats.nrm.z * attrs.nrm;
}
if(dot(vec3(ray_dir), vec3(N)) * dot(vec3(ray_dir), vec3(prd->geometryNormal))<0)
{
N = prd->geometryNormal;
}
// if(dot(vec3(ray_dir), vec3(N)) * dot(vec3(ray_dir), vec3(prd->geometryNormal))<0)
// {
// N = prd->geometryNormal;
// }

if (prd->trace_denoise_albedo) {

Expand Down
4 changes: 2 additions & 2 deletions zenovis/xinxinoptix/DisneyBRDF.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ vec3 EvalMicrofacetRefraction(vec3 baseColor, float ax, float ay, float eta, vec
float denom = LDotH * eta + VDotH;
denom *= denom;
float eta2 = eta * eta;
float jacobian = abs(LDotH) / denom;
float jacobian = abs(LDotH) / (denom + 1e-5f);

pdf = G1 * max(0.0, VDotH) * D * jacobian / abs(V.z);
return pow(baseColor, vec3(0.5f)) * (vec3(1.0f) - F)
Expand Down Expand Up @@ -498,7 +498,7 @@ vec3 EvalClearcoat(float ccR, vec3 V, vec3 L, vec3 H, float &pdf)
float jacobian = 1.0f / (4.0f * VDotH);

pdf = D * H.z * jacobian;
return vec3(F) * D * G;
return vec3(1.0) * D * G;
}
}

Expand Down
26 changes: 15 additions & 11 deletions zenovis/xinxinoptix/DisneyBSDF.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ namespace DisneyBSDF{
world2local(wo, T, B, N);
world2local(N2, T, B, N);

bool reflect = (dot(wi, N2) * dot(wo, N2) > 0.0f) || (dot(wi, N) * dot(wo, N) > 0.0f);
bool reflect = (dot(wi, N2) * dot(wo, N2) > 0.0f) || (wi.z * wo.z > 0.0f);

vec3 Csheen, Cspec0;
float F0;
Expand All @@ -355,11 +355,12 @@ namespace DisneyBSDF{
float glassWt = (1.0 - mat.metallic) * mat.specTrans;

float schlickWt = BRDFBasics::SchlickWeight(abs(dot(wo, wm)));
float F = BRDFBasics::DielectricFresnel(abs(dot(wo, wm)), mat.ior);
float psss = mat.subsurface;
//event probability
float diffPr = dielectricWt;
float sssPr = dielectricWt * psss;
float dielectricPr = dielectricWt * Luminance(mix(Cspec0, vec3(1.0), schlickWt));
float dielectricPr = dielectricWt * luminance(mix(Cspec0, vec3(1.0f), F) * mat.specular);
float metalPr = metalWt;
float glassPr = glassWt;
float clearCtPr = 0.25 * mat.clearcoat;
Expand Down Expand Up @@ -387,7 +388,6 @@ namespace DisneyBSDF{
dterm = vec3(0.0f);
sterm = vec3(0.0f);
tterm = vec3(0.0f);

if(diffPr > 0.0 && reflect)
{

Expand All @@ -403,14 +403,15 @@ namespace DisneyBSDF{
float ax, ay;
BRDFBasics::CalculateAnisotropicParams(mat.roughness,mat.anisotropic,ax,ay);
vec3 s = BRDFBasics::EvalMicrofacetReflection(ax, ay, wo, wi, wm,
mix(Cspec0, vec3(1.0f), F) * mat.specular, tmpPdf) * dielectricWt;
mix(mix(Cspec0, mat.diffractColor, mat.diffraction), vec3(1.0f), F) * mat.specular,
tmpPdf) * dielectricWt;
sterm = sterm + s;
f = f + s;
fPdf += tmpPdf * dielectricPr;
}
if(metalPr>0.0 && reflect)
{
vec3 F = mix(mat.basecolor, vec3(1.0), BRDFBasics::SchlickWeight(HoV));
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);
vec3 s = BRDFBasics::EvalMicrofacetReflection(ax, ay, wo, wi, wm, F, tmpPdf) * metalWt;
Expand All @@ -429,23 +430,24 @@ namespace DisneyBSDF{

vec3 wm = normalize(wi + wo);
float F = BRDFBasics::DielectricFresnel(abs(dot(wm, wo)), entering?mat.ior:1.0/mat.ior);
vec3 s = BRDFBasics::EvalMicrofacetReflection(ax, ay, wo, wi, wm, vec3(F) * mat.specular,
vec3 s = BRDFBasics::EvalMicrofacetReflection(ax, ay, wo, wi, wm,
mix(mix(Cspec0, mat.diffractColor, mat.diffraction), vec3(1.0f), F) * mat.specular,
tmpPdf) * glassWt;
sterm = sterm + s;
f = f + s;
fPdf += tmpPdf * glassPr * F;
} else {
if(thin)
{
vec3 t = sqrt(mat.transColor) * glassWt;
vec3 t = sqrt(mix(mat.transColor, mat.diffractColor, mat.diffraction)) * glassWt;
tterm = tterm + t;
f = f + t;
fPdf += 1.0f * glassPr;
}else {
vec3 wm = entering?-normalize(wo + mat.ior * wi) : normalize(wo + 1.0f/mat.ior * wi);
float F = BRDFBasics::DielectricFresnel(abs(dot(wm, wo)), entering?mat.ior:1.0/mat.ior);
float tmpPdf;
vec3 brdf = BRDFBasics::EvalMicrofacetRefraction(mat.transColor,
vec3 brdf = BRDFBasics::EvalMicrofacetRefraction(mix(mat.transColor, mat.diffractColor, mat.diffraction),
ax, ay,
entering? mat.ior:1.0f/mat.ior,
wo, wi, wm,
Expand All @@ -463,7 +465,9 @@ namespace DisneyBSDF{
if(clearCtPr>0.0 && reflect)
{
vec3 wm = normalize(wi + wo);
vec3 s = BRDFBasics::EvalClearcoat(mat.clearcoatRoughness, wo, wi,
//ior related clearCt
float F = BRDFBasics::DielectricFresnel(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;
Expand Down Expand Up @@ -580,13 +584,14 @@ namespace DisneyBSDF{

float hov = mix(hov1, hov2, c);
float schlickWt = BRDFBasics::SchlickWeight(hov);
float F = BRDFBasics::DielectricFresnel(hov, mat.ior);
float psss = mat.subsurface;
//dielectricWt *= 1.0f - psub;

//event probability
float diffPr = dielectricWt ;
float sssPr = dielectricWt * psss;
float dielectricPr = dielectricWt * Luminance(mix(Cspec0, vec3(1.0), schlickWt));
float dielectricPr = dielectricWt * luminance(mix(Cspec0, vec3(1.0f), F) * mat.specular);
float metalPr = metalWt;
float glassPr = glassWt;
float clearCtPr = 0.25 * mat.clearcoat;
Expand Down Expand Up @@ -634,7 +639,6 @@ namespace DisneyBSDF{
prd->fromDiff = true;
wi = BRDFBasics::CosineSampleHemisphere(r1, r2);
isSS = false;
wi = normalize(reflect(-wo, wm));
tbn.inverse_transform(wi);
wi = normalize(wi);

Expand Down
3 changes: 3 additions & 0 deletions zenovis/xinxinoptix/IOMat.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ struct MatOutput {
float transDistance;
vec3 transScatterColor;

float diffraction;
vec3 diffractColor;

float subsurface;
vec3 sssColor;
vec3 sssParam;
Expand Down
2 changes: 1 addition & 1 deletion zenovis/xinxinoptix/TraceStuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ struct RadiancePRD
auto dir = forward? geometryNormal:-geometryNormal;
auto offset = rtgems::offset_ray(P, dir);
float l = length( offset - P );
float l2 = this->alphaHit? clamp(l, 1e-4, 1e-2) : max(l, 1e-5);
float l2 = this->alphaHit? max(l, 1e-4) : max(l, 1e-5);
P = P + l2 * dir;
}

Expand Down

0 comments on commit 3769fe8

Please sign in to comment.