Skip to content

Commit 3765018

Browse files
committed
Moved the odd functions that were only used once internally from scalar.h.
1 parent 2c72251 commit 3765018

File tree

3 files changed

+45
-46
lines changed

3 files changed

+45
-46
lines changed

Source/DFPSR/image/draw.cpp

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,35 @@
2929

3030
using namespace dsr;
3131

32+
// Preconditions:
33+
// 0 <= a <= 255
34+
// 0 <= b <= 255
35+
// Postconditions:
36+
// Returns the normalized multiplication of a and b, where the 0..255 range represents decimal values from 0.0 to 1.0.
37+
// The result may not be less than zero or larger than any of the inputs.
38+
// Examples:
39+
// normalizedByteMultiplication(0, 0) = 0
40+
// normalizedByteMultiplication(x, 0) = 0
41+
// normalizedByteMultiplication(0, x) = 0
42+
// normalizedByteMultiplication(x, 255) = x
43+
// normalizedByteMultiplication(255, x) = x
44+
// normalizedByteMultiplication(255, 255) = 255
45+
static inline uint32_t normalizedByteMultiplication(uint32_t a, uint32_t b) {
46+
// Approximate the reciprocal of an unsigned byte's maximum value 255 for normalization
47+
// 256³ / 255 ≈ 65793
48+
// Truncation goes down, so add half a unit before rounding to get the closest value
49+
// 2^24 / 2 = 8388608
50+
// No overflow for unsigned 32-bit integers
51+
// 255² * 65793 + 8388608 = 4286578433 < 2^32
52+
return (a * b * 65793 + 8388608) >> 24;
53+
}
54+
55+
// True iff high and low bytes are equal
56+
// Equivalent to value % 257 == 0 because A + B * 256 = A * 257 when A = B.
57+
inline bool isUniformByteU16(uint16_t value) {
58+
return (value & 0x00FF) == ((value & 0xFF00) >> 8);
59+
}
60+
3261
// -------------------------------- Drawing shapes --------------------------------
3362

