Skip to content

Commit

Permalink
Merge pull request #1573 from zenustech/fixVNDFProblem
Browse files Browse the repository at this point in the history
Fix vndf problem
  • Loading branch information
zhxx1987 authored Dec 5, 2023
2 parents 14ee652 + 9cb07b8 commit 92a711f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 57 deletions.
5 changes: 3 additions & 2 deletions projects/ImgCV/ImageProcessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,9 @@ struct ImageBlur : INode {
cv::bilateralFilter(imagecvin,imagecvout, kernelSize, sigmaColor, sigmaSpace);
}
else if(type == "Stack"){
//cv::stackBlur(imagecvin,imagecvout,cv::Size(kernelSize, kernelSize));
//opencv version :(

// cv::stackBlur(imagecvin,imagecvout,cv::Size(kernelSize, kernelSize));

}
else{
zeno::log_error("ImageBlur: Blur type does not exist");
Expand Down
3 changes: 3 additions & 0 deletions zeno/src/nodes/mtl/ShaderFinalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct ShaderFinalize : INode {
{1, "mat_ior"},

{1, "mat_flatness"},
{1, "mat_shadowReceiver"},
{1, "mat_thin"},
{1, "mat_doubleSide"},
{3, "mat_normal"},
Expand Down Expand Up @@ -115,6 +116,7 @@ struct ShaderFinalize : INode {
get_input<IObject>("ior", std::make_shared<NumericObject>(float(1.5f))),

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))),
get_input<IObject>("doubleSide", std::make_shared<NumericObject>(float(0.0f))),
get_input<IObject>("normal", std::make_shared<NumericObject>(vec3f(0, 0, 1))),
Expand Down Expand Up @@ -291,6 +293,7 @@ ZENDEFNODE(ShaderFinalize, {
{"float", "ior", "1.5"},

{"float", "flatness", "0.0"},
{"float", "shadowReceiver", "0.0"},
{"float", "thin", "0.0"},
{"float", "doubleSide", "0.0"},
{"vec3f", "normal", "0,0,1"},
Expand Down
68 changes: 39 additions & 29 deletions zenovis/xinxinoptix/DeflMatShader.cu
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ static __inline__ __device__ MatOutput evalMat(cudaTextureObject_t zenotex[], fl
float mat_emissionIntensity = float(0);
vec3 mat_emission = vec3(1.0f, 1.0f, 1.0f);
float mat_displacement = 0.0f;
float mat_shadowReceiver = 0.0f;
float mat_NoL = 1.0f;
float mat_LoV = 1.0f;
vec3 mat_reflectance = att_reflectance;
Expand Down Expand Up @@ -140,6 +141,7 @@ static __inline__ __device__ MatOutput evalMat(cudaTextureObject_t zenotex[], fl
mats.flatness = mat_flatness;
mats.thin = mat_thin;
mats.doubleSide = mat_doubleSide;
mats.shadowReceiver = mat_shadowReceiver;


mats.smoothness = mat_smoothness;
Expand Down Expand Up @@ -655,7 +657,7 @@ extern "C" __global__ void __closesthit__radiance()

bool next_ray_is_going_inside = false;
mats.sssParam = mats.subsurface>0 ? mats.subsurface*mats.sssParam : mats.sssParam;
//mats.subsurface = mats.subsurface>0 ? 1 : 0;
mats.subsurface = mats.subsurface>0 ? 1 : 0;

/* MODME */

Expand Down Expand Up @@ -995,18 +997,18 @@ extern "C" __global__ void __closesthit__radiance()

prd->medium = next_ray_is_going_inside?DisneyBSDF::PhaseFunctions::isotropic : prd->curMatIdx==0?DisneyBSDF::PhaseFunctions::vacuum : DisneyBSDF::PhaseFunctions::isotropic;

if(mats.thin>0.5f){
vec3 H = normalize(vec3(normalize(wi)) + vec3(-normalize(ray_dir)));
attrs.N = N;
attrs.T = cross(B,N);
attrs.L = vec3(normalize(wi));
attrs.V = vec3(-normalize(ray_dir));
attrs.H = normalize(H);
attrs.reflectance = reflectance;
attrs.fresnel = DisneyBSDF::DisneyFresnel(mats.basecolor, mats.metallic, mats.ior, mats.specularTint, dot(attrs.H, attrs.V), dot(attrs.H, attrs.L), false);
MatOutput mat2 = evalReflectance(zenotex, rt_data->uniforms, attrs);
reflectance = mat2.reflectance;
}
// if(mats.thin>0.5f){
// vec3 H = normalize(vec3(normalize(wi)) + vec3(-normalize(ray_dir)));
// attrs.N = N;
// attrs.T = cross(B,N);
// attrs.L = vec3(normalize(wi));
// attrs.V = vec3(-normalize(ray_dir));
// attrs.H = normalize(H);
// attrs.reflectance = reflectance;
// attrs.fresnel = DisneyBSDF::DisneyFresnel(mats.basecolor, mats.metallic, mats.ior, mats.specularTint, dot(attrs.H, attrs.V), dot(attrs.H, attrs.L), false);
// MatOutput mat2 = evalReflectance(zenotex, rt_data->uniforms, attrs);
// reflectance = mat2.reflectance;
// }

prd->countEmitted = false;
prd->attenuation *= reflectance;
Expand All @@ -1029,20 +1031,20 @@ extern "C" __global__ void __closesthit__radiance()
mats.thin > 0.5f, flag == DisneyBSDF::transmissionEvent ? inToOut : next_ray_is_going_inside, thisPDF, rrPdf,
dot(N, L), rd, rs, rt);

MatOutput mat2;
if(mats.thin>0.5f){
vec3 H = normalize(vec3(normalize(L)) + V);
attrs.N = N;
attrs.T = cross(B,N);
attrs.L = vec3(normalize(L));
attrs.V = V;
attrs.H = normalize(H);
attrs.reflectance = lbrdf;
attrs.fresnel = DisneyBSDF::DisneyFresnel( mats.basecolor, mats.metallic, mats.ior, mats.specularTint, dot(attrs.H, attrs.V), dot(attrs.H, attrs.L), false);
mat2 = evalReflectance(zenotex, rt_data->uniforms, attrs);
}

return (mats.thin>0.5f? float3(mat2.reflectance):lbrdf);
// MatOutput mat2;
// if(mats.thin>0.5f){
// vec3 H = normalize(vec3(normalize(L)) + V);
// attrs.N = N;
// attrs.T = cross(B,N);
// attrs.L = vec3(normalize(L));
// attrs.V = V;
// attrs.H = normalize(H);
// attrs.reflectance = lbrdf;
// attrs.fresnel = DisneyBSDF::DisneyFresnel( mats.basecolor, mats.metallic, mats.ior, mats.specularTint, dot(attrs.H, attrs.V), dot(attrs.H, attrs.L), false);
// mat2 = evalReflectance(zenotex, rt_data->uniforms, attrs);
// }

return lbrdf;
};

auto taskAux = [&](const vec3& weight) {
Expand All @@ -1057,8 +1059,16 @@ extern "C" __global__ void __closesthit__radiance()
shadow_prd.nonThinTransHit = (mats.thin == false && mats.specTrans > 0) ? 1 : 0;

prd->direction = normalize(wi);

DirectLighting<true>(prd, shadow_prd, shadingP, ray_dir, evalBxDF, &taskAux);
float3 radianceNoShadow = make_float3(0,0,0);
DirectLighting<true>(prd, shadow_prd, shadingP, ray_dir, evalBxDF, radianceNoShadow, &taskAux);
if(mats.shadowReceiver > 0.5f)
{
auto radiance = length(prd->radiance);
prd->radiance.x = radiance;//the light contribution received with shadow attenuation
prd->radiance.y = length(radianceNoShadow);
prd->radiance.z = 0;
prd->done = true;
}

if(mats.thin<0.5f && mats.doubleSide<0.5f){
prd->origin = rtgems::offset_ray(P, (next_ray_is_going_inside)? -prd->geometryNormal : prd->geometryNormal);
Expand Down
52 changes: 31 additions & 21 deletions zenovis/xinxinoptix/DisneyBSDF.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ namespace DisneyBSDF{
vec3 CalculateExtinction(vec3 apparantColor, float scaler)
{
return 1.0/(max(apparantColor * scaler,vec3(0.000001)));
// return (1.0f - apparantColor) * scaler;
}

static __inline__ __device__
Expand Down Expand Up @@ -301,7 +300,7 @@ namespace DisneyBSDF{
float diffPr = dielectricWt;
float sssPr = dielectricWt * psss;
float dielectricPr = dielectricWt * Luminance(mix(Cspec0, vec3(1.0), schlickWt));
float metalPr = metalWt * Luminance(mix(mat.basecolor, vec3(1.0), schlickWt));
float metalPr = metalWt;
float glassPr = glassWt;
float clearCtPr = 0.25 * mat.clearcoat;

Expand Down Expand Up @@ -332,7 +331,7 @@ namespace DisneyBSDF{
if(diffPr > 0.0 && reflect)
{

vec3 d = BRDFBasics::EvalDisneyDiffuse(mix(mat.basecolor,mat.sssColor,mat.subsurface), mat.subsurface, mat.roughness, mat.sheen,
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 * illum;
dterm = dterm + d;
f = f + d;
Expand Down Expand Up @@ -403,6 +402,7 @@ namespace DisneyBSDF{
}
if(clearCtPr>0.0 && reflect)
{
vec3 wm = normalize(wi + wo);
vec3 s = BRDFBasics::EvalClearcoat(mat.clearcoatRoughness, wo, wi,
wm, tmpPdf) * 0.25 * mat.clearcoat * illum;
sterm = sterm + s;
Expand All @@ -416,8 +416,15 @@ namespace DisneyBSDF{
float FV = BRDFBasics::SchlickWeight(abs(wo.z));
float term = wo.z>0?FV:FL;
float tmpPdf = trans? 1.0f : 0.0f;//0.5/M_PIf:0.0f;

// auto wo2 = wo;
// wo2.z *= -1.0f;
// vec3 d2 = BRDFBasics::EvalDisneyDiffuse(mix(mat.basecolor,mat.sssColor,mat.subsurface), mat.subsurface, mat.roughness, mat.sheen,
// Csheen, wo2, wi, wm, tmpPdf) * illum;


// vec3 d = 1.0f/M_PIf * (1.0f - 0.5f * term) * (trans?vec3(1.0f):vec3(0.0f)) * dielectricWt * subsurface;
vec3 d = (trans?vec3(1.0f):vec3(0.0f)) * dielectricWt * mat.subsurface;
vec3 d = (trans? (thin? mat.sssColor : vec3(1.0f)): vec3(0.0f)) * dielectricWt * mat.subsurface;
dterm = dterm + d;
f = f + d;
fPdf += tmpPdf * sssPr;
Expand Down Expand Up @@ -544,10 +551,10 @@ namespace DisneyBSDF{
rotateTangent(T, B, N, mat.anisoRotation * 2 * 3.1415926f);
world2local(wo, T, B, N);
float2 r = sobolRnd(eventseed);
float r1 = r.x;
float r2 = r.y;
// float r1 = rnd(seed);
// float r2 = rnd(seed);
// float r1 = r.x;
// float r2 = r.y;
float r1 = rnd(seed);
float r2 = rnd(seed);

vec3 Csheen, Cspec0;
float F0;
Expand Down Expand Up @@ -575,7 +582,7 @@ namespace DisneyBSDF{
float diffPr = dielectricWt ;
float sssPr = dielectricWt * psss;
float dielectricPr = dielectricWt * Luminance(mix(Cspec0, vec3(1.0), schlickWt));
float metalPr = metalWt * Luminance(mix(mat.basecolor, vec3(1.0), schlickWt));
float metalPr = metalWt;
float glassPr = glassWt;
float clearCtPr = 0.25 * mat.clearcoat;

Expand Down Expand Up @@ -619,6 +626,7 @@ namespace DisneyBSDF{
isSS = false;
}else
{

//go inside
wi = -BRDFBasics::UniformSampleHemisphere(r1, r2);
isSS = true;
Expand Down Expand Up @@ -654,21 +662,23 @@ namespace DisneyBSDF{
float ax, ay;
BRDFBasics::CalculateAnisotropicParams(mat.roughness,mat.anisotropic,ax,ay);

vec3 wm = BRDFBasics::SampleGGXVNDF(wo.z>0?wo:-wo, ax, ay, r1, r2);
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 = -wm;
wm.z = -wm.z;

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

wi = normalize(reflect(-wo, wm));
tbn.inverse_transform(wi);
wi = normalize(wi);

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

Expand Down Expand Up @@ -714,15 +724,15 @@ namespace DisneyBSDF{
vec3 wm = BRDFBasics::SampleGTR1(mat.clearcoatRoughness, r1, r2);

if (wm.z < 0.0)
wm = -wm;
wm.z = -wm.z;
wm = wo.z>0?wm:-wm;
wi = normalize(reflect(-wo, wm));
tbn.inverse_transform(wi);
wi = normalize(wi);
if(dot(wi, N2)<0)
{
wi = normalize(wi - 1.01f * dot(wi, N2) * N2);
}
// if(dot(wi, N2)<0)
// {
// wi = normalize(wi - 1.01f * dot(wi, N2) * N2);
// }
}
tbn.inverse_transform(wo);
float pdf, pdf2;
Expand Down
1 change: 1 addition & 0 deletions zenovis/xinxinoptix/IOMat.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct MatOutput {
float scatterStep;
float smoothness;
float displacement;
float shadowReceiver;

vec3 nrm;
vec3 emission;
Expand Down
11 changes: 6 additions & 5 deletions zenovis/xinxinoptix/Light.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ namespace detail {

template<bool _MIS_, typename TypeEvalBxDF, typename TypeAux = void>
static __forceinline__ __device__
void DirectLighting(RadiancePRD *prd, RadiancePRD& shadow_prd, const float3& shadingP, const float3& ray_dir, TypeEvalBxDF& evalBxDF, TypeAux* taskAux=nullptr) {
void DirectLighting(RadiancePRD *prd, RadiancePRD& shadow_prd, const float3& shadingP, const float3& ray_dir, TypeEvalBxDF& evalBxDF, float3 &radianceNoShadow, TypeAux* taskAux=nullptr) {

const float3 wo = normalize(-ray_dir);
float3 light_attenuation = vec3(1.0f);
Expand Down Expand Up @@ -403,7 +403,7 @@ void DirectLighting(RadiancePRD *prd, RadiancePRD& shadow_prd, const float3& sha

light_attenuation = shadow_prd.shadowAttanuation;

if (lengthSquared(light_attenuation) > 0.0f) {
//if (lengthSquared(light_attenuation) > 0.0f) {

auto bxdf_value = evalBxDF(lsr.dir, wo, scatterPDF);
auto misWeight = 1.0f;
Expand All @@ -415,8 +415,9 @@ void DirectLighting(RadiancePRD *prd, RadiancePRD& shadow_prd, const float3& sha
emission *= lsr.intensity;

prd->radiance = light_attenuation * emission * bxdf_value;
prd->radiance *= misWeight / lsr.PDF;
}
prd->radiance *= misWeight / lsr.PDF;
radianceNoShadow = emission * bxdf_value * misWeight / lsr.PDF;
//}
}

} else {
Expand Down Expand Up @@ -474,7 +475,7 @@ void DirectLighting(RadiancePRD *prd, RadiancePRD& shadow_prd, const float3& sha
}

prd->radiance += (float3)(tmp) * bxdf_value;

radianceNoShadow += (float3)(tmp) * bxdf_value;
if constexpr (!detail::is_void<TypeAux>::value) {
if (taskAux != nullptr) {
(*taskAux)(tmp);
Expand Down

0 comments on commit 92a711f

Please sign in to comment.