Skip to content

Commit ae82b14

Browse files
gmtaawesomekling
authored andcommitted
LibGL+LibWeb: Remove WebGL-specific API from GLContext
The OpenGL API has ways to retrieve these values, so let's make sure to implement them. :^)
1 parent bc12939 commit ae82b14

File tree

5 files changed

+72
-9
lines changed

5 files changed

+72
-9
lines changed

Tests/LibGL/TestAPI.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,51 @@ TEST_CASE(0003_gl_bind_buffer_names_must_be_allocated)
6161
glBindBuffer(GL_ARRAY_BUFFER, 123);
6262
EXPECT_EQ(glGetError(), static_cast<GLenum>(GL_INVALID_VALUE));
6363
}
64+
65+
TEST_CASE(0004_gl_color_clear_value)
66+
{
67+
auto context = create_testing_context();
68+
69+
Array<GLdouble, 4> clear_color;
70+
glGetDoublev(GL_COLOR_CLEAR_VALUE, clear_color.data());
71+
EXPECT_EQ(clear_color[0], 0.);
72+
EXPECT_EQ(clear_color[1], 0.);
73+
EXPECT_EQ(clear_color[2], 0.);
74+
EXPECT_EQ(clear_color[3], 0.);
75+
76+
glClearColor(.1f, .2f, .3f, .4f);
77+
78+
glGetDoublev(GL_COLOR_CLEAR_VALUE, clear_color.data());
79+
EXPECT_APPROXIMATE(clear_color[0], .1);
80+
EXPECT_APPROXIMATE(clear_color[1], .2);
81+
EXPECT_APPROXIMATE(clear_color[2], .3);
82+
EXPECT_APPROXIMATE(clear_color[3], .4);
83+
}
84+
85+
TEST_CASE(0005_gl_depth_clear_value)
86+
{
87+
auto context = create_testing_context();
88+
89+
GLdouble clear_depth;
90+
glGetDoublev(GL_DEPTH_CLEAR_VALUE, &clear_depth);
91+
EXPECT_EQ(clear_depth, 1.);
92+
93+
glClearDepth(.1f);
94+
95+
glGetDoublev(GL_DEPTH_CLEAR_VALUE, &clear_depth);
96+
EXPECT_APPROXIMATE(clear_depth, .1);
97+
}
98+
99+
TEST_CASE(0006_gl_stencil_clear_value)
100+
{
101+
auto context = create_testing_context();
102+
103+
GLint clear_stencil;
104+
glGetIntegerv(GL_STENCIL_CLEAR_VALUE, &clear_stencil);
105+
EXPECT_EQ(clear_stencil, 0);
106+
107+
glClearStencil(255);
108+
109+
glGetIntegerv(GL_STENCIL_CLEAR_VALUE, &clear_stencil);
110+
EXPECT_EQ(clear_stencil, 255);
111+
}

Userland/Libraries/LibGL/ContextParameter.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ Optional<ContextParameter> GLContext::get_context_parameter(GLenum name)
3030
return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(u8) * 8 } };
3131
case GL_CLIENT_ACTIVE_TEXTURE:
3232
return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(GL_TEXTURE0 + m_client_active_texture) } };
33+
case GL_COLOR_CLEAR_VALUE:
34+
return ContextParameter {
35+
.type = GL_DOUBLE,
36+
.count = 4,
37+
.value = {
38+
.double_list = {
39+
static_cast<GLdouble>(m_clear_color.x()),
40+
static_cast<GLdouble>(m_clear_color.y()),
41+
static_cast<GLdouble>(m_clear_color.z()),
42+
static_cast<GLdouble>(m_clear_color.w()),
43+
} }
44+
};
3345
case GL_COLOR_MATERIAL:
3446
return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_color_material_enabled } };
3547
case GL_COLOR_MATERIAL_FACE:
@@ -52,6 +64,8 @@ Optional<ContextParameter> GLContext::get_context_parameter(GLenum name)
5264
return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_cull_faces } };
5365
case GL_DEPTH_BITS:
5466
return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
67+
case GL_DEPTH_CLEAR_VALUE:
68+
return ContextParameter { .type = GL_DOUBLE, .value = { .double_value = static_cast<GLdouble>(m_clear_depth) } };
5569
case GL_DEPTH_TEST:
5670
return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_depth_test_enabled } };
5771
case GL_DITHER:

Userland/Libraries/LibGL/GL/gl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ extern "C" {
103103
#define GL_COLOR_MATERIAL 0x0B57
104104
#define GL_FOG_START 0x0B63
105105
#define GL_FOG_END 0x0B64
106+
#define GL_DEPTH_CLEAR_VALUE 0x0B73
106107
#define GL_STENCIL_CLEAR_VALUE 0x0B91
107108
#define GL_MATRIX_MODE 0x0BA0
108109
#define GL_NORMALIZE 0x0BA1
109110
#define GL_VIEWPORT 0x0BA2
111+
#define GL_COLOR_CLEAR_VALUE 0x0C22
110112
#define GL_DOUBLEBUFFER 0x0C32
111113
#define GL_TEXTURE_GEN_S 0x0C60
112114
#define GL_TEXTURE_GEN_T 0x0C61

Userland/Libraries/LibGL/GLContext.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,6 @@ class GLContext final {
107107
NonnullRefPtr<Gfx::Bitmap> frontbuffer() const { return m_frontbuffer; };
108108
void present();
109109

110-
// Used by WebGL to preserve the clear values when implicitly clearing the front buffer.
111-
// FIXME: Add ContextParameters for these and expose them through methods such as gl_get_floatv instead of having a public API like this.
112-
FloatVector4 current_clear_color() const { return m_clear_color; }
113-
GLdouble current_clear_depth() const { return m_clear_depth; }
114-
GLint current_clear_stencil() const { return m_clear_stencil; }
115-
116110
void gl_begin(GLenum mode);
117111
void gl_clear(GLbitfield mask);
118112
void gl_clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);

Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ void WebGLRenderingContextBase::present()
5252
// This default behavior can be changed by setting the preserveDrawingBuffer attribute of the WebGLContextAttributes object.
5353
// If this flag is true, the contents of the drawing buffer shall be preserved until the author either clears or overwrites them."
5454
if (!m_context_creation_parameters.preserve_drawing_buffer) {
55-
auto current_clear_color = m_context->current_clear_color();
56-
auto current_clear_depth = m_context->current_clear_depth();
57-
auto current_clear_stencil = m_context->current_clear_stencil();
55+
Array<GLdouble, 4> current_clear_color;
56+
m_context->gl_get_doublev(GL_COLOR_CLEAR_VALUE, current_clear_color.data());
57+
58+
GLdouble current_clear_depth;
59+
m_context->gl_get_doublev(GL_DEPTH_CLEAR_VALUE, &current_clear_depth);
60+
61+
GLint current_clear_stencil;
62+
m_context->gl_get_integerv(GL_STENCIL_CLEAR_VALUE, &current_clear_stencil);
5863

5964
// The implicit clear value for the color buffer is (0, 0, 0, 0)
6065
m_context->gl_clear_color(0, 0, 0, 0);

0 commit comments

Comments
 (0)