Skip to content

efactor(shader): migrate GLSL shaders to ShaderLab and clean up shader infrastructure #2961

Draft
zhuxudong wants to merge 11 commits intogalacean:dev/2.0from
zhuxudong:refactor/glsl-to-shaderlab
Draft

efactor(shader): migrate GLSL shaders to ShaderLab and clean up shader infrastructure #2961
zhuxudong wants to merge 11 commits intogalacean:dev/2.0from
zhuxudong:refactor/glsl-to-shaderlab

Conversation

@zhuxudong
Copy link
Copy Markdown
Member

Summary

  • Migrate all built-in shaders from core/shaderlib (raw GLSL + rollup-plugin-glsl) to shader package as ShaderLab
    .shader files
  • Reorganize shader package into Shaders/ (ShaderLab entry points) and ShaderLibrary/ (GLSL include fragments)
  • Unify ShadowCaster/DepthOnly as shared Utility passes; PBR/BlinnPhong/Unlit/PBRSpecular use UsePass to reference them
  • Remove ShaderChunkLoader and shader path/chunk loading infrastructure (_shaderRootPath, basePathForIncludeKey)
  • Fix _resolveUsePass to support shader names containing "/" (e.g. Utility/ShadowMap)
  • Restore original FXAA3_11.glsl without logic modifications

Motivation

Raw GLSL shaders scattered across core/shaderlib relied on rollup-plugin-glsl for bundling and lacked the render
state / pass structure that ShaderLab provides. This migration:

  1. Consolidates all shader source into the shader package with a clear Shaders + ShaderLibrary layout
  2. Enables ShaderLab features (render state declarations, UsePass, Editor properties) for built-in shaders
  3. Removes the legacy shader chunk loading path — the engine now exclusively uses registerIncludes() for include
    registration and ShaderLab for shader creation

Key Changes

Shader Migration (packages/shader/)

  • Shaders/: PBR, PBRSpecular, BlinnPhong, Unlit, Sprite, SpriteMask, Text, Trail, UIDefault, Skybox, SkyProcedural,
    BackgroundTexture, Particle, Blit, BlitScreen, ShadowMap, DepthOnly, PostProcess shaders (Bloom, SAO, FinalSRGB,
    FinalAntiAliasing, UberShader)
  • ShaderLibrary/: All .glsl include fragments organized by category (Common, Lighting, PBR, Skin, Shadow, Fog,
    PostProcess, Particle)

UsePass Architecture

  • Utility/ShadowMap and Utility/DepthOnly are canonical shared passes
  • Material shaders reference them via UsePass "Utility/ShadowMap/Default/ShadowCaster"
  • Registration order in registerShaders(): Utility first, then material shaders

Infrastructure Cleanup

  • Delete ShaderChunkLoader, remove _shaderRootPath, basePathForIncludeKey from Preprocessor/ShaderLab/IShaderLab
  • Shader.create() only accepts ShaderLab source (vertex/fragment string overload removed from implementation, kept as
    overload signature for now)
  • _resolveUsePass parses from end to handle multi-segment shader names

Known Issue

  • Precompile.test.ts suite fails at load time because FinalAntiAliasing.shader includes FXAA3_11.glsl which uses
    conditional #define type aliases (FxaaFloat, FxaaBool) — the Preprocessor collects these as macros causing parse
    failures. To be fixed on fix/shaderlab branch.

Test Plan

  • npm run build passes
  • npx vitest run tests/src/shader-lab/ — 62/62 tests pass
  • ShaderLab parsing: PBR UsePass resolves correctly, render states preserved
  • Precompile benchmark: _createFromPrecompiled round-trip works for PBR
  • Verify no runtime regression with built-in materials (PBR, Unlit, BlinnPhong, etc.)

… and clean up old files

