Skip to content

Instantly share code, notes, and snippets.

@jdupuy
jdupuy / BitReverse.c
Last active April 24, 2024 12:01
Reversing bits of a word
uint8_t BitReverse(uint8_t x)
{
x = ((x & 0x0F) << 4) | ((x & 0xF0) >> 4);
x = ((x & 0x33) << 2) | ((x & 0xCC) >> 2);
x = ((x & 0x55) << 1) | ((x & 0xAA) >> 1);
return x;
}
uint16_t BitReverse(uint16_t x)
@jdupuy
jdupuy / SampleVndf_GGX.cpp
Last active April 15, 2024 20:09
Sampling Visible GGX Normals with Spherical Caps
// Helper function: sample the visible hemisphere from a spherical cap
vec3 SampleVndf_Hemisphere(vec2 u, vec3 wi)
{
// sample a spherical cap in (-wi.z, 1]
float phi = 2.0f * M_PI * u.x;
float z = fma((1.0f - u.y), (1.0f + wi.z), -wi.z);
float sinTheta = sqrt(clamp(1.0f - z * z, 0.0f, 1.0f));
float x = sinTheta * cos(phi);
float y = sinTheta * sin(phi);
vec3 c = vec3(x, y, z);
@jdupuy
jdupuy / BitonicSort.c
Created June 1, 2022 09:28
CPU Bitonic Sort
// sorts an array in ascending order (arraySize muste be a power of two)
void BitonicSort(uint32_t *array, uint32_t arraySize)
{
for (uint32_t d2 = 1u; d2 < arraySize; d2*= 2u) {
for (uint32_t d1 = d2; d1 >= 1u; d1/= 2u) {
const uint32_t mask = (0xFFFFFFFEu * d1);
#pragma omp parallel for
for (uint32_t i = 0; i < (arraySize / 2); ++i) {
const uint32_t i1 = ((i << 1) & mask) | (i & ~(mask >> 1));
@jdupuy
jdupuy / Inverse.cpp
Last active March 12, 2020 10:01
C++ code to compute the inverse of a linearly-interpolated 1D table with values in [0, 1] x [0, 1] (note: the table size must be a power of two)
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
// inverting piecewise linear functions
float InverseLinear(float yStart, float yEnd, float y)
{
return (y - yStart) / (yEnd - yStart);
}