Debian Codegraph @

Source/Core/VideoCommon/CPUCull.cpp

raw symbols SCIP
1 // Copyright 2022 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/CPUCull.h"
5
6 #include "Common/Assert.h"
7 #include "Common/CPUDetect.h"
8 #include "Common/MathUtil.h"
9 #include "Common/MemoryUtil.h"
10 #include "Core/System.h"
11
12 #include "VideoCommon/CPMemory.h"
13 #include "VideoCommon/VertexManagerBase.h"
14 #include "VideoCommon/VertexShaderManager.h"
15 #include "VideoCommon/XFMemory.h"
16
17 // We really want things like c.w * a.x - a.w * c.x to stay symmetric, so they cancel to zero on
18 // degenerate triangles. Make sure the compiler doesn't optimize in fmas where not requested.
19 #ifdef _MSC_VER
20 #pragma fp_contract(off)
21 #else
22 // GCC doesn't support any in-file way to turn off fp contract yet
23 // Not ideal, but worst case scenario its cpu cull is worse at detecting degenerate triangles
24 // (Most likely to happen on arm, as we don't compile the cull code for x86 fma)
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
27 #pragma STDC FP_CONTRACT OFF
28 #pragma GCC diagnostic pop
29 #endif
30
31 #if defined(_M_X86) || defined(_M_X86_64)
32 #define USE_SSE
33 #elif defined(_M_ARM_64)
34 #define USE_NEON
35 #else
36 #define NO_SIMD
37 #endif
38
39 #if defined(USE_SSE)
40 #include <immintrin.h>
41 #elif defined(USE_NEON)
42 #include <arm_neon.h>
43 #endif
44
45 #include "VideoCommon/CPUCullImpl.h"
46 #ifdef USE_SSE
47 #define USE_SSE3
48 #include "VideoCommon/CPUCullImpl.h"
49 #define USE_SSE41
50 #include "VideoCommon/CPUCullImpl.h"
51 #define USE_AVX
52 #include "VideoCommon/CPUCullImpl.h"
53 #define USE_FMA
54 #include "VideoCommon/CPUCullImpl.h"
55 #endif
56
57 #if defined(USE_SSE)
58 #if defined(__AVX__) && defined(__FMA__)
59 static constexpr int MIN_SSE = 51;
60 #elif defined(__AVX__)
61 static constexpr int MIN_SSE = 50;
62 #elif defined(__SSE4_1__)
63 static constexpr int MIN_SSE = 41;
64 #elif defined(__SSE3__)
65 static constexpr int MIN_SSE = 30;
66 #else
67 static constexpr int MIN_SSE = 0;
68 #endif
69 #endif
70
71 template <bool PositionHas3Elems, bool PerVertexPosMtx>
72 static CPUCull::TransformFunction GetTransformFunction()
73 {
74 #if defined(USE_SSE)
75 if (MIN_SSE >= 51 || (cpu_info.bAVX && cpu_info.bFMA))
76 return CPUCull_FMA::TransformVertices<PositionHas3Elems, PerVertexPosMtx>;
77 else if (MIN_SSE >= 50 || cpu_info.bAVX)
78 return CPUCull_AVX::TransformVertices<PositionHas3Elems, PerVertexPosMtx>;
79 else if (PositionHas3Elems && PerVertexPosMtx && (MIN_SSE >= 41 || cpu_info.bSSE4_1))
80 return CPUCull_SSE41::TransformVertices<PositionHas3Elems, PerVertexPosMtx>;
81 else if (PositionHas3Elems && (MIN_SSE >= 30 || cpu_info.bSSE3))
82 return CPUCull_SSE3::TransformVertices<PositionHas3Elems, PerVertexPosMtx>;
83 else
84 return CPUCull_SSE::TransformVertices<PositionHas3Elems, PerVertexPosMtx>;
85 #elif defined(USE_NEON)
86 return CPUCull_NEON::TransformVertices<PositionHas3Elems, PerVertexPosMtx>;
87 #else
88 return CPUCull_Scalar::TransformVertices<PositionHas3Elems, PerVertexPosMtx>;
89 #endif
90 }
91
92 template <OpcodeDecoder::Primitive Primitive, CullMode Mode>
93 static CPUCull::CullFunction GetCullFunction0()
94 {
95 #if defined(USE_SSE)
96 // Note: AVX version only actually AVX on compilers that support __attribute__((target))
97 // Sorry, MSVC + Sandy Bridge. (Ivy+ and AMD see very little benefit thanks to mov elimination)
98 if (MIN_SSE >= 50 || cpu_info.bAVX)
99 return CPUCull_AVX::AreAllVerticesCulled<Primitive, Mode>;
100 else if (MIN_SSE >= 30 || cpu_info.bSSE3)
101 return CPUCull_SSE3::AreAllVerticesCulled<Primitive, Mode>;
102 else
103 return CPUCull_SSE::AreAllVerticesCulled<Primitive, Mode>;
104 #elif defined(USE_NEON)
105 return CPUCull_NEON::AreAllVerticesCulled<Primitive, Mode>;
106 #else
107 return CPUCull_Scalar::AreAllVerticesCulled<Primitive, Mode>;
108 #endif
109 }
110
111 template <OpcodeDecoder::Primitive Primitive>
112 static Common::EnumMap<CPUCull::CullFunction, CullMode::All> GetCullFunction1()
113 {
114 return {
115 GetCullFunction0<Primitive, CullMode::None>(),
116 GetCullFunction0<Primitive, CullMode::Back>(),
117 GetCullFunction0<Primitive, CullMode::Front>(),
118 GetCullFunction0<Primitive, CullMode::All>(),
119 };
120 }
121
122 CPUCull::~CPUCull() = default;
123
124 void CPUCull::Init()
125 {
126 m_transform_table[false][false] = GetTransformFunction<false, false>();
127 m_transform_table[false][true] = GetTransformFunction<false, true>();
128 m_transform_table[true][false] = GetTransformFunction<true, false>();
129 m_transform_table[true][true] = GetTransformFunction<true, true>();
130 using Prim = OpcodeDecoder::Primitive;
131 m_cull_table[Prim::GX_DRAW_QUADS] = GetCullFunction1<Prim::GX_DRAW_QUADS>();
132 m_cull_table[Prim::GX_DRAW_QUADS_2] = GetCullFunction1<Prim::GX_DRAW_QUADS>();
133 m_cull_table[Prim::GX_DRAW_TRIANGLES] = GetCullFunction1<Prim::GX_DRAW_TRIANGLES>();
134 m_cull_table[Prim::GX_DRAW_TRIANGLE_STRIP] = GetCullFunction1<Prim::GX_DRAW_TRIANGLE_STRIP>();
135 m_cull_table[Prim::GX_DRAW_TRIANGLE_FAN] = GetCullFunction1<Prim::GX_DRAW_TRIANGLE_FAN>();
136 }
137
138 bool CPUCull::AreAllVerticesCulled(VertexLoaderBase* loader, OpcodeDecoder::Primitive primitive,
139 const u8* src, u32 count)
140 {
141 ASSERT_MSG(VIDEO, primitive < OpcodeDecoder::Primitive::GX_DRAW_LINES,
142 "CPUCull should not be called on lines or points");
143 const u32 stride = loader->m_native_vtx_decl.stride;
144 const bool posHas3Elems = loader->m_native_vtx_decl.position.components >= 3;
145 const bool perVertexPosMtx = loader->m_native_vtx_decl.posmtx.enable;
146 if (m_transform_buffer_size < count) [[unlikely]]
147 {
148 u32 new_size = MathUtil::NextPowerOf2(count);
149 m_transform_buffer_size = new_size;
150 m_transform_buffer.reset(static_cast<TransformedVertex*>(
151 Common::AllocateAlignedMemory(new_size * sizeof(TransformedVertex), 32)));
152 }
153
154 // transform functions need the projection matrix to transform to clip space
155 auto& system = Core::System::GetInstance();
156 system.GetVertexShaderManager().SetProjectionMatrix(system.GetXFStateManager());
157
158 static constexpr Common::EnumMap<CullMode, CullMode::All> cullmode_invert = {
159 CullMode::None, CullMode::Front, CullMode::Back, CullMode::All};
160
161 CullMode cull_mode = bpmem.genMode.cull_mode;
162 if (xfmem.viewport.ht > 0) // See videosoftware Clipper.cpp:IsBackface
163 cull_mode = cullmode_invert[cull_mode];
164 const TransformFunction transform = m_transform_table[posHas3Elems][perVertexPosMtx];
165 transform(m_transform_buffer.get(), src, stride, count);
166 const CullFunction cull = m_cull_table[primitive][cull_mode];
167 return cull(m_transform_buffer.get(), count);
168 }
169
170 template <typename T>
171 void CPUCull::BufferDeleter<T>::operator()(T* ptr)
172 {
173 Common::FreeAlignedMemory(ptr);
174 }