Skip to content

Conversation

jerome-caucat
Copy link
Contributor

@jerome-caucat jerome-caucat commented Jul 3, 2025

In order to integrate Bevy with Dioxus, I adapted the wgpu-texture example to use Bevy for the 3D rendering.

I added a new bevy-texture example where:

  • The rendering is done by a headless Bevy app.
  • Each frame is captured as a screenshot and used to fill a wgpu texture.
    • This part could probably be improved.
  • The texture is displayed using dioxus_native custom paint source.
Screencast.from.2025-07-03.15-22-42.webm

closes #4359

@jerome-caucat jerome-caucat force-pushed the example-bevy-texture branch from c1b1c40 to 8e87b54 Compare July 3, 2025 15:08
@nicoburns
Copy link
Contributor

Thank you for submitting this! However, it look like this is copying from the bevy Image into a cpu-side Vec<u8> and then re-uploading that back into a wgpu Texture, which is going to be terrible for performance. I think it ought to be possible to either get Bevy to render directly into an appropriate texture, or at least do a gpu-side texture-to-texture copy.

@nicoburns
Copy link
Contributor

nicoburns commented Jul 3, 2025

I think we might want to use a https://docs.rs/bevy/latest/bevy/render/camera/struct.ManualTextureView.html as the render target (or an Image, but access the texture directly if that's possible).

@jerome-caucat
Copy link
Contributor Author

I think we might want to use a https://docs.rs/bevy/latest/bevy/render/camera/struct.ManualTextureView.html as the render target (or an Image, but access the texture directly if that's possible).

@nicoburns Yes, I am going to fix that. Thanks for the suggestion!

I also would like to add a simple camera controller, to showcase how to pass mouse and key events to Bevy. Not sure how I am going to transfer the events yet.

@nicoburns nicoburns added enhancement New feature or request native Related to dioxus-native labels Jul 3, 2025
@jerome-caucat
Copy link
Contributor Author

I am struggling to use ManualTextureView.

With this code I am getting the following error:

thread '<unnamed>' panicked at /home/jeromecaucat/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/wgpu-core-24.0.5/src/storage.rs:130:9:
assertion `left == right` failed: TextureView[Id(1,6)] is no longer alive
  left: 6
 right: 1

I've tried storing the Texture and TextureView in the BevyRenderer. Perhaps the problem is that I am using the device from Dioxus to create the texture.

I'll see if I can render in a Bevy texture and get its wgpu texture.

@nicoburns
Copy link
Contributor

Perhaps the problem is that I am using the device from Dioxus to create the texture.

Yes, I had the same problem with the wgpu-texture example when I was using two separate devices. I think that one way or the other you'll need Bevy and Blitz to use the same wgpu::Device

@nicoburns
Copy link
Contributor

If you initialise Bevy RenderPlugin with RenderCreation::Manual then it looks like you can pass in your own Device.

@jerome-caucat
Copy link
Contributor Author

jerome-caucat commented Jul 4, 2025

I had a quick look at RenderCreation::Manual but it seems hard to setup so that the Bevy app works normally. I'll give it another go.

@nicoburns
Copy link
Contributor

I had a quick look at RenderCreation::Manual but it seems hard to setup so that the Bevy app works normally. I'll give it another go.

Does this help? DioxusLabs/blitz#232 (you would need to point dioxus-native to the git version of Blitz)

I think that gives you access to all the types you need. Instance is passed directly, DeviceHandle contains Device, Queue, and Adapter (you can clone them if you need owned versions), and AdapterInfo can be obtained from Adapter.

@nicoburns
Copy link
Contributor

Hang on, I'll see if I can hook it up.

@nicoburns
Copy link
Contributor

@jerome-caucat Ok, I have RenderCreation::Manual setup. So you should be able to try the texture code again now.

Signed-off-by: Nico Burns <[email protected]>
@nicoburns
Copy link
Contributor

Ok, and I also ported the code you posted above (sorry - I got excited!).

Seems to be working! I'm not sure if it is correctly responding to resizes: the wgpu-texture cube seems to resize to match the window size whereas bevy code does not (not sure if this is bug where the resize isn't being picked up, or just a difference in how the Bevy scene is being rendered)

@jerome-caucat
Copy link
Contributor Author

@nicoburns Thanks a lot for your help. I was also updating the code on my side. I'll clean the code and fix the remaining issues.

@nicoburns
Copy link
Contributor

@nicoburns Thanks a lot for your help. I was also updating the code on my side. I'll clean the code and fix the remaining issues.

Awesome. Now I've unblocked you, I'll stop interfering and let you get on with it :)

@nicoburns
Copy link
Contributor

nicoburns commented Jul 4, 2025

I've published a new versions of the anyrender and blitz crates so this PR doesn't need to depend on git versions. Anyrender crates should be set to 0.4. Blitz crates to =0.1.0-alpha.5

@jerome-caucat
Copy link
Contributor Author

@nicoburns I cleaned the code, added some comments, fixed the resize problem and renamed the example.

I noted there is a segfault when we close the window, but the wgpu-texture example has the same issue.

I wanted to add mouse and keyboard events to this demo, but perhaps I should do this in a separate issue.

nicoburns added 2 commits July 4, 2025 21:26
Signed-off-by: Nico Burns <[email protected]>
Signed-off-by: Nico Burns <[email protected]>
Copy link
Contributor

@nicoburns nicoburns left a comment

Choose a reason for hiding this comment

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

@jerome-caucat All looks good to me. Just need to fix the issues with CI and then we can land this. Long-term we may want to move this into a separate repo (as this one is already a very large workspace, and pulling in the whole of Bevy just for an example is quite a lot). But lets land it here first, and we move it and improve on it later.

I noted there is a segfault when we close the window, but the wgpu-texture example has the same issue.

Not ideal, but not disastrous in an example. If the wgpu-texture example has the same problem let's not block this PR on it.

I wanted to add mouse and keyboard events to this demo, but perhaps I should do this in a separate issue.

Yeah, let's do a new issue/PR. This will likely require further changes in the Blitz layer.

@nicoburns nicoburns merged commit 5495bce into DioxusLabs:main Jul 4, 2025
20 checks passed
@nicoburns nicoburns added example Update examples and removed enhancement New feature or request labels Jul 5, 2025
@jerome-caucat jerome-caucat deleted the example-bevy-texture branch July 7, 2025 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
example Update examples native Related to dioxus-native
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add an example integrating Bevy rendering with the native backend
2 participants