Skip to content

Instantly share code, notes, and snippets.

@samloeschen
samloeschen / deferred.wgsl
Created October 3, 2023 03:12
simple gbuffer shader in wgsl
@group(0) @binding(0) var gBufferNormal: texture_2d<f32>;
@group(0) @binding(1) var gBufferAlbedo: texture_2d<f32>;
@group(0) @binding(2) var gBufferDepth: texture_depth_2d;
struct LightData {
position : vec4<f32>,
color : vec3<f32>,
radius : f32,
}
@samloeschen
samloeschen / Arena.cs
Last active July 27, 2023 14:12
C# unmanaged arena allocator and collections for it
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using Range = System.Range;
public unsafe struct Arena {
UnmanagedArray<Optional<Block>> _blocks;
int _currentBlockIndex;
@samloeschen
samloeschen / main.odin
Last active March 23, 2023 21:12
odin M1 alignment error repro
package main
import "core:fmt"
DangerStruct :: struct {
guid: u128,
value: u64,
}
SafeStruct1 :: struct {
@samloeschen
samloeschen / GlobalID.cs
Last active January 18, 2023 04:15
GlobalID
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using Object = UnityEngine.Object;
// This is a System.Guid, except it can be serialized by Unity,
// so it can be used to permanently store the ID of an entity.
@samloeschen
samloeschen / main.odin
Created June 5, 2022 19:21
d3d11 hello triangle in odin
package main
import "core:fmt"
import "vendor:directx/d3d11"
import "vendor:directx/d3d_compiler"
import "vendor:directx/dxgi"
import SDL "vendor:sdl2"
import "core:unicode/utf16"
WINDOW_WIDTH : i32 = 1280
@samloeschen
samloeschen / main.rs
Created December 18, 2021 15:18
wgpu-hello-triangle
use std::borrow::Cow;
use winit::{
event::*,
event_loop::{ControlFlow, EventLoop},
window::Window,
};
struct AppState {
surface: wgpu::Surface,
device: wgpu::Device,
@samloeschen
samloeschen / Bezier.cs
Last active November 29, 2021 21:54
Bezier Length Estimation
using UnityEngine;
using Unity.Mathematics;
using static Unity.Mathematics.math;
[ExecuteInEditMode]
public class Bezier : MonoBehaviour {
public Vector3 a;
public Vector3 b;
public Vector3 c;
@samloeschen
samloeschen / MathematicsTests.cs
Last active March 4, 2021 01:40
Mathf vs Mathematics
using Unity.Mathematics;
using UnityEngine;
using System.Diagnostics;
public class MathematicsTests : MonoBehaviour {
const int NUM_TESTS = 1_000_000;
void Test() {
Matrix4x4 identityMatrix4x4 = Matrix4x4.identity;
@samloeschen
samloeschen / RenderTexturePool.cs
Created October 19, 2020 18:11
RenderTexturePool
// Sometimes, RenderTexture.GetTemporary() and RenderTexture.ReleaseTemporary() leak textures.
// For those times, there is RenderTexturePool.
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
public class RenderTexturePool {
readonly Dictionary<RenderTextureDescriptor, List<RenderTextureCounter>> _pool;
readonly int _initialListCapacity;
@samloeschen
samloeschen / NativeListExtensions.cs
Last active October 1, 2020 04:10
NativeListExtensions
public static class NativeListExtensions {
public static unsafe void UnsafeAddRange<T>(this NativeList<T> nativeList, void* ptr, int length) where T: struct {
int idx = nativeList.Length;
int newCount = idx + length;
if (nativeList.Capacity < newCount) {
nativeList.Resize(nativeList.Capacity + length, NativeArrayOptions.UninitializedMemory);
}
nativeList.Length = newCount;
int stride = UnsafeUtility.SizeOf<T>();