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

Add RenderTarget::TextureView #8042

Merged
merged 19 commits into from
Jun 19, 2023
Merged

Add RenderTarget::TextureView #8042

merged 19 commits into from
Jun 19, 2023

Conversation

mrchantey
Copy link
Contributor

@mrchantey mrchantey commented Mar 11, 2023

Objective

We can currently set camera.target to either an Image or Window. For OpenXR & WebXR we need to be able to render to a TextureView.

This partially addresses #115 as with the addition we can create internal and external xr crates.

Solution

A TextureView item is added to the RenderTarget enum. It holds an id which is looked up by a ManualTextureViews resource, much like how Assets<Image> works.
I believe this approach was first used by @kcking in their xr fork. The only change is that a u32 is used to index the textures as FromReflect does not support uuid and I don't know how to implement that.


Changelog

Added

Render: Added RenderTarget::TextureView as a camera.target option, enabling rendering directly to a TextureView.

Migration Guide

References to the RenderTarget enum will need to handle the additional field, ie in match statements.


Comments

Screenshot 2023-03-11 230651

@github-actions
Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@james7132 james7132 added C-Feature A new feature, making something new possible A-Rendering Drawing game state to the screen M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Mar 11, 2023
@james7132 james7132 added this to the 0.11 milestone Mar 11, 2023
@github-actions
Copy link
Contributor

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Could you add some context on what users should update when this change get released in a new version of Bevy?
It will be used to help writing the migration guide for the version. Putting it after a ## Migration Guide will help it get automatically picked up by our tooling.

@james7132 james7132 added the O-XR Specific to virtual and augmented reality platforms label Mar 16, 2023
@MalekiRe
Copy link
Contributor

MalekiRe commented May 3, 2023

Bumping

@@ -368,6 +370,24 @@ impl CameraRenderGraph {
}
}

/// Stores Texture Views used as render targets.
#[derive(Default, Clone, Resource, ExtractResource)]
pub struct ManualTextureViews(HashMap<u32, (TextureView, UVec2)>);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure what "Manual" is supposed to mean here.

I think I'd prefer using a wrapper type for the u32. It wasn't immediately obvious to me what the u32 was supposed to be.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd also add a note in the comment that the UVec2 is the size. Or maybe just say that it stores the texture view and it's size

Copy link
Contributor Author

@mrchantey mrchantey May 24, 2023

Choose a reason for hiding this comment

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

Ok great I've updated the comment.

Manual is intended to describe that these are texture views we are managing ourselves outside of Bevy, either by creating them in wgpu or being given them by OpenXR, WebXR etc. I agree its not obvious, any suggestions? Maybe something like Custom or External?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added the ManualTextureViewHandle wrapper type, im a little concerned its bloating the camera.rs file, would you like the ManualTextureView struct moved to a seperate file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've created manual_texture_view.rs, let me know if you want that reverted.

