Browse Source

LibGL: Implement `GL_TEXTURE_LOD_BIAS` for texture objects

Jelle Raaijmakers 2 years ago
parent
commit
eda1ffba73

+ 5 - 0
Userland/Libraries/LibGL/Tex/Texture.h

@@ -1,5 +1,6 @@
 /*
 /*
  * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
+ * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
  * Copyright (c) 2022, the SerenityOS developers.
  * Copyright (c) 2022, the SerenityOS developers.
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
@@ -25,8 +26,12 @@ public:
     RefPtr<GPU::Image> device_image() { return m_device_image; }
     RefPtr<GPU::Image> device_image() { return m_device_image; }
     void set_device_image(RefPtr<GPU::Image> image) { m_device_image = image; }
     void set_device_image(RefPtr<GPU::Image> image) { m_device_image = image; }
 
 
+    float level_of_detail_bias() const { return m_level_of_detail_bias; }
+    void set_level_of_detail_bias(float level_of_detail_bias) { m_level_of_detail_bias = level_of_detail_bias; }
+
 private:
 private:
     RefPtr<GPU::Image> m_device_image;
     RefPtr<GPU::Image> m_device_image;
+    float m_level_of_detail_bias { 0.f };
 };
 };
 
 
 }
 }

+ 5 - 1
Userland/Libraries/LibGL/Texture.cpp

@@ -588,6 +588,7 @@ void GLContext::gl_tex_parameter(GLenum target, GLenum pname, GLfloat param)
 
 
     // FIXME: implement the remaining parameters. (https://docs.gl/gl2/glTexParameter)
     // FIXME: implement the remaining parameters. (https://docs.gl/gl2/glTexParameter)
     RETURN_WITH_ERROR_IF(pname != GL_GENERATE_MIPMAP
     RETURN_WITH_ERROR_IF(pname != GL_GENERATE_MIPMAP
+            && pname != GL_TEXTURE_LOD_BIAS
             && pname != GL_TEXTURE_MIN_FILTER
             && pname != GL_TEXTURE_MIN_FILTER
             && pname != GL_TEXTURE_MAG_FILTER
             && pname != GL_TEXTURE_MAG_FILTER
             && pname != GL_TEXTURE_WRAP_S
             && pname != GL_TEXTURE_WRAP_S
@@ -603,6 +604,9 @@ void GLContext::gl_tex_parameter(GLenum target, GLenum pname, GLfloat param)
         RETURN_WITH_ERROR_IF(param != GL_TRUE && param != GL_FALSE, GL_INVALID_ENUM);
         RETURN_WITH_ERROR_IF(param != GL_TRUE && param != GL_FALSE, GL_INVALID_ENUM);
         texture_2d->set_generate_mipmaps(param == GL_TRUE);
         texture_2d->set_generate_mipmaps(param == GL_TRUE);
         break;
         break;
+    case GL_TEXTURE_LOD_BIAS:
+        texture_2d->set_level_of_detail_bias(param);
+        break;
     case GL_TEXTURE_MIN_FILTER:
     case GL_TEXTURE_MIN_FILTER:
         RETURN_WITH_ERROR_IF(!(param == GL_NEAREST
         RETURN_WITH_ERROR_IF(!(param == GL_NEAREST
                                  || param == GL_LINEAR
                                  || param == GL_LINEAR
@@ -733,7 +737,7 @@ void GLContext::sync_device_sampler_config()
         auto texture_2d = texture_unit.texture_2d_target_texture();
         auto texture_2d = texture_unit.texture_2d_target_texture();
         VERIFY(!texture_2d.is_null());
         VERIFY(!texture_2d.is_null());
         config.bound_image = texture_2d->device_image();
         config.bound_image = texture_2d->device_image();
-        config.level_of_detail_bias = texture_unit.level_of_detail_bias();
+        config.level_of_detail_bias = texture_2d->level_of_detail_bias() + texture_unit.level_of_detail_bias();
 
 
         auto const& sampler = texture_2d->sampler();
         auto const& sampler = texture_2d->sampler();
 
 

+ 0 - 1
Userland/Libraries/LibSoftGPU/Sampler.cpp

@@ -134,7 +134,6 @@ Vector4<AK::SIMD::f32x4> Sampler::sample_2d(Vector2<AK::SIMD::f32x4> const& uv)
     if (m_config.mipmap_filter == GPU::MipMapFilter::None)
     if (m_config.mipmap_filter == GPU::MipMapFilter::None)
         return sample_2d_lod(uv, expand4(base_level), m_config.texture_min_filter);
         return sample_2d_lod(uv, expand4(base_level), m_config.texture_min_filter);
 
 
-    // FIXME: add texture-level support for GL_TEXTURE_LOD_BIAS; below is only texture unit-level
     auto texture_lod_bias = AK::clamp(m_config.level_of_detail_bias, -MAX_TEXTURE_LOD_BIAS, MAX_TEXTURE_LOD_BIAS);
     auto texture_lod_bias = AK::clamp(m_config.level_of_detail_bias, -MAX_TEXTURE_LOD_BIAS, MAX_TEXTURE_LOD_BIAS);
     // FIXME: Instead of clamping to num_levels - 1, actually make the max mipmap level configurable with glTexParameteri(GL_TEXTURE_MAX_LEVEL, max_level)
     // FIXME: Instead of clamping to num_levels - 1, actually make the max mipmap level configurable with glTexParameteri(GL_TEXTURE_MAX_LEVEL, max_level)
     auto min_level = expand4(static_cast<float>(base_level));
     auto min_level = expand4(static_cast<float>(base_level));