Skip to content

Commit

Permalink
fix assymetrical light transport for glass (refraction)
Browse files Browse the repository at this point in the history
  • Loading branch information
FROL256 committed Feb 4, 2019
1 parent 9cd9307 commit 3307128
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 21 deletions.
6 changes: 4 additions & 2 deletions hydra_app/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Input::Input()
//noWindow = false; ///< run 'console_main', else run 'window_main'
//inLibraryPath = "tests/test_42"; ///< cornell box with teapot
//inLibraryPath = "tests/test_223_small"; ///< cornell box with sphere
inLibraryPath = "tests/test_224_sphere";
//inLibraryPath = "tests/test_224_sphere";
//inLibraryPath = "tests/test_224_sphere_microfacet";
//inLibraryPath = "/media/frol/886234F06234E49A/scenes/benchmark4"; ///< cornell box with mirror glossy back wall
//inLibraryPath = "/media/frol/886234F06234E49A/scenes/phong_test/torspar1";
Expand All @@ -31,13 +31,15 @@ Input::Input()
//inLibraryPath = "D:/[archive]/2017/HydraAPP/hydra_app/tests/hydra_benchmark_07";
//inLibraryPath = "D:/[archive]/2017/HydraOldRepo/HydraAPP/home/frol/hydra/rendered_images/a_3602.png/hydra_app/tests/hydra_benchmark_07";
//inLibraryPath = "/home/frol/PROG/HydraAPI/main/tests/test_77";
//inLibraryPath = "/home/frol/PROG/HydraAPI/main/tests_f/test_202";
inLibraryPath = "/home/frol/PROG/HydraAPI/main/tests_f/test_403";

//inLibraryPath = "C:/[Hydra]/pluginFiles/scenelib";
//inLibraryPath = "/media/frol/6E0467C0046789C3/[Hydra]/pluginFiles/scenelib";
//inLibraryPath = "D:/temp/scenelib/";
//inLibraryPath = "/home/frol/PROG/HydraAPI/main/tests/test_76";
//inLibraryPath = "/home/frol/temp/scenelib";
//inLibraryPath = "/home/frol/temp/scenelib_thinSphere";
//inLibraryPath = "/home/frol/PROG/HydraCore/hydra_app/tests/test_pool";

inDevelopment = true; ///< recompile shaders each time; note that nvidia have their own shader cache!
inDeviceId = 0; ///< opencl device id
Expand Down
6 changes: 3 additions & 3 deletions hydra_drv/CPUExp_IntegratorSSS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ std::tuple<MatSample, int, float3> IntegratorStupidPTSSS::sampleAndEvalBxDF(floa
return std::make_tuple(res, 0, float3(1, 1, 1));
}
else
return IntegratorCommon::sampleAndEvalBxDF(ray_dir, surfElem, flags, shadow);
return IntegratorCommon::sampleAndEvalBxDF(ray_dir, surfElem, flags, false, shadow);
}

float3 SampleHenyeyGreenstein(const float g, const float e1, const float e2)
Expand Down Expand Up @@ -303,7 +303,7 @@ std::tuple<MatSample, int, float3> IntegratorShadowPTSSS::sampleAndEvalBxDF(floa
return std::make_tuple(res, 0, float3(1, 1, 1));
}
else
return IntegratorCommon::sampleAndEvalBxDF(ray_dir, surfElem, flags, shadow);
return IntegratorCommon::sampleAndEvalBxDF(ray_dir, surfElem, flags, false, shadow);
}


Expand Down Expand Up @@ -537,7 +537,7 @@ std::tuple<MatSample, int, float3> IntegratorShadowPTSSS::sampleAndEvalBxDF(floa
return std::make_tuple(res, 0, float3(1, 1, 1));
}
else
return IntegratorCommon::sampleAndEvalBxDF(ray_dir, surfElem, flags, shadow);
return IntegratorCommon::sampleAndEvalBxDF(ray_dir, surfElem, flags, false, shadow);
}

