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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(renderer): three more subsystems can rebuild their GPU objects
esenginef47f737a8cf7
diff --git a/src/esengine/renderer/frame/RenderContext.cpp b/src/esengine/renderer/frame/RenderContext.cppindex b1eb3cde2..0bc3a3db7 100644--- a/src/esengine/renderer/frame/RenderContext.cpp+++ b/src/esengine/renderer/frame/RenderContext.cpp@@ -78,6 +78,26 @@ void RenderContext::shutdown() {ES_LOG_INFO("RenderContext shutdown");}+void RenderContext::recreateGpuResources() {+ if (!initialized_) return;+ // Handles are dropped, not deleted: the objects died with the device, and+ // asking it to free their ids would look up metadata the backend just wiped.+ whiteTexture_ = TextureHandle::Invalid;+ blackTexture_ = TextureHandle::Invalid;+ flatNormalTexture_ = TextureHandle::Invalid;+ frameUbo_ = BufferHandle::Invalid;+ timeUbo_ = BufferHandle::Invalid;+ drawParamsFallback_ = BufferHandle::Invalid;++ initDefaultTextures();+ initFrameUbo();+ // The built-ins are new objects, so the materials falling back to them have+ // to be told; otherwise every unset texture param samples a dead id.+ materials_.setBuiltinDefaults(whiteTexture_, blackTexture_, flatNormalTexture_);+ materials_.recreateGpuResources();+ lights_.recreateGpuResources();+}+TextureHandle RenderContext::make1x1Texture(u32 rgba) {TextureDesc desc;desc.width = 1;diff --git a/src/esengine/renderer/frame/RenderContext.hpp b/src/esengine/renderer/frame/RenderContext.hppindex fc43113ba..fbfadc0f0 100644--- a/src/esengine/renderer/frame/RenderContext.hpp+++ b/src/esengine/renderer/frame/RenderContext.hpp@@ -99,6 +99,10 @@ public:*/void shutdown();+ /// Rebuilds the built-in textures and frame UBOs after a device loss, and+ /// re-points the stores that depend on them.+ void recreateGpuResources();+/*** @brief Checks if the context is initialized* @return True if init() has been called successfullydiff --git a/src/esengine/renderer/rhi/RenderTarget.cpp b/src/esengine/renderer/rhi/RenderTarget.cppindex 1f12b592b..293899d2a 100644--- a/src/esengine/renderer/rhi/RenderTarget.cpp+++ b/src/esengine/renderer/rhi/RenderTarget.cpp@@ -4,6 +4,13 @@namespace esengine {+void RenderTarget::recreateGpuResources(GfxDevice& device) {+ // The framebuffer and its attachment textures died with the device; the size+ // and format it was created at are still here, which is all init needs.+ framebuffer_.reset();+ init(device, width_, height_, has_depth_, linear_filter_);+}+void RenderTarget::init(GfxDevice& device, u32 width, u32 height, bool depth, bool linearFilter) {width_ = width;height_ = height;diff --git a/src/esengine/renderer/rhi/RenderTarget.hpp b/src/esengine/renderer/rhi/RenderTarget.hppindex 9751fe8d8..f6f706aa7 100644--- a/src/esengine/renderer/rhi/RenderTarget.hpp+++ b/src/esengine/renderer/rhi/RenderTarget.hpp@@ -33,6 +33,9 @@ public:u32 getWidth() const { return width_; }u32 getHeight() const { return height_; }+ /// Re-creates the framebuffer after a device loss, at the size it already has.+ void recreateGpuResources(GfxDevice& device);+bool isValid() const { return framebuffer_ != nullptr; }FramebufferHandle getFramebuffer() const;@@ -58,6 +61,15 @@ public:Handle create(u32 width, u32 height, bool depth = true, bool linearFilter = false);RenderTarget* get(Handle handle);void release(Handle handle);++ /// Re-creates every live target after a device loss. Handles are indices, so+ /// everything holding one keeps pointing at the same target.+ void recreateGpuResources() {+ if (!device_) return;+ for (auto& target : targets_) {+ if (target) target->recreateGpuResources(*device_);+ }+ }bool isValid(Handle handle) const;private:diff --git a/src/esengine/renderer/rhi/TransientBufferPool.cpp b/src/esengine/renderer/rhi/TransientBufferPool.cppindex 023cf3a1d..b0f035c21 100644--- a/src/esengine/renderer/rhi/TransientBufferPool.cpp+++ b/src/esengine/renderer/rhi/TransientBufferPool.cpp@@ -24,6 +24,24 @@ void TransientBufferPool::init(u32 initialVertexBytes, u32 initialIndexCount) {initialized_ = true;}+void TransientBufferPool::recreateGpuResources() {+ if (!initialized_) return;+ // Dropped without deleting, then re-set up from the same initial sizes. The+ // staging vectors are CPU-side and survive; only their write positions reset.+ for (auto& s : streams_) {+ s.vbo = BufferHandle::Invalid;+ s.ebo = BufferHandle::Invalid;+ s.quad_vbo = BufferHandle::Invalid;+ s.layout = VertexLayoutHandle::Invalid;+ s.vbo_capacity = 0;+ s.ebo_capacity = 0;+ s.vertex_write_pos = 0;+ s.index_write_pos = 0;+ }+ initialized_ = false;+ init(initial_vertex_bytes_, initial_index_count_);+}+void TransientBufferPool::shutdown() {if (!initialized_) return;diff --git a/src/esengine/renderer/rhi/TransientBufferPool.hpp b/src/esengine/renderer/rhi/TransientBufferPool.hppindex df0075fd6..852f35199 100644--- a/src/esengine/renderer/rhi/TransientBufferPool.hpp+++ b/src/esengine/renderer/rhi/TransientBufferPool.hpp@@ -32,6 +32,9 @@ public:u32 initialIndexCount = 256 * 1024);void shutdown();+ /// Re-creates the streams' GPU buffers and layouts after a device loss.+ void recreateGpuResources();+void beginFrame();u32 allocVertices(LayoutId layout, u32 byteSize);