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

built-in prefiltered lut #2463

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

Conversation

zhuxudong
Copy link
Member

@zhuxudong zhuxudong commented Dec 13, 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 property for managing a prefiltered LUT texture in the BasicResources class.
    • Added a static property in the Scene class to enhance handling of prefiltered lookup textures.
    • New utility function for creating and managing prefiltered LUT textures.
    • Added an end-to-end test for materials using a Look-Up Table (LUT).
    • Expanded configuration options for materials to include Look-Up Tables in end-to-end testing.
  • Bug Fixes

    • Improved error handling for texture loading in the new utility function.

@zhuxudong zhuxudong added the Rendering Rendering related functions label Dec 13, 2024
@zhuxudong zhuxudong added this to the 1.4 milestone Dec 13, 2024
Copy link

coderabbitai bot commented Dec 13, 2024

Walkthrough

The changes introduce enhancements to the rendering capabilities of the BasicResources and Scene classes. A new function, getPrefilterdLUTTexture, is added to manage a prefiltered LUT texture, which is then integrated into the BasicResources class. Additionally, a static property for handling this texture is introduced in the Scene class, allowing it to utilize the prefiltered LUT for improved rendering effects. The changes also include the creation of a new utility module for texture management and the addition of end-to-end tests for materials using LUTs.

Changes

File Change Summary
packages/core/src/BasicResources.ts Added public property readonly prefilteredLUTTexture: Texture2D and initialized it in the constructor using getPrefilterdLUTTexture(engine).
packages/core/src/Scene.ts Added static property private static _prefilterdLUTProperty and set it in the constructor using shader data.
packages/core/src/material/Utils/prefilteredLUT.ts Introduced function getPrefilterdLUTTexture(engine: Engine): Texture2D for creating and returning a prefiltered LUT texture.
e2e/case/material-LUT.ts Created a new E2E test file for material using a Look-Up Table (LUT) in a WebGL engine context.
e2e/config.ts Added new property LUT in E2E_CONFIG.Material with attributes for end-to-end testing.

Possibly related PRs

  • Fix the mesh was not restored after context lost. #2418: This PR modifies the BasicResources class, which is directly related to the changes made in the main PR that also involves modifications to BasicResources.ts, specifically regarding resource management and restoration.
  • Fix renderQueueType error #2437: This PR updates the RenderQueue and BasicRenderPipeline, which may indirectly relate to the rendering enhancements introduced in the main PR through the management of textures and rendering states.

Suggested labels

enhancement, material, shader

Suggested reviewers

  • GuoLei1990
  • Sway007

Poem

In the garden where textures bloom,
A prefiltered LUT dispels the gloom.
With shaders bright and colors true,
The scene comes alive with a vibrant hue.
Hopping along, we celebrate this sight,
For rendering magic brings pure delight! 🐇✨


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. (Experiment)
  • @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.

@zhuxudong zhuxudong linked an issue Dec 13, 2024 that may be closed by this pull request
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: 3

🧹 Outside diff range and nitpick comments (1)
packages/core/src/material/Utils/prefilteredLUT.ts (1)

6-9: Consider moving the base64 string to a separate asset file

The large base64 encoded PNG string should be moved to a separate asset file to improve code maintainability and loading performance.

Consider:

  1. Creating a separate asset file (e.g., prefilteredLUT.asset.ts)
  2. Loading it asynchronously if needed
  3. Adding documentation about the LUT image format and purpose
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between afc9087 and c18de86.

📒 Files selected for processing (3)
  • packages/core/src/BasicResources.ts (3 hunks)
  • packages/core/src/Scene.ts (2 hunks)
  • packages/core/src/material/Utils/prefilteredLUT.ts (1 hunks)
🔇 Additional comments (1)
packages/core/src/BasicResources.ts (1)

12-12: LGTM! Clean integration of prefiltered LUT texture

The changes follow the existing patterns in the codebase and properly integrate the new texture functionality.

Also applies to: 120-121, 168-168

Comment on lines +12 to +44
export function getPrefilterdLUTTexture(engine: Engine): Texture2D {
const texture = new Texture2D(engine, size, size, TextureFormat.R8G8B8, false);
texture.wrapModeU = texture.wrapModeV = TextureWrapMode.Clamp;
texture.isGCIgnored = true;

if (prefilterdLUTImage) {
texture.setImageSource(prefilterdLUTImage);
} else {
const image = new Image();
prefilterdLUTImage = image;

image.onload = () => {
texture.setImageSource(image);
};
image.onerror = image.onabort = () => {
Logger.error("Failed to load prefiltered LUT image.");
};
image.src = base64;
}

engine.resourceManager.addContentRestorer(
new (class extends ContentRestorer<Texture2D> {
constructor() {
super(texture);
}
restoreContent() {
texture.setImageSource(prefilterdLUTImage);
}
})()
);

return texture;
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Fix typo in function name and enhance error handling

The implementation has a few areas for improvement:

  1. The function name has a typo: 'prefilterd' should be 'prefiltered'
  2. Error handling could be enhanced for image loading failures

Consider applying these changes:

-export function getPrefilterdLUTTexture(engine: Engine): Texture2D {
+export function getPrefilteredLUTTexture(engine: Engine): Texture2D {
   // ... existing code ...
   
   image.onerror = image.onabort = () => {
-    Logger.error("Failed to load prefiltered LUT image.");
+    Logger.error("Failed to load prefiltered LUT image. This may impact rendering quality.");
+    // Consider providing a fallback texture or throwing an error if critical
   };

Committable suggestion skipped: line range outside the PR's diff.

@@ -27,6 +27,7 @@ import { ShadowType } from "./shadow/enum/ShadowType";
export class Scene extends EngineObject {
private static _fogColorProperty = ShaderProperty.getByName("scene_FogColor");
private static _fogParamsProperty = ShaderProperty.getByName("scene_FogParams");
private static _prefilterdLUTProperty = ShaderProperty.getByName("scene_prefilteredLUT");
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Maintain consistent naming with prefilteredLUT.ts

The property name has the same typo as the function in prefilteredLUT.ts ('prefilterd' vs 'prefiltered'). While this maintains consistency with the current implementation, it should be updated if the function name is fixed.

-private static _prefilterdLUTProperty = ShaderProperty.getByName("scene_prefilteredLUT");
+private static _prefilteredLUTProperty = ShaderProperty.getByName("scene_prefilteredLUT");

Also applies to: 288-288


💡 Codebase verification

Fix typo in shader property name and ensure consistent naming

The codebase shows inconsistent spelling between the property name "scene_prefilteredLUT" and the variable/function names containing "prefilterd" (missing 'e'). This needs to be corrected for consistency:

  • packages/core/src/Scene.ts: Variable _prefilterdLUTProperty and its usage
  • packages/core/src/BasicResources.ts: Function getPrefilterdLUTTexture
🔗 Analysis chain

Verify shader property name usage

Let's verify that the shader property name "scene_prefilteredLUT" is consistently used across the codebase.

Also applies to: 288-288

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any usage of the shader property name
rg "scene_prefilteredLUT"

# Search for any similar prefiltered LUT related properties
rg -i "prefilt[er]{2}[ed]{2}.*LUT"

Length of output: 836

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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c18de86 and c8bfd31.

⛔ Files ignored due to path filters (1)
  • e2e/fixtures/originImage/Material_material-LUT.jpg is excluded by !**/*.jpg
📒 Files selected for processing (2)
  • e2e/case/material-LUT.ts (1 hunks)
  • e2e/config.ts (1 hunks)
🔇 Additional comments (2)
e2e/case/material-LUT.ts (1)

40-40: Verify uniform name consistency

The uniform name scene_prefilteredLUT should match the actual uniform name provided by the engine's Scene class.

Let's verify the uniform name in the engine's Scene class:

✅ Verification successful

Uniform name scene_prefilteredLUT is correctly defined and used

The verification confirms that the uniform name scene_prefilteredLUT is consistent with the engine's implementation:

  • It's properly defined as a shader property in Scene.ts: _prefilterdLUTProperty = ShaderProperty.getByName("scene_prefilteredLUT")
  • The Scene class correctly sets this uniform using the engine's prefiltered LUT texture
  • The uniform name in the shader matches exactly with what's defined in the engine
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for prefilteredLUT-related code in Scene class
ast-grep --pattern 'class Scene {
  $$$
  prefilteredLUT
  $$$
}'

Length of output: 67


Script:

#!/bin/bash
# Let's try a broader search for prefilteredLUT across the codebase
rg -i "prefilteredLUT" -A 2 -B 2

# Also search for uniform declarations in shader-related files
fd -e glsl -e ts -e js . -x rg -l "uniform.*LUT"

Length of output: 2519

e2e/config.ts (1)

128-131: LGTM! Configuration follows established patterns

The LUT test configuration is properly structured and uses consistent threshold values with other material tests.

Comment on lines +49 to +52
setTimeout(() => {
updateForE2E(engine);
initScreenshot(engine, camera);
}, 0);
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider adding validation before screenshot

The test immediately captures a screenshot without ensuring the LUT texture is loaded and applied. Consider adding validation or a proper loading callback.

 setTimeout(() => {
+  // Ensure LUT texture is loaded
+  const material = renderer.getMaterial();
+  if (!material.getShaderData().getTexture("scene_prefilteredLUT")) {
+    throw new Error("LUT texture not loaded");
+  }
   updateForE2E(engine);
   initScreenshot(engine, camera);
 }, 0);

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +13 to +14
const rootEntity = scene.createRootEntity();
// engine.run();
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Uncomment engine.run() to align with other test implementations

Looking at the codebase, several test files including material-unlit.ts, particleRenderer-textureSheetAnimation.ts, and others actively use engine.run(). The current implementation in material-LUT.ts has it commented out, which deviates from the established testing pattern. This could affect the test's ability to properly simulate the rendering pipeline.

  • Uncomment line 14: engine.run();
🔗 Analysis chain

Consider adding engine.run() for realistic testing

The commented out engine.run() might affect the test's realism. In a real application, the engine would be running to render frames continuously.

Let's check if other material tests use engine.run():

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for engine.run() usage in other material test files
rg -B 2 -A 2 "engine\.run\(\)" "e2e/case/material-*.ts"

Length of output: 120


Script:

#!/bin/bash
# Let's first find all material test files
fd "material-.*\.ts" "e2e/case"

# Then search for engine.run() usage in all test files to understand the pattern
rg -B 2 -A 2 "engine\.run\(\)" "e2e/case/"

Length of output: 1830

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rendering Rendering related functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

built-in brdf LUT
1 participant