Browse Source

Moved the odd functions that were only used once internally from scalar.h.

David Piuva 2 years ago
parent
commit
3765018ec7
3 changed files with 45 additions and 46 deletions
  1. 38 9
      Source/DFPSR/image/draw.cpp
  2. 0 37
      Source/DFPSR/math/scalar.h
  3. 7 0
      Source/DFPSR/render/Camera.h

+ 38 - 9
Source/DFPSR/image/draw.cpp

@@ -29,6 +29,35 @@
 
 
 using namespace dsr;
 using namespace dsr;
 
 
+// Preconditions:
+//   0 <= a <= 255
+//   0 <= b <= 255
+// Postconditions:
+//   Returns the normalized multiplication of a and b, where the 0..255 range represents decimal values from 0.0 to 1.0.
+//   The result may not be less than zero or larger than any of the inputs.
+// Examples:
+//   normalizedByteMultiplication(0, 0) = 0
+//   normalizedByteMultiplication(x, 0) = 0
+//   normalizedByteMultiplication(0, x) = 0
+//   normalizedByteMultiplication(x, 255) = x
+//   normalizedByteMultiplication(255, x) = x
+//   normalizedByteMultiplication(255, 255) = 255
+static inline uint32_t normalizedByteMultiplication(uint32_t a, uint32_t b) {
+	// Approximate the reciprocal of an unsigned byte's maximum value 255 for normalization
+	//   256³ / 255 ≈ 65793
+	// Truncation goes down, so add half a unit before rounding to get the closest value
+	//   2^24 / 2 = 8388608
+	// No overflow for unsigned 32-bit integers
+	//   255² * 65793 + 8388608 = 4286578433 < 2^32
+	return (a * b * 65793 + 8388608) >> 24;
+}
+
+// True iff high and low bytes are equal
+//   Equivalent to value % 257 == 0 because A + B * 256 = A * 257 when A = B.
+inline bool isUniformByteU16(uint16_t value) {
+	return (value & 0x00FF) == ((value & 0xFF00) >> 8);
+}
+
 // -------------------------------- Drawing shapes --------------------------------
 // -------------------------------- Drawing shapes --------------------------------
 
 
 template <typename COLOR_TYPE>
 template <typename COLOR_TYPE>
@@ -568,10 +597,10 @@ void dsr::imageImpl_drawAlphaFilter(ImageRgbaU8Impl& target, const ImageRgbaU8Im
 					targetPixel[target.packOrder.alphaIndex] = 255;
 					targetPixel[target.packOrder.alphaIndex] = 255;
 				} else {
 				} else {
 					uint32_t targetRatio = 255 - sourceRatio;
 					uint32_t targetRatio = 255 - sourceRatio;
-					targetPixel[target.packOrder.redIndex]   = mulByte_8(targetPixel[target.packOrder.redIndex], targetRatio) + mulByte_8(sourcePixel[source.packOrder.redIndex], sourceRatio);
-					targetPixel[target.packOrder.greenIndex] = mulByte_8(targetPixel[target.packOrder.greenIndex], targetRatio) + mulByte_8(sourcePixel[source.packOrder.greenIndex], sourceRatio);
-					targetPixel[target.packOrder.blueIndex]  = mulByte_8(targetPixel[target.packOrder.blueIndex], targetRatio) + mulByte_8(sourcePixel[source.packOrder.blueIndex], sourceRatio);
-					targetPixel[target.packOrder.alphaIndex] = mulByte_8(targetPixel[target.packOrder.alphaIndex], targetRatio) + sourceRatio;
+					targetPixel[target.packOrder.redIndex]   = normalizedByteMultiplication(targetPixel[target.packOrder.redIndex], targetRatio) + normalizedByteMultiplication(sourcePixel[source.packOrder.redIndex], sourceRatio);
+					targetPixel[target.packOrder.greenIndex] = normalizedByteMultiplication(targetPixel[target.packOrder.greenIndex], targetRatio) + normalizedByteMultiplication(sourcePixel[source.packOrder.greenIndex], sourceRatio);
+					targetPixel[target.packOrder.blueIndex]  = normalizedByteMultiplication(targetPixel[target.packOrder.blueIndex], targetRatio) + normalizedByteMultiplication(sourcePixel[source.packOrder.blueIndex], sourceRatio);
+					targetPixel[target.packOrder.alphaIndex] = normalizedByteMultiplication(targetPixel[target.packOrder.alphaIndex], targetRatio) + sourceRatio;
 				}
 				}
 			}
 			}
 		);
 		);
