Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate shader-shaderlab from engine-toolkit #2467

Merged

Conversation

zhuxudong
Copy link
Member

@zhuxudong zhuxudong commented Dec 17, 2024

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

What is the current behavior? (You can also link to an open issue here)

What is the new behavior (if this is a feature change)?

Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)

Other information:

Summary by CodeRabbit

  • New Features

    • Introduced a new package @galacean/engine-shader-shaderlab with version 1.4.0-alpha.0.
    • Added TypeScript declaration support for .glsl and .gs files.
    • Implemented shader registration functionality within the graphics engine context.
    • Added a physically-based rendering (PBR) shader definition with various material parameters.
  • Documentation

    • Updated README.md to indicate the package's subpackage status.
  • Chores

    • Added TypeScript configuration file for improved compilation settings.
    • Updated build configuration to handle additional file types and improve output clarity.

@zhuxudong zhuxudong added the Rendering Rendering related functions label Dec 17, 2024
@zhuxudong zhuxudong added this to the 1.4 milestone Dec 17, 2024
@zhuxudong zhuxudong self-assigned this Dec 17, 2024
Copy link

coderabbitai bot commented Dec 17, 2024

Walkthrough

This pull request introduces a new shader-shaderlab package for the Galacean engine, focusing on physically-based rendering (PBR) shader management. The package includes comprehensive shader configurations, TypeScript declarations for GLSL and GS files, and a structured approach to registering and managing shader includes and shader components. The implementation provides a flexible system for defining material properties, textures, and rendering passes with support for advanced rendering techniques.

Changes

File Change Summary
README.md Added line indicating subpackage of @galacean/engine
package.json New package configuration for @galacean/engine-shader-shaderlab with version 1.4.0-alpha.0
src/global.d.ts Added TypeScript module declarations for .glsl and .gs file types
src/index.ts Implemented shader registration functions and exports
src/shaders/PBR.gs Introduced comprehensive PBR shader with extensive material properties
src/shaders/index.ts Created shader fragment management system
src/shaders/shadingPBR/index.ts Exported array of PBR shader modules
tsconfig.json Added TypeScript compilation configuration
rollup.config.js Updated plugin configurations and build process

Sequence Diagram

sequenceDiagram
    participant Index as Shader Index
    participant Factory as ShaderFactory
    participant Includes as Shader Includes
    participant Shader as PBR Shader

    Index->>Factory: registerIncludes()
    Factory-->>Includes: Register fragment includes
    
    Index->>Shader: registerShader()
    Shader-->>Factory: Create and register PBR shader
Loading

Possibly related PRs

Suggested labels

enhancement, shader, glTF, material

Suggested reviewers

  • GuoLei1990
  • Sway007

Poem

🐰 Shader magic, lines of light,
PBR dancing, pixel's delight!
Includes registered, fragments flow,
Galacean's engine starts to glow
Rendering dreams in code's embrace 🌟

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
packages/shader-shaderlab/src/shaders/shadingPBR/index.ts (2)

1-10: Add documentation for shader module relationships.

Consider adding JSDoc comments to document the relationships and dependencies between these shader modules, making it easier for developers to understand the PBR pipeline structure.


12-23: Consider organizing shader modules by pipeline stage.

The shader modules could be organized based on their role in the rendering pipeline (vertex processing, fragment processing, lighting calculations, etc.) to improve maintainability.

Example organization:

 export default [
+  // Vertex Stage
   { source: AttributesPBR, includeKey: "AttributesPBR.glsl" },
   { source: VaryingsPBR, includeKey: "VaryingsPBR.glsl" },
   { source: VertexPBR, includeKey: "VertexPBR.glsl" },
+  // Fragment Stage
   { source: FragmentPBR, includeKey: "FragmentPBR.glsl" },
+  // Lighting
   { source: LightDirectPBR, includeKey: "LightDirectPBR.glsl" },
   { source: LightIndirectPBR, includeKey: "LightIndirectPBR.glsl" },
   { source: LightIndirectFunctions, includeKey: "LightIndirectFunctions.glsl" },
+  // BRDF & Reflection
   { source: BRDF, includeKey: "BRDF.glsl" },
   { source: ReflectionLobe, includeKey: "ReflectionLobe.glsl" },
+  // Pipeline Configuration
   { source: ForwardPassPBR, includeKey: "ForwardPassPBR.glsl" },
 ];
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4167e83 and fc030ab.

⛔ Files ignored due to path filters (20)
  • packages/shader-shaderlab/src/shaders/BlendShape.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/Common.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/Fog.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/Light.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/Normal.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/Shadow.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/ShadowSampleTent.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/Skin.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/Transform.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/BRDF.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/LightIndirectFunctions.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/ReflectionLobe.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/VertexPBR.glsl is excluded by !**/*.glsl
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (9)
  • packages/shader-shaderlab/README.md (1 hunks)
  • packages/shader-shaderlab/package.json (1 hunks)
  • packages/shader-shaderlab/src/global.d.ts (1 hunks)
  • packages/shader-shaderlab/src/index.ts (1 hunks)
  • packages/shader-shaderlab/src/shaders/PBR.gs (1 hunks)
  • packages/shader-shaderlab/src/shaders/index.ts (1 hunks)
  • packages/shader-shaderlab/src/shaders/shadingPBR/index.ts (1 hunks)
  • packages/shader-shaderlab/tsconfig.json (1 hunks)
  • rollup.config.js (1 hunks)
✅ Files skipped from review due to trivial changes (3)
  • packages/shader-shaderlab/README.md
  • packages/shader-shaderlab/tsconfig.json
  • packages/shader-shaderlab/package.json
🔇 Additional comments (5)
packages/shader-shaderlab/src/shaders/PBR.gs (1)

31-33: Verify the type of material_OcclusionTextureCoord

The property material_OcclusionTextureCoord is declared as a Float, but texture coordinates are typically represented as Vector2 or Vector3. Please verify if a single float value is intended for this property.

Run the following script to check the usage of material_OcclusionTextureCoord in the codebase:

✅ Verification successful

Using Float for texture coordinate selection is correct

Based on the code usage, material_OcclusionTextureCoord is intentionally defined as a Float because it's used as a selector between different UV sets (UV0 vs UV1), not as actual texture coordinates. This is evident from its usage in the shader code:

if(material_OcclusionTextureCoord == 1.0){
    aoUV = varyings.uv1;  // or v_uv1 in other files
}

The property acts as a switch where:

  • 0 (default) uses the first UV set (UV0)
  • 1 uses the second UV set (UV1)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the type and usage of 'material_OcclusionTextureCoord'

# Search for the property usage. Expect: Consistent use as a Float.
rg -A 5 'material_OcclusionTextureCoord'

Length of output: 3487

packages/shader-shaderlab/src/global.d.ts (1)

1-9: LGTM!

The module declarations for .glsl and .gs files correctly enable TypeScript to recognize shader imports as strings.

packages/shader-shaderlab/src/shaders/index.ts (1)

1-31: LGTM!

Shader fragments are properly imported and assembled into fragmentList. The structure and export statements are correctly set up.

packages/shader-shaderlab/src/shaders/shadingPBR/index.ts (1)

1-23: Verify shader file existence and imports.

Let's verify that all referenced shader files exist in the correct locations.

✅ Verification successful

Let me generate a new script to verify the files using a different approach.

This will list all .glsl files in the directory, giving us a clearer picture of what files actually exist there.


All shader files are present and correctly located

All 10 shader files referenced in the imports exist in the correct location under packages/shader-shaderlab/src/shaders/shadingPBR/. The file structure matches exactly with the import statements and the exported array configuration.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if all shader files exist in the correct location

# Get the directory of the current file
cd packages/shader-shaderlab/src/shaders/shadingPBR

# Check each shader file
for shader in AttributesPBR.glsl BRDF.glsl ForwardPassPBR.glsl FragmentPBR.glsl \
             LightDirectPBR.glsl LightIndirectFunctions.glsl LightIndirectPBR.glsl \
             ReflectionLobe.glsl VaryingsPBR.glsl VertexPBR.glsl; do
    if [ ! -f "$shader" ]; then
        echo "Missing shader file: $shader"
    fi
done

Length of output: 2451


Script:

#!/bin/bash
# Let's use fd to find these files and check their existence
cd packages/shader-shaderlab/src/shaders/shadingPBR
fd -e glsl --type f --max-depth 1

Length of output: 277

rollup.config.js (1)

37-37: Document the purpose of .gs files and verify shader processing.

The inclusion of .gs files in the glslify plugin configuration needs documentation to explain their purpose in the shader system.

✅ Verification successful

Let me verify the content of the PBR.gs file to understand its purpose and relationship with other shader files.


Based on the analysis of the codebase, I can now provide a final response:

The .gs file extension is used for ShaderLab shader files in the PBR system

The .gs file (PBR.gs) is a ShaderLab shader file that defines the PBR (Physically Based Rendering) material properties and shader configuration. This is evidenced by:

  • The file contains ShaderLab syntax for defining material properties like metallic, roughness, clear coat, etc.
  • It's properly imported and used in the shader system alongside other .glsl files
  • The file is part of the shader-shaderlab package's PBR implementation
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify shader file types and their usage

# Search for .gs files and their imports
echo "Checking .gs file usage:"
fd -e gs

# Check if .gs files are properly imported in TypeScript files
echo -e "\nChecking .gs imports in TypeScript files:"
rg -t ts "import.*\.gs"

# Verify glslify is properly handling both file types
echo -e "\nChecking shader imports:"
rg "import.*\.(glsl|gs)"

Length of output: 13289


Script:

#!/bin/bash
# Check the content and context of PBR.gs file
echo "Content of PBR.gs:"
cat packages/shader-shaderlab/src/shaders/PBR.gs

# Check if there are any references to this file in documentation
echo -e "\nChecking documentation references:"
rg -g "*.md" -i "\.gs|shaderlab|pbr" 

Length of output: 28897

Comment on lines +51 to +55
ui_SheenIntensity("Intensity", Range(0, 1, 0.01)) = 0;
ui_SheenColor("Color", Color ) = (0, 0, 0, 0);
material_SheenRoughness("Roughness", Range(0, 1, 0.01)) = 0;
material_SheenTexture("ColorTexture", Texture2D);
material_SheenRoughnessTexture("RoughnessTexture", Texture2D);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Inconsistent naming convention in 'Sheen' properties

The properties in the "Sheen" section use the prefix ui_ instead of material_, which is inconsistent with the rest of the properties. For consistency and clarity, consider renaming ui_SheenIntensity and ui_SheenColor to material_SheenIntensity and material_SheenColor.

Apply this diff to fix the inconsistency:

-            ui_SheenIntensity("Intensity", Range(0, 1, 0.01)) = 0;
-            ui_SheenColor("Color", Color ) = (0, 0, 0, 0);
+            material_SheenIntensity("Intensity", Range(0, 1, 0.01)) = 0;
+            material_SheenColor("Color", Color ) = (0, 0, 0, 0);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
ui_SheenIntensity("Intensity", Range(0, 1, 0.01)) = 0;
ui_SheenColor("Color", Color ) = (0, 0, 0, 0);
material_SheenRoughness("Roughness", Range(0, 1, 0.01)) = 0;
material_SheenTexture("ColorTexture", Texture2D);
material_SheenRoughnessTexture("RoughnessTexture", Texture2D);
material_SheenIntensity("Intensity", Range(0, 1, 0.01)) = 0;
material_SheenColor("Color", Color ) = (0, 0, 0, 0);
material_SheenRoughness("Roughness", Range(0, 1, 0.01)) = 0;
material_SheenTexture("ColorTexture", Texture2D);
material_SheenRoughnessTexture("RoughnessTexture", Texture2D);

Comment on lines +17 to +23
export function registerShader() {
if (shaderRegistered) return;

Shader.create(PBRSource);

shaderRegistered = true;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure includes are registered before creating shaders

The function registerShader() calls Shader.create(PBRSource) without ensuring that registerIncludes() has been invoked. If registerShader() is called before registerIncludes(), it may lead to missing shader includes and runtime errors. To prevent this, consider invoking registerIncludes() at the beginning of registerShader().

Apply this diff to fix the issue:

export function registerShader() {
+  registerIncludes();
  if (shaderRegistered) return;

  Shader.create(PBRSource);

  shaderRegistered = true;
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function registerShader() {
if (shaderRegistered) return;
Shader.create(PBRSource);
shaderRegistered = true;
}
export function registerShader() {
registerIncludes();
if (shaderRegistered) return;
Shader.create(PBRSource);
shaderRegistered = true;
}

Copy link

codecov bot commented Dec 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.27%. Comparing base (1405f8b) to head (fc030ab).
Report is 2 commits behind head on dev/1.4.

Additional details and impacted files
@@             Coverage Diff             @@
##           dev/1.4    #2467      +/-   ##
===========================================
+ Coverage    67.91%   68.27%   +0.35%     
===========================================
  Files          908      911       +3     
  Lines        94505    94585      +80     
  Branches      7937     7937              
===========================================
+ Hits         64187    64574     +387     
+ Misses       30068    29760     -308     
- Partials       250      251       +1     
Flag Coverage Δ
unittests 68.27% <100.00%> (+0.35%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@GuoLei1990 GuoLei1990 added the ignore for release ignore for release label Dec 17, 2024
@GuoLei1990 GuoLei1990 merged commit 2ec06df into galacean:dev/1.4 Dec 17, 2024
9 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Dec 26, 2024
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ignore for release ignore for release Rendering Rendering related functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants