Hi all,
I’ve ported a pattern based on this shader but am getting < 1 FPS on a Pico.
Does anyone have any ideas on how to optimize? Is it possible to profile performance?
Thanks in advance!
export var t1;
export function beforeRender(delta) {
t1 = time(.1);
}
var log_step = 79. / 53.;
function uv(p) {
return p + 2.0 * perlin(p * 0.1 + t1, 0, 0, 0);
}
function draw(p) {
var d = log_step * 2.0;
var u = uv(p);
var ctr = 0.0;
var lid = getLocalID(u, d, ctr);
var gid = getGlobalID(lid, d);
return getRandomColor(gid);
}
function quantize(d) {
return pow(log_step, floor(log(d) / log(log_step)));
}
function d2level(d) {
return floor(log(d) / log(log_step));
}
function level2d(level) {
return pow(log_step, round(level));
}
function interpolate(d) {
var level = d2level(d);
var df = level2d(level);
var dc = level2d(level - 1);
var linear = (d - df) / (df - dc) / log_step;
return smoothstep(0.0, 1.0, linear);
}
function spacing(d) {
return 1.0 / d;
}
function id2Lborder(id, d) {
return (id + (d / spacing(d))) * spacing(d);
}
function id2center(id, d) {
return (id + 0.5 + (d / spacing(d))) * spacing(d);
}
function u2id(u, d) {
return floor(u / spacing(d) - (d / spacing(d)));
}
function u2closestLBorderId(u, d) {
var lid = u2id(u, d);
if (u < id2center(lid, d)) {
return lid;
} else {
return lid + 1;
}
}
function u2closestBorder(u, d) {
return id2Lborder(u2closestLBorderId(u, d), d);
}
function getLocalID(u, d, ctr) {
var level = d2level(d);
var df = level2d(level);
var dc = level2d(level - 1);
var lid = u2id(u, df);
var lf_brdr = id2Lborder(lid, df);
var rf_brdr = id2Lborder(lid + 1, df);
var lc_brdr = u2closestBorder(lf_brdr, dc);
var rc_brdr = u2closestBorder(rf_brdr, dc);
var a = interpolate(d);
var left = lf_brdr * a + lc_brdr * (1.0 - a);
var right = rf_brdr * a + rc_brdr * (1.0 - a);
if (u < left) lid--;
else if (u > right) lid++;
if (abs(left - right) < 0.5 * spacing(df)) {
ctr = -99999.0;
} else {
ctr = (left + right) / 2.0;
}
return lid;
}
function setId(id, bit) {
if (bit > 127) return id;
if (bit < 64) {
if (bit < 32) {
return [id[0] | 1 << bit, id[1] | 1 << bit, id[2] | 1 << bit, id[3] | 1 << bit];
} else {
return [id[0] | 1 << (bit - 32), id[1] | 1 << (bit - 32), id[2] | 1 << (bit - 32), id[3]];
}
} else {
if (bit < 96) {
return [id[0], id[1] | 1 << (bit - 64), id[2], id[3]];
} else {
return [id[0] | 1 << (bit - 96), id[1], id[2], id[3]];
}
}
}
function getGlobalID(lid, d) {
var gid = [0, 0, 0, 0];
var start_level = d2level(d);
for (var level = start_level; level > 0; level--) {
var df = level2d(level);
var dc = level2d(level - 1);
var lf_brdr = id2Lborder(lid, df);
var rf_brdr = id2Lborder(lid + 1, df);
var lid_brdr = u2closestLBorderId(lf_brdr, dc);
var rid_brdr = u2closestLBorderId(rf_brdr, dc);
if (lid_brdr == rid_brdr) {
gid = setId(gid, 96 - level);
lid = u2id(lf_brdr, dc);
} else {
lid = lid_brdr;
}
}
gid[0] = lid;
return gid;
}
function paletteAt(t) {
var palette = [0.500, 0.500, 0.500, 0.500, 0.500, 0.500, 1.000, 1.000, 1.000, 0.000, 0.333, 0.667];
_r = palette[0] + palette[3] * cos(PI2 * ((palette[6] * t) + palette[9]));
_g = palette[1] + palette[4] * cos(PI2 * ((palette[7] * t) + palette[10]));
_b = palette[2] + palette[5] * cos(PI2 * ((palette[8] * t) + palette[11]));
return rgb(_r, _g, _b);
}
function getRandomColor(id) {
return paletteAt(frac(sin(id[0] * 12.9898) * 43758.5453123));
}
export function render(index) {
draw(15 * index / pixelCount);
}