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

@ -398,7 +398,7 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
CachedPixelShaderSetting<SamplerLayer_t> m_texture3{"texture3"}; CachedPixelShaderSetting<SamplerLayer_t> m_texture3{"texture3"};
CachedVertexShaderSetting<float, 2> m_texel_size0_vertex{"texelSize0"}; CachedVertexShaderSetting<float, 2> m_texel_size0_vertex{"texelSize0"};
CachedPixelShaderSetting<float, 2> m_texel_size0_pixel{"texelSize0"}; CachedPixelShaderSetting<float, 2> m_texel_size0_pixel{"texelSize0"};
std::array<float, 2> m_texel_size0_values; v2f m_texel_size0;
CachedStructPixelShaderSetting<float, 7> m_exposure_params_pixel{ CachedStructPixelShaderSetting<float, 7> m_exposure_params_pixel{
"exposureParams", "exposureParams",
std::array<const char*, 7> { std::array<const char*, 7> {
@ -477,10 +477,7 @@ public:
void onSetConstants(video::IMaterialRendererServices *services) override void onSetConstants(video::IMaterialRendererServices *services) override
{ {
video::SColorf fogcolorf(m_sky->getFogColor()); video::SColorf fogcolorf(m_sky->getFogColor());
float fogcolorfa[4] = { m_fog_color.set(fogcolorf, services);
fogcolorf.r, fogcolorf.g, fogcolorf.b, fogcolorf.a,
};
m_fog_color.set(fogcolorfa, services);
float fog_distance = 10000 * BS; float fog_distance = 10000 * BS;
if (m_fog_enabled && !*m_force_fog_off) if (m_fog_enabled && !*m_force_fog_off)
@ -494,11 +491,7 @@ public:
u32 daynight_ratio = (float)m_client->getEnv().getDayNightRatio(); u32 daynight_ratio = (float)m_client->getEnv().getDayNightRatio();
video::SColorf sunlight; video::SColorf sunlight;
get_sunlight_color(&sunlight, daynight_ratio); get_sunlight_color(&sunlight, daynight_ratio);
float dnc[3] = { m_day_light.set(sunlight, services);
sunlight.r,
sunlight.g,
sunlight.b };
m_day_light.set(dnc, services);
video::SColorf star_color = m_sky->getCurrentStarColor(); video::SColorf star_color = m_sky->getCurrentStarColor();
float clr[4] = {star_color.r, star_color.g, star_color.b, star_color.a}; float clr[4] = {star_color.r, star_color.g, star_color.b, star_color.a};
@ -513,24 +506,18 @@ public:
m_animation_timer_delta_vertex.set(&animation_timer_delta_f, services); m_animation_timer_delta_vertex.set(&animation_timer_delta_f, services);
m_animation_timer_delta_pixel.set(&animation_timer_delta_f, services); m_animation_timer_delta_pixel.set(&animation_timer_delta_f, services);
float eye_position_array[3];
v3f epos = m_client->getEnv().getLocalPlayer()->getEyePosition(); v3f epos = m_client->getEnv().getLocalPlayer()->getEyePosition();
epos.getAs3Values(eye_position_array); m_eye_position_pixel.set(epos, services);
m_eye_position_pixel.set(eye_position_array, services); m_eye_position_vertex.set(epos, services);
m_eye_position_vertex.set(eye_position_array, services);
if (m_client->getMinimap()) { if (m_client->getMinimap()) {
float minimap_yaw_array[3];
v3f minimap_yaw = m_client->getMinimap()->getYawVec(); v3f minimap_yaw = m_client->getMinimap()->getYawVec();
minimap_yaw.getAs3Values(minimap_yaw_array); m_minimap_yaw.set(minimap_yaw, services);
m_minimap_yaw.set(minimap_yaw_array, services);
} }
float camera_offset_array[3];
v3f offset = intToFloat(m_client->getCamera()->getOffset(), BS); v3f offset = intToFloat(m_client->getCamera()->getOffset(), BS);
offset.getAs3Values(camera_offset_array); m_camera_offset_pixel.set(offset, services);
m_camera_offset_pixel.set(camera_offset_array, services); m_camera_offset_vertex.set(offset, services);
m_camera_offset_vertex.set(camera_offset_array, services);
SamplerLayer_t tex_id; SamplerLayer_t tex_id;
tex_id = 0; tex_id = 0;
@ -542,8 +529,8 @@ public:
tex_id = 3; tex_id = 3;
m_texture3.set(&tex_id, services); m_texture3.set(&tex_id, services);
m_texel_size0_vertex.set(m_texel_size0_values.data(), services); m_texel_size0_vertex.set(m_texel_size0, services);
m_texel_size0_pixel.set(m_texel_size0_values.data(), services); m_texel_size0_pixel.set(m_texel_size0, services);
const AutoExposure &exposure_params = m_client->getEnv().getLocalPlayer()->getLighting().exposure; const AutoExposure &exposure_params = m_client->getEnv().getLocalPlayer()->getLighting().exposure;
std::array<float, 7> exposure_buffer = { std::array<float, 7> exposure_buffer = {
@ -577,14 +564,12 @@ public:
transform.transformVect(sun_position); transform.transformVect(sun_position);
sun_position.normalize(); sun_position.normalize();
float sun_position_array[3] = { sun_position.X, sun_position.Y, sun_position.Z}; m_sun_position_pixel.set(sun_position, services);
m_sun_position_pixel.set(sun_position_array, services);
float sun_brightness = rangelim(107.143f * m_sky->getSunDirection().dotProduct(v3f(0.f, 1.f, 0.f)), 0.f, 1.f); float sun_brightness = rangelim(107.143f * m_sky->getSunDirection().dotProduct(v3f(0.f, 1.f, 0.f)), 0.f, 1.f);
m_sun_brightness_pixel.set(&sun_brightness, services); m_sun_brightness_pixel.set(&sun_brightness, services);
} else { } else {
float sun_position_array[3] = { 0.f, 0.f, -1.f }; m_sun_position_pixel.set(v3f(0.f, 0.f, -1.f), services);
m_sun_position_pixel.set(sun_position_array, services);
float sun_brightness = 0.f; float sun_brightness = 0.f;
m_sun_brightness_pixel.set(&sun_brightness, services); m_sun_brightness_pixel.set(&sun_brightness, services);
@ -596,15 +581,12 @@ public:
transform.transformVect(moon_position); transform.transformVect(moon_position);
moon_position.normalize(); moon_position.normalize();
float moon_position_array[3] = { moon_position.X, moon_position.Y, moon_position.Z}; m_moon_position_pixel.set(moon_position, services);
m_moon_position_pixel.set(moon_position_array, services);
float moon_brightness = rangelim(107.143f * m_sky->getMoonDirection().dotProduct(v3f(0.f, 1.f, 0.f)), 0.f, 1.f); float moon_brightness = rangelim(107.143f * m_sky->getMoonDirection().dotProduct(v3f(0.f, 1.f, 0.f)), 0.f, 1.f);
m_moon_brightness_pixel.set(&moon_brightness, services); m_moon_brightness_pixel.set(&moon_brightness, services);
} } else {
else { m_moon_position_pixel.set(v3f(0.f, 0.f, -1.f), services);
float moon_position_array[3] = { 0.f, 0.f, -1.f };
m_moon_position_pixel.set(moon_position_array, services);
float moon_brightness = 0.f; float moon_brightness = 0.f;
m_moon_brightness_pixel.set(&moon_brightness, services); m_moon_brightness_pixel.set(&moon_brightness, services);
@ -619,12 +601,9 @@ public:
video::ITexture *texture = material.getTexture(0); video::ITexture *texture = material.getTexture(0);
if (texture) { if (texture) {
core::dimension2du size = texture->getSize(); core::dimension2du size = texture->getSize();
m_texel_size0_values[0] = 1.f / size.Width; m_texel_size0 = v2f(1.f / size.Width, 1.f / size.Height);
m_texel_size0_values[1] = 1.f / size.Height; } else {
} m_texel_size0 = v2f();
else {
m_texel_size0_values[0] = 0.f;
m_texel_size0_values[1] = 0.f;
} }
} }
}; };

@ -227,7 +227,7 @@ public:
// Set world matrix // Set world matrix
core::matrix4 world = driver->getTransform(video::ETS_WORLD); core::matrix4 world = driver->getTransform(video::ETS_WORLD);
m_world.set(*reinterpret_cast<float(*)[16]>(world.pointer()), services); m_world.set(world, services);
// Set clip matrix // Set clip matrix
core::matrix4 worldView; core::matrix4 worldView;
@ -237,12 +237,12 @@ public:
core::matrix4 worldViewProj; core::matrix4 worldViewProj;
worldViewProj = driver->getTransform(video::ETS_PROJECTION); worldViewProj = driver->getTransform(video::ETS_PROJECTION);
worldViewProj *= worldView; worldViewProj *= worldView;
m_world_view_proj.set(*reinterpret_cast<float(*)[16]>(worldViewProj.pointer()), services); m_world_view_proj.set(worldViewProj, services);
if (driver->getDriverType() == video::EDT_OGLES2 || driver->getDriverType() == video::EDT_OPENGL3) { if (driver->getDriverType() == video::EDT_OGLES2 || driver->getDriverType() == video::EDT_OPENGL3) {
core::matrix4 texture = driver->getTransform(video::ETS_TEXTURE_0); core::matrix4 texture = driver->getTransform(video::ETS_TEXTURE_0);
m_world_view.set(*reinterpret_cast<float(*)[16]>(worldView.pointer()), services); m_world_view.set(worldView, services);
m_texture.set(*reinterpret_cast<float(*)[16]>(texture.pointer()), services); m_texture.set(texture, services);
} }
} }
}; };

