Skip to content

[codex] reuse registered shaders across engines - #3087

Closed
cptbtptpbcptdtptp wants to merge 2 commits into
galacean:dev/2.0from
cptbtptpbcptdtptp:codex/reuse-loaded-shaders-2.0
Closed

[codex] reuse registered shaders across engines#3087
cptbtptpbcptdtptp wants to merge 2 commits into
galacean:dev/2.0from
cptbtptpbcptdtptp:codex/reuse-loaded-shaders-2.0

Conversation

@cptbtptpbcptdtptp

@cptbtptpbcptdtptp cptbtptpbcptdtptp commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • reuse globally registered Shader descriptors only when a new Engine ResourceManager reloads the same URL and exact ShaderLab source or precompiled .shaderc content
  • reject same-name shaders when their recorded URL or source content differs, or when the existing shader has no loader provenance
  • make duplicate Shader.create and _createFromPrecompiled calls return the registered Shader instead of undefined
  • add a browser regression test that compiles a Shader, destroys the first Engine, recreates an Engine on the same canvas, reloads the assets, and compiles again

Problem

Shader descriptors live in a global name registry, while every Engine owns an independent ResourceManager cache. After an Engine is destroyed, a new Engine loading the same shader asset reaches Shader.create again. The duplicate-name branch previously returned undefined; ResourceManager then tried to cache that value and accessed instanceId, turning an expected descriptor reuse into a loading failure.

GPU Shader Programs are still scoped to their Engine/WebGL context. Engine.destroy() clears the destroyed Engine's program maps, and the reused Shader descriptor creates a new program when compiled by the next Engine.

The loader records the original URL and source string in a WeakMap keyed by Shader. This keeps exact comparison off the render loop and preserves the previous duplicate-name conflict behavior for different projects that happen to use the same shader name.

Verification

  • pnpm run b:module
  • pnpm run b:types
  • pnpm exec vitest run tests/src/loader/ShaderLoader.test.ts tests/src/core/Shader.test.ts tests/src/core/resource/ResourceManager.test.ts --browser.headless (30 tests passed)
  • repository pre-commit ESLint and Prettier hooks

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 29c1116d-1d61-4c52-b06c-db347aec9423

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant