Skip to content

Commit 7cbaaf8

Browse files
sunverwerthalimpfard
authored andcommitted
LibGL: Implement glDrawBuffer
1 parent b069c13 commit 7cbaaf8

File tree

7 files changed

+52
-1
lines changed

7 files changed

+52
-1
lines changed

Userland/Libraries/LibGL/GL/gl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern "C" {
2525
// OpenGL related `defines`
2626
#define GL_TRUE 1
2727
#define GL_FALSE 0
28+
#define GL_NONE 0
2829

2930
// Matrix Modes
3031
#define GL_MODELVIEW 0x0050
@@ -352,6 +353,7 @@ GLAPI void glShadeModel(GLenum mode);
352353
GLAPI void glAlphaFunc(GLenum func, GLclampf ref);
353354
GLAPI void glHint(GLenum target, GLenum mode);
354355
GLAPI void glReadBuffer(GLenum mode);
356+
GLAPI void glDrawBuffer(GLenum buffer);
355357
GLAPI void glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels);
356358
GLAPI void glTexImage2D(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* data);
357359
GLAPI void glTexCoord2f(GLfloat s, GLfloat t);

Userland/Libraries/LibGL/GLContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class GLContext {
5656
virtual void gl_alpha_func(GLenum func, GLclampf ref) = 0;
5757
virtual void gl_hint(GLenum target, GLenum mode) = 0;
5858
virtual void gl_read_buffer(GLenum mode) = 0;
59+
virtual void gl_draw_buffer(GLenum buffer) = 0;
5960
virtual void gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels) = 0;
6061
virtual void gl_tex_image_2d(GLenum target, GLint level, GLint internal_format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* data) = 0;
6162
virtual void gl_tex_parameter(GLenum target, GLenum pname, GLfloat param) = 0;

Userland/Libraries/LibGL/GLUtils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ void glReadBuffer(GLenum mode)
8585
g_gl_context->gl_read_buffer(mode);
8686
}
8787

88+
void glDrawBuffer(GLenum buffer)
89+
{
90+
g_gl_context->gl_draw_buffer(buffer);
91+
}
92+
8893
void glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels)
8994
{
9095
g_gl_context->gl_read_pixels(x, y, width, height, format, type, pixels);

Userland/Libraries/LibGL/SoftwareGLContext.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,46 @@ void SoftwareGLContext::gl_read_buffer(GLenum mode)
960960
m_current_read_buffer = mode;
961961
}
962962

963+
void SoftwareGLContext::gl_draw_buffer(GLenum buffer)
964+
{
965+
APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_buffer, buffer);
966+
967+
RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
968+
969+
// FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here
970+
// plus any aux buffer between 0 and GL_AUX_BUFFERS
971+
RETURN_WITH_ERROR_IF(buffer != GL_NONE
972+
&& buffer != GL_FRONT_LEFT
973+
&& buffer != GL_FRONT_RIGHT
974+
&& buffer != GL_BACK_LEFT
975+
&& buffer != GL_BACK_RIGHT
976+
&& buffer != GL_FRONT
977+
&& buffer != GL_BACK
978+
&& buffer != GL_LEFT
979+
&& buffer != GL_RIGHT,
980+
GL_INVALID_ENUM);
981+
982+
// FIXME: We do not currently have aux buffers, so make it an invalid
983+
// operation to select anything but front or back buffers. Also we do
984+
// not allow selecting the stereoscopic RIGHT buffers since we do not
985+
// have them configured.
986+
RETURN_WITH_ERROR_IF(buffer != GL_NONE
987+
&& buffer != GL_FRONT_LEFT
988+
&& buffer != GL_FRONT
989+
&& buffer != GL_BACK_LEFT
990+
&& buffer != GL_BACK
991+
&& buffer != GL_FRONT
992+
&& buffer != GL_BACK
993+
&& buffer != GL_LEFT,
994+
GL_INVALID_OPERATION);
995+
996+
m_current_draw_buffer = buffer;
997+
998+
auto rasterizer_options = m_rasterizer.options();
999+
rasterizer_options.draw_buffer = m_current_draw_buffer;
1000+
m_rasterizer.set_options(rasterizer_options);
1001+
}
1002+
9631003
void SoftwareGLContext::gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels)
9641004
{
9651005
RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);

Userland/Libraries/LibGL/SoftwareGLContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class SoftwareGLContext : public GLContext {
6666
virtual void gl_alpha_func(GLenum func, GLclampf ref) override;
6767
virtual void gl_hint(GLenum target, GLenum mode) override;
6868
virtual void gl_read_buffer(GLenum mode) override;
69+
virtual void gl_draw_buffer(GLenum buffer) override;
6970
virtual void gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels) override;
7071
virtual void gl_tex_image_2d(GLenum target, GLint level, GLint internal_format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* data) override;
7172
virtual void gl_tex_parameter(GLenum target, GLenum pname, GLfloat param) override;
@@ -152,6 +153,7 @@ class SoftwareGLContext : public GLContext {
152153
GLclampf m_alpha_test_ref_value = 0;
153154

154155
GLenum m_current_read_buffer = GL_BACK;
156+
GLenum m_current_draw_buffer = GL_BACK;
155157

156158
// Client side arrays
157159
bool m_client_side_vertex_array_enabled = false;

Userland/Libraries/LibGL/SoftwareRasterizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ static void rasterize_triangle(const RasterizerOptions& options, Gfx::Bitmap& re
334334
}
335335

336336
// We will not update the color buffer at all
337-
if (!options.color_mask)
337+
if (!options.color_mask || options.draw_buffer == GL_NONE)
338338
continue;
339339

340340
// Draw the pixels according to the previously generated mask

Userland/Libraries/LibGL/SoftwareRasterizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct RasterizerOptions {
4444
GLboolean fog_enabled { false };
4545
GLfloat fog_start { 0.0f };
4646
GLfloat fog_end { 1.0f };
47+
GLenum draw_buffer { GL_BACK };
4748
};
4849

4950
class SoftwareRasterizer final {

0 commit comments

Comments
 (0)