Skip to content

Commit 040b3ed

Browse files
author
Carmelo Fernandez Aguera
committedMay 19, 2021
WIP: Reviving the probe tool to analyze multiple scattering and energy conservation.
1 parent 1ef6325 commit 040b3ed

File tree

11 files changed

+284
-58
lines changed

11 files changed

+284
-58
lines changed
 

‎CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
################################################################################
22
# Rev project
3-
# Sample project that creates a simple scene graph
43
################################################################################
54
cmake_minimum_required (VERSION 3.10)
65
project(revAll)
@@ -33,6 +32,7 @@ add_subdirectory(engine)
3332
if(REV_BUILD_TOOLS)
3433
add_subdirectory(tools/gltfOptimizer)
3534
set_target_properties(gltfOptimizer PROPERTIES FOLDER "tools")
35+
add_subdirectory(tools/probe)
3636
endif()
3737

3838
# Compile sample projects

‎bin/ibl.png

3.12 KB
Loading

‎engine/src/math/linear.h

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ namespace rev::math
3737
return a<b?a:b; // Same NaN behavior as simd
3838
}
3939

40+
inline float clamp(float t, float a, float b)
41+
{
42+
return max(a, min(b, t));
43+
}
44+
4045
template<class T> struct minOp
4146
{
4247
T operator()(T a, T b) const { return (a < b) ? a : b; } // Same NaN behavior as simd

‎engine/src/math/noise.h

+106-37
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,111 @@
1111
#include "numericTraits.h"
1212
#include <random>
1313

14-
namespace rev {
15-
namespace math {
16-
class SNoise
14+
namespace rev::math {
15+
class SNoise
16+
{
17+
public:
18+
// Simplex noise
19+
//static void setSimplexSeed(int _seed);
20+
static float simplex(float _x, float _y); // 2D simplex noise
21+
private:
22+
static int fastFloor(double _x);
23+
24+
static Vec2f grad2[12];
25+
static unsigned char p[256];
26+
};
27+
28+
class RandomGenerator
29+
{
30+
public:
31+
float scalar()
1732
{
18-
public:
19-
// Simplex noise
20-
//static void setSimplexSeed(int _seed);
21-
static float simplex(float _x, float _y); // 2D simplex noise
22-
private:
23-
static int fastFloor(double _x);
24-
25-
static Vec2f grad2[12];
26-
static unsigned char p[256];
27-
};
28-
29-
class RandomGenerator
33+
return distrib(engine);
34+
}
35+
36+
math::Vec3f unit_vector()
3037
{
31-
public:
32-
float scalar()
33-
{
34-
return distrib(engine);
35-
}
36-
37-
math::Vec3f unit_vector()
38-
{
39-
auto theta = math::TwoPi*scalar();
40-
auto cosPhi = 2*scalar()-1;
41-
auto sinPhi = sqrt(1-cosPhi*cosPhi);
42-
return math::Vec3f(
43-
cos(theta)*sinPhi,
44-
sin(theta)*sinPhi,
45-
cosPhi);
46-
}
47-
private:
48-
std::default_random_engine engine;
49-
std::uniform_real_distribution<float> distrib;
50-
};
51-
}
52-
} // namespace rev
38+
auto theta = math::TwoPi*scalar();
39+
auto cosPhi = 2*scalar()-1;
40+
auto sinPhi = sqrt(1-cosPhi*cosPhi);
41+
return math::Vec3f(
42+
cos(theta)*sinPhi,
43+
sin(theta)*sinPhi,
44+
cosPhi);
45+
}
46+
private:
47+
std::default_random_engine engine;
48+
std::uniform_real_distribution<float> distrib;
49+
};
50+
51+
inline float RadicalInverse_VdC(uint32_t bits)
52+
{
53+
bits = (bits << 16u) | (bits >> 16u);
54+
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
55+
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
56+
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
57+
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
58+
return float(bits) / 0x100000000;
59+
}
60+
61+
//------------------------------------------------------------------------------
62+
inline Vec2f Hammersley(uint32_t i, uint32_t N)
63+
{
64+
return Vec2f(float(i) / N, RadicalInverse_VdC(i));
65+
}
66+
67+
inline Vec3f ImportanceSampleGGX(Vec2f Xi, float Roughness)
68+
{
69+
float a = Roughness * Roughness;
70+
float Phi = 2 * Pi * Xi.x();
71+
float CosTheta = sqrt((1 - Xi.y()) / (1 + (a * a - 1) * Xi.y()));
72+
float SinTheta = sqrt(1 - CosTheta * CosTheta);
73+
Vec3f H;
74+
H.x() = SinTheta * cos(Phi);
75+
H.y() = SinTheta * sin(Phi);
76+
H.z() = CosTheta;
77+
78+
return H;
79+
}
80+
81+
inline Vec3f ImportanceSampleGGX_r1(Vec2f Xi)
82+
{
83+
float Phi = 2 * Pi * Xi.x();
84+
float CosTheta = sqrt(1 - Xi.y());
85+
float SinTheta = sqrt(1 - CosTheta * CosTheta);
86+
Vec3f H;
87+
H.x() = SinTheta * cos(Phi);
88+
H.y() = SinTheta * sin(Phi);
89+
H.z() = CosTheta;
90+
91+
return H;
92+
}
93+
94+
inline float SmithGGXCorrelatedG2(float ndv, float ndl, float alpha)
95+
{
96+
float a2 = alpha * alpha;
97+
float GV = ndv * sqrt(a2 + (1 - a2) * ndl * ndl);
98+
float GL = ndl * sqrt(a2 + (1 - a2) * ndv * ndv);
99+
return 2 * ndv * ndl / (GL + GV);
100+
}
101+
102+
inline float SmithGGXCorrelatedG2_over_ndv(float ndv, float ndl, float alpha)
103+
{
104+
float a2 = alpha * alpha;
105+
float GV = ndv * sqrt(a2 + (1 - a2) * ndl * ndl);
106+
float GL = ndl * sqrt(a2 + (1 - a2) * ndv * ndv);
107+
return 2 * ndl / (GL + GV);
108+
}
109+
110+
111+
inline float SmithGGXCorrelatedG2_a1(float ndv, float ndl)
112+
{
113+
return 2 * ndv * ndl / (ndl + ndv);
114+
}
115+
116+
inline float SmithGGXCorrelatedG2_overNdv_ndv0(float alpha)
117+
{
118+
return 2 / alpha;
119+
}
120+
121+
} // namespace rev::math