3463
template <typename COLOR_TYPE>
@@ -568,10 +597,10 @@ void dsr::imageImpl_drawAlphaFilter(ImageRgbaU8Impl& target, const ImageRgbaU8Im
568597
targetPixel[target.packOrder.alphaIndex] = 255;
569598
} else {
570599
uint32_t targetRatio = 255 - sourceRatio;
571-
targetPixel[target.packOrder.redIndex] = mulByte_8(targetPixel[target.packOrder.redIndex], targetRatio) + mulByte_8(sourcePixel[source.packOrder.redIndex], sourceRatio);
572-
targetPixel[target.packOrder.greenIndex] = mulByte_8(targetPixel[target.packOrder.greenIndex], targetRatio) + mulByte_8(sourcePixel[source.packOrder.greenIndex], sourceRatio);
573-
targetPixel[target.packOrder.blueIndex] = mulByte_8(targetPixel[target.packOrder.blueIndex], targetRatio) + mulByte_8(sourcePixel[source.packOrder.blueIndex], sourceRatio);
574-
targetPixel[target.packOrder.alphaIndex] = mulByte_8(targetPixel[target.packOrder.alphaIndex], targetRatio) + sourceRatio;
600+
targetPixel[target.packOrder.redIndex] = normalizedByteMultiplication(targetPixel[target.packOrder.redIndex], targetRatio) + normalizedByteMultiplication(sourcePixel[source.packOrder.redIndex], sourceRatio);
601+
targetPixel[target.packOrder.greenIndex] = normalizedByteMultiplication(targetPixel[target.packOrder.greenIndex], targetRatio) + normalizedByteMultiplication(sourcePixel[source.packOrder.greenIndex], sourceRatio);
602+
targetPixel[target.packOrder.blueIndex] = normalizedByteMultiplication(targetPixel[target.packOrder.blueIndex], targetRatio) + normalizedByteMultiplication(sourcePixel[source.packOrder.blueIndex], sourceRatio);
603+
targetPixel[target.packOrder.alphaIndex] = normalizedByteMultiplication(targetPixel[target.packOrder.alphaIndex], targetRatio) + sourceRatio;
575604
}
576605
}
577606
);
@@ -636,7 +665,7 @@ static void drawSilhouette_template(ImageRgbaU8Impl& target, const ImageU8Impl&
636665
if (FULL_ALPHA) {
637666
sourceRatio = *sourcePixel;
638667
} else {
639-
sourceRatio = mulByte_8(*sourcePixel, color.alpha);
668+
sourceRatio = normalizedByteMultiplication(*sourcePixel, color.alpha);
640669
}
641670
if (sourceRatio > 0) {
642671
if (sourceRatio == 255) {
@@ -646,10 +675,10 @@ static void drawSilhouette_template(ImageRgbaU8Impl& target, const ImageU8Impl&
646675
targetPixel[target.packOrder.alphaIndex] = 255;
647676
} else {
648677
uint32_t targetRatio = 255 - sourceRatio;
649-
targetPixel[target.packOrder.redIndex] = mulByte_8(targetPixel[target.packOrder.redIndex], targetRatio) + mulByte_8(color.red, sourceRatio);
650-
targetPixel[target.packOrder.greenIndex] = mulByte_8(targetPixel[target.packOrder.greenIndex], targetRatio) + mulByte_8(color.green, sourceRatio);
651-
targetPixel[target.packOrder.blueIndex] = mulByte_8(targetPixel[target.packOrder.blueIndex], targetRatio) + mulByte_8(color.blue, sourceRatio);
652-
targetPixel[target.packOrder.alphaIndex] = mulByte_8(targetPixel[target.packOrder.alphaIndex], targetRatio) + sourceRatio;
678+
targetPixel[target.packOrder.redIndex] = normalizedByteMultiplication(targetPixel[target.packOrder.redIndex], targetRatio) + normalizedByteMultiplication(color.red, sourceRatio);
679+
targetPixel[target.packOrder.greenIndex] = normalizedByteMultiplication(targetPixel[target.packOrder.greenIndex], targetRatio) + normalizedByteMultiplication(color.green, sourceRatio);
680+
targetPixel[target.packOrder.blueIndex] = normalizedByteMultiplication(targetPixel[target.packOrder.blueIndex], targetRatio) + normalizedByteMultiplication(color.blue, sourceRatio);
681+
targetPixel[target.packOrder.alphaIndex] = normalizedByteMultiplication(targetPixel[target.packOrder.alphaIndex], targetRatio) + sourceRatio;
653682
}
654683
}
655684
);

Source/DFPSR/math/scalar.h

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,6 @@ inline T max(const T &a, const T &b, TAIL... tail) {
5050
return max(max(a, b), tail...);
5151
}
5252

53-
// Preconditions:
54-
// 0 <= a <= 255
55-
// 0 <= b <= 255
56-
// Postconditions:
57-
// Returns the normalized multiplication of a and b, where the 0..255 range represents decimal values from 0.0 to 1.0.
58-
// The result may not be less than zero or larger than any of the inputs.
59-
// Examples:
60-
// mulByte_8(0, 0) = 0
61-
// mulByte_8(x, 0) = 0
62-
// mulByte_8(0, x) = 0
63-
// mulByte_8(x, 255) = x
64-
// mulByte_8(255, x) = x
65-
// mulByte_8(255, 255) = 255
66-
static inline uint32_t mulByte_8(uint32_t a, uint32_t b) {
67-
// Approximate the reciprocal of an unsigned byte's maximum value 255 for normalization
68-
// 256³ / 255 ≈ 65793
69-
// Truncation goes down, so add half a unit before rounding to get the closest value
70-
// 2^24 / 2 = 8388608
71-
// No overflow for unsigned 32-bit integers
72-
// 255² * 65793 + 8388608 = 4286578433 < 2^32
73-
return (a * b * 65793 + 8388608) >> 24;
74-
}
75-
7653
// Returns a modulo b where 0 <= a < b
7754
inline int signedModulo(int a, int b) {
7855
int result = 0;
@@ -143,20 +120,6 @@ inline void replaceWithLarger(T& target, T source) {
143120
}
144121
}
145122

146-
// True iff high and low bytes are equal
147-
// Equivalent to value % 257 == 0 because A + B * 256 = A * 257 when A = B.
148-
inline bool isUniformByteU16(uint16_t value) {
149-
return (value & 0x00FF) == ((value & 0xFF00) >> 8);
150-
}
151-
152-
// A special rounding used for triangle rasterization
153-
inline int64_t safeRoundInt64(float value) {
154-
int64_t result = floor(value);
155-
if (value <= -1048576.0f || value >= 1048576.0f) { result = 0; }
156-
if (value < 0.0f) { result--; }
157-
return result;
158-
}
159-
160123
}
161124

162125
#endif

Source/DFPSR/render/Camera.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737

3838
namespace dsr {
3939

40+
// A special rounding used for vertex projection
41+
inline int64_t safeRoundInt64(float value) {
42+
int64_t result = (int64_t)value;
43+
if (value <= -1048576.0f || value >= 1048576.0f) { result = 0; }
44+
return result;
45+
}
46+
4047
class ViewFrustum {
4148
private:
4249
FPlane3D planes[6];

0 commit comments

Comments
 (0)