From b64ee0e190ccd360f5f7fb04783f010613c4c7c2 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sun, 10 Sep 2023 06:46:31 +0100 Subject: [PATCH] Use `if constexpr` in clx/dun_render --- Source/engine/render/clx_render.cpp | 77 ++++++++++++++++++----------- Source/engine/render/dun_render.cpp | 24 ++++----- 2 files changed, 59 insertions(+), 42 deletions(-) diff --git a/Source/engine/render/clx_render.cpp b/Source/engine/render/clx_render.cpp index 25b02ab6d..cff554ee5 100644 --- a/Source/engine/render/clx_render.cpp +++ b/Source/engine/render/clx_render.cpp @@ -212,45 +212,47 @@ void DoRenderBackwards( template DVL_ALWAYS_INLINE DVL_ATTRIBUTE_HOT void RenderOutlineForPixel(uint8_t *dst, int dstPitch, uint8_t color) { - if (North) + if constexpr (North) dst[-dstPitch] = color; - if (West) + if constexpr (West) dst[-1] = color; - if (East) + if constexpr (East) dst[1] = color; - if (South) + if constexpr (South) dst[dstPitch] = color; } template DVL_ALWAYS_INLINE DVL_ATTRIBUTE_HOT void RenderOutlineForPixel(uint8_t *dst, int dstPitch, uint8_t srcColor, uint8_t color) { - if (SkipColorIndexZero && srcColor == 0) - return; + if constexpr (SkipColorIndexZero) { + if (srcColor == 0) + return; + } RenderOutlineForPixel(dst, dstPitch, color); } template DVL_ALWAYS_INLINE DVL_ATTRIBUTE_HOT void RenderOutlineForPixels(uint8_t *dst, int dstPitch, int width, uint8_t color) { - if (North) + if constexpr (North) std::memset(dst - dstPitch, color, width); - if (West && East) + if constexpr (West && East) std::memset(dst - 1, color, width + 2); - else if (West) + else if constexpr (West) std::memset(dst - 1, color, width); - else if (East) + else if constexpr (East) std::memset(dst + 1, color, width); - if (South) + if constexpr (South) std::memset(dst + dstPitch, color, width); } template DVL_ALWAYS_INLINE DVL_ATTRIBUTE_HOT void RenderOutlineForPixels(uint8_t *dst, int dstPitch, int width, const uint8_t *src, uint8_t color) { - if (SkipColorIndexZero) { + if constexpr (SkipColorIndexZero) { while (width-- > 0) RenderOutlineForPixel(dst++, dstPitch, *src++, color); } else { @@ -264,7 +266,7 @@ void RenderClxOutlinePixelsCheckFirstColumn( const uint8_t *src, uint8_t width, uint8_t color) { if (dstX == -1) { - if (Fill) { + if constexpr (Fill) { RenderOutlineForPixel( dst++, dstPitch, color); } else { @@ -274,7 +276,7 @@ void RenderClxOutlinePixelsCheckFirstColumn( --width; } if (width > 0) { - if (Fill) { + if constexpr (Fill) { RenderOutlineForPixel(dst++, dstPitch, color); } else { RenderOutlineForPixel(dst++, dstPitch, *src++, color); @@ -282,7 +284,7 @@ void RenderClxOutlinePixelsCheckFirstColumn( --width; } if (width > 0) { - if (Fill) { + if constexpr (Fill) { RenderOutlineForPixels(dst, dstPitch, width, color); } else { RenderOutlineForPixels(dst, dstPitch, width, src, color); @@ -300,7 +302,7 @@ void RenderClxOutlinePixelsCheckLastColumn( const int numSpecialPixels = (lastPixel ? 1 : 0) + (oobPixel ? 1 : 0); if (width > numSpecialPixels) { width -= numSpecialPixels; - if (Fill) { + if constexpr (Fill) { RenderOutlineForPixels(dst, dstPitch, width, color); } else { RenderOutlineForPixels(dst, dstPitch, width, src, color); @@ -309,14 +311,14 @@ void RenderClxOutlinePixelsCheckLastColumn( dst += width; } if (lastPixel) { - if (Fill) { + if constexpr (Fill) { RenderOutlineForPixel(dst++, dstPitch, color); } else { RenderOutlineForPixel(dst++, dstPitch, *src++, color); } } if (oobPixel) { - if (Fill) { + if constexpr (Fill) { RenderOutlineForPixel(dst, dstPitch, color); } else { RenderOutlineForPixel(dst, dstPitch, *src, color); @@ -329,16 +331,27 @@ void RenderClxOutlinePixels( uint8_t *dst, int dstPitch, int dstX, int dstW, const uint8_t *src, uint8_t width, uint8_t color) { - if (SkipColorIndexZero && Fill && *src == 0) - return; + if constexpr (SkipColorIndexZero && Fill) { + if (*src == 0) + return; + } - if (CheckFirstColumn && dstX <= 0) { - RenderClxOutlinePixelsCheckFirstColumn( - dst, dstPitch, dstX, src, width, color); - } else if (CheckLastColumn && dstX + width >= dstW) { - RenderClxOutlinePixelsCheckLastColumn( - dst, dstPitch, dstX, dstW, src, width, color); - } else if (Fill) { + if constexpr (CheckFirstColumn) { + if (dstX <= 0) { + RenderClxOutlinePixelsCheckFirstColumn( + dst, dstPitch, dstX, src, width, color); + return; + } + } + if constexpr (CheckLastColumn) { + if (dstX + width >= dstW) { + RenderClxOutlinePixelsCheckLastColumn( + dst, dstPitch, dstX, dstW, src, width, color); + return; + } + } + + if constexpr (Fill) { RenderOutlineForPixels(dst, dstPitch, width, color); } else { RenderOutlineForPixels(dst, dstPitch, width, src, color); @@ -370,7 +383,7 @@ const uint8_t *RenderClxOutlineRowClipped( // NOLINT(readability-function-cognit dst += w; }; - if (ClipWidth) { + if constexpr (ClipWidth) { auto remainingLeftClip = clipX.left - skipSize.xOffset; if (skipSize.xOffset > clipX.left) { position.x += static_cast(skipSize.xOffset - clipX.left); @@ -409,7 +422,11 @@ const uint8_t *RenderClxOutlineRowClipped( // NOLINT(readability-function-cognit if (IsClxOpaque(v)) { const bool fill = IsClxOpaqueFill(v); v = fill ? GetClxOpaqueFillWidth(v) : GetClxOpaquePixelsWidth(v); - renderPixels(fill, ClipWidth ? std::min(remainingWidth, static_cast(v)) : v); + if constexpr (ClipWidth) { + renderPixels(fill, std::min(remainingWidth, static_cast(v))); + } else { + renderPixels(fill, v); + } } else { dst += v; } @@ -417,7 +434,7 @@ const uint8_t *RenderClxOutlineRowClipped( // NOLINT(readability-function-cognit position.x += v; } - if (ClipWidth) { + if constexpr (ClipWidth) { remainingWidth += clipX.right; if (remainingWidth > 0) { skipSize.xOffset = static_cast(srcWidth) - remainingWidth; diff --git a/Source/engine/render/dun_render.cpp b/Source/engine/render/dun_render.cpp index f9129474c..56b50c1e9 100644 --- a/Source/engine/render/dun_render.cpp +++ b/Source/engine/render/dun_render.cpp @@ -214,7 +214,7 @@ DVL_ALWAYS_INLINE DVL_ATTRIBUTE_HOT void RenderLineTransparent(uint8_t *DVL_REST template DVL_ALWAYS_INLINE DVL_ATTRIBUTE_HOT void RenderLineTransparentOrOpaque(uint8_t *DVL_RESTRICT dst, const uint8_t *DVL_RESTRICT src, uint_fast8_t width, const uint8_t *DVL_RESTRICT tbl) { - if (Transparent) { + if constexpr (Transparent) { RenderLineTransparent(dst, src, width, tbl); } else { RenderLineOpaque(dst, src, width, tbl); @@ -224,12 +224,12 @@ DVL_ALWAYS_INLINE DVL_ATTRIBUTE_HOT void RenderLineTransparentOrOpaque(uint8_t * template DVL_ALWAYS_INLINE DVL_ATTRIBUTE_HOT void RenderLineTransparentAndOpaque(uint8_t *DVL_RESTRICT dst, const uint8_t *DVL_RESTRICT src, uint_fast8_t prefixWidth, uint_fast8_t width, const uint8_t *DVL_RESTRICT tbl) { - if (OpaquePrefix) { + if constexpr (OpaquePrefix) { RenderLineOpaque(dst, src, prefixWidth, tbl); - if (!SkipTransparentPixels) + if constexpr (!SkipTransparentPixels) RenderLineTransparent(dst + prefixWidth, src + prefixWidth, width - prefixWidth, tbl); } else { - if (!SkipTransparentPixels) + if constexpr (!SkipTransparentPixels) RenderLineTransparent(dst, src, prefixWidth, tbl); RenderLineOpaque(dst + prefixWidth, src + prefixWidth, width - prefixWidth, tbl); } @@ -238,21 +238,21 @@ DVL_ALWAYS_INLINE DVL_ATTRIBUTE_HOT void RenderLineTransparentAndOpaque(uint8_t template DVL_ALWAYS_INLINE DVL_ATTRIBUTE_HOT void RenderLine(uint8_t *DVL_RESTRICT dst, const uint8_t *DVL_RESTRICT src, uint_fast8_t n, const uint8_t *DVL_RESTRICT tbl, int8_t prefix) { - if (PrefixIncrement == 0) { + if constexpr (PrefixIncrement == 0) { RenderLineTransparentOrOpaque(dst, src, n, tbl); } else if (prefix >= static_cast(n)) { // We std::clamp the prefix to (0, n] and avoid calling `RenderLineTransparent/Opaque` with width=0. - if (OpaquePrefix) { + if constexpr (OpaquePrefix) { RenderLineOpaque(dst, src, n, tbl); } else { - if (!SkipTransparentPixels) + if constexpr (!SkipTransparentPixels) RenderLineTransparent(dst, src, n, tbl); } } else if (prefix <= 0) { - if (!OpaquePrefix) { + if constexpr (!OpaquePrefix) { RenderLineOpaque(dst, src, n, tbl); } else { - if (!SkipTransparentPixels) + if constexpr (!SkipTransparentPixels) RenderLineTransparent(dst, src, n, tbl); } } else { @@ -727,13 +727,13 @@ DVL_ALWAYS_INLINE DVL_ATTRIBUTE_HOT void RenderRightTriangle(uint8_t *DVL_RESTRI template DVL_ALWAYS_INLINE DVL_ATTRIBUTE_HOT void RenderTrapezoidUpperHalf(uint8_t *DVL_RESTRICT dst, uint16_t dstPitch, const uint8_t *DVL_RESTRICT src, const uint8_t *DVL_RESTRICT tbl) { - if (PrefixIncrement != 0) { + if constexpr (PrefixIncrement != 0) { // The first and the last line are always fully transparent/opaque (or vice-versa). // We handle them specially to avoid calling the blitter with width=0. const uint8_t *srcEnd = src + Width * TrapezoidUpperHeight; constexpr bool FirstLineIsTransparent = OpaquePrefix ^ (PrefixIncrement < 0); - if (FirstLineIsTransparent) { - if (!SkipTransparentPixels) + if constexpr (FirstLineIsTransparent) { + if constexpr (!SkipTransparentPixels) RenderLineTransparent(dst, src, Width, tbl); } else { RenderLineOpaque(dst, src, Width, tbl);