Skip to content

Instantly share code, notes, and snippets.

@benanil
Last active February 3, 2024 20:40
Show Gist options
  • Save benanil/2eef97b1315b383d7cf1e44056b71df1 to your computer and use it in GitHub Desktop.
Save benanil/2eef97b1315b383d7cf1e44056b71df1 to your computer and use it in GitHub Desktop.
Script, that generates random noise for SSAO algorithms
// g++ -std=c++14 -O3 -mavx2 -march=native -msse4.2 -fno-rtti -fno-stack-protector -fno-exceptions -static-libstdc++ -static-libgcc RandomSSAOKernelGen.cpp -o RandomSSAOKernelGen.exe
#include <stdio.h>
// https://github.com/benanil/ASTL/blob/master/Random.hpp
#include "../ASTL/Random.hpp"
// https://github.com/benanil/ASTL/blob/master/Math/Vector.hpp
#include "../ASTL/Math/Vector.hpp"
Vector3f GenKernelVec(uint64_t xoro[2], float i, float kernelSize)
{
Vector3f vec;
vec.x = Random::NextFloat01(Random::Xoroshiro128Plus(xoro)) * 2.0f - 1.0f;
vec.y = Random::NextFloat01(Random::Xoroshiro128Plus(xoro)) * 2.0f - 1.0f;
vec.z = Random::NextFloat01(Random::Xoroshiro128Plus(xoro));
vec.NormalizeSelf();
float scale = i / kernelSize;
scale = Lerp(0.1f, 1.0f, scale * scale);
vec *= scale;
return vec;
}
void CreateSSAOKernel(float kernelSize)
{
FILE* file = fopen("RandomShaderVectors.txt", "wb");
fprintf(file, "const float KernelSizef = %f;\n", kernelSize);
fprintf(file, "const int KernelSize = %i;\n", (int)kernelSize);
fprintf(file, "mediump vec3 SSAOKernel[%i] = vec3[%i](\n", (int)kernelSize, (int)kernelSize);
uint64_t xoro[2];
Random::Xoroshiro128PlusInit(xoro);
for (int i = 0; i < 32; i++) Random::Xoroshiro128Plus(xoro); // < heat the random num generator
for (float i = 0.0; i < kernelSize - 1.0; i += 1.0)
{
Vector3f vec = GenKernelVec(xoro, i, kernelSize);
fprintf(file, " vec3(%f, %f, %f),\n", vec.x, vec.y, vec.z);
}
Vector3f vec = GenKernelVec(xoro, kernelSize-1.0, kernelSize);
fprintf(file, " vec3(%f, %f, %f));\n\n", vec.x, vec.y, vec.z);
fprintf(file, "mediump vec2 Noise[32] = vec2[32](\n");
for (int i = 0; i < 31; i++)
{
fprintf(file, " vec2(%f, %f),\n",
Random::NextFloat01(Random::Xoroshiro128Plus(xoro)) * 2.0 - 1.0,
Random::NextFloat01(Random::Xoroshiro128Plus(xoro)) * 2.0 - 1.0);
}
fprintf(file, " vec2(%f, %f));", Random::NextFloat01(Random::Xoroshiro128Plus(xoro)) * 2.0 - 1.0,
Random::NextFloat01(Random::Xoroshiro128Plus(xoro)) * 2.0 - 1.0);
fclose(file);
}
int main(int argc, char** argv)
{
float kernelSize = 42.0;
if (argc > 1)
{
const char* buff = argv[1];
kernelSize = ParseFloat(buff);
if (kernelSize < 8.0) kernelSize = 42.0f;
}
CreateSSAOKernel(kernelSize);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment