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.

fix(render): a shadow view draws through its own matrix, not the last one's

yhh065e9a60ca7f
diff --git a/src/esengine/renderer/frame/RenderContext.cpp b/src/esengine/renderer/frame/RenderContext.cpp
index 1bf91698e..c92685f5c 100644
--- a/src/esengine/renderer/frame/RenderContext.cpp
+++ b/src/esengine/renderer/frame/RenderContext.cpp
@@ -164,6 +164,21 @@ void RenderContext::updateFrameConstants(const glm::mat4& viewProjection) {
uploadFrameConstants(viewProjection, viewProjection);
}
+u32 RenderContext::frameConstantsSize() {
+ return static_cast<u32>(sizeof(FrameConstants));
+}
+
+void RenderContext::useFrameConstants(BufferHandle ubo, const glm::mat4& viewProjection) {
+ if (ubo == BufferHandle::Invalid) return;
+ const FrameConstants frame{viewProjection, cameraFromViewProjection(viewProjection),
+ glm::inverse(viewProjection)};
+ device_.updateBuffer(ubo, 0, &frame, sizeof(FrameConstants));
+ device_.setUniformBuffer(FRAME_CONSTANTS_BINDING, ubo);
+ // Same re-arm the frame's own upload does: a previous pass may have left a
+ // differently sized params buffer on the shared slot.
+ device_.setUniformBuffer(DRAW_PARAMS_BINDING, drawParamsFallback_);
+}
+
// `engine` is the projection in the engine's own convention, which is what the
// viewpoint is read from: the two agree on it, but only one of them is the answer
// to "where is the eye" that the rest of the engine also holds.
@@ -174,6 +189,10 @@ void RenderContext::uploadFrameConstants(const glm::mat4& upload, const glm::mat
// space a shader will hand back.
const FrameConstants frame{upload, cameraFromViewProjection(engine), glm::inverse(upload)};
device_.updateBuffer(frameUbo_, 0, &frame, sizeof(FrameConstants));
+ // Take the binding back. A pass that draws several views points this slot at
+ // one buffer per view; without this the scene would keep reading whichever
+ // view was bound last, having uploaded its own camera to a buffer nothing reads.
+ device_.setUniformBuffer(FRAME_CONSTANTS_BINDING, frameUbo_);
// Re-arm the params fallback for this pass: a post-process or custom-draw
// commit from the previous pass left its own (differently sized) buffer on
// the shared slot.
diff --git a/src/esengine/renderer/frame/RenderContext.hpp b/src/esengine/renderer/frame/RenderContext.hpp
index b805b610f..747e2699a 100644
--- a/src/esengine/renderer/frame/RenderContext.hpp
+++ b/src/esengine/renderer/frame/RenderContext.hpp
@@ -137,6 +137,18 @@ public:
*/
void updateFrameConstants(const glm::mat4& viewProjection);
+ /**
+ * @brief Frame constants for @p viewProjection in a buffer the CALLER owns, bound.
+ * @details For a pass that draws several views. A buffer write is a queue
+ * operation and does not land between draws already recorded into an
+ * open pass, so views sharing one UBO all read the last write. One
+ * buffer each makes the BINDING, an encoder command, what varies.
+ */
+ void useFrameConstants(BufferHandle ubo, const glm::mat4& viewProjection);
+
+ /** @brief Bytes a buffer passed to useFrameConstants must hold. */
+ static u32 frameConstantsSize();
+
/** @brief Uploads the frame clock + canvas size into the injected TimeConstants UBO. */
void setFrameTime(f32 elapsedSec, u32 viewportW, u32 viewportH);
diff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cpp
index 65740479a..0c8fea0ce 100644
--- a/src/esengine/renderer/frame/RenderFrame.cpp
+++ b/src/esengine/renderer/frame/RenderFrame.cpp
@@ -180,6 +180,7 @@ void RenderFrame::shutdown() {
plugins_.clear();
pool_.shutdown();
shadow_pool_.shutdown();
+ releaseShadowFrameUbos();
releaseFrameTargets();
target_pool_.clear();
@@ -196,6 +197,10 @@ void RenderFrame::shutdown() {
void RenderFrame::recreateGpuResources() {
pool_.recreateGpuResources();
shadow_pool_.recreateGpuResources();
+ // The buffers behind these died with the device; the next pass makes as many
+ // as it needs. Kept here rather than recreated eagerly: how many is a property
+ // of the frame that has not been collected yet.
+ releaseShadowFrameUbos();
target_manager_.recreateGpuResources();
// The framebuffers the pool is holding died with the device: the loans go
// back and the memory behind them goes with it, or the next frame would draw
@@ -1372,6 +1377,23 @@ void RenderFrame::declareShadowPass(ecs::Registry& registry) {
graph_.addPass(std::move(shadow));
}
+void RenderFrame::ensureShadowFrameUbos(u32 count) {
+ const u32 bytes = RenderContext::frameConstantsSize();
+ while (shadow_frame_ubos_.size() < count) {
+ const BufferHandle ubo = device_.createBuffer(
+ {GfxBufferUsage::Uniform, bytes, /*dynamic=*/true}, nullptr);
+ if (ubo == BufferHandle::Invalid) break;
+ shadow_frame_ubos_.push_back(ubo);
+ }
+}
+
+void RenderFrame::releaseShadowFrameUbos() {
+ for (BufferHandle ubo : shadow_frame_ubos_) {
+ if (ubo != BufferHandle::Invalid) device_.deleteBuffer(ubo);
+ }
+ shadow_frame_ubos_.clear();
+}
+
void RenderFrame::executeShadowPass(ecs::Registry& registry) {
// What the pass cost, for a profiler and the gates that pin it: the maps it
// actually rendered and the draws they took. Neither shows in a pixel, and a
@@ -1379,12 +1401,21 @@ void RenderFrame::executeShadowPass(ecs::Registry& registry) {
u32 shadowTiles = 0;
u32 shadowDraws = 0;
- for (const ShadowView& view : shadow_views_) {
- // Its own pool and list. The scene's are finalized and uploaded by now and
- // its pass is still to come, so collecting through them here would overwrite
- // the vertices the scene is about to draw from.
- shadow_pool_.beginFrame();
- shadow_draw_list_.clear();
+ const usize viewCount = shadow_views_.size();
+ // Reset once for the pass, NOT once per view: a draw carries an offset rather
+ // than the bytes behind it, so six faces reset to zero are six draws reading
+ // whatever landed at zero last.
+ shadow_pool_.beginFrame();
+ ensureShadowFrameUbos(static_cast<u32>(viewCount));
+ if (shadow_view_lists_.size() < viewCount) shadow_view_lists_.resize(viewCount);
+
+ // Collect every view before any is drawn, so the pool uploads once and then
+ // stays still: growth re-creates the buffer behind the handle, and a recorded
+ // draw names the buffer it was recorded with.
+ for (usize vi = 0; vi < viewCount; ++vi) {
+ const ShadowView& view = shadow_views_[vi];
+ DrawList& list = shadow_view_lists_[vi];
+ list.clear();
// What this looks through is the view's, held here rather than swapped into
// the frame's own: the scene pass after this one still reads the camera it
@@ -1397,31 +1428,40 @@ void RenderFrame::executeShadowPass(ecs::Registry& registry) {
// Only the plugins that cast are asked: a map holds depth, and everything
// else would write colour into a texture every receiver reads back as one.
RenderCollectContext collectCtx{registry, viewFrustum, clip_state_, shadow_pool_,
- shadow_draw_list_, ctx,
+ list, ctx,
computeCameraView(view.view_projection)};
for (auto& plugin : plugins_) {
if (plugin->castsShadows()) plugin->collect(collectCtx);
}
- shadow_draw_list_.finalize(shadow_pool_);
+ list.finalize(shadow_pool_);
+ }
+ shadow_pool_.upload();
+
+ for (usize vi = 0; vi < viewCount; ++vi) {
+ const ShadowView& view = shadow_views_[vi];
+ DrawList& list = shadow_view_lists_[vi];
// A cube face pointing away from every mesh collects nothing, and a point
// light has six chances at that. The square already holds what the atlas was
// cleared to — depth 1, nothing here — so drawing it would agree at cost.
- if (shadow_draw_list_.commandCount() == 0) continue;
+ if (list.commandCount() == 0) continue;
device_.setViewport(view.x, view.y, view.size, view.size);
- shadow_pool_.upload();
// Not a camera's: this projection spans exactly the occluders it draws, so
// it sits inside either clip volume as built, and the depths it writes are
// compared against the copy of it the receiving shader holds.
- context_.updateFrameConstants(view.view_projection);
+ //
+ // One buffer per view, not one written per view: the write reaches the GPU
+ // on the queue, never between draws already recorded into this open pass.
+ // Binding IS an encoder command, so that is what tells the faces apart.
+ context_.useFrameConstants(shadow_frame_ubos_[vi], view.view_projection);
// The depth variant is still a Lit shader and still declares the light block,
// and a draw whose declared UBO is unbound is undefined behaviour that draws
// nothing — the whole pass came back empty for exactly this.
context_.lights().uploadAndBind();
- shadow_draw_list_.execute(device_, shadow_pool_, context_.materials(),
- context_.getWhiteTextureId(), nullptr, context_.skinUbo());
+ list.execute(device_, shadow_pool_, context_.materials(),
+ context_.getWhiteTextureId(), nullptr, context_.skinUbo());
++shadowTiles;
- shadowDraws += shadow_draw_list_.mergedDrawCallCount();
+ shadowDraws += list.mergedDrawCallCount();
}
ES_PROFILE_COUNTER("render.shadow.tiles", shadowTiles);
diff --git a/src/esengine/renderer/frame/RenderFrame.hpp b/src/esengine/renderer/frame/RenderFrame.hpp
index 1edbe390b..b7ceaa232 100644
--- a/src/esengine/renderer/frame/RenderFrame.hpp
+++ b/src/esengine/renderer/frame/RenderFrame.hpp
@@ -489,6 +489,12 @@ private:
/// The shadow pass's body: for each planned square, collect the occluders it can
/// see and draw them. The graph has bound and cleared the atlas.
void executeShadowPass(ecs::Registry& registry);
+
+ /// Grows the per-view frame-constant buffers to @p count and no further.
+ void ensureShadowFrameUbos(u32 count);
+
+ /// Deletes them. Device loss and shutdown: the handles name nothing afterwards.
+ void releaseShadowFrameUbos();
/// The scene pass's body: the draw, and the constants it needs current.
void drawScene();
/// Drops the atlas texture id the frame published; its target is the graph's
@@ -583,7 +589,13 @@ private:
* vertices the scene is about to draw from.
*/
TransientBufferPool shadow_pool_;
- DrawList shadow_draw_list_;
+ /// One per view, kept across frames for the capacity. The whole pass is collected
+ /// before any of it is drawn, so a view's commands have to outlive its collect.
+ std::vector<DrawList> shadow_view_lists_;
+ /// One FrameConstants buffer per view, grown to the busiest frame and never shrunk.
+ /// A buffer write lands on the queue, not between two draws already recorded into
+ /// an open pass — so what tells one view from another is which buffer is BOUND.
+ std::vector<BufferHandle> shadow_frame_ubos_;
/// Who owns which square of the atlas. Rebuilt every frame: a tile means
/// nothing once the depths in it belong to a frame that is gone.
ShadowAtlas shadow_atlas_{kShadowAtlasSize, kShadowCellSize};