Skip to content

Commit

Permalink
Traktor: Some cleanup in SH code.
Browse files Browse the repository at this point in the history
  • Loading branch information
apistol78 committed Jan 23, 2025
1 parent c896243 commit e282dd9
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 117 deletions.
22 changes: 11 additions & 11 deletions code/Render/SH/SH.inl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2025 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -12,7 +12,7 @@ double P(int l, int m, double x)
double pmm = 1.0;
if (m > 0)
{
double somx2 = sqrtf((1.0 - x) * (1.0 + x));
double somx2 = std::sqrt((1.0 - x) * (1.0 + x));
double fact = 1.0;
for (int i = 1; i <= m; ++i)
{
Expand Down Expand Up @@ -46,26 +46,26 @@ int factorial(int n)

double K(int l, int m)
{
double temp = ((2.0 * l + 1.0) * factorial(l - m)) / (4.0 * PI * factorial(l + m));
return sqrtf(temp);
const double temp = ((2.0 * l + 1.0) * factorial(l - m)) / (4.0 * PI * factorial(l + m));
return std::sqrt(temp);
}

double SH(int l, int m, double phi, double theta)
{
const double sqrt2 = sqrtf(2.0);
const double sqrt2 = std::sqrt(2.0);
if (m == 0)
return K(l, 0) * P(l, m, cosf(phi));
return K(l, 0) * P(l, m, std::cos(phi));
else if (m > 0)
return sqrt2 * K(l, m) * cosf(m * theta) * P(l, m, cosf(phi));
return sqrt2 * K(l, m) * std::cos(m * theta) * P(l, m, std::cos(phi));
else
return sqrt2 * K(l, -m) * sinf(-m * theta) * P(l, -m, cosf(phi));
return sqrt2 * K(l, -m) * std::sin(-m * theta) * P(l, -m, std::cos(phi));
}

void shEvaluate3(const float fX, const float fY, const float fZ, float* __restrict pSH)
{
float fTmpA = -0.48860251190292f;
float fTmpB = -1.092548430592079f * fY;
float fTmpC = 0.5462742152960395f;
const float fTmpA = -0.48860251190292f;
const float fTmpB = -1.092548430592079f * fY;
const float fTmpC = 0.5462742152960395f;

pSH[0] = 0.2820947917738781f;
pSH[2] = 0.4886025119029199f * fY;
Expand Down
76 changes: 37 additions & 39 deletions code/Render/SH/SHEngine.cpp
Original file line number Diff line number Diff line change
@@ -1,77 +1,67 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2025 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <cmath>
#include "Render/SH/SHEngine.h"

#include "Core/Math/Const.h"
#include "Core/Math/Float.h"
#include "Core/Math/MathUtils.h"
#include "Core/Math/Quasirandom.h"
#include "Core/Math/RandomGeometry.h"
#include "Core/Thread/JobManager.h"
#include "Render/SH/SHEngine.h"
#include "Render/SH/SHFunction.h"
#include "Render/SH/SHMatrix.h"

#include <cmath>

namespace traktor::render
{
namespace
{
namespace
{

#include "Render/SH/SH.inl"

}
}

T_IMPLEMENT_RTTI_CLASS(L"traktor.render.SHEngine", SHEngine, Object)

SHEngine::SHEngine(uint32_t bandCount)
: m_bandCount(bandCount)
, m_coefficientCount(bandCount * bandCount)
: m_bandCount(bandCount)
, m_coefficientCount(bandCount * bandCount)
{
}

void SHEngine::generateSamplePoints(uint32_t count)
{
RandomGeometry rg;

const uint32_t sqrtCount = (uint32_t)std::sqrt((double)count);
count = sqrtCount * sqrtCount;

m_samplePoints.resize(count);
for (uint32_t i = 0; i < sqrtCount; ++i)
for (uint32_t i = 0; i < count; ++i)
{
for (uint32_t j = 0; j < sqrtCount; ++j)
{
const uint32_t o = i + j * sqrtCount;

const Vector2 uv = Quasirandom::hammersley(o, count);
const Vector2 uv = Quasirandom::hammersley(i, count);
const Vector4 direction = Quasirandom::uniformSphere(uv);

const float phi = 2.0f * std::acos(std::sqrt(1.0f - uv.x));
const float theta = 2.0f * PI * uv.y;
m_samplePoints[i].direction = Polar::fromUnitCartesian(direction);
m_samplePoints[i].coefficients.resize(m_coefficientCount);

m_samplePoints[o].direction = Polar(phi, theta);
m_samplePoints[o].coefficients.resize(m_coefficientCount);

for (int32_t l = 0; l < (int32_t)m_bandCount; ++l)
for (int32_t l = 0; l < (int32_t)m_bandCount; ++l)
{
for (int32_t m = -l; m <= l; ++m)
{
for (int32_t m = -l; m <= l; ++m)
{
const int32_t index = l * (l + 1) + m;
const float shc = (float)SH(l, m, phi, theta);
m_samplePoints[o].coefficients[index] = Vector4(shc, shc, shc, 0.0f);
}
const int32_t index = l * (l + 1) + m;
const float shc = (float)SH(l, m, m_samplePoints[i].direction.phi, m_samplePoints[i].direction.theta);
m_samplePoints[i].coefficients[index] = Vector4(shc, shc, shc, 0.0f);
}
}
}
}

void SHEngine::generateCoefficients(SHFunction* function, bool parallell, SHCoeffs& outResult)
void SHEngine::generateCoefficients(const SHFunction* function, bool parallell, SHCoeffs& outResult) const
{
const float weight = 4.0 * PI;
const float weight = 4.0f * PI;
const uint32_t nsp = (uint32_t)m_samplePoints.size();

if (parallell)
Expand All @@ -85,10 +75,18 @@ void SHEngine::generateCoefficients(SHFunction* function, bool parallell, SHCoef
intermediate[3].resize(m_bandCount);

Job::task_t jobs[4];
jobs[0] = [&](){ generateCoefficientsJob(function, 0 * sc, 1 * sc, &intermediate[0]); };
jobs[1] = [&](){ generateCoefficientsJob(function, 1 * sc, 2 * sc, &intermediate[1]); };
jobs[2] = [&](){ generateCoefficientsJob(function, 2 * sc, 3 * sc, &intermediate[2]); };
jobs[3] = [&](){ generateCoefficientsJob(function, 3 * sc, nsp, &intermediate[3]); };
jobs[0] = [&]() {
generateCoefficientsJob(function, 0 * sc, 1 * sc, &intermediate[0]);
};
jobs[1] = [&]() {
generateCoefficientsJob(function, 1 * sc, 2 * sc, &intermediate[1]);
};
jobs[2] = [&]() {
generateCoefficientsJob(function, 2 * sc, 3 * sc, &intermediate[2]);
};
jobs[3] = [&]() {
generateCoefficientsJob(function, 3 * sc, nsp, &intermediate[3]);
};
JobManager::getInstance().fork(jobs, sizeof_array(jobs));

outResult.resize(m_bandCount);
Expand All @@ -106,7 +104,7 @@ void SHEngine::generateCoefficients(SHFunction* function, bool parallell, SHCoef
outResult[i] *= factor;
}

// SHMatrix SHEngine::generateTransferMatrix(SHFunction* function) const
// SHMatrix SHEngine::generateTransferMatrix(const SHFunction* function) const
// {
// const double weight = 4.0 * PI;

Expand All @@ -127,7 +125,7 @@ void SHEngine::generateCoefficients(SHFunction* function, bool parallell, SHCoef
// return out;
// }

void SHEngine::generateCoefficientsJob(SHFunction* function, uint32_t start, uint32_t end, SHCoeffs* outResult)
void SHEngine::generateCoefficientsJob(const SHFunction* function, uint32_t start, uint32_t end, SHCoeffs* outResult) const
{
for (uint32_t i = start; i < end; ++i)
{
Expand Down
12 changes: 6 additions & 6 deletions code/Render/SH/SHEngine.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*
* TRAKTOR
* Copyright (c) 2022-2024 Anders Pistol.
* Copyright (c) 2022-2025 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once

#include "Core/Object.h"
#include "Core/Math/Vector4.h"
#include "Core/Containers/AlignedVector.h"
#include "Core/Math/Vector4.h"
#include "Core/Object.h"
#include "Render/SH/SHCoeffs.h"

// import/export mechanism.
Expand Down Expand Up @@ -45,16 +45,16 @@ class T_DLLCLASS SHEngine : public Object

void generateSamplePoints(uint32_t count);

void generateCoefficients(SHFunction* function, bool parallell, SHCoeffs& outResult);
void generateCoefficients(const SHFunction* function, bool parallell, SHCoeffs& outResult) const;

// SHMatrix generateTransferMatrix(SHFunction* function) const;
// SHMatrix generateTransferMatrix(const SHFunction* function) const;

private:
uint32_t m_bandCount;
uint32_t m_coefficientCount;
AlignedVector< Sample > m_samplePoints;

void generateCoefficientsJob(SHFunction* function, uint32_t start, uint32_t end, SHCoeffs* outResult);
void generateCoefficientsJob(const SHFunction* function, uint32_t start, uint32_t end, SHCoeffs* outResult) const;
};

}
Loading

0 comments on commit e282dd9

Please sign in to comment.