Skip to content

Commit

Permalink
Remove mulDiv255
Browse files Browse the repository at this point in the history
That appears to be significantly slower in x86
  • Loading branch information
JD557 committed Nov 27, 2024
1 parent d6d2175 commit cdb91f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ private[graphics] object Blitter {
val colorDest = dest.unsafeGetPixel(destX, destY)
val factor = 255 - colorSource.a
val color = Color(
Math.min(Color.mulDiv255(colorDest.r, factor) + colorSource.r, 255),
Math.min(Color.mulDiv255(colorDest.g, factor) + colorSource.g, 255),
Math.min(Color.mulDiv255(colorDest.b, factor) + colorSource.b, 255)
Math.min((colorDest.r * factor) / 255 + colorSource.r, 255),
Math.min((colorDest.g * factor) / 255 + colorSource.g, 255),
Math.min((colorDest.b * factor) / 255 + colorSource.b, 255)
)
dest.unsafePutPixel(destX, destY, color)
dx += 1
Expand Down Expand Up @@ -254,9 +254,9 @@ private[graphics] object Blitter {
val colorDest = dest.unsafeGetPixel(destX, destY)
val factor = 255 - colorSource.a
val color = Color(
Math.min(Color.mulDiv255(colorDest.r, factor) + colorSource.r, 255),
Math.min(Color.mulDiv255(colorDest.g, factor) + colorSource.g, 255),
Math.min(Color.mulDiv255(colorDest.b, factor) + colorSource.b, 255)
Math.min((colorDest.r * factor) / 255 + colorSource.r, 255),
Math.min((colorDest.g * factor) / 255 + colorSource.g, 255),
Math.min((colorDest.b * factor) / 255 + colorSource.b, 255)
)
dest.unsafePutPixel(destX, destY, color)
dx += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ object Color {
*/
def *(that: Color): Color =
Color(
mulDiv255(color.r, that.r),
mulDiv255(color.g, that.g),
mulDiv255(color.b, that.b)
color.r * that.r / 255,
color.g * that.g / 255,
color.b * that.b / 255
)

/** Combines this with another color by multiplying each RGB value (on the [0.0, 1.0] range).
Expand All @@ -123,9 +123,9 @@ object Color {
*/
def :*(that: Color): Color =
Color(
mulDiv255(color.r, that.r),
mulDiv255(color.g, that.g),
mulDiv255(color.b, that.b),
color.r * that.r / 255,
color.g * that.g / 255,
color.b * that.b / 255,
color.a
)

Expand All @@ -136,9 +136,9 @@ object Color {
*/
def *:(that: Color): Color =
Color(
mulDiv255(color.r, that.r),
mulDiv255(color.g, that.g),
mulDiv255(color.b, that.b),
color.r * that.r / 255,
color.g * that.g / 255,
color.b * that.b / 255,
that.a
)

Expand Down Expand Up @@ -225,12 +225,4 @@ object Color {
*/
def unapply(color: Color): Some[(Int, Int, Int)] =
Some(color.r, color.g, color.b)

/** Computes (x * y) / 255
* Based on https://github.com/libsdl-org/SDL/blob/bbc9c75618fbc60583123e03f31f4e856f2654bf/src/video/SDL_blit.h#L524-L530
*/
inline private[graphics] def mulDiv255(x: Int, y: Int): Int = {
val out = (x * y) + 1
(out + (out >> 8) >> 8)
}
}

0 comments on commit cdb91f5

Please sign in to comment.