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(renderer): a recovered material's samplers point where they did before

esengine1e2eab35c6d1
diff --git a/src/esengine/bindings/MaterialBindings.cpp b/src/esengine/bindings/MaterialBindings.cpp
index 85d03eda9..77b231e33 100644
--- a/src/esengine/bindings/MaterialBindings.cpp
+++ b/src/esengine/bindings/MaterialBindings.cpp
@@ -98,18 +98,10 @@ u32 material_compileEsshader(const std::string& source, const std::string& featu
// Kept so the same material can be compiled for another vertex source
// (GPU-resident geometry) without the author's file being read again.
rc->materials().rememberSource(handle, source, featuresCsv);
- // Point each texture param's sampler at its unit, once per program (GLSL ES 300 has
- // no layout(binding=); mirrors the batch path's u_textures setup in RenderFrame).
- // Sampler seeding is a GLSL concept; on WGSL the unit rides the bind group.
- if (s->language() == GfxShaderLanguage::GLSL_ES300) {
- s->bind();
- for (const auto& p : parsed.properties) {
- if (p.fromParam && p.type == resource::ShaderPropertyType::Texture && p.textureUnit >= 0) {
- s->setUniform(p.name, static_cast<i32>(p.textureUnit));
- }
- }
- s->unbind();
- }
+ // Through the store's own seeding, not a copy of it: a device
+ // recovery has to redo exactly this, and two spellings of "which
+ // unit does this sampler read" is how one of them stops being done.
+ MaterialStore::seedSamplers(*s, parsed);
}
}
return handle.id();
diff --git a/src/esengine/renderer/store/MaterialStore.cpp b/src/esengine/renderer/store/MaterialStore.cpp
index 43e321fc8..d263b9679 100644
--- a/src/esengine/renderer/store/MaterialStore.cpp
+++ b/src/esengine/renderer/store/MaterialStore.cpp
@@ -119,17 +119,7 @@ u32 MaterialStore::variantProgram(u32 materialId, resource::ResourceManager& res
mesh_programs_[key] = 0;
return 0;
}
- // Same sampler seeding the batch variant gets: GLSL ES 300 has no
- // layout(binding=), so each program points its texture params at their units.
- if (shader->language() == GfxShaderLanguage::GLSL_ES300) {
- shader->bind();
- for (const auto& p : parsed.properties) {
- if (p.fromParam && p.type == resource::ShaderPropertyType::Texture && p.textureUnit >= 0) {
- shader->setUniform(p.name, static_cast<i32>(p.textureUnit));
- }
- }
- shader->unbind();
- }
+ seedSamplers(*shader, parsed);
// No layout of its own: uniforms pack through the material's shaderRef, and
// this variant declares the same params, so its handle is never looked up.
const u32 program = shader->getProgramId();
@@ -137,12 +127,32 @@ u32 MaterialStore::variantProgram(u32 materialId, resource::ResourceManager& res
return program;
}
+void MaterialStore::seedSamplers(Shader& shader, const resource::ParsedShader& parsed) {
+ if (shader.language() != GfxShaderLanguage::GLSL_ES300) return;
+ shader.bind();
+ for (const auto& p : parsed.properties) {
+ if (p.fromParam && p.type == resource::ShaderPropertyType::Texture && p.textureUnit >= 0) {
+ shader.setUniform(p.name, static_cast<i32>(p.textureUnit));
+ }
+ }
+ shader.unbind();
+}
+
void MaterialStore::refreshShaderPrograms(resource::ResourceManager& resources) {
// The cached program id is the one thing a record holds that the device can
// invalidate; shaderRef is what makes recomputing it possible at all.
for (auto& [id, rec] : materials_) {
rec.shader = 0;
- if (Shader* shader = resources.getShader(rec.shaderRef)) rec.shader = shader->getProgramId();
+ Shader* shader = resources.getShader(rec.shaderRef);
+ if (!shader) continue;
+ rec.shader = shader->getProgramId();
+ // And the sampler units, which the relink behind that id cleared. Reading
+ // the id back is half of restoring a program: the other half is the
+ // uniform state that says which unit each texture param reads.
+ auto src = sources_.find(rec.shaderRef.id());
+ if (src != sources_.end()) {
+ seedSamplers(*shader, resource::ShaderParser::parse(src->second.source));
+ }
}
}
diff --git a/src/esengine/renderer/store/MaterialStore.hpp b/src/esengine/renderer/store/MaterialStore.hpp
index fd0d74bac..981644d1d 100644
--- a/src/esengine/renderer/store/MaterialStore.hpp
+++ b/src/esengine/renderer/store/MaterialStore.hpp
@@ -34,6 +34,7 @@
#include <vector>
namespace esengine {
+namespace resource { struct ParsedShader; }
namespace resource { class ResourceManager; }
@@ -160,6 +161,16 @@ public:
/// the device rebuilt the programs behind them.
void refreshShaderPrograms(resource::ResourceManager& resources);
+ /**
+ * @brief Point each of @p shader's texture params at its unit.
+ *
+ * @details GLSL ES 300 has no `layout(binding=)`, so a program carries this
+ * as uniform state — which a relink clears. Public and shared so
+ * registration and device recovery seed by one rule; they did not,
+ * and a recovered material sampled every texture from unit 0.
+ */
+ static void seedSamplers(Shader& shader, const resource::ParsedShader& parsed);
+
/// Keeps a material shader's SOURCE beside its handle, so the same material can
/// be compiled for another vertex source later. Without it the store holds only
/// the assembled program and a variant would need the author's file again.
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index 6cf0df52b..f3f4b3739 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -535,6 +535,10 @@ export const SCENES = [
// Takes the GPU away for real and captures AFTER the cycle: the four points
// sample the atlas, so they fail unless the content came back. A vertex-colour
// scene passed with the re-upload deleted. No webgpu — no GL extension there.
+ // A MATERIAL across a recovery. The two below are tilemaps, which read no
+ // material sampler — so a recovered material sampling every texture from unit
+ // 0 looked exactly like one with no normal map, and nothing could tell.
+ { id: "device-loss-material", tier: "pr", env: { ESTELLA_VERIFY_DEVICE_LOSS: "1", ESTELLA_VERIFY_SCENE: "/scenes/mat-lit-normal.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mat-lit-normal.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.333,\"y\":0.5,\"rgb\":[0,252,0],\"tol\":45},{\"x\":0.667,\"y\":0.5,\"rgb\":[0,36,0],\"tol\":50}]" } },
{ id: "device-loss", tier: "pr", env: { ESTELLA_VERIFY_DEVICE_LOSS: "1", ESTELLA_VERIFY_SCENE: "/scenes/tilemap-flip.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/tilemap-flip.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.40,\"y\":0.36,\"rgb\":[255,0,0]},{\"x\":0.60,\"y\":0.36,\"rgb\":[0,255,0]},{\"x\":0.40,\"y\":0.58,\"rgb\":[0,0,255]},{\"x\":0.60,\"y\":0.58,\"rgb\":[255,255,0]}]" } },
// The player's case: nothing asks for the recovery, only frames pass, and it
// happens FOUR times — backgrounding a tab is not a one-off, a recovery that