Skip to content

Comments

Add texture pool to render cache node#3804

Open
TrueDoctor wants to merge 1 commit intomasterfrom
texture-pool
Open

Add texture pool to render cache node#3804
TrueDoctor wants to merge 1 commit intomasterfrom
texture-pool

Conversation

@TrueDoctor
Copy link
Member

Reuse existing but no longer used viewport textures to reduce memory consumption while waiting for a gc on web

@gemini-code-assist
Copy link

Summary of Changes

Hello @TrueDoctor, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly optimizes memory management within the rendering pipeline by implementing a texture pooling system. By leveraging Arc for shared ownership of wgpu::Texture objects, the system can now reuse textures that are no longer in active use, thereby reducing the frequency of new texture allocations. This change is particularly beneficial for web platforms where memory garbage collection can be a performance bottleneck, leading to a smoother and more efficient user experience.

Highlights

  • Texture Pooling Implementation: Introduced a texture pooling mechanism in TileCacheImpl to reuse wgpu::Texture instances, reducing memory allocations and improving performance, especially for web environments.
  • Shared Ownership for Textures: Migrated several wgpu::Texture fields and parameters across the codebase to use std::sync::Arc<wgpu::Texture>, enabling shared ownership and facilitating texture reuse.
  • Refactored Texture Compositing: The composite_cached_regions function was updated to accept a pre-allocated wgpu::Texture from the new texture pool, rather than creating a new one each time.
  • ImageTexture Updates: The ImageTexture struct now holds an Arc<wgpu::Texture> and derives DynAny for improved type handling.
Changelog
  • desktop/src/render/state.rs
    • Updated transparent_texture and viewport_texture fields to use std::sync::Arc<wgpu::Texture>.
    • Wrapped the creation of transparent_texture in Arc::new().
    • Modified bind_viewport_texture to accept std::sync::Arc<wgpu::Texture>.
  • desktop/wrapper/src/lib.rs
    • Changed the HasRun variant of NodeGraphExecutionResult to hold an Option<std::sync::Arc<wgpu::Texture>>.
  • editor/src/node_graph_executor/runtime.rs
    • Added a new field current_viewport_texture: Option<ImageTexture> to NodeRuntime.
    • Initialized current_viewport_texture to None in the NodeRuntime::new constructor.
    • Modified calls to Raster::new_gpu to clone the Arc reference from image_texture.texture.
    • Stored the image_texture in current_viewport_texture after rendering to the WASM viewport surface.
  • node-graph/libraries/application-io/src/lib.rs
    • Added the DynAny derive macro to the ImageTexture struct.
    • Updated the texture field within ImageTexture to be Arc<wgpu::Texture>.
    • Removed the unsafe impl StaticType for ImageTexture block.
  • node-graph/nodes/gstd/src/render_cache.rs
    • Introduced a texture_cache field to TileCacheImpl, consisting of a UVec2 size and a Vec of Arc<wgpu::Texture>.
    • Initialized the texture_cache to its default value in TileCacheImpl::default.
    • Implemented a request_texture method in TileCacheImpl to manage and provide reusable wgpu::Texture instances from the cache.
    • Added a public request_texture method to TileCache that delegates to the internal TileCacheImpl.
    • Modified render_output_cache to acquire the output texture from tile_cache.request_texture.
    • Updated the composite_cached_regions function to accept a reference to an existing wgpu::Texture for output, instead of creating a new one, and changed its return type to only rendering::RenderMetadata.
    • Changed CachedRegion.texture to store a cloned Arc reference.
  • node-graph/nodes/gstd/src/render_node.rs
    • Wrapped the wgpu::Texture returned from exec.render_vello_scene_to_texture in an Arc::new() call.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a texture pool to the render cache node, aimed at reducing memory consumption and allocation overhead, particularly for WASM-based viewport rendering. By wrapping wgpu::Texture in std::sync::Arc and tracking strong reference counts, the system can now reuse textures that are no longer in use by the editor or previous frames. The implementation is clean and leverages Rust's ownership model effectively to manage GPU resource lifetimes without explicit manual pooling logic in most places. The identified redundant comment should be removed to maintain code clarity.

let exec = editor_api.application_io.as_ref().unwrap().gpu_executor().unwrap();
let (output_texture, combined_metadata) = composite_cached_regions(&all_regions, &viewport_bounds, physical_resolution, logical_scale, physical_scale, exec);

// TODO: Use texture pool to reuse existing unused textures instead of allocating fresh ones every time

Choose a reason for hiding this comment

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

medium

This TODO comment is redundant as the logic to use the texture pool is implemented immediately below on line 470. It should be removed to avoid confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant