summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2026-02-16 09:49:45 -0800
committer3gg <3gg@shellblade.net>2026-02-16 09:49:45 -0800
commitdcb77dcaad1fb0be23bfadbaad688a7b7eb1135e (patch)
tree60fba9740fa9620cbfbe853be77a16c3e4fe21d7 /src
parent117c310e8baf60aa5f052214e1747b5846f34b4e (diff)
Faster gammaHEADmain
Diffstat (limited to 'src')
-rw-r--r--src/swgfx.c19
1 files changed, 10 insertions, 9 deletions
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 -
96static inline sgVec3 mul3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x * b.x, a.y * b.y, a.z * b.z}; } 96static inline sgVec3 mul3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x * b.x, a.y * b.y, a.z * b.z}; }
97static inline sgVec3 div3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x / b.x, a.y / b.y, a.z / b.z}; } 97static inline sgVec3 div3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x / b.x, a.y / b.y, a.z / b.z}; }
98static inline sgVec3 scale3(sgVec3 v, R s) { return (sgVec3){v.x * s, v.y * s, v.z * s}; } 98static inline sgVec3 scale3(sgVec3 v, R s) { return (sgVec3){v.x * s, v.y * s, v.z * s}; }
99static inline sgVec3 lerp3(sgVec3 a, sgVec3 b, R t) { return add3(a, scale3(sub3(b,a), t)); } 99static inline sgVec3 lerp3(sgVec3 a, sgVec3 b, R t) { return add3(a, scale3(sub3(b,a), t)); }
100static inline sgVec3 exp3(sgVec3 v, R exp) { return (sgVec3){powf(v.x, exp), powf(v.y, exp), powf(v.z, exp)};} 100static inline sgVec3 exp3(sgVec3 v, R exp) { return (sgVec3){powf(v.x, exp), powf(v.y, exp), powf(v.z, exp)};}
101static inline sgVec3 square3(sgVec3 v) { return (sgVec3){v.x*v.x, v.y*v.y, v.z*v.z }; }
102static inline sgVec3 sqrt3 (sgVec3 v) { return (sgVec3){sqrt(v.x), sqrt(v.y), sqrt(v.z) }; }
101static inline R dot3(sgVec3 a, sgVec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; } 103static inline R dot3(sgVec3 a, sgVec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
102static inline R normsq3(sgVec3 v) { return v.x * v.x + v.y * v.y + v.z * v.z; } 104static inline R normsq3(sgVec3 v) { return v.x * v.x + v.y * v.y + v.z * v.z; }
103static inline R norm3 (sgVec3 v) { return (R)sqrt(normsq3(v)); } 105static inline R norm3 (sgVec3 v) { return (R)sqrt(normsq3(v)); }
@@ -1058,23 +1060,22 @@ void sgNormals(swgfx* gfx) {
1058 } 1060 }
1059} 1061}
1060 1062
1061void sgGamma(swgfx* gfx, sgTexel* texels, int width, int height) { 1063void sgGamma(sgTexel* texels, int width, int height) {
1062 assert(gfx);
1063 assert(texels); 1064 assert(texels);
1064 constexpr R exp = 2.2f;
1065 for (int i = 0; i < width * height; ++i) { 1065 for (int i = 0; i < width * height; ++i) {
1066 sgTexel* const p = &texels[i]; 1066 sgTexel* const p = &texels[i];
1067 *p = Colour3ToTexel(exp3(TexelToColour3(*p), exp), p->a); 1067 //*p = Colour3ToTexel(exp3(TexelToColour3(*p), 2.2f), p->a);
1068 *p = Colour3ToTexel(square3(TexelToColour3(*p)), p->a);
1068 } 1069 }
1069} 1070}
1070 1071
1071void sgGammaInv(swgfx* gfx, sgColour4* pixels, int width, int height) { 1072void sgGammaInv(sgColour4* pixels, int width, int height) {
1072 assert(gfx);
1073 assert(pixels); 1073 assert(pixels);
1074 constexpr R exp = 1.0f/2.2f;
1075 for (int i = 0; i < width * height; ++i) { 1074 for (int i = 0; i < width * height; ++i) {
1076 sgColour4* const p = &pixels[i]; 1075 sgColour4* const p = &pixels[i];
1077 *p = Vec4FromVec3(exp3(Vec3FromVec4(*p), exp), p->a); 1076 // sqrt() is faster (~500us at 320x240) than pow().
1077 //*p = Vec4FromVec3(exp3(Vec3FromVec4(*p), 1.0f/2.2f), p->a);
1078 *p = Vec4FromVec3(sqrt3(Vec3FromVec4(*p)), p->a);
1078 } 1079 }
1079} 1080}
1080 1081