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

Fix generic type deduce bug of ShaderLab compiler in verbose mode #2477

Open
wants to merge 3 commits into
base: dev/1.4
Choose a base branch
from

Conversation

Sway007
Copy link
Member

@Sway007 Sway007 commented Dec 25, 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, ...)

Fix generic type deduce bug in ShaderLab compiler of verbose mode.

vec3 baseF0;

// In the snippets below, the compiler incorrectly inferred the signature 
// of the `clamp` function as `float clamp(float, float, float)`, 
// the correct signature should be `vec3 clamp(vec3, float, float)`.
fresnelToIOR(clamp(baseF0, 0.0, 0.9999))

image

Summary by CodeRabbit

  • New Features

    • Enhanced handling of built-in functions in shader parsing.
    • Introduced macro definitions for shader validation in tests.
  • Bug Fixes

    • Improved error logging for shader validation.
  • Tests

    • Added new test cases to validate shader macros and includes.
  • Chores

    • Updated dependencies for testing framework compatibility.

Copy link

coderabbitai bot commented Dec 25, 2024

Walkthrough

The pull request introduces modifications to the shader parsing and semantic analysis process, focusing on improvements in handling built-in functions. The changes primarily affect the FunctionCallGeneric class and BuiltinFunction class, streamlining how function calls are processed. A new signatures property and realReturnType getter are added to enhance type management. The modifications also include updates to texture function signatures and improvements in error logging for shader validation.

Changes

File Change Summary
packages/shader-lab/src/parser/AST.ts - Updated FunctionCallGeneric method to directly pass paramSig
- Modified return type assignment for built-in functions
packages/shader-lab/src/parser/builtin/functions.ts - Added signatures property to BuiltinFunction
- Introduced realReturnType getter
- Refactored getFn method
- Updated texture2D function signatures
tests/package.json - Added @galacean/engine-shader-shaderlab dependency
tests/src/shader-lab/ShaderLab.test.ts - Added import for registerIncludes
- Introduced commonMacros constant
tests/src/shader-lab/ShaderValidate.ts - Enhanced error logging for shader parsing errors
tests/vitest.config.ts - Removed headless: true from browser configuration

Possibly related PRs

Suggested Labels

bug, shader, high priority

Suggested Reviewers

  • zhuxudong
  • GuoLei1990

Poem

🐰 In shader's realm, where types dance free,
Functions call with new clarity
Signatures bloom, errors take flight
A rabbit's code, now shining bright!
Parsing magic, type by type 🌟

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.

@Sway007 Sway007 self-assigned this Dec 25, 2024
@Sway007 Sway007 added bug Something isn't working Rendering Rendering related functions shader Shader related functions medium priority Medium priority issue labels Dec 25, 2024
Copy link

codecov bot commented Dec 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.94%. Comparing base (90c3228) to head (15eb742).

Additional details and impacted files
@@             Coverage Diff             @@
##           dev/1.4    #2477      +/-   ##
===========================================
+ Coverage    68.43%   68.94%   +0.50%     
===========================================
  Files          922      922              
  Lines        95661    95670       +9     
  Branches      8121     8134      +13     
===========================================
+ Hits         65466    65959     +493     
+ Misses       29942    29458     -484     
  Partials       253      253              
Flag Coverage Δ
unittests 68.94% <100.00%> (+0.50%) ⬆️

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.

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: 4

🧹 Nitpick comments (3)
packages/shader-lab/src/parser/builtin/functions.ts (1)

35-36: Consider clarifying the purpose of signatures.

It’s not immediately clear that this array is being used to store both the return type and parameter types. A more descriptive name (e.g., resolvedTypes) or explanatory comment could improve readability.

tests/src/shader-lab/ShaderLab.test.ts (1)

21-42: Consider isolating or documenting common macros.

Defining the macros inline with the test is convenient, but future expansions might complicate the file. Consider extracting them into a separate constants file or adding doc comments.

tests/src/shader-lab/ShaderValidate.ts (1)

93-97: Consider adding specific test cases for generic type deduction.

Given that this PR fixes a bug with generic type deduction for the clamp function, it would be valuable to add specific test cases that verify:

  1. Correct type deduction for vec3 clamp(vec3, float, float)
  2. Error cases for incorrect type usage

Would you like me to help create test cases that specifically validate the generic type deduction fix for the clamp function?

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 90c3228 and 15eb742.

⛔ Files ignored due to path filters (2)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
  • tests/src/shader-lab/shaders/builtin-function.shader is excluded by !**/*.shader
📒 Files selected for processing (6)
  • packages/shader-lab/src/parser/AST.ts (1 hunks)
  • packages/shader-lab/src/parser/builtin/functions.ts (3 hunks)
  • tests/package.json (1 hunks)
  • tests/src/shader-lab/ShaderLab.test.ts (3 hunks)
  • tests/src/shader-lab/ShaderValidate.ts (1 hunks)
  • tests/vitest.config.ts (1 hunks)
🔇 Additional comments (9)
packages/shader-lab/src/parser/builtin/functions.ts (3)

67-67: Refine function signature for clarity.

The updated getFn method uses clearer parameter naming (parameterTypes). This improves maintainability by making the signature more explicit.


89-97: Validate type matching logic fully.

Proceeding with found = true if each compared type matches or is generic is correct. However, for more complex scenarios (e.g., multiple generics in a single built-in), you may need additional checks. If you anticipate more generics, consider systematically storing type mappings.


261-262: Confirm return type for texture2D.

Changing the return from SAMPLER2D to VEC4 aligns with GLSL usage (sampler2D typically returns vec4). Ensure that no other parts of the code still expect the deprecated return type.

tests/vitest.config.ts (1)

22-22: Ensure that running in non-headless mode meets CI requirements.

Removing the headless: true option can be beneficial for interactive debugging, but might break or slow automated CI pipelines. Verify that CI supports the new setting.

tests/src/shader-lab/ShaderLab.test.ts (2)

12-12: New import for registerIncludes.

This import is necessary for the new macros and include registration. Ensure that the underlying library is installed and well-documented in the test setup instructions.


150-156: Test coverage for built-in functions.

Introducing the "builtin-function" test is a good step. Confirm coverage includes scenarios where clamp with vector parameters is used, matching the PR’s objective about correct type deduction.

Would you like an additional test focusing specifically on clamp(vec3, float, float) usage?

packages/shader-lab/src/parser/AST.ts (2)

704-704: Retrieve built-in function carefully.

getFn(fnIdent, paramSig) may return undefined; ensure you handle that scenario before proceeding. Otherwise, an error might occur on the following lines.


706-706: Safely use builtinFn.realReturnType.

After verifying builtinFn is not null, using builtinFn.realReturnType is appropriate. Good job aligning this AST logic with the newly introduced property in BuiltinFunction.

tests/package.json (1)

26-27: Added @galacean/engine-shader-shaderlab dependency.

This is essential for the new shader-lab tests and macros. Verify that the version is pinned correctly to avoid mismatches during local or CI builds.

@Sway007 Sway007 changed the title Fix generic type deduce bug in ShaderLab compiler of verbose mode Fix generic type deduce bug of ShaderLab compiler in verbose mode Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working medium priority Medium priority issue Rendering Rendering related functions shader Shader related functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant