Skip to content

Instantly share code, notes, and snippets.

@TheGag96
TheGag96 / raytracer.jai
Created January 14, 2025 15:19
Jai adaptation of Casey Muratori's Handemade Ray example, day 0 (with modifications)
// Casey Muratori's Handmade Ray tutorial, translated to Jai
// https://guide.handmadehero.org/ray/ray00/
main :: () {
image := allocate_image(1920, 1080);
materials := Material.[
.{emit_color = srgb_to_linear(.{0.3, 0.4, 0.5})},
.{ref_color = srgb_to_linear(.{0.5, 0.5, 0.5})},
.{ref_color = srgb_to_linear(.{0.7, 0.5, 0.3})},
@TheGag96
TheGag96 / Late_Load.jai
Last active January 9, 2025 07:09
Jai metaprogram: Automagically turn modules with #foreign procedures into dynamically-loaded function pointers
late_load_import :: ($module: Type) -> string #compile_time {
#insert -> string {
module_name := name_of_module(module);
return sprint("LIBRARIES :: Late_Load_Module_%.LIBRARIES;", module_name);
};
using_statements: String_Builder;
for LIBRARIES {
print_to_builder(*using_statements, "using __late_load_library_%;\n", it);
@TheGag96
TheGag96 / Atomics.jai
Created December 17, 2024 14:02
Hacky ARM64 atomics in Jai
// ARM64 hack: Unlike x64, even though aligned reads are atomic, ARM64 doesn't guarantee acquire semantics with
// normal reads and release semantics with normal stores. There are separate instructions for those,
// and since we don't have ARM64 inline assembly yet, we must implement them with this very very spooky
// hack where we hardcode the machine instructions we want. The parameters are assumed to be passed
// according to Jai's current LLVM ABI!
// Thanks @ctpeepee!
atomic_read :: no_inline (pointer: *s64) -> s64 {
// ldr x1, [x1] x1 initially contains a pointer to "pointer", which itself is a pointer
// ldar x1, [x1]
// str x1, [x2] x2 here contains a pointer to the first return value.
@TheGag96
TheGag96 / Game_Of_Life.jai
Created September 23, 2024 21:48
GPU-accelerated Conway's Game of Life
Simp :: #import "Simp";
Input :: #import "Input";
#import "Basic";
#import "Random";
#import "Math";
#import "Window_Creation";
#import "GL";
Render_Texture :: struct {
@TheGag96
TheGag96 / Bitfields.jai
Last active August 2, 2024 20:39
An implementation of bitfields in Jai
// Tries to implement bitfields in the most ergonomic way I can think of.
// Inspired in part by what D's standard library did before the language supported bitfields.
#insert #run bitfields_struct(
"Thing",
bf("a", u32, 4),
bf("b", u32, 4),
bf("c", s32, 4),
bf("d", u32, 4),
);
@TheGag96
TheGag96 / build_cross_gcc
Last active July 23, 2023 17:27 — forked from preshing/build_cross_gcc
Raspberry Pi x64 Cross GCC build script
#! /bin/bash
set -e
trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG
trap 'echo FAILED COMMAND: $previous_command' EXIT
#-------------------------------------------------------------------------------------------
# This script will download packages for, configure, build and install a GCC cross-compiler.
# Customize the variables (INSTALL_PATH, TARGET, etc.) to your liking before running.
# If you get an error and need to resume the script from some point in the middle,
# just delete/comment the preceding lines before running it again.
@TheGag96
TheGag96 / main.c
Created May 26, 2020 02:40
gpusprites example, but print out spritesheet load time
// Simple citro2d sprite drawing example
// Images borrowed from:
// https://kenney.nl/assets/space-shooter-redux
#include <citro2d.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@TheGag96
TheGag96 / staticforeach.d
Last active March 28, 2020 20:04
Static foreach example
void func(size_t size)() {
static foreach (p; 0..size) {
perform_some_action(p);
}
}
void perform_some_action(size_t p) {
import std.stdio : writeln;
writeln(p);
}
@TheGag96
TheGag96 / soa.d
Created March 11, 2020 02:32
D SOA example
import std.string : format;
struct Soa(T, long n) {
static foreach (member; T.tupleof) {
mixin(format("typeof(member)[n] %s;", member.stringof));
}
T opIndex(size_t index) const {
T result = void;
// Simple citro2d sprite drawing example
// Images borrowed from:
// https://kenney.nl/assets/space-shooter-redux
#include <citro2d.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>