From dcb77dcaad1fb0be23bfadbaad688a7b7eb1135e Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Mon, 16 Feb 2026 09:49:45 -0800 Subject: Faster gamma --- src/swgfx.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'src/swgfx.c') diff --git a/src/swgfx.c b/src/swgfx.c index 1901057..1e22ade 100644 --- a/src/swgfx.c +++ b/src/swgfx.c @@ -96,8 +96,10 @@ static inline sgVec3 sub3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x - b.x, a.y - static inline sgVec3 mul3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x * b.x, a.y * b.y, a.z * b.z}; } static inline sgVec3 div3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x / b.x, a.y / b.y, a.z / b.z}; } static inline sgVec3 scale3(sgVec3 v, R s) { return (sgVec3){v.x * s, v.y * s, v.z * s}; } -static inline sgVec3 lerp3(sgVec3 a, sgVec3 b, R t) { return add3(a, scale3(sub3(b,a), t)); } +static inline sgVec3 lerp3(sgVec3 a, sgVec3 b, R t) { return add3(a, scale3(sub3(b,a), t)); } static inline sgVec3 exp3(sgVec3 v, R exp) { return (sgVec3){powf(v.x, exp), powf(v.y, exp), powf(v.z, exp)};} +static inline sgVec3 square3(sgVec3 v) { return (sgVec3){v.x*v.x, v.y*v.y, v.z*v.z }; } +static inline sgVec3 sqrt3 (sgVec3 v) { return (sgVec3){sqrt(v.x), sqrt(v.y), sqrt(v.z) }; } static inline R dot3(sgVec3 a, sgVec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; } static inline R normsq3(sgVec3 v) { return v.x * v.x + v.y * v.y + v.z * v.z; } static inline R norm3 (sgVec3 v) { return (R)sqrt(normsq3(v)); } @@ -1058,23 +1060,22 @@ void sgNormals(swgfx* gfx) { } } -void sgGamma(swgfx* gfx, sgTexel* texels, int width, int height) { - assert(gfx); +void sgGamma(sgTexel* texels, int width, int height) { assert(texels); - constexpr R exp = 2.2f; for (int i = 0; i < width * height; ++i) { sgTexel* const p = &texels[i]; - *p = Colour3ToTexel(exp3(TexelToColour3(*p), exp), p->a); + //*p = Colour3ToTexel(exp3(TexelToColour3(*p), 2.2f), p->a); + *p = Colour3ToTexel(square3(TexelToColour3(*p)), p->a); } } -void sgGammaInv(swgfx* gfx, sgColour4* pixels, int width, int height) { - assert(gfx); +void sgGammaInv(sgColour4* pixels, int width, int height) { assert(pixels); - constexpr R exp = 1.0f/2.2f; for (int i = 0; i < width * height; ++i) { sgColour4* const p = &pixels[i]; - *p = Vec4FromVec3(exp3(Vec3FromVec4(*p), exp), p->a); + // sqrt() is faster (~500us at 320x240) than pow(). + //*p = Vec4FromVec3(exp3(Vec3FromVec4(*p), 1.0f/2.2f), p->a); + *p = Vec4FromVec3(sqrt3(Vec3FromVec4(*p)), p->a); } } -- cgit v1.2.3