float3 IntegratorShadowPTSSS::PathTrace(float3 ray_pos, float3 ray_dir, MisData misPrev, int a_currDepth, uint flags)
Expand Down
2 changes: 1 addition & 1 deletion hydra_drv/CPUExp_Integrators.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class IntegratorCommon : public Integrator
virtual std::tuple<float3, float3> makeEyeRay3(float4 lensOffs);

float3 emissionEval(float3 ray_pos, float3 ray_dir, const SurfaceHit& surfElem, uint flags, const MisData misPrev, const int a_instId);
virtual std::tuple<MatSample, int, float3> sampleAndEvalBxDF(float3 ray_dir, const SurfaceHit& surfElem, uint flags = 0, float3 shadow = float3(0,0,0), bool mmltMode = false);
virtual std::tuple<MatSample, int, float3> sampleAndEvalBxDF(float3 ray_dir, const SurfaceHit& surfElem, uint flags = 0, bool a_fwdDir = false, float3 shadow = float3(0,0,0), bool mmltMode = false);

GBufferAll gbufferSample(float3 ray_pos, float3 ray_dir);

Expand Down
4 changes: 2 additions & 2 deletions hydra_drv/CPUExp_Integrators_Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ RandomGen& IntegratorCommon::randomGen()
return m_perThread[omp_get_thread_num()].gen;
}

