Skip to content

Instantly share code, notes, and snippets.

@JohnnyonFlame
Created May 23, 2016 09:32
Show Gist options
  • Save JohnnyonFlame/ec215c1a4a8e8597431ea4e4acffb56a to your computer and use it in GitHub Desktop.
Save JohnnyonFlame/ec215c1a4a8e8597431ea4e4acffb56a to your computer and use it in GitHub Desktop.
R5G6B5 Software dithered fog for TinyGLES
uint32_t (*fb_dst)[surface->w/2] = (uint32_t(*)[surface->w/2])surface->pixels;
uint32_t (*px_src)[surface->w/2] = (uint32_t(*)[surface->w/2])cur_zbuf->pbuf;
uint32_t (*zb_src)[surface->w/2] = (uint32_t(*)[surface->w/2])cur_zbuf->zbuf;
uint16_t err_acc = 0;
#define basecoef 0xFFFF
#define precision 0xFFC0
#define accumulate ((basecoef ^ precision))
#define fixmath_depth(c, mul) (c) = (((c) * ((mul) & precision)) >> 10)
#define MACRO_PROPAGATE_FOG(r, g, b, alpha) \
if (alpha) { \
alpha=((alpha > 0x3FF) ? 0x3FF : alpha)+err_acc; \
fixmath_depth(r, alpha); \
fixmath_depth(g, alpha); \
fixmath_depth(b, alpha); \
err_acc = (alpha & accumulate); \
}
#define MACRO_PIX_BUF(ra, ga, ba, rb, gb, bb, px) \
ra = (px >> 27) & 0x1F; \
ga = (px >> 21) & 0x3F; \
ba = (px >> 16) & 0x1F; \
rb = (px >> 11) & 0x1F; \
gb = (px >> 5 ) & 0x3F; \
bb = (px ) & 0x1F;
#define MACRO_Z_BUF(z1, z2, zbuf) \
z1 = zbuf >> 16, \
z2 = zbuf & 0xFFFF;
#define MACRO_GET_PX_TUPLE(ra, ga, ba, rb, gb, bb) ((ra) << 27 | (ga) << 21 | (ba) << 16 | (rb) << 11 | (gb) << 5 | (bb));
for (int i=0; i<surface->h; i++) {
for (int j=i; j<surface->w/2; j++) {
uint8_t r1, g1, b1, r2, g2, b2;
uint16_t z1, z2;
MACRO_PIX_BUF(r1, g1, b1, r2, g2, b2, px_src[i*2 ][j]);
MACRO_Z_BUF(z1, z2, zb_src[i*2 ][j]);
MACRO_PROPAGATE_FOG(r1, g1, b1, z1);
MACRO_PROPAGATE_FOG(r2, g2, b2, z2);
fb_dst[i*2 ][j] = MACRO_GET_PX_TUPLE(r1, g1, b1, r2, g2, b2);
MACRO_PIX_BUF(r1, g1, b1, r2, g2, b2, px_src[i*2+1][j]);
MACRO_Z_BUF(z1, z2, zb_src[i*2+1][j]);
MACRO_PROPAGATE_FOG(r1, g1, b1, z1);
MACRO_PROPAGATE_FOG(r2, g2, b2, z2);
fb_dst[i*2+1][j] = MACRO_GET_PX_TUPLE(r1, g1, b1, r2, g2, b2);
if (j*2 < surface->h) {
uint8_t r1, g1, b1, r2, g2, b2;
uint16_t z1, z2;
MACRO_PIX_BUF(r1, g1, b1, r2, g2, b2, px_src[j*2 ][i]);
MACRO_Z_BUF(z1, z2, zb_src[j*2 ][i]);
MACRO_PROPAGATE_FOG(r1, g1, b1, z1);
MACRO_PROPAGATE_FOG(r2, g2, b2, z2);
fb_dst[j*2 ][i] = MACRO_GET_PX_TUPLE(r1, g1, b1, r2, g2, b2);
MACRO_PIX_BUF(r1, g1, b1, r2, g2, b2, px_src[j*2+1][i]);
MACRO_Z_BUF(z1, z2, zb_src[j*2+1][i]);
MACRO_PROPAGATE_FOG(r1, g1, b1, z1);
MACRO_PROPAGATE_FOG(r2, g2, b2, z2);
fb_dst[j*2+1][i] = MACRO_GET_PX_TUPLE(r1, g1, b1, r2, g2, b2);
}
}
err_acc = ((err_acc*0x416AA2678) >> 3) & (accumulate >> 4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment