[Scummvm-git-logs] scummvm master -> ed484ae6d7a429171cf6d0dc50ab97ef29c85afa

aquadran noreply at scummvm.org
Wed Nov 11 05:30:24 UTC 2026


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://api.github.com/repos/scummvm/scummvm .

Summary:
ed484ae6d7 TINYGL: Clamp interpolated colours instead of letting them wrap


Commit: ed484ae6d7a429171cf6d0dc50ab97ef29c85afa
    https://github.com/scummvm/scummvm/commit/ed484ae6d7a429171cf6d0dc50ab97ef29c85afa
Author: Joel Teichroeb (joel at teichroeb.net)
Date: 2026-11-11T06:30:19+01:00

Commit Message:
TINYGL: Clamp interpolated colours instead of letting them wrap

Stepping a colour across a triangle can take it a little below 0 or
above the maximum at the edge, and converting that to a byte wrapped it
to the other extreme. Keep the colours signed and clamp them instead.

Assisted-by: Claude:claude-opus-5-5

Changed paths:
    graphics/tinygl/zbuffer.cpp
    graphics/tinygl/zbuffer.h
    graphics/tinygl/ztriangle.cpp


diff --git a/graphics/tinygl/zbuffer.cpp b/graphics/tinygl/zbuffer.cpp
index 27ddb80cd24..0b8e883f330 100644
--- a/graphics/tinygl/zbuffer.cpp
+++ b/graphics/tinygl/zbuffer.cpp
@@ -285,9 +285,8 @@ static byte satAdd(byte a, byte b) {
 	return (byte)(r | -(r < a));
 }
 
-static byte sat16_to_8(uint32 x) {
-	x = (x + 128) >> 8; // rounding 16 to 8 
-	return (byte)(x | -!!(x >> 8)); // branchfree saturation
+static byte sat16_to_8(int v) {
+       return (byte)((CLIP(v, 0, 0xFF7F) + 128) >> 8);
 }
 
 static byte fpMul(byte a, byte b) {
@@ -300,7 +299,7 @@ static byte fpMul(byte a, byte b) {
 
 void FrameBuffer::applyTextureEnvironment(
 	int internalformat,
-	uint previousA, uint previousR, uint previousG, uint previousB,
+	int previousA, int previousR, int previousG, int previousB,
 	byte &texA, byte &texR, byte &texG, byte &texB)
 {
 	// summary notation is used from https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glTexEnv.xml
@@ -476,7 +475,7 @@ void FrameBuffer::applyTextureEnvironment(
 }
 
 void FrameBuffer::applyModulation(
-	uint previousA, uint previousR, uint previousG, uint previousB,
+	int previousA, int previousR, int previousG, int previousB,
 	byte &texA, byte &texR, byte &texG, byte &texB) {
 	texA = fpMul(sat16_to_8(previousA), texA);
 	texR = fpMul(sat16_to_8(previousR), texR);
diff --git a/graphics/tinygl/zbuffer.h b/graphics/tinygl/zbuffer.h
index 4a92156169c..cdd94ef0018 100644
--- a/graphics/tinygl/zbuffer.h
+++ b/graphics/tinygl/zbuffer.h
@@ -361,8 +361,8 @@ private:
 
 	template <bool kDepthWrite, bool kSmoothMode, bool kFogMode, bool kEnableAlphaTest, bool kEnableScissor, bool kEnableBlending, bool kStencilEnabled, bool kDepthTestEnabled>
 	void putPixelNoTexture(int fbOffset, uint *pz, byte *ps, int _a,
-	                       int x, int y, uint &z, uint &r, uint &g, uint &b, uint &a,
-	                       int &dzdx, int &drdx, int &dgdx, int &dbdx, uint dadx,
+	                       int x, int y, uint &z, int &r, int &g, int &b, int &a,
+	                       int &dzdx, int &drdx, int &dgdx, int &dbdx, int dadx,
 	                       uint &fog, int fog_r, int fog_g, int fog_b, int &dfdx,
 						   bool kStippleEnabled);
 
@@ -377,8 +377,8 @@ private:
 						 FrameBuffer::ColorMode colorMode,
 	                     uint wrap_s, uint wrap_t, uint *pz, byte *ps, int _a,
 	                     int x, int y, uint &z, int &t, int &s,
-	                     uint &r, uint &g, uint &b, uint &a,
-	                     int &dzdx, int &dsdx, int &dtdx, int &drdx, int &dgdx, int &dbdx, uint dadx,
+	                     int &r, int &g, int &b, int &a,
+	                     int &dzdx, int &dsdx, int &dtdx, int &drdx, int &dgdx, int &dbdx, int dadx,
 	                     uint &fog, int fog_r, int fog_g, int fog_b, int &dfdx);
 
 	template <bool kDepthWrite, bool kEnableScissor, bool kStencilEnabled, bool kDepthTestEnabled>
@@ -808,12 +808,12 @@ private:
 
 	void applyTextureEnvironment(
 		int internalformat,
-		uint previousA, uint previousR, uint previousG, uint previousB,
+		int previousA, int previousR, int previousG, int previousB,
 		byte &texA, byte &texR, byte &texG, byte &texB);
 
 	// the same as GL_TEXTURE_ENV_MODE == GL_MODULATE as fast-path for the default mode
 	void applyModulation(
-		uint previousA, uint previousR, uint previousG, uint previousB,
+		int previousA, int previousR, int previousG, int previousB,
 		byte &texA, byte &texR, byte &texG, byte &texB);
 
 	Buffer _offscreenBuffer;
diff --git a/graphics/tinygl/ztriangle.cpp b/graphics/tinygl/ztriangle.cpp
index 1aceeb49420..eb70733dc8d 100644
--- a/graphics/tinygl/ztriangle.cpp
+++ b/graphics/tinygl/ztriangle.cpp
@@ -45,10 +45,15 @@ static bool applyStipplePattern(int x, int y, const byte *stipple) {
 	return (stipple[byteIndex] & bitmask);
 }
 
+template <int kBits>
+static byte colorToByte(int v) {
+	return (byte)(CLIP(v, 0, (1 << kBits) - 1) >> (kBits - 8));
+}
+
 template <bool kDepthWrite, bool kSmoothMode, bool kFogMode, bool kEnableAlphaTest, bool kEnableScissor, bool kEnableBlending, bool kStencilEnabled, bool kDepthTestEnabled>
 void FrameBuffer::putPixelNoTexture(int fbOffset, uint *pz, byte *ps, int _a,
-                                    int x, int y, uint &z, uint &r, uint &g, uint &b, uint &a,
-                                    int &dzdx, int &drdx, int &dgdx, int &dbdx, uint dadx,
+                                    int x, int y, uint &z, int &r, int &g, int &b, int &a,
+                                    int &dzdx, int &drdx, int &dgdx, int &dbdx, int dadx,
                                     uint &fog, int fog_r, int fog_g, int fog_b, int &dfdx,
                                     bool stippleEnabled) {
 	bool useStippleColor = false;
@@ -85,10 +90,11 @@ void FrameBuffer::putPixelNoTexture(int fbOffset, uint *pz, byte *ps, int _a,
 			uint sg = (_stippleColor >> 8) & 0xFF;
 			uint sr = _stippleColor & 0xFF;
 			writePixel<kEnableAlphaTest, kEnableBlending, kDepthWrite, kFogMode>
-					(fbOffset + _a, a >> (ZB_POINT_ALPHA_BITS - 8), sr, sg, sb, z, fog, fog_r, fog_g, fog_b);
+					(fbOffset + _a, colorToByte<ZB_POINT_ALPHA_BITS>(a), sr, sg, sb, z, fog, fog_r, fog_g, fog_b);
 		} else {
 			writePixel<kEnableAlphaTest, kEnableBlending, kDepthWrite, kFogMode>
-					(fbOffset + _a, a >> (ZB_POINT_ALPHA_BITS - 8), r >> (ZB_POINT_RED_BITS - 8), g >> (ZB_POINT_GREEN_BITS - 8), b >> (ZB_POINT_BLUE_BITS - 8),
+					(fbOffset + _a, colorToByte<ZB_POINT_ALPHA_BITS>(a), colorToByte<ZB_POINT_RED_BITS>(r),
+					 colorToByte<ZB_POINT_GREEN_BITS>(g), colorToByte<ZB_POINT_BLUE_BITS>(b),
 					z, fog, fog_r, fog_g, fog_b);
 		}
 	}
@@ -110,8 +116,8 @@ void FrameBuffer::putPixelTexture(int fbOffset, const TexelBuffer *texture,
                                   FrameBuffer::ColorMode colorMode,
                                   uint wrap_s, uint wrap_t, uint *pz, byte *ps, int _a,
                                   int x, int y, uint &z, int &t, int &s,
-                                  uint &r, uint &g, uint &b, uint &a,
-                                  int &dzdx, int &dsdx, int &dtdx, int &drdx, int &dgdx, int &dbdx, uint dadx,
+                                  int &r, int &g, int &b, int &a,
+                                  int &dzdx, int &dsdx, int &dtdx, int &drdx, int &dgdx, int &dbdx, int dadx,
                                   uint &fog, int fog_r, int fog_g, int fog_b, int &dfdx) {
 	if (kEnableScissor && scissorPixel(x + _a, y)) {
 		goto end;
@@ -525,7 +531,8 @@ void FrameBuffer::fillTriangle(ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint
 				uint *pz = nullptr;
 				byte *ps = nullptr;
 				int pp;
-				uint z = 0, r, g, b, a, fog;
+				uint z = 0, fog;
+				int r, g, b, a;
 				int n = (x2 >> 16) - x1;
 				pp = pp1 + x1;
 				r = r1;
@@ -578,7 +585,8 @@ void FrameBuffer::fillTriangle(ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint
 				uint *pz = nullptr;
 				byte *ps = nullptr;
 				int s, t;
-				uint z = 0, r, g, b, a, fog;
+				uint z = 0, fog;
+				int r, g, b, a;
 				int n, pp;
 				float sz, tz, fz, zinv;
 				int dsdx, dtdx;




More information about the Scummvm-git-logs mailing list