‎samples/gltfViewer/shaders/gbuffer.frag

+16-6
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,25 @@ vec3 directLight(
5555
{
5656
float r = max(1e-4, roughness);
5757
// Single bounce specular lighting0
58-
vec3 specular = specularBRDF(specularColor, ndh, ndl, ndv, hdv, r);
58+
vec3 specular = specularBRDF(specularColor, ndh, ndl, ndv, hdv, r); // Single scattering
5959

6060
// Energy compensation - substract single bounce specular from the light that goes into diffuse
61-
vec2 reflectedLight = textureLod(iblLUT, vec2(ndl, roughness), 0).xy;
62-
vec3 totalFresnel = specularColor * reflectedLight.x + reflectedLight.y;
63-
64-
vec3 kD = diffuseColor * (1-totalFresnel);
61+
vec2 Fss_wi = textureLod(iblLUT, vec2(ndl, roughness), 0).xy;
62+
vec3 FssRss_wi = specularColor * Fss_wi.x + Fss_wi.y; // Single scattering
63+
float Eavg = 1-0.6*roughness;
64+
float Ess_wi = Fss_wi.x + Fss_wi.y;
65+
vec2 Fss_wo = textureLod(iblLUT, vec2(ndv, roughness), 0).xy;
66+
vec3 FssRss_wo = specularColor * Fss_wo.x + Fss_wo.y; // Single scattering
67+
vec3 Favg = (20+specularColor)/21;
68+
vec3 Fwi = FssRss_wi / Ess_wi;
69+
70+
vec3 num = (Fwi-FssRss_wi)*FssRss_wo;
71+
vec3 den = 1-Favg+Eavg*Favg;
72+
vec3 FmsRms = num/den;
73+
74+
vec3 kD = diffuseColor * (1-FssRss_wi);
6575
vec3 diffuse = kD / PI;
66-
return (specular + diffuse) * ndl * lightColor;
76+
return (specular + FmsRms + diffuse) * ndl * lightColor;
6777
}
6878

6979
vec3 envLight(

‎samples/gltfViewer/shaders/postPro.frag

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void main()
4848

4949
// Gather slight bloom
5050
vec4 hdrColor = vec4(0);
51-
int bloomWin = 5;
51+
int bloomWin = 0;
5252
ivec2 minPos = max(pixelPos-bloomWin, ivec2(0));
5353
ivec2 maxPos = min(pixelPos+bloomWin, ivec2(pushC.windowSize-1));
5454

‎scripts/brdf.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import numpy as np
2+
from math import *

‎tools/probe/CMakeLists.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
################################################################################
2+
# Rev project
3+
# Light probe processing tool
4+
# And brdf study
5+
################################################################################
6+
7+
add_executable(probeTool
8+
main.cpp
9+
Heitz/MicrosurfaceScattering.h
10+
Heitz/MicrosurfaceScattering.cpp)
11+
target_include_directories(probeTool PUBLIC ../../engine/src)
12+
target_include_directories(probeTool PUBLIC ../../include)
13+
target_link_libraries(probeTool LINK_PUBLIC revCore revGfx revMath)
14+
set_target_properties(probeTool PROPERTIES FOLDER "tools")
46.8 KB
Binary file not shown.
17.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.