1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:17:44 +00:00

LibGL+LibSoftGPU: Use device samplers for rendering

We now sample textures from the device owned image samplers.
Passing of enabled texture units has been simplified by only passing a
list of texture unit indices.
This commit is contained in:
Stephan Unverwerth 2021-12-22 22:14:18 +01:00 committed by Brian Gianforcaro
parent f69de5e850
commit d8c17c8838
5 changed files with 19 additions and 30 deletions

View file

@ -224,14 +224,14 @@ void SoftwareGLContext::gl_end()
RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
}
m_bound_texture_units.clear();
for (auto& texture_unit : m_texture_units) {
if (texture_unit.is_bound())
m_bound_texture_units.append(texture_unit);
Vector<size_t, 32> enabled_texture_units;
for (size_t i = 0; i < m_texture_units.size(); ++i) {
if (m_texture_units[i].texture_2d_enabled())
enabled_texture_units.append(i);
}
sync_device_config();
m_rasterizer.draw_primitives(m_current_draw_mode, m_projection_matrix * m_model_view_matrix, m_texture_matrix, m_vertex_list, m_bound_texture_units);
m_rasterizer.draw_primitives(m_current_draw_mode, m_projection_matrix * m_model_view_matrix, m_texture_matrix, m_vertex_list, enabled_texture_units);
m_vertex_list.clear_with_capacity();
}

View file

@ -235,7 +235,6 @@ private:
HashMap<GLuint, RefPtr<Texture>> m_allocated_textures;
Array<TextureUnit, 32> m_texture_units;
TextureUnit* m_active_texture_unit { &m_texture_units[0] };
TextureUnit::BoundList m_bound_texture_units;
SoftGPU::Device m_rasterizer;
bool m_sampler_config_is_dirty { true };

View file

@ -36,9 +36,6 @@ public:
bool texture_cube_map_enabled() const { return m_texture_cube_map_enabled; };
void set_texture_cube_map_enabled(bool texture_cube_map_enabled) { m_texture_cube_map_enabled = texture_cube_map_enabled; }
IntrusiveListNode<TextureUnit> m_bound_node;
using BoundList = IntrusiveList<&TextureUnit::m_bound_node>;
private:
mutable RefPtr<Texture2D> m_texture_target_2d { nullptr };
mutable RefPtr<Texture> m_currently_bound_texture { nullptr };

View file

@ -495,7 +495,7 @@ Device::Device(const Gfx::IntSize& min_size)
m_options.scissor_box = m_render_target->rect();
}
void Device::draw_primitives(GLenum primitive_type, FloatMatrix4x4 const& transform, FloatMatrix4x4 const& texture_matrix, Vector<Vertex> const& vertices, GL::TextureUnit::BoundList const& bound_texture_units)
void Device::draw_primitives(GLenum primitive_type, FloatMatrix4x4 const& transform, FloatMatrix4x4 const& texture_matrix, Vector<Vertex> const& vertices, Vector<size_t> const& enabled_texture_units)
{
// At this point, the user has effectively specified that they are done with defining the geometry
// of what they want to draw. We now need to do a few things (https://www.khronos.org/opengl/wiki/Rendering_Pipeline_Overview):
@ -644,38 +644,31 @@ void Device::draw_primitives(GLenum primitive_type, FloatMatrix4x4 const& transf
swap(triangle.vertices[0], triangle.vertices[1]);
}
submit_triangle(triangle, bound_texture_units);
submit_triangle(triangle, enabled_texture_units);
}
}
void Device::submit_triangle(const Triangle& triangle, GL::TextureUnit::BoundList const& bound_texture_units)
void Device::submit_triangle(const Triangle& triangle, Vector<size_t> const& enabled_texture_units)
{
rasterize_triangle(m_options, *m_render_target, *m_depth_buffer, triangle, [this, &bound_texture_units](FloatVector4 const& uv, FloatVector4 const& color, float z) -> FloatVector4 {
rasterize_triangle(m_options, *m_render_target, *m_depth_buffer, triangle, [this, &enabled_texture_units](FloatVector4 const& uv, FloatVector4 const& color, float z) -> FloatVector4 {
FloatVector4 fragment = color;
for (auto const& texture_unit : bound_texture_units) {
for (size_t i : enabled_texture_units) {
// FIXME: implement GL_TEXTURE_1D, GL_TEXTURE_3D and GL_TEXTURE_CUBE_MAP
FloatVector4 texel;
switch (texture_unit.currently_bound_target()) {
case GL_TEXTURE_2D:
if (!texture_unit.texture_2d_enabled() || texture_unit.texture_3d_enabled() || texture_unit.texture_cube_map_enabled())
continue;
texel = texture_unit.bound_texture_2d()->sampler().sample(uv);
break;
default:
VERIFY_NOT_REACHED();
}
auto const& sampler = m_samplers[i];
FloatVector4 texel = sampler.sample_2d({ uv.x(), uv.y() });
// FIXME: Implement more blend modes
switch (texture_unit.env_mode()) {
case GL_MODULATE:
switch (sampler.config().fixed_function_texture_env_mode) {
case TextureEnvMode::Modulate:
default:
fragment = fragment * texel;
break;
case GL_REPLACE:
case TextureEnvMode::Replace:
fragment = texel;
break;
case GL_DECAL: {
case TextureEnvMode::Decal: {
float src_alpha = fragment.w();
float one_minus_src_alpha = 1 - src_alpha;
fragment.set_x(texel.x() * src_alpha + fragment.x() * one_minus_src_alpha);

View file

@ -68,7 +68,7 @@ class Device final {
public:
Device(const Gfx::IntSize& min_size);
void draw_primitives(GLenum primitive_type, FloatMatrix4x4 const& transform, FloatMatrix4x4 const& texture_matrix, Vector<Vertex> const& vertices, GL::TextureUnit::BoundList const& bound_texture_units);
void draw_primitives(GLenum primitive_type, FloatMatrix4x4 const& transform, FloatMatrix4x4 const& texture_matrix, Vector<Vertex> const& vertices, Vector<size_t> const& enabled_texture_units);
void resize(const Gfx::IntSize& min_size);
void clear_color(const FloatVector4&);
void clear_depth(float);
@ -85,7 +85,7 @@ public:
void set_sampler_config(unsigned, SamplerConfig const&);
private:
void submit_triangle(Triangle const& triangle, GL::TextureUnit::BoundList const& bound_texture_units);
void submit_triangle(Triangle const& triangle, Vector<size_t> const& enabled_texture_units);
private:
RefPtr<Gfx::Bitmap> m_render_target;