Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return to the main menu if a shader compilation fails #14256

Merged
merged 6 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/client/clientlauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
error_message = gettext("Connection error (timed out?)");
errorstream << error_message << std::endl;
}
catch (ShaderException &e) {
error_message = e.what();
errorstream << error_message << std::endl;
}

#ifdef NDEBUG
catch (std::exception &e) {
Expand Down
65 changes: 57 additions & 8 deletions src/client/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,44 @@ class MainShaderConstantSetterFactory : public IShaderConstantSetterFactory
};


namespace {
class IrrlichtErrorCapturer : public IEventReceiver
{
std::string &m_output;
IEventReceiver *m_old_recv;

public:
IrrlichtErrorCapturer(std::string &output):
m_output{output}
{
IrrlichtDevice *device{RenderingEngine::get_raw_device()};
m_old_recv = device->getEventReceiver();
device->setEventReceiver(this);
}

~IrrlichtErrorCapturer()
{
IrrlichtDevice *device{RenderingEngine::get_raw_device()};
device->setEventReceiver(m_old_recv);
}

IrrlichtErrorCapturer(const IrrlichtErrorCapturer&) = delete;
IrrlichtErrorCapturer &operator=(const IrrlichtErrorCapturer&) = delete;

virtual bool OnEvent(const SEvent &event)
{
if (event.EventType == EET_LOG_TEXT_EVENT &&
event.LogEvent.Level == ELL_ERROR) {
// Log messages from Irrlicht are *usually* not
// newline-terminated.
m_output.append(event.LogEvent.Text).append("\n");
return true;
}
return m_old_recv->OnEvent(event);
}
};
}

/*
ShaderSource
*/
Expand Down Expand Up @@ -588,8 +626,8 @@ ShaderInfo ShaderSource::generateShader(const std::string &name,

video::IVideoDriver *driver = RenderingEngine::get_video_driver();
if (!driver->queryFeature(video::EVDF_ARB_GLSL)) {
errorstream << "Shaders are enabled but GLSL is not supported by the driver\n";
return shaderinfo;
throw ShaderException("Shaders are enabled but GLSL is not supported "
"by the driver");
HybridDog marked this conversation as resolved.
Show resolved Hide resolved
}
video::IGPUProgrammingServices *gpu = driver->getGPUProgrammingServices();

Expand Down Expand Up @@ -777,11 +815,16 @@ ShaderInfo ShaderSource::generateShader(const std::string &name,

irr_ptr<ShaderCallback> cb{new ShaderCallback(m_setter_factories)};
infostream<<"Compiling high level shaders for "<<name<<std::endl;
s32 shadermat = gpu->addHighLevelShaderMaterial(
vertex_shader.c_str(), nullptr, video::EVST_VS_1_1,
fragment_shader.c_str(), nullptr, video::EPST_PS_1_1,
geometry_shader_ptr, nullptr, video::EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLES, 0,
cb.get(), shaderinfo.base_material, 1);
std::string shader_compilation_errors{""};
s32 shadermat;
{
IrrlichtErrorCapturer capturer{shader_compilation_errors};
shadermat = gpu->addHighLevelShaderMaterial(
vertex_shader.c_str(), nullptr, video::EVST_VS_1_1,
fragment_shader.c_str(), nullptr, video::EPST_PS_1_1,
geometry_shader_ptr, nullptr, video::EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLES, 0,
cb.get(), shaderinfo.base_material, 1);
}
if (shadermat == -1) {
errorstream<<"generate_shader(): "
"failed to generate \""<<name<<"\", "
Expand All @@ -790,7 +833,13 @@ ShaderInfo ShaderSource::generateShader(const std::string &name,
dumpShaderProgram(warningstream, "Vertex", vertex_shader);
dumpShaderProgram(warningstream, "Fragment", fragment_shader);
dumpShaderProgram(warningstream, "Geometry", geometry_shader);
return shaderinfo;
throw ShaderException(
"Failed to compile the \"" + name + "\" shader.\n" +
shader_compilation_errors +
"Check debug.txt for details.");
HybridDog marked this conversation as resolved.
Show resolved Hide resolved
} else if (!shader_compilation_errors.empty()) {
// Don't suppress any error messages.
errorstream << shader_compilation_errors;
SmallJoker marked this conversation as resolved.
Show resolved Hide resolved
}

// Apply the newly created material type
Expand Down
5 changes: 5 additions & 0 deletions src/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ class PrngException : public BaseException {
PrngException(const std::string &s): BaseException(s) {}
};

class ShaderException : public BaseException {
public:
ShaderException(const std::string &s): BaseException(s) {}
};

class ModError : public BaseException {
public:
ModError(const std::string &s): BaseException(s) {}
Expand Down