From 17ec4333265d376e526c53c180492dfd67bab540 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Tue, 11 Jan 2022 01:37:12 +0100 Subject: [PATCH] LibSoftGPU: Only render complete primitives MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, we were expecting triangles and quads to consist of complete sets of vertices. However, a more common behavior is to ignore all vertices that do not make up a full primitive. For example, OpenGL specifies for `GL_QUADS`: "The total number of vertices between Begin and End is 4n + k, where 0 ≤ k ≤ 3; if k is not zero, the final k vertices are ignored." This changes the behavior of `Device::draw_primitives()` to both return early if no full set of vertices was provided, and to ignore any additional vertices that are not part of a full set. --- Userland/Libraries/LibSoftGPU/Device.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibSoftGPU/Device.cpp b/Userland/Libraries/LibSoftGPU/Device.cpp index ef64a5526f..23d15ce1dd 100644 --- a/Userland/Libraries/LibSoftGPU/Device.cpp +++ b/Userland/Libraries/LibSoftGPU/Device.cpp @@ -568,7 +568,9 @@ void Device::draw_primitives(PrimitiveType primitive_type, FloatMatrix4x4 const& // Let's construct some triangles if (primitive_type == PrimitiveType::Triangles) { Triangle triangle; - for (size_t i = 0; i < vertices.size(); i += 3) { + if (vertices.size() < 3) + return; + for (size_t i = 0; i < vertices.size() - 2; i += 3) { triangle.vertices[0] = vertices.at(i); triangle.vertices[1] = vertices.at(i + 1); triangle.vertices[2] = vertices.at(i + 2); @@ -578,8 +580,9 @@ void Device::draw_primitives(PrimitiveType primitive_type, FloatMatrix4x4 const& } else if (primitive_type == PrimitiveType::Quads) { // We need to construct two triangles to form the quad Triangle triangle; - VERIFY(vertices.size() % 4 == 0); - for (size_t i = 0; i < vertices.size(); i += 4) { + if (vertices.size() < 4) + return; + for (size_t i = 0; i < vertices.size() - 3; i += 4) { // Triangle 1 triangle.vertices[0] = vertices.at(i); triangle.vertices[1] = vertices.at(i + 1);