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(renderer): the stores give their buffers back on a device loss

esengineb9554121d04f
diff --git a/src/esengine/renderer/store/LightStore.hpp b/src/esengine/renderer/store/LightStore.hpp
index d1554ea42..87317cac8 100644
--- a/src/esengine/renderer/store/LightStore.hpp
+++ b/src/esengine/renderer/store/LightStore.hpp
@@ -35,6 +35,13 @@ public:
/// The render path uses this device to create/upload/delete the lighting UBO.
void setDevice(GfxDevice* device) { device_ = device; }
+ /// Forgets the UBO after a device loss WITHOUT deleting it — the buffer died
+ /// with the device, and the upload path creates a fresh one on demand.
+ void recreateGpuResources() {
+ ubo_ = BufferHandle::Invalid;
+ dirty_ = true;
+ }
+
/// Begins a frame's collection: zeroes ambient + all light slots (inactive slots contribute
/// nothing because their intensity is 0) and marks the UBO for re-upload.
void clear() {
diff --git a/src/esengine/renderer/store/MaterialStore.cpp b/src/esengine/renderer/store/MaterialStore.cpp
index 7769b3414..1b56bf29f 100644
--- a/src/esengine/renderer/store/MaterialStore.cpp
+++ b/src/esengine/renderer/store/MaterialStore.cpp
@@ -29,6 +29,19 @@ void MaterialStore::clear() {
layouts_.clear();
}
+void MaterialStore::recreateGpuResources() {
+ // Buffers are dropped, not deleted: they died with the device. The material
+ // RECORDS stay, so every materialId the scene refers to still resolves and
+ // bindForDraw re-creates the UBO from the parameters it already holds.
+ for (auto& [id, rec] : materials_) {
+ rec.ubo = BufferHandle::Invalid;
+ rec.uboDirty = true;
+ }
+ // Texture bindings are NOT covered and cannot be from here: setTexture
+ // resolves a resource::TextureHandle to a raw GPU id at call time, so the
+ // record no longer knows which texture it meant.
+}
+
void MaterialStore::bindForDraw(u32 materialId) {
if (!device_) return;
auto it = materials_.find(materialId);
diff --git a/src/esengine/renderer/store/MaterialStore.hpp b/src/esengine/renderer/store/MaterialStore.hpp
index aa84edf4b..96736f7e1 100644
--- a/src/esengine/renderer/store/MaterialStore.hpp
+++ b/src/esengine/renderer/store/MaterialStore.hpp
@@ -115,6 +115,10 @@ public:
/// The render path uses this device to create/upload/delete per-material UBOs.
void setDevice(GfxDevice* device) { device_ = device; }
+ /// Drops every material UBO after a device loss, keeping the records so the
+ /// scene's materialIds stay meaningful. See the definition.
+ void recreateGpuResources();
+
/// Registers (or replaces) a shader's MaterialConstants layout — called when a shader
/// authored with #pragma param is compiled, so materials on it can pack their uniforms.
void registerLayout(u32 shaderId, MaterialUniformLayout layout) {