@ -105,6 +105,57 @@ public:
has_been_set = true; 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> template <typename T, std::size_t count = 1, bool cache=true>

@ -32,9 +32,7 @@ void ShadowConstantSetter::onSetConstants(video::IMaterialRendererServices *serv
shadowViewProj *= light.getViewMatrix(); shadowViewProj *= light.getViewMatrix();
m_shadow_view_proj.set(shadowViewProj.pointer(), services); m_shadow_view_proj.set(shadowViewProj.pointer(), services);
f32 v_LightDirection[3]; m_light_direction.set(light.getDirection(), services);
light.getDirection().getAs3Values(v_LightDirection);
m_light_direction.set(v_LightDirection, services);
f32 TextureResolution = light.getMapResolution(); f32 TextureResolution = light.getMapResolution();
m_texture_res.set(&TextureResolution, services); m_texture_res.set(&TextureResolution, services);
@ -79,7 +77,7 @@ void ShadowDepthShaderCB::OnSetConstants(
lightMVP *= driver->getTransform(video::ETS_WORLD); lightMVP *= driver->getTransform(video::ETS_WORLD);
m_light_mvp_setting.set(lightMVP.pointer(), services); m_light_mvp_setting.set(lightMVP, services);
m_map_resolution_setting.set(&MapRes, services); m_map_resolution_setting.set(&MapRes, services);
m_max_far_setting.set(&MaxFar, services); m_max_far_setting.set(&MaxFar, services);
s32 TextureId = 0; s32 TextureId = 0;