All GLSL chunks and complete shaders have been migrated from
packages/core/src/shaderlib/ to packages/shader/src/shaders/ with a
unified directory structure. ShaderLib.ts is now an empty runtime
registry populated by the shader package's registerIncludes(). Old
VS/FS shader pairs in extra/ are replaced by ShaderLab .shader files.
Post-process and AO GLSL includes updated to use new .glsl-suffixed
keys. Blit.vs.glsl relocated from extra/ to shaderlib root.
… assertions

Move camera_ProjectionParams declaration from Transform.glsl to
Common.glsl where it is actually used by remapDepthBufferEyeDepth().
Update ShaderLab test to match new PBR.shader structure with inlined
ShadowCaster/DepthOnly passes. Fix PrecompileABTest macro expansion
test to find Forward Pass by name instead of hardcoded index.
…te PostProcess/AO

Split shader package into two layers, create .shader files for
PostProcess and AO, simplify FXAA3_11.glsl for ShaderLab
compatibility, remove Shader.create() from core passes.
…ng with pre-migration source

FXAA3_11.glsl was incorrectly stripped of conditional branches
(FXAA_DISCARD, FXAA_FAST_PIXEL_OFFSET, FXAA_GATHER4_ALPHA) and type
alias macros during the initial migration. Restore the full original
1028-line version to preserve all code paths.

FinalAntiAliasing.glsl now includes the FXAA_GLSL_130/120 conditional
defines and uses FxaaFloat type aliases, matching the pre-migration
FinalAntiAliasing.fs.glsl source exactly.
…ly across all material shaders

PBR/BlinnPhong/Unlit/PBRSpecular now reference Utility/ShadowMap and
Utility/DepthOnly via UsePass instead of inlining or referencing PBR.
Fix _resolveUsePass to handle shader names containing "/" by parsing
from the end. Reorder registerShaders() so Utility shaders are created
before material shaders.
- Remove `path` parameter from `Shader.create()` ShaderLab overload
- Remove `_shaderRootPath` from ShaderPass
- Remove `basePathForIncludeKey` from IShaderLab, ShaderLab, and Preprocessor
- Remove relative path resolution in Preprocessor._replace()
- Delete ShaderChunkLoader and simplify ShaderLoader
- Clean up basePath references in tests and devtools example
@zhuxudong zhuxudong self-assigned this Apr 13, 2026
@zhuxudong zhuxudong added the shader Shader related functions label Apr 13, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 13, 2026

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

Run ID: 5c509f5a-4189-40bc-bd8f-63b5fbd9f7ad

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
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

…alacean#2960

- Delete unused noise files (NoiseCellular, NoisePerlin, NoisePsrd, NoiseSimplex)
- Add NoiseSimplexGrad.glsl with simplexGrad() returning vec3 gradient
- Add algorithm attribution comments to NoiseCommon.glsl
- Add Particle/Module/NoiseModule.glsl with curl noise sampling
- Integrate noise velocity into ParticleFeedback.glsl
- Update Shaders/index.ts registrations for new includes
- Remove _createShader wrapper that handled ShaderPool name conflicts
- ShaderPool no longer registers VS/FS fallbacks, so no conflict exists
- Fix Shaders/index.ts noise registrations reverted by case issue
…m core

- Strip shader package to zero dependencies (pure data exports only)
- Move include registration to ShaderPool.init() and shader registration
  to ShaderPool.registerShaders() in core
- Split exports: ShaderLibrary/index.ts for fragmentList,
  Shaders/index.ts for complete shader sources
- Auto-register built-in shaders in Engine._initialize() after ShaderLab
  is set, so external callers no longer need manual register calls
- Remove registerIncludes/registerShaders from all tests, examples, e2e
- Update docs to reflect automatic registration
… DepthOnly passes

- ShadowMap.shader: bind RenderQueueType = material_ShadowCasterRenderQueue
- DepthOnly.shader: bind RenderQueueType = material_DepthOnlyRenderQueue
- Remove duplicate lowercase shaders/utility/ entries from git index
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation shader Shader related functions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant