Jelajahi Sumber

LibGL+LibSoftGPU: Move Vertex and Triangle structs to LibSoftGPU

Stephan Unverwerth 3 tahun lalu
induk
melakukan
251f3c007f

+ 0 - 11
Userland/Libraries/LibGL/GLStruct.h

@@ -17,17 +17,6 @@ struct GLColor {
     GLclampf r, g, b, a;
 };
 
-struct GLVertex {
-    FloatVector4 position;
-    FloatVector4 color;
-    FloatVector4 tex_coord;
-    FloatVector3 normal;
-};
-
-struct GLTriangle {
-    GLVertex vertices[3];
-};
-
 struct GLEdge {
     GLfloat x1;
     GLfloat y1;

+ 1 - 1
Userland/Libraries/LibGL/SoftwareGLContext.cpp

@@ -500,7 +500,7 @@ void SoftwareGLContext::gl_vertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w
 {
     APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_vertex, x, y, z, w);
 
-    GLVertex vertex;
+    SoftGPU::Vertex vertex;
 
     vertex.position = { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z), static_cast<float>(w) };
     vertex.color = m_current_vertex_color;

+ 2 - 1
Userland/Libraries/LibGL/SoftwareGLContext.h

@@ -23,6 +23,7 @@
 #include <LibGfx/Vector3.h>
 #include <LibSoftGPU/Clipper.h>
 #include <LibSoftGPU/SoftwareRasterizer.h>
+#include <LibSoftGPU/Vertex.h>
 
 namespace GL {
 
@@ -176,7 +177,7 @@ private:
     FloatVector4 m_current_vertex_tex_coord = { 0.0f, 0.0f, 0.0f, 1.0f };
     FloatVector3 m_current_vertex_normal = { 0.0f, 0.0f, 1.0f };
 
-    Vector<GLVertex, 96> m_vertex_list;
+    Vector<SoftGPU::Vertex> m_vertex_list;
 
     GLenum m_error = GL_NO_ERROR;
     bool m_in_draw_state = false;

+ 3 - 3
Userland/Libraries/LibSoftGPU/Clipper.cpp

@@ -31,7 +31,7 @@ bool Clipper::point_within_clip_plane(const FloatVector4& vertex, ClipPlane plan
     return false;
 }
 
-GL::GLVertex Clipper::clip_intersection_point(const GL::GLVertex& p1, const GL::GLVertex& p2, ClipPlane plane_index)
+Vertex Clipper::clip_intersection_point(const Vertex& p1, const Vertex& p2, ClipPlane plane_index)
 {
     // See https://www.microsoft.com/en-us/research/wp-content/uploads/1978/01/p245-blinn.pdf
     // "Clipping Using Homogeneous Coordinates" Blinn/Newell, 1978
@@ -42,14 +42,14 @@ GL::GLVertex Clipper::clip_intersection_point(const GL::GLVertex& p1, const GL::
     float x2 = clip_plane_normals[plane_index].dot(p2.position);
     float a = (w1 + x1) / ((w1 + x1) - (w2 + x2));
 
-    GL::GLVertex out;
+    Vertex out;
     out.position = p1.position * (1 - a) + p2.position * a;
     out.color = p1.color * (1 - a) + p2.color * a;
     out.tex_coord = p1.tex_coord * (1 - a) + p2.tex_coord * a;
     return out;
 }
 
-void Clipper::clip_triangle_against_frustum(Vector<GL::GLVertex>& input_verts)
+void Clipper::clip_triangle_against_frustum(Vector<Vertex>& input_verts)
 {
     list_a = input_verts;
     list_b.clear_with_capacity();

+ 5 - 5
Userland/Libraries/LibSoftGPU/Clipper.h

@@ -7,8 +7,8 @@
 #pragma once
 
 #include <AK/Vector.h>
-#include <LibGL/GLStruct.h>
 #include <LibGfx/Vector4.h>
+#include <LibSoftGPU/Vertex.h>
 
 namespace SoftGPU {
 
@@ -46,13 +46,13 @@ class Clipper final {
 public:
     Clipper() { }
 
-    void clip_triangle_against_frustum(Vector<GL::GLVertex>& input_vecs);
+    void clip_triangle_against_frustum(Vector<Vertex>& input_vecs);
 
 private:
     bool point_within_clip_plane(const FloatVector4& vertex, ClipPlane plane);
-    GL::GLVertex clip_intersection_point(const GL::GLVertex& vec, const GL::GLVertex& prev_vec, ClipPlane plane_index);
-    Vector<GL::GLVertex> list_a;
-    Vector<GL::GLVertex> list_b;
+    Vertex clip_intersection_point(const Vertex& vec, const Vertex& prev_vec, ClipPlane plane_index);
+    Vector<Vertex> list_a;
+    Vector<Vertex> list_b;
 };
 
 }

+ 10 - 10
Userland/Libraries/LibSoftGPU/SoftwareRasterizer.cpp

@@ -111,7 +111,7 @@ static constexpr void setup_blend_factors(GLenum mode, FloatVector4& constant, f
 }
 
 template<typename PS>
-static void rasterize_triangle(const RasterizerOptions& options, Gfx::Bitmap& render_target, DepthBuffer& depth_buffer, const GL::GLTriangle& triangle, PS pixel_shader)
+static void rasterize_triangle(const RasterizerOptions& options, Gfx::Bitmap& render_target, DepthBuffer& depth_buffer, const Triangle& triangle, PS pixel_shader)
 {
     // Since the algorithm is based on blocks of uniform size, we need
     // to ensure that our render_target size is actually a multiple of the block size
@@ -495,7 +495,7 @@ SoftwareRasterizer::SoftwareRasterizer(const Gfx::IntSize& min_size)
     m_options.scissor_box = m_render_target->rect();
 }
 
-void SoftwareRasterizer::draw_primitives(GLenum primitive_type, FloatMatrix4x4 const& transform, FloatMatrix4x4 const& texture_matrix, Vector<GL::GLVertex> const& vertices, GL::TextureUnit::BoundList const& bound_texture_units)
+void SoftwareRasterizer::draw_primitives(GLenum primitive_type, FloatMatrix4x4 const& transform, FloatMatrix4x4 const& texture_matrix, Vector<Vertex> const& vertices, GL::TextureUnit::BoundList const& bound_texture_units)
 {
     // At this point, the user has effectively specified that they are done with defining the geometry
     // of what they want to draw. We now need to do a few things (https://www.khronos.org/opengl/wiki/Rendering_Pipeline_Overview):
@@ -515,7 +515,7 @@ void SoftwareRasterizer::draw_primitives(GLenum primitive_type, FloatMatrix4x4 c
 
     // Let's construct some triangles
     if (primitive_type == GL_TRIANGLES) {
-        GL::GLTriangle triangle;
+        Triangle triangle;
         for (size_t i = 0; i < vertices.size(); i += 3) {
             triangle.vertices[0] = vertices.at(i);
             triangle.vertices[1] = vertices.at(i + 1);
@@ -525,7 +525,7 @@ void SoftwareRasterizer::draw_primitives(GLenum primitive_type, FloatMatrix4x4 c
         }
     } else if (primitive_type == GL_QUADS) {
         // We need to construct two triangles to form the quad
-        GL::GLTriangle triangle;
+        Triangle triangle;
         VERIFY(vertices.size() % 4 == 0);
         for (size_t i = 0; i < vertices.size(); i += 4) {
             // Triangle 1
@@ -541,7 +541,7 @@ void SoftwareRasterizer::draw_primitives(GLenum primitive_type, FloatMatrix4x4 c
             m_triangle_list.append(triangle);
         }
     } else if (primitive_type == GL_TRIANGLE_FAN || primitive_type == GL_POLYGON) {
-        GL::GLTriangle triangle;
+        Triangle triangle;
         triangle.vertices[0] = vertices.at(0); // Root vertex is always the vertex defined first
 
         for (size_t i = 1; i < vertices.size() - 1; i++) // This is technically `n-2` triangles. We start at index 1
@@ -551,7 +551,7 @@ void SoftwareRasterizer::draw_primitives(GLenum primitive_type, FloatMatrix4x4 c
             m_triangle_list.append(triangle);
         }
     } else if (primitive_type == GL_TRIANGLE_STRIP) {
-        GL::GLTriangle triangle;
+        Triangle triangle;
         for (size_t i = 0; i < vertices.size() - 2; i++) {
             triangle.vertices[0] = vertices.at(i);
             triangle.vertices[1] = vertices.at(i + 1);
@@ -562,7 +562,7 @@ void SoftwareRasterizer::draw_primitives(GLenum primitive_type, FloatMatrix4x4 c
 
     // Now let's transform each triangle and send that to the GPU
     for (size_t i = 0; i < m_triangle_list.size(); i++) {
-        GL::GLTriangle& triangle = m_triangle_list.at(i);
+        Triangle& triangle = m_triangle_list.at(i);
 
         // First multiply the vertex by the MODELVIEW matrix and then the PROJECTION matrix
         triangle.vertices[0].position = transform * triangle.vertices[0].position;
@@ -607,7 +607,7 @@ void SoftwareRasterizer::draw_primitives(GLenum primitive_type, FloatMatrix4x4 c
             vec.position.set_y(scr_height / 2 - vec.position.y() * scr_height / 2);
         }
 
-        GL::GLTriangle tri;
+        Triangle tri;
         tri.vertices[0] = m_clipped_vertices[0];
         for (size_t i = 1; i < m_clipped_vertices.size() - 1; i++) {
             tri.vertices[1] = m_clipped_vertices[i];
@@ -617,7 +617,7 @@ void SoftwareRasterizer::draw_primitives(GLenum primitive_type, FloatMatrix4x4 c
     }
 
     for (size_t i = 0; i < m_processed_triangles.size(); i++) {
-        GL::GLTriangle& triangle = m_processed_triangles.at(i);
+        Triangle& triangle = m_processed_triangles.at(i);
 
         // Let's calculate the (signed) area of the triangle
         // https://cp-algorithms.com/geometry/oriented-triangle-area.html
@@ -648,7 +648,7 @@ void SoftwareRasterizer::draw_primitives(GLenum primitive_type, FloatMatrix4x4 c
     }
 }
 
-void SoftwareRasterizer::submit_triangle(const GL::GLTriangle& triangle, GL::TextureUnit::BoundList const& bound_texture_units)
+void SoftwareRasterizer::submit_triangle(const Triangle& triangle, GL::TextureUnit::BoundList const& bound_texture_units)
 {
     rasterize_triangle(m_options, *m_render_target, *m_depth_buffer, triangle, [this, &bound_texture_units](FloatVector4 const& uv, FloatVector4 const& color, float z) -> FloatVector4 {
         FloatVector4 fragment = color;

+ 7 - 5
Userland/Libraries/LibSoftGPU/SoftwareRasterizer.h

@@ -18,6 +18,8 @@
 #include <LibGfx/Vector4.h>
 #include <LibSoftGPU/Clipper.h>
 #include <LibSoftGPU/DepthBuffer.h>
+#include <LibSoftGPU/Triangle.h>
+#include <LibSoftGPU/Vertex.h>
 
 namespace SoftGPU {
 
@@ -61,7 +63,7 @@ class SoftwareRasterizer final {
 public:
     SoftwareRasterizer(const Gfx::IntSize& min_size);
 
-    void draw_primitives(GLenum primitive_type, FloatMatrix4x4 const& transform, FloatMatrix4x4 const& texture_matrix, Vector<GL::GLVertex> const& vertices, GL::TextureUnit::BoundList const& bound_texture_units);
+    void draw_primitives(GLenum primitive_type, FloatMatrix4x4 const& transform, FloatMatrix4x4 const& texture_matrix, Vector<Vertex> const& vertices, GL::TextureUnit::BoundList const& bound_texture_units);
     void resize(const Gfx::IntSize& min_size);
     void clear_color(const FloatVector4&);
     void clear_depth(float);
@@ -74,16 +76,16 @@ public:
     float get_depthbuffer_value(int x, int y);
 
 private:
-    void submit_triangle(GL::GLTriangle const& triangle, GL::TextureUnit::BoundList const& bound_texture_units);
+    void submit_triangle(Triangle const& triangle, GL::TextureUnit::BoundList const& bound_texture_units);
 
 private:
     RefPtr<Gfx::Bitmap> m_render_target;
     OwnPtr<DepthBuffer> m_depth_buffer;
     RasterizerOptions m_options;
     Clipper m_clipper;
-    Vector<GL::GLTriangle, 32> m_triangle_list;
-    Vector<GL::GLTriangle, 32> m_processed_triangles;
-    Vector<GL::GLVertex> m_clipped_vertices;
+    Vector<Triangle> m_triangle_list;
+    Vector<Triangle> m_processed_triangles;
+    Vector<Vertex> m_clipped_vertices;
 };
 
 }

+ 18 - 0
Userland/Libraries/LibSoftGPU/Triangle.h

@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
+ * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibSoftGPU/Vertex.h>
+
+namespace SoftGPU {
+
+struct Triangle {
+    Vertex vertices[3];
+};
+
+}

+ 22 - 0
Userland/Libraries/LibSoftGPU/Vertex.h

@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
+ * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibGfx/Vector3.h>
+#include <LibGfx/Vector4.h>
+
+namespace SoftGPU {
+
+struct Vertex {
+    FloatVector4 position;
+    FloatVector4 color;
+    FloatVector4 tex_coord;
+    FloatVector3 normal;
+};
+
+}