@@ -636,7 +665,7 @@ static void drawSilhouette_template(ImageRgbaU8Impl& target, const ImageU8Impl&
 			if (FULL_ALPHA) {
 			if (FULL_ALPHA) {
 				sourceRatio = *sourcePixel;
 				sourceRatio = *sourcePixel;
 			} else {
 			} else {
-				sourceRatio = mulByte_8(*sourcePixel, color.alpha);
+				sourceRatio = normalizedByteMultiplication(*sourcePixel, color.alpha);
 			}
 			}
 			if (sourceRatio > 0) {
 			if (sourceRatio > 0) {
 				if (sourceRatio == 255) {
 				if (sourceRatio == 255) {
@@ -646,10 +675,10 @@ static void drawSilhouette_template(ImageRgbaU8Impl& target, const ImageU8Impl&
 					targetPixel[target.packOrder.alphaIndex] = 255;
 					targetPixel[target.packOrder.alphaIndex] = 255;
 				} else {
 				} else {
 					uint32_t targetRatio = 255 - sourceRatio;
 					uint32_t targetRatio = 255 - sourceRatio;
-					targetPixel[target.packOrder.redIndex]   = mulByte_8(targetPixel[target.packOrder.redIndex], targetRatio) + mulByte_8(color.red, sourceRatio);
-					targetPixel[target.packOrder.greenIndex] = mulByte_8(targetPixel[target.packOrder.greenIndex], targetRatio) + mulByte_8(color.green, sourceRatio);
-					targetPixel[target.packOrder.blueIndex]  = mulByte_8(targetPixel[target.packOrder.blueIndex], targetRatio) + mulByte_8(color.blue, sourceRatio);
-					targetPixel[target.packOrder.alphaIndex] = mulByte_8(targetPixel[target.packOrder.alphaIndex], targetRatio) + sourceRatio;
+					targetPixel[target.packOrder.redIndex]   = normalizedByteMultiplication(targetPixel[target.packOrder.redIndex], targetRatio) + normalizedByteMultiplication(color.red, sourceRatio);
+					targetPixel[target.packOrder.greenIndex] = normalizedByteMultiplication(targetPixel[target.packOrder.greenIndex], targetRatio) + normalizedByteMultiplication(color.green, sourceRatio);
+					targetPixel[target.packOrder.blueIndex]  = normalizedByteMultiplication(targetPixel[target.packOrder.blueIndex], targetRatio) + normalizedByteMultiplication(color.blue, sourceRatio);
+					targetPixel[target.packOrder.alphaIndex] = normalizedByteMultiplication(targetPixel[target.packOrder.alphaIndex], targetRatio) + sourceRatio;
 				}
 				}
 			}
 			}
 		);
 		);

+ 0 - 37
Source/DFPSR/math/scalar.h

@@ -50,29 +50,6 @@ inline T max(const T &a, const T &b, TAIL... tail) {
 	return max(max(a, b), tail...);
 	return max(max(a, b), tail...);
 }
 }
 
 
-// Preconditions:
-//   0 <= a <= 255
-//   0 <= b <= 255
-// Postconditions:
-//   Returns the normalized multiplication of a and b, where the 0..255 range represents decimal values from 0.0 to 1.0.
-//   The result may not be less than zero or larger than any of the inputs.
-// Examples:
-//   mulByte_8(0, 0) = 0
-//   mulByte_8(x, 0) = 0
-//   mulByte_8(0, x) = 0
-//   mulByte_8(x, 255) = x
-//   mulByte_8(255, x) = x
-//   mulByte_8(255, 255) = 255
-static inline uint32_t mulByte_8(uint32_t a, uint32_t b) {
-	// Approximate the reciprocal of an unsigned byte's maximum value 255 for normalization
-	//   256³ / 255 ≈ 65793
-	// Truncation goes down, so add half a unit before rounding to get the closest value
-	//   2^24 / 2 = 8388608
-	// No overflow for unsigned 32-bit integers
-	//   255² * 65793 + 8388608 = 4286578433 < 2^32
-	return (a * b * 65793 + 8388608) >> 24;
-}
-
 // Returns a modulo b where 0 <= a < b
 // Returns a modulo b where 0 <= a < b
 inline int signedModulo(int a, int b) {
 inline int signedModulo(int a, int b) {
 	int result = 0;
 	int result = 0;
@@ -143,20 +120,6 @@ inline void replaceWithLarger(T& target, T source) {
 	}
 	}
 }
 }
 
 
-// True iff high and low bytes are equal
-//   Equivalent to value % 257 == 0 because A + B * 256 = A * 257 when A = B.
-inline bool isUniformByteU16(uint16_t value) {
-	return (value & 0x00FF) == ((value & 0xFF00) >> 8);
-}
-
-// A special rounding used for triangle rasterization
-inline int64_t safeRoundInt64(float value) {
-	int64_t result = floor(value);
-	if (value <= -1048576.0f || value >= 1048576.0f) { result = 0; }
-	if (value < 0.0f) { result--; }
-	return result;
-}
-
 }
 }
 
 
 #endif
 #endif

+ 7 - 0
Source/DFPSR/render/Camera.h

@@ -37,6 +37,13 @@
 
 
 namespace dsr {
 namespace dsr {
 
 
+// A special rounding used for vertex projection
+inline int64_t safeRoundInt64(float value) {
+	int64_t result = (int64_t)value;
+	if (value <= -1048576.0f || value >= 1048576.0f) { result = 0; }
+	return result;
+}
+
 class ViewFrustum {
 class ViewFrustum {
 private:
 private:
 	FPlane3D planes[6];
 	FPlane3D planes[6];