diff options
Diffstat (limited to 'src/video_core/swrasterizer')
-rw-r--r-- | src/video_core/swrasterizer/clipper.cpp | 197 | ||||
-rw-r--r-- | src/video_core/swrasterizer/clipper.h | 21 | ||||
-rw-r--r-- | src/video_core/swrasterizer/framebuffer.cpp | 360 | ||||
-rw-r--r-- | src/video_core/swrasterizer/framebuffer.h | 29 | ||||
-rw-r--r-- | src/video_core/swrasterizer/lighting.cpp | 308 | ||||
-rw-r--r-- | src/video_core/swrasterizer/lighting.h | 19 | ||||
-rw-r--r-- | src/video_core/swrasterizer/proctex.cpp | 223 | ||||
-rw-r--r-- | src/video_core/swrasterizer/proctex.h | 16 | ||||
-rw-r--r-- | src/video_core/swrasterizer/rasterizer.cpp | 853 | ||||
-rw-r--r-- | src/video_core/swrasterizer/rasterizer.h | 48 | ||||
-rw-r--r-- | src/video_core/swrasterizer/swrasterizer.cpp | 15 | ||||
-rw-r--r-- | src/video_core/swrasterizer/swrasterizer.h | 27 | ||||
-rw-r--r-- | src/video_core/swrasterizer/texturing.cpp | 244 | ||||
-rw-r--r-- | src/video_core/swrasterizer/texturing.h | 28 |
14 files changed, 0 insertions, 2388 deletions
diff --git a/src/video_core/swrasterizer/clipper.cpp b/src/video_core/swrasterizer/clipper.cpp deleted file mode 100644 index c1ed48398..000000000 --- a/src/video_core/swrasterizer/clipper.cpp +++ /dev/null @@ -1,197 +0,0 @@ -// Copyright 2014 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include <algorithm> -#include <array> -#include <cstddef> -#include <boost/container/static_vector.hpp> -#include <boost/container/vector.hpp> -#include "common/bit_field.h" -#include "common/common_types.h" -#include "common/logging/log.h" -#include "common/vector_math.h" -#include "video_core/pica_state.h" -#include "video_core/pica_types.h" -#include "video_core/shader/shader.h" -#include "video_core/swrasterizer/clipper.h" -#include "video_core/swrasterizer/rasterizer.h" - -using Pica::Rasterizer::Vertex; - -namespace Pica { - -namespace Clipper { - -struct ClippingEdge { -public: - ClippingEdge(Math::Vec4<float24> coeffs, Math::Vec4<float24> bias = Math::Vec4<float24>( - float24::FromFloat32(0), float24::FromFloat32(0), - float24::FromFloat32(0), float24::FromFloat32(0))) - : coeffs(coeffs), bias(bias) {} - - bool IsInside(const Vertex& vertex) const { - return Math::Dot(vertex.pos + bias, coeffs) >= float24::FromFloat32(0); - } - - bool IsOutSide(const Vertex& vertex) const { - return !IsInside(vertex); - } - - Vertex GetIntersection(const Vertex& v0, const Vertex& v1) const { - float24 dp = Math::Dot(v0.pos + bias, coeffs); - float24 dp_prev = Math::Dot(v1.pos + bias, coeffs); - float24 factor = dp_prev / (dp_prev - dp); - - return Vertex::Lerp(factor, v0, v1); - } - -private: - float24 pos; - Math::Vec4<float24> coeffs; - Math::Vec4<float24> bias; -}; - -static void InitScreenCoordinates(Vertex& vtx) { - struct { - float24 halfsize_x; - float24 offset_x; - float24 halfsize_y; - float24 offset_y; - float24 zscale; - float24 offset_z; - } viewport; - - const auto& regs = g_state.regs; - viewport.halfsize_x = float24::FromRaw(regs.rasterizer.viewport_size_x); - viewport.halfsize_y = float24::FromRaw(regs.rasterizer.viewport_size_y); - viewport.offset_x = float24::FromFloat32(static_cast<float>(regs.rasterizer.viewport_corner.x)); - viewport.offset_y = float24::FromFloat32(static_cast<float>(regs.rasterizer.viewport_corner.y)); - - float24 inv_w = float24::FromFloat32(1.f) / vtx.pos.w; - vtx.pos.w = inv_w; - vtx.quat *= inv_w; - vtx.color *= inv_w; - vtx.tc0 *= inv_w; - vtx.tc1 *= inv_w; - vtx.tc0_w *= inv_w; - vtx.view *= inv_w; - vtx.tc2 *= inv_w; - - vtx.screenpos[0] = - (vtx.pos.x * inv_w + float24::FromFloat32(1.0)) * viewport.halfsize_x + viewport.offset_x; - vtx.screenpos[1] = - (vtx.pos.y * inv_w + float24::FromFloat32(1.0)) * viewport.halfsize_y + viewport.offset_y; - vtx.screenpos[2] = vtx.pos.z * inv_w; -} - -void ProcessTriangle(const OutputVertex& v0, const OutputVertex& v1, const OutputVertex& v2) { - using boost::container::static_vector; - - // Clipping a planar n-gon against a plane will remove at least 1 vertex and introduces 2 at - // the new edge (or less in degenerate cases). As such, we can say that each clipping plane - // introduces at most 1 new vertex to the polygon. Since we start with a triangle and have a - // fixed 6 clipping planes, the maximum number of vertices of the clipped polygon is 3 + 6 = 9. - static const size_t MAX_VERTICES = 9; - static_vector<Vertex, MAX_VERTICES> buffer_a = {v0, v1, v2}; - static_vector<Vertex, MAX_VERTICES> buffer_b; - - auto FlipQuaternionIfOpposite = [](auto& a, const auto& b) { - if (Math::Dot(a, b) < float24::Zero()) - a = a * float24::FromFloat32(-1.0f); - }; - - // Flip the quaternions if they are opposite to prevent interpolating them over the wrong - // direction. - FlipQuaternionIfOpposite(buffer_a[1].quat, buffer_a[0].quat); - FlipQuaternionIfOpposite(buffer_a[2].quat, buffer_a[0].quat); - - auto* output_list = &buffer_a; - auto* input_list = &buffer_b; - - // NOTE: We clip against a w=epsilon plane to guarantee that the output has a positive w value. - // TODO: Not sure if this is a valid approach. Also should probably instead use the smallest - // epsilon possible within float24 accuracy. - static const float24 EPSILON = float24::FromFloat32(0.00001f); - static const float24 f0 = float24::FromFloat32(0.0); - static const float24 f1 = float24::FromFloat32(1.0); - static const std::array<ClippingEdge, 7> clipping_edges = {{ - {Math::MakeVec(-f1, f0, f0, f1)}, // x = +w - {Math::MakeVec(f1, f0, f0, f1)}, // x = -w - {Math::MakeVec(f0, -f1, f0, f1)}, // y = +w - {Math::MakeVec(f0, f1, f0, f1)}, // y = -w - {Math::MakeVec(f0, f0, -f1, f0)}, // z = 0 - {Math::MakeVec(f0, f0, f1, f1)}, // z = -w - {Math::MakeVec(f0, f0, f0, f1), Math::Vec4<float24>(f0, f0, f0, EPSILON)}, // w = EPSILON - }}; - - // Simple implementation of the Sutherland-Hodgman clipping algorithm. - // TODO: Make this less inefficient (currently lots of useless buffering overhead happens here) - auto Clip = [&](const ClippingEdge& edge) { - std::swap(input_list, output_list); - output_list->clear(); - - const Vertex* reference_vertex = &input_list->back(); - - for (const auto& vertex : *input_list) { - // NOTE: This algorithm changes vertex order in some cases! - if (edge.IsInside(vertex)) { - if (edge.IsOutSide(*reference_vertex)) { - output_list->push_back(edge.GetIntersection(vertex, *reference_vertex)); - } - - output_list->push_back(vertex); - } else if (edge.IsInside(*reference_vertex)) { - output_list->push_back(edge.GetIntersection(vertex, *reference_vertex)); - } - reference_vertex = &vertex; - } - }; - - for (auto edge : clipping_edges) { - Clip(edge); - - // Need to have at least a full triangle to continue... - if (output_list->size() < 3) - return; - } - - if (g_state.regs.rasterizer.clip_enable) { - ClippingEdge custom_edge{g_state.regs.rasterizer.GetClipCoef()}; - Clip(custom_edge); - - if (output_list->size() < 3) - return; - } - - InitScreenCoordinates((*output_list)[0]); - InitScreenCoordinates((*output_list)[1]); - - for (size_t i = 0; i < output_list->size() - 2; i++) { - Vertex& vtx0 = (*output_list)[0]; - Vertex& vtx1 = (*output_list)[i + 1]; - Vertex& vtx2 = (*output_list)[i + 2]; - - InitScreenCoordinates(vtx2); - - LOG_TRACE(Render_Software, - "Triangle %lu/%lu at position (%.3f, %.3f, %.3f, %.3f), " - "(%.3f, %.3f, %.3f, %.3f), (%.3f, %.3f, %.3f, %.3f) and " - "screen position (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f)", - i + 1, output_list->size() - 2, vtx0.pos.x.ToFloat32(), vtx0.pos.y.ToFloat32(), - vtx0.pos.z.ToFloat32(), vtx0.pos.w.ToFloat32(), vtx1.pos.x.ToFloat32(), - vtx1.pos.y.ToFloat32(), vtx1.pos.z.ToFloat32(), vtx1.pos.w.ToFloat32(), - vtx2.pos.x.ToFloat32(), vtx2.pos.y.ToFloat32(), vtx2.pos.z.ToFloat32(), - vtx2.pos.w.ToFloat32(), vtx0.screenpos.x.ToFloat32(), - vtx0.screenpos.y.ToFloat32(), vtx0.screenpos.z.ToFloat32(), - vtx1.screenpos.x.ToFloat32(), vtx1.screenpos.y.ToFloat32(), - vtx1.screenpos.z.ToFloat32(), vtx2.screenpos.x.ToFloat32(), - vtx2.screenpos.y.ToFloat32(), vtx2.screenpos.z.ToFloat32()); - - Rasterizer::ProcessTriangle(vtx0, vtx1, vtx2); - } -} - -} // namespace - -} // namespace diff --git a/src/video_core/swrasterizer/clipper.h b/src/video_core/swrasterizer/clipper.h deleted file mode 100644 index b51af0af9..000000000 --- a/src/video_core/swrasterizer/clipper.h +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2014 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -namespace Pica { - -namespace Shader { -struct OutputVertex; -} - -namespace Clipper { - -using Shader::OutputVertex; - -void ProcessTriangle(const OutputVertex& v0, const OutputVertex& v1, const OutputVertex& v2); - -} // namespace - -} // namespace diff --git a/src/video_core/swrasterizer/framebuffer.cpp b/src/video_core/swrasterizer/framebuffer.cpp deleted file mode 100644 index f34eab6cf..000000000 --- a/src/video_core/swrasterizer/framebuffer.cpp +++ /dev/null @@ -1,360 +0,0 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include <algorithm> - -#include "common/assert.h" -#include "common/color.h" -#include "common/common_types.h" -#include "common/logging/log.h" -#include "common/math_util.h" -#include "common/vector_math.h" -#include "core/hw/gpu.h" -#include "core/memory.h" -#include "video_core/pica_state.h" -#include "video_core/regs_framebuffer.h" -#include "video_core/swrasterizer/framebuffer.h" -#include "video_core/utils.h" - -namespace Pica { -namespace Rasterizer { - -void DrawPixel(int x, int y, const Math::Vec4<u8>& color) { - const auto& framebuffer = g_state.regs.framebuffer.framebuffer; - const PAddr addr = framebuffer.GetColorBufferPhysicalAddress(); - - // Similarly to textures, the render framebuffer is laid out from bottom to top, too. - // NOTE: The framebuffer height register contains the actual FB height minus one. - y = framebuffer.height - y; - - const u32 coarse_y = y & ~7; - u32 bytes_per_pixel = - GPU::Regs::BytesPerPixel(GPU::Regs::PixelFormat(framebuffer.color_format.Value())); - u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + - coarse_y * framebuffer.width * bytes_per_pixel; - u8* dst_pixel = Memory::GetPhysicalPointer(addr) + dst_offset; - - switch (framebuffer.color_format) { - case FramebufferRegs::ColorFormat::RGBA8: - Color::EncodeRGBA8(color, dst_pixel); - break; - - case FramebufferRegs::ColorFormat::RGB8: - Color::EncodeRGB8(color, dst_pixel); - break; - - case FramebufferRegs::ColorFormat::RGB5A1: - Color::EncodeRGB5A1(color, dst_pixel); - break; - - case FramebufferRegs::ColorFormat::RGB565: - Color::EncodeRGB565(color, dst_pixel); - break; - - case FramebufferRegs::ColorFormat::RGBA4: - Color::EncodeRGBA4(color, dst_pixel); - break; - - default: - LOG_CRITICAL(Render_Software, "Unknown framebuffer color format %x", - framebuffer.color_format.Value()); - UNIMPLEMENTED(); - } -} - -const Math::Vec4<u8> GetPixel(int x, int y) { - const auto& framebuffer = g_state.regs.framebuffer.framebuffer; - const PAddr addr = framebuffer.GetColorBufferPhysicalAddress(); - - y = framebuffer.height - y; - - const u32 coarse_y = y & ~7; - u32 bytes_per_pixel = - GPU::Regs::BytesPerPixel(GPU::Regs::PixelFormat(framebuffer.color_format.Value())); - u32 src_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + - coarse_y * framebuffer.width * bytes_per_pixel; - u8* src_pixel = Memory::GetPhysicalPointer(addr) + src_offset; - - switch (framebuffer.color_format) { - case FramebufferRegs::ColorFormat::RGBA8: - return Color::DecodeRGBA8(src_pixel); - - case FramebufferRegs::ColorFormat::RGB8: - return Color::DecodeRGB8(src_pixel); - - case FramebufferRegs::ColorFormat::RGB5A1: - return Color::DecodeRGB5A1(src_pixel); - - case FramebufferRegs::ColorFormat::RGB565: - return Color::DecodeRGB565(src_pixel); - - case FramebufferRegs::ColorFormat::RGBA4: - return Color::DecodeRGBA4(src_pixel); - - default: - LOG_CRITICAL(Render_Software, "Unknown framebuffer color format %x", - framebuffer.color_format.Value()); - UNIMPLEMENTED(); - } - - return {0, 0, 0, 0}; -} - -u32 GetDepth(int x, int y) { - const auto& framebuffer = g_state.regs.framebuffer.framebuffer; - const PAddr addr = framebuffer.GetDepthBufferPhysicalAddress(); - u8* depth_buffer = Memory::GetPhysicalPointer(addr); - - y = framebuffer.height - y; - - const u32 coarse_y = y & ~7; - u32 bytes_per_pixel = FramebufferRegs::BytesPerDepthPixel(framebuffer.depth_format); - u32 stride = framebuffer.width * bytes_per_pixel; - - u32 src_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * stride; - u8* src_pixel = depth_buffer + src_offset; - - switch (framebuffer.depth_format) { - case FramebufferRegs::DepthFormat::D16: - return Color::DecodeD16(src_pixel); - case FramebufferRegs::DepthFormat::D24: - return Color::DecodeD24(src_pixel); - case FramebufferRegs::DepthFormat::D24S8: - return Color::DecodeD24S8(src_pixel).x; - default: - LOG_CRITICAL(HW_GPU, "Unimplemented depth format %u", framebuffer.depth_format); - UNIMPLEMENTED(); - return 0; - } -} - -u8 GetStencil(int x, int y) { - const auto& framebuffer = g_state.regs.framebuffer.framebuffer; - const PAddr addr = framebuffer.GetDepthBufferPhysicalAddress(); - u8* depth_buffer = Memory::GetPhysicalPointer(addr); - - y = framebuffer.height - y; - - const u32 coarse_y = y & ~7; - u32 bytes_per_pixel = Pica::FramebufferRegs::BytesPerDepthPixel(framebuffer.depth_format); - u32 stride = framebuffer.width * bytes_per_pixel; - - u32 src_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * stride; - u8* src_pixel = depth_buffer + src_offset; - - switch (framebuffer.depth_format) { - case FramebufferRegs::DepthFormat::D24S8: - return Color::DecodeD24S8(src_pixel).y; - - default: - LOG_WARNING( - HW_GPU, - "GetStencil called for function which doesn't have a stencil component (format %u)", - framebuffer.depth_format); - return 0; - } -} - -void SetDepth(int x, int y, u32 value) { - const auto& framebuffer = g_state.regs.framebuffer.framebuffer; - const PAddr addr = framebuffer.GetDepthBufferPhysicalAddress(); - u8* depth_buffer = Memory::GetPhysicalPointer(addr); - - y = framebuffer.height - y; - - const u32 coarse_y = y & ~7; - u32 bytes_per_pixel = FramebufferRegs::BytesPerDepthPixel(framebuffer.depth_format); - u32 stride = framebuffer.width * bytes_per_pixel; - - u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * stride; - u8* dst_pixel = depth_buffer + dst_offset; - - switch (framebuffer.depth_format) { - case FramebufferRegs::DepthFormat::D16: - Color::EncodeD16(value, dst_pixel); - break; - - case FramebufferRegs::DepthFormat::D24: - Color::EncodeD24(value, dst_pixel); - break; - - case FramebufferRegs::DepthFormat::D24S8: - Color::EncodeD24X8(value, dst_pixel); - break; - - default: - LOG_CRITICAL(HW_GPU, "Unimplemented depth format %u", framebuffer.depth_format); - UNIMPLEMENTED(); - break; - } -} - -void SetStencil(int x, int y, u8 value) { - const auto& framebuffer = g_state.regs.framebuffer.framebuffer; - const PAddr addr = framebuffer.GetDepthBufferPhysicalAddress(); - u8* depth_buffer = Memory::GetPhysicalPointer(addr); - - y = framebuffer.height - y; - - const u32 coarse_y = y & ~7; - u32 bytes_per_pixel = Pica::FramebufferRegs::BytesPerDepthPixel(framebuffer.depth_format); - u32 stride = framebuffer.width * bytes_per_pixel; - - u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * stride; - u8* dst_pixel = depth_buffer + dst_offset; - - switch (framebuffer.depth_format) { - case Pica::FramebufferRegs::DepthFormat::D16: - case Pica::FramebufferRegs::DepthFormat::D24: - // Nothing to do - break; - - case Pica::FramebufferRegs::DepthFormat::D24S8: - Color::EncodeX24S8(value, dst_pixel); - break; - - default: - LOG_CRITICAL(HW_GPU, "Unimplemented depth format %u", framebuffer.depth_format); - UNIMPLEMENTED(); - break; - } -} - -u8 PerformStencilAction(FramebufferRegs::StencilAction action, u8 old_stencil, u8 ref) { - switch (action) { - case FramebufferRegs::StencilAction::Keep: - return old_stencil; - - case FramebufferRegs::StencilAction::Zero: - return 0; - - case FramebufferRegs::StencilAction::Replace: - return ref; - - case FramebufferRegs::StencilAction::Increment: - // Saturated increment - return std::min<u8>(old_stencil, 254) + 1; - - case FramebufferRegs::StencilAction::Decrement: - // Saturated decrement - return std::max<u8>(old_stencil, 1) - 1; - - case FramebufferRegs::StencilAction::Invert: - return ~old_stencil; - - case FramebufferRegs::StencilAction::IncrementWrap: - return old_stencil + 1; - - case FramebufferRegs::StencilAction::DecrementWrap: - return old_stencil - 1; - - default: - LOG_CRITICAL(HW_GPU, "Unknown stencil action %x", (int)action); - UNIMPLEMENTED(); - return 0; - } -} - -Math::Vec4<u8> EvaluateBlendEquation(const Math::Vec4<u8>& src, const Math::Vec4<u8>& srcfactor, - const Math::Vec4<u8>& dest, const Math::Vec4<u8>& destfactor, - FramebufferRegs::BlendEquation equation) { - Math::Vec4<int> result; - - auto src_result = (src * srcfactor).Cast<int>(); - auto dst_result = (dest * destfactor).Cast<int>(); - - switch (equation) { - case FramebufferRegs::BlendEquation::Add: - result = (src_result + dst_result) / 255; - break; - - case FramebufferRegs::BlendEquation::Subtract: - result = (src_result - dst_result) / 255; - break; - - case FramebufferRegs::BlendEquation::ReverseSubtract: - result = (dst_result - src_result) / 255; - break; - - // TODO: How do these two actually work? OpenGL doesn't include the blend factors in the - // min/max computations, but is this what the 3DS actually does? - case FramebufferRegs::BlendEquation::Min: - result.r() = std::min(src.r(), dest.r()); - result.g() = std::min(src.g(), dest.g()); - result.b() = std::min(src.b(), dest.b()); - result.a() = std::min(src.a(), dest.a()); - break; - - case FramebufferRegs::BlendEquation::Max: - result.r() = std::max(src.r(), dest.r()); - result.g() = std::max(src.g(), dest.g()); - result.b() = std::max(src.b(), dest.b()); - result.a() = std::max(src.a(), dest.a()); - break; - - default: - LOG_CRITICAL(HW_GPU, "Unknown RGB blend equation %x", equation); - UNIMPLEMENTED(); - } - - return Math::Vec4<u8>(MathUtil::Clamp(result.r(), 0, 255), MathUtil::Clamp(result.g(), 0, 255), - MathUtil::Clamp(result.b(), 0, 255), MathUtil::Clamp(result.a(), 0, 255)); -}; - -u8 LogicOp(u8 src, u8 dest, FramebufferRegs::LogicOp op) { - switch (op) { - case FramebufferRegs::LogicOp::Clear: - return 0; - - case FramebufferRegs::LogicOp::And: - return src & dest; - - case FramebufferRegs::LogicOp::AndReverse: - return src & ~dest; - - case FramebufferRegs::LogicOp::Copy: - return src; - - case FramebufferRegs::LogicOp::Set: - return 255; - - case FramebufferRegs::LogicOp::CopyInverted: - return ~src; - - case FramebufferRegs::LogicOp::NoOp: - return dest; - - case FramebufferRegs::LogicOp::Invert: - return ~dest; - - case FramebufferRegs::LogicOp::Nand: - return ~(src & dest); - - case FramebufferRegs::LogicOp::Or: - return src | dest; - - case FramebufferRegs::LogicOp::Nor: - return ~(src | dest); - - case FramebufferRegs::LogicOp::Xor: - return src ^ dest; - - case FramebufferRegs::LogicOp::Equiv: - return ~(src ^ dest); - - case FramebufferRegs::LogicOp::AndInverted: - return ~src & dest; - - case FramebufferRegs::LogicOp::OrReverse: - return src | ~dest; - - case FramebufferRegs::LogicOp::OrInverted: - return ~src | dest; - } - - UNREACHABLE(); -}; - -} // namespace Rasterizer -} // namespace Pica diff --git a/src/video_core/swrasterizer/framebuffer.h b/src/video_core/swrasterizer/framebuffer.h deleted file mode 100644 index 4a32a4979..000000000 --- a/src/video_core/swrasterizer/framebuffer.h +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include "common/common_types.h" -#include "common/vector_math.h" -#include "video_core/regs_framebuffer.h" - -namespace Pica { -namespace Rasterizer { - -void DrawPixel(int x, int y, const Math::Vec4<u8>& color); -const Math::Vec4<u8> GetPixel(int x, int y); -u32 GetDepth(int x, int y); -u8 GetStencil(int x, int y); -void SetDepth(int x, int y, u32 value); -void SetStencil(int x, int y, u8 value); -u8 PerformStencilAction(FramebufferRegs::StencilAction action, u8 old_stencil, u8 ref); - -Math::Vec4<u8> EvaluateBlendEquation(const Math::Vec4<u8>& src, const Math::Vec4<u8>& srcfactor, - const Math::Vec4<u8>& dest, const Math::Vec4<u8>& destfactor, - FramebufferRegs::BlendEquation equation); - -u8 LogicOp(u8 src, u8 dest, FramebufferRegs::LogicOp op); - -} // namespace Rasterizer -} // namespace Pica diff --git a/src/video_core/swrasterizer/lighting.cpp b/src/video_core/swrasterizer/lighting.cpp deleted file mode 100644 index 5fa748611..000000000 --- a/src/video_core/swrasterizer/lighting.cpp +++ /dev/null @@ -1,308 +0,0 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include "common/math_util.h" -#include "video_core/swrasterizer/lighting.h" - -namespace Pica { - -static float LookupLightingLut(const Pica::State::Lighting& lighting, size_t lut_index, u8 index, - float delta) { - ASSERT_MSG(lut_index < lighting.luts.size(), "Out of range lut"); - ASSERT_MSG(index < lighting.luts[lut_index].size(), "Out of range index"); - - const auto& lut = lighting.luts[lut_index][index]; - - float lut_value = lut.ToFloat(); - float lut_diff = lut.DiffToFloat(); - - return lut_value + lut_diff * delta; -} - -std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors( - const Pica::LightingRegs& lighting, const Pica::State::Lighting& lighting_state, - const Math::Quaternion<float>& normquat, const Math::Vec3<float>& view, - const Math::Vec4<u8> (&texture_color)[4]) { - - Math::Vec3<float> surface_normal; - Math::Vec3<float> surface_tangent; - - if (lighting.config0.bump_mode != LightingRegs::LightingBumpMode::None) { - Math::Vec3<float> perturbation = - texture_color[lighting.config0.bump_selector].xyz().Cast<float>() / 127.5f - - Math::MakeVec(1.0f, 1.0f, 1.0f); - if (lighting.config0.bump_mode == LightingRegs::LightingBumpMode::NormalMap) { - if (!lighting.config0.disable_bump_renorm) { - const float z_square = 1 - perturbation.xy().Length2(); - perturbation.z = std::sqrt(std::max(z_square, 0.0f)); - } - surface_normal = perturbation; - surface_tangent = Math::MakeVec(1.0f, 0.0f, 0.0f); - } else if (lighting.config0.bump_mode == LightingRegs::LightingBumpMode::TangentMap) { - surface_normal = Math::MakeVec(0.0f, 0.0f, 1.0f); - surface_tangent = perturbation; - } else { - LOG_ERROR(HW_GPU, "Unknown bump mode %u", lighting.config0.bump_mode.Value()); - } - } else { - surface_normal = Math::MakeVec(0.0f, 0.0f, 1.0f); - surface_tangent = Math::MakeVec(1.0f, 0.0f, 0.0f); - } - - // Use the normalized the quaternion when performing the rotation - auto normal = Math::QuaternionRotate(normquat, surface_normal); - auto tangent = Math::QuaternionRotate(normquat, surface_tangent); - - Math::Vec4<float> diffuse_sum = {0.0f, 0.0f, 0.0f, 1.0f}; - Math::Vec4<float> specular_sum = {0.0f, 0.0f, 0.0f, 1.0f}; - - for (unsigned light_index = 0; light_index <= lighting.max_light_index; ++light_index) { - unsigned num = lighting.light_enable.GetNum(light_index); - const auto& light_config = lighting.light[num]; - - Math::Vec3<float> refl_value = {}; - Math::Vec3<float> position = {float16::FromRaw(light_config.x).ToFloat32(), - float16::FromRaw(light_config.y).ToFloat32(), - float16::FromRaw(light_config.z).ToFloat32()}; - Math::Vec3<float> light_vector; - - if (light_config.config.directional) - light_vector = position; - else - light_vector = position + view; - - light_vector.Normalize(); - - Math::Vec3<float> norm_view = view.Normalized(); - Math::Vec3<float> half_vector = norm_view + light_vector; - - float dist_atten = 1.0f; - if (!lighting.IsDistAttenDisabled(num)) { - auto distance = (-view - position).Length(); - float scale = Pica::float20::FromRaw(light_config.dist_atten_scale).ToFloat32(); - float bias = Pica::float20::FromRaw(light_config.dist_atten_bias).ToFloat32(); - size_t lut = - static_cast<size_t>(LightingRegs::LightingSampler::DistanceAttenuation) + num; - - float sample_loc = MathUtil::Clamp(scale * distance + bias, 0.0f, 1.0f); - - u8 lutindex = - static_cast<u8>(MathUtil::Clamp(std::floor(sample_loc * 256.0f), 0.0f, 255.0f)); - float delta = sample_loc * 256 - lutindex; - dist_atten = LookupLightingLut(lighting_state, lut, lutindex, delta); - } - - auto GetLutValue = [&](LightingRegs::LightingLutInput input, bool abs, - LightingRegs::LightingScale scale_enum, - LightingRegs::LightingSampler sampler) { - float result = 0.0f; - - switch (input) { - case LightingRegs::LightingLutInput::NH: - result = Math::Dot(normal, half_vector.Normalized()); - break; - - case LightingRegs::LightingLutInput::VH: - result = Math::Dot(norm_view, half_vector.Normalized()); - break; - - case LightingRegs::LightingLutInput::NV: - result = Math::Dot(normal, norm_view); - break; - - case LightingRegs::LightingLutInput::LN: - result = Math::Dot(light_vector, normal); - break; - - case LightingRegs::LightingLutInput::SP: { - Math::Vec3<s32> spot_dir{light_config.spot_x.Value(), light_config.spot_y.Value(), - light_config.spot_z.Value()}; - result = Math::Dot(light_vector, spot_dir.Cast<float>() / 2047.0f); - break; - } - case LightingRegs::LightingLutInput::CP: - if (lighting.config0.config == LightingRegs::LightingConfig::Config7) { - const Math::Vec3<float> norm_half_vector = half_vector.Normalized(); - const Math::Vec3<float> half_vector_proj = - norm_half_vector - normal * Math::Dot(normal, norm_half_vector); - result = Math::Dot(half_vector_proj, tangent); - } else { - result = 0.0f; - } - break; - default: - LOG_CRITICAL(HW_GPU, "Unknown lighting LUT input %u\n", static_cast<u32>(input)); - UNIMPLEMENTED(); - result = 0.0f; - } - - u8 index; - float delta; - - if (abs) { - if (light_config.config.two_sided_diffuse) - result = std::abs(result); - else - result = std::max(result, 0.0f); - - float flr = std::floor(result * 256.0f); - index = static_cast<u8>(MathUtil::Clamp(flr, 0.0f, 255.0f)); - delta = result * 256 - index; - } else { - float flr = std::floor(result * 128.0f); - s8 signed_index = static_cast<s8>(MathUtil::Clamp(flr, -128.0f, 127.0f)); - delta = result * 128.0f - signed_index; - index = static_cast<u8>(signed_index); - } - - float scale = lighting.lut_scale.GetScale(scale_enum); - return scale * - LookupLightingLut(lighting_state, static_cast<size_t>(sampler), index, delta); - }; - - // If enabled, compute spot light attenuation value - float spot_atten = 1.0f; - if (!lighting.IsSpotAttenDisabled(num) && - LightingRegs::IsLightingSamplerSupported( - lighting.config0.config, LightingRegs::LightingSampler::SpotlightAttenuation)) { - auto lut = LightingRegs::SpotlightAttenuationSampler(num); - spot_atten = GetLutValue(lighting.lut_input.sp, lighting.abs_lut_input.disable_sp == 0, - lighting.lut_scale.sp, lut); - } - - // Specular 0 component - float d0_lut_value = 1.0f; - if (lighting.config1.disable_lut_d0 == 0 && - LightingRegs::IsLightingSamplerSupported( - lighting.config0.config, LightingRegs::LightingSampler::Distribution0)) { - d0_lut_value = - GetLutValue(lighting.lut_input.d0, lighting.abs_lut_input.disable_d0 == 0, - lighting.lut_scale.d0, LightingRegs::LightingSampler::Distribution0); - } - - Math::Vec3<float> specular_0 = d0_lut_value * light_config.specular_0.ToVec3f(); - - // If enabled, lookup ReflectRed value, otherwise, 1.0 is used - if (lighting.config1.disable_lut_rr == 0 && - LightingRegs::IsLightingSamplerSupported(lighting.config0.config, - LightingRegs::LightingSampler::ReflectRed)) { - refl_value.x = - GetLutValue(lighting.lut_input.rr, lighting.abs_lut_input.disable_rr == 0, - lighting.lut_scale.rr, LightingRegs::LightingSampler::ReflectRed); - } else { - refl_value.x = 1.0f; - } - - // If enabled, lookup ReflectGreen value, otherwise, ReflectRed value is used - if (lighting.config1.disable_lut_rg == 0 && - LightingRegs::IsLightingSamplerSupported(lighting.config0.config, - LightingRegs::LightingSampler::ReflectGreen)) { - refl_value.y = - GetLutValue(lighting.lut_input.rg, lighting.abs_lut_input.disable_rg == 0, - lighting.lut_scale.rg, LightingRegs::LightingSampler::ReflectGreen); - } else { - refl_value.y = refl_value.x; - } - - // If enabled, lookup ReflectBlue value, otherwise, ReflectRed value is used - if (lighting.config1.disable_lut_rb == 0 && - LightingRegs::IsLightingSamplerSupported(lighting.config0.config, - LightingRegs::LightingSampler::ReflectBlue)) { - refl_value.z = - GetLutValue(lighting.lut_input.rb, lighting.abs_lut_input.disable_rb == 0, - lighting.lut_scale.rb, LightingRegs::LightingSampler::ReflectBlue); - } else { - refl_value.z = refl_value.x; - } - - // Specular 1 component - float d1_lut_value = 1.0f; - if (lighting.config1.disable_lut_d1 == 0 && - LightingRegs::IsLightingSamplerSupported( - lighting.config0.config, LightingRegs::LightingSampler::Distribution1)) { - d1_lut_value = - GetLutValue(lighting.lut_input.d1, lighting.abs_lut_input.disable_d1 == 0, - lighting.lut_scale.d1, LightingRegs::LightingSampler::Distribution1); - } - - Math::Vec3<float> specular_1 = - d1_lut_value * refl_value * light_config.specular_1.ToVec3f(); - - // Fresnel - // Note: only the last entry in the light slots applies the Fresnel factor - if (light_index == lighting.max_light_index && lighting.config1.disable_lut_fr == 0 && - LightingRegs::IsLightingSamplerSupported(lighting.config0.config, - LightingRegs::LightingSampler::Fresnel)) { - - float lut_value = - GetLutValue(lighting.lut_input.fr, lighting.abs_lut_input.disable_fr == 0, - lighting.lut_scale.fr, LightingRegs::LightingSampler::Fresnel); - - // Enabled for diffuse lighting alpha component - if (lighting.config0.fresnel_selector == - LightingRegs::LightingFresnelSelector::PrimaryAlpha || - lighting.config0.fresnel_selector == LightingRegs::LightingFresnelSelector::Both) { - diffuse_sum.a() = lut_value; - } - - // Enabled for the specular lighting alpha component - if (lighting.config0.fresnel_selector == - LightingRegs::LightingFresnelSelector::SecondaryAlpha || - lighting.config0.fresnel_selector == LightingRegs::LightingFresnelSelector::Both) { - specular_sum.a() = lut_value; - } - } - - auto dot_product = Math::Dot(light_vector, normal); - - // Calculate clamp highlights before applying the two-sided diffuse configuration to the dot - // product. - float clamp_highlights = 1.0f; - if (lighting.config0.clamp_highlights) { - if (dot_product <= 0.0f) - clamp_highlights = 0.0f; - else - clamp_highlights = 1.0f; - } - - if (light_config.config.two_sided_diffuse) - dot_product = std::abs(dot_product); - else - dot_product = std::max(dot_product, 0.0f); - - if (light_config.config.geometric_factor_0 || light_config.config.geometric_factor_1) { - float geo_factor = half_vector.Length2(); - geo_factor = geo_factor == 0.0f ? 0.0f : std::min(dot_product / geo_factor, 1.0f); - if (light_config.config.geometric_factor_0) { - specular_0 *= geo_factor; - } - if (light_config.config.geometric_factor_1) { - specular_1 *= geo_factor; - } - } - - auto diffuse = - light_config.diffuse.ToVec3f() * dot_product + light_config.ambient.ToVec3f(); - diffuse_sum += Math::MakeVec(diffuse * dist_atten * spot_atten, 0.0f); - - specular_sum += Math::MakeVec( - (specular_0 + specular_1) * clamp_highlights * dist_atten * spot_atten, 0.0f); - } - - diffuse_sum += Math::MakeVec(lighting.global_ambient.ToVec3f(), 0.0f); - - auto diffuse = Math::MakeVec<float>(MathUtil::Clamp(diffuse_sum.x, 0.0f, 1.0f) * 255, - MathUtil::Clamp(diffuse_sum.y, 0.0f, 1.0f) * 255, - MathUtil::Clamp(diffuse_sum.z, 0.0f, 1.0f) * 255, - MathUtil::Clamp(diffuse_sum.w, 0.0f, 1.0f) * 255) - .Cast<u8>(); - auto specular = Math::MakeVec<float>(MathUtil::Clamp(specular_sum.x, 0.0f, 1.0f) * 255, - MathUtil::Clamp(specular_sum.y, 0.0f, 1.0f) * 255, - MathUtil::Clamp(specular_sum.z, 0.0f, 1.0f) * 255, - MathUtil::Clamp(specular_sum.w, 0.0f, 1.0f) * 255) - .Cast<u8>(); - return std::make_tuple(diffuse, specular); -} - -} // namespace Pica diff --git a/src/video_core/swrasterizer/lighting.h b/src/video_core/swrasterizer/lighting.h deleted file mode 100644 index d807a3d94..000000000 --- a/src/video_core/swrasterizer/lighting.h +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include <tuple> -#include "common/quaternion.h" -#include "common/vector_math.h" -#include "video_core/pica_state.h" - -namespace Pica { - -std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors( - const Pica::LightingRegs& lighting, const Pica::State::Lighting& lighting_state, - const Math::Quaternion<float>& normquat, const Math::Vec3<float>& view, - const Math::Vec4<u8> (&texture_color)[4]); - -} // namespace Pica diff --git a/src/video_core/swrasterizer/proctex.cpp b/src/video_core/swrasterizer/proctex.cpp deleted file mode 100644 index b69892778..000000000 --- a/src/video_core/swrasterizer/proctex.cpp +++ /dev/null @@ -1,223 +0,0 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include <array> -#include <cmath> -#include "common/math_util.h" -#include "video_core/swrasterizer/proctex.h" - -namespace Pica { -namespace Rasterizer { - -using ProcTexClamp = TexturingRegs::ProcTexClamp; -using ProcTexShift = TexturingRegs::ProcTexShift; -using ProcTexCombiner = TexturingRegs::ProcTexCombiner; -using ProcTexFilter = TexturingRegs::ProcTexFilter; - -static float LookupLUT(const std::array<State::ProcTex::ValueEntry, 128>& lut, float coord) { - // For NoiseLUT/ColorMap/AlphaMap, coord=0.0 is lut[0], coord=127.0/128.0 is lut[127] and - // coord=1.0 is lut[127]+lut_diff[127]. For other indices, the result is interpolated using - // value entries and difference entries. - coord *= 128; - const int index_int = std::min(static_cast<int>(coord), 127); - const float frac = coord - index_int; - return lut[index_int].ToFloat() + frac * lut[index_int].DiffToFloat(); -} - -// These function are used to generate random noise for procedural texture. Their results are -// verified against real hardware, but it's not known if the algorithm is the same as hardware. -static unsigned int NoiseRand1D(unsigned int v) { - static constexpr std::array<unsigned int, 16> table{ - {0, 4, 10, 8, 4, 9, 7, 12, 5, 15, 13, 14, 11, 15, 2, 11}}; - return ((v % 9 + 2) * 3 & 0xF) ^ table[(v / 9) & 0xF]; -} - -static float NoiseRand2D(unsigned int x, unsigned int y) { - static constexpr std::array<unsigned int, 16> table{ - {10, 2, 15, 8, 0, 7, 4, 5, 5, 13, 2, 6, 13, 9, 3, 14}}; - unsigned int u2 = NoiseRand1D(x); - unsigned int v2 = NoiseRand1D(y); - v2 += ((u2 & 3) == 1) ? 4 : 0; - v2 ^= (u2 & 1) * 6; - v2 += 10 + u2; - v2 &= 0xF; - v2 ^= table[u2]; - return -1.0f + v2 * 2.0f / 15.0f; -} - -static float NoiseCoef(float u, float v, TexturingRegs regs, State::ProcTex state) { - const float freq_u = float16::FromRaw(regs.proctex_noise_frequency.u).ToFloat32(); - const float freq_v = float16::FromRaw(regs.proctex_noise_frequency.v).ToFloat32(); - const float phase_u = float16::FromRaw(regs.proctex_noise_u.phase).ToFloat32(); - const float phase_v = float16::FromRaw(regs.proctex_noise_v.phase).ToFloat32(); - const float x = 9 * freq_u * std::abs(u + phase_u); - const float y = 9 * freq_v * std::abs(v + phase_v); - const int x_int = static_cast<int>(x); - const int y_int = static_cast<int>(y); - const float x_frac = x - x_int; - const float y_frac = y - y_int; - - const float g0 = NoiseRand2D(x_int, y_int) * (x_frac + y_frac); - const float g1 = NoiseRand2D(x_int + 1, y_int) * (x_frac + y_frac - 1); - const float g2 = NoiseRand2D(x_int, y_int + 1) * (x_frac + y_frac - 1); - const float g3 = NoiseRand2D(x_int + 1, y_int + 1) * (x_frac + y_frac - 2); - const float x_noise = LookupLUT(state.noise_table, x_frac); - const float y_noise = LookupLUT(state.noise_table, y_frac); - return Math::BilinearInterp(g0, g1, g2, g3, x_noise, y_noise); -} - -static float GetShiftOffset(float v, ProcTexShift mode, ProcTexClamp clamp_mode) { - const float offset = (clamp_mode == ProcTexClamp::MirroredRepeat) ? 1 : 0.5f; - switch (mode) { - case ProcTexShift::None: - return 0; - case ProcTexShift::Odd: - return offset * (((int)v / 2) % 2); - case ProcTexShift::Even: - return offset * ((((int)v + 1) / 2) % 2); - default: - LOG_CRITICAL(HW_GPU, "Unknown shift mode %u", static_cast<u32>(mode)); - return 0; - } -}; - -static void ClampCoord(float& coord, ProcTexClamp mode) { - switch (mode) { - case ProcTexClamp::ToZero: - if (coord > 1.0f) - coord = 0.0f; - break; - case ProcTexClamp::ToEdge: - coord = std::min(coord, 1.0f); - break; - case ProcTexClamp::SymmetricalRepeat: - coord = coord - std::floor(coord); - break; - case ProcTexClamp::MirroredRepeat: { - int integer = static_cast<int>(coord); - float frac = coord - integer; - coord = (integer % 2) == 0 ? frac : (1.0f - frac); - break; - } - case ProcTexClamp::Pulse: - if (coord <= 0.5f) - coord = 0.0f; - else - coord = 1.0f; - break; - default: - LOG_CRITICAL(HW_GPU, "Unknown clamp mode %u", static_cast<u32>(mode)); - coord = std::min(coord, 1.0f); - break; - } -} - -float CombineAndMap(float u, float v, ProcTexCombiner combiner, - const std::array<State::ProcTex::ValueEntry, 128>& map_table) { - float f; - switch (combiner) { - case ProcTexCombiner::U: - f = u; - break; - case ProcTexCombiner::U2: - f = u * u; - break; - case TexturingRegs::ProcTexCombiner::V: - f = v; - break; - case TexturingRegs::ProcTexCombiner::V2: - f = v * v; - break; - case TexturingRegs::ProcTexCombiner::Add: - f = (u + v) * 0.5f; - break; - case TexturingRegs::ProcTexCombiner::Add2: - f = (u * u + v * v) * 0.5f; - break; - case TexturingRegs::ProcTexCombiner::SqrtAdd2: - f = std::min(std::sqrt(u * u + v * v), 1.0f); - break; - case TexturingRegs::ProcTexCombiner::Min: - f = std::min(u, v); - break; - case TexturingRegs::ProcTexCombiner::Max: - f = std::max(u, v); - break; - case TexturingRegs::ProcTexCombiner::RMax: - f = std::min(((u + v) * 0.5f + std::sqrt(u * u + v * v)) * 0.5f, 1.0f); - break; - default: - LOG_CRITICAL(HW_GPU, "Unknown combiner %u", static_cast<u32>(combiner)); - f = 0.0f; - break; - } - return LookupLUT(map_table, f); -} - -Math::Vec4<u8> ProcTex(float u, float v, TexturingRegs regs, State::ProcTex state) { - u = std::abs(u); - v = std::abs(v); - - // Get shift offset before noise generation - const float u_shift = GetShiftOffset(v, regs.proctex.u_shift, regs.proctex.u_clamp); - const float v_shift = GetShiftOffset(u, regs.proctex.v_shift, regs.proctex.v_clamp); - - // Generate noise - if (regs.proctex.noise_enable) { - float noise = NoiseCoef(u, v, regs, state); - u += noise * regs.proctex_noise_u.amplitude / 4095.0f; - v += noise * regs.proctex_noise_v.amplitude / 4095.0f; - u = std::abs(u); - v = std::abs(v); - } - - // Shift - u += u_shift; - v += v_shift; - - // Clamp - ClampCoord(u, regs.proctex.u_clamp); - ClampCoord(v, regs.proctex.v_clamp); - - // Combine and map - const float lut_coord = CombineAndMap(u, v, regs.proctex.color_combiner, state.color_map_table); - - // Look up the color - // For the color lut, coord=0.0 is lut[offset] and coord=1.0 is lut[offset+width-1] - const u32 offset = regs.proctex_lut_offset; - const u32 width = regs.proctex_lut.width; - const float index = offset + (lut_coord * (width - 1)); - Math::Vec4<u8> final_color; - // TODO(wwylele): implement mipmap - switch (regs.proctex_lut.filter) { - case ProcTexFilter::Linear: - case ProcTexFilter::LinearMipmapLinear: - case ProcTexFilter::LinearMipmapNearest: { - const int index_int = static_cast<int>(index); - const float frac = index - index_int; - const auto color_value = state.color_table[index_int].ToVector().Cast<float>(); - const auto color_diff = state.color_diff_table[index_int].ToVector().Cast<float>(); - final_color = (color_value + frac * color_diff).Cast<u8>(); - break; - } - case ProcTexFilter::Nearest: - case ProcTexFilter::NearestMipmapLinear: - case ProcTexFilter::NearestMipmapNearest: - final_color = state.color_table[static_cast<int>(std::round(index))].ToVector(); - break; - } - - if (regs.proctex.separate_alpha) { - // Note: in separate alpha mode, the alpha channel skips the color LUT look up stage. It - // uses the output of CombineAndMap directly instead. - const float final_alpha = - CombineAndMap(u, v, regs.proctex.alpha_combiner, state.alpha_map_table); - return Math::MakeVec<u8>(final_color.rgb(), static_cast<u8>(final_alpha * 255)); - } else { - return final_color; - } -} - -} // namespace Rasterizer -} // namespace Pica diff --git a/src/video_core/swrasterizer/proctex.h b/src/video_core/swrasterizer/proctex.h deleted file mode 100644 index 036e4620e..000000000 --- a/src/video_core/swrasterizer/proctex.h +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include "common/common_types.h" -#include "common/vector_math.h" -#include "video_core/pica_state.h" - -namespace Pica { -namespace Rasterizer { - -/// Generates procedural texture color for the given coordinates -Math::Vec4<u8> ProcTex(float u, float v, TexturingRegs regs, State::ProcTex state); - -} // namespace Rasterizer -} // namespace Pica diff --git a/src/video_core/swrasterizer/rasterizer.cpp b/src/video_core/swrasterizer/rasterizer.cpp deleted file mode 100644 index 862135614..000000000 --- a/src/video_core/swrasterizer/rasterizer.cpp +++ /dev/null @@ -1,853 +0,0 @@ -// Copyright 2014 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include <algorithm> -#include <array> -#include <cmath> -#include <tuple> -#include "common/assert.h" -#include "common/bit_field.h" -#include "common/color.h" -#include "common/common_types.h" -#include "common/logging/log.h" -#include "common/math_util.h" -#include "common/microprofile.h" -#include "common/quaternion.h" -#include "common/vector_math.h" -#include "core/hw/gpu.h" -#include "core/memory.h" -#include "video_core/debug_utils/debug_utils.h" -#include "video_core/pica_state.h" -#include "video_core/pica_types.h" -#include "video_core/regs_framebuffer.h" -#include "video_core/regs_rasterizer.h" -#include "video_core/regs_texturing.h" -#include "video_core/shader/shader.h" -#include "video_core/swrasterizer/framebuffer.h" -#include "video_core/swrasterizer/lighting.h" -#include "video_core/swrasterizer/proctex.h" -#include "video_core/swrasterizer/rasterizer.h" -#include "video_core/swrasterizer/texturing.h" -#include "video_core/texture/texture_decode.h" -#include "video_core/utils.h" - -namespace Pica { -namespace Rasterizer { - -// NOTE: Assuming that rasterizer coordinates are 12.4 fixed-point values -struct Fix12P4 { - Fix12P4() {} - Fix12P4(u16 val) : val(val) {} - - static u16 FracMask() { - return 0xF; - } - static u16 IntMask() { - return (u16)~0xF; - } - - operator u16() const { - return val; - } - - bool operator<(const Fix12P4& oth) const { - return (u16) * this < (u16)oth; - } - -private: - u16 val; -}; - -/** - * Calculate signed area of the triangle spanned by the three argument vertices. - * The sign denotes an orientation. - * - * @todo define orientation concretely. - */ -static int SignedArea(const Math::Vec2<Fix12P4>& vtx1, const Math::Vec2<Fix12P4>& vtx2, - const Math::Vec2<Fix12P4>& vtx3) { - const auto vec1 = Math::MakeVec(vtx2 - vtx1, 0); - const auto vec2 = Math::MakeVec(vtx3 - vtx1, 0); - // TODO: There is a very small chance this will overflow for sizeof(int) == 4 - return Math::Cross(vec1, vec2).z; -}; - -/// Convert a 3D vector for cube map coordinates to 2D texture coordinates along with the face name -static std::tuple<float24, float24, PAddr> ConvertCubeCoord(float24 u, float24 v, float24 w, - const TexturingRegs& regs) { - const float abs_u = std::abs(u.ToFloat32()); - const float abs_v = std::abs(v.ToFloat32()); - const float abs_w = std::abs(w.ToFloat32()); - float24 x, y, z; - PAddr addr; - if (abs_u > abs_v && abs_u > abs_w) { - if (u > float24::FromFloat32(0)) { - addr = regs.GetCubePhysicalAddress(TexturingRegs::CubeFace::PositiveX); - y = -v; - } else { - addr = regs.GetCubePhysicalAddress(TexturingRegs::CubeFace::NegativeX); - y = v; - } - x = -w; - z = u; - } else if (abs_v > abs_w) { - if (v > float24::FromFloat32(0)) { - addr = regs.GetCubePhysicalAddress(TexturingRegs::CubeFace::PositiveY); - x = u; - } else { - addr = regs.GetCubePhysicalAddress(TexturingRegs::CubeFace::NegativeY); - x = -u; - } - y = w; - z = v; - } else { - if (w > float24::FromFloat32(0)) { - addr = regs.GetCubePhysicalAddress(TexturingRegs::CubeFace::PositiveZ); - y = -v; - } else { - addr = regs.GetCubePhysicalAddress(TexturingRegs::CubeFace::NegativeZ); - y = v; - } - x = u; - z = w; - } - const float24 half = float24::FromFloat32(0.5f); - return std::make_tuple(x / z * half + half, y / z * half + half, addr); -} - -MICROPROFILE_DEFINE(GPU_Rasterization, "GPU", "Rasterization", MP_RGB(50, 50, 240)); - -/** - * Helper function for ProcessTriangle with the "reversed" flag to allow for implementing - * culling via recursion. - */ -static void ProcessTriangleInternal(const Vertex& v0, const Vertex& v1, const Vertex& v2, - bool reversed = false) { - const auto& regs = g_state.regs; - MICROPROFILE_SCOPE(GPU_Rasterization); - - // vertex positions in rasterizer coordinates - static auto FloatToFix = [](float24 flt) { - // TODO: Rounding here is necessary to prevent garbage pixels at - // triangle borders. Is it that the correct solution, though? - return Fix12P4(static_cast<unsigned short>(round(flt.ToFloat32() * 16.0f))); - }; - static auto ScreenToRasterizerCoordinates = [](const Math::Vec3<float24>& vec) { - return Math::Vec3<Fix12P4>{FloatToFix(vec.x), FloatToFix(vec.y), FloatToFix(vec.z)}; - }; - - Math::Vec3<Fix12P4> vtxpos[3]{ScreenToRasterizerCoordinates(v0.screenpos), - ScreenToRasterizerCoordinates(v1.screenpos), - ScreenToRasterizerCoordinates(v2.screenpos)}; - - if (regs.rasterizer.cull_mode == RasterizerRegs::CullMode::KeepAll) { - // Make sure we always end up with a triangle wound counter-clockwise - if (!reversed && SignedArea(vtxpos[0].xy(), vtxpos[1].xy(), vtxpos[2].xy()) <= 0) { - ProcessTriangleInternal(v0, v2, v1, true); - return; - } - } else { - if (!reversed && regs.rasterizer.cull_mode == RasterizerRegs::CullMode::KeepClockWise) { - // Reverse vertex order and use the CCW code path. - ProcessTriangleInternal(v0, v2, v1, true); - return; - } - - // Cull away triangles which are wound clockwise. - if (SignedArea(vtxpos[0].xy(), vtxpos[1].xy(), vtxpos[2].xy()) <= 0) - return; - } - - u16 min_x = std::min({vtxpos[0].x, vtxpos[1].x, vtxpos[2].x}); - u16 min_y = std::min({vtxpos[0].y, vtxpos[1].y, vtxpos[2].y}); - u16 max_x = std::max({vtxpos[0].x, vtxpos[1].x, vtxpos[2].x}); - u16 max_y = std::max({vtxpos[0].y, vtxpos[1].y, vtxpos[2].y}); - - // Convert the scissor box coordinates to 12.4 fixed point - u16 scissor_x1 = (u16)(regs.rasterizer.scissor_test.x1 << 4); - u16 scissor_y1 = (u16)(regs.rasterizer.scissor_test.y1 << 4); - // x2,y2 have +1 added to cover the entire sub-pixel area - u16 scissor_x2 = (u16)((regs.rasterizer.scissor_test.x2 + 1) << 4); - u16 scissor_y2 = (u16)((regs.rasterizer.scissor_test.y2 + 1) << 4); - - if (regs.rasterizer.scissor_test.mode == RasterizerRegs::ScissorMode::Include) { - // Calculate the new bounds - min_x = std::max(min_x, scissor_x1); - min_y = std::max(min_y, scissor_y1); - max_x = std::min(max_x, scissor_x2); - max_y = std::min(max_y, scissor_y2); - } - - min_x &= Fix12P4::IntMask(); - min_y &= Fix12P4::IntMask(); - max_x = ((max_x + Fix12P4::FracMask()) & Fix12P4::IntMask()); - max_y = ((max_y + Fix12P4::FracMask()) & Fix12P4::IntMask()); - - // Triangle filling rules: Pixels on the right-sided edge or on flat bottom edges are not - // drawn. Pixels on any other triangle border are drawn. This is implemented with three bias - // values which are added to the barycentric coordinates w0, w1 and w2, respectively. - // NOTE: These are the PSP filling rules. Not sure if the 3DS uses the same ones... - auto IsRightSideOrFlatBottomEdge = [](const Math::Vec2<Fix12P4>& vtx, - const Math::Vec2<Fix12P4>& line1, - const Math::Vec2<Fix12P4>& line2) { - if (line1.y == line2.y) { - // just check if vertex is above us => bottom line parallel to x-axis - return vtx.y < line1.y; - } else { - // check if vertex is on our left => right side - // TODO: Not sure how likely this is to overflow - return (int)vtx.x < (int)line1.x + - ((int)line2.x - (int)line1.x) * ((int)vtx.y - (int)line1.y) / - ((int)line2.y - (int)line1.y); - } - }; - int bias0 = - IsRightSideOrFlatBottomEdge(vtxpos[0].xy(), vtxpos[1].xy(), vtxpos[2].xy()) ? -1 : 0; - int bias1 = - IsRightSideOrFlatBottomEdge(vtxpos[1].xy(), vtxpos[2].xy(), vtxpos[0].xy()) ? -1 : 0; - int bias2 = - IsRightSideOrFlatBottomEdge(vtxpos[2].xy(), vtxpos[0].xy(), vtxpos[1].xy()) ? -1 : 0; - - auto w_inverse = Math::MakeVec(v0.pos.w, v1.pos.w, v2.pos.w); - - auto textures = regs.texturing.GetTextures(); - auto tev_stages = regs.texturing.GetTevStages(); - - bool stencil_action_enable = - g_state.regs.framebuffer.output_merger.stencil_test.enable && - g_state.regs.framebuffer.framebuffer.depth_format == FramebufferRegs::DepthFormat::D24S8; - const auto stencil_test = g_state.regs.framebuffer.output_merger.stencil_test; - - // Enter rasterization loop, starting at the center of the topleft bounding box corner. - // TODO: Not sure if looping through x first might be faster - for (u16 y = min_y + 8; y < max_y; y += 0x10) { - for (u16 x = min_x + 8; x < max_x; x += 0x10) { - - // Do not process the pixel if it's inside the scissor box and the scissor mode is set - // to Exclude - if (regs.rasterizer.scissor_test.mode == RasterizerRegs::ScissorMode::Exclude) { - if (x >= scissor_x1 && x < scissor_x2 && y >= scissor_y1 && y < scissor_y2) - continue; - } - - // Calculate the barycentric coordinates w0, w1 and w2 - int w0 = bias0 + SignedArea(vtxpos[1].xy(), vtxpos[2].xy(), {x, y}); - int w1 = bias1 + SignedArea(vtxpos[2].xy(), vtxpos[0].xy(), {x, y}); - int w2 = bias2 + SignedArea(vtxpos[0].xy(), vtxpos[1].xy(), {x, y}); - int wsum = w0 + w1 + w2; - - // If current pixel is not covered by the current primitive - if (w0 < 0 || w1 < 0 || w2 < 0) - continue; - - auto baricentric_coordinates = - Math::MakeVec(float24::FromFloat32(static_cast<float>(w0)), - float24::FromFloat32(static_cast<float>(w1)), - float24::FromFloat32(static_cast<float>(w2))); - float24 interpolated_w_inverse = - float24::FromFloat32(1.0f) / Math::Dot(w_inverse, baricentric_coordinates); - - // interpolated_z = z / w - float interpolated_z_over_w = - (v0.screenpos[2].ToFloat32() * w0 + v1.screenpos[2].ToFloat32() * w1 + - v2.screenpos[2].ToFloat32() * w2) / - wsum; - - // Not fully accurate. About 3 bits in precision are missing. - // Z-Buffer (z / w * scale + offset) - float depth_scale = float24::FromRaw(regs.rasterizer.viewport_depth_range).ToFloat32(); - float depth_offset = - float24::FromRaw(regs.rasterizer.viewport_depth_near_plane).ToFloat32(); - float depth = interpolated_z_over_w * depth_scale + depth_offset; - - // Potentially switch to W-Buffer - if (regs.rasterizer.depthmap_enable == - Pica::RasterizerRegs::DepthBuffering::WBuffering) { - // W-Buffer (z * scale + w * offset = (z / w * scale + offset) * w) - depth *= interpolated_w_inverse.ToFloat32() * wsum; - } - - // Clamp the result - depth = MathUtil::Clamp(depth, 0.0f, 1.0f); - - // Perspective correct attribute interpolation: - // Attribute values cannot be calculated by simple linear interpolation since - // they are not linear in screen space. For example, when interpolating a - // texture coordinate across two vertices, something simple like - // u = (u0*w0 + u1*w1)/(w0+w1) - // will not work. However, the attribute value divided by the - // clipspace w-coordinate (u/w) and and the inverse w-coordinate (1/w) are linear - // in screenspace. Hence, we can linearly interpolate these two independently and - // calculate the interpolated attribute by dividing the results. - // I.e. - // u_over_w = ((u0/v0.pos.w)*w0 + (u1/v1.pos.w)*w1)/(w0+w1) - // one_over_w = (( 1/v0.pos.w)*w0 + ( 1/v1.pos.w)*w1)/(w0+w1) - // u = u_over_w / one_over_w - // - // The generalization to three vertices is straightforward in baricentric coordinates. - auto GetInterpolatedAttribute = [&](float24 attr0, float24 attr1, float24 attr2) { - auto attr_over_w = Math::MakeVec(attr0, attr1, attr2); - float24 interpolated_attr_over_w = Math::Dot(attr_over_w, baricentric_coordinates); - return interpolated_attr_over_w * interpolated_w_inverse; - }; - - Math::Vec4<u8> primary_color{ - (u8)( - GetInterpolatedAttribute(v0.color.r(), v1.color.r(), v2.color.r()).ToFloat32() * - 255), - (u8)( - GetInterpolatedAttribute(v0.color.g(), v1.color.g(), v2.color.g()).ToFloat32() * - 255), - (u8)( - GetInterpolatedAttribute(v0.color.b(), v1.color.b(), v2.color.b()).ToFloat32() * - 255), - (u8)( - GetInterpolatedAttribute(v0.color.a(), v1.color.a(), v2.color.a()).ToFloat32() * - 255), - }; - - Math::Vec2<float24> uv[3]; - uv[0].u() = GetInterpolatedAttribute(v0.tc0.u(), v1.tc0.u(), v2.tc0.u()); - uv[0].v() = GetInterpolatedAttribute(v0.tc0.v(), v1.tc0.v(), v2.tc0.v()); - uv[1].u() = GetInterpolatedAttribute(v0.tc1.u(), v1.tc1.u(), v2.tc1.u()); - uv[1].v() = GetInterpolatedAttribute(v0.tc1.v(), v1.tc1.v(), v2.tc1.v()); - uv[2].u() = GetInterpolatedAttribute(v0.tc2.u(), v1.tc2.u(), v2.tc2.u()); - uv[2].v() = GetInterpolatedAttribute(v0.tc2.v(), v1.tc2.v(), v2.tc2.v()); - - Math::Vec4<u8> texture_color[4]{}; - for (int i = 0; i < 3; ++i) { - const auto& texture = textures[i]; - if (!texture.enabled) - continue; - - DEBUG_ASSERT(0 != texture.config.address); - - int coordinate_i = - (i == 2 && regs.texturing.main_config.texture2_use_coord1) ? 1 : i; - float24 u = uv[coordinate_i].u(); - float24 v = uv[coordinate_i].v(); - - // Only unit 0 respects the texturing type (according to 3DBrew) - // TODO: Refactor so cubemaps and shadowmaps can be handled - PAddr texture_address = texture.config.GetPhysicalAddress(); - if (i == 0) { - switch (texture.config.type) { - case TexturingRegs::TextureConfig::Texture2D: - break; - case TexturingRegs::TextureConfig::TextureCube: { - auto w = GetInterpolatedAttribute(v0.tc0_w, v1.tc0_w, v2.tc0_w); - std::tie(u, v, texture_address) = ConvertCubeCoord(u, v, w, regs.texturing); - break; - } - case TexturingRegs::TextureConfig::Projection2D: { - auto tc0_w = GetInterpolatedAttribute(v0.tc0_w, v1.tc0_w, v2.tc0_w); - u /= tc0_w; - v /= tc0_w; - break; - } - default: - // TODO: Change to LOG_ERROR when more types are handled. - LOG_DEBUG(HW_GPU, "Unhandled texture type %x", (int)texture.config.type); - UNIMPLEMENTED(); - break; - } - } - - int s = (int)(u * float24::FromFloat32(static_cast<float>(texture.config.width))) - .ToFloat32(); - int t = (int)(v * float24::FromFloat32(static_cast<float>(texture.config.height))) - .ToFloat32(); - - bool use_border_s = false; - bool use_border_t = false; - - if (texture.config.wrap_s == TexturingRegs::TextureConfig::ClampToBorder) { - use_border_s = s < 0 || s >= static_cast<int>(texture.config.width); - } else if (texture.config.wrap_s == TexturingRegs::TextureConfig::ClampToBorder2) { - use_border_s = s >= static_cast<int>(texture.config.width); - } - - if (texture.config.wrap_t == TexturingRegs::TextureConfig::ClampToBorder) { - use_border_t = t < 0 || t >= static_cast<int>(texture.config.height); - } else if (texture.config.wrap_t == TexturingRegs::TextureConfig::ClampToBorder2) { - use_border_t = t >= static_cast<int>(texture.config.height); - } - - if (use_border_s || use_border_t) { - auto border_color = texture.config.border_color; - texture_color[i] = {border_color.r, border_color.g, border_color.b, - border_color.a}; - } else { - // Textures are laid out from bottom to top, hence we invert the t coordinate. - // NOTE: This may not be the right place for the inversion. - // TODO: Check if this applies to ETC textures, too. - s = GetWrappedTexCoord(texture.config.wrap_s, s, texture.config.width); - t = texture.config.height - 1 - - GetWrappedTexCoord(texture.config.wrap_t, t, texture.config.height); - - const u8* texture_data = Memory::GetPhysicalPointer(texture_address); - auto info = - Texture::TextureInfo::FromPicaRegister(texture.config, texture.format); - - // TODO: Apply the min and mag filters to the texture - texture_color[i] = Texture::LookupTexture(texture_data, s, t, info); -#if PICA_DUMP_TEXTURES - DebugUtils::DumpTexture(texture.config, texture_data); -#endif - } - } - - // sample procedural texture - if (regs.texturing.main_config.texture3_enable) { - const auto& proctex_uv = uv[regs.texturing.main_config.texture3_coordinates]; - texture_color[3] = ProcTex(proctex_uv.u().ToFloat32(), proctex_uv.v().ToFloat32(), - g_state.regs.texturing, g_state.proctex); - } - - // Texture environment - consists of 6 stages of color and alpha combining. - // - // Color combiners take three input color values from some source (e.g. interpolated - // vertex color, texture color, previous stage, etc), perform some very simple - // operations on each of them (e.g. inversion) and then calculate the output color - // with some basic arithmetic. Alpha combiners can be configured separately but work - // analogously. - Math::Vec4<u8> combiner_output; - Math::Vec4<u8> combiner_buffer = {0, 0, 0, 0}; - Math::Vec4<u8> next_combiner_buffer = { - regs.texturing.tev_combiner_buffer_color.r, - regs.texturing.tev_combiner_buffer_color.g, - regs.texturing.tev_combiner_buffer_color.b, - regs.texturing.tev_combiner_buffer_color.a, - }; - - Math::Vec4<u8> primary_fragment_color = {0, 0, 0, 0}; - Math::Vec4<u8> secondary_fragment_color = {0, 0, 0, 0}; - - if (!g_state.regs.lighting.disable) { - Math::Quaternion<float> normquat = Math::Quaternion<float>{ - {GetInterpolatedAttribute(v0.quat.x, v1.quat.x, v2.quat.x).ToFloat32(), - GetInterpolatedAttribute(v0.quat.y, v1.quat.y, v2.quat.y).ToFloat32(), - GetInterpolatedAttribute(v0.quat.z, v1.quat.z, v2.quat.z).ToFloat32()}, - GetInterpolatedAttribute(v0.quat.w, v1.quat.w, v2.quat.w).ToFloat32(), - }.Normalized(); - - Math::Vec3<float> view{ - GetInterpolatedAttribute(v0.view.x, v1.view.x, v2.view.x).ToFloat32(), - GetInterpolatedAttribute(v0.view.y, v1.view.y, v2.view.y).ToFloat32(), - GetInterpolatedAttribute(v0.view.z, v1.view.z, v2.view.z).ToFloat32(), - }; - std::tie(primary_fragment_color, secondary_fragment_color) = ComputeFragmentsColors( - g_state.regs.lighting, g_state.lighting, normquat, view, texture_color); - } - - for (unsigned tev_stage_index = 0; tev_stage_index < tev_stages.size(); - ++tev_stage_index) { - const auto& tev_stage = tev_stages[tev_stage_index]; - using Source = TexturingRegs::TevStageConfig::Source; - - auto GetSource = [&](Source source) -> Math::Vec4<u8> { - switch (source) { - case Source::PrimaryColor: - return primary_color; - - case Source::PrimaryFragmentColor: - return primary_fragment_color; - - case Source::SecondaryFragmentColor: - return secondary_fragment_color; - - case Source::Texture0: - return texture_color[0]; - - case Source::Texture1: - return texture_color[1]; - - case Source::Texture2: - return texture_color[2]; - - case Source::Texture3: - return texture_color[3]; - - case Source::PreviousBuffer: - return combiner_buffer; - - case Source::Constant: - return {tev_stage.const_r, tev_stage.const_g, tev_stage.const_b, - tev_stage.const_a}; - - case Source::Previous: - return combiner_output; - - default: - LOG_ERROR(HW_GPU, "Unknown color combiner source %d", (int)source); - UNIMPLEMENTED(); - return {0, 0, 0, 0}; - } - }; - - // color combiner - // NOTE: Not sure if the alpha combiner might use the color output of the previous - // stage as input. Hence, we currently don't directly write the result to - // combiner_output.rgb(), but instead store it in a temporary variable until - // alpha combining has been done. - Math::Vec3<u8> color_result[3] = { - GetColorModifier(tev_stage.color_modifier1, GetSource(tev_stage.color_source1)), - GetColorModifier(tev_stage.color_modifier2, GetSource(tev_stage.color_source2)), - GetColorModifier(tev_stage.color_modifier3, GetSource(tev_stage.color_source3)), - }; - auto color_output = ColorCombine(tev_stage.color_op, color_result); - - u8 alpha_output; - if (tev_stage.color_op == TexturingRegs::TevStageConfig::Operation::Dot3_RGBA) { - // result of Dot3_RGBA operation is also placed to the alpha component - alpha_output = color_output.x; - } else { - // alpha combiner - std::array<u8, 3> alpha_result = {{ - GetAlphaModifier(tev_stage.alpha_modifier1, - GetSource(tev_stage.alpha_source1)), - GetAlphaModifier(tev_stage.alpha_modifier2, - GetSource(tev_stage.alpha_source2)), - GetAlphaModifier(tev_stage.alpha_modifier3, - GetSource(tev_stage.alpha_source3)), - }}; - alpha_output = AlphaCombine(tev_stage.alpha_op, alpha_result); - } - - combiner_output[0] = - std::min((unsigned)255, color_output.r() * tev_stage.GetColorMultiplier()); - combiner_output[1] = - std::min((unsigned)255, color_output.g() * tev_stage.GetColorMultiplier()); - combiner_output[2] = - std::min((unsigned)255, color_output.b() * tev_stage.GetColorMultiplier()); - combiner_output[3] = - std::min((unsigned)255, alpha_output * tev_stage.GetAlphaMultiplier()); - - combiner_buffer = next_combiner_buffer; - - if (regs.texturing.tev_combiner_buffer_input.TevStageUpdatesCombinerBufferColor( - tev_stage_index)) { - next_combiner_buffer.r() = combiner_output.r(); - next_combiner_buffer.g() = combiner_output.g(); - next_combiner_buffer.b() = combiner_output.b(); - } - - if (regs.texturing.tev_combiner_buffer_input.TevStageUpdatesCombinerBufferAlpha( - tev_stage_index)) { - next_combiner_buffer.a() = combiner_output.a(); - } - } - - const auto& output_merger = regs.framebuffer.output_merger; - // TODO: Does alpha testing happen before or after stencil? - if (output_merger.alpha_test.enable) { - bool pass = false; - - switch (output_merger.alpha_test.func) { - case FramebufferRegs::CompareFunc::Never: - pass = false; - break; - - case FramebufferRegs::CompareFunc::Always: - pass = true; - break; - - case FramebufferRegs::CompareFunc::Equal: - pass = combiner_output.a() == output_merger.alpha_test.ref; - break; - - case FramebufferRegs::CompareFunc::NotEqual: - pass = combiner_output.a() != output_merger.alpha_test.ref; - break; - - case FramebufferRegs::CompareFunc::LessThan: - pass = combiner_output.a() < output_merger.alpha_test.ref; - break; - - case FramebufferRegs::CompareFunc::LessThanOrEqual: - pass = combiner_output.a() <= output_merger.alpha_test.ref; - break; - - case FramebufferRegs::CompareFunc::GreaterThan: - pass = combiner_output.a() > output_merger.alpha_test.ref; - break; - - case FramebufferRegs::CompareFunc::GreaterThanOrEqual: - pass = combiner_output.a() >= output_merger.alpha_test.ref; - break; - } - - if (!pass) - continue; - } - - // Apply fog combiner - // Not fully accurate. We'd have to know what data type is used to - // store the depth etc. Using float for now until we know more - // about Pica datatypes - if (regs.texturing.fog_mode == TexturingRegs::FogMode::Fog) { - const Math::Vec3<u8> fog_color = { - static_cast<u8>(regs.texturing.fog_color.r.Value()), - static_cast<u8>(regs.texturing.fog_color.g.Value()), - static_cast<u8>(regs.texturing.fog_color.b.Value()), - }; - - // Get index into fog LUT - float fog_index; - if (g_state.regs.texturing.fog_flip) { - fog_index = (1.0f - depth) * 128.0f; - } else { - fog_index = depth * 128.0f; - } - - // Generate clamped fog factor from LUT for given fog index - float fog_i = MathUtil::Clamp(floorf(fog_index), 0.0f, 127.0f); - float fog_f = fog_index - fog_i; - const auto& fog_lut_entry = g_state.fog.lut[static_cast<unsigned int>(fog_i)]; - float fog_factor = fog_lut_entry.ToFloat() + fog_lut_entry.DiffToFloat() * fog_f; - fog_factor = MathUtil::Clamp(fog_factor, 0.0f, 1.0f); - - // Blend the fog - for (unsigned i = 0; i < 3; i++) { - combiner_output[i] = static_cast<u8>(fog_factor * combiner_output[i] + - (1.0f - fog_factor) * fog_color[i]); - } - } - - u8 old_stencil = 0; - - auto UpdateStencil = [stencil_test, x, y, - &old_stencil](Pica::FramebufferRegs::StencilAction action) { - u8 new_stencil = - PerformStencilAction(action, old_stencil, stencil_test.reference_value); - if (g_state.regs.framebuffer.framebuffer.allow_depth_stencil_write != 0) - SetStencil(x >> 4, y >> 4, (new_stencil & stencil_test.write_mask) | - (old_stencil & ~stencil_test.write_mask)); - }; - - if (stencil_action_enable) { - old_stencil = GetStencil(x >> 4, y >> 4); - u8 dest = old_stencil & stencil_test.input_mask; - u8 ref = stencil_test.reference_value & stencil_test.input_mask; - - bool pass = false; - switch (stencil_test.func) { - case FramebufferRegs::CompareFunc::Never: - pass = false; - break; - - case FramebufferRegs::CompareFunc::Always: - pass = true; - break; - - case FramebufferRegs::CompareFunc::Equal: - pass = (ref == dest); - break; - - case FramebufferRegs::CompareFunc::NotEqual: - pass = (ref != dest); - break; - - case FramebufferRegs::CompareFunc::LessThan: - pass = (ref < dest); - break; - - case FramebufferRegs::CompareFunc::LessThanOrEqual: - pass = (ref <= dest); - break; - - case FramebufferRegs::CompareFunc::GreaterThan: - pass = (ref > dest); - break; - - case FramebufferRegs::CompareFunc::GreaterThanOrEqual: - pass = (ref >= dest); - break; - } - - if (!pass) { - UpdateStencil(stencil_test.action_stencil_fail); - continue; - } - } - - // Convert float to integer - unsigned num_bits = - FramebufferRegs::DepthBitsPerPixel(regs.framebuffer.framebuffer.depth_format); - u32 z = (u32)(depth * ((1 << num_bits) - 1)); - - if (output_merger.depth_test_enable) { - u32 ref_z = GetDepth(x >> 4, y >> 4); - - bool pass = false; - - switch (output_merger.depth_test_func) { - case FramebufferRegs::CompareFunc::Never: - pass = false; - break; - - case FramebufferRegs::CompareFunc::Always: - pass = true; - break; - - case FramebufferRegs::CompareFunc::Equal: - pass = z == ref_z; - break; - - case FramebufferRegs::CompareFunc::NotEqual: - pass = z != ref_z; - break; - - case FramebufferRegs::CompareFunc::LessThan: - pass = z < ref_z; - break; - - case FramebufferRegs::CompareFunc::LessThanOrEqual: - pass = z <= ref_z; - break; - - case FramebufferRegs::CompareFunc::GreaterThan: - pass = z > ref_z; - break; - - case FramebufferRegs::CompareFunc::GreaterThanOrEqual: - pass = z >= ref_z; - break; - } - - if (!pass) { - if (stencil_action_enable) - UpdateStencil(stencil_test.action_depth_fail); - continue; - } - } - - if (regs.framebuffer.framebuffer.allow_depth_stencil_write != 0 && - output_merger.depth_write_enable) { - - SetDepth(x >> 4, y >> 4, z); - } - - // The stencil depth_pass action is executed even if depth testing is disabled - if (stencil_action_enable) - UpdateStencil(stencil_test.action_depth_pass); - - auto dest = GetPixel(x >> 4, y >> 4); - Math::Vec4<u8> blend_output = combiner_output; - - if (output_merger.alphablend_enable) { - auto params = output_merger.alpha_blending; - - auto LookupFactor = [&](unsigned channel, - FramebufferRegs::BlendFactor factor) -> u8 { - DEBUG_ASSERT(channel < 4); - - const Math::Vec4<u8> blend_const = { - static_cast<u8>(output_merger.blend_const.r), - static_cast<u8>(output_merger.blend_const.g), - static_cast<u8>(output_merger.blend_const.b), - static_cast<u8>(output_merger.blend_const.a), - }; - - switch (factor) { - case FramebufferRegs::BlendFactor::Zero: - return 0; - - case FramebufferRegs::BlendFactor::One: - return 255; - - case FramebufferRegs::BlendFactor::SourceColor: - return combiner_output[channel]; - - case FramebufferRegs::BlendFactor::OneMinusSourceColor: - return 255 - combiner_output[channel]; - - case FramebufferRegs::BlendFactor::DestColor: - return dest[channel]; - - case FramebufferRegs::BlendFactor::OneMinusDestColor: - return 255 - dest[channel]; - - case FramebufferRegs::BlendFactor::SourceAlpha: - return combiner_output.a(); - - case FramebufferRegs::BlendFactor::OneMinusSourceAlpha: - return 255 - combiner_output.a(); - - case FramebufferRegs::BlendFactor::DestAlpha: - return dest.a(); - - case FramebufferRegs::BlendFactor::OneMinusDestAlpha: - return 255 - dest.a(); - - case FramebufferRegs::BlendFactor::ConstantColor: - return blend_const[channel]; - - case FramebufferRegs::BlendFactor::OneMinusConstantColor: - return 255 - blend_const[channel]; - - case FramebufferRegs::BlendFactor::ConstantAlpha: - return blend_const.a(); - - case FramebufferRegs::BlendFactor::OneMinusConstantAlpha: - return 255 - blend_const.a(); - - case FramebufferRegs::BlendFactor::SourceAlphaSaturate: - // Returns 1.0 for the alpha channel - if (channel == 3) - return 255; - return std::min(combiner_output.a(), static_cast<u8>(255 - dest.a())); - - default: - LOG_CRITICAL(HW_GPU, "Unknown blend factor %x", factor); - UNIMPLEMENTED(); - break; - } - - return combiner_output[channel]; - }; - - auto srcfactor = Math::MakeVec(LookupFactor(0, params.factor_source_rgb), - LookupFactor(1, params.factor_source_rgb), - LookupFactor(2, params.factor_source_rgb), - LookupFactor(3, params.factor_source_a)); - - auto dstfactor = Math::MakeVec(LookupFactor(0, params.factor_dest_rgb), - LookupFactor(1, params.factor_dest_rgb), - LookupFactor(2, params.factor_dest_rgb), - LookupFactor(3, params.factor_dest_a)); - - blend_output = EvaluateBlendEquation(combiner_output, srcfactor, dest, dstfactor, - params.blend_equation_rgb); - blend_output.a() = EvaluateBlendEquation(combiner_output, srcfactor, dest, - dstfactor, params.blend_equation_a) - .a(); - } else { - blend_output = - Math::MakeVec(LogicOp(combiner_output.r(), dest.r(), output_merger.logic_op), - LogicOp(combiner_output.g(), dest.g(), output_merger.logic_op), - LogicOp(combiner_output.b(), dest.b(), output_merger.logic_op), - LogicOp(combiner_output.a(), dest.a(), output_merger.logic_op)); - } - - const Math::Vec4<u8> result = { - output_merger.red_enable ? blend_output.r() : dest.r(), - output_merger.green_enable ? blend_output.g() : dest.g(), - output_merger.blue_enable ? blend_output.b() : dest.b(), - output_merger.alpha_enable ? blend_output.a() : dest.a(), - }; - - if (regs.framebuffer.framebuffer.allow_color_write != 0) - DrawPixel(x >> 4, y >> 4, result); - } - } -} - -void ProcessTriangle(const Vertex& v0, const Vertex& v1, const Vertex& v2) { - ProcessTriangleInternal(v0, v1, v2); -} - -} // namespace Rasterizer - -} // namespace Pica diff --git a/src/video_core/swrasterizer/rasterizer.h b/src/video_core/swrasterizer/rasterizer.h deleted file mode 100644 index 66cd6cfd4..000000000 --- a/src/video_core/swrasterizer/rasterizer.h +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2014 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include "video_core/shader/shader.h" - -namespace Pica { - -namespace Rasterizer { - -struct Vertex : Shader::OutputVertex { - Vertex(const OutputVertex& v) : OutputVertex(v) {} - - // Attributes used to store intermediate results - // position after perspective divide - Math::Vec3<float24> screenpos; - - // Linear interpolation - // factor: 0=this, 1=vtx - // Note: This function cannot be called after perspective divide - void Lerp(float24 factor, const Vertex& vtx) { - pos = pos * factor + vtx.pos * (float24::FromFloat32(1) - factor); - quat = quat * factor + vtx.quat * (float24::FromFloat32(1) - factor); - color = color * factor + vtx.color * (float24::FromFloat32(1) - factor); - tc0 = tc0 * factor + vtx.tc0 * (float24::FromFloat32(1) - factor); - tc1 = tc1 * factor + vtx.tc1 * (float24::FromFloat32(1) - factor); - tc0_w = tc0_w * factor + vtx.tc0_w * (float24::FromFloat32(1) - factor); - view = view * factor + vtx.view * (float24::FromFloat32(1) - factor); - tc2 = tc2 * factor + vtx.tc2 * (float24::FromFloat32(1) - factor); - } - - // Linear interpolation - // factor: 0=v0, 1=v1 - // Note: This function cannot be called after perspective divide - static Vertex Lerp(float24 factor, const Vertex& v0, const Vertex& v1) { - Vertex ret = v0; - ret.Lerp(factor, v1); - return ret; - } -}; - -void ProcessTriangle(const Vertex& v0, const Vertex& v1, const Vertex& v2); - -} // namespace Rasterizer - -} // namespace Pica diff --git a/src/video_core/swrasterizer/swrasterizer.cpp b/src/video_core/swrasterizer/swrasterizer.cpp deleted file mode 100644 index 402b705dd..000000000 --- a/src/video_core/swrasterizer/swrasterizer.cpp +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2015 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include "video_core/swrasterizer/clipper.h" -#include "video_core/swrasterizer/swrasterizer.h" - -namespace VideoCore { - -void SWRasterizer::AddTriangle(const Pica::Shader::OutputVertex& v0, - const Pica::Shader::OutputVertex& v1, - const Pica::Shader::OutputVertex& v2) { - Pica::Clipper::ProcessTriangle(v0, v1, v2); -} -} diff --git a/src/video_core/swrasterizer/swrasterizer.h b/src/video_core/swrasterizer/swrasterizer.h deleted file mode 100644 index 04ebd5312..000000000 --- a/src/video_core/swrasterizer/swrasterizer.h +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2015 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include "common/common_types.h" -#include "video_core/rasterizer_interface.h" - -namespace Pica { -namespace Shader { -struct OutputVertex; -} -} - -namespace VideoCore { - -class SWRasterizer : public RasterizerInterface { - void AddTriangle(const Pica::Shader::OutputVertex& v0, const Pica::Shader::OutputVertex& v1, - const Pica::Shader::OutputVertex& v2) override; - void DrawTriangles() override {} - void NotifyPicaRegisterChanged(u32 id) override {} - void FlushAll() override {} - void FlushRegion(PAddr addr, u64 size) override {} - void FlushAndInvalidateRegion(PAddr addr, u64 size) override {} -}; -} diff --git a/src/video_core/swrasterizer/texturing.cpp b/src/video_core/swrasterizer/texturing.cpp deleted file mode 100644 index 79b1ce841..000000000 --- a/src/video_core/swrasterizer/texturing.cpp +++ /dev/null @@ -1,244 +0,0 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include <algorithm> - -#include "common/assert.h" -#include "common/common_types.h" -#include "common/math_util.h" -#include "common/vector_math.h" -#include "video_core/regs_texturing.h" -#include "video_core/swrasterizer/texturing.h" - -namespace Pica { -namespace Rasterizer { - -using TevStageConfig = TexturingRegs::TevStageConfig; - -int GetWrappedTexCoord(TexturingRegs::TextureConfig::WrapMode mode, int val, unsigned size) { - switch (mode) { - case TexturingRegs::TextureConfig::ClampToEdge2: - // For negative coordinate, ClampToEdge2 behaves the same as Repeat - if (val < 0) { - return static_cast<int>(static_cast<unsigned>(val) % size); - } - // [[fallthrough]] - case TexturingRegs::TextureConfig::ClampToEdge: - val = std::max(val, 0); - val = std::min(val, static_cast<int>(size) - 1); - return val; - - case TexturingRegs::TextureConfig::ClampToBorder: - return val; - - case TexturingRegs::TextureConfig::ClampToBorder2: - // For ClampToBorder2, the case of positive coordinate beyond the texture size is already - // handled outside. Here we only handle the negative coordinate in the same way as Repeat. - case TexturingRegs::TextureConfig::Repeat2: - case TexturingRegs::TextureConfig::Repeat3: - case TexturingRegs::TextureConfig::Repeat: - return static_cast<int>(static_cast<unsigned>(val) % size); - - case TexturingRegs::TextureConfig::MirroredRepeat: { - unsigned int coord = (static_cast<unsigned>(val) % (2 * size)); - if (coord >= size) - coord = 2 * size - 1 - coord; - return static_cast<int>(coord); - } - - default: - LOG_ERROR(HW_GPU, "Unknown texture coordinate wrapping mode %x", (int)mode); - UNIMPLEMENTED(); - return 0; - } -}; - -Math::Vec3<u8> GetColorModifier(TevStageConfig::ColorModifier factor, - const Math::Vec4<u8>& values) { - using ColorModifier = TevStageConfig::ColorModifier; - - switch (factor) { - case ColorModifier::SourceColor: - return values.rgb(); - - case ColorModifier::OneMinusSourceColor: - return (Math::Vec3<u8>(255, 255, 255) - values.rgb()).Cast<u8>(); - - case ColorModifier::SourceAlpha: - return values.aaa(); - - case ColorModifier::OneMinusSourceAlpha: - return (Math::Vec3<u8>(255, 255, 255) - values.aaa()).Cast<u8>(); - - case ColorModifier::SourceRed: - return values.rrr(); - - case ColorModifier::OneMinusSourceRed: - return (Math::Vec3<u8>(255, 255, 255) - values.rrr()).Cast<u8>(); - - case ColorModifier::SourceGreen: - return values.ggg(); - - case ColorModifier::OneMinusSourceGreen: - return (Math::Vec3<u8>(255, 255, 255) - values.ggg()).Cast<u8>(); - - case ColorModifier::SourceBlue: - return values.bbb(); - - case ColorModifier::OneMinusSourceBlue: - return (Math::Vec3<u8>(255, 255, 255) - values.bbb()).Cast<u8>(); - } - - UNREACHABLE(); -}; - -u8 GetAlphaModifier(TevStageConfig::AlphaModifier factor, const Math::Vec4<u8>& values) { - using AlphaModifier = TevStageConfig::AlphaModifier; - - switch (factor) { - case AlphaModifier::SourceAlpha: - return values.a(); - - case AlphaModifier::OneMinusSourceAlpha: - return 255 - values.a(); - - case AlphaModifier::SourceRed: - return values.r(); - - case AlphaModifier::OneMinusSourceRed: - return 255 - values.r(); - - case AlphaModifier::SourceGreen: - return values.g(); - - case AlphaModifier::OneMinusSourceGreen: - return 255 - values.g(); - - case AlphaModifier::SourceBlue: - return values.b(); - - case AlphaModifier::OneMinusSourceBlue: - return 255 - values.b(); - } - - UNREACHABLE(); -}; - -Math::Vec3<u8> ColorCombine(TevStageConfig::Operation op, const Math::Vec3<u8> input[3]) { - using Operation = TevStageConfig::Operation; - - switch (op) { - case Operation::Replace: - return input[0]; - - case Operation::Modulate: - return ((input[0] * input[1]) / 255).Cast<u8>(); - - case Operation::Add: { - auto result = input[0] + input[1]; - result.r() = std::min(255, result.r()); - result.g() = std::min(255, result.g()); - result.b() = std::min(255, result.b()); - return result.Cast<u8>(); - } - - case Operation::AddSigned: { - // TODO(bunnei): Verify that the color conversion from (float) 0.5f to - // (byte) 128 is correct - auto result = - input[0].Cast<int>() + input[1].Cast<int>() - Math::MakeVec<int>(128, 128, 128); - result.r() = MathUtil::Clamp<int>(result.r(), 0, 255); - result.g() = MathUtil::Clamp<int>(result.g(), 0, 255); - result.b() = MathUtil::Clamp<int>(result.b(), 0, 255); - return result.Cast<u8>(); - } - - case Operation::Lerp: - return ((input[0] * input[2] + - input[1] * (Math::MakeVec<u8>(255, 255, 255) - input[2]).Cast<u8>()) / - 255) - .Cast<u8>(); - - case Operation::Subtract: { - auto result = input[0].Cast<int>() - input[1].Cast<int>(); - result.r() = std::max(0, result.r()); - result.g() = std::max(0, result.g()); - result.b() = std::max(0, result.b()); - return result.Cast<u8>(); - } - - case Operation::MultiplyThenAdd: { - auto result = (input[0] * input[1] + 255 * input[2].Cast<int>()) / 255; - result.r() = std::min(255, result.r()); - result.g() = std::min(255, result.g()); - result.b() = std::min(255, result.b()); - return result.Cast<u8>(); - } - - case Operation::AddThenMultiply: { - auto result = input[0] + input[1]; - result.r() = std::min(255, result.r()); - result.g() = std::min(255, result.g()); - result.b() = std::min(255, result.b()); - result = (result * input[2].Cast<int>()) / 255; - return result.Cast<u8>(); - } - case Operation::Dot3_RGB: - case Operation::Dot3_RGBA: { - // Not fully accurate. Worst case scenario seems to yield a +/-3 error. Some HW results - // indicate that the per-component computation can't have a higher precision than 1/256, - // while dot3_rgb((0x80,g0,b0), (0x7F,g1,b1)) and dot3_rgb((0x80,g0,b0), (0x80,g1,b1)) give - // different results. - int result = ((input[0].r() * 2 - 255) * (input[1].r() * 2 - 255) + 128) / 256 + - ((input[0].g() * 2 - 255) * (input[1].g() * 2 - 255) + 128) / 256 + - ((input[0].b() * 2 - 255) * (input[1].b() * 2 - 255) + 128) / 256; - result = std::max(0, std::min(255, result)); - return {(u8)result, (u8)result, (u8)result}; - } - default: - LOG_ERROR(HW_GPU, "Unknown color combiner operation %d", (int)op); - UNIMPLEMENTED(); - return {0, 0, 0}; - } -}; - -u8 AlphaCombine(TevStageConfig::Operation op, const std::array<u8, 3>& input) { - switch (op) { - using Operation = TevStageConfig::Operation; - case Operation::Replace: - return input[0]; - - case Operation::Modulate: - return input[0] * input[1] / 255; - - case Operation::Add: - return std::min(255, input[0] + input[1]); - - case Operation::AddSigned: { - // TODO(bunnei): Verify that the color conversion from (float) 0.5f to (byte) 128 is correct - auto result = static_cast<int>(input[0]) + static_cast<int>(input[1]) - 128; - return static_cast<u8>(MathUtil::Clamp<int>(result, 0, 255)); - } - - case Operation::Lerp: - return (input[0] * input[2] + input[1] * (255 - input[2])) / 255; - - case Operation::Subtract: - return std::max(0, (int)input[0] - (int)input[1]); - - case Operation::MultiplyThenAdd: - return std::min(255, (input[0] * input[1] + 255 * input[2]) / 255); - - case Operation::AddThenMultiply: - return (std::min(255, (input[0] + input[1])) * input[2]) / 255; - - default: - LOG_ERROR(HW_GPU, "Unknown alpha combiner operation %d", (int)op); - UNIMPLEMENTED(); - return 0; - } -}; - -} // namespace Rasterizer -} // namespace Pica diff --git a/src/video_core/swrasterizer/texturing.h b/src/video_core/swrasterizer/texturing.h deleted file mode 100644 index 24f74a5a3..000000000 --- a/src/video_core/swrasterizer/texturing.h +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include "common/common_types.h" -#include "common/vector_math.h" -#include "video_core/regs_texturing.h" - -namespace Pica { -namespace Rasterizer { - -int GetWrappedTexCoord(TexturingRegs::TextureConfig::WrapMode mode, int val, unsigned size); - -Math::Vec3<u8> GetColorModifier(TexturingRegs::TevStageConfig::ColorModifier factor, - const Math::Vec4<u8>& values); - -u8 GetAlphaModifier(TexturingRegs::TevStageConfig::AlphaModifier factor, - const Math::Vec4<u8>& values); - -Math::Vec3<u8> ColorCombine(TexturingRegs::TevStageConfig::Operation op, - const Math::Vec3<u8> input[3]); - -u8 AlphaCombine(TexturingRegs::TevStageConfig::Operation op, const std::array<u8, 3>& input); - -} // namespace Rasterizer -} // namespace Pica |