Skip to content

Instantly share code, notes, and snippets.

@slembcke
slembcke / frame-timing.c
Last active June 18, 2023 19:05
Filter frame timing to try and remove scheduler jitter.
double delta_time_filtered(double dt_nanos){
// Warm start the filter by assuming 60 Hz.
static double x[] = {1.6e7, 1.6e7, 1.6e7}, y[] = {1.6e7, 1.6e7, 1.6e7};
// IIR filter coefficients. 2rd order lowpass butterworth at 1/128 the sample rate.
static const double b[] = {6.321391700454014e-5, 0.00012642783400908025, 6.321391700454014e-5};
static const double a[] = {1.0, -1.9681971279272976, 0.9684499835953156};
// Apply IIR filter coefficients.
double value = b[0]*dt_nanos;
for(uint i = 2; i > 0; i--){
@garettbass
garettbass / type_index_generator.zig
Last active June 7, 2022 15:49
A generic type that maps provided types to monotonically increasing integers.
const std = @import("std");
pub fn TypeIndexGenerator(comptime TIndex: type) type {
const index_info = @typeInfo(TIndex);
const TInt = switch (index_info) {
.Int => TIndex,
.Enum => |t| if (t.is_exhaustive)
@compileError("TIndex enum must be non-exhaustive")
else
t.tag_type,

Emscripten as a linker for Zig and C

This shows how to build a nontrivial program using Zig+Emscripten or C+Emscripten. In both cases Emscripten is only used as a linker, that is the frontend is either zig or clang.

"Nontrivial" here means the program uses interesting Emscripten features:

  • Asyncify
  • Full GLES3 support
  • GLFW3 support
// Example: Opcode dispatch in a bytecode VM. Assume the opcode case dispatching is mispredict heavy,
// and that pc, ins, next_ins, next_opcase are always in registers.
#define a ((ins >> 8) & 0xFF)
#define b ((ins >> 16) & 0xFF)
#define c ((ins >> 24) & 0xFF)
// Version 1: Synchronous instruction fetch and opcode dispatch. The big bottleneck is that given how light
// the essential work is for each opcode case (e.g. something like ADD is typical), you're dominated
// by the cost of the opcode dispatch branch mispredicts. When there's a mispredict, the pipeline restarts
@pervognsen
pervognsen / expr.c
Last active February 5, 2023 17:27
void parse_expr(Value *dest);
Sym *parse_ident(void) {
if (tok != TOK_IDENT) {
error("Expected identifier");
}
Sym *ident = tok_sym;
next();
return ident;
}
@pixelsnafu
pixelsnafu / CloudsResources.md
Last active May 2, 2024 13:46
Useful Resources for Rendering Volumetric Clouds

Volumetric Clouds Resources List

  1. A. Schneider, "Real-Time Volumetric Cloudscapes," in GPU Pro 7: Advanced Rendering Techniques, 2016, pp. 97-127. (Follow up presentations here, and here.)

  2. S. Hillaire, "Physically Based Sky, Atmosphere and Cloud Rendering in Frostbite" in Physically Based Shading in Theory and Practice course, SIGGRAPH 2016. [video] [course notes] [scatter integral shadertoy]

  3. [R. Högfeldt, "Convincing Cloud Rendering – An Implementation of Real-Time Dynamic Volumetric Clouds in Frostbite"](https://odr.chalmers.se/hand

@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
@vurtun
vurtun / x11_clipboard.c
Last active June 13, 2023 21:28
X11 clipboard
static char*
sys_clip_get(struct sys *s, Atom selection, Atom target)
{
assert(s);
struct sys_x11 *x11 = s->platform;
/* blocking wait for clipboard data */
XEvent notify;
XConvertSelection(x11->dpy, selection, target, selection, x11->helper, CurrentTime);
while (!XCheckTypedWindowEvent(x11->dpy, x11->helper, SelectionNotify, &notify)) {
@Reedbeta
Reedbeta / interesting-libs.txt
Last active July 4, 2023 02:38
Interesting libraries I might like to use in a project
Interesting libraries I might like to use in a project...
Asset loading:
assetsys.h - virtual filesystem with ZIP backing, overlaying, etc https://github.com/mattiasgustavsson/libs/blob/master/docs/assetsys.md
cute_filewatch.h - file modification watching, for runtime reloading etc https://github.com/RandyGaul/cute_headers/blob/master/cute_filewatch.h
flatbuffers - data serialization, zero-copy deserialization, extensible schemas https://github.com/google/flatbuffers
stb_image - https://github.com/nothings/stb/blob/master/stb_image.h
tinyexr - https://github.com/syoyo/tinyexr
tinygltf - https://github.com/syoyo/tinygltf
tinyobjloader - https://github.com/syoyo/tinyobjloader
/*
The MIT License (MIT)
Copyright (c) 2016 Andre Leiradella
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is