Skip to content

Instantly share code, notes, and snippets.

@JarkkoPFC
JarkkoPFC / sphere_screen_extents.h
Last active June 13, 2023 21:27
Calculates view space 3D sphere extents on the screen
struct vec3f {float x, y, z;};
struct vec4f {float x, y, z, w;};
struct mat44f {vec4f x, y, z, w;};
//============================================================================
// sphere_screen_extents
//============================================================================
// Calculates the exact screen extents xyzw=[left, bottom, right, top] in
// normalized screen coordinates [-1, 1] for a sphere in view space. For
// performance, the projection matrix (v2p) is assumed to be setup so that
@JarkkoPFC
JarkkoPFC / cone_vector.h
Last active February 4, 2020 06:44
Calculate 3D unit vectors in a cone for MC-integration
struct vec2f {float x, y;};
struct vec3f {float x, y, z;};
//============================================================================
// cone_uniform_vector
//============================================================================
// Returns uniformly distributed unit vector on a [0, 0, 1] oriented cone of
// given apex angle and uniform random vector xi ([x, y] in range [0, 1]).
// e.g. cos_half_apex_angle = 0 returns samples on a hemisphere (cos(pi/2)=0),
// while cos_half_apex_angle = -1 returns samples on a sphere (cos(pi)=-1)
@JarkkoPFC
JarkkoPFC / morton.h
Created September 30, 2021 06:19
Faster 16/32bit 2D Morton Code encode/decode functions
uint16_t encode16_morton2(uint8_t x_, uint8_t y_)
{
uint32_t res=x_|(uint32_t(y_)<<16);
res=(res|(res<<4))&0x0f0f0f0f;
res=(res|(res<<2))&0x33333333;
res=(res|(res<<1))&0x55555555;
return uint16_t(res|(res>>15));
}
//----
@JarkkoPFC
JarkkoPFC / bitonic_sort64.h
Last active October 17, 2022 03:46
Bitonic sort for 64 elements
template<typename T>
void bitonic_sort64(T vals_[64])
{
int h=-1;
do
{
for(unsigned i=0; i<32; ++i)
{
unsigned idx0=2*i;
unsigned idx1=4*(i&h)-2*(i+h)-1;
@JarkkoPFC
JarkkoPFC / ocl_include.inc
Last active February 16, 2024 23:26
#include OpenCL files as a C++ source or string
//============================================================================
// Copyright (c) 2024, Jarkko Lempiainen
// All rights reserved.
//============================================================================
// This #include helper file can be used to #include OpenCL source files
// either as regular C++ source files to be compiled with a C++11 compiler
// (as normally done with regular #include) OR embedded as a c-strings to C++
// programs.
//
// The included OpenCL files must be enclosed within OCL_SOURCE_BEGIN and