Skip to content

Commit

Permalink
Bevy 0.10 (#16)
Browse files Browse the repository at this point in the history
* start port

* cleanups

* move most things to system state

* add more todos

* First steps towards 0.10

* Fix compilations

* Fix multi window gui

* Fix rest of the errors

* Tweak some comments

---------

Co-authored-by: Jacob LeCoq <lecoqjacob@gmail.com>
  • Loading branch information
hakolao and bayou-brogrammer authored Apr 11, 2023
1 parent 9d104f6 commit 985c5ed
Show file tree
Hide file tree
Showing 15 changed files with 2,217 additions and 2,206 deletions.
1,461 changes: 738 additions & 723 deletions Cargo.lock

Large diffs are not rendered by default.

54 changes: 39 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,40 +1,64 @@
[package]
name = "bevy_vulkano"
version = "0.11.0"
edition = "2021"
authors = ["Okko Hakola <okkohakola@gmail.com>"]
categories = ["gui", "game-development"]
description = "Vulkano Backend for Bevy"
edition = "2021"
homepage = "https://github.com/hakolao/bevy_vulkano"
keywords = ["gui", "imgui", "vulkano", "gamedev", "bevy"]
license = "Apache-2.0"
name = "bevy_vulkano"
readme = "README.md"
repository = "https://github.com/hakolao/bevy_vulkano"
categories = ["gui", "game-development"]
keywords = ["gui", "imgui", "vulkano", "gamedev", "bevy"]
version = "0.11.0"

[features]
default = []
example_has_gui = ["gui", "links", "clipboard"]
gui = ["egui_winit_vulkano"]
links = ["gui", "egui_winit_vulkano/links"]
clipboard = ["gui", "egui_winit_vulkano/clipboard"]
gui = ["egui_winit_vulkano"]

[dependencies]
vulkano = "0.32"
vulkano-util = "0.32"
vulkano-shaders = "0.32"
egui_winit_vulkano = { version = "0.23.0", optional = true, default_features = false, features = [] }
winit = "0.27"
image = "0.24.5"
approx = "0.5.1"
egui_winit_vulkano = { version = "0.24.0", optional = true, default_features = false, features = [] }
image = "0.24.5"
raw-window-handle = "0.5"
vulkano = "0.33"
vulkano-shaders = "0.33"
vulkano-util = "0.33"
winit = "0.28"

[dependencies.bevy]
version = "0.9.1"
default-features = false
features = []
version = "0.10"

[dev-dependencies]
anyhow = "1.0.66"
rand = "0.8.5"
bytemuck = "1.12.3"

[[example]]
name = "game_of_life"
path = "examples/game_of_life/main.rs"
required-features = []

[package.metadata.example.game_of_life]
name = "Game of life"
description = "Example running a more comples vulkano pipeline with compute shaders"

[[example]]
name = "multi_window_gui"
path = "examples/multi_window_gui/main.rs"
required-features = ["gui", "links", "clipboard"]

[package.metadata.example.multi_window_gui]
name = "Multi Window Gui"
description = "Example running multiple windows with egui gui"

[[example]]
name = "windowless_compute"
path = "examples/windowless_compute/main.rs"
required-features = []

[package.metadata.example.windowless_compute]
name = "Windowless compute shader"
description = "Example running a windowless vulkano compute shader"
78 changes: 3 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,97 +18,25 @@ However, you'll need to do everything in between yourself. A good way to get sta

This should be especially useful for learning graphics pipelines from scratch using Vulkano.

1. Add `VulkanoWinitPlugin`. It also adds `WindowPlugin` and anything that's needed.
1. Add `VulkanoWinitPlugin`. (Don't forget to add `WindowPlugin`, and some basic bevy plugins). Don't add default plugins.
2. Then create your own rendering systems using vulkano's pipelines (See example.). You'll need to know how to use [Vulkano](https://github.com/vulkano-rs/vulkano).
3. If you want to use [egui](https://github.com/emilk/egui) library with this, add `egui` and `bevy_vulkano` with feature `gui`.

## Usage

```rust
fn main() {
App::new()
// Vulkano configs (Modify this if you want to add features to vulkano (vulkan backend).
// You can also disable primary window opening here
.insert_non_send_resource(VulkanoWinitConfig::default())
.add_plugin(bevy::input::InputPlugin::default())
// Window settings for primary window (if you want no window, modify config above)
.add_plugin(VulkanoWinitPlugin {
window_descriptor: WindowDescriptor {
width: 1920.0,
height: 1080.0,
title: "Bevy Vulkano".to_string(),
present_mode: bevy::window::PresentMode::Immediate,
resizable: true,
mode: WindowMode::Windowed,
..WindowDescriptor::default()
}
})
.run();
}
```

### Creating a pipeline

```rust
/// Creates a render pipeline. Add this system with app.add_startup_system(create_pipelines).
fn create_pipelines_system(mut commands: Commands, vulkano_windows: NonSend<VulkanoWindows>) {
let primary_window = vulkano_windows.get_primary_window_renderer().unwrap();
// Create your render pass & pipelines (MyRenderPass could contain your pipelines, e.g. draw_circle)
let my_pipeline = YourPipeline::new(
primary_window.graphics_queue(),
primary_window.swapchain_format(),
);
// Insert as a resource
commands.insert_resource(my_pipeline);
}
```

### Rendering system

```rust

/// This system should be added either at `CoreStage::PostUpdate` or `CoreStage::Last`. You could also create your own
/// render stage and place it after `CoreStage::Update`.
fn my_pipeline_render_system(
mut vulkano_windows: NonSendMut<VulkanoWindows>,
mut pipeline: ResMut<YourPipeline>,
) {
let primary_window = vulkano_windows.get_primary_window_renderer_mut().unwrap();
// Start frame
let before = match primary_window.acquire() {
Err(e) => {
bevy::log::error!("Failed to start frame: {}", e);
return;
}
Ok(f) => f,
};

// Access the swapchain image directly
let final_image = primary_window.swapchain_image_view();
// Draw your pipeline
let after_your_pipeline = pipeline.draw(final_image);

// Finish Frame by passing your last future. Wait on the future if needed.
primary_window.present(after_your_pipeline, true);
}
```
See examples.

## Dependencies

This library re-exports `egui_winit_vulkano`.

## Examples:
```bash
cargo run --example multi_window_gui --features example_has_gui
cargo run --example multi_window_gui
cargo run --example windowless_compute
cargo run --example game_of_life
```

### Disclaimer
While you can use `bevy_vulkano` with bevy `0.9`,
the windowing features are not quite up to date with latest `bevy_window`.
Feel free to make contributions if some feature is missing.

### Contributing

Feel free to open a PR to improve or fix anything that you see would be useful.
40 changes: 15 additions & 25 deletions examples/game_of_life/game_of_life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::sync::Arc;
use bevy::{math::IVec2, prelude::Resource};
use rand::Rng;
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer},
buffer::{Buffer, BufferCreateInfo, BufferUsage, Subbuffer},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
PrimaryAutoCommandBuffer,
Expand All @@ -23,7 +23,7 @@ use vulkano::{
device::{DeviceOwned, Queue},
format::Format,
image::{ImageAccess, ImageUsage, StorageImage},
memory::allocator::StandardMemoryAllocator,
memory::allocator::{AllocationCreateInfo, MemoryUsage, StandardMemoryAllocator},
pipeline::{ComputePipeline, Pipeline, PipelineBindPoint},
sync::GpuFuture,
};
Expand All @@ -40,22 +40,22 @@ pub struct GameOfLifeComputePipeline {
command_buffer_allocator: StandardCommandBufferAllocator,
descriptor_set_allocator: StandardDescriptorSetAllocator,
compute_life_pipeline: Arc<ComputePipeline>,
life_in: Arc<CpuAccessibleBuffer<[u32]>>,
life_out: Arc<CpuAccessibleBuffer<[u32]>>,
life_in: Subbuffer<[u32]>,
life_out: Subbuffer<[u32]>,
image: DeviceImageView,
}

fn rand_grid(
allocator: &Arc<StandardMemoryAllocator>,
size: [u32; 2],
) -> Arc<CpuAccessibleBuffer<[u32]>> {
CpuAccessibleBuffer::from_iter(
fn rand_grid(allocator: &Arc<StandardMemoryAllocator>, size: [u32; 2]) -> Subbuffer<[u32]> {
Buffer::from_iter(
allocator,
BufferUsage {
storage_buffer: true,
..BufferUsage::empty()
BufferCreateInfo {
usage: BufferUsage::STORAGE_BUFFER,
..Default::default()
},
AllocationCreateInfo {
usage: MemoryUsage::Upload,
..Default::default()
},
false,
(0..(size[0] * size[1]))
.map(|_| rand::thread_rng().gen_range(0u32..=1))
.collect::<Vec<u32>>(),
Expand Down Expand Up @@ -89,12 +89,7 @@ impl GameOfLifeComputePipeline {
compute_queue.clone(),
size,
Format::R8G8B8A8_UNORM,
ImageUsage {
sampled: true,
storage: true,
transfer_dst: true,
..ImageUsage::empty()
},
ImageUsage::SAMPLED | ImageUsage::STORAGE | ImageUsage::TRANSFER_DST,
)
.unwrap();
GameOfLifeComputePipeline {
Expand Down Expand Up @@ -182,7 +177,7 @@ impl GameOfLifeComputePipeline {
])
.unwrap();

let push_constants = compute_life_cs::ty::PushConstants {
let push_constants = compute_life_cs::PushConstants {
life_color,
dead_color,
step,
Expand Down Expand Up @@ -273,10 +268,5 @@ void main() {
compute_color();
}
}",
types_meta: {
use bytemuck::{Pod, Zeroable};

#[derive(Clone, Copy, Zeroable, Pod)]
},
}
}
Loading

0 comments on commit 985c5ed

Please sign in to comment.