Skip to content

Commit

Permalink
Bevy render PoC.
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed Jan 26, 2024
1 parent d24220a commit cb03fc8
Show file tree
Hide file tree
Showing 26 changed files with 5,457 additions and 8 deletions.
44 changes: 44 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 29 additions & 3 deletions bevy_nannou/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,50 @@
use bevy::prelude::*;
use bevy_nannou_render::mesh::{Vertex, ViewMesh};

struct NannouPlugin;
pub struct NannouPlugin;

impl Plugin for NannouPlugin {
fn build(&self, app: &mut App) {
app.add_plugins((
app.add_systems(Startup, setup).add_plugins((
bevy_nannou_render::NannouRenderPlugin,
bevy_nannou_draw::NannouDrawPlugin,
));
}
}

fn setup(mut commands: Commands) {
let mut mesh = ViewMesh::default();
mesh.extend_from_slices(
&[
Vec3::new(-512.0, -384.0, 0.0),
Vec3::new(-512.0, 384.0, 0.0),
Vec3::new(512.0, 384.0, 0.0),
],
&[0, 1, 2],
&[Vec4::new(1.0, 0.0, 0.0, 1.0); 3],
&[Vec2::new(0.0, 0.0); 3],
);
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, -10.0).looking_at(Vec3::ZERO, Vec3::Z),
projection: OrthographicProjection {
..Default::default()
}
.into(),
..Default::default()
},
mesh,
));
}

#[cfg(test)]
mod tests {
use bevy::app::App;

#[test]
fn it_works() {
let mut app = App::new();
app.add_plugins(super::NannouPlugin);
app.add_plugins((bevy::DefaultPlugins, super::NannouPlugin));
app.update();
}
}
7 changes: 7 additions & 0 deletions bevy_nannou/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use bevy_nannou::NannouPlugin;

pub fn main() {
let mut app = bevy::app::App::new();
app.add_plugins((bevy::DefaultPlugins, NannouPlugin));
app.run();
}
6 changes: 6 additions & 0 deletions bevy_nannou_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ edition = "2021"

[dependencies]
bevy = { workspace = true }
bevy_nannou_wgpu = { path = "../bevy_nannou_wgpu" }
nannou_core = { path = "../nannou_core" }
nannou_mesh = { path = "../nannou_mesh" }
rusttype = { version = "0.8", features = ["gpu_cache"] }
lyon = "0.17"
bevy_mod_debugdump = "0.9.0"
125 changes: 124 additions & 1 deletion bevy_nannou_render/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,130 @@
use bevy::asset::load_internal_asset;
use bevy::core_pipeline::core_3d;
use bevy::core_pipeline::core_3d::CORE_3D;
use bevy::prelude::*;
use bevy::render::{Extract, render_resource as wgpu, RenderSet};
use bevy::render::{Render, RenderApp};
use bevy::render::extract_component::ExtractComponentPlugin;
use bevy::render::render_asset::RenderAsset;
use bevy::render::render_graph::{RenderGraphApp, ViewNode, ViewNodeRunner};
use bevy::render::render_phase::{
AddRenderCommand, PhaseItem, RenderCommand,
};
use bevy::render::render_resource::{
ShaderType, SpecializedRenderPipeline,
SpecializedRenderPipelines,
};
use bevy::render::renderer::RenderDevice;
use bevy::render::view::ViewUniforms;

use mesh::ViewMesh;
use pipeline::queue_pipelines;

use crate::pipeline::{NannouPipeline, NannouViewNode};

pub mod mesh;
mod pipeline;
mod text;
// mod reshaper;

pub struct NannouRenderPlugin;

pub const NANNOU_SHADER_HANDLE: Handle<Shader> = Handle::weak_from_u128(43700360588854283521);

impl Plugin for NannouRenderPlugin {
fn build(&self, _app: &mut App) {}
fn build(&self, app: &mut App) {
load_internal_asset!(
app,
NANNOU_SHADER_HANDLE,
"shaders/nannou.wgsl",
Shader::from_wgsl
);

app
.add_plugins(ExtractComponentPlugin::<ViewMesh>::default());


println!("NannouRenderPlugin::build");
app.get_sub_app_mut(RenderApp)
.unwrap()
.init_resource::<SpecializedRenderPipelines<NannouPipeline>>()
.add_systems(
Render,
prepare_view_uniform.in_set(RenderSet::PrepareBindGroups),
)
.add_systems(
Render,
queue_pipelines
.in_set(RenderSet::PrepareAssets)
)
.add_render_graph_node::<ViewNodeRunner<NannouViewNode>>(
core_3d::graph::NAME,
NannouViewNode::NAME,
)
.add_render_graph_edges(
CORE_3D,
&[
core_3d::graph::node::MAIN_TRANSPARENT_PASS,
NannouViewNode::NAME,
core_3d::graph::node::END_MAIN_PASS,
],
);

bevy_mod_debugdump::print_render_graph(app);
}

fn finish(&self, app: &mut App) {
let Ok(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
};

render_app.init_resource::<NannouPipeline>();
}
}

fn prepare_view_uniform(
mut commands: Commands,
render_device: Res<RenderDevice>,
pipline: Res<NannouPipeline>,
view_uniforms: Res<ViewUniforms>,
) {
if let Some(binding) = view_uniforms.uniforms.binding() {
commands.insert_resource(ViewUniformBindGroup::new(
&render_device,
&pipline.view_bind_group_layout,
binding,
));
}
}
#[derive(Resource)]
struct ViewUniformBindGroup {
bind_group: wgpu::BindGroup,
}

impl ViewUniformBindGroup {
fn new(
device: &RenderDevice,
layout: &wgpu::BindGroupLayout,
binding: wgpu::BindingResource,
) -> ViewUniformBindGroup {
let bind_group = bevy_nannou_wgpu::BindGroupBuilder::new()
.binding(binding)
.build(device, layout);

ViewUniformBindGroup { bind_group }
}
}

/// A top-level indicator of whether or not
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
#[repr(u32)]
pub enum VertexMode {
/// Use the color values and ignore the texture coordinates.
Color = 0,
/// Use the texture color and ignore the color values.
Texture = 1,
/// A special mode used by the text primitive.
///
/// Uses the color values, but multiplies the alpha by the glyph cache texture's red value.
Text = 2,
}
Loading

0 comments on commit cb03fc8

Please sign in to comment.