Skip to content

Instantly share code, notes, and snippets.

@paniq
Last active May 30, 2019 10:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paniq/492933e6511ee69be6f9c82769283d25 to your computer and use it in GitHub Desktop.
Save paniq/492933e6511ee69be6f9c82769283d25 to your computer and use it in GitHub Desktop.
#!/usr/bin/env scopes
using import glm
using import glsl
using import itertools
using import ..tukan.gl
using import ..tukan.bitmap
using import ..tukan.random
using import .testfragment
let N = 512
let R2 = 11
let image =
Bitmap1 (ivec2 N)
for x y in (dim N N)
('fetch image x y) =
(x % 256) as u8
fn gaussian (x mu sigma)
h := (x - mu) / sigma
h := h * h * -0.5:f64
a := 1.0:f64 / (sigma * (sqrt (2.0:f64 * pi:f64)))
a * (exp h)
# 1.4375
# 1.40625
# 1.390625
let lo = 1.375:f64
let hi = 1.40625:f64
let mid = (((lo + hi) * 0.5) as f64)
print mid
fn quantify-error (image x y R2 val0 val1)
let Rf = ((R2 / 2) as f64)
let R = (Rf as i32)
local has0 = 0.0:f64
local has1 = 0.0:f64
local w = 0.0:f64
for sx sy in (dim R2 R2)
let sx sy =
sx - R
sy - R
let d = (length (dvec2 sx sy))
if ((d > Rf) | ((sx == 0) & (sy == 0)))
continue;
let px =
(x + sx + N) % N
let py =
(y + sy + N) % N
let v = (deref ('fetch image px py))
let dist0 = (abs ((v as i32) - (val0 as i32)))
let dist1 = (abs ((v as i32) - (val1 as i32)))
let q = (gaussian d 0.0:f64 mid)
w += 1.0:f64
inline distf (x)
1.0:f64 - (clamp ((x / 255) as f64) 0.0:f64 1.0:f64)
has0 += ((distf dist0) * q)
has1 += ((distf dist1) * q)
_
has0 / w
has1 / w
fn exchange (image x0 y0 x1 y1)
let p0 =
'fetch image x0 y0
let p1 =
'fetch image x1 y1
let v0 = (deref p0)
let v1 = (deref p1)
let s0 x0 = (quantify-error image x0 y0 R2 v0 v1)
let s1 x1 = (quantify-error image x1 y1 R2 v1 v0)
inline pow2 (x) (x * x)
let err_s =
+
pow2 s0
pow2 s1
let err_x =
+
pow2 x0
pow2 x1
if (err_x < err_s)
p0 = v1
p1 = v0
true
else
false
local random : (Random)
fn process (image random)
local changes = 0
for i in (range (N * N))
let x0 y0 =
'range random N
'range random N
let x1 y1 =
'range random N
'range random N
if (exchange image x0 y0 x1 y1)
changes += 1
print changes
inline main ()
let test_texture = (GL.CreateTexture GL.TEXTURE_2D)
#let image = (Bitmap4f (ivec2 256))
#('fetch image 64 64) = 1
'setup test_texture
bitmap = image
uniform smp : sampler2D
location = 2
inline per-frame-setup ()
process (view image) random
'update (view test_texture) (view image)
GL.BindTextureUnit 0 (view test_texture)
GL.Uniform smp 0
inline shader (uv)
let size =
vec2 (textureSize smp 0)
let val = (texture smp uv)
let r = val.r
vec4 r r r 1
_ per-frame-setup shader
render-fragment-shader main
#debug = true
size = (ivec2 N)
'save-png image (.. module-dir "/bluenoise.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment