From 39ff1459f81b484eec888ab82bf941a404f8d982 Mon Sep 17 00:00:00 2001 From: Stephan Unverwerth Date: Mon, 16 Aug 2021 19:08:16 +0200 Subject: [PATCH] LibGL: Improve texture sampling performance GL_NEAREST: Remove unnecessary modulo. UV is always in range due to wrapping. GL_LINEAR: Rewrite filter equation to save a few math operations. --- Userland/Libraries/LibGL/Tex/Sampler2D.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibGL/Tex/Sampler2D.cpp b/Userland/Libraries/LibGL/Tex/Sampler2D.cpp index d72fc0fe82..caf3efc1cb 100644 --- a/Userland/Libraries/LibGL/Tex/Sampler2D.cpp +++ b/Userland/Libraries/LibGL/Tex/Sampler2D.cpp @@ -68,7 +68,7 @@ FloatVector4 Sampler2D::sample(FloatVector2 const& uv) const // Sampling implemented according to https://www.khronos.org/registry/OpenGL/specs/gl/glspec121.pdf Chapter 3.8 if (m_mag_filter == GL_NEAREST) { - return mip.texel(static_cast(x) % mip.width(), static_cast(y) % mip.height()); + return mip.texel(static_cast(x), static_cast(y)); } else if (m_mag_filter == GL_LINEAR) { // FIXME: Implement different sampling points for wrap modes other than GL_REPEAT @@ -88,8 +88,11 @@ FloatVector4 Sampler2D::sample(FloatVector2 const& uv) const float frac_x = x - floorf(x); float frac_y = y - floorf(y); + float one_minus_frac_x = 1 - frac_x; - return t0 * (1 - frac_x) * (1 - frac_y) + t1 * frac_x * (1 - frac_y) + t2 * (1 - frac_x) * frac_y + t3 * frac_x * frac_y; + auto h1 = t0 * one_minus_frac_x + t1 * frac_x; + auto h2 = t2 * one_minus_frac_x + t3 * frac_x; + return h1 * (1 - frac_y) + h2 * frac_y; } else { VERIFY_NOT_REACHED(); }