1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-11 17:51:04 +00:00

Improve ergonomics of CachedShaderSetting

This commit is contained in:
sfan5 2024-02-20 14:59:47 +01:00
parent e9ab5bc223
commit 0c3a4cc7b9
4 changed files with 75 additions and 47 deletions

View file

@ -105,6 +105,57 @@ public:
has_been_set = true;
}
}
/* Type specializations */
/*
* T2 looks redundant here but it is necessary so the compiler won't
* resolve the templates at class instantiation and then fail because
* some of these methods don't have valid types (= are not usable).
* ref: <https://stackoverflow.com/a/6972771>
*
* Note: a `bool dummy` template parameter would have been easier but MSVC
* does not like that. Also make sure not to define different specializations
* with the same parameters, MSVC doesn't like that either.
* I extend my thanks to Microsoft®
*/
#define SPECIALIZE(_type, _count_expr) \
template<typename T2 = T> \
std::enable_if_t<std::is_same_v<T, T2> && std::is_same_v<T2, _type> && (_count_expr)>
SPECIALIZE(float, count == 2)
set(const v2f value, video::IMaterialRendererServices *services)
{
float array[2] = { value.X, value.Y };
set(array, services);
}
SPECIALIZE(float, count == 3)
set(const v3f value, video::IMaterialRendererServices *services)
{
float array[3] = { value.X, value.Y, value.Z };
set(array, services);
}
SPECIALIZE(float, count == 3 || count == 4)
set(const video::SColorf value, video::IMaterialRendererServices *services)
{
if constexpr (count == 3) {
float array[3] = { value.r, value.g, value.b };
set(array, services);
} else {
float array[4] = { value.r, value.g, value.b, value.a };
set(array, services);
}
}
SPECIALIZE(float, count == 16)
set(const core::matrix4 &value, video::IMaterialRendererServices *services)
{
set(value.pointer(), services);
}
#undef SPECIALIZE
};
template <typename T, std::size_t count = 1, bool cache=true>