1
0
mirror of https://github.com/luanti-org/luanti.git synced 2025-11-21 08:55:21 +01:00

Check shadow map initialization more carefully

This commit is contained in:
sfan5
2025-11-14 15:23:10 +01:00
parent d92e9e3126
commit d280d10e29
3 changed files with 44 additions and 29 deletions

View File

@@ -21,8 +21,8 @@ ShadowRenderer::ShadowRenderer(IrrlichtDevice *device, Client *client) :
m_time_day(0.0f), m_force_update_shadow_map(false), m_current_frame(0), m_time_day(0.0f), m_force_update_shadow_map(false), m_current_frame(0),
m_perspective_bias_xy(0.8f), m_perspective_bias_z(0.5f) m_perspective_bias_xy(0.8f), m_perspective_bias_z(0.5f)
{ {
m_shadows_supported = true; // assume shadows supported. We will check actual support in initialize m_shadows_supported = true; // we will check actual support in initialize()
m_shadows_enabled = true; m_shadows_enabled = false;
m_shadow_strength_gamma = g_settings->getFloat("shadow_strength_gamma"); m_shadow_strength_gamma = g_settings->getFloat("shadow_strength_gamma");
if (std::isnan(m_shadow_strength_gamma)) if (std::isnan(m_shadow_strength_gamma))
@@ -112,20 +112,38 @@ void ShadowRenderer::preInit(IWritableShaderSource *shsrc)
} }
} }
void ShadowRenderer::initialize() bool ShadowRenderer::initialize()
{ {
m_shadows_supported = ShadowRenderer::isSupported(m_driver);
if (!m_shadows_supported)
return false;
/* Set up texture formats */
auto &fmt1 = m_texture_format;
auto &fmt2 = m_texture_format_color;
if (m_shadow_map_texture_32bit && m_driver->queryTextureFormat(video::ECF_R32F))
fmt1 = video::ECF_R32F;
else if (m_driver->queryTextureFormat(video::ECF_R16F))
fmt1 = video::ECF_R16F;
if (m_shadow_map_texture_32bit && m_driver->queryTextureFormat(video::ECF_G32R32F))
fmt2 = video::ECF_G32R32F;
else if (m_driver->queryTextureFormat(video::ECF_G16R16F))
fmt2 = video::ECF_G16R16F;
infostream << "ShadowRenderer: color format = " << video::ColorFormatName(fmt1)
<< " or " << video::ColorFormatName(fmt2) << std::endl;
// Note: this is just a sanity check since the version checks in isSupported()
// should already guarantee availability
if (fmt1 == video::ECF_UNKNOWN || fmt2 == video::ECF_UNKNOWN)
m_shadows_supported = false;
if (!m_shadows_supported)
return false;
createShaders(); createShaders();
return true;
m_texture_format = m_shadow_map_texture_32bit
? video::ECOLOR_FORMAT::ECF_R32F
: video::ECOLOR_FORMAT::ECF_R16F;
m_texture_format_color = m_shadow_map_texture_32bit
? video::ECOLOR_FORMAT::ECF_G32R32F
: video::ECOLOR_FORMAT::ECF_G16R16F;
m_shadows_enabled &= m_shadows_supported;
} }
@@ -228,7 +246,7 @@ void ShadowRenderer::updateSMTextures()
assert(shadowMapTextureColors != nullptr); assert(shadowMapTextureColors != nullptr);
} }
// The merge all shadowmaps texture // Then merge all shadowmap textures
if (!shadowMapTextureFinal) { if (!shadowMapTextureFinal) {
video::ECOLOR_FORMAT frt; video::ECOLOR_FORMAT frt;
if (m_shadow_map_texture_32bit) { if (m_shadow_map_texture_32bit) {
@@ -580,21 +598,16 @@ std::unique_ptr<ShadowRenderer> createShadowRenderer(IrrlichtDevice *device, Cli
if (!g_settings->getBool("enable_dynamic_shadows")) if (!g_settings->getBool("enable_dynamic_shadows"))
return nullptr; return nullptr;
// disable if unsupported auto renderer = std::make_unique<ShadowRenderer>(device, client);
if (!ShadowRenderer::isSupported(device)) { if (!renderer->initialize()) {
warningstream << "Shadows: disabled dynamic shadows due to being unsupported" << std::endl; warningstream << "Disabling dynamic shadows due to being unsupported." << std::endl;
g_settings->setBool("enable_dynamic_shadows", false); renderer.reset();
return nullptr;
} }
return renderer;
auto shadow_renderer = std::make_unique<ShadowRenderer>(device, client);
shadow_renderer->initialize();
return shadow_renderer;
} }
bool ShadowRenderer::isSupported(IrrlichtDevice *device) bool ShadowRenderer::isSupported(video::IVideoDriver *driver)
{ {
auto driver = device->getVideoDriver();
const video::E_DRIVER_TYPE type = driver->getDriverType(); const video::E_DRIVER_TYPE type = driver->getDriverType();
v2s32 glver = driver->getLimits().GLVersion; v2s32 glver = driver->getLimits().GLVersion;

View File

@@ -51,7 +51,8 @@ public:
// the shaders are dealt with. // the shaders are dealt with.
static void preInit(IWritableShaderSource *shsrc); static void preInit(IWritableShaderSource *shsrc);
void initialize(); /// @return shadows supported?
bool initialize();
/// Adds a directional light shadow map (Usually just one (the sun) except in /// Adds a directional light shadow map (Usually just one (the sun) except in
/// Tattoine ). /// Tattoine ).
@@ -92,7 +93,7 @@ public:
f32 getPerspectiveBiasXY() { return m_perspective_bias_xy; } f32 getPerspectiveBiasXY() { return m_perspective_bias_xy; }
f32 getPerspectiveBiasZ() { return m_perspective_bias_z; } f32 getPerspectiveBiasZ() { return m_perspective_bias_z; }
static bool isSupported(IrrlichtDevice *device); static bool isSupported(video::IVideoDriver *driver);
private: private:
video::ITexture *getSMTexture(const std::string &shadow_map_name, video::ITexture *getSMTexture(const std::string &shadow_map_name,

View File

@@ -31,7 +31,8 @@ int ModApiMenuCommon::l_get_active_driver(lua_State *L)
int ModApiMenuCommon::l_driver_supports_shadows(lua_State *L) int ModApiMenuCommon::l_driver_supports_shadows(lua_State *L)
{ {
lua_pushboolean(L, ShadowRenderer::isSupported(RenderingEngine::get_raw_device())); auto *device = RenderingEngine::get_raw_device();
lua_pushboolean(L, ShadowRenderer::isSupported(device->getVideoDriver()));
return 1; return 1;
} }