Skip to content

Instantly share code, notes, and snippets.

Avatar
👣
Gruntled

Scott Lembcke slembcke

👣
Gruntled
View GitHub Profile
View CullOBB.c
/*
* Used for culling tiles in a map renderer I wrote.
* All tiles used geometry in the [-1, 1] range, and their mvp matrix would get precalculated.
* So it was simply a matter of seeing if their projected OBB overlapped with the screen's clip space.
* Returns true if the cube is visible. (false positives for near misses due to conservative w-value)
*/
static inline bool MapTileFrustumCull(const mat4 mvp){
// Clip space center of the cube is the last column of the matrix.
vec4 c = {mvp.m[12], mvp.m[13], mvp.m[14], mvp.m[15]};
// Clip space extents are the component wise absolute values of the first three columns.
@slembcke
slembcke / FourierLights.hlsl
Created Jan 21, 2020
Fourier lighting code
View FourierLights.hlsl
struct FragOutput {
half4 a0 : SV_TARGET0;
half4 a1 : SV_TARGET1;
half4 b1 : SV_TARGET2;
half4 a2 : SV_TARGET3;
half4 b2 : SV_TARGET4;
};
// Double angle formula derived from the trig identity.
float2 DoubleAngle(float2 n){
@slembcke
slembcke / mulx.c
Last active Oct 30, 2019
Signed 16.16 fixed point multiply.
View mulx.c
static inline fixed mulx_fast(fixed x, fixed y){return (fixed){(x.asint >> 8) * (y.asint >> 8)};}
static inline fixed mulx(fixed x, fixed y){
bool neg = (x.asint ^ y.asint) < 0;
union {
u32 asint;
struct {u16 hi, lo;};
} _x, _y;
if(x.asint >= 0) _x.asint = x.asint; else _x.asint = -x.asint;
View genesis-boot.s
00000204 <start>:
204: 1039 00a1 0001 moveb a10001 <VDP_REG_DMA_SRC2+0xa06901>,%d0
20a: 0200 000f andib #15,%d0
20e: 6700 000c beqw 21c <start+0x18>
212: 23fc 04fd 1e0d movel #83697165,a14000 <VDP_REG_DMA_SRC2+0xa0a900>
218: 00a1 4000
21c: 4a79 00c0 0004 tstw c00004 <VDP_CTRL>
222: 41f9 00c0 0004 lea c00004 <VDP_CTRL>,%a0
228: 30bc 8004 movew #-32764,%a0@
22c: 30bc 8144 movew #-32444,%a0@
@slembcke
slembcke / screenspace.shader
Last active Sep 3, 2019
Screenspace coordinates
View screenspace.shader
CGPROGRAM
// Pass a clean copy of the VP matrix.
// Shader.SetGlobalMatrix("_MyMatrixVP", Camera.current.projectionMatrix*Camera.current.worldToCameraMatrix);
// Unity applies poorly documented magic to UNITY_MATRIX_VP that you don't want.
float4x4 _MyMatrixVP;
struct VertexOutput {
// Pass the screen space coordinate as a new varying.
// Don't re-use SV_POSITION in your frag shader, it only works on non-DirectX platforms IIRC.
float4 screenSpace : TEXCOORD1;
@slembcke
slembcke / ProjectedBounds.cs
Last active Apr 29, 2019
Calculate a bounded mvp matrix for an object.
View ProjectedBounds.cs
using UnityEngine;
[ExecuteInEditMode]
public class ProjectedBounds : MonoBehaviour {
private Transform _transform;
private Renderer _renderer;
private Mesh _mesh;
private MaterialPropertyBlock _block;
private void Start(){
View timing.c
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
static const size_t COUNT = 64*(1<<20);
typedef float FLOAT_T;
View disassembler.c
#include <stdint.h>
#include <stdio.h>
#include "disassembler.h"
// 6502 instruction tables.
enum OPCODE {
OP_LDA, OP_LDX, OP_LDY, OP_STA, OP_STX, OP_STY, OP_ADC, OP_SBC,
OP_INC, OP_INX, OP_INY, OP_DEC, OP_DEX, OP_DEY, OP_ASL, OP_LSR,
OP_ROL, OP_ROR, OP_AND, OP_ORA, OP_EOR, OP_CMP, OP_CPX, OP_CPY,
@slembcke
slembcke / anisotropy.hlsl
Created Jan 30, 2019
Anisotropic reflections
View anisotropy.hlsl
// 'coord.y' is the clip space y-coordinate of the pixel on the water.
// 'ray0.y/ray0.w' is the clip space y-coordinate of the reflected pixel.
// The basic idea is near the horizion where the reflected point is close to the water,
// you should have a strong reflection. The further it gets from the horizon the more it should be blurred vertically.
// The result is pretty good. :D
float aniso = _SSRAniso*abs(coord.y - ray0.y/ray0.w);
float2 ddy_uv = max(ddy(uv), float2(0, aniso));
...
float4 color = tex2Dgrad(_CameraColorTexture, uv, ddx(uv), ddy_uv);
@slembcke
slembcke / decompress_lz4.s
Created Oct 28, 2018
GBZ80 Lz4 decompression function progress.
View decompress_lz4.s
section "lz4-data", hram
token: db ; Most recently read token.
backref: dw
; NOTES:
; dst/src is always stored in de/hl
section "lz4-code", rom0