diff --git a/GPU/Common/SplineCommon.cpp b/GPU/Common/SplineCommon.cpp index 6fe6436291..5e514b31d7 100644 --- a/GPU/Common/SplineCommon.cpp +++ b/GPU/Common/SplineCommon.cpp @@ -360,11 +360,11 @@ public: // Pre-tessellate U lines tess_pos.SampleU(wu.basis); - if (sampleCol) + if constexpr (sampleCol) tess_col.SampleU(wu.basis); - if (sampleTex) + if constexpr (sampleTex) tess_tex.SampleU(wu.basis); - if (sampleNrm) + if constexpr (sampleNrm) tess_nrm.SampleU(wu.deriv); for (int tile_v = start_v; tile_v <= surface.tess_v; ++tile_v) { @@ -375,24 +375,24 @@ public: // Tessellate vert.pos = tess_pos.SampleV(wv.basis); - if (sampleCol) { + if constexpr (sampleCol) { vert.color_32 = tess_col.SampleV(wv.basis).ToRGBA(); } else { vert.color_32 = points.defcolor; } - if (sampleTex) { + if constexpr (sampleTex) { tess_tex.SampleV(wv.basis).Write(vert.uv); } else { // Generate texcoord vert.uv[0] = patch_u + tile_u * inv_u; vert.uv[1] = patch_v + tile_v * inv_v; } - if (sampleNrm) { + if constexpr (sampleNrm) { const Vec3f derivU = tess_nrm.SampleV(wv.basis); const Vec3f derivV = tess_pos.SampleV(wv.deriv); vert.nrm = Cross(derivU, derivV).Normalized(useSSE4); - if (patchFacing) + if constexpr (patchFacing) vert.nrm *= -1.0f; } else { vert.nrm.SetZero(); diff --git a/GPU/Common/TextureCacheCommon.cpp b/GPU/Common/TextureCacheCommon.cpp index 6f9d235bc6..4984df18e8 100644 --- a/GPU/Common/TextureCacheCommon.cpp +++ b/GPU/Common/TextureCacheCommon.cpp @@ -1641,17 +1641,12 @@ static CheckAlphaResult DecodeDXTBlocks(uint8_t *out, int outPitch, uint32_t tex int blockHeight = std::min(h - y, 4); for (int x = 0; x < minw; x += 4) { int blockWidth = std::min(minw - x, 4); - switch (n) { - case 1: + if constexpr (n == 1) DecodeDXT1Block(dst + outPitch32 * y + x, (const DXT1Block *)src + blockIndex, outPitch32, blockWidth, blockHeight, &alphaSum); - break; - case 3: + else if constexpr (n == 3) DecodeDXT3Block(dst + outPitch32 * y + x, (const DXT3Block *)src + blockIndex, outPitch32, blockWidth, blockHeight); - break; - case 5: + else if constexpr (n == 5) DecodeDXT5Block(dst + outPitch32 * y + x, (const DXT5Block *)src + blockIndex, outPitch32, blockWidth, blockHeight); - break; - } blockIndex++; } } @@ -1660,7 +1655,7 @@ static CheckAlphaResult DecodeDXTBlocks(uint8_t *out, int outPitch, uint32_t tex ReverseColors(out, out, GE_TFMT_8888, outPitch32 * h); } - if (n == 1) { + if constexpr (n == 1) { return alphaSum == 1 ? CHECKALPHA_FULL : CHECKALPHA_ANY; } else { // Just report that we don't have full alpha, since these formats are made for that. diff --git a/GPU/Software/BinManager.h b/GPU/Software/BinManager.h index 116fb19e27..53db6435c2 100644 --- a/GPU/Software/BinManager.h +++ b/GPU/Software/BinManager.h @@ -65,7 +65,7 @@ struct BinQueue { } void Setup() { - items_ = (T *)AllocateAlignedMemory(sizeof(T) * N, 16); + items_ = (T *)AllocateAlignedMemory(sizeof_, 16); } void Reset() { @@ -152,6 +152,7 @@ struct BinQueue { std::atomic head_; std::atomic tail_ ; std::atomic size_; + static constexpr size_t sizeof_ = sizeof(T) * N; }; union BinClut { diff --git a/GPU/Software/DrawPixel.cpp b/GPU/Software/DrawPixel.cpp index eda04b0b41..6d6ba70ed9 100644 --- a/GPU/Software/DrawPixel.cpp +++ b/GPU/Software/DrawPixel.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2013- PPSSPP Project. +// Copyright (c) 2013- PPSSPP Project. // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -670,29 +670,33 @@ void SOFTRAST_CALL DrawSinglePixel(int x, int y, int z, int fog, Vec4IntArg colo if (z < pixelID.cached.minz || z > pixelID.cached.maxz) return; - if (pixelID.AlphaTestFunc() != GE_COMP_ALWAYS && !clearMode) - if (!AlphaTestPassed(pixelID, prim_color.a())) - return; + if constexpr (!clearMode) + if (pixelID.AlphaTestFunc() != GE_COMP_ALWAYS) + if (!AlphaTestPassed(pixelID, prim_color.a())) + return; // Fog is applied prior to color test. - if (pixelID.applyFog && !clearMode) { - Vec3 fogColor = Vec3::FromRGB(pixelID.cached.fogColor); - // This is very similar to the BLEND texfunc, and simply always rounds up. - static constexpr Vec3 roundup = Vec3::AssignToAll(255); - fogColor = (prim_color.rgb() * fog + fogColor * (255 - fog) + roundup) / 256; - prim_color.r() = fogColor.r(); - prim_color.g() = fogColor.g(); - prim_color.b() = fogColor.b(); + if constexpr (!clearMode) { + if (pixelID.applyFog) { + Vec3 fogColor = Vec3::FromRGB(pixelID.cached.fogColor); + // This is very similar to the BLEND texfunc, and simply always rounds up. + static constexpr Vec3 roundup = Vec3::AssignToAll(255); + fogColor = (prim_color.rgb() * fog + fogColor * (255 - fog) + roundup) / 256; + prim_color.r() = fogColor.r(); + prim_color.g() = fogColor.g(); + prim_color.b() = fogColor.b(); + } } - if (pixelID.colorTest && !clearMode) - if (!ColorTestPassed(pixelID, prim_color.rgb())) - return; + if constexpr (!clearMode) + if (pixelID.colorTest) + if (!ColorTestPassed(pixelID, prim_color.rgb())) + return; // In clear mode, it uses the alpha color as stencil. uint32_t targetWriteMask = pixelID.applyColorWriteMask ? pixelID.cached.colorWriteMask : 0; u8 stencil = clearMode ? prim_color.a() : GetPixelStencil(fbFormat, pixelID.cached.framebufStride, x, y); - if (clearMode) { + if constexpr (clearMode) { if (pixelID.DepthClear()) SetPixelDepth(x, y, pixelID.cached.depthbufStride, z); } else if (pixelID.stencilTest) { @@ -717,24 +721,27 @@ void SOFTRAST_CALL DrawSinglePixel(int x, int y, int z, int fog, Vec4IntArg colo } } - if (pixelID.depthWrite && !clearMode) - SetPixelDepth(x, y, pixelID.cached.depthbufStride, z); + if constexpr (!clearMode) + if (pixelID.depthWrite) + SetPixelDepth(x, y, pixelID.cached.depthbufStride, z); const u32 old_color = GetPixelColor(fbFormat, pixelID.cached.framebufStride, x, y); u32 new_color; // Dithering happens before the logic op and regardless of framebuffer format or clear mode. // We do it while alpha blending because it happens before clamping. - if (pixelID.alphaBlend && !clearMode) { - const Vec4 dst = Vec4::FromRGBA(old_color); - Vec3 blended = AlphaBlendingResult(pixelID, prim_color, dst); - if (pixelID.dithering) { - blended += Vec3::AssignToAll(pixelID.cached.ditherMatrix[(y & 3) * 4 + (x & 3)]); + if (pixelID.alphaBlend) { + if constexpr (!clearMode) { + const Vec4 dst = Vec4::FromRGBA(old_color); + Vec3 blended = AlphaBlendingResult(pixelID, prim_color, dst); + if (pixelID.dithering) { + blended += Vec3::AssignToAll(pixelID.cached.ditherMatrix[(y & 3) * 4 + (x & 3)]); + } + + // ToRGB() always automatically clamps. + new_color = blended.ToRGB(); + new_color |= stencil << 24; } - - // ToRGB() always automatically clamps. - new_color = blended.ToRGB(); - new_color |= stencil << 24; } else { if (pixelID.dithering) { // We'll discard alpha anyway. @@ -750,12 +757,14 @@ void SOFTRAST_CALL DrawSinglePixel(int x, int y, int z, int fog, Vec4IntArg colo } // Logic ops are applied after blending (if blending is enabled.) - if (pixelID.applyLogicOp && !clearMode) { - // Logic ops don't affect stencil, which happens inside ApplyLogicOp. - new_color = ApplyLogicOp(pixelID.cached.logicOp, old_color, new_color); + if (pixelID.applyLogicOp) { + if constexpr (!clearMode) { + // Logic ops don't affect stencil, which happens inside ApplyLogicOp. + new_color = ApplyLogicOp(pixelID.cached.logicOp, old_color, new_color); + } } - if (clearMode) { + if constexpr (clearMode) { if (!pixelID.ColorClear()) new_color = (new_color & 0xFF000000) | (old_color & 0x00FFFFFF); if (!pixelID.StencilClear()) diff --git a/GPU/Software/Rasterizer.cpp b/GPU/Software/Rasterizer.cpp index 391942fde7..19e50222ac 100644 --- a/GPU/Software/Rasterizer.cpp +++ b/GPU/Software/Rasterizer.cpp @@ -728,7 +728,7 @@ Vec4 TriangleEdge::Start(const ScreenCoords &v0, const ScreenCoord stepY = Vec4::AssignToAll(yf * SCREEN_SCALE_FACTOR * 2); #if defined(_M_SSE) && !PPSSPP_ARCH(X86) - if (useSSE4) + if constexpr (useSSE4) return TriangleEdgeStartSSE4(initX.ivec, initY.ivec, xf, yf, c); #endif return Vec4::AssignToAll(xf) * initX + Vec4::AssignToAll(yf) * initY + Vec4::AssignToAll(c); @@ -771,7 +771,7 @@ template void TriangleEdge::NarrowMinMaxX(const Vec4 &w, int64_t minX, int64_t &rowMinX, int64_t &rowMaxX) { int wmax; #if defined(_M_SSE) && !PPSSPP_ARCH(X86) - if (useSSE4) { + if constexpr (useSSE4) { wmax = MaxWeightSSE4(w.ivec); } else { wmax = std::max(std::max(w.x, w.y), std::max(w.z, w.w)); @@ -809,7 +809,7 @@ static inline __m128i SOFTRAST_CALL StepTimesSSE4(__m128i w, __m128i step, int c template inline Vec4 TriangleEdge::StepXTimes(const Vec4 &w, int c) { #if defined(_M_SSE) && !PPSSPP_ARCH(X86) - if (useSSE4) + if constexpr (useSSE4) return StepTimesSSE4(w.ivec, stepX.ivec, c); #elif PPSSPP_ARCH(ARM64_NEON) return vaddq_s32(w.ivec, vmulq_s32(vdupq_n_s32(c), stepX.ivec)); @@ -848,7 +848,7 @@ static inline bool SOFTRAST_CALL AnyMaskSSE4(__m128i mask) { template static inline bool AnyMask(const Vec4 &mask) { #if defined(_M_SSE) && !PPSSPP_ARCH(X86) - if (useSSE4) { + if constexpr (useSSE4) { return AnyMaskSSE4(mask.ivec); } @@ -1015,33 +1015,37 @@ void DrawTriangleSlice( } } - if (state.enableTextures && !clearMode) { - Vec4 s, t; - if (state.throughMode) { - s = Interpolate(v0.texturecoords.s(), v1.texturecoords.s(), v2.texturecoords.s(), w0, w1, w2, wsum_recip); - t = Interpolate(v0.texturecoords.t(), v1.texturecoords.t(), v2.texturecoords.t(), w0, w1, w2, wsum_recip); + if (state.enableTextures) { + if constexpr (!clearMode) { + Vec4 s, t; + if (state.throughMode) { + s = Interpolate(v0.texturecoords.s(), v1.texturecoords.s(), v2.texturecoords.s(), w0, w1, + w2, wsum_recip); + t = Interpolate(v0.texturecoords.t(), v1.texturecoords.t(), v2.texturecoords.t(), w0, w1, + w2, wsum_recip); - // For levels > 0, mipmapping is always based on level 0. Simpler to scale first. - s *= 1.0f / (float)(1 << state.samplerID.width0Shift); - t *= 1.0f / (float)(1 << state.samplerID.height0Shift); - } else if (state.textureProj) { - // Texture coordinate interpolation must definitely be perspective-correct. - GetTextureCoordinatesProj(v0, v1, v2, w0, w1, w2, wsum_recip, s, t); - } else { - // Texture coordinate interpolation must definitely be perspective-correct. - GetTextureCoordinates(v0, v1, v2, w0, w1, w2, wsum_recip, s, t); - } + // For levels > 0, mipmapping is always based on level 0. Simpler to scale first. + s *= 1.0f / (float) (1 << state.samplerID.width0Shift); + t *= 1.0f / (float) (1 << state.samplerID.height0Shift); + } else if (state.textureProj) { + // Texture coordinate interpolation must definitely be perspective-correct. + GetTextureCoordinatesProj(v0, v1, v2, w0, w1, w2, wsum_recip, s, t); + } else { + // Texture coordinate interpolation must definitely be perspective-correct. + GetTextureCoordinates(v0, v1, v2, w0, w1, w2, wsum_recip, s, t); + } - if (state.TexLevelMode() == GE_TEXLEVEL_MODE_SLOPE) { - // Not sure what's right, but we need one value for the slope. - float clipw = (v0.clipw * w0.x + v1.clipw * w1.x + v2.clipw * w2.x) * wsum_recip.x; - ApplyTexturing(state, prim_color, mask, s, t, clipw); - } else { - ApplyTexturing(state, prim_color, mask, s, t, 0.0f); - } + if (state.TexLevelMode() == GE_TEXLEVEL_MODE_SLOPE) { + // Not sure what's right, but we need one value for the slope. + float clipw = (v0.clipw * w0.x + v1.clipw * w1.x + v2.clipw * w2.x) * wsum_recip.x; + ApplyTexturing(state, prim_color, mask, s, t, clipw); + } else { + ApplyTexturing(state, prim_color, mask, s, t, 0.0f); + } + } } - if (!clearMode) { + if constexpr (!clearMode) { for (int i = 0; i < 4; ++i) { #if defined(_M_SSE) // TODO: Tried making Vec4 do this, but things got slower. diff --git a/GPU/Software/RasterizerRectangle.cpp b/GPU/Software/RasterizerRectangle.cpp index db14c3306e..16db7ad504 100644 --- a/GPU/Software/RasterizerRectangle.cpp +++ b/GPU/Software/RasterizerRectangle.cpp @@ -305,8 +305,9 @@ static inline void DrawSpriteTex(const DrawingCoords &pos0, const DrawingCoords template static void DrawSpriteNoTex(const DrawingCoords &pos0, const DrawingCoords &pos1, u32 color0, const RasterizerState &state) { - if (alphaBlend && Vec4::FromRGBA(color0).a() == 0) - return; + if constexpr (alphaBlend) + if (Vec4::FromRGBA(color0).a() == 0) + return; for (int y = pos0.y; y < pos1.y; y++) { if (fmt == GE_FORMAT_8888) { diff --git a/GPU/Software/Sampler.cpp b/GPU/Software/Sampler.cpp index eef759344f..6793f9a007 100644 --- a/GPU/Software/Sampler.cpp +++ b/GPU/Software/Sampler.cpp @@ -280,7 +280,7 @@ static inline int GetPixelDataOffset(uint32_t row_pitch_pixels, uint32_t u, uint const int tiles_in_block_horizontal = 4; const int tiles_in_block_vertical = 8; - int texels_per_tile = tile_size_bits / texel_size_bits; + constexpr int texels_per_tile = tile_size_bits / texel_size_bits; int tile_u = u / texels_per_tile; int tile_idx = (v % tiles_in_block_vertical) * (tiles_in_block_horizontal) + // TODO: not sure if the *texel_size_bits/8 factor is correct diff --git a/GPU/Software/TransformUnit.cpp b/GPU/Software/TransformUnit.cpp index e4ee125736..f17f12cf19 100644 --- a/GPU/Software/TransformUnit.cpp +++ b/GPU/Software/TransformUnit.cpp @@ -177,7 +177,7 @@ static ScreenCoords ClipToScreenInternal(Vec3f scaled, const ClipCoords &coords, const float SCREEN_BOUND = 4095.0f + (15.5f / 16.0f); // This matches hardware tests - depth is clamped when this flag is on. - if (depthClamp) { + if constexpr (depthClamp) { // Note: if the depth is clipped (z/w <= -1.0), the outside_range_flag should NOT be set, even for x and y. if ((alwaysCheckRange || coords.z > -coords.w) && (scaled.x >= SCREEN_BOUND || scaled.y >= SCREEN_BOUND || scaled.x < 0 || scaled.y < 0)) { *outside_range_flag = true;