From 62285e0569a926948b14c472fb2290d4acedaa51 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Fri, 17 Feb 2023 17:10:35 +0100 Subject: [PATCH] LibSoftGPU: Use multiplication instead of division for linear fog Sampling profiling shows a reduction of nearly 60% for the linear fog calculation case. --- Userland/Libraries/LibSoftGPU/Device.cpp | 4 +++- Userland/Libraries/LibSoftGPU/Device.h | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibSoftGPU/Device.cpp b/Userland/Libraries/LibSoftGPU/Device.cpp index 846ca34c7af..57b32491216 100644 --- a/Userland/Libraries/LibSoftGPU/Device.cpp +++ b/Userland/Libraries/LibSoftGPU/Device.cpp @@ -1281,7 +1281,7 @@ ALWAYS_INLINE void Device::shade_fragments(PixelQuad& quad) f32x4 factor; switch (m_options.fog_mode) { case GPU::FogMode::Linear: - factor = (m_options.fog_end - quad.fog_depth) / (m_options.fog_end - m_options.fog_start); + factor = (m_options.fog_end - quad.fog_depth) * m_one_over_fog_depth; break; case GPU::FogMode::Exp: { auto argument = -m_options.fog_density * quad.fog_depth; @@ -1555,6 +1555,8 @@ void Device::draw_statistics_overlay(Gfx::Bitmap& target) void Device::set_options(GPU::RasterizerOptions const& options) { m_options = options; + if (m_options.fog_enabled) + m_one_over_fog_depth = 1.f / (m_options.fog_end - m_options.fog_start); } void Device::set_light_model_params(GPU::LightModelParameters const& lighting_model) diff --git a/Userland/Libraries/LibSoftGPU/Device.h b/Userland/Libraries/LibSoftGPU/Device.h index ced6abac168..b53e3246989 100644 --- a/Userland/Libraries/LibSoftGPU/Device.h +++ b/Userland/Libraries/LibSoftGPU/Device.h @@ -110,6 +110,7 @@ private: Vector m_triangle_list; Vector m_processed_triangles; Vector m_clipped_vertices; + float m_one_over_fog_depth; Array m_samplers; bool m_samplers_need_texture_staging { false }; Array m_lights;