upgrade legion (breaks plugins)

This commit is contained in:
Carter Anderson 2020-03-08 23:19:07 -07:00
parent 6055bf677b
commit 9355a53980
35 changed files with 270 additions and 157 deletions

View file

@ -6,7 +6,8 @@ edition = "2018"
[dependencies]
# Modified to use std::any::type_name instead of std::any::TypeId
legion = { path = "bevy_legion", features = ["serde-1"] }
# legion = { path = "bevy_legion", features = ["serde-1"] }
legion = { git = "https://github.com/TomGillen/legion", rev = "c5b9628630d4f9fc54b6843b5ce02d0669434a61", features = ["serialize"] }
wgpu = { git = "https://github.com/gfx-rs/wgpu-rs.git", rev = "a7b0d5ae5bc0934439ef559ed145e93f0117c39a"}
glam = "0.8.4"
winit = "0.21.0"

View file

@ -7,7 +7,8 @@ edition = "2018"
license = "MIT"
[dependencies]
legion = { path = "../bevy_legion" }
# legion = { path = "../bevy_legion" }
legion = { git = "https://github.com/TomGillen/legion", rev = "c5b9628630d4f9fc54b6843b5ce02d0669434a61", features = ["serialize"] }
glam = "0.8.3"
log = "0.4"
rayon = "1.2"

View file

