1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:47:45 +00:00

LibGL+LibGPU+LibSoftGPU: Implement blend equations

This implements support for `glBlendEquation` and
`glBlendEquationSeparate`. These functions modify the calculation of the
resulting color in blending mode.
This commit is contained in:
Jelle Raaijmakers 2024-02-15 22:55:28 +01:00
parent 55668c3e48
commit aa3a6767f6
10 changed files with 202 additions and 30 deletions

View file

@ -1,7 +1,7 @@
/*
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
* Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
* Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
* Copyright (c) 2022-2024, Jelle Raaijmakers <jelle@gmta.nl>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -25,6 +25,10 @@ Optional<ContextParameter> GLContext::get_context_parameter(GLenum name)
case GL_BLEND_DST:
case GL_BLEND_DST_ALPHA:
return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_blend_destination_factor) } };
case GL_BLEND_EQUATION_ALPHA:
return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_blend_equation_alpha) } };
case GL_BLEND_EQUATION_RGB:
return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_blend_equation_rgb) } };
case GL_BLEND_SRC:
case GL_BLEND_SRC_ALPHA:
return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_blend_source_factor) } };

View file

@ -1,7 +1,7 @@
/*
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
* Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
* Copyright (c) 2021-2022, Jelle Raaijmakers <jelle@gmta.nl>
* Copyright (c) 2021-2024, Jelle Raaijmakers <jelle@gmta.nl>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -380,11 +380,18 @@ extern "C" {
#define GL_LIGHT6 0x4006
#define GL_LIGHT7 0x4007
// More blend factors
// Blend factors & modes
#define GL_CONSTANT_COLOR 0x8001
#define GL_ONE_MINUS_CONSTANT_COLOR 0x8002
#define GL_CONSTANT_ALPHA 0x8003
#define GL_ONE_MINUS_CONSTANT_ALPHA 0x8004
#define GL_FUNC_ADD 0x8006
#define GL_MIN 0x8007
#define GL_MAX 0x8008
#define GL_BLEND_EQUATION_RGB 0x8009
#define GL_FUNC_SUBTRACT 0x800A
#define GL_FUNC_REVERSE_SUBTRACT 0x800B
#define GL_BLEND_EQUATION_ALPHA 0x883D
// Points
#define GL_POINT_SMOOTH 0x0B10

View file

@ -50,9 +50,15 @@
},
"BlendEquation": {
"arguments": [
{"type": "GLenum", "name": "mode"}
{"type": "GLenum", "name": "mode"},
{"expression": "mode"}
],
"unimplemented": true
"implementation": "blend_equation_separate"
},
"BlendEquationSeparate": {
"arguments": [
{"type": "GLenum", "name": ["modeRGB", "modeAlpha"]}
]
},
"BlendFunc": {
"arguments": [

View file

@ -1,7 +1,7 @@
/*
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
* Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
* Copyright (c) 2022-2023, Jelle Raaijmakers <jelle@gmta.nl>
* Copyright (c) 2022-2024, Jelle Raaijmakers <jelle@gmta.nl>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -261,6 +261,50 @@ void GLContext::gl_finish()
// No-op since GLContext is completely synchronous at the moment
}
void GLContext::gl_blend_equation_separate(GLenum rgb_mode, GLenum alpha_mode)
{
APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_blend_equation_separate, rgb_mode, alpha_mode);
RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
RETURN_WITH_ERROR_IF(!(rgb_mode == GL_FUNC_ADD
|| rgb_mode == GL_FUNC_SUBTRACT
|| rgb_mode == GL_FUNC_REVERSE_SUBTRACT
|| rgb_mode == GL_MIN
|| rgb_mode == GL_MAX),
GL_INVALID_ENUM);
RETURN_WITH_ERROR_IF(!(alpha_mode == GL_FUNC_ADD
|| alpha_mode == GL_FUNC_SUBTRACT
|| alpha_mode == GL_FUNC_REVERSE_SUBTRACT
|| alpha_mode == GL_MIN
|| alpha_mode == GL_MAX),
GL_INVALID_ENUM);
m_blend_equation_rgb = rgb_mode;
m_blend_equation_alpha = alpha_mode;
auto map_gl_blend_equation_to_device = [](GLenum equation) constexpr {
switch (equation) {
case GL_FUNC_ADD:
return GPU::BlendEquation::Add;
case GL_FUNC_SUBTRACT:
return GPU::BlendEquation::Subtract;
case GL_FUNC_REVERSE_SUBTRACT:
return GPU::BlendEquation::ReverseSubtract;
case GL_MIN:
return GPU::BlendEquation::Min;
case GL_MAX:
return GPU::BlendEquation::Max;
default:
VERIFY_NOT_REACHED();
}
};
auto options = m_rasterizer->options();
options.blend_equation_rgb = map_gl_blend_equation_to_device(m_blend_equation_rgb);
options.blend_equation_alpha = map_gl_blend_equation_to_device(m_blend_equation_alpha);
m_rasterizer->set_options(options);
}
void GLContext::gl_blend_func(GLenum src_factor, GLenum dst_factor)
{
APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_blend_func, src_factor, dst_factor);

View file

@ -1,7 +1,7 @@
/*
* Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
* Copyright (c) 2021-2022, Jesse Buhagiar <jooster669@gmail.com>
* Copyright (c) 2022-2023, Jelle Raaijmakers <jelle@gmta.nl>
* Copyright (c) 2022-2024, Jelle Raaijmakers <jelle@gmta.nl>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -148,6 +148,7 @@ public:
GLboolean gl_is_list(GLuint list);
void gl_flush();
void gl_finish();
void gl_blend_equation_separate(GLenum rgb_mode, GLenum alpha_mode);
void gl_blend_func(GLenum src_factor, GLenum dst_factor);
void gl_shade_model(GLenum mode);
void gl_alpha_func(GLenum func, GLclampf ref);
@ -336,6 +337,9 @@ private:
GLenum m_blend_source_factor = GL_ONE;
GLenum m_blend_destination_factor = GL_ZERO;
GLenum m_blend_equation_rgb = GL_FUNC_ADD;
GLenum m_blend_equation_alpha = GL_FUNC_ADD;
bool m_alpha_test_enabled = false;
GLenum m_alpha_test_func = GL_ALWAYS;
GLclampf m_alpha_test_ref_value = 0;
@ -469,6 +473,7 @@ private:
decltype(&GLContext::gl_cull_face),
decltype(&GLContext::gl_call_list),
decltype(&GLContext::gl_call_lists),
decltype(&GLContext::gl_blend_equation_separate),
decltype(&GLContext::gl_blend_func),
decltype(&GLContext::gl_shade_model),
decltype(&GLContext::gl_alpha_func),