LibGL: Implement glDepthFunc

This commit is contained in:
Stephan Unverwerth 2021-08-16 17:59:45 +02:00 committed by Andreas Kling
parent a97dbd2317
commit 5b9c87a8b5
Notes: sideshowbarker 2024-07-18 05:30:34 +09:00
7 changed files with 60 additions and 2 deletions

View file

@ -370,6 +370,7 @@ GLAPI void glTexCoordPointer(GLint size, GLenum type, GLsizei stride, const void
GLAPI void glDrawArrays(GLenum mode, GLint first, GLsizei count);
GLAPI void glDrawElements(GLenum mode, GLsizei count, GLenum type, const void* indices);
GLAPI void glDepthRange(GLdouble nearVal, GLdouble farVal);
GLAPI void glDepthFunc(GLenum func);
#ifdef __cplusplus
}

View file

@ -75,6 +75,7 @@ public:
virtual void gl_get_booleanv(GLenum pname, GLboolean* data) = 0;
virtual void gl_get_integerv(GLenum pname, GLint* data) = 0;
virtual void gl_depth_range(GLdouble min, GLdouble max) = 0;
virtual void gl_depth_func(GLenum func) = 0;
virtual void present() = 0;
};

View file

@ -124,3 +124,8 @@ void glDepthRange(GLdouble min, GLdouble max)
{
g_gl_context->gl_depth_range(min, max);
}
void glDepthFunc(GLenum func)
{
g_gl_context->gl_depth_func(func);
}

View file

@ -1653,6 +1653,27 @@ void SoftwareGLContext::gl_depth_range(GLdouble min, GLdouble max)
m_rasterizer.set_options(options);
}
void SoftwareGLContext::gl_depth_func(GLenum func)
{
APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_func, func);
RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
RETURN_WITH_ERROR_IF(!(func == GL_NEVER
|| func == GL_LESS
|| func == GL_EQUAL
|| func == GL_LEQUAL
|| func == GL_GREATER
|| func == GL_NOTEQUAL
|| func == GL_GEQUAL
|| func == GL_ALWAYS),
GL_INVALID_ENUM);
auto options = m_rasterizer.options();
options.depth_func = func;
m_rasterizer.set_options(options);
}
// General helper function to read arbitrary vertex attribute data into a float array
void SoftwareGLContext::read_from_vertex_attribute_pointer(VertexAttribPointer const& attrib, int index, float* elements, bool normalize)
{

View file

@ -85,6 +85,7 @@ public:
virtual void gl_get_booleanv(GLenum pname, GLboolean* data) override;
virtual void gl_get_integerv(GLenum pname, GLint* data) override;
virtual void gl_depth_range(GLdouble min, GLdouble max) override;
virtual void gl_depth_func(GLenum func) override;
virtual void present() override;
private:

View file

@ -261,8 +261,36 @@ static void rasterize_triangle(const RasterizerOptions& options, Gfx::Bitmap& re
auto barycentric = FloatVector3(coords.x(), coords.y(), coords.z()) * one_over_area;
float z = interpolate(triangle.vertices[0].z, triangle.vertices[1].z, triangle.vertices[2].z, barycentric);
z = options.depth_min + (options.depth_max - options.depth_min) * (z + 1) / 2;
if (z >= *depth) {
bool pass = false;
switch (options.depth_func) {
case GL_ALWAYS:
pass = true;
break;
case GL_NEVER:
pass = false;
break;
case GL_GREATER:
pass = z > *depth;
break;
case GL_GEQUAL:
pass = z >= *depth;
break;
case GL_NOTEQUAL:
pass = z != *depth;
break;
case GL_EQUAL:
pass = z == *depth;
break;
case GL_LEQUAL:
pass = z <= *depth;
break;
case GL_LESS:
pass = z < *depth;
break;
}
if (!pass) {
pixel_mask[y] ^= 1 << x;
continue;
}

View file

@ -31,6 +31,7 @@ struct RasterizerOptions {
u32 color_mask { 0xffffffff };
float depth_min { 0 };
float depth_max { 1 };
GLenum depth_func { GL_LESS };
};
class SoftwareRasterizer final {