@ -1,8 +1,5 @@
#![allow(dead_code)]
use crate::{
components::*,
ecs::{prelude::*, system::SubWorld},
};
use crate::{components::*, ecs::{prelude::*, systems::SubWorld}};
pub fn build(_: &mut World) -> Box<dyn Schedulable> {
SystemBuilder::<()>::new("LocalToWorldPropagateSystem")
@ -59,7 +56,7 @@ mod test {
use crate::{
hierarchy_maintenance_system, local_to_parent_system, local_to_world_propagate_system,
local_to_world_system,
math::{Vec3, Mat4}
math::{Mat4, Vec3},
};
#[test]

View file

@ -73,9 +73,9 @@ fn main() {
.run();
}
fn setup(world: &mut World) {
fn setup(world: &mut World, resources: &mut Resources) {
let cube_handle = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
mesh_storage.add(Mesh::load(MeshType::Cube))
};

View file

@ -160,12 +160,12 @@ fn create_entities_builder_archetype(
.build();
}
fn setup(world: &mut World) {
fn setup(world: &mut World, resources: &mut Resources) {
let cube = Mesh::load(MeshType::Cube);
let plane = Mesh::load(MeshType::Plane { size: 10.0 });
let (cube_handle, plane_handle) = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
(mesh_storage.add(cube), mesh_storage.add(plane))
};

View file

@ -29,10 +29,10 @@ fn main() {
.run();
}
fn setup(world: &mut World) {
fn setup(world: &mut World, resources: &mut Resources) {
let cube = Mesh::load(MeshType::Cube);
let cube_handle = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
mesh_storage.add(cube)
};

View file

@ -21,12 +21,12 @@ fn build_rotator_system() -> Box<dyn Schedulable> {
})
}
fn setup(world: &mut World) {
fn setup(world: &mut World, resources: &mut Resources) {
let cube = Mesh::load(MeshType::Cube);
let plane = Mesh::load(MeshType::Plane { size: 10.0 });
let (cube_handle, plane_handle) = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
(mesh_storage.add(cube), mesh_storage.add(plane))
};

View file

@ -1,4 +1,4 @@
use bevy::prelude::*;
use bevy::{prelude::*, plugin::AppPlugin};
use bevy_derive::RegisterAppPlugin;
#[derive(RegisterAppPlugin)]
@ -14,29 +14,32 @@ impl AppPlugin for ExamplePlugin {
}
}
pub fn setup(world: &mut World) {
pub fn setup(world: &mut World, resources: &mut Resources) {
let cube = Mesh::load(MeshType::Cube);
let plane = Mesh::load(MeshType::Plane { size: 10.0 });
let (cube_handle, plane_handle) = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
(mesh_storage.add(cube), mesh_storage.add(plane))
};
world.build()
// plane
.add_archetype(NewMeshEntity {
.add_archetype(MeshEntity {
mesh: plane_handle,
material: Material::new(Albedo::Color(math::vec4(0.1, 0.2, 0.1, 1.0))),
local_to_world: LocalToWorld::identity(),
translation: Translation::new(0.0, 0.0, 0.0),
material: StandardMaterial {
albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(),
},
..Default::default()
})
// cube
.add_archetype(NewMeshEntity {
.add_archetype(MeshEntity {
mesh: cube_handle,
material: Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0))),
local_to_world: LocalToWorld::identity(),
material: StandardMaterial {
albedo: math::vec4(0.5, 0.4, 0.3, 1.0).into(),
},
translation: Translation::new(0.0, 0.0, 1.0),
..Default::default()
})
// light
// .add_archetype(LightEntity {

View file

@ -3,6 +3,9 @@ use bevy::prelude::*;
fn main() {
AppBuilder::new()
.add_defaults()
.load_plugin("examples/plugin_loading/example_plugin/target/release/libexample_plugin.so")
.load_plugin(concat!(
env!("CARGO_MANIFEST_DIR"),
"/examples/plugin_loading/example_plugin/target/release/libexample_plugin.so"
))
.run();
}

View file

@ -9,7 +9,7 @@ fn main() {
let tag_registrations = [];
let ser_helper = SerializeImpl::new(&comp_registrations, &tag_registrations);
let serializable = legion::ser::serializable_world(&app.world, &ser_helper);
let serializable = legion::serialize::ser::serializable_world(&app.world, &ser_helper);
let serialized_data = serde_json::to_string(&serializable).unwrap();
println!("{}", serialized_data);
let de_helper = DeserializeImpl::new(
@ -20,13 +20,13 @@ fn main() {
let mut new_world = app.universe.create_world();
let mut deserializer = serde_json::Deserializer::from_str(&serialized_data);
legion::de::deserialize(&mut new_world, &de_helper, &mut deserializer).unwrap();
legion::serialize::de::deserialize(&mut new_world, &de_helper, &mut deserializer).unwrap();
let ser_helper = SerializeImpl::new_with_map(
&comp_registrations,
&tag_registrations,
de_helper.entity_map.into_inner(),
);
let serializable = legion::ser::serializable_world(&new_world, &ser_helper);
let serializable = legion::serialize::ser::serializable_world(&new_world, &ser_helper);
let roundtrip_data = serde_json::to_string(&serializable).unwrap();
println!("{}", roundtrip_data);
assert_eq!(roundtrip_data, serialized_data);
@ -39,7 +39,7 @@ pub struct Test {
pub y: f32,
}
fn setup(world: &mut World) {
fn setup(world: &mut World, _resources: &mut Resources) {
// plane
world.insert((), vec![(Test { x: 3.0, y: 4.0 },)]);
}

View file

@ -4,12 +4,12 @@ fn main() {
AppBuilder::new().add_defaults().setup_world(setup).run();
}
fn setup(world: &mut World) {
fn setup(world: &mut World, resources: &mut Resources) {
let cube = Mesh::load(MeshType::Cube);
let plane = Mesh::load(MeshType::Plane { size: 10.0 });
let (cube_handle, plane_handle) = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
(mesh_storage.add(cube), mesh_storage.add(plane))
};

View file

@ -61,12 +61,12 @@ fn build_print_status_system() -> Box<dyn Schedulable> {
})
}
fn setup(world: &mut World) {
fn setup(world: &mut World, resources: &mut Resources) {
let cube = Mesh::load(MeshType::Cube);
let plane = Mesh::load(MeshType::Plane { size: 10.0 });
let (cube_handle, plane_handle) = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
(mesh_storage.add(cube), mesh_storage.add(plane))
};

View file

@ -4,14 +4,14 @@ fn main() {
AppBuilder::new().add_defaults().setup_world(setup).run();
}
fn setup(world: &mut World) {
fn setup(world: &mut World, resources: &mut Resources) {
let cube_handle = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
mesh_storage.add(Mesh::load(MeshType::Cube))
};
let texture_handle = {
let mut texture_storage = world.resources.get_mut::<AssetStorage<Texture>>().unwrap();
let mut texture_storage = resources.get_mut::<AssetStorage<Texture>>().unwrap();
let texture = Texture::load(TextureType::Png(
concat!(env!("CARGO_MANIFEST_DIR"), "/assets/temp_bevy_logo.png").to_string(),
));

View file

@ -4,10 +4,10 @@ fn main() {
AppBuilder::new().add_defaults().setup_world(setup).run();
}
fn setup(world: &mut World) {
fn setup(world: &mut World, resources: &mut Resources) {
let cube = Mesh::load(MeshType::Cube);
let cube_handle = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
mesh_storage.add(cube)
};

View file

@ -15,6 +15,7 @@ use crate::{
pub struct App {
pub universe: Universe,
pub world: World,
pub resources: Resources,
pub renderer: Option<Box<dyn Renderer>>,
pub render_graph: RenderGraph,
pub schedule: Schedule,
@ -25,6 +26,7 @@ impl App {
universe: Universe,
world: World,
schedule: Schedule,
resources: Resources,
renderer: Option<Box<dyn Renderer>>,
render_graph: RenderGraph,
) -> App {
@ -33,6 +35,7 @@ impl App {
world,
schedule,
renderer,
resources,
render_graph,
}
}
@ -42,16 +45,20 @@ impl App {
}
fn update(&mut self) {
if let Some(mut time) = self.world.resources.get_mut::<Time>() {
if let Some(mut time) = self.resources.get_mut::<Time>() {
time.start();
}
self.schedule.execute(&mut self.world);
self.schedule.execute(&mut self.world, &mut self.resources);
if let Some(ref mut renderer) = self.renderer {
renderer.process_render_graph(&mut self.render_graph, &mut self.world);
renderer.process_render_graph(
&mut self.render_graph,
&mut self.world,
&mut self.resources,
);
}
if let Some(mut time) = self.world.resources.get_mut::<Time>() {
if let Some(mut time) = self.resources.get_mut::<Time>() {
time.stop();
}
}
@ -67,12 +74,12 @@ impl App {
window.set_title("bevy");
window.set_inner_size(winit::dpi::LogicalSize::new(1280, 720));
self.world.resources.insert(window);
self.resources.insert(window);
log::info!("Initializing the example...");
if let Some(ref mut renderer) = self.renderer {
renderer.initialize(&mut self.world, &mut self.render_graph);
renderer.initialize(&mut self.world, &mut self.resources, &mut self.render_graph);
}
log::info!("Entering render loop...");
@ -90,6 +97,7 @@ impl App {
if let Some(ref mut renderer) = self.renderer {
renderer.resize(
&mut self.world,
&mut self.resources,
&mut self.render_graph,
size.width,
size.height,

View file

@ -2,7 +2,7 @@ use crate::{
app::{system_stage, App},
asset::*,
core::Time,
legion::prelude::{Runnable, Schedulable, Schedule, Universe, World},
legion::prelude::{Resources, Runnable, Schedulable, Schedule, Universe, World},
plugin::load_plugin,
render::{
render_graph::{
@ -20,6 +20,7 @@ use std::collections::HashMap;
pub struct AppBuilder {
pub world: World,
pub resources: Resources,
pub universe: Universe,
pub renderer: Option<Box<dyn Renderer>>,
pub render_graph_builder: RenderGraphBuilder,
@ -32,9 +33,11 @@ impl AppBuilder {
pub fn new() -> Self {
let universe = Universe::new();
let world = universe.create_world();
let resources = Resources::default();
AppBuilder {
universe,
world,
resources,
render_graph_builder: RenderGraphBuilder::new(),
renderer: None,
system_stages: HashMap::new(),
@ -67,6 +70,7 @@ impl AppBuilder {
self.universe,
self.world,
schedule_builder.build(),
self.resources,
self.renderer,
self.render_graph_builder.build(),
)
@ -81,8 +85,8 @@ impl AppBuilder {
self
}
pub fn setup_world(mut self, setup: impl Fn(&mut World)) -> Self {
setup(&mut self.world);
pub fn setup_world(mut self, setup: impl Fn(&mut World, &mut Resources)) -> Self {
setup(&mut self.world, &mut self.resources);
self
}
@ -117,14 +121,14 @@ impl AppBuilder {
}
pub fn add_default_resources(mut self) -> Self {
let resources = &mut self.world.resources;
resources.insert(Time::new());
resources.insert(AssetStorage::<Mesh>::new());
resources.insert(AssetStorage::<Texture>::new());
resources.insert(AssetStorage::<Shader>::new());
resources.insert(AssetStorage::<PipelineDescriptor>::new());
resources.insert(ShaderPipelineAssignments::new());
resources.insert(CompiledShaderMap::new());
self.resources.insert(Time::new());
self.resources.insert(AssetStorage::<Mesh>::new());
self.resources.insert(AssetStorage::<Texture>::new());
self.resources.insert(AssetStorage::<Shader>::new());
self.resources
.insert(AssetStorage::<PipelineDescriptor>::new());
self.resources.insert(ShaderPipelineAssignments::new());
self.resources.insert(CompiledShaderMap::new());
self
}
@ -166,15 +170,10 @@ impl AppBuilder {
) -> Self {
{
let mut pipeline_storage = self
.world
.resources
.get_mut::<AssetStorage<PipelineDescriptor>>()
.unwrap();
let mut shader_storage = self
.world
.resources
.get_mut::<AssetStorage<Shader>>()
.unwrap();
let mut shader_storage = self.resources.get_mut::<AssetStorage<Shader>>().unwrap();
self.render_graph_builder = setup(
self.render_graph_builder,
&mut pipeline_storage,

View file

@ -8,7 +8,7 @@ pub use world_builder::*;
use bevy_transform::prelude::Children;
use legion::{
prelude::{Entity, World},
system::SubWorld,
systems::SubWorld,
};
pub fn run_on_hierarchy<T>(

View file

@ -14,7 +14,9 @@ pub use bevy_transform::prelude::*;
pub use glam as math;
pub use legion::{
prelude::*,
schedule::{Builder, Schedulable},
system::{SubWorld, SystemBuilder},
systems::{
schedule::{Builder, Schedulable},
SubWorld, SystemBuilder,
},
};
pub use math::{Mat3, Mat4, Quat, Vec2, Vec3, Vec4};

View file

@ -3,19 +3,21 @@ use crate::{
asset::Handle,
render::render_graph::{pipeline::PipelineDescriptor, RenderPass},
};
use legion::prelude::World;
use legion::prelude::{Resources, World};
// A set of draw calls. ex: get + draw meshes, get + draw instanced meshes, draw ui meshes, etc
pub trait DrawTarget {
fn draw(
&self,
world: &World,
resources: &Resources,
render_pass: &mut dyn RenderPass,
pipeline_handle: Handle<PipelineDescriptor>,
);
fn setup(
&mut self,
world: &World,
world: &Resources,
renderer: &mut dyn Renderer,
pipeline_handle: Handle<PipelineDescriptor>,
);

View file

@ -14,11 +14,11 @@ impl DrawTarget for AssignedMeshesDrawTarget {
fn draw(
&self,
world: &World,
resources: &Resources,
render_pass: &mut dyn RenderPass,
pipeline_handle: Handle<PipelineDescriptor>,
) {
let shader_pipeline_assignments =
world.resources.get::<ShaderPipelineAssignments>().unwrap();
let shader_pipeline_assignments = resources.get::<ShaderPipelineAssignments>().unwrap();
let mut current_mesh_handle = None;
let mut current_mesh_index_len = 0;
@ -66,18 +66,15 @@ impl DrawTarget for AssignedMeshesDrawTarget {
fn setup(
&mut self,
world: &World,
resources: &Resources,
renderer: &mut dyn crate::render::render_graph::Renderer,
pipeline_handle: Handle<PipelineDescriptor>,
) {
let shader_pipeline_assignments =
world.resources.get::<ShaderPipelineAssignments>().unwrap();
let shader_pipeline_assignments = resources.get::<ShaderPipelineAssignments>().unwrap();
let assigned_entities = shader_pipeline_assignments
.assignments
.get(&pipeline_handle);
let pipeline_storage = world
.resources
.get::<AssetStorage<PipelineDescriptor>>()
.unwrap();
let pipeline_storage = resources.get::<AssetStorage<PipelineDescriptor>>().unwrap();
let pipeline_descriptor = pipeline_storage.get(&pipeline_handle).unwrap();
if let Some(assigned_entities) = assigned_entities {
for entity in assigned_entities.iter() {

View file

@ -16,6 +16,7 @@ impl DrawTarget for MeshesDrawTarget {
fn draw(
&self,
world: &World,
_resources: &Resources,
render_pass: &mut dyn RenderPass,
_pipeline_handle: Handle<PipelineDescriptor>,
) {
@ -59,6 +60,7 @@ impl DrawTarget for MeshesDrawTarget {
fn setup(
&mut self,
_world: &World,
_resources: &Resources,
_renderer: &mut dyn crate::render::render_graph::Renderer,
_pipeline_handle: Handle<PipelineDescriptor>,
) {

View file

@ -19,6 +19,7 @@ impl DrawTarget for UiDrawTarget {
fn draw(
&self,
_world: &World,
_resources: &Resources,
render_pass: &mut dyn RenderPass,
_pipeline_handle: Handle<PipelineDescriptor>,
) {
@ -48,12 +49,16 @@ impl DrawTarget for UiDrawTarget {
render_pass.set_index_buffer(self.mesh_index_buffer.unwrap(), 0);
render_pass.set_vertex_buffer(0, self.mesh_vertex_buffer.unwrap(), 0);
render_pass.set_vertex_buffer(1, ui_instances_buffer, 0);
render_pass.draw_indexed(0..self.mesh_index_length as u32, 0, 0..(index_count.unwrap() as u32));
render_pass.draw_indexed(
0..self.mesh_index_length as u32,
0,
0..(index_count.unwrap() as u32),
);
}
fn setup(
&mut self,
world: &World,
_world: &World,
resources: &Resources,
renderer: &mut dyn crate::render::render_graph::Renderer,
_pipeline_handle: Handle<PipelineDescriptor>,
) {
@ -75,7 +80,7 @@ impl DrawTarget for UiDrawTarget {
if let ResourceInfo::InstanceBuffer { mesh_id, .. } =
renderer.get_resource_info(ui_instances_buffer).unwrap()
{
let mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
let mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
if let Some(mesh_asset) = mesh_storage.get_id(*mesh_id) {
self.mesh_vertex_buffer = Some(renderer.create_buffer_with_data(
mesh_asset.vertices.as_bytes(),

View file

@ -1,6 +1,4 @@
use super::{
DrawTarget, PassDescriptor, PipelineDescriptor, ResourceProvider, TextureDescriptor,
};
use super::{DrawTarget, PassDescriptor, PipelineDescriptor, ResourceProvider, TextureDescriptor};
use crate::asset::{AssetStorage, Handle};
use std::collections::{HashMap, HashSet};

View file

@ -95,18 +95,19 @@ fn try_compiling_shader_with_macros(
Some(compiled_shader_handle)
}
}
pub fn update_shader_assignments(world: &mut World, render_graph: &mut RenderGraph) {
pub fn update_shader_assignments(
world: &mut World,
resources: &mut Resources,
render_graph: &mut RenderGraph,
) {
// PERF: this seems like a lot of work for things that don't change that often.
// lots of string + hashset allocations. sees uniform_resource_provider for more context
{
let mut shader_pipeline_assignments = world
.resources
.get_mut::<ShaderPipelineAssignments>()
.unwrap();
let mut compiled_shader_map = world.resources.get_mut::<CompiledShaderMap>().unwrap();
let mut shader_storage = world.resources.get_mut::<AssetStorage<Shader>>().unwrap();
let mut pipeline_descriptor_storage = world
.resources
let mut shader_pipeline_assignments =
resources.get_mut::<ShaderPipelineAssignments>().unwrap();
let mut compiled_shader_map = resources.get_mut::<CompiledShaderMap>().unwrap();
let mut shader_storage = resources.get_mut::<AssetStorage<Shader>>().unwrap();
let mut pipeline_descriptor_storage = resources
.get_mut::<AssetStorage<PipelineDescriptor>>()
.unwrap();

View file

@ -9,15 +9,26 @@ use crate::{
use std::ops::Range;
pub trait Renderer {
fn initialize(&mut self, world: &mut World, render_graph: &mut RenderGraph);
fn initialize(
&mut self,
world: &mut World,
resources: &mut Resources,
render_graph: &mut RenderGraph,
);
fn resize(
&mut self,
world: &mut World,
resources: &mut Resources,
render_graph: &mut RenderGraph,
width: u32,
height: u32,
);
fn process_render_graph(&mut self, render_graph: &mut RenderGraph, world: &mut World);
fn process_render_graph(
&mut self,
render_graph: &mut RenderGraph,
world: &mut World,
resources: &mut Resources,
);
// TODO: swap out wgpu::BufferUsage for non-wgpu type
fn create_buffer_with_data(
&mut self,

View file

@ -295,6 +295,7 @@ impl WgpuRenderer {
pub fn initialize_resource_providers(
&mut self,
world: &mut World,
resources: &mut Resources,
render_graph: &mut RenderGraph,
) {
self.encoder = Some(
@ -302,7 +303,7 @@ impl WgpuRenderer {
.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }),
);
for resource_provider in render_graph.resource_providers.iter_mut() {
resource_provider.initialize(self, world);
resource_provider.initialize(self, world, resources);
}
// consume current encoder
@ -312,9 +313,14 @@ impl WgpuRenderer {
}
impl Renderer for WgpuRenderer {
fn initialize(&mut self, world: &mut World, render_graph: &mut RenderGraph) {
fn initialize(
&mut self,
world: &mut World,
resources: &mut Resources,
render_graph: &mut RenderGraph,
) {
let (surface, window_size) = {
let window = world.resources.get::<winit::window::Window>().unwrap();
let window = resources.get::<winit::window::Window>().unwrap();
let surface = wgpu::Surface::create(window.deref());
let window_size = window.inner_size();
(surface, window_size)
@ -322,14 +328,21 @@ impl Renderer for WgpuRenderer {
self.surface = Some(surface);
self.initialize_resource_providers(world, render_graph);
self.initialize_resource_providers(world, resources, render_graph);
self.resize(world, render_graph, window_size.width, window_size.height);
self.resize(
world,
resources,
render_graph,
window_size.width,
window_size.height,
);
}
fn resize(
&mut self,
world: &mut World,
resources: &mut Resources,
render_graph: &mut RenderGraph,
width: u32,
height: u32,
@ -345,9 +358,9 @@ impl Renderer for WgpuRenderer {
.create_swap_chain(self.surface.as_ref().unwrap(), &self.swap_chain_descriptor);
// WgpuRenderer can't own swap_chain without creating lifetime ergonomics issues, so lets just store it in World.
world.resources.insert(swap_chain);
resources.insert(swap_chain);
for resource_provider in render_graph.resource_providers.iter_mut() {
resource_provider.resize(self, world, width, height);
resource_provider.resize(self, world, resources, width, height);
}
// consume current encoder
@ -355,7 +368,12 @@ impl Renderer for WgpuRenderer {
self.queue.submit(&[command_buffer]);
}
fn process_render_graph(&mut self, render_graph: &mut RenderGraph, world: &mut World) {
fn process_render_graph(
&mut self,
render_graph: &mut RenderGraph,
world: &mut World,
resources: &mut Resources,
) {
// TODO: this self.encoder handoff is a bit gross, but its here to give resource providers access to buffer copies without
// exposing the wgpu renderer internals to ResourceProvider traits. if this can be made cleaner that would be pretty cool.
self.encoder = Some(
@ -364,10 +382,10 @@ impl Renderer for WgpuRenderer {
);
for resource_provider in render_graph.resource_providers.iter_mut() {
resource_provider.update(self, world);
resource_provider.update(self, world, resources);
}
update_shader_assignments(world, render_graph);
update_shader_assignments(world, resources, render_graph);
for (name, texture_descriptor) in render_graph.queued_textures.drain(..) {
let resource = self.create_texture(&texture_descriptor, None);
@ -378,7 +396,7 @@ impl Renderer for WgpuRenderer {
let mut encoder = self.encoder.take().unwrap();
let mut swap_chain = world.resources.get_mut::<wgpu::SwapChain>().unwrap();
let mut swap_chain = resources.get_mut::<wgpu::SwapChain>().unwrap();
let frame = swap_chain
.get_next_texture()
.expect("Timeout when acquiring next swap chain texture");
@ -386,11 +404,10 @@ impl Renderer for WgpuRenderer {
// self.setup_dynamic_entity_shader_uniforms(world, render_graph, &mut encoder);
// setup, pipelines, bind groups, and resources
let mut pipeline_storage = world
.resources
let mut pipeline_storage = resources
.get_mut::<AssetStorage<PipelineDescriptor>>()
.unwrap();
let shader_storage = world.resources.get::<AssetStorage<Shader>>().unwrap();
let shader_storage = resources.get::<AssetStorage<Shader>>().unwrap();
for pipeline_descriptor_handle in render_graph.pipeline_descriptors.iter() {
let pipeline_descriptor = pipeline_storage
@ -438,7 +455,7 @@ impl Renderer for WgpuRenderer {
for draw_target_name in pipeline_descriptor.draw_targets.iter() {
let draw_target =
render_graph.draw_targets.get_mut(draw_target_name).unwrap();
draw_target.setup(world, self, *pass_pipeline);
draw_target.setup(world, resources, self, *pass_pipeline);
}
}
}
@ -467,7 +484,7 @@ impl Renderer for WgpuRenderer {
for draw_target_name in pipeline_descriptor.draw_targets.iter() {
let draw_target = render_graph.draw_targets.get(draw_target_name).unwrap();
draw_target.draw(world, &mut wgpu_render_pass, *pass_pipeline);
draw_target.draw(world, resources, &mut wgpu_render_pass, *pass_pipeline);
}
}
}

View file

@ -2,7 +2,14 @@ use crate::render::render_graph::Renderer;
use legion::prelude::*;
pub trait ResourceProvider {
fn initialize(&mut self, renderer: &mut dyn Renderer, world: &mut World);
fn update(&mut self, renderer: &mut dyn Renderer, world: &mut World);
fn resize(&mut self, renderer: &mut dyn Renderer, world: &mut World, width: u32, height: u32);
fn initialize(&mut self, renderer: &mut dyn Renderer, world: &mut World, resources: &Resources);
fn update(&mut self, renderer: &mut dyn Renderer, world: &mut World, resources: &Resources);
fn resize(
&mut self,
renderer: &mut dyn Renderer,
world: &mut World,
resources: &Resources,
width: u32,
height: u32,
);
}

View file

@ -12,7 +12,12 @@ pub struct Camera2dResourceProvider {
}
impl ResourceProvider for Camera2dResourceProvider {
fn initialize(&mut self, renderer: &mut dyn Renderer, _world: &mut World) {
fn initialize(
&mut self,
renderer: &mut dyn Renderer,
_world: &mut World,
_resources: &Resources,
) {
let buffer = renderer.create_buffer(
std::mem::size_of::<[[f32; 4]; 4]>() as u64,
wgpu::BufferUsage::COPY_DST | wgpu::BufferUsage::UNIFORM,
@ -24,8 +29,16 @@ impl ResourceProvider for Camera2dResourceProvider {
self.camera_buffer = Some(buffer);
}
fn update(&mut self, _renderer: &mut dyn Renderer, _world: &mut World) {}
fn resize(&mut self, renderer: &mut dyn Renderer, world: &mut World, width: u32, height: u32) {
fn update(&mut self, _renderer: &mut dyn Renderer, _world: &mut World, _resources: &Resources) {
}
fn resize(
&mut self,
renderer: &mut dyn Renderer,
world: &mut World,
_resources: &Resources,
width: u32,
height: u32,
) {
let matrix_size = std::mem::size_of::<[[f32; 4]; 4]>();
for (mut camera, _) in <(Write<Camera>, Read<ActiveCamera2d>)>::query().iter_mut(world) {
camera.update(width, height);

View file

@ -13,7 +13,12 @@ pub struct CameraResourceProvider {
}
impl ResourceProvider for CameraResourceProvider {
fn initialize(&mut self, renderer: &mut dyn Renderer, _world: &mut World) {
fn initialize(
&mut self,
renderer: &mut dyn Renderer,
_world: &mut World,
_resources: &Resources,
) {
let buffer = renderer.create_buffer(
std::mem::size_of::<[[f32; 4]; 4]>() as u64,
wgpu::BufferUsage::COPY_DST | wgpu::BufferUsage::UNIFORM,
@ -25,8 +30,16 @@ impl ResourceProvider for CameraResourceProvider {
self.camera_buffer = Some(buffer);
}
fn update(&mut self, _renderer: &mut dyn Renderer, _world: &mut World) {}
fn resize(&mut self, renderer: &mut dyn Renderer, world: &mut World, width: u32, height: u32) {
fn update(&mut self, _renderer: &mut dyn Renderer, _world: &mut World, _resources: &Resources) {
}
fn resize(
&mut self,
renderer: &mut dyn Renderer,
world: &mut World,
_resources: &Resources,
width: u32,
height: u32,
) {
let matrix_size = std::mem::size_of::<[[f32; 4]; 4]>();
for (mut camera, local_to_world, _) in
<(Write<Camera>, Read<LocalToWorld>, Read<ActiveCamera>)>::query().iter_mut(world)

View file

@ -2,6 +2,7 @@ use crate::{
prelude::World,
render::render_graph::{Renderer, ResourceProvider, TextureDescriptor},
};
use legion::prelude::Resources;
pub struct FrameTextureResourceProvider {
pub name: String,
@ -16,8 +17,8 @@ impl FrameTextureResourceProvider {
}
}
pub fn update(&mut self, renderer: &mut dyn Renderer, world: &World) {
let window = world.resources.get::<winit::window::Window>().unwrap();
pub fn update(&mut self, renderer: &mut dyn Renderer, _world: &World, resources: &Resources) {
let window = resources.get::<winit::window::Window>().unwrap();
let window_size = window.inner_size();
self.descriptor.size.width = window_size.width;
self.descriptor.size.height = window_size.height;
@ -37,19 +38,26 @@ impl FrameTextureResourceProvider {
}
impl ResourceProvider for FrameTextureResourceProvider {
fn initialize(&mut self, renderer: &mut dyn Renderer, world: &mut World) {
self.update(renderer, world);
fn initialize(
&mut self,
renderer: &mut dyn Renderer,
world: &mut World,
resources: &Resources,
) {
self.update(renderer, world, resources);
}
fn update(&mut self, _renderer: &mut dyn Renderer, _world: &mut World) {}
fn update(&mut self, _renderer: &mut dyn Renderer, _world: &mut World, _resources: &Resources) {
}
fn resize(
&mut self,
renderer: &mut dyn Renderer,
world: &mut World,
resources: &Resources,
_width: u32,
_height: u32,
) {
self.update(renderer, world);
self.update(renderer, world, resources);
}
}

View file

@ -33,7 +33,12 @@ impl LightResourceProvider {
}
impl ResourceProvider for LightResourceProvider {
fn initialize(&mut self, renderer: &mut dyn Renderer, _world: &mut World) {
fn initialize(
&mut self,
renderer: &mut dyn Renderer,
_world: &mut World,
_resources: &Resources,
) {
let light_uniform_size = (std::mem::size_of::<LightCount>()
+ self.max_lights * std::mem::size_of::<LightRaw>())
as wgpu::BufferAddress;
@ -48,7 +53,7 @@ impl ResourceProvider for LightResourceProvider {
self.light_buffer = Some(buffer);
}
fn update(&mut self, renderer: &mut dyn Renderer, world: &mut World) {
fn update(&mut self, renderer: &mut dyn Renderer, world: &mut World, _resources: &Resources) {
if self.lights_are_dirty {
let light_query = <(Read<Light>, Read<LocalToWorld>, Read<Translation>)>::query();
let light_count = light_query.iter(world).count();
@ -113,6 +118,7 @@ impl ResourceProvider for LightResourceProvider {
&mut self,
_renderer: &mut dyn Renderer,
_world: &mut World,
_resources: &Resources,
_width: u32,
_height: u32,
) {

View file

@ -35,10 +35,16 @@ impl MeshResourceProvider {
}
impl ResourceProvider for MeshResourceProvider {
fn initialize(&mut self, _renderer: &mut dyn Renderer, _world: &mut World) {}
fn initialize(
&mut self,
_renderer: &mut dyn Renderer,
_world: &mut World,
_resources: &Resources,
) {
}
fn update(&mut self, renderer: &mut dyn Renderer, world: &mut World) {
let mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
fn update(&mut self, renderer: &mut dyn Renderer, world: &mut World, resources: &Resources) {
let mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
for (mesh_handle, _renderable) in self.mesh_query.iter(world) {
if let None = renderer
.get_render_resources()
@ -65,6 +71,7 @@ impl ResourceProvider for MeshResourceProvider {
&mut self,
_renderer: &mut dyn Renderer,
_world: &mut World,
_resources: &Resources,
_width: u32,
_height: u32,
) {

View file

@ -90,9 +90,14 @@ impl UiResourceProvider {
}
impl ResourceProvider for UiResourceProvider {
fn initialize(&mut self, _renderer: &mut dyn Renderer, world: &mut World) {
fn initialize(
&mut self,
_renderer: &mut dyn Renderer,
_world: &mut World,
resources: &Resources,
) {
// self.update(renderer, world);
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
let quad = Mesh::load(MeshType::Quad {
north_west: math::vec2(-0.5, 0.5),
north_east: math::vec2(0.5, 0.5),
@ -102,7 +107,7 @@ impl ResourceProvider for UiResourceProvider {
self.quad = Some(mesh_storage.add(quad));
}
fn update(&mut self, renderer: &mut dyn Renderer, world: &mut World) {
fn update(&mut self, renderer: &mut dyn Renderer, world: &mut World, _resources: &Resources) {
self.update(renderer, world);
}
@ -110,6 +115,7 @@ impl ResourceProvider for UiResourceProvider {
&mut self,
_renderer: &mut dyn Renderer,
_world: &mut World,
_resources: &Resources,
_width: u32,
_height: u32,
) {

View file

@ -39,11 +39,16 @@ impl<T> ResourceProvider for UniformResourceProvider<T>
where
T: AsUniforms + Send + Sync + 'static,
{
fn initialize(&mut self, renderer: &mut dyn Renderer, world: &mut World) {
self.update(renderer, world);
fn initialize(
&mut self,
renderer: &mut dyn Renderer,
world: &mut World,
resources: &Resources,
) {
self.update(renderer, world, resources);
}
fn update(&mut self, renderer: &mut dyn Renderer, world: &mut World) {
fn update(&mut self, renderer: &mut dyn Renderer, world: &mut World, resources: &Resources) {
let query = <(Read<T>, Read<Renderable>)>::query();
// TODO: this breaks down in multiple ways:
// (SOLVED 1) resource_info will be set after the first run so this won't update.
@ -79,7 +84,7 @@ where
BindType::SampledTexture { .. } => {
let texture_handle =
uniforms.get_uniform_texture(&uniform_info.name).unwrap();
let storage = world.resources.get::<AssetStorage<Texture>>().unwrap();
let storage = resources.get::<AssetStorage<Texture>>().unwrap();
let texture = storage.get(&texture_handle).unwrap();
let resource = match renderer
.get_render_resources()
@ -102,7 +107,7 @@ where
BindType::Sampler { .. } => {
let texture_handle =
uniforms.get_uniform_texture(&uniform_info.name).unwrap();
let storage = world.resources.get::<AssetStorage<Texture>>().unwrap();
let storage = resources.get::<AssetStorage<Texture>>().unwrap();
let texture = storage.get(&texture_handle).unwrap();
let resource = match renderer
.get_render_resources()
@ -225,6 +230,7 @@ where
&mut self,
_renderer: &mut dyn Renderer,
_world: &mut World,
_resources: &Resources,
_width: u32,
_height: u32,
) {

View file

@ -13,7 +13,7 @@ use serde::{
Deserialize, Deserializer, Serialize, Serializer,
};
use std::{
any::type_name, cell::RefCell, collections::HashMap, iter::FromIterator, marker::PhantomData,
any::TypeId, cell::RefCell, collections::HashMap, iter::FromIterator, marker::PhantomData,
ptr::NonNull,
};
use type_uuid::TypeUuid;
@ -101,7 +101,7 @@ impl<'de, 'a, T: for<'b> Deserialize<'b> + 'static> Visitor<'de>
#[derive(Clone)]
pub struct TagRegistration {
uuid: type_uuid::Bytes,
ty: String,
ty: TypeId,
tag_serialize_fn: fn(&TagStorage, &mut dyn FnMut(&dyn erased_serde::Serialize)),
tag_deserialize_fn: fn(
deserializer: &mut dyn erased_serde::Deserializer,
@ -123,7 +123,7 @@ impl TagRegistration {
>() -> Self {
Self {
uuid: T::UUID,
ty: type_name::<T>().to_string(),
ty: TypeId::of::<T>(),
tag_serialize_fn: |tag_storage, serialize_fn| {
// it's safe because we know this is the correct type due to lookup
let slice = unsafe { tag_storage.data_slice::<T>() };
@ -150,7 +150,7 @@ impl TagRegistration {
#[derive(Clone)]
pub struct ComponentRegistration {
uuid: type_uuid::Bytes,
ty: String,
ty: TypeId,
comp_serialize_fn: fn(&ComponentResourceSet, &mut dyn FnMut(&dyn erased_serde::Serialize)),
comp_deserialize_fn: fn(
deserializer: &mut dyn erased_serde::Deserializer,
@ -164,7 +164,7 @@ impl ComponentRegistration {
{
Self {
uuid: T::UUID,
ty: type_name::<T>().to_string(),
ty: TypeId::of::<T>(),
comp_serialize_fn: |comp_storage, serialize_fn| {
// it's safe because we know this is the correct type due to lookup
let slice = unsafe { comp_storage.data_slice::<T>() };
@ -192,8 +192,8 @@ struct SerializedArchetypeDescription {
}
pub struct SerializeImpl {
pub tag_types: HashMap<String, TagRegistration>,
pub comp_types: HashMap<String, ComponentRegistration>,
pub tag_types: HashMap<TypeId, TagRegistration>,
pub comp_types: HashMap<TypeId, ComponentRegistration>,
pub entity_map: RefCell<HashMap<Entity, uuid::Bytes>>,
}
@ -240,12 +240,12 @@ impl SerializeImpl {
}
}
impl legion::ser::WorldSerializer for SerializeImpl {
impl legion::serialize::ser::WorldSerializer for SerializeImpl {
fn can_serialize_tag(&self, ty: &TagTypeId, _meta: &TagMeta) -> bool {
self.tag_types.get(ty.0).is_some()
self.tag_types.get(&ty.0).is_some()
}
fn can_serialize_component(&self, ty: &ComponentTypeId, _meta: &ComponentMeta) -> bool {
self.comp_types.get(ty.0).is_some()
self.comp_types.get(&ty.0).is_some()
}
fn serialize_archetype_description<S: Serializer>(
&self,
@ -255,13 +255,13 @@ impl legion::ser::WorldSerializer for SerializeImpl {
let tags_to_serialize = archetype_desc
.tags()
.iter()
.filter_map(|(ty, _)| self.tag_types.get(ty.0))
.filter_map(|(ty, _)| self.tag_types.get(&ty.0))
.map(|reg| reg.uuid)
.collect::<Vec<_>>();
let components_to_serialize = archetype_desc
.components()
.iter()
.filter_map(|(ty, _)| self.comp_types.get(ty.0))
.filter_map(|(ty, _)| self.comp_types.get(&ty.0))
.map(|reg| reg.uuid)
.collect::<Vec<_>>();
SerializedArchetypeDescription {
@ -277,7 +277,7 @@ impl legion::ser::WorldSerializer for SerializeImpl {
_component_meta: &ComponentMeta,
components: &ComponentResourceSet,
) -> Result<S::Ok, S::Error> {
if let Some(reg) = self.comp_types.get(component_type.0) {
if let Some(reg) = self.comp_types.get(&component_type.0) {
let result = RefCell::new(None);
let serializer = RefCell::new(Some(serializer));
{
@ -303,7 +303,7 @@ impl legion::ser::WorldSerializer for SerializeImpl {
_tag_meta: &TagMeta,
tags: &TagStorage,
) -> Result<S::Ok, S::Error> {
if let Some(reg) = self.tag_types.get(tag_type.0) {
if let Some(reg) = self.tag_types.get(&tag_type.0) {
let result = RefCell::new(None);
let serializer = RefCell::new(Some(serializer));
{
@ -337,8 +337,8 @@ impl legion::ser::WorldSerializer for SerializeImpl {
}
pub struct DeserializeImpl {
pub tag_types: HashMap<String, TagRegistration>,
pub comp_types: HashMap<String, ComponentRegistration>,
pub tag_types: HashMap<TypeId, TagRegistration>,
pub comp_types: HashMap<TypeId, ComponentRegistration>,
pub tag_types_by_uuid: HashMap<type_uuid::Bytes, TagRegistration>,
pub comp_types_by_uuid: HashMap<type_uuid::Bytes, ComponentRegistration>,
pub entity_map: RefCell<HashMap<uuid::Bytes, Entity>>,
@ -346,8 +346,8 @@ pub struct DeserializeImpl {
impl DeserializeImpl {
pub fn new(
component_types: HashMap<String, ComponentRegistration>,
tag_types: HashMap<String, TagRegistration>,
component_types: HashMap<TypeId, ComponentRegistration>,
tag_types: HashMap<TypeId, TagRegistration>,
entity_map: RefCell<HashMap<Entity, uuid::Bytes>>,
) -> Self {
DeserializeImpl {
@ -372,7 +372,7 @@ impl DeserializeImpl {
}
}
impl legion::de::WorldDeserializer for DeserializeImpl {
impl legion::serialize::de::WorldDeserializer for DeserializeImpl {
fn deserialize_archetype_description<'de, D: Deserializer<'de>>(
&self,
deserializer: D,
@ -399,7 +399,7 @@ impl legion::de::WorldDeserializer for DeserializeImpl {
_component_meta: &ComponentMeta,
get_next_storage_fn: &mut dyn FnMut() -> Option<(NonNull<u8>, usize)>,
) -> Result<(), <D as Deserializer<'de>>::Error> {
if let Some(reg) = self.comp_types.get(component_type.0) {
if let Some(reg) = self.comp_types.get(&component_type.0) {
let mut erased = erased_serde::Deserializer::erase(deserializer);
(reg.comp_deserialize_fn)(&mut erased, get_next_storage_fn)
.map_err(<<D as serde::Deserializer<'de>>::Error as serde::de::Error>::custom)?;
@ -415,7 +415,7 @@ impl legion::de::WorldDeserializer for DeserializeImpl {
_tag_meta: &TagMeta,
tags: &mut TagStorage,
) -> Result<(), <D as Deserializer<'de>>::Error> {
if let Some(reg) = self.tag_types.get(tag_type.0) {
if let Some(reg) = self.tag_types.get(&tag_type.0) {
let mut erased = erased_serde::Deserializer::erase(deserializer);
(reg.tag_deserialize_fn)(&mut erased, tags)
.map_err(<<D as serde::Deserializer<'de>>::Error as serde::de::Error>::custom)?;