From 7571ef0343e0daab63aa519d456e61fff2effc32 Mon Sep 17 00:00:00 2001 From: Stephan Unverwerth Date: Sat, 15 Jan 2022 18:33:55 +0100 Subject: [PATCH] LibGL+LibSoftGPU: Add multiple texture coordinates to vertex struct We now have one set of texture coordinates per texture unit. Texture coordinate generation and texture coordinate assignment is currently only stubbed. This will be rectified in another commit. --- .../Libraries/LibGL/SoftwareGLContext.cpp | 3 ++- Userland/Libraries/LibSoftGPU/Clipper.cpp | 3 ++- Userland/Libraries/LibSoftGPU/Device.cpp | 24 ++++++++++--------- Userland/Libraries/LibSoftGPU/PixelQuad.h | 3 ++- Userland/Libraries/LibSoftGPU/Vertex.h | 4 +++- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Userland/Libraries/LibGL/SoftwareGLContext.cpp b/Userland/Libraries/LibGL/SoftwareGLContext.cpp index ee5669941f..d1413e22fb 100644 --- a/Userland/Libraries/LibGL/SoftwareGLContext.cpp +++ b/Userland/Libraries/LibGL/SoftwareGLContext.cpp @@ -615,7 +615,8 @@ void SoftwareGLContext::gl_vertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w vertex.position = { static_cast(x), static_cast(y), static_cast(z), static_cast(w) }; vertex.color = m_current_vertex_color; - vertex.tex_coord = m_current_vertex_tex_coord; + for (size_t i = 0; i < m_device_info.num_texture_units; ++i) + vertex.tex_coords[i] = m_current_vertex_tex_coord; vertex.normal = m_current_vertex_normal; m_vertex_list.append(vertex); diff --git a/Userland/Libraries/LibSoftGPU/Clipper.cpp b/Userland/Libraries/LibSoftGPU/Clipper.cpp index 0b4405bd19..9db116e884 100644 --- a/Userland/Libraries/LibSoftGPU/Clipper.cpp +++ b/Userland/Libraries/LibSoftGPU/Clipper.cpp @@ -47,7 +47,8 @@ Vertex Clipper::clip_intersection_point(const Vertex& p1, const Vertex& p2, Clip out.eye_coordinates = mix(p1.eye_coordinates, p2.eye_coordinates, a); out.clip_coordinates = mix(p1.clip_coordinates, p2.clip_coordinates, a); out.color = mix(p1.color, p2.color, a); - out.tex_coord = mix(p1.tex_coord, p2.tex_coord, a); + for (size_t i = 0; i < NUM_SAMPLERS; ++i) + out.tex_coords[i] = mix(p1.tex_coords[i], p2.tex_coords[i], a); out.normal = mix(p1.normal, p2.normal, a); return out; } diff --git a/Userland/Libraries/LibSoftGPU/Device.cpp b/Userland/Libraries/LibSoftGPU/Device.cpp index 5e11bcf168..944cbfd226 100644 --- a/Userland/Libraries/LibSoftGPU/Device.cpp +++ b/Userland/Libraries/LibSoftGPU/Device.cpp @@ -522,7 +522,8 @@ void Device::rasterize_triangle(const Triangle& triangle) quad.vertex_color = expand4(vertex0.color); } - quad.uv = interpolate(expand4(vertex0.tex_coord), expand4(vertex1.tex_coord), expand4(vertex2.tex_coord), quad.barycentrics); + for (size_t i = 0; i < NUM_SAMPLERS; ++i) + quad.texture_coordinates[i] = interpolate(expand4(vertex0.tex_coords[i]), expand4(vertex1.tex_coords[i]), expand4(vertex2.tex_coords[i]), quad.barycentrics); if (m_options.fog_enabled) { // Calculate depth of fragment for fog @@ -667,11 +668,11 @@ static void generate_texture_coordinates(Vertex& vertex, RasterizerOptions const }; auto const enabled_coords = options.texcoord_generation_enabled_coordinates; - vertex.tex_coord = { - ((enabled_coords & TexCoordGenerationCoordinate::S) > 0) ? generate_coordinate(0) : vertex.tex_coord.x(), - ((enabled_coords & TexCoordGenerationCoordinate::T) > 0) ? generate_coordinate(1) : vertex.tex_coord.y(), - ((enabled_coords & TexCoordGenerationCoordinate::R) > 0) ? generate_coordinate(2) : vertex.tex_coord.z(), - ((enabled_coords & TexCoordGenerationCoordinate::Q) > 0) ? generate_coordinate(3) : vertex.tex_coord.w(), + vertex.tex_coords[0] = { + ((enabled_coords & TexCoordGenerationCoordinate::S) > 0) ? generate_coordinate(0) : vertex.tex_coords[0].x(), + ((enabled_coords & TexCoordGenerationCoordinate::T) > 0) ? generate_coordinate(1) : vertex.tex_coords[0].y(), + ((enabled_coords & TexCoordGenerationCoordinate::R) > 0) ? generate_coordinate(2) : vertex.tex_coords[0].z(), + ((enabled_coords & TexCoordGenerationCoordinate::Q) > 0) ? generate_coordinate(3) : vertex.tex_coords[0].w(), }; } @@ -994,10 +995,11 @@ void Device::draw_primitives(PrimitiveType primitive_type, FloatMatrix4x4 const& } // Apply texture transformation - // FIXME: implement multi-texturing: texcoords should be stored per texture unit - triangle.vertices[0].tex_coord = texture_transform * triangle.vertices[0].tex_coord; - triangle.vertices[1].tex_coord = texture_transform * triangle.vertices[1].tex_coord; - triangle.vertices[2].tex_coord = texture_transform * triangle.vertices[2].tex_coord; + for (size_t i = 0; i < NUM_SAMPLERS; ++i) { + triangle.vertices[0].tex_coords[i] = texture_transform * triangle.vertices[0].tex_coords[i]; + triangle.vertices[1].tex_coords[i] = texture_transform * triangle.vertices[1].tex_coords[i]; + triangle.vertices[2].tex_coords[i] = texture_transform * triangle.vertices[2].tex_coords[i]; + } rasterize_triangle(triangle); } @@ -1011,7 +1013,7 @@ ALWAYS_INLINE void Device::shade_fragments(PixelQuad& quad) // FIXME: implement GL_TEXTURE_1D, GL_TEXTURE_3D and GL_TEXTURE_CUBE_MAP auto const& sampler = m_samplers[i]; - auto texel = sampler.sample_2d({ quad.uv.x(), quad.uv.y() }); + auto texel = sampler.sample_2d({ quad.texture_coordinates[i].x(), quad.texture_coordinates[i].y() }); INCREASE_STATISTICS_COUNTER(g_num_sampler_calls, 1); // FIXME: Implement more blend modes diff --git a/Userland/Libraries/LibSoftGPU/PixelQuad.h b/Userland/Libraries/LibSoftGPU/PixelQuad.h index ce2e09e43d..110eb99647 100644 --- a/Userland/Libraries/LibSoftGPU/PixelQuad.h +++ b/Userland/Libraries/LibSoftGPU/PixelQuad.h @@ -10,6 +10,7 @@ #include #include #include +#include namespace SoftGPU { @@ -18,7 +19,7 @@ struct PixelQuad final { Vector3 barycentrics; AK::SIMD::f32x4 depth; Vector4 vertex_color; - Vector4 uv; + Array, NUM_SAMPLERS> texture_coordinates; Vector4 out_color; AK::SIMD::f32x4 fog_depth; AK::SIMD::i32x4 mask; diff --git a/Userland/Libraries/LibSoftGPU/Vertex.h b/Userland/Libraries/LibSoftGPU/Vertex.h index ca98b6145e..323d6b95dc 100644 --- a/Userland/Libraries/LibSoftGPU/Vertex.h +++ b/Userland/Libraries/LibSoftGPU/Vertex.h @@ -7,8 +7,10 @@ #pragma once +#include #include #include +#include namespace SoftGPU { @@ -18,7 +20,7 @@ struct Vertex { FloatVector4 clip_coordinates; FloatVector4 window_coordinates; FloatVector4 color; - FloatVector4 tex_coord; + Array tex_coords; FloatVector3 normal; };