Skip to content

Instantly share code, notes, and snippets.

View dwilliamson's full-sized avatar

Don Williamson dwilliamson

View GitHub Profile
@dwilliamson
dwilliamson / Description.md
Last active June 24, 2026 18:05
Compile-time Shader Assembly for DX8

In the olden DX8 days a common technique was to compile shaders at runtime using assembly instructions in strings. You would composite sections of pre-written shader based on how many lights were active, what type of material you were lighting and any other properties that would affect rendering.

This had two problems, in order of effect:

  1. You'd have to pass the assembly code onto the DX8 runtime which would then parse and compile it.
  2. You're doing a whole bunch of string concatenations everywhere. With dynamic register allocation, that means sprintf calls.

The reason runtime compilation was common was that precompiling all combinations offline was prohibitive as the total count would be in the 10s of thousands.

Anyway, I was never happy with the performance of compiling shaders at runtime; there'd be noticable hitching as the game would spend upwards of 100ms compiling. There were imperfect ways of stepping around the issue, like precompiling all shaders on specific offline camera paths, but you'd st

@dwilliamson
dwilliamson / CUDA.cpp
Last active May 11, 2026 21:03
Star Engine CUDA Implementation from 2015
@@ -1,785 +0,0 @@
#include "CUDA.h"
#include "CUDACompute.h"
#include <Core/File.h>
clcpp_impl_destruct(cuda::Queue);
clcpp_impl_class(cuda::Kernel);
@dwilliamson
dwilliamson / GUIDGenerator.cpp
Created September 14, 2015 17:56
GUID Generator
namespace core
{
//
// A globally unique identifier with a generator that should allow multiple machines/users
// to never generate the same ID.
//
struct clcpp_attr(reflect) Guid
{
Guid()
: mac_address_hash(0)
@dwilliamson
dwilliamson / Out.cpp
Last active February 28, 2026 09:54
Can you force C++ call sites to prefix "out" for all mutate-via-reference parameters?
#define out OutCatcher() <<
template <typename TYPE> struct Out
{
explicit Out(TYPE& ref) : ref(ref) { }
TYPE& ref;
};
struct OutCatcher
{
@dwilliamson
dwilliamson / MarchingCubes.js
Last active February 13, 2026 16:00
Marching Cubes Lookup Tables
//
// Lookup Tables for Marching Cubes
//
// These tables differ from the original paper (Marching Cubes: A High Resolution 3D Surface Construction Algorithm)
//
// The co-ordinate system has the more convenient properties:
//
// i = cube index [0, 7]
// x = (i & 1) >> 0
// y = (i & 2) >> 1
@dwilliamson
dwilliamson / ModifiedMarchingCubes.js
Created February 8, 2022 16:20
Transvoxel's Modified Marching Cubes Lookup Tables
//
// Lookup Tables for Transvoxel's Modified Marching Cubes
//
// Unlike the original paper (Marching Cubes: A High Resolution 3D Surface Construction Algorithm), these tables guarantee
// a closed mesh "whose connected components are continuous and free of holes."
//
// Rotations are prioritised over inversions so that 3 of the 6 cases containing ambiguous faces are never added. 3 extra
// cases are added as a post-process, overriding inverses through custom-build rotations to eliminate the rest.
//
// Uses the exact same co-ordinate system as https://gist.github.com/dwilliamson/c041e3454a713e58baf6e4f8e5fffecd
@dwilliamson
dwilliamson / shellmap.md
Created January 11, 2019 16:38
Shell Mapping

So the technique is called Shell Mapping and I got the basics from:

Interactive Smooth and Curved Shell Mapping
https://www.cg.tuwien.ac.at/research/publications/2007/JESCHKE-2007-ISC/

The technique as documented starts with Geometry Shaders which is a really bad idea so I kept things simple and did all the pre-processing on the CPU when meshes were imported. These days I'd look at doing it on-demand in a Compute Shader. Preprocessing steps are:

  1. Parameterise a heightmap for your mesh.
@dwilliamson
dwilliamson / Adjacency.cpp
Last active June 2, 2025 17:54
Simple, fast adjacency builder
struct Adjacency
{
struct Vertex
{
// Vertices are shared by a variable number of edges. This indexes the global EdgeRef array.
uint32_t edgeStartRef;
uint32_t nbEdges;
};
union EdgeRef
//
// TinyCRT, revamp and TinyWin support by Don Williamson, 2011
// Based on http://www.codeproject.com/KB/library/tlibc.aspx and LIBCTINY by Matt Pietrek
//
#pragma once
#ifdef USE_DEFAULT_CRT
@dwilliamson
dwilliamson / Doc.md
Last active April 23, 2023 14:17
Minimal Code Generation for STL-like Containers

This is my little Christmas-break experiment trying to (among other things) reduce the amount of generated code for containers.

THIS CODE WILL CONTAIN BUGS AND IS ONLY PRESENTED AS AN EXAMPLE.

The C++ STL is still an undesirable library for many reasons I have extolled in the past. But it's also a good library. Demons lie in this here debate and I have no interest in revisiting it right now.

The goals that I have achieved with this approach are: