From 4c944eaa41c1ad3f0d85af1cbe05ba87b4fe8554 Mon Sep 17 00:00:00 2001 From: Stephan Unverwerth Date: Thu, 23 Dec 2021 13:28:19 +0100 Subject: [PATCH] LibGL: Remove sampling code from Sampler2D Texture sampling now happens entirely in SoftGPU thus this class will now only be used to hold the sampler configuration. --- Userland/Libraries/LibGL/CMakeLists.txt | 1 - Userland/Libraries/LibGL/Tex/Sampler2D.cpp | 101 --------------------- Userland/Libraries/LibGL/Tex/Sampler2D.h | 13 --- Userland/Libraries/LibGL/Tex/Texture2D.h | 6 -- 4 files changed, 121 deletions(-) delete mode 100644 Userland/Libraries/LibGL/Tex/Sampler2D.cpp diff --git a/Userland/Libraries/LibGL/CMakeLists.txt b/Userland/Libraries/LibGL/CMakeLists.txt index 9894558b10..a58f3d2e1b 100644 --- a/Userland/Libraries/LibGL/CMakeLists.txt +++ b/Userland/Libraries/LibGL/CMakeLists.txt @@ -14,7 +14,6 @@ set(SOURCES GLVertexArrays.cpp SoftwareGLContext.cpp Tex/NameAllocator.cpp - Tex/Sampler2D.cpp Tex/Texture2D.cpp Tex/TextureUnit.cpp ) diff --git a/Userland/Libraries/LibGL/Tex/Sampler2D.cpp b/Userland/Libraries/LibGL/Tex/Sampler2D.cpp deleted file mode 100644 index be9886bfe2..0000000000 --- a/Userland/Libraries/LibGL/Tex/Sampler2D.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (c) 2021, Stephan Unverwerth - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include "Sampler2D.h" - -#include -#include - -namespace GL { - -static constexpr float wrap_repeat(float value) -{ - return value - floorf(value); -} - -static constexpr float wrap_clamp_to_edge(float value, int num_texels) -{ - float const clamp_limit = 1.f / (2 * num_texels); - return clamp(value, clamp_limit, 1.0f - clamp_limit); -} - -static constexpr float wrap_mirrored_repeat(float value, int num_texels) -{ - float integer = floorf(value); - float frac = value - integer; - bool iseven = fmodf(integer, 2.0f) == 0.0f; - return wrap_clamp_to_edge(iseven ? frac : 1 - frac, num_texels); -} - -static constexpr float wrap(float value, GLint mode, int num_texels) -{ - switch (mode) { - case GL_REPEAT: - return wrap_repeat(value); - - // FIXME: These clamp modes actually have slightly different behavior. Currently we use GL_CLAMP_TO_EDGE for all of them. - case GL_CLAMP: - case GL_CLAMP_TO_BORDER: - case GL_CLAMP_TO_EDGE: - return wrap_clamp_to_edge(value, num_texels); - - case GL_MIRRORED_REPEAT: - return wrap_mirrored_repeat(value, num_texels); - - default: - VERIFY_NOT_REACHED(); - } -} - -FloatVector4 Sampler2D::sample(FloatVector4 const& uv) const -{ - // FIXME: Calculate the correct mipmap level here, need to receive uv derivatives for that - unsigned lod = 0; - - MipMap const& mip = m_texture.mipmap(lod); - - if (mip.width() < 1 || mip.height() < 1) - return { 1, 1, 1, 1 }; - - float x = wrap(uv.x(), m_wrap_s_mode, mip.width()); - float y = wrap(uv.y(), m_wrap_t_mode, mip.height()); - - x *= mip.width(); - y *= mip.height(); - - // 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), static_cast(y)); - } else if (m_mag_filter == GL_LINEAR) { - // FIXME: Implement different sampling points for wrap modes other than GL_REPEAT - - x -= 0.5f; - y -= 0.5f; - - unsigned i0 = static_cast(x) % mip.width(); - unsigned j0 = static_cast(y) % mip.height(); - - unsigned i1 = (i0 + 1) % mip.width(); - unsigned j1 = (j0 + 1) % mip.height(); - - auto t0 = mip.texel(i0, j0); - auto t1 = mip.texel(i1, j0); - auto t2 = mip.texel(i0, j1); - auto t3 = mip.texel(i1, j1); - - float frac_x = x - floorf(x); - float frac_y = y - floorf(y); - float one_minus_frac_x = 1 - frac_x; - - 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(); - } -} - -} diff --git a/Userland/Libraries/LibGL/Tex/Sampler2D.h b/Userland/Libraries/LibGL/Tex/Sampler2D.h index eea80abb1d..e885e7b494 100644 --- a/Userland/Libraries/LibGL/Tex/Sampler2D.h +++ b/Userland/Libraries/LibGL/Tex/Sampler2D.h @@ -7,20 +7,11 @@ #pragma once #include -#include -#include namespace GL { -class Texture2D; - class Sampler2D final { public: - Sampler2D(Texture2D const& texture) - : m_texture(texture) - { - } - GLint min_filter() const { return m_min_filter; } GLint mag_filter() const { return m_mag_filter; } GLint wrap_s_mode() const { return m_wrap_s_mode; } @@ -31,11 +22,7 @@ public: void set_wrap_s_mode(GLint value) { m_wrap_s_mode = value; } void set_wrap_t_mode(GLint value) { m_wrap_t_mode = value; } - FloatVector4 sample(FloatVector4 const& uv) const; - private: - Texture2D const& m_texture; - GLint m_min_filter { GL_NEAREST_MIPMAP_LINEAR }; GLint m_mag_filter { GL_LINEAR }; GLint m_wrap_s_mode { GL_REPEAT }; diff --git a/Userland/Libraries/LibGL/Tex/Texture2D.h b/Userland/Libraries/LibGL/Tex/Texture2D.h index 9dcd3a2c28..e071a182a4 100644 --- a/Userland/Libraries/LibGL/Tex/Texture2D.h +++ b/Userland/Libraries/LibGL/Tex/Texture2D.h @@ -27,12 +27,6 @@ public: static constexpr u8 LOG2_MAX_TEXTURE_SIZE = 11; public: - Texture2D() - : m_sampler(*this) - { - } - ~Texture2D() { } - virtual bool is_texture_2d() const override { return true; } void upload_texture_data(GLuint lod, GLint internal_format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* pixels, GLsizei pixels_per_row, u8 byte_alignment);