Skip to content

Commit 104a0ff

Browse files
committed
Code improvements and fixes
1 parent aca86df commit 104a0ff

5 files changed

Lines changed: 117 additions & 93 deletions

File tree

include/math/brdf.hpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626

2727
#pragma once
28-
#include "math/vector.hpp"
28+
#include "math/matrix.hpp"
2929

3030
namespace math::brdf
3131
{
@@ -119,40 +119,33 @@ static constexpr float2 hammersley(uint32 index, float invSampleCount) noexcept
119119
*/
120120
static f32x4 importanceSamplingNdfDggx(float2 u, float linearRoughness) noexcept
121121
{
122+
// TODO: use faster alg +7.5%: https://arxiv.org/pdf/2306.05044
122123
auto a2 = linearRoughness * linearRoughness;
123124
auto phi = u.x * float(M_PI * 2.0);
124125
auto cosTheta2 = (1.0f - u.y) / std::fma(a2 - 1.0f, u.y, 1.0f);
125126
auto cosTheta = std::sqrt(cosTheta2);
126127
auto sinTheta = std::sqrt(1.0f - cosTheta2);
127128
return f32x4(std::cos(phi) * sinTheta, std::sin(phi) * sinTheta, cosTheta);
128129
}
129-
// TODO: use faster alg +7.5%: https://arxiv.org/pdf/2306.05044
130130

131131
/**
132132
* @brief Computes diffuse irradiance from spherical harmonics (SH) using a 3rd-order.
133133
*
134134
* @details
135-
* This function evaluates irradiance (light arriving at a surface) from an environment map,
136-
* encoded using spherical harmonics (SH), for a given surface normal.
135+
* This function evaluates diffuse irradiance (light arriving at a surface) from an
136+
* environment map, encoded using spherical harmonics (SH), for a given surface normal.
137137
*
138138
* @param normal target sample normal vector
139-
* @param[in] shBuffer IBL spherical harmonics buffer
139+
* @param[in] shDiffuse diffuse irradiance SH matrices
140140
*/
141-
static f32x4 diffuseIrradiance(f32x4 normal, const f32x4* shBuffer) noexcept
141+
static f32x4 diffuseIrradiance(f32x4 normal, const f32x4x4* shDiffuse) noexcept
142142
{
143143
auto qb = normal.swizzle<SwY, SwY, SwZ, SwZ>() * normal.swizzle<SwX, SwZ, SwZ, SwX>();
144-
qb.setZ(std::fma(qb.getZ(), 3.0f, -1.0f));
145-
auto ft = normal.getX() * normal.getX() - normal.getY() * normal.getY();
146-
147-
auto irradiance = shBuffer[0];
148-
irradiance = fma(shBuffer[1], f32x4(normal.getY()), irradiance);
149-
irradiance = fma(shBuffer[2], f32x4(normal.getZ()), irradiance);
150-
irradiance = fma(shBuffer[3], f32x4(normal.getX()), irradiance);
151-
irradiance = fma(shBuffer[4], f32x4(qb.getX()), irradiance);
152-
irradiance = fma(shBuffer[5], f32x4(qb.getY()), irradiance);
153-
irradiance = fma(shBuffer[6], f32x4(qb.getZ()), irradiance);
154-
irradiance = fma(shBuffer[7], f32x4(qb.getW()), irradiance);
155-
irradiance = fma(shBuffer[8], f32x4(ft), irradiance);
144+
auto b1 = f32x4(1.0f, normal.getY(), normal.getZ());
145+
auto b2 = f32x4(normal.getX(), qb.getX(), qb.getY());
146+
auto b3 = f32x4(std::fma(qb.getZ(), 3.0f, -1.0f), qb.getW(),
147+
normal.getX() * normal.getX() - normal.getY() * normal.getY());
148+
auto irradiance = shDiffuse[0] * b1 + shDiffuse[1] * b2 + shDiffuse[2] * b3;
156149
return max(irradiance, f32x4::zero);
157150
}
158151

include/math/color.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ static bool isBinaryLess(Color a, Color b) noexcept { return *((const uint32*)&
309309
* @param b blue channel color value
310310
* @param a alpha channel color value (transparency)
311311
*/
312-
static constexpr Color rgba(uint8 r, uint8 g, uint8 b, float a) noexcept { return Color(r, g, b, a); }
312+
static constexpr Color rgba(uint8 r, uint8 g, uint8 b, float a) noexcept { return Color(Color(r, g, b), a); }
313313

314314
// TODO: color conversion functions.
315315

include/math/ibl.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
namespace math::ibl
3333
{
3434

35-
constexpr int32 shBandCount = 3; /**< Spherical harmonics band count. */
36-
constexpr int32 shCoeffCount = 9; /**< Spherical harmonics coefficient count. (3 * 3) */
37-
3835
/**
3936
* @brief Converts texture coordinates to the clip space.
4037
*

include/math/normal-mapping.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ static f32x4x4 computeTBN(f32x4x4 model, f32x4 normal, f32x4 tangent) noexcept
4444
return f32x4x4(t, b, n);
4545
}
4646

47-
static float3x3 approximateTBN(float3 normal) noexcept // Branchless ONB (Duff et al. Orthonormal Basis)
47+
static f32x4x4 approximateTBN(f32x4 normal) noexcept
48+
{
49+
auto up = std::abs(normal.getZ()) < 0.999f ?
50+
f32x4(0.0f, 0.0f, 1.0f) : f32x4(1.0f, 0.0f, 0.0f);
51+
auto tangent = normalize3(cross3(up, normal));
52+
auto bitangent = cross3(normal, tangent);
53+
return f32x4x4(tangent, bitangent, normal);
54+
}
55+
static float3x3 fastApproximateTBN(float3 normal) noexcept // Branchless ONB (Duff et al. Orthonormal Basis)
4856
{
4957
assert(normal == normalize(normal));
5058
float signZ = normal.z >= 0.0f ? 1.0f : -1.0f;

0 commit comments

Comments
 (0)