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

Enable delay initialization of the MeshoptDecoder #2146

Merged
merged 12 commits into from
Jul 2, 2024

Conversation

gz65555
Copy link
Collaborator

@gz65555 gz65555 commented Jun 27, 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)

Summary by CodeRabbit

  • Refactor
    • Improved GLTF file loading by updating the way MeshoptDecoder is handled. This change ensures more efficient initialization and usage of the decoder in the GLTFLoader.

@gz65555 gz65555 requested a review from GuoLei1990 June 27, 2024 12:13
@GuoLei1990
Copy link
Member

GuoLei1990 commented Jun 28, 2024

@coderabbitai review

Copy link

coderabbitai bot commented Jun 28, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

coderabbitai bot commented Jun 28, 2024

Walkthrough

These updates in the GLTFLoader.ts and EXT_meshopt_compression.ts files pivot around the integration and utilization of the meshoptDecoder using async methods. By modifying the import statements and implementing promises to dynamically acquire the meshoptDecoder, the refactoring enhances flexibility and potential configurations in initializing and releasing this resource across the loader functionalities.

Changes

Files Change Summary
packages/loader/src/GLTFLoader.ts Import of MeshoptDecoder changed to getMeshoptDecoder. Refactored release and initialize methods to use promises for obtaining meshoptDecoder.
packages/loader/src/gltf/extensions/... Refactored EXT_meshopt_compression.ts to replace MeshoptDecoder import with a call to getMeshoptDecoder(), updating the method usage accordingly.

Sequence Diagram(s)

Legacy Implementation Flow

sequenceDiagram
    participant GLTFLoader
    participant MeshoptDecoder

    GLTFLoader ->> MeshoptDecoder: import and usage during initialization
    GLTFLoader ->> MeshoptDecoder: invoke decodeGltfBuffer()
    GLTFLoader -->> MeshoptDecoder: release resources
Loading

Refactored Implementation Flow

sequenceDiagram
    participant GLTFLoader
    participant meshoptDecoderFactory

    GLTFLoader ->> meshoptDecoderFactory: invoke getMeshoptDecoder()
    meshoptDecoderFactory -->> GLTFLoader: returns meshoptDecoder
    GLTFLoader ->> meshoptDecoder: invoke decodeGltfBuffer()
    GLTFLoader ->> meshoptDecoderFactory: async release via promise
Loading

Poem

In the world of code so fine,
We now async our decoder divine.
Promises fulfilled with grace,
We set the pace, a swifter race.
MeshoptDecoder, now you gleam,
Through GLTF, you streamline our dream.


Tip

Early access features: enabled

We are currently testing the following features in early access:

  • OpenAI gpt-4o model for code reviews and chat: OpenAI claims that this model is better at understanding and generating code than the previous models. We seek your feedback over the next few weeks before making it generally available.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.
  • Please join our Discord Community to provide feedback and report issues.
  • OSS projects are currently opted into early access features by default.

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7eb70be and b23fdcc.

Files selected for processing (4)
  • e2e/case/gltf-meshopt.ts (2 hunks)
  • packages/loader/src/GLTFLoader.ts (2 hunks)
  • packages/loader/src/gltf/extensions/EXT_meshopt_compression.ts (1 hunks)
  • packages/loader/src/gltf/extensions/MeshoptDecoder.ts (4 hunks)
Additional comments not posted (7)
packages/loader/src/gltf/extensions/EXT_meshopt_compression.ts (2)

5-5: Refactor import to use getMeshoptDecoder.

This change aligns with the new asynchronous pattern for initializing the MeshoptDecoder, ensuring that it's loaded only when needed.


11-18: Ensure proper handling of promises and errors in createAndParse.

The method now uses promises to handle the decoder asynchronously. It's crucial to ensure that any potential rejections are handled appropriately to avoid uncaught promise rejections.

e2e/case/gltf-meshopt.ts (2)

6-6: Update imports to include GLTFLoader.

The addition of GLTFLoader in the imports is crucial for the test to utilize the loader functionality directly in the end-to-end testing environment.


42-42: Ensure GLTFLoader.release() is called correctly.

This line ensures that resources are cleaned up after the test, which is critical for preventing memory leaks in long-running applications or extensive test suites.

packages/loader/src/GLTFLoader.ts (2)

13-13: Update import to use getMeshoptDecoder.

Changing the import to getMeshoptDecoder is consistent with the new asynchronous initialization pattern of the MeshoptDecoder.


30-33: Refactor initialize method to use promises with configuration settings.

This method now properly initializes the MeshoptDecoder with the configured worker count and worker usage, ensuring that the loader is fully configured before use.

packages/loader/src/gltf/extensions/MeshoptDecoder.ts (1)

Line range hint 20-215: Major refactor of MeshoptDecoder to use promises and manage worker processes.

This comprehensive refactor introduces a promise-based system for handling decoder initialization and operations, which aligns with modern asynchronous programming practices. Ensure thorough testing to validate the correct functioning of all new methods, especially in edge cases and error scenarios.

@GuoLei1990 GuoLei1990 changed the title Delay the initialization of the MeshoptDecoder Enable delay initialization of the MeshoptDecoder Jun 30, 2024
@GuoLei1990 GuoLei1990 added enhancement New feature or request glTF labels Jun 30, 2024
@GuoLei1990 GuoLei1990 added this to the 1.3 milestone Jun 30, 2024
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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b23fdcc and 2a9c69f.

Files selected for processing (3)
  • packages/loader/src/GLTFLoader.ts (2 hunks)
  • packages/loader/src/gltf/extensions/EXT_meshopt_compression.ts (2 hunks)
  • packages/loader/src/gltf/extensions/MeshoptDecoder.ts (4 hunks)
Files skipped from review due to trivial changes (1)
  • packages/loader/src/GLTFLoader.ts
Additional context used
GitHub Check: lint
packages/loader/src/gltf/extensions/EXT_meshopt_compression.ts

[failure] 30-30:
Delete ·


[failure] 31-31:
Delete ·


[failure] 33-33:
Insert ·

Additional comments not posted (7)
packages/loader/src/gltf/extensions/EXT_meshopt_compression.ts (2)

5-5: LGTM! Import change aligns with the objective.

The import statement now uses getMeshoptDecoder for delayed initialization.


11-18: LGTM! Proper handling of asynchronous initialization.

The createAndParse method now correctly initializes the decoder before usage.

packages/loader/src/gltf/extensions/MeshoptDecoder.ts (5)

13-18: LGTM! Promise-based initialization ensures readiness.

The ready promise ensures that the decoder is initialized before usage.


20-22: LGTM! Proper handling of decoder initialization.

The getMeshoptDecoder function correctly handles the initialization and returns the decoder instance.


39-43: LGTM! WebAssembly instantiation with proper promise handling.

The ready promise ensures that the WebAssembly instance is created and initialized before resolving.


44-67: LGTM! Encapsulation of decoder functionality.

The resolved object includes methods for using workers, decoding GLTF buffers, and releasing resources, effectively encapsulating the decoder functionality.


216-217: LGTM! Optimized worker usage for decoding.

The decodeWorker function ensures that the worker with the least pending requests is used for decoding, optimizing workload distribution.

Comment on lines 30 to 33
/**
* Worker count for transcoder, default is 4.
* @defaultValue 4
*/
Copy link

Choose a reason for hiding this comment

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

Fix formatting issues.

Address the formatting issues as suggested by the static analysis tool.

-        /** 
-         * Worker count for transcoder, default is 4. 
-         * @defaultValue 4
-        */
+        /**
+         * Worker count for transcoder, default is 4.
+         * @defaultValue 4
+         */
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
/**
* Worker count for transcoder, default is 4.
* @defaultValue 4
*/
/**
* Worker count for transcoder, default is 4.
* @defaultValue 4
*/
Tools
GitHub Check: lint

[failure] 30-30:
Delete ·


[failure] 31-31:
Delete ·


[failure] 33-33:
Insert ·

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2a9c69f and 53dbdf6.

Files selected for processing (2)
  • packages/loader/src/GLTFLoader.ts (2 hunks)
  • packages/loader/src/gltf/extensions/MeshoptDecoder.ts (4 hunks)
Files skipped from review due to trivial changes (1)
  • packages/loader/src/GLTFLoader.ts
Files skipped from review as they are similar to previous changes (1)
  • packages/loader/src/gltf/extensions/MeshoptDecoder.ts

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 53dbdf6 and b277102.

Files selected for processing (3)
  • packages/loader/src/GLTFLoader.ts (2 hunks)
  • packages/loader/src/gltf/extensions/EXT_meshopt_compression.ts (1 hunks)
  • packages/loader/src/gltf/extensions/MeshoptDecoder.ts (4 hunks)
Files skipped from review due to trivial changes (1)
  • packages/loader/src/GLTFLoader.ts
Files skipped from review as they are similar to previous changes (1)
  • packages/loader/src/gltf/extensions/EXT_meshopt_compression.ts
Additional comments not posted (7)
packages/loader/src/gltf/extensions/MeshoptDecoder.ts (7)

13-19: Initialization of the ready promise looks good.

The ready promise is correctly set up to handle delayed initialization.


20-22: Refactoring of getMeshoptDecoder function looks good.

The function correctly checks for the ready promise and handles the ensure parameter.


39-43: Initialization of WebAssembly instance looks good.

The WebAssembly instance is correctly initialized within the ready promise.


44-67: Addition of methods to the resolved object looks good.

The workerCount, useWorkers, decodeGltfBuffer, and release methods are correctly defined and added to the resolved object.


143-158: Definition of decode function looks good.

The decode function is correctly defined within the worker initialization code.


160-175: Definition of workerProcess function looks good.

The workerProcess function correctly handles messages from the main thread.


216-217: Return of ready promise looks good.

The ready promise is correctly returned from the getMeshoptDecoder function.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b277102 and 1e8ac15.

Files selected for processing (2)
  • packages/loader/src/GLTFLoader.ts (2 hunks)
  • packages/loader/src/gltf/extensions/MeshoptDecoder.ts (4 hunks)
Files skipped from review as they are similar to previous changes (2)
  • packages/loader/src/GLTFLoader.ts
  • packages/loader/src/gltf/extensions/MeshoptDecoder.ts

@GuoLei1990 GuoLei1990 merged commit 5669ab9 into galacean:main Jul 2, 2024
6 checks passed
@@ -106,39 +139,39 @@ const MeshoptDecoder = (function () {
".then(function(result) {instance = result.instance; instance.exports.__wasm_call_ctors();});\n" +
Copy link
Contributor

@lixinhang lixinhang Jul 3, 2024

Choose a reason for hiding this comment

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

   function initWorkers(count) {
       const script = generateWorkerScript();
       const blob = new Blob([script], { type: "text/javascript" });
       const url = URL.createObjectURL(blob);
       for (let i = 0; i < count; ++i) {
           workers[i] = createWorker(url);
       }
       URL.revokeObjectURL(url);
   }

   function generateWorkerScript() {
       return `
       var instance;
       var ready = WebAssembly.instantiate(new Uint8Array([${new Uint8Array(unpack(wasm)).toString()}]), {})
           .then(result => {
               instance = result.instance;
               instance.exports.__wasm_call_ctors();
           });
       self.onmessage = async function(event) {
           await ready;
           const data = event.data;
           try {
               const target = new Uint8Array(data.count * data.size);
               decode(instance.exports[data.mode], target, data.count, data.size, data.source, instance.exports[data.filter]);
               self.postMessage({ id: data.id, count: data.count, action: "resolve", value: target }, [target.buffer]);
           } catch (error) {
               self.postMessage({ id: data.id, count: data.count, action: "reject", value: error });
           }
       };
       `;
   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request glTF
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

3 participants