@@ -502,13 +542,16 @@ impl NormalizedRenderTarget {
/// [`OrthographicProjection`]: crate::camera::OrthographicProjection
/// [`PerspectiveProjection`]: crate::camera::PerspectiveProjection
/// [`Projection`]: crate::camera::Projection
/// [`CoreSet::PostUpdate`]: bevy_app::CoreSet::PostUpdate
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this line added?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Git blame is showing this as from an older pr, i guess it has been removed, i've deleted it from this pr.
Screenshot 2023-05-24 144633

@@ -436,13 +465,15 @@ impl NormalizedRenderTarget {
NormalizedRenderTarget::Image(image_handle) => {
images.get(image_handle).map(|image| image.texture_format)
}
NormalizedRenderTarget::TextureView(_) => Some(TextureFormat::bevy_default()),
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this line is correct. As far as I know, it's possible for a TextureView to be a different format.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok we should check that, my understanding was that the TextureView is opaque and we dont have access to the format, @kcking do you have thoughts on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if it is something we want to track, maybe instead of storing a (TextureView,UVec2) tuple, we can store some struct

pub struct ManualTextureView {
    texture_view: TextureView,
    size: UVec2,
    format: TextureFormat,
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, storing the correct TextureFormat makes the most sense to me.

@mrchantey mrchantey requested a review from IceSentry May 30, 2023 00:27
@mrchantey mrchantey changed the title Add RenderTarget::TextureView Add RenderTarget::TextureView May 30, 2023
@IceSentry
Copy link
Contributor

So, the implementation seems good to me, but I'm not sure I understand why this is necessary. Isn't the Image render target just a wrapper around targetting a TextureView? As in, couldn't you just use the render to image feature and use the texture view of the image?

@kcking
Copy link
Contributor

kcking commented Jun 11, 2023

So, the implementation seems good to me, but I'm not sure I understand why this is necessary. Isn't the Image render target just a wrapper around targetting a TextureView? As in, couldn't you just use the render to image feature and use the texture view of the image?

There's a quick discord conversation about this, essentially OpenXR requires you to use its TextureViews, you can't provide your own. AFAIU bevy's render to image feature creates new TextureViews so it's incompatible with OpenXR

@IceSentry
Copy link
Contributor

Alright, makes sense. In that case this PR seems fine.

Copy link
Contributor

@paul-hansen paul-hansen left a comment

Choose a reason for hiding this comment

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

Added a suggestion for adding a comment regarding the use case for this.
Looks good otherwise, though I'm not a rendering expert. Has my approval with or without my suggestion.

crates/bevy_render/src/camera/camera.rs Outdated Show resolved Hide resolved
crates/bevy_render/src/camera/camera.rs Outdated Show resolved Hide resolved
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jun 13, 2023
mrchantey and others added 2 commits June 14, 2023 09:06
Co-authored-by: Paul Hansen <mail@paul.rs>
Co-authored-by: Paul Hansen <mail@paul.rs>
Copy link
Contributor

@paul-hansen paul-hansen left a comment

Choose a reason for hiding this comment

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

Oof looks like my suggestion somehow ended up with trailing spaces and it broke cargo format. My bad.

crates/bevy_render/src/camera/camera.rs Outdated Show resolved Hide resolved
crates/bevy_render/src/camera/camera.rs Outdated Show resolved Hide resolved
mrchantey and others added 2 commits June 14, 2023 14:33
Co-authored-by: Paul Hansen <mail@paul.rs>
Co-authored-by: Paul Hansen <mail@paul.rs>
@alice-i-cecile
Copy link
Member

Straightforward enough, and on the critical path. Let's see what the community can do with this <3

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jun 19, 2023
Merged via the queue into bevyengine:main with commit 6ce4bf5 Jun 19, 2023
james7132 pushed a commit to james7132/bevy that referenced this pull request Jun 19, 2023
# Objective

We can currently set `camera.target` to either an `Image` or `Window`.
For OpenXR & WebXR we need to be able to render to a `TextureView`.

This partially addresses bevyengine#115 as with the addition we can create
internal and external xr crates.

## Solution

A `TextureView` item is added to the `RenderTarget` enum. It holds an id
which is looked up by a `ManualTextureViews` resource, much like how
`Assets<Image>` works.
I believe this approach was first used by @kcking in their [xr
fork](https://github.com/kcking/bevy/blob/eb39afd51bcbab38de6efbeeb0646e01e2ce4766/crates/bevy_render/src/camera/camera.rs#L322).
The only change is that a `u32` is used to index the textures as
`FromReflect` does not support `uuid` and I don't know how to implement
that.

---

## Changelog

### Added
Render: Added `RenderTarget::TextureView` as a `camera.target` option,
enabling rendering directly to a `TextureView`.

## Migration Guide

References to the `RenderTarget` enum will need to handle the additional
field, ie in `match` statements.

---

## Comments
- The [wgpu
work](gfx-rs/wgpu@c039a74)
done by @expenses allows us to create framebuffer texture views from
`wgpu v0.15, bevy 0.10`.
- I got the WebXR techniques from the [xr
fork](https://github.com/dekuraan/xr-bevy) by @dekuraan.
- I have tested this with a wip [external webxr
crate](https://github.com/mrchantey/forky/blob/018e22bb06b7542419db95f5332c7684931c9c95/crates/bevy_webxr/src/bevy_utils/xr_render.rs#L50)
on an Oculus Quest 2.

![Screenshot 2023-03-11
230651](https://user-images.githubusercontent.com/25616826/224483696-c176c06f-a806-4abe-a494-b2e096ac96b7.png)

---------

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
Co-authored-by: Paul Hansen <mail@paul.rs>
@cart cart mentioned this pull request Oct 13, 2023
43 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Feature A new feature, making something new possible M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide O-XR Specific to virtual and augmented reality platforms S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants