Bevy Developer Network
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    :::warning **Note:** I need to edit this note when I get the chance, so some things might not be up to date: - need to rewrite the parts that include `SubView`, since I misunderstood what it was for previously and the names mismatch here. - Also, need to expand on my thoughts regarding "full copy-less" a.k.a. sharing stacked camera results through intermediate textures. spoiler: I don't think it's even *correct* in all cases (bugs aside) and if it's the only way we can minimize copies then I think we should support it, but in a constrained way. There's some other architectural issues with it, but I'll elaborate on that later :slightly_smiling_face: ::: # Camera Restructure Currently, Bevy `Camera`s are kind of a leaky abstraction: multi-camera setups require a lot of manual fiddling with viewports, and post-processing artifacts are common. I'm proposing to remove `Camera` from its role as a fundamental rendering primitive and introduce a few new concepts to fill in the gap: `Compositor`s and `View`s. ## Issues with `Camera` ### Sorting, Ordering, Clearing, Oh My! There's a conflict within `Camera` that I think is the source of a lot of our issues: cameras are conceptually configured fully individually, with their own ordering index and viewport, rendering effects, even potentially a custom render graph, but we currently try *really* hard to share intermediate textures between cameras. As a result, a lot of the compositing logic leaks into the rest of the graph: each camera has to recalculate projection matrices, pass through viewports, worry whether or not to clear the main textures or not, and whether or not to apply tonemapping. ### Manual Viewports :( Viewports are annoying to set up. Part of my rationale for doing "top down" layout from an external entity is that it opens up the possibility of nicer layout APIs in the future. If each camera stays in charge of choosing its own viewport, I don't see a good way forward there. ### UI is Special. Why? Currently, UI hierarchies render themselves to the entity referenced by their `UiTargetCamera` component. This `Camera` is expected to render the UI by calling the UI render graph at the appropriate point in its render graph. This is kind of a weird relationship-- I think UI should be the same fundamental "renders to a render target" thing under the hood as cameras, and not require any special handling. ## My Proposal `Camera`s are no longer the primitive for rendering to a render target. They don't choose their own viewports, either. But, in return, they get full control over their own rendering flow and intermediate textures. A few new primitives: `Compositor`: manages layout and ordering for its child `View`s, and is where a `RenderTarget` is actually selected. `View`: a child of a `Compositor`, which it requests a viewport from. Currently this is stupid simple, and keeps the same Camera-`SubView` API we have now. Each one has the responsibility of rendering to its assigned viewport once per frame, but the exact method is for each view to decide. `Views` can be cameras, UI canvases, or anything that needs a render graph and writes to a final output texture. Some pros: - using immutable components for components like `RenderTarget` lets us respond to most changes in realtime, and to simplify `camera_system` - the "top down" nature of compositors means we get ordering for free through the `CompositedViews` relationship - Since cameras manage their own intermediate textures: - we can remove (almost) all the atomics from `ViewTarget` (now `ViewTextures`) - gives upscalers like dlss an easier time, since all views are "fullscreen" - opens the door for better customizability (custom formats, low-res rendering) - no explicit viewport handling in the majority of render graph nodes - Since the existence of a `ViewTarget` implies a properly setup compositor and render target, all of its methods can be infallible! (`physical_target_size`, `logical_viewport_rect`, etc). Contrast this with the same methods on `Camera`, which all return `Option<...>` currently. - unified rendering primitives: UI views don't need any special handling from the cameras they composite onto. They can just be handled like any other view, blended over the top of the game by the compositor - for the crate reorg: - better separating concerns around `Camera` should make it a lot nicer to abstract into its own crate: - `bevy_ui` can depend on `bevy_render` rather than `bevy_camera` - Some cons: - have to remember to order UI above its respective camera. We could probably detect this and warn. - harder to do copy-less. See below. ``` RenderTargetChanged ┌────────────┐ ▼ │ ┌────┬────────────┬─┴────────────┬───────────────────┐ │ e1 │ Compositor │ RenderTarget │ RenderGraphDriver │ └────┴──────────┬─┴──────────────┴───────────────────┘ ▲ ▲ │ │ │ │ ViewChanged │ └───────┬─┐ │ │ │ │ │ SubViewChanged │ │ │ │ │ │ │ │ ▼ │ ┌────┬─┴────┬─┴───────┬────────────┬────────┬───────────────────┐ │ e2 │ View │ SubView │ ViewTarget | Camera | RenderGraphDriver │ └────┴──────┴─────────┴────────────┴────────┴───────────────────┘ │ │ │ │ │ ▼ ┌────┬─┴────┬─┴───────┬────────────┬──────────┬───────────────────┐ │ e3 │ View │ SubView │ ViewTarget | UiCanvas │ RenderGraphDriver │ └────┴──────┴─────────┴────────────┴──────────┴───────────────────┘ Note: see below. `SubView` and `ViewTarget` are slightly mismatched from their current definitions in the engine. Need to edit/clarify this better later ``` ### What does it look like in the render world? 1. extract compositors and views 2. run compositor render graphs - default compositor render graph calls each of its child views in order, then presents the result - compositor either maintains its own intermediate texture or has views write directly to the swapchain, depending on what's faster for each platform 3. views create and manage their own intermediate textures (or not) and are always conceptually "fullscreen" until they blit to the compositor. See copy-less section below, I don't think this is worth compromising in the general case to eliminate ~1 copy per camera. #### MSAA writeback The compositor gives each view a `TextureView` and `Viewport` for the view target. We can just read back from this at the start of the graph! #### Actually compositing Each camera gets to decide on its own how to write to the view target, but we can provide a "standard" node to blit/copy from the main intermediate texture to the `ViewTarget`. ### What would the new use of `Camera` be? If we remove all the bits of camera that actually drive rendering, what's the use of keeping `Camera` around? Well, it's still useful for all the stuff that needs the physical metaphor of a camera! Projection transforms, the concept of space (`#[require(Transform)]`), and all the machinery for visibility checking, etc. We can also get more opinionated about the "standard" feel of what a camera does in the future, since it wouldn't be as fundamental to rendering. Like, I'm imagining once we have render-graphs-as-schedules, I want to provide some common system sets so third party crates can integrate with more than the built-in render graph. I still want to keep `Camera` around and have it stay important, but I think the more we want to clean the renderer up and solve bugs with compositing, its scope needs to be smaller than it is now. ### Compositors ```rust! #[derive(Component, Default, MapEntities)] #[require( RenderTarget, CompositedViews, RenderGraphDriver::new(DefaultCompositorGraph), SyncToRenderWorld )] pub struct Compositor { // This is the actual list of active views, and is a subset of // the `CompositedViews` component. it also dictates the order // in which the views are rendered. #[entities] views: Vec<Entity>, target: Option<Arc<(NormalizedRenderTarget, RenderTargetInfo)>>, } // now an immutable component on compositors!!! #[derive(Component, Debug, Clone, Reflect, From)] #[component( immutable, on_insert = Self::on_insert, // trigger `CompositorEvent::RenderTargetChanged` on_remove = Self::on_remove // reinsert default if on a compositor )] #[reflect(Clone)] pub enum RenderTarget { Window(WindowRef), Image(ImageRenderTarget), TextureView(ManualTextureViewHandle), } #[derive(Component, Default)] #[relationship_target(relationship = CompositedBy)] pub struct CompositedViews(Vec<Entity>); ``` ### Views ```rust! #[derive(Component, Default)] #[component( immutable, on_insert = Self::on_insert, // trigger `CompositorEvent::ViewChanged` on_remove = Self::on_remove // same as above )] #[require(RenderGraphDriver, SyncToRenderWorld)] pub enum View { Disabled, #[default] Enabled, } // Sorry for the overloaded name! I've renamed the other `ViewTarget` // to `ViewTextures`. All the viewport- and target-related methods // previously on `Camera` are on `ViewTarget` now, and are no longer // fallible. // // This is completely managed by the parent `Compositor` in response // to layout events. #[derive(Component, Clone)] pub struct ViewTarget { target: Arc<(NormalizedRenderTarget, RenderTargetInfo)>, viewport: Option<Viewport>, } // Same as we have now, just as an immutable component // (and renamed from `CameraSubView`). A full fancy layout api // can come later, and needs some design first :p #[derive(Debug, Component, Clone, Copy, Reflect, PartialEq)] #[component( immutable, on_insert = Self::on_insert // trigger `CompositorEvent::SubViewChanged` on_remove = Self::on_remove // reinsert default if on a view )] #[reflect(Clone, PartialEq, Default)] pub struct SubView { pub full_size: UVec2, pub offset: Vec2, pub size: UVec2, } #[derive(Component)] #[relationship(relationship_target = CompositedViews)] pub struct CompositedBy(pub Entity); // the newly-renamed version of `CameraSubGraph`, // which is used for both Views *and* Compositors #[derive(Component)] pub struct RenderGraphDriver(InternedRenderSubGraph); ``` ## Future Work/Questions ### Compositing isn't simple There's a lot of more esoteric ways to composite rendering effects, for example with world-space render targets (think portals or mirrors), stencil-based compositing, or interleaving 2d and 3d elements in a transform hierarchy. While I think the model proposed here is a dramatic improvement over what we have currently, we should consider the direction we want to take moving forward. ### Copy-less compositing Texture copies can be expensive, so we want to minimize them where possible. It should always be possible to eliminate them entirely in constrained circumstances (no per-camera post-processing, all cameras have same settings). Currently, the way we do this is through sharing camera results through ordering and intermediate textures, which is incredibly bug-prone, and I don't believe this is the way we should move forward (at least in the general case) ### RTaE? Bevy `Window`s are already entities, so I don't think it's that much of a stretch to imagine modeling all render targets as entities, and using 1:1 relationships to prevent conflicts. We could also merge `RenderTarget`/`NormalizedRenderTarget` once we have `Construct` :thinking_face: ### Picking Integration Picking is pretty crucial to get right, but I don't have any experience with it really. If there's a part of this that might compromise `bevy_picking` or could be made to better work with it, please let me know :)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully