Skip to content

Commit

Permalink
Added initial support for isosurfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismile committed Nov 29, 2023
1 parent 0faf7f7 commit b7bb585
Show file tree
Hide file tree
Showing 17 changed files with 662 additions and 145 deletions.
6 changes: 6 additions & 0 deletions Data/Shaders/VPT/Clouds.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#version 450

//#extension GL_EXT_debug_printf : enable

#include "VptHeader.glsl"

#ifdef USE_NANOVDB
Expand Down Expand Up @@ -59,6 +61,10 @@ void pathTraceSample(int i, bool onlyFirstEvent, out ScatterEvent firstEvent){
vec3 x, w;
createCameraRay(screenCoord, x, w);

#ifdef USE_NANOVDB
accessor = createAccessor();
#endif

// Perform a single path and get radiance
#ifdef COMPUTE_SCATTER_RAY_ABSORPTION_MOMENTS
float scatterRayAbsorptionMoments[NUM_SCATTER_RAY_ABSORPTION_MOMENTS + 1];
Expand Down
5 changes: 0 additions & 5 deletions Data/Shaders/VPT/DecompositionTracking.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
vec3 analogDecompositionTracking(vec3 x, vec3 w, out ScatterEvent firstEvent) {
firstEvent = ScatterEvent(false, x, 0.0, w, 0.0, 0.0, 0.0);

#ifdef USE_NANOVDB
pnanovdb_readaccessor_t accessor = createAccessor();
#endif

const vec3 EPSILON_VEC = vec3(1e-6);
float tMinVal, tMaxVal;
if (rayBoxIntersect(parameters.boxMin + EPSILON_VEC, parameters.boxMax - EPSILON_VEC, x, w, tMinVal, tMaxVal)) {
Expand Down Expand Up @@ -136,7 +132,6 @@ vec3 analogDecompositionTracking(vec3 x, vec3 w, out ScatterEvent firstEvent) {
vec3 analogDecompositionTracking(vec3 x, vec3 w, out ScatterEvent firstEvent) {
firstEvent = ScatterEvent(false, x, 0.0, w, 0.0, 0.0, 0.0);

pnanovdb_readaccessor_t accessor = createAccessor();
pnanovdb_buf_t buf = pnanovdb_buf_t(0);
pnanovdb_grid_handle_t gridHandle = pnanovdb_grid_handle_t(pnanovdb_address_null());

Expand Down
151 changes: 123 additions & 28 deletions Data/Shaders/VPT/DeltaTracking.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
* ACM Trans. Graph., 36(4), Jul. 2017.
*/
vec3 deltaTrackingSpectral(vec3 x, vec3 w, out ScatterEvent firstEvent) {
#ifdef USE_NANOVDB
pnanovdb_readaccessor_t accessor = createAccessor();
#endif

firstEvent = ScatterEvent(false, x, 0.0, w, 0.0, 0.0, 0.0);

float majorant = maxComponent(parameters.extinction);

vec3 weights = vec3(1, 1, 1);
#ifdef USE_ISOSURFACES
float lastScalarSign, currentScalarSign;
bool isFirstPoint = true;
#endif

vec3 absorptionAlbedo = vec3(1, 1, 1) - parameters.scatteringAlbedo;
vec3 scatteringAlbedo = parameters.scatteringAlbedo;
Expand All @@ -57,23 +57,45 @@ vec3 deltaTrackingSpectral(vec3 x, vec3 w, out ScatterEvent firstEvent) {
break;
}

x += w * t;
vec3 xNew = x + w * t;

#ifdef USE_TRANSFER_FUNCTION
#ifdef USE_NANOVDB
vec4 densityEmission = sampleCloudDensityEmission(accessor, x);
#else
vec4 densityEmission = sampleCloudDensityEmission(x);
#endif
vec4 densityEmission = sampleCloudDensityEmission(xNew);
float density = densityEmission.a;
#else
#ifdef USE_NANOVDB
float density = sampleCloud(accessor, x);
float density = sampleCloud(xNew);
#endif

#ifdef USE_ISOSURFACES
#if defined(ISOSURFACE_TYPE_DENSITY) && !defined(USE_TRANSFER_FUNCTION)
float scalarValue = density;
#else
float density = sampleCloud(x);
float scalarValue = sampleCloudDirect(xNew);
#endif
currentScalarSign = sign(scalarValue - parameters.isoValue);
if (isFirstPoint) {
isFirstPoint = false;
lastScalarSign = currentScalarSign;
}

if (lastScalarSign != currentScalarSign) {
vec3 isosurfaceHitPoint = xNew;
refineIsoSurfaceHit(isosurfaceHitPoint, x, currentScalarSign);
vec3 color = getIsoSurfaceHit(isosurfaceHitPoint, w);
weights *= color;
x = isosurfaceHitPoint;
x += w * 1e-4;
isFirstPoint = true;
if (rayBoxIntersect(parameters.boxMin, parameters.boxMax, x, w, tMin, tMax)) {
x += w * tMin;
d = tMax - tMin;
}
continue;
}
#endif

x = xNew;

vec3 sigma_a = absorptionAlbedo * parameters.extinction * density;
vec3 sigma_s = scatteringAlbedo * parameters.extinction * density;
vec3 sigma_n = vec3(majorant) - parameters.extinction * density;
Expand Down Expand Up @@ -167,16 +189,19 @@ vec3 deltaTracking(

firstEvent = ScatterEvent(false, x, 0.0, w, 0.0, 0.0, 0.0);

#ifdef USE_NANOVDB
pnanovdb_readaccessor_t accessor = createAccessor();
#endif

float majorant = parameters.extinction.x;
float absorptionAlbedo = 1.0 - parameters.scatteringAlbedo.x;
float scatteringAlbedo = parameters.scatteringAlbedo.x;
float PA = absorptionAlbedo * parameters.extinction.x;
float PS = scatteringAlbedo * parameters.extinction.x;

#ifdef USE_ISOSURFACES
vec3 weights = vec3(1, 1, 1);
float lastScalarSign, currentScalarSign;
bool isFirstPoint = true;
#endif

int i = 0;
float tMin, tMax;
if (rayBoxIntersect(parameters.boxMin, parameters.boxMax, x, w, tMin, tMax)) {
x += w * tMin;
Expand All @@ -189,6 +214,11 @@ vec3 deltaTracking(
float transmittance = 1.0;

while (true) {
i++;
if (i == 1000) {
//debugPrintfEXT("HERE");
return vec3(0.0, 1.0, 0.0);
}
#ifdef COMPUTE_SCATTER_RAY_ABSORPTION_MOMENTS
float absorbance = -log(transmittance);
if (absorbance > ABSORBANCE_MAX_VALUE) {
Expand All @@ -213,31 +243,84 @@ vec3 deltaTracking(
#endif
transmittance = 1.0;
#endif
float t = -log(max(0.0000000001, 1 - random()))/majorant;
float t = -log(max(0.0000000001, 1 - random())) / majorant;

if (t > d) {
break;
}

x += w * t;
vec3 xNew = x + w * t;
#ifdef COMPUTE_SCATTER_RAY_ABSORPTION_MOMENTS
depth += t;
#endif

#ifdef USE_TRANSFER_FUNCTION
#ifdef USE_NANOVDB
vec4 densityEmission = sampleCloudDensityEmission(accessor, x);
#else
vec4 densityEmission = sampleCloudDensityEmission(x);
#endif
vec4 densityEmission = sampleCloudDensityEmission(xNew);
float density = densityEmission.a;
#else
#ifdef USE_NANOVDB
float density = sampleCloud(accessor, x);
#else
float density = sampleCloud(x);
float density = sampleCloud(xNew);
#endif

#ifdef USE_ISOSURFACES
//#if defined(ISOSURFACE_TYPE_DENSITY) && !defined(USE_TRANSFER_FUNCTION)
// float scalarValue = density;
//#else
// float scalarValue = sampleCloudDirect(xNew);
//#endif
const int isoSubdivs = 2;
bool foundHit = false;
for (int subdiv = 0; subdiv < isoSubdivs; subdiv++) {
vec3 x0 = mix(x, xNew, float(subdiv) / float(isoSubdivs));
vec3 x1 = mix(x, xNew, float(subdiv + 1) / float(isoSubdivs));
float scalarValue = sampleCloudDirect(x1);

currentScalarSign = sign(scalarValue - parameters.isoValue);
if (isFirstPoint) {
isFirstPoint = false;
lastScalarSign = currentScalarSign;
}

if (lastScalarSign != currentScalarSign) {
refineIsoSurfaceHit(x1, x0, currentScalarSign);
x = x1;
vec3 color = getIsoSurfaceHit(x, w);
weights *= color;
x += w * 1e-4;
isFirstPoint = true;
if (rayBoxIntersect(parameters.boxMin, parameters.boxMax, x, w, tMin, tMax)) {
x += w * tMin;
d = tMax - tMin;
}
foundHit = true;
break;
}
}
if (foundHit) {
continue;
}
/*currentScalarSign = sign(scalarValue - parameters.isoValue);
if (isFirstPoint) {
isFirstPoint = false;
lastScalarSign = currentScalarSign;
}
if (lastScalarSign != currentScalarSign) {
vec3 isosurfaceHitPoint = xNew;
refineIsoSurfaceHit(isosurfaceHitPoint, x, currentScalarSign);
vec3 color = getIsoSurfaceHit(isosurfaceHitPoint, w);
weights *= color;
x = isosurfaceHitPoint;
x += w * 1e-4;
isFirstPoint = true;
if (rayBoxIntersect(parameters.boxMin, parameters.boxMax, x, w, tMin, tMax)) {
x += w * tMin;
d = tMax - tMin;
}
continue;
}*/
#endif

x = xNew;
transmittance *= 1.0 - density;

float sigma_a = PA * density;
Expand Down Expand Up @@ -265,14 +348,22 @@ vec3 deltaTracking(
#endif
#ifdef USE_EMISSION
L_e += sampleEmission(x);
#ifdef USE_ISOSURFACES
return weights * emission;
#else
return emission;
#endif
#endif
#ifdef USE_TRANSFER_FUNCTION
L_e += parameters.emissionStrength * densityEmission.rgb;
//return weights * sigma_a / (majorant * Pa) * L_e;
#endif
#if defined(USE_EMISSION) || defined(USE_TRANSFER_FUNCTION)
#ifdef USE_ISOSURFACES
return weights * L_e;
#else
return L_e;
#endif
#else
return vec3(0); // weights * sigma_a / (majorant * Pa) * L_e; // 0 - No emission
#endif
Expand Down Expand Up @@ -309,6 +400,10 @@ vec3 deltaTracking(
}
}

#ifdef USE_ISOSURFACES
return weights * (sampleSkybox(w) + sampleLight(w));
#else
return sampleSkybox(w) + sampleLight(w);
#endif
}
#endif
Loading

0 comments on commit b7bb585

Please sign in to comment.