cargo fmt

This commit is contained in:
Carter Anderson 2020-02-22 15:02:01 -08:00
parent 7eb99f3eb5
commit f84e71d051
23 changed files with 190 additions and 105 deletions

View file

@ -1,10 +1,7 @@
use bevy::prelude::*;
fn main() {
AppBuilder::new()
.add_defaults()
.setup_world(setup)
.run();
AppBuilder::new().add_defaults().setup_world(setup).run();
}
#[allow(dead_code)]

View file

@ -2,10 +2,7 @@ use bevy::{prelude::*, serialization::*};
use serde::{Deserialize, Serialize};
use type_uuid::TypeUuid;
fn main() {
let app = AppBuilder::new()
.add_defaults()
.setup_world(setup)
.build();
let app = AppBuilder::new().add_defaults().setup_world(setup).build();
let comp_registrations = [ComponentRegistration::of::<Test>()];

View file

@ -25,7 +25,8 @@ fn build_move_system() -> Box<dyn Schedulable> {
-time.delta_seconds,
time.delta_seconds,
0.0,
)).into();
))
.into();
}
}
})
@ -127,7 +128,8 @@ fn setup(world: &mut World) {
rng.gen_range(0.0, 1.0),
rng.gen_range(0.0, 1.0),
1.0,
).into(),
)
.into(),
},
translation: Translation::new(
rng.gen_range(-50.0, 50.0),

View file

@ -1,5 +1,5 @@
use crate::{
asset::{Texture, Handle},
asset::{Handle, Texture},
math::Vec4,
};
use zerocopy::AsBytes;

View file

@ -1,5 +1,5 @@
pub mod time;
pub mod bytes;
pub mod time;
pub use time::Time;
pub use bytes::GetBytes;
pub use time::Time;

View file

@ -1,5 +1,9 @@
use crate::ecs::EntityArchetype;
use legion::{world::{TagSet, TagLayout, IntoComponentSource}, prelude::*, filter::{Filter, ChunksetFilterData}};
use legion::{
filter::{ChunksetFilterData, Filter},
prelude::*,
world::{IntoComponentSource, TagLayout, TagSet},
};
pub trait WorldBuilderSource {
fn build(&mut self) -> WorldBuilder;
@ -48,7 +52,7 @@ impl<'a> WorldBuilder<'a> {
self
}
pub fn add_entities<T,C>(self, tags: T, components: C) -> Self
pub fn add_entities<T, C>(self, tags: T, components: C) -> Self
where
T: TagSet + TagLayout + for<'b> Filter<ChunksetFilterData<'b>>,
C: IntoComponentSource,

View file

@ -5,8 +5,8 @@ pub use crate::{
ecs,
ecs::{default_archetypes::*, EntityArchetype, WorldBuilder, WorldBuilderSource},
render::{
ActiveCamera, ActiveCamera2d, Camera, CameraType, Instanced, Light,
render_graph::{StandardMaterial, Renderable, ShaderDefSuffixProvider}, ColorSource
render_graph::{Renderable, ShaderDefSuffixProvider, StandardMaterial},
ActiveCamera, ActiveCamera2d, Camera, CameraType, ColorSource, Instanced, Light,
},
ui::{Anchors, Margins, Node},
};

View file

@ -1,7 +1,8 @@
use crate::{
asset::{Handle, Texture},
core::GetBytes,
math::Vec4,
render::render_graph::ShaderDefSuffixProvider, core::GetBytes,
render::render_graph::ShaderDefSuffixProvider,
};
pub enum ColorSource {

View file

@ -8,10 +8,10 @@ mod light;
mod vertex;
pub use camera::*;
pub use color::*;
pub use light::*;
pub use shader::*;
pub use color::*;
pub use vertex::Vertex;
pub struct Instanced;
pub struct Instanced;

View file

@ -1,4 +1,7 @@
use crate::{asset::Handle, render::render_graph::{pipeline::PipelineDescriptor, RenderPass}};
use crate::{
asset::Handle,
render::render_graph::{pipeline::PipelineDescriptor, RenderPass},
};
use legion::prelude::World;
// A set of draw calls. ex: get + draw meshes, get + draw instanced meshes, draw ui meshes, etc

View file

@ -1,8 +1,8 @@
use crate::{
asset::{AssetStorage, Handle, Mesh},
legion::prelude::*,
render::{
render_graph::{resource_name, RenderPass, Renderable, PipelineDescriptor, ShaderPipelineAssignments},
render::render_graph::{
resource_name, PipelineDescriptor, RenderPass, Renderable, ShaderPipelineAssignments,
},
};

View file

@ -2,19 +2,23 @@ use crate::{
asset::{AssetStorage, Handle, Mesh},
legion::prelude::*,
render::{
render_graph::{resource_name, RenderPass, Renderable, PipelineDescriptor},
render_graph::{resource_name, PipelineDescriptor, RenderPass, Renderable},
Instanced,
},
};
use zerocopy::AsBytes;
pub fn meshes_draw_target(world: &World, render_pass: &mut dyn RenderPass, _pipeline_handle: Handle<PipelineDescriptor>) {
pub fn meshes_draw_target(
world: &World,
render_pass: &mut dyn RenderPass,
_pipeline_handle: Handle<PipelineDescriptor>,
) {
let mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
let mut current_mesh_id = None;
let mut current_mesh_index_length = 0;
let mesh_query = <(Read<Handle<Mesh>>, Read<Renderable>)>::query()
.filter(!component::<Instanced>());
let mesh_query =
<(Read<Handle<Mesh>>, Read<Renderable>)>::query().filter(!component::<Instanced>());
for (entity, (mesh, renderable)) in mesh_query.iter_entities(world) {
if !renderable.is_visible {
continue;

View file

@ -1,7 +1,7 @@
mod meshes_draw_target;
mod assigned_meshes_draw_target;
mod meshes_draw_target;
mod ui_draw_target;
pub use meshes_draw_target::*;
pub use assigned_meshes_draw_target::*;
pub use meshes_draw_target::*;
pub use ui_draw_target::*;

View file

@ -1,12 +1,16 @@
use crate::{
asset::{AssetStorage, Mesh, Handle},
asset::{AssetStorage, Handle, Mesh},
legion::prelude::*,
render::render_graph::{resource_name, RenderPass, ResourceInfo, PipelineDescriptor},
render::render_graph::{resource_name, PipelineDescriptor, RenderPass, ResourceInfo},
};
use zerocopy::AsBytes;
pub fn ui_draw_target(world: &World, render_pass: &mut dyn RenderPass, _pipeline_handle: Handle<PipelineDescriptor>) {
pub fn ui_draw_target(
world: &World,
render_pass: &mut dyn RenderPass,
_pipeline_handle: Handle<PipelineDescriptor>,
) {
let mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
// NOTE: this is ugly and borrowing is stupid
let result = {

View file

@ -1,7 +1,11 @@
use crate::{asset::{AssetStorage, Handle}, render::{
render_graph::{BindGroup, PipelineLayout, resource_name},
shader::{Shader, ShaderStages}, Vertex,
}};
use crate::{
asset::{AssetStorage, Handle},
render::{
render_graph::{resource_name, BindGroup, PipelineLayout},
shader::{Shader, ShaderStages},
Vertex,
},
};
#[derive(Clone, Debug)]
pub struct VertexBufferDescriptor {
@ -87,22 +91,25 @@ impl PipelineDescriptor {
}
pub fn get_layout(&self) -> Option<&PipelineLayout> {
match self.layout {
PipelineLayoutType::Reflected(ref layout) => layout.as_ref(),
PipelineLayoutType::Manual(ref layout) => Some(layout),
}
match self.layout {
PipelineLayoutType::Reflected(ref layout) => layout.as_ref(),
PipelineLayoutType::Manual(ref layout) => Some(layout),
}
}
pub fn get_layout_mut(&mut self) -> Option<&mut PipelineLayout> {
match self.layout {
PipelineLayoutType::Reflected(ref mut layout) => layout.as_mut(),
PipelineLayoutType::Manual(ref mut layout) => Some(layout),
}
match self.layout {
PipelineLayoutType::Reflected(ref mut layout) => layout.as_mut(),
PipelineLayoutType::Manual(ref mut layout) => Some(layout),
}
}
}
impl PipelineDescriptor {
pub fn build(shader_storage: &mut AssetStorage<Shader>, vertex_shader: Shader) -> PipelineBuilder {
pub fn build(
shader_storage: &mut AssetStorage<Shader>,
vertex_shader: Shader,
) -> PipelineBuilder {
PipelineBuilder::new(shader_storage, vertex_shader)
}
}
@ -208,23 +215,22 @@ impl<'a> PipelineBuilder<'a> {
}
pub fn with_standard_config(self) -> Self {
self
.with_depth_stencil_state(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth32Float,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE,
stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE,
stencil_read_mask: 0,
stencil_write_mask: 0,
})
.add_color_state(wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8UnormSrgb,
color_blend: wgpu::BlendDescriptor::REPLACE,
alpha_blend: wgpu::BlendDescriptor::REPLACE,
write_mask: wgpu::ColorWrite::ALL,
})
.add_vertex_buffer_descriptor(Vertex::get_vertex_buffer_descriptor())
.add_draw_target(resource_name::draw_target::ASSIGNED_MESHES)
self.with_depth_stencil_state(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth32Float,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE,
stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE,
stencil_read_mask: 0,
stencil_write_mask: 0,
})
.add_color_state(wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8UnormSrgb,
color_blend: wgpu::BlendDescriptor::REPLACE,
alpha_blend: wgpu::BlendDescriptor::REPLACE,
write_mask: wgpu::ColorWrite::ALL,
})
.add_vertex_buffer_descriptor(Vertex::get_vertex_buffer_descriptor())
.add_draw_target(resource_name::draw_target::ASSIGNED_MESHES)
}
}

View file

@ -1,10 +1,7 @@
use crate::{
asset::AssetStorage,
render::{
render_graph::{
resource_name, PipelineDescriptor,
RenderGraphBuilder,
},
render_graph::{resource_name, PipelineDescriptor, RenderGraphBuilder},
shader::{Shader, ShaderStage},
Vertex,
},
@ -32,7 +29,7 @@ impl ForwardFlatPipelineBuilder for RenderGraphBuilder {
)
.with_fragment_shader(Shader::from_glsl(
ShaderStage::Fragment,
include_str!("forward_flat.frag")
include_str!("forward_flat.frag"),
))
.with_rasterization_state(wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Ccw,

View file

@ -2,8 +2,8 @@ use crate::{
asset::AssetStorage,
render::{
render_graph::{
resource_providers::RectData,
PipelineDescriptor, RenderGraphBuilder, VertexBufferDescriptor, resource_name,
resource_name, resource_providers::RectData, PipelineDescriptor, RenderGraphBuilder,
VertexBufferDescriptor,
},
shader::{Shader, ShaderStage},
Vertex,
@ -31,7 +31,7 @@ impl UiPipelineBuilder for RenderGraphBuilder {
)
.with_fragment_shader(Shader::from_glsl(
ShaderStage::Fragment,
include_str!("ui.frag")
include_str!("ui.frag"),
))
.with_rasterization_state(wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Ccw,

View file

@ -1,10 +1,13 @@
use super::PipelineDescriptor;
use crate::{
asset::{AssetStorage, Handle},
render::{render_graph::{RenderGraph, resource_name}, Shader, ShaderSource},
render::{
render_graph::{resource_name, RenderGraph},
Shader, ShaderSource,
},
};
use legion::prelude::*;
use std::collections::{HashMap, HashSet};
use super::PipelineDescriptor;
pub struct Renderable {
pub is_visible: bool,
@ -27,7 +30,8 @@ impl Default for Renderable {
pub struct CompiledShaderMap {
// TODO: need macro hash lookup
pub source_to_compiled: HashMap<Handle<Shader>, Vec<(HashSet<String>, Handle<Shader>)>>,
pub pipeline_to_macro_pipelines: HashMap<Handle<PipelineDescriptor>, Vec<(HashSet<String>, Handle<PipelineDescriptor>)>>,
pub pipeline_to_macro_pipelines:
HashMap<Handle<PipelineDescriptor>, Vec<(HashSet<String>, Handle<PipelineDescriptor>)>>,
}
impl CompiledShaderMap {
@ -51,14 +55,22 @@ impl ShaderPipelineAssignments {
}
}
fn try_compiling_shader_with_macros(compiled_shader_map: &mut CompiledShaderMap, shader_storage: &mut AssetStorage<Shader>, renderable: &Renderable, shader_handle: &Handle<Shader>) -> Option<Handle<Shader>> {
fn try_compiling_shader_with_macros(
compiled_shader_map: &mut CompiledShaderMap,
shader_storage: &mut AssetStorage<Shader>,
renderable: &Renderable,
shader_handle: &Handle<Shader>,
) -> Option<Handle<Shader>> {
if let None = compiled_shader_map.source_to_compiled.get(shader_handle) {
compiled_shader_map
.source_to_compiled
.insert(shader_handle.clone(), Vec::new());
}
let compiled_shaders = compiled_shader_map.source_to_compiled.get_mut(shader_handle).unwrap();
let compiled_shaders = compiled_shader_map
.source_to_compiled
.get_mut(shader_handle)
.unwrap();
let shader = shader_storage.get(shader_handle).unwrap();
// don't produce new shader if the input source is already spriv
@ -66,44 +78,84 @@ fn try_compiling_shader_with_macros(compiled_shader_map: &mut CompiledShaderMap,
return None;
}
if let Some((_shader_defs, compiled_shader)) = compiled_shaders.iter().find(|(shader_defs, _shader)| *shader_defs == renderable.shader_defs) {
if let Some((_shader_defs, compiled_shader)) = compiled_shaders
.iter()
.find(|(shader_defs, _shader)| *shader_defs == renderable.shader_defs)
{
Some(compiled_shader.clone())
} else {
let shader_def_vec = renderable.shader_defs.iter().cloned().collect::<Vec<String>>();
let shader_def_vec = renderable
.shader_defs
.iter()
.cloned()
.collect::<Vec<String>>();
let compiled_shader = shader.get_spirv_shader(Some(&shader_def_vec));
compiled_shaders.push((renderable.shader_defs.clone(), shader_handle.clone()));
let compiled_shader_handle = shader_storage.add(compiled_shader);
Some(compiled_shader_handle)
}
}
pub fn update_shader_assignments(world: &mut World, 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 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.get_mut::<AssetStorage<PipelineDescriptor>>().unwrap();
let mut pipeline_descriptor_storage = world
.resources
.get_mut::<AssetStorage<PipelineDescriptor>>()
.unwrap();
// reset assignments so they are updated every frame
shader_pipeline_assignments.assignments = HashMap::new();
for (entity, renderable) in <Read<Renderable>>::query().iter_entities(world) {
for pipeline_handle in renderable.pipelines.iter() {
if let None = compiled_shader_map.pipeline_to_macro_pipelines.get(pipeline_handle) {
if let None = compiled_shader_map
.pipeline_to_macro_pipelines
.get(pipeline_handle)
{
compiled_shader_map
.pipeline_to_macro_pipelines
.insert(pipeline_handle.clone(), Vec::new());
}
let final_handle = if let Some((_shader_defs, macroed_pipeline_handle)) = compiled_shader_map.pipeline_to_macro_pipelines.get_mut(pipeline_handle).unwrap().iter().find(|(shader_defs, _macroed_pipeline_handle)| *shader_defs == renderable.shader_defs) {
let final_handle = if let Some((_shader_defs, macroed_pipeline_handle)) =
compiled_shader_map
.pipeline_to_macro_pipelines
.get_mut(pipeline_handle)
.unwrap()
.iter()
.find(|(shader_defs, _macroed_pipeline_handle)| {
*shader_defs == renderable.shader_defs
}) {
macroed_pipeline_handle.clone()
} else {
let pipeline_descriptor = pipeline_descriptor_storage.get(pipeline_handle).unwrap();
let pipeline_descriptor =
pipeline_descriptor_storage.get(pipeline_handle).unwrap();
let macroed_pipeline_handle = {
let mut macroed_vertex_handle = try_compiling_shader_with_macros(&mut compiled_shader_map, &mut shader_storage, &renderable, &pipeline_descriptor.shader_stages.vertex);
let mut macroed_fragment_handle = pipeline_descriptor.shader_stages.fragment.as_ref().map(|fragment| try_compiling_shader_with_macros(&mut compiled_shader_map, &mut shader_storage, &renderable, fragment));
let mut macroed_vertex_handle = try_compiling_shader_with_macros(
&mut compiled_shader_map,
&mut shader_storage,
&renderable,
&pipeline_descriptor.shader_stages.vertex,
);
let mut macroed_fragment_handle = pipeline_descriptor
.shader_stages
.fragment
.as_ref()
.map(|fragment| {
try_compiling_shader_with_macros(
&mut compiled_shader_map,
&mut shader_storage,
&renderable,
fragment,
)
});
if macroed_vertex_handle.is_some() || macroed_fragment_handle.is_some() {
let mut macroed_pipeline = pipeline_descriptor.clone();
@ -115,26 +167,41 @@ pub fn update_shader_assignments(world: &mut World, render_graph: &mut RenderGra
macroed_pipeline.shader_stages.fragment = fragment;
}
let macroed_pipeline_handle = pipeline_descriptor_storage.add(macroed_pipeline);
let macroed_pipeline_handle =
pipeline_descriptor_storage.add(macroed_pipeline);
// TODO: get correct pass name
render_graph.add_pipeline(resource_name::pass::MAIN, macroed_pipeline_handle.clone());
render_graph.add_pipeline(
resource_name::pass::MAIN,
macroed_pipeline_handle.clone(),
);
macroed_pipeline_handle
} else {
pipeline_handle.clone()
}
};
let macro_pipelines = compiled_shader_map.pipeline_to_macro_pipelines.get_mut(pipeline_handle).unwrap();
macro_pipelines.push((renderable.shader_defs.clone(), macroed_pipeline_handle.clone()));
let macro_pipelines = compiled_shader_map
.pipeline_to_macro_pipelines
.get_mut(pipeline_handle)
.unwrap();
macro_pipelines.push((
renderable.shader_defs.clone(),
macroed_pipeline_handle.clone(),
));
macroed_pipeline_handle
};
// TODO: this will break down if pipeline layout changes. fix this with "autolayout"
if let None = shader_pipeline_assignments.assignments.get(&final_handle) {
shader_pipeline_assignments.assignments.insert(final_handle.clone(), Vec::new());
shader_pipeline_assignments
.assignments
.insert(final_handle.clone(), Vec::new());
}
let assignments = shader_pipeline_assignments.assignments.get_mut(&final_handle).unwrap();
let assignments = shader_pipeline_assignments
.assignments
.get_mut(&final_handle)
.unwrap();
assignments.push(entity);
}
}

View file

@ -1,8 +1,7 @@
use crate::{
legion::prelude::*,
render::render_graph::{
DynamicUniformBufferInfo, PipelineDescriptor, RenderGraph, ResourceInfo,
TextureDescriptor,
DynamicUniformBufferInfo, PipelineDescriptor, RenderGraph, ResourceInfo, TextureDescriptor,
},
};
use std::ops::Range;

View file

@ -15,4 +15,4 @@ pub enum ResourceInfo {
mesh_id: usize,
// pub layout: Option<
},
}
}

View file

@ -23,4 +23,4 @@ pub mod draw_target {
pub mod pass {
pub const MAIN: &str = "Main";
}
}

View file

@ -1,9 +1,10 @@
use crate::{
core::GetBytes,
math::Vec4,
render::{
color::ColorSource,
render_graph::{BindType, TextureViewDimension},
}, core::GetBytes,
},
};
use legion::prelude::Entity;
use std::collections::HashMap;
@ -133,8 +134,7 @@ where
}
}
impl AsFieldBindType for Vec4
{
impl AsFieldBindType for Vec4 {
fn get_field_bind_type(&self) -> FieldBindType {
FieldBindType::Uniform
}
@ -161,4 +161,4 @@ impl DynamicUniformBufferInfo {
offsets: HashMap::new(),
}
}
}
}

View file

@ -1,10 +1,10 @@
use crate::render::render_graph::{
BindGroup, BindType, Binding, UniformProperty, UniformPropertyType, TextureViewDimension
BindGroup, BindType, Binding, TextureViewDimension, UniformProperty, UniformPropertyType,
};
use spirv_reflect::{
types::{
ReflectDescriptorBinding, ReflectDescriptorSet, ReflectDescriptorType,
ReflectTypeDescription, ReflectTypeFlags, ReflectDimension,
ReflectDescriptorBinding, ReflectDescriptorSet, ReflectDescriptorType, ReflectDimension,
ReflectTypeDescription, ReflectTypeFlags,
},
ShaderModule,
};
@ -44,7 +44,7 @@ pub fn get_shader_layout(spirv_data: &[u32]) -> ShaderLayout {
entry_point: entry_point_name,
}
}
Err(err) => panic!("Failed to reflect shader layout: {:?}", err)
Err(err) => panic!("Failed to reflect shader layout: {:?}", err),
}
}
@ -145,7 +145,11 @@ fn reflect_uniform_numeric(type_description: &ReflectTypeDescription) -> Uniform
.type_flags
.contains(ReflectTypeFlags::MATRIX)
{
match (number_type, traits.numeric.matrix.column_count, traits.numeric.matrix.row_count) {
match (
number_type,
traits.numeric.matrix.column_count,
traits.numeric.matrix.row_count,
) {
(NumberType::Float, 3, 3) => UniformPropertyType::Mat3,
(NumberType::Float, 4, 4) => UniformPropertyType::Mat4,
(number_type, column_count, row_count) => panic!(