Skip to content

Instantly share code, notes, and snippets.

@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 / sphere_screen_extents.h
Last active May 28, 2025 13:51
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
@thefranke
thefranke / RSS.md
Last active May 18, 2025 02:49
A list of RSS endpoints, readers and resources

The RSS Endpoint List

Please refer to this blogpost to get an overview.

Replace *-INSTANCE with one of the public instances listed in the scrapers section. Replace CAPITALIZED words with their corresponding identifiers on the website.

Social Media

Twitter

@sebbbi
sebbbi / FastUniformLoadWithWaveOps.txt
Last active May 26, 2025 11:54
Fast uniform load with wave ops (up to 64x speedup)
In shader programming, you often run into a problem where you want to iterate an array in memory over all pixels in a compute shader
group (tile). Tiled deferred lighting is the most common case. 8x8 tile loops over a light list culled for that tile.
Simplified HLSL code looks like this:
Buffer<float4> lightDatas;
Texture2D<uint2> lightStartCounts;
RWTexture2D<float4> output;
[numthreads(8, 8, 1)]
@sebbbi
sebbbi / SinglePassMipPyramid.hlsl
Last active May 7, 2025 16:00
Single pass globallycoherent mip pyramid generation
// NOTE: Must bind 8x single mip RWTexture views, because HLSL doesn't have .mips member for RWTexture2D. (SRVs only have .mips member)
// NOTE: globallycoherent attribute is needed. Without it writes aren't guaranteed to be seen by other groups
globallycoherent RWTexture2D<float> MipTextures[8];
RWTexture2D<uint> Counters[8];
groupshared uint CounterReturnLDS;
[numthreads(16, 16, 1)]
void GenerateMipPyramid(uint3 Tid : SV_DispatchThreadID, uint3 Group : SV_GroupId, uint Gix : SV_GroupIndex)
{
[unroll]
@Erkaman
Erkaman / partition_of_unity.cpp
Last active April 22, 2018 11:02
Program that provides empirical evidence that the clamped cosine lobes 0.5cos^2 and (5/6)cos^4 form a partition of unity over the unit sphere. This is related to the ambient dice paper: http://www.ppsloan.org/publications/AmbientDice.pdf
#include <stdio.h>
#include <math.h>
float max(float x, float y) {
return x > y ? x : y;
}
class vec3 {
public:
float x;
struct FloatBits
{
u32 mantissa : 23;
u32 exponent : 8;
u32 sign : 1;
};
template <typename ResultT, typename InputT>
inline ResultT bitCast(InputT v)
{
@paniq
paniq / ellipsoid_frustum.md
Last active December 30, 2021 20:13
Ellipsoid Frustum Intersection

Ellipsoid Frustum Intersection

Yesterday I posted a problem to math stack exchange that bothered me for a while now, and right after I've had a few exchanges on Twitter, I got inspired to attempt a solution.

Here it goes. It's 100% untested but I'm fairly certain that it will work.

The problem is about a form of refining raytracing where we render a big list of convex 3D brushes (and I decided to start with Ellipsoids, since they're so useful) to the screen or a shadow map, without any prebuilt accelleration structure. How does it work? Well, if we had a way to figure out for a portion of the frustum whether it contained a brush, we could

  1. Start with a very low resolution
@romainguy
romainguy / d_ggx.glsl
Last active October 26, 2023 06:18
D_GGX in mediump/half float
// From https://github.com/google/filament
float D_GGX(float linearRoughness, float NoH, const vec3 h) {
// Walter et al. 2007, "Microfacet Models for Refraction through Rough Surfaces"
// In mediump, there are two problems computing 1.0 - NoH^2
// 1) 1.0 - NoH^2 suffers floating point cancellation when NoH^2 is close to 1 (highlights)
// 2) NoH doesn't have enough precision around 1.0
// Both problem can be fixed by computing 1-NoH^2 in highp and providing NoH in highp as well
// However, we can do better using Lagrange's identity: