Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(resource): a shader survives its program, like a texture survives its object

esengineadbd407f0fc0
diff --git a/src/esengine/renderer/rhi/Shader.cpp b/src/esengine/renderer/rhi/Shader.cpp
index f8647c5f4..9cff405a1 100644
--- a/src/esengine/renderer/rhi/Shader.cpp
+++ b/src/esengine/renderer/rhi/Shader.cpp
@@ -147,12 +147,34 @@ void Shader::unbind() const {
if (device_) device_->useProgram(ShaderHandle::Invalid);
}
+bool Shader::recompile() {
+ if (!device_ || vertexSource_.empty()) return false;
+ // Copied out first: compile() assigns these members from its arguments, and
+ // passing them to it directly would be self-assignment.
+ const std::string vert = vertexSource_;
+ const std::string frag = fragmentSource_;
+ const std::vector<AttribBinding> bindings = attribBindings_;
+
+ // The program, its resolved locations and the DrawParams UBO all died with
+ // the device. Cleared rather than deleted; compile() rebuilds them.
+ program_ = ShaderHandle::Invalid;
+ paramsUbo_ = BufferHandle::Invalid;
+ uniformCache_.clear();
+ attribCache_.clear();
+ activeUniforms_.clear();
+
+ return compile(vert, frag, bindings, nullptr, nullptr, language_);
+}
+
bool Shader::compile(const std::string& vertexSrc, const std::string& fragmentSrc,
- std::initializer_list<AttribBinding> bindings,
+ const std::vector<AttribBinding>& bindings,
std::string* outLog,
ShaderStageFailure* outFailedStage,
GfxShaderLanguage language) {
language_ = language;
+ vertexSource_ = vertexSrc;
+ fragmentSource_ = fragmentSrc;
+ attribBindings_.assign(bindings.begin(), bindings.end());
// Fail fast on a language the backend cannot compile, before any GPU call —
// the caller gets a diagnostic instead of a backend-specific compile error.
diff --git a/src/esengine/renderer/rhi/Shader.hpp b/src/esengine/renderer/rhi/Shader.hpp
index 73f5b5071..3b8747217 100644
--- a/src/esengine/renderer/rhi/Shader.hpp
+++ b/src/esengine/renderer/rhi/Shader.hpp
@@ -245,6 +245,10 @@ public:
/** @brief Gets the program handle's raw value, for command payloads and sort keys */
u32 getProgramId() const { return static_cast<u32>(program_); }
+ /// Rebuilds the program from the sources it was compiled with, after the
+ /// device that held it went away. The handle naming this Shader is unchanged.
+ bool recompile();
+
private:
/**
* @brief Compiles and links shader sources
@@ -253,7 +257,7 @@ private:
* @return True on success
*/
bool compile(const std::string& vertexSrc, const std::string& fragmentSrc,
- std::initializer_list<AttribBinding> bindings = {},
+ const std::vector<AttribBinding>& bindings = {},
std::string* outLog = nullptr,
ShaderStageFailure* outFailedStage = nullptr,
GfxShaderLanguage language = GfxShaderLanguage::GLSL_ES300);
@@ -267,6 +271,13 @@ private:
* @return True when consumed (caller must not fall through to a loose upload). */
bool writeParam(const std::string& name, DrawParamType type, const void* src) const;
+ /// What this program was built from, kept so it can be rebuilt behind the
+ /// SAME handle after a device loss — which is what keeps a material's
+ /// shaderRef valid. A few KB each; a texture's pixels are not.
+ std::string vertexSource_;
+ std::string fragmentSource_;
+ std::vector<AttribBinding> attribBindings_;
+
GfxDevice* device_ = nullptr; ///< Set by the create* factories; all GL goes through it.
GfxShaderLanguage language_ = GfxShaderLanguage::GLSL_ES300;
ShaderHandle program_ = ShaderHandle::Invalid;
diff --git a/src/esengine/resource/ResourceManager.cpp b/src/esengine/resource/ResourceManager.cpp
index 49b776a13..ab4958a7a 100644
--- a/src/esengine/resource/ResourceManager.cpp
+++ b/src/esengine/resource/ResourceManager.cpp
@@ -266,6 +266,18 @@ TextureHandle ResourceManager::registerExternalTexture(u32 glTextureId, u32 widt
return textures_.add(std::move(texture), "", bytes);
}
+u32 ResourceManager::recreateGpuShaders() {
+ u32 rebuilt = 0;
+ u32 failed = 0;
+ shaders_.forEachAlive([&](ShaderHandle, Shader& shader) {
+ if (shader.recompile()) ++rebuilt;
+ else ++failed;
+ });
+ if (failed > 0) ES_LOG_ERROR("Device recovery: {} shader(s) failed to rebuild", failed);
+ ES_LOG_INFO("Device recovery: {} shader(s) rebuilt behind their handles", rebuilt);
+ return rebuilt;
+}
+
void ResourceManager::invalidateGpuTextures(::esengine::TextureHandle placeholder) {
awaitingReupload_.clear();
textures_.forEachAlive([&](TextureHandle handle, Texture& texture) {
diff --git a/src/esengine/resource/ResourceManager.hpp b/src/esengine/resource/ResourceManager.hpp
index e7fe84fb0..cc18a15c0 100644
--- a/src/esengine/resource/ResourceManager.hpp
+++ b/src/esengine/resource/ResourceManager.hpp
@@ -296,6 +296,15 @@ public:
* textures by them, so re-uploading behind one is invisible.
* Sampling the placeholder meanwhile renders pale, not garbage.
*/
+ /**
+ * @brief Re-compiles every shader behind its existing handle.
+ * @details Shaders keep their sources precisely so this is possible: a
+ * material's shaderRef and a plugin's handle stay valid, where
+ * creating NEW shaders would invalidate every one of them.
+ * @return How many were rebuilt.
+ */
+ u32 recreateGpuShaders();
+
void invalidateGpuTextures(::esengine::TextureHandle placeholder);
/**