From a241058d7b809b34e5f1a4d52fafa6e2903d2182 Mon Sep 17 00:00:00 2001 From: xebra Date: Sun, 22 Jul 2018 01:11:23 +0900 Subject: [PATCH] [spline/bezier]Improve TemplateParameterDispatcher class macro to a real class. --- GPU/Common/SplineCommon.cpp | 5 +-- GPU/Common/SplineCommon.h | 75 +++++++++++++++++++++---------------- 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/GPU/Common/SplineCommon.cpp b/GPU/Common/SplineCommon.cpp index e3718dad3811..349563541733 100644 --- a/GPU/Common/SplineCommon.cpp +++ b/GPU/Common/SplineCommon.cpp @@ -409,13 +409,12 @@ class SubdivisionSurface { patch.BuildIndex(indices, count); } - // Define class TemplateParameterDispatcherTess - TEMPLATE_PARAMETER_DISPATCHER(Tess, SubdivisionSurface::Tessellate); + TEMPLATE_PARAMETER_DISPATCHER_FUNCTION(Tess, SubdivisionSurface::Tessellate); void Tessellate(SimpleVertex *vertices, u16 *indices, int &count, u32 origVertType) { using TessFunc = void(SubdivisionSurface::*)(SimpleVertex *, u16 *, int &); constexpr int NumParams = 5; - static TemplateParameterDispatcherTess dispatcher; // Initialize only once + static TemplateParameterDispatcher dispatcher; // Initialize only once const bool params[NumParams] = { (origVertType & GE_VTYPE_NRM_MASK) != 0, diff --git a/GPU/Common/SplineCommon.h b/GPU/Common/SplineCommon.h index 7fa9585a9adc..ebc82fa7dd0b 100644 --- a/GPU/Common/SplineCommon.h +++ b/GPU/Common/SplineCommon.h @@ -195,38 +195,47 @@ bool CanUseHardwareTessellation(GEPatchPrimType prim); void TessellateSplinePatch(u8 *&dest, u16 *indices, int &count, SplinePatchLocal &spatch, u32 origVertType, int maxVertices); void TessellateBezierPatch(u8 *&dest, u16 *&indices, int &count, int tess_u, int tess_v, const BezierPatch &patch, u32 origVertType, int maxVertices); -#define TEMPLATE_PARAMETER_DISPATCHER(NAME, FUNCNAME) \ -template \ -class TemplateParameterDispatcher##NAME { \ - /* Store all combinations of template functions into an array */ \ - template \ - struct Initializer { \ - Initializer(Func funcs[]) { \ - Initializer _true(funcs); \ - Initializer _false(funcs); \ - } \ - }; \ - /* Specialized for terminates the recursive loop */ \ - template \ - struct Initializer<0, Index, Params...> { \ - Initializer(Func funcs[]) { \ - funcs[Index] = &FUNCNAME; \ - } \ - }; \ - \ -private: \ - Func funcs[1 << NumParams]; /* Function pointers array */ \ -public: \ - TemplateParameterDispatcher##NAME() { \ - Initializer::Initializer(funcs); \ - } \ - \ - Func GetFunc(const bool params[]) const { \ - /* Convert bool parameters to index of the array */ \ - int param = 0; \ - for (int i = 0; i < NumParams; ++i) \ - param |= params[i] << i; \ - \ - return funcs[param]; \ +// Define function object for TemplateParameterDispatcher +#define TEMPLATE_PARAMETER_DISPATCHER_FUNCTION(NAME, FUNCNAME) \ +struct NAME { \ + template \ + static auto GetFunc() { \ + return &FUNCNAME; \ } \ }; + +template +class TemplateParameterDispatcher { + + /* Store all combinations of template functions into an array */ + template + struct Initializer { + static void Init(Func funcs[]) { + Initializer::Init(funcs); // true + Initializer::Init(funcs); // false + } + }; + /* Specialized for terminates the recursive loop */ + template + struct Initializer<0, Index, Params...> { + static void Init(Func funcs[]) { + funcs[Index] = Dispatcher::GetFunc(); + } + }; + +private: + Func funcs[1 << NumParams]; /* Function pointers array */ +public: + TemplateParameterDispatcher() { + Initializer::Init(funcs); + } + + Func GetFunc(const bool params[]) const { + /* Convert bool parameters to index of the array */ + int index = 0; + for (int i = 0; i < NumParams; ++i) + index |= params[i] << i; + + return funcs[index]; + } +};