std::tuple<MatSample, int, float3> IntegratorCommon::sampleAndEvalBxDF(float3 ray_dir, const SurfaceHit& surfElem, uint flags, float3 shadow, bool a_mmltMode)
std::tuple<MatSample, int, float3> IntegratorCommon::sampleAndEvalBxDF(float3 ray_dir, const SurfaceHit& surfElem, uint flags, bool a_fwdDir, float3 shadow, bool a_mmltMode)
{
const PlainMaterial* pHitMaterial = materialAt(m_pGlobals, m_matStorage, surfElem.matId);
auto& gen = randomGen();
Expand All @@ -552,7 +552,7 @@ std::tuple<MatSample, int, float3> IntegratorCommon::sampleAndEvalBxDF(float3 ra
allRands);

MatSample brdfSample; int matOffset;
MaterialSampleAndEvalBxDF(pHitMaterial, allRands, &surfElem, ray_dir, shadow, flags,
MaterialSampleAndEvalBxDF(pHitMaterial, allRands, &surfElem, ray_dir, shadow, flags, a_fwdDir,
m_pGlobals, m_texStorage, m_texStorageAux, &m_ptlDummy,
&brdfSample, &matOffset);

Expand Down
4 changes: 2 additions & 2 deletions hydra_drv/CPUExp_Integrators_MMLT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ void IntegratorMMLT::TraceLightPath(float3 ray_pos, float3 ray_dir, int a_currDe
a_perThread->pdfArray[a_currDepth].pdfFwd = -1.0f*GTermPrev;

const PlainMaterial* pHitMaterial = materialAt(m_pGlobals, m_matStorage, surfElem.matId);
const MatSample matSam = std::get<0>(sampleAndEvalBxDF(ray_dir, surfElem, packBounceNum(0, a_currDepth - 1), float3(0, 0, 0), true));
const MatSample matSam = std::get<0>(sampleAndEvalBxDF(ray_dir, surfElem, packBounceNum(0, a_currDepth - 1), true, float3(0, 0, 0), true));

// calc new ray
//
Expand Down Expand Up @@ -869,7 +869,7 @@ PathVertex IntegratorMMLT::CameraPath(float3 ray_pos, float3 ray_dir, MisData a_
// (3) eval reverse and forward pdfs
//
const PlainMaterial* pHitMaterial = materialAt(m_pGlobals, m_matStorage, surfElem.matId);
const MatSample matSam = std::get<0>(sampleAndEvalBxDF(ray_dir, surfElem, packBounceNum(0, a_currDepth - 1), float3(0, 0, 0), true));
const MatSample matSam = std::get<0>(sampleAndEvalBxDF(ray_dir, surfElem, packBounceNum(0, a_currDepth - 1), false, float3(0, 0, 0), true));
const float3 bxdfVal = matSam.color; // *(1.0f / fmaxf(matSam.pdf, 1e-20f));
const float cosNext = fabs(dot(matSam.direction, surfElem.normal));

Expand Down
2 changes: 1 addition & 1 deletion hydra_drv/CPUExp_bxdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ std::tuple<MatSample, int, float3> IntegratorShadowPTSSS::sampleAndEvalGGXBxDF(f
return std::make_tuple(res, 0, float3(1, 1, 1));
}
else
return IntegratorCommon::sampleAndEvalBxDF(ray_dir, surfElem, flags, shadow);
return IntegratorCommon::sampleAndEvalBxDF(ray_dir, surfElem, flags, false, shadow);
}


Expand Down
22 changes: 15 additions & 7 deletions hydra_drv/cmaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ typedef struct RefractResultT

float3 ray_dir;
bool success;
float eta;

}RefractResult;

Expand Down Expand Up @@ -522,11 +523,15 @@ static inline RefractResult myrefract(float3 ray_dir, float3 a_normal, float a_m
RefractResult res;
res.ray_dir = ray_dir;
res.success = refrSuccess;
if (refrSuccess)
res.eta = eta;
else
res.eta = 1.0f;
return res;
}

static inline void GlassSampleAndEvalBRDF(__global const PlainMaterial* a_pMat, const float3 rands, const float3 ray_dir, float3 a_normal, const float2 a_texCoord, const bool a_hitFromInside,
__global const EngineGlobals* a_globals, texture2d_t a_tex, __private const ProcTextureList* a_ptList,
static inline void GlassSampleAndEvalBRDF(__global const PlainMaterial* a_pMat, const float3 rands, const float3 ray_dir, float3 a_normal, const float2 a_texCoord, const bool a_hitFromInside,
__global const EngineGlobals* a_globals, texture2d_t a_tex, __private const ProcTextureList* a_ptList, const bool a_isFwdDir,
__private MatSample* a_out)
{
const float3 normal2 = a_hitFromInside ? (-1.0f)*a_normal : a_normal;
Expand Down Expand Up @@ -555,10 +560,13 @@ static inline void GlassSampleAndEvalBRDF(__global const PlainMaterial* a_pMat,

const float cosThetaOut = dot(refractData.ray_dir, a_normal);
const float cosMult = 1.0f / fmax(fabs(cosThetaOut), 1e-6f);
// only camera paths are multiplied by this factor, and etas
// are swapped because radiance flows in the opposite direction
const float adjointBsdfMult = a_isFwdDir ? 1.0f : refractData.eta*refractData.eta; // see SmallVCM and or Veach adjoint bsdf

a_out->direction = refractData.ray_dir;
a_out->pdf = pdf;
a_out->color = refractData.success ? fVal*clamp(glassGetColor(a_pMat)*texColor, 0.0f, 1.0f)*cosMult : make_float3(1.0f, 1.0f, 1.0f)*cosMult;
a_out->color = refractData.success ? fVal*clamp(glassGetColor(a_pMat)*texColor, 0.0f, 1.0f)*cosMult*adjointBsdfMult : make_float3(1.0f, 1.0f, 1.0f)*cosMult;

if(spec)
a_out->flags = (RAY_EVENT_S | RAY_EVENT_T);
Expand Down Expand Up @@ -1304,7 +1312,7 @@ static inline float3 BumpMapping(const float3 tangent, const float3 bitangent, c
return normalize(mul3x3x3(inverse(tangentTransform), normalTS));
}

static inline void MaterialLeafSampleAndEvalBRDF(__global const PlainMaterial* pMat, __private const SurfaceHit* pSurfHit, const float3 ray_dir, const float3 rands, const float3 a_shadow,
static inline void MaterialLeafSampleAndEvalBRDF(__global const PlainMaterial* pMat, __private const SurfaceHit* pSurfHit, const float3 ray_dir, const float3 rands, const float3 a_shadow, const bool a_isFwdDir,
__global const EngineGlobals* a_globals, texture2d_t a_tex, texture2d_t a_texNormal, __private const ProcTextureList* a_ptList,
__private MatSample* a_out)
{
Expand Down Expand Up @@ -1348,7 +1356,7 @@ static inline void MaterialLeafSampleAndEvalBRDF(__global const PlainMaterial* p
a_out);
break;
case PLAIN_MAT_CLASS_GLASS:
GlassSampleAndEvalBRDF(pMat, rands, ray_dir, hitNorm, pSurfHit->texCoord, pSurfHit->hfi, a_globals, a_tex, a_ptList,
GlassSampleAndEvalBRDF(pMat, rands, ray_dir, hitNorm, pSurfHit->texCoord, pSurfHit->hfi, a_globals, a_tex, a_ptList, a_isFwdDir,
a_out);
break;

Expand Down Expand Up @@ -1399,7 +1407,7 @@ Sample and eval layered material (with blends). Store result in a_out.
*/
static inline void MaterialSampleAndEvalBxDF(__global const PlainMaterial* pMat, __private float a_rands[MMLT_FLOATS_PER_BOUNCE],
__private const SurfaceHit* pSurfHit, const float3 a_rayDir, const float3 a_shadow, const uint rayFlags,
__private const SurfaceHit* pSurfHit, const float3 a_rayDir, const float3 a_shadow, const uint rayFlags, const bool a_isFwdDir,
__global const EngineGlobals* a_globals, texture2d_t a_tex, texture2d_t a_texNormal, __private const ProcTextureList* a_ptList,
__private MatSample* a_out, __private int* pLocalOffset)
{
Expand All @@ -1413,7 +1421,7 @@ static inline void MaterialSampleAndEvalBxDF(__global const PlainMaterial* pMat,
__global const PlainMaterial* pMatLeaf = pMat + mixSelector.localOffs;
(*pLocalOffset) = mixSelector.localOffs;

MaterialLeafSampleAndEvalBRDF(pMatLeaf, pSurfHit, a_rayDir, make_float3(a_rands[0], a_rands[1], a_rands[2]), a_shadow,
MaterialLeafSampleAndEvalBRDF(pMatLeaf, pSurfHit, a_rayDir, make_float3(a_rands[0], a_rands[1], a_rands[2]), a_shadow, a_isFwdDir,
a_globals, a_tex, a_texNormal, a_ptList,
a_out);

Expand Down
2 changes: 1 addition & 1 deletion hydra_drv/shaders/material.cl
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ __kernel void NextBounce(__global float4* restrict a_rpos,
const float3 shadowVal = decompressShadow(in_shadow[tid]);

MatSample brdfSample; int localOffset = 0;
MaterialSampleAndEvalBxDF(pHitMaterial, allRands, &surfHit, ray_dir, shadowVal, flags,
MaterialSampleAndEvalBxDF(pHitMaterial, allRands, &surfHit, ray_dir, shadowVal, flags, ((a_globals->g_flags & HRT_FORWARD_TRACING) != 0),
a_globals, in_texStorage1, in_texStorage2, &ptl,
&brdfSample, &localOffset);

Expand Down
4 changes: 2 additions & 2 deletions hydra_drv/shaders/mlt.cl
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ __kernel void MMLTCameraPathBounce(__global float4* restrict a_rpos,
int matOffset = materialOffset(a_globals, surfElem.matId);

MatSample matSam; int localOffset = 0;
MaterialSampleAndEvalBxDF(pHitMaterial, allRands, &surfElem, ray_dir, make_float3(1,1,1), flags,
MaterialSampleAndEvalBxDF(pHitMaterial, allRands, &surfElem, ray_dir, make_float3(1,1,1), flags, false,
a_globals, in_texStorage1, in_texStorage2, &ptl,
&matSam, &localOffset);

Expand Down Expand Up @@ -1101,7 +1101,7 @@ __kernel void MMLTLightPathBounce (__global float4* restrict a_rpos,
int matOffset = materialOffset(a_globals, surfElem.matId);

MatSample matSam; int localOffset = 0;
MaterialSampleAndEvalBxDF(pHitMaterial, allRands, &surfElem, ray_dir, make_float3(1,1,1), flags,
MaterialSampleAndEvalBxDF(pHitMaterial, allRands, &surfElem, ray_dir, make_float3(1,1,1), flags, true,
a_globals, in_texStorage1, in_texStorage2, &ptl,
&matSam, &localOffset);

Expand Down

0 comments on commit 3307128

Please sign in to comment.