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

Remove gamma color space #2565

Merged
merged 4 commits into from
Feb 26, 2025
Merged

Conversation

GuoLei1990
Copy link
Member

@GuoLei1990 GuoLei1990 commented Feb 18, 2025

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

  • Refactor

    • Streamlined shader code to consistently apply gamma correction for a uniform visual output.
    • Removed conditional color space handling, simplifying the rendering pipeline.
  • Chore

    • Eliminated legacy color configuration options from the engine setup for a cleaner experience.
  • Tests

    • Updated tests to reflect the new, always-on gamma correction behavior.

Copy link

coderabbitai bot commented Feb 18, 2025

Walkthrough

The changes remove conditional shader logic for gamma color space conversion across multiple modules. Shader code now always applies gamma-to-linear and linear-to-gamma conversions, eliminating preprocessor checks. This update spans end-to-end tests, examples, lighting, particle systems, and shader uniforms. In addition, engine configuration has been simplified by removing the configurable color space property and associated enumeration from core engine modules.

Changes

Files Change Summary
e2e/case/..., examples/..., tests/src/core/Shader.test.ts Removed conditional preprocessor directives in fragment shaders; now gamma conversions are applied unconditionally.
packages/core/src/Engine.ts, packages/core/src/EngineSettings.ts, packages/core/src/enums/ColorSpace.ts, packages/core/src/index.ts Removed the colorSpace property and related configuration; deleted the ColorSpace enum and its export.
packages/core/src/lighting/DirectLight.ts, PointLight.ts, SpotLight.ts Eliminated conditional checks for color space in lighting logic; unconditionally applied gamma correction to light color values.
packages/core/src/particle/ParticleGenerator.ts, particle/modules/ColorOverLifetimeModule.ts, particle/modules/ParticleGradient.ts, shader/ShaderUniform.ts Removed color space conditionals in particle and shader uniform processing; conversion functions are now always invoked.

Sequence Diagram(s)

sequenceDiagram
    participant S as Shader
    participant G as Gamma Converter
    S->>G: Apply gammaToLinear(baseColor)
    G-->>S: Return linear baseColor
    S->>G: Apply linearToGamma(finalColor)
    G-->>S: Return gamma-corrected finalColor
Loading

Possibly related PRs

  • Merge main to dev/1.4 #2450: This PR also removes conditional checks for gamma corrections in shader code, aligning with the changes made here.

Suggested labels

bug, high priority

Poem

I'm just a bunny, hopping through code,
Lighting up shaders on a smooth, bright road.
No more ifs or conditions that hide,
Gamma conversions now shine far and wide.
With a twitch of my nose, I celebrate the glow—
Carrots and code, in an endless flow!


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0c1a793 and dbd9b12.

⛔ Files ignored due to path filters (1)
  • packages/shader-shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl is excluded by !**/*.glsl
📒 Files selected for processing (1)
  • tests/src/core/Light.test.ts (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: e2e (22.x)
🔇 Additional comments (3)
tests/src/core/Light.test.ts (3)

1-21: LGTM! Import statements are well-organized.

The imports are logically grouped and maintain the required functionality.


61-298: LGTM! Test coverage is comprehensive.

The test suite thoroughly validates light functionality including:

  • Multi-scene light behavior
  • Default values and property updates
  • Transform and matrix calculations
  • Shadow properties
  • Different light types (Direct, Point, Spot, Ambient)

40-40:

✅ Verification successful

Verify test coverage after removing colorSpace parameter.

The removal of the colorSpace parameter aligns with the PR objective to remove gamma color space. However, we should ensure that the tests still provide adequate coverage for color-related functionality.

Let's verify the test coverage for color-related functionality:

Also applies to: 277-277


🏁 Script executed:

#!/bin/bash
# Description: Check test coverage for color-related functionality

# Search for color-related test assertions
rg -A 2 'expect.*color' tests/

Length of output: 8070


Ensure comprehensive color functionality tests post colorSpace removal.

The removal of the colorSpace parameter in both engine initialization instances (lines 40 and 277 in tests/src/core/Light.test.ts) aligns with the PR’s objective to eliminate gamma color space handling. Verification using our test grep confirmed that multiple test files (e.g., in GLTFLoader, RenderTarget, TextRenderer, Light, ModelMesh, Material, ParticleGradient, SpriteRenderer, SphericalHarmonics, and Color modules) already assert correct color behavior. Please double-check that the indirect testing of engine-driven color outputs remains robust and that no test expectations were inadvertently relying on the removed parameter.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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.

@github-actions github-actions bot added the documentation Improvements or additions to documentation label Feb 18, 2025
@GuoLei1990 GuoLei1990 added the enhancement New feature or request label Feb 18, 2025
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: 0

🔭 Outside diff range comments (3)
packages/core/src/shader/ShaderUniform.ts (3)

41-60: ⚠️ Potential issue

Fix caching logic for gamma-corrected values.

While the gamma correction changes align with the PR objective, the caching logic is inconsistent. The cache stores the original values but compares them against gamma-corrected values, which could lead to unnecessary uploads.

Apply this diff to fix the caching logic:

   if ((<Color>value).r !== undefined) {
-    if (cacheValue.x !== (<Color>value).r || cacheValue.y !== (<Color>value).g) {
+    const r = Color.gammaToLinearSpace((<Color>value).r);
+    const g = Color.gammaToLinearSpace((<Color>value).g);
+    if (cacheValue.x !== r || cacheValue.y !== g) {
       this._gl.uniform2f(
         shaderUniform.location,
-        Color.gammaToLinearSpace((<Color>value).r),
-        Color.gammaToLinearSpace((<Color>value).g)
+        r,
+        g
       );
-      cacheValue.x = (<Color>value).r;
-      cacheValue.y = (<Color>value).g;
+      cacheValue.x = r;
+      cacheValue.y = g;
     }
   }

66-92: ⚠️ Potential issue

Fix caching logic for gamma-corrected values.

The caching logic is inconsistent. The cache stores the original values but compares them against gamma-corrected values.

Apply this diff to fix the caching logic:

   if ((<Color>value).r !== undefined) {
-    if (cacheValue.x !== (<Color>value).r || cacheValue.y !== (<Color>value).g || cacheValue.z !== (<Color>value).b) {
+    const r = Color.gammaToLinearSpace((<Color>value).r);
+    const g = Color.gammaToLinearSpace((<Color>value).g);
+    const b = Color.gammaToLinearSpace((<Color>value).b);
+    if (cacheValue.x !== r || cacheValue.y !== g || cacheValue.z !== b) {
       this._gl.uniform3f(
         shaderUniform.location,
-        Color.gammaToLinearSpace((<Color>value).r),
-        Color.gammaToLinearSpace((<Color>value).g),
-        Color.gammaToLinearSpace((<Color>value).b)
+        r,
+        g,
+        b
       );
-      cacheValue.x = (<Color>value).r;
-      cacheValue.y = (<Color>value).g;
-      cacheValue.z = (<Color>value).b;
+      cacheValue.x = r;
+      cacheValue.y = g;
+      cacheValue.z = b;
     }
   }

98-139: ⚠️ Potential issue

Fix caching logic for gamma-corrected values.

The caching logic is inconsistent. The cache stores the original values but compares them against gamma-corrected values.

Apply this diff to fix the caching logic:

   if ((<Color>value).r !== undefined) {
     if (
-      cacheValue.x !== (<Color>value).r ||
-      cacheValue.y !== (<Color>value).g ||
-      cacheValue.z !== (<Color>value).b ||
+      cacheValue.x !== Color.gammaToLinearSpace((<Color>value).r) ||
+      cacheValue.y !== Color.gammaToLinearSpace((<Color>value).g) ||
+      cacheValue.z !== Color.gammaToLinearSpace((<Color>value).b) ||
       cacheValue.w !== (<Color>value).a
     ) {
+      const r = Color.gammaToLinearSpace((<Color>value).r);
+      const g = Color.gammaToLinearSpace((<Color>value).g);
+      const b = Color.gammaToLinearSpace((<Color>value).b);
       this._gl.uniform4f(
         shaderUniform.location,
-        Color.gammaToLinearSpace((<Color>value).r),
-        Color.gammaToLinearSpace((<Color>value).g),
-        Color.gammaToLinearSpace((<Color>value).b),
+        r,
+        g,
+        b,
         (<Color>value).a
       );
-      cacheValue.x = (<Color>value).r;
-      cacheValue.y = (<Color>value).g;
-      cacheValue.z = (<Color>value).b;
+      cacheValue.x = r;
+      cacheValue.y = g;
+      cacheValue.z = b;
       cacheValue.w = (<Color>value).a;
     }
   }
🧹 Nitpick comments (2)
packages/core/src/EngineSettings.ts (1)

4-4: Consider using a type alias instead of an empty interface.

Since the interface is now empty after removing the colorSpace property, it would be more appropriate to use a type alias.

-export interface EngineSettings {}
+export type EngineSettings = {};
🧰 Tools
🪛 Biome (1.9.4)

[error] 4-4: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)

packages/core/src/particle/ParticleGenerator.ts (1)

734-734: Remove extra whitespace.

There is an extra whitespace line that should be removed.

    main.startColor.evaluate(undefined, main._startColorRand.random(), startColor);
-    
    startColor.toLinear(startColor);
🧰 Tools
🪛 ESLint

[error] 734-734: Delete ····

(prettier/prettier)

🪛 GitHub Check: lint

[failure] 734-734:
Delete ····

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between efbc8fd and 1f6c4f1.

⛔ Files ignored due to path filters (28)
  • packages/core/src/postProcess/shaders/Bloom/BloomBlurH.glsl is excluded by !**/*.glsl
  • packages/core/src/postProcess/shaders/Bloom/BloomBlurV.glsl is excluded by !**/*.glsl
  • packages/core/src/postProcess/shaders/Bloom/BloomPrefilter.glsl is excluded by !**/*.glsl
  • packages/core/src/postProcess/shaders/Bloom/BloomUpsample.glsl is excluded by !**/*.glsl
  • packages/core/src/postProcess/shaders/PostCommon.glsl is excluded by !**/*.glsl
  • packages/core/src/postProcess/shaders/UberPost.glsl is excluded by !**/*.glsl
  • packages/core/src/shaderlib/begin_mobile_frag.glsl is excluded by !**/*.glsl
  • packages/core/src/shaderlib/extra/SkyProcedural.fs.glsl is excluded by !**/*.glsl
  • packages/core/src/shaderlib/extra/SkyProcedural.vs.glsl is excluded by !**/*.glsl
  • packages/core/src/shaderlib/extra/blinn-phong.fs.glsl is excluded by !**/*.glsl
  • packages/core/src/shaderlib/extra/particle.fs.glsl is excluded by !**/*.glsl
  • packages/core/src/shaderlib/extra/pbr-specular.fs.glsl is excluded by !**/*.glsl
  • packages/core/src/shaderlib/extra/pbr.fs.glsl is excluded by !**/*.glsl
  • packages/core/src/shaderlib/extra/skybox.fs.glsl is excluded by !**/*.glsl
  • packages/core/src/shaderlib/extra/unlit.fs.glsl is excluded by !**/*.glsl
  • packages/core/src/shaderlib/pbr/btdf.glsl is excluded by !**/*.glsl
  • packages/core/src/shaderlib/pbr/ibl_frag_define.glsl is excluded by !**/*.glsl
  • packages/core/src/shaderlib/pbr/pbr_frag.glsl is excluded by !**/*.glsl
  • packages/core/src/shaderlib/pbr/pbr_helper.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/BTDF.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/LightIndirectFunctions.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl is excluded by !**/*.glsl
  • tests/src/shader-lab/shaders/compilation-error.shader is excluded by !**/*.shader
  • tests/src/shader-lab/shaders/demo.shader is excluded by !**/*.shader
  • tests/src/shader-lab/shaders/macro-pre.shader is excluded by !**/*.shader
  • tests/src/shader-lab/shaders/unlit.shader is excluded by !**/*.shader
📒 Files selected for processing (17)
  • e2e/case/camera-opaque-texture.ts (2 hunks)
  • e2e/case/shaderLab-mrt.ts (1 hunks)
  • examples/camera-depth-texture.ts (1 hunks)
  • examples/model-mesh.ts (1 hunks)
  • examples/transparent-shadow.ts (1 hunks)
  • packages/core/src/Engine.ts (0 hunks)
  • packages/core/src/EngineSettings.ts (1 hunks)
  • packages/core/src/enums/ColorSpace.ts (0 hunks)
  • packages/core/src/index.ts (0 hunks)
  • packages/core/src/lighting/DirectLight.ts (1 hunks)
  • packages/core/src/lighting/PointLight.ts (1 hunks)
  • packages/core/src/lighting/SpotLight.ts (1 hunks)
  • packages/core/src/particle/ParticleGenerator.ts (1 hunks)
  • packages/core/src/particle/modules/ColorOverLifetimeModule.ts (1 hunks)
  • packages/core/src/particle/modules/ParticleGradient.ts (2 hunks)
  • packages/core/src/shader/ShaderUniform.ts (3 hunks)
  • tests/src/core/Shader.test.ts (2 hunks)
💤 Files with no reviewable changes (3)
  • packages/core/src/enums/ColorSpace.ts
  • packages/core/src/index.ts
  • packages/core/src/Engine.ts
🧰 Additional context used
🪛 Biome (1.9.4)
packages/core/src/EngineSettings.ts

[error] 4-4: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)

🪛 ESLint
packages/core/src/particle/ParticleGenerator.ts

[error] 734-734: Delete ····

(prettier/prettier)

🪛 GitHub Check: lint
packages/core/src/particle/ParticleGenerator.ts

[failure] 734-734:
Delete ····

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: codecov
  • GitHub Check: e2e (22.x)
🔇 Additional comments (12)
packages/core/src/lighting/PointLight.ts (1)

51-53: LGTM! Consistent color space handling.

The changes ensure that light colors are always converted from gamma to linear space, aligning with the PR's objective of removing conditional gamma color space handling.

packages/core/src/lighting/DirectLight.ts (1)

60-62: LGTM! Consistent color space handling.

The changes ensure that light colors are always converted from gamma to linear space, aligning with the PR's objective of removing conditional gamma color space handling.

e2e/case/camera-opaque-texture.ts (1)

98-98: LGTM! Consistent color space handling in shaders.

The changes ensure consistent color space conversion by:

  1. Converting the base color from gamma to linear space
  2. Converting the final color back to gamma space before output

This aligns with the PR's objective of removing conditional gamma color space handling.

Also applies to: 108-108

e2e/case/shaderLab-mrt.ts (1)

65-65: LGTM! Consistent color space handling.

The unconditional gamma-to-linear conversion aligns with the PR's objective to standardize color space handling.

examples/camera-depth-texture.ts (1)

132-132: LGTM! Proper depth visualization.

The unconditional linear-to-gamma conversion ensures correct display of depth values.

packages/core/src/particle/modules/ColorOverLifetimeModule.ts (1)

56-57:

✅ Verification successful

LGTM! Simplified particle color handling.

The removal of colorSpace parameter streamlines the color conversion process while maintaining the core functionality of particle color gradients.

Also applies to: 62-63


🏁 Script executed:

#!/bin/bash
# Verify that all _getColorTypeArray calls have been updated consistently
rg -A 2 "_getColorTypeArray\(" --type ts

Length of output: 1268


LGTM! Simplified particle color handling.

The updated calls to _getColorTypeArray and _getAlphaTypeArray in both the max (lines 56-57) and min (lines 62-63) gradient sections correctly remove the unused colorSpace parameter. This streamlines color conversion while preserving the intended gradient behavior, as verified by the consistent updates across the module.

examples/model-mesh.ts (1)

171-171: LGTM! Consistent color space conversion with fog.

The unconditional linear-to-gamma conversion ensures correct color display while maintaining proper fog effect visualization.

packages/core/src/lighting/SpotLight.ts (1)

83-85: LGTM! Consistent gamma correction.

The changes align with the PR objective by ensuring gamma to linear conversion is always applied to light color components.

examples/transparent-shadow.ts (1)

66-66: LGTM! Consistent gamma correction.

The changes align with the PR objective by ensuring linear to gamma conversion is always applied to the fragment color.

packages/core/src/particle/modules/ParticleGradient.ts (1)

156-156: LGTM! Consistent gamma correction.

The changes align with the PR objective by:

  1. Removing the colorSpace parameter from the method signature.
  2. Ensuring gamma to linear conversion is always applied to color components.

Also applies to: 165-167

tests/src/core/Shader.test.ts (1)

379-379: LGTM! Gamma color space conversion is now unconditional.

The changes correctly implement unconditional gamma color space conversion by:

  1. Converting texture color from gamma to linear space
  2. Converting final color from linear to gamma space

Also applies to: 397-397

packages/core/src/particle/ParticleGenerator.ts (1)

734-735: LGTM! Particle color now always uses linear color space.

The change correctly implements unconditional conversion of particle start color to linear space, aligning with the removal of gamma color space.

🧰 Tools
🪛 ESLint

[error] 734-734: Delete ····

(prettier/prettier)

🪛 GitHub Check: lint

[failure] 734-734:
Delete ····

@GuoLei1990 GuoLei1990 added Rendering Rendering related functions shader Shader related functions and removed documentation Improvements or additions to documentation labels Feb 21, 2025
@GuoLei1990 GuoLei1990 added this to the 1.5 milestone Feb 21, 2025
@GuoLei1990 GuoLei1990 self-assigned this Feb 21, 2025
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Feb 21, 2025
Copy link

codecov bot commented Feb 21, 2025

Codecov Report

Attention: Patch coverage is 72.97297% with 10 lines in your changes missing coverage. Please review.

Project coverage is 68.93%. Comparing base (efbc8fd) to head (dbd9b12).
Report is 1 commits behind head on dev/1.5.

Files with missing lines Patch % Lines
packages/core/src/shader/ShaderUniform.ts 72.22% 5 Missing ⚠️
...ages/core/src/particle/modules/ParticleGradient.ts 25.00% 3 Missing ⚠️
...re/src/particle/modules/ColorOverLifetimeModule.ts 0.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           dev/1.5    #2565   +/-   ##
========================================
  Coverage    68.93%   68.93%           
========================================
  Files          961      960    -1     
  Lines       100271   100199   -72     
  Branches      8654     8643   -11     
========================================
- Hits         69124    69075   -49     
+ Misses       30887    30864   -23     
  Partials       260      260           
Flag Coverage Δ
unittests 68.93% <72.97%> (+<0.01%) ⬆️

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 merged commit a979c2d into galacean:dev/1.5 Feb 26, 2025
9 checks passed
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 enhancement New feature or request Rendering Rendering related functions shader Shader related functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants