Преглед на файлове

LibGL: Add supporting code for depth buffer

This adds glClearDepth() and new caps for enabling and disabling
the depth buffer with glEnable() and glDisable()
Stephan Unverwerth преди 4 години
родител
ревизия
a8fc4be47a

+ 3 - 0
Userland/Libraries/LibGL/GL/gl.h

@@ -31,9 +31,11 @@ extern "C" {
 
 // Buffer bits
 #define GL_COLOR_BUFFER_BIT 0x0200
+#define GL_DEPTH_BUFFER_BIT 0x0400
 
 // Enable capabilities
 #define GL_CULL_FACE 0x0B44
+#define GL_DEPTH_TEST 0x0B71
 
 // Utility
 #define GL_VENDOR 0x1F00
@@ -85,6 +87,7 @@ typedef unsigned int GLbitfield;
 GLAPI void glBegin(GLenum mode);
 GLAPI void glClear(GLbitfield mask);
 GLAPI void glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
+GLAPI void glClearDepth(GLdouble depth);
 GLAPI void glColor3f(GLfloat r, GLfloat g, GLfloat b);
 GLAPI void glColor4f(GLfloat r, GLfloat g, GLfloat b, GLfloat a);
 GLAPI void glColor4fv(const GLfloat* v);

+ 1 - 0
Userland/Libraries/LibGL/GLContext.h

@@ -21,6 +21,7 @@ public:
     virtual void gl_begin(GLenum mode) = 0;
     virtual void gl_clear(GLbitfield mask) = 0;
     virtual void gl_clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) = 0;
+    virtual void gl_clear_depth(GLdouble depth) = 0;
     virtual void gl_color(GLdouble r, GLdouble g, GLdouble b, GLdouble a) = 0;
     virtual void gl_end() = 0;
     virtual void gl_frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val) = 0;

+ 5 - 0
Userland/Libraries/LibGL/GLUtils.cpp

@@ -40,6 +40,11 @@ void glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
     g_gl_context->gl_clear_color(red, green, blue, alpha);
 }
 
+void glClearDepth(GLdouble depth)
+{
+    g_gl_context->gl_clear_depth(depth);
+}
+
 GLubyte* glGetString(GLenum name)
 {
     return g_gl_context->gl_get_string(name);

+ 43 - 4
Userland/Libraries/LibGL/SoftwareGLContext.cpp

@@ -55,12 +55,18 @@ void SoftwareGLContext::gl_clear(GLbitfield mask)
         return;
     }
 
-    if (mask & GL_COLOR_BUFFER_BIT) {
-        m_rasterizer.clear_color(m_clear_color);
-        m_error = GL_NO_ERROR;
-    } else {
+    if (mask & ~(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) {
         m_error = GL_INVALID_ENUM;
+        return;
     }
+
+    if (mask & GL_COLOR_BUFFER_BIT)
+        m_rasterizer.clear_color(m_clear_color);
+
+    if (mask & GL_DEPTH_BUFFER_BIT)
+        m_rasterizer.clear_depth(static_cast<float>(m_clear_depth));
+
+    m_error = GL_NO_ERROR;
 }
 
 void SoftwareGLContext::gl_clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
@@ -74,6 +80,17 @@ void SoftwareGLContext::gl_clear_color(GLclampf red, GLclampf green, GLclampf bl
     m_error = GL_NO_ERROR;
 }
 
+void SoftwareGLContext::gl_clear_depth(GLdouble depth)
+{
+    if (m_in_draw_state) {
+        m_error = GL_INVALID_OPERATION;
+        return;
+    }
+
+    m_clear_depth = depth;
+    m_error = GL_NO_ERROR;
+}
+
 void SoftwareGLContext::gl_color(GLdouble r, GLdouble g, GLdouble b, GLdouble a)
 {
     m_current_vertex_color = { (float)r, (float)g, (float)b, (float)a };
@@ -601,14 +618,25 @@ void SoftwareGLContext::gl_enable(GLenum capability)
         return;
     }
 
+    auto rasterizer_options = m_rasterizer.options();
+    bool update_rasterizer_options = false;
+
     switch (capability) {
     case GL_CULL_FACE:
         m_cull_faces = true;
         break;
+    case GL_DEPTH_TEST:
+        m_depth_test_enabled = true;
+        rasterizer_options.enable_depth_test = true;
+        update_rasterizer_options = true;
+        break;
     default:
         m_error = GL_INVALID_ENUM;
         break;
     }
+
+    if (update_rasterizer_options)
+        m_rasterizer.set_options(rasterizer_options);
 }
 
 void SoftwareGLContext::gl_disable(GLenum capability)
@@ -618,14 +646,25 @@ void SoftwareGLContext::gl_disable(GLenum capability)
         return;
     }
 
+    auto rasterizer_options = m_rasterizer.options();
+    bool update_rasterizer_options = false;
+
     switch (capability) {
     case GL_CULL_FACE:
         m_cull_faces = false;
         break;
+    case GL_DEPTH_TEST:
+        m_depth_test_enabled = false;
+        rasterizer_options.enable_depth_test = false;
+        update_rasterizer_options = true;
+        break;
     default:
         m_error = GL_INVALID_ENUM;
         break;
     }
+
+    if (update_rasterizer_options)
+        m_rasterizer.set_options(rasterizer_options);
 }
 
 void SoftwareGLContext::gl_front_face(GLenum face)

+ 4 - 0
Userland/Libraries/LibGL/SoftwareGLContext.h

@@ -25,6 +25,7 @@ public:
     virtual void gl_begin(GLenum mode) override;
     virtual void gl_clear(GLbitfield mask) override;
     virtual void gl_clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) override;
+    virtual void gl_clear_depth(GLdouble depth) override;
     virtual void gl_color(GLdouble r, GLdouble g, GLdouble b, GLdouble a) override;
     virtual void gl_end() override;
     virtual void gl_frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val) override;
@@ -60,6 +61,7 @@ private:
     Vector<FloatMatrix4x4> m_model_view_matrix_stack;
 
     FloatVector4 m_clear_color = { 0.0f, 0.0f, 0.0f, 0.0f };
+    double m_clear_depth = { 1.0 };
     FloatVector4 m_current_vertex_color = { 1.0f, 1.0f, 1.0f, 1.0f };
 
     Vector<GLVertex, 96> vertex_list;
@@ -69,6 +71,8 @@ private:
     GLenum m_error = GL_NO_ERROR;
     bool m_in_draw_state = false;
 
+    bool m_depth_test_enabled = false;
+
     bool m_cull_faces = false;
     GLenum m_front_face = GL_CCW;
     GLenum m_culled_sides = GL_BACK;

+ 1 - 0
Userland/Libraries/LibGL/SoftwareRasterizer.h

@@ -14,6 +14,7 @@ namespace GL {
 
 struct RasterizerOptions {
     bool shade_smooth { false };
+    bool enable_depth_test { false };
 };
 
 class SoftwareRasterizer final {