Skip to content

Instantly share code, notes, and snippets.

View jedjoud10's full-sized avatar

Jedjoud10 jedjoud10

View GitHub Profile
@jedjoud10
jedjoud10 / RleCompressionJob.cs
Created March 25, 2024 00:03
RLE (De)Compression in Unity Jobs System
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
[BurstCompile(CompileSynchronously = true)]
internal struct RleCompressionJob : IJob {
[ReadOnly]
public NativeArray<byte> bytesIn;
[WriteOnly]
public NativeList<uint> uintsOut;
@jedjoud10
jedjoud10 / utils.rs
Created November 18, 2023 04:44
Bitwise operation utilities in rust. Uses PrimInt from the num_traits crate.
/// Update a value in a specific bitmask, though return the unwritten value first
pub fn toggle_bit<T: PrimInt>(bitmask: &mut T, index: usize, value: bool) -> bool {
let copy = ((*bitmask >> index) & T::one()) == T::one();
if value {
*bitmask = *bitmask | (T::one() << index);
} else {
*bitmask = *bitmask & (!(T::one() << index));
}
@jedjoud10
jedjoud10 / specialize_spec_constants.rs
Created November 18, 2023 04:43
Specialize SpecConstants in SPIRV. Rust.
// Specialize spec constants ourselves cause there's no other way to do it (fuck)
fn specialize_spec_constants(binary: &mut [u32], constants: &Constants) {
// Converts a SpecConstant op code to it's specialized variant (Constant)
fn specialize(op_code_index: usize, binary: &mut [u32], defined: SpecConstant) {
// Get the op code of the spec constant
let op_code = binary[op_code_index] & 0x0000ffff;
// Get the index of the spec constant literal
let literal_index = match op_code {
48 | 49 => op_code_index + 2,
@jedjoud10
jedjoud10 / build.rs
Created November 14, 2023 23:48
WASM4 Rust build.rs sprite packing into .pak file
use bitvec::{field::BitField, prelude::*};
use std::{
fs, io,
path::{Path, PathBuf},
};
// Limitations:
// max sprite sheet size: (256 in either direction)
// max number of colors: 4 (for wasm4)
// ASSUMES THE FILE HAS BEEN EXPORTED FROM ASEPRITE IN INDEXED MODE