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.

refactor(material): a material names its shader by handle too

esengineb1ccb35acd1f
diff --git a/src/esengine/bindings/GeometryBindings.cpp b/src/esengine/bindings/GeometryBindings.cpp
index d3851cdfa..af9bfc162 100644
--- a/src/esengine/bindings/GeometryBindings.cpp
+++ b/src/esengine/bindings/GeometryBindings.cpp
@@ -186,7 +186,7 @@ bool draw_meshWithMaterial(u32 geometryHandle, u32 materialId) {
// #pragma-param layout (compileEsshader). A raw-GLSL createShader program has no
// layout — return false so the SDK falls back to the legacy uniform-stream path.
const MaterialRecord* rec = rc->materials().find(materialId);
- if (!rec || !rc->materials().layoutFor(rec->shader)) return false;
+ if (!rec || !rc->materials().layoutFor(rec->shaderRef)) return false;
auto* geom = g_geometryManager->get(geometryHandle);
if (!geom || !geom->isValid()) return true; // routed modern; nothing to draw
diff --git a/src/esengine/bindings/MaterialBindings.cpp b/src/esengine/bindings/MaterialBindings.cpp
index 7cdafeb55..685a7477d 100644
--- a/src/esengine/bindings/MaterialBindings.cpp
+++ b/src/esengine/bindings/MaterialBindings.cpp
@@ -94,7 +94,7 @@ u32 material_compileEsshader(const std::string& source, const std::string& featu
if (!handle.isValid()) return 0;
if (auto* rc = g_renderContext) {
if (Shader* s = rm->getShader(handle)) {
- rc->materials().registerLayout(s->getProgramId(), buildMaterialLayout(parsed, *rc));
+ rc->materials().registerLayout(handle, buildMaterialLayout(parsed, *rc));
// 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.
@@ -115,16 +115,13 @@ u32 material_compileEsshader(const std::string& source, const std::string& featu
void material_define(u32 materialId, u32 shaderHandle, u32 blendMode, u32 flags) {
auto* rc = g_renderContext;
if (!rc) return;
- u32 programId = 0;
- if (shaderHandle != 0) {
- if (auto* rm = g_resourceManager) {
- if (Shader* s = rm->getShader(resource::ShaderHandle(shaderHandle))) {
- programId = s->getProgramId();
- }
- }
- }
+ // The handle is the identity; the program id is a cache of what it currently
+ // resolves to, refreshed by refreshShaderPrograms when the device rebuilds it.
MaterialRecord rec;
- rec.shader = programId;
+ rec.shaderRef = resource::ShaderHandle(shaderHandle);
+ if (auto* rm = g_resourceManager) {
+ if (Shader* s = rm->getShader(rec.shaderRef)) rec.shader = s->getProgramId();
+ }
rec.blend = static_cast<BlendMode>(blendMode);
rec.depthTest = (flags & 0x1u) != 0;
rec.depthWrite = (flags & 0x2u) != 0;
diff --git a/src/esengine/renderer/frame/PostProcessPipeline.cpp b/src/esengine/renderer/frame/PostProcessPipeline.cpp
index d9126e20f..8790002d2 100644
--- a/src/esengine/renderer/frame/PostProcessPipeline.cpp
+++ b/src/esengine/renderer/frame/PostProcessPipeline.cpp
@@ -420,7 +420,7 @@ void PostProcessPipeline::renderPass(PostProcessPass& pass, TextureHandle inputT
// (binding 1) exactly like a material's — compileEsshader registered the
// layout when the effect compiled. Resolution-style inputs come from the
// injected u_viewport, so nothing per-pass remains loose.
- const MaterialUniformLayout* layout = context_.materials().layoutFor(shader->getProgramId());
+ const MaterialUniformLayout* layout = context_.materials().layoutFor(pass.shader);
if (layout) {
if (layout->blockSize > 0) {
if (pass.paramDirty || pass.paramBytes.size() < layout->blockSize) {
diff --git a/src/esengine/renderer/store/MaterialStore.cpp b/src/esengine/renderer/store/MaterialStore.cpp
index 2a4f5226b..85e03f517 100644
--- a/src/esengine/renderer/store/MaterialStore.cpp
+++ b/src/esengine/renderer/store/MaterialStore.cpp
@@ -39,9 +39,15 @@ void MaterialStore::recreateGpuResources() {
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::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();
+ }
}
void MaterialStore::bindForDraw(u32 materialId) {
@@ -50,7 +56,7 @@ void MaterialStore::bindForDraw(u32 materialId) {
if (it == materials_.end()) return;
MaterialRecord& rec = it->second;
- auto lit = layouts_.find(rec.shader);
+ auto lit = layouts_.find(rec.shaderRef.id());
if (lit != layouts_.end()) ensureDefaults(rec, lit->second);
// Per-material constants UBO (binding 1) — present only when the shader declares params.
diff --git a/src/esengine/renderer/store/MaterialStore.hpp b/src/esengine/renderer/store/MaterialStore.hpp
index 67e5bd5c1..584c4663e 100644
--- a/src/esengine/renderer/store/MaterialStore.hpp
+++ b/src/esengine/renderer/store/MaterialStore.hpp
@@ -75,7 +75,7 @@ struct MaterialTextureSlot {
MaterialDefaultTexture defaultTexture = MaterialDefaultTexture::White;
};
-/** @brief A material's bound texture: the GL texture id at a sampler unit. */
+/** @brief A material's bound texture at a sampler unit. */
struct MaterialTextureBinding {
u32 unit = 0;
/// The resource handle, NOT a resolved GPU id. Resolving at bind time is what
@@ -108,7 +108,11 @@ struct MaterialUniformLayout {
* @brief A material's resolved render state (P0) plus its packed std140 constants (P1).
*/
struct MaterialRecord {
- u32 shader = 0; ///< Shader program; 0 means "use the path's default batch shader".
+ /// The identity: what the layout is filed under, and what survives the
+ /// program being rebuilt. `shader` below caches what it currently resolves
+ /// to, because the collect path reads that per entity.
+ resource::ShaderHandle shaderRef;
+ u32 shader = 0; ///< Program id; 0 means "use the path's default batch shader".
BlendMode blend = BlendMode::Normal;
bool depthTest = false;
bool depthWrite = true;
@@ -120,7 +124,7 @@ struct MaterialRecord {
BufferHandle ubo = BufferHandle::Invalid;
bool uboDirty = false;
- /// Texture params bound to sampler units (GL texture ids, resolved at set time).
+ /// Texture params bound to sampler units, by handle (see MaterialTextureBinding).
std::vector<MaterialTextureBinding> textures;
};
@@ -152,10 +156,17 @@ public:
/// scene's materialIds stay meaningful. See the definition.
void recreateGpuResources();
+ /// Re-resolves each material's cached program id from its shader handle, after
+ /// the device rebuilt the programs behind them.
+ void refreshShaderPrograms(resource::ResourceManager& resources);
+
/// 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) {
- if (shaderId != 0) layouts_[shaderId] = std::move(layout);
+ /// Filed under the HANDLE, not the program id: rebuilding the program (a
+ /// device loss, a shader reload) changes the id, and a layout filed under the
+ /// old one would be unreachable from the material that still names it.
+ void registerLayout(resource::ShaderHandle shader, MaterialUniformLayout layout) {
+ if (shader.isValid()) layouts_[shader.id()] = std::move(layout);
}
/// Pushes a material's resolved render state. Preserves any already-packed constants
@@ -169,7 +180,7 @@ public:
return;
}
MaterialRecord& rec = it->second;
- const bool shaderChanged = rec.shader != record.shader;
+ const bool shaderChanged = rec.shaderRef != record.shaderRef;
const BufferHandle ubo = rec.ubo;
std::vector<u8> bytes = std::move(rec.uboBytes);
std::vector<MaterialTextureBinding> texs = std::move(rec.textures);
@@ -191,7 +202,7 @@ public:
auto it = materials_.find(materialId);
if (it == materials_.end()) return;
MaterialRecord& rec = it->second;
- auto lit = layouts_.find(rec.shader);
+ auto lit = layouts_.find(rec.shaderRef.id());
if (lit == layouts_.end()) return;
const MaterialParamSlot* slot = lit->second.find(name);
if (!slot) return;
@@ -206,7 +217,7 @@ public:
void setTexture(u32 materialId, const std::string& name, resource::TextureHandle texture) {
auto it = materials_.find(materialId);
if (it == materials_.end()) return;
- auto lit = layouts_.find(it->second.shader);
+ auto lit = layouts_.find(it->second.shaderRef.id());
if (lit == layouts_.end()) return;
const MaterialTextureSlot* slot = lit->second.findTexture(name);
if (!slot) return;
@@ -228,8 +239,8 @@ public:
/// The registered #pragma-param layout for a shader program, or nullptr.
/// The post-process pipeline reads it to pack pass params through the same
/// reflected MaterialConstants block a material would use.
- const MaterialUniformLayout* layoutFor(u32 shaderId) const {
- auto it = layouts_.find(shaderId);
+ const MaterialUniformLayout* layoutFor(resource::ShaderHandle shader) const {
+ auto it = layouts_.find(shader.id());
return it != layouts_.end() ? &it->second : nullptr;
}