This commit is contained in:
Carter Anderson 2020-02-17 19:06:12 -08:00
parent 6f376b5f3f
commit 57f798c0bd
20 changed files with 104 additions and 363 deletions

View file

@ -87,10 +87,10 @@ fn setup(world: &mut World) {
world
.build()
// red cube
.add_archetype(NewMeshEntity {
.add_archetype(MeshEntity {
mesh: cube_handle,
translation: Translation::new(0.0, 0.0, 1.0),
..NewMeshEntity::default()
..MeshEntity::default()
})
// camera
.add_archetype(CameraEntity {

View file

@ -18,7 +18,10 @@ fn create_entities_insert_vec(
(),
vec![(
plane_handle.clone(),
Material::new(Albedo::Color(math::vec4(0.1, 0.2, 0.1, 1.0))),
StandardMaterial {
albedo: math::vec4(0.1, 0.2, 0.1, 1.0),
everything_is_red: false,
},
LocalToWorld::identity(),
Translation::new(0.0, 0.0, 0.0),
)],
@ -29,7 +32,10 @@ fn create_entities_insert_vec(
(),
vec![(
cube_handle,
Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0))),
StandardMaterial {
albedo: math::vec4(0.5, 0.3, 0.3, 1.0),
everything_is_red: false,
},
LocalToWorld::identity(),
Translation::new(0.0, 0.0, 1.0),
)],
@ -39,17 +45,7 @@ fn create_entities_insert_vec(
world.insert(
(),
vec![(
Light {
color: wgpu::Color {
r: 0.8,
g: 0.8,
b: 0.5,
a: 1.0,
},
fov: f32::to_radians(60.0),
depth: 0.1..50.0,
target_view: None,
},
Light::default(),
LocalToWorld::identity(),
Translation::new(4.0, -4.0, 5.0),
Rotation::from_euler_angles(0.0, 0.0, 0.0),
@ -87,28 +83,24 @@ fn create_entities_builder_add_component(
// plane
.build_entity()
.add(plane_handle.clone())
.add(Material::new(Albedo::Color(math::vec4(0.1, 0.2, 0.1, 1.0))))
.add(StandardMaterial {
albedo: math::vec4(0.1, 0.2, 0.1, 1.0),
everything_is_red: false,
})
.add(LocalToWorld::identity())
.add(Translation::new(0.0, 0.0, 0.0))
// cube
.build_entity()
.add(cube_handle)
.add(Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0))))
.add(StandardMaterial {
albedo: math::vec4(0.5, 0.3, 0.3, 1.0),
everything_is_red: false,
})
.add(LocalToWorld::identity())
.add(Translation::new(0.0, 0.0, 1.0))
// light
.build_entity()
.add(Light {
color: wgpu::Color {
r: 0.8,
g: 0.8,
b: 0.5,
a: 1.0,
},
fov: f32::to_radians(60.0),
depth: 0.1..50.0,
target_view: None,
})
.add(Light::default())
.add(LocalToWorld::identity())
.add(Translation::new(4.0, -4.0, 5.0))
.add(Rotation::from_euler_angles(0.0, 0.0, 0.0))
@ -139,33 +131,25 @@ fn create_entities_builder_archetype(
// plane
.add_archetype(MeshEntity {
mesh: plane_handle.clone(),
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),
everything_is_red: false
},
..Default::default()
})
// cube
.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(),
translation: Translation::new(0.0, 0.0, 1.0),
material: StandardMaterial {
albedo: math::vec4(0.5, 0.3, 0.3, 1.0),
everything_is_red: false
},
..Default::default()
})
// light
.add_archetype(LightEntity {
light: Light {
color: wgpu::Color {
r: 0.8,
g: 0.8,
b: 0.5,
a: 1.0,
},
fov: f32::to_radians(60.0),
depth: 0.1..50.0,
target_view: None,
},
local_to_world: LocalToWorld::identity(),
translation: Translation::new(4.0, -4.0, 5.0),
rotation: Rotation::from_euler_angles(0.0, 0.0, 0.0),
..Default::default()
})
// camera
.add_archetype(CameraEntity {

View file

@ -41,18 +41,7 @@ fn setup(world: &mut World) {
vec![
// lights
(
Light {
color: wgpu::Color {
r: 0.8,
g: 0.8,
b: 0.5,
a: 1.0,
},
fov: f32::to_radians(60.0),
depth: 0.1..50.0,
target_view: None,
},
Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0))),
Light::default(),
LocalToWorld::identity(),
Translation::new(4.0, -4.0, 5.0),
Rotation::from_euler_angles(0.0, 0.0, 0.0),
@ -204,9 +193,10 @@ fn create_person(world: &mut World, mesh_handle: Handle<Mesh>, translation: Tran
value: math::vec3(0.0, 0.0, 0.0),
},
Instanced,
Material::new(Albedo::Color(
math::vec4(0.5, 0.3, 0.3, 1.0) * random::<f32>(),
)),
StandardMaterial {
albedo: math::vec4(0.5, 0.3, 0.3, 1.0) * random::<f32>(),
everything_is_red: false,
},
mesh_handle,
LocalToWorld::identity(),
translation,

View file

@ -35,7 +35,10 @@ fn setup(world: &mut World) {
(),
vec![(
plane_handle.clone(),
Material::new(Albedo::Color(math::vec4(0.1, 0.2, 0.1, 1.0))),
StandardMaterial {
albedo: math::vec4(0.1, 0.2, 0.1, 1.0),
everything_is_red: false,
},
LocalToWorld::identity(),
Translation::new(0.0, 0.0, -5.0),
)],
@ -47,7 +50,10 @@ fn setup(world: &mut World) {
(),
vec![(
cube_handle.clone(),
Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0))),
StandardMaterial {
albedo: math::vec4(0.5, 0.3, 0.3, 1.0),
everything_is_red: false,
},
LocalToWorld::identity(),
Translation::new(0.0, 0.0, 1.0),
Rotation::from_euler_angles(0.0, 0.0, 0.0),
@ -62,7 +68,10 @@ fn setup(world: &mut World) {
(),
vec![(
cube_handle,
Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0))),
StandardMaterial {
albedo: math::vec4(0.5, 0.3, 0.3, 1.0),
everything_is_red: false,
},
LocalToWorld::identity(),
Translation::new(0.0, 0.0, 3.0),
Parent(parent_cube),
@ -74,18 +83,7 @@ fn setup(world: &mut World) {
world.insert(
(),
vec![(
Light {
color: wgpu::Color {
r: 0.8,
g: 0.8,
b: 0.5,
a: 1.0,
},
fov: f32::to_radians(60.0),
depth: 0.1..50.0,
target_view: None,
},
Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0))),
Light::default(),
LocalToWorld::identity(),
Translation::new(4.0, -4.0, 5.0),
Rotation::from_euler_angles(0.0, 0.0, 0.0),

View file

@ -26,14 +26,14 @@ pub fn setup(world: &mut World) {
world.build()
// plane
.add_archetype(MeshEntity {
.add_archetype(NewMeshEntity {
mesh: plane_handle.clone(),
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),
})
// cube
.add_archetype(MeshEntity {
.add_archetype(NewMeshEntity {
mesh: cube_handle,
material: Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0))),
local_to_world: LocalToWorld::identity(),

View file

@ -16,7 +16,7 @@ fn setup(world: &mut World) {
world
.build()
// plane
.add_archetype(NewMeshEntity {
.add_archetype(MeshEntity {
mesh: plane_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(0.1, 0.2, 0.1, 1.0),
@ -25,7 +25,7 @@ fn setup(world: &mut World) {
..Default::default()
})
// tan cube
.add_archetype(NewMeshEntity {
.add_archetype(MeshEntity {
mesh: cube_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(0.5, 0.4, 0.3, 1.0),
@ -35,7 +35,7 @@ fn setup(world: &mut World) {
..Default::default()
})
// red cube
.add_archetype(NewMeshEntity {
.add_archetype(MeshEntity {
mesh: cube_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(0.5, 0.4, 0.3, 1.0),
@ -46,20 +46,9 @@ fn setup(world: &mut World) {
})
// light
.add_archetype(LightEntity {
light: Light {
color: wgpu::Color {
r: 0.8,
g: 0.8,
b: 0.8,
a: 1.0,
},
fov: f32::to_radians(60.0),
depth: 0.1..50.0,
target_view: None,
},
local_to_world: LocalToWorld::identity(),
translation: Translation::new(4.0, -4.0, 5.0),
rotation: Rotation::from_euler_angles(0.0, 0.0, 0.0),
..Default::default()
})
// camera
.add_archetype(CameraEntity {

View file

@ -70,7 +70,7 @@ fn setup(world: &mut World) {
let mut builder = world
.build()
// plane
.add_archetype(NewMeshEntity {
.add_archetype(MeshEntity {
mesh: plane_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(0.1, 0.2, 0.1, 1.0),
@ -79,7 +79,7 @@ fn setup(world: &mut World) {
..Default::default()
})
// cube
.add_archetype(NewMeshEntity {
.add_archetype(MeshEntity {
mesh: cube_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(1.0, 1.0, 1.0, 1.0),
@ -88,7 +88,7 @@ fn setup(world: &mut World) {
translation: Translation::new(0.0, 0.0, 1.0),
..Default::default()
})
.add_archetype(NewMeshEntity {
.add_archetype(MeshEntity {
mesh: cube_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(0.0, 1.0, 0.0, 1.0),
@ -99,20 +99,8 @@ fn setup(world: &mut World) {
})
// light
.add_archetype(LightEntity {
light: Light {
color: wgpu::Color {
r: 0.8,
g: 0.8,
b: 0.5,
a: 1.0,
},
fov: f32::to_radians(60.0),
depth: 0.1..50.0,
target_view: None,
},
local_to_world: LocalToWorld::identity(),
translation: Translation::new(4.0, -4.0, 5.0),
rotation: Rotation::from_euler_angles(0.0, 0.0, 0.0),
..Default::default()
})
// camera
.add_archetype(CameraEntity {
@ -132,7 +120,7 @@ fn setup(world: &mut World) {
let mut rng = StdRng::from_entropy();
for _ in 0..10000 {
builder = builder.add_archetype(NewMeshEntity {
builder = builder.add_archetype(MeshEntity {
mesh: cube_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(

View file

@ -19,7 +19,7 @@ fn setup(world: &mut World) {
world
.build()
// cube
.add_archetype(NewMeshEntity {
.add_archetype(MeshEntity {
mesh: cube_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(0.5, 0.3, 0.3, 1.0),
@ -30,20 +30,8 @@ fn setup(world: &mut World) {
})
// light
.add_archetype(LightEntity {
light: Light {
color: wgpu::Color {
r: 0.8,
g: 0.8,
b: 0.5,
a: 1.0,
},
fov: f32::to_radians(60.0),
depth: 0.1..50.0,
target_view: None,
},
local_to_world: LocalToWorld::identity(),
translation: Translation::new(4.0, -4.0, 5.0),
rotation: Rotation::from_euler_angles(0.0, 0.0, 0.0),
..Default::default()
})
// camera
.add_archetype(CameraEntity {

View file

@ -14,7 +14,7 @@ fn setup(world: &mut World) {
world
.build()
// cube
.add_archetype(NewMeshEntity {
.add_archetype(MeshEntity {
mesh: cube_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(0.5, 0.3, 0.3, 1.0),
@ -25,20 +25,8 @@ fn setup(world: &mut World) {
})
// light
.add_archetype(LightEntity {
light: Light {
color: wgpu::Color {
r: 0.8,
g: 0.8,
b: 0.5,
a: 1.0,
},
fov: f32::to_radians(60.0),
depth: 0.1..50.0,
target_view: None,
},
local_to_world: LocalToWorld::identity(),
translation: Translation::new(4.0, -4.0, 5.0),
rotation: Rotation::from_euler_angles(0.0, 0.0, 0.0),
..Default::default()
})
// 3d camera
.add_archetype(CameraEntity {

View file

@ -1,28 +1,19 @@
use crate::{
prelude::*,
render::render_graph::{Renderable, ShaderUniforms, StandardMaterial},
render::render_graph::{Renderable, StandardMaterial},
};
use bevy_derive::EntityArchetype;
#[derive(EntityArchetype)]
#[derive(EntityArchetype, Default)]
pub struct MeshEntity {
pub mesh: Handle<Mesh>,
pub material: Material,
pub material: StandardMaterial,
pub renderable: Renderable,
pub local_to_world: LocalToWorld,
pub translation: Translation,
}
#[derive(EntityArchetype, Default)]
pub struct NewMeshEntity {
pub mesh: Handle<Mesh>,
pub material: StandardMaterial,
pub renderable: Renderable,
pub shader_uniforms: ShaderUniforms,
pub local_to_world: LocalToWorld,
pub translation: Translation,
}
#[derive(EntityArchetype)]
pub struct LightEntity {
pub light: Light,
pub local_to_world: LocalToWorld,

View file

@ -5,7 +5,7 @@ pub use crate::{
ecs,
ecs::{default_archetypes::*, EntityArchetype, WorldBuilder, WorldBuilderSource},
render::{
ActiveCamera, ActiveCamera2d, Albedo, Camera, CameraType, Instanced, Light, Material,
ActiveCamera, ActiveCamera2d, Camera, CameraType, Instanced, Light,
render_graph::StandardMaterial,
},
ui::{Anchors, Margins, Node},

View file

@ -1,5 +0,0 @@
pub struct InstanceBufferInfo {
pub buffer: wgpu::Buffer,
pub instance_count: usize,
pub mesh_id: usize,
}

View file

@ -1,12 +1,21 @@
use crate::{math, prelude::Translation, render::camera};
use crate::{math, math::Vec4, prelude::Translation, render::camera};
use std::ops::Range;
use zerocopy::{AsBytes, FromBytes};
pub struct Light {
pub color: wgpu::Color,
pub color: Vec4,
pub fov: f32,
pub depth: Range<f32>,
pub target_view: Option<wgpu::TextureView>,
}
impl Default for Light {
fn default() -> Self {
Light {
color: Vec4::new(1.0, 1.0, 1.0, 1.0),
depth: 0.1..50.0,
fov: f32::to_radians(60.0),
}
}
}
#[repr(C)]
@ -29,12 +38,7 @@ impl LightRaw {
LightRaw {
proj: proj.to_cols_array_2d(),
pos: [x, y, z, 1.0],
color: [
light.color.r as f32,
light.color.g as f32,
light.color.b as f32,
1.0,
],
color: light.color.into(),
}
}
}

View file

@ -1,49 +0,0 @@
use crate::{
asset::{Handle, Texture},
math,
};
use zerocopy::{AsBytes, FromBytes};
pub enum Albedo {
Color(math::Vec4),
Texture(Handle<Texture>),
}
pub struct Material {
pub albedo: Albedo,
pub bind_group: Option<wgpu::BindGroup>,
pub uniform_buf: Option<wgpu::Buffer>,
}
pub struct Instanced;
impl Material {
pub fn new(albedo: Albedo) -> Self {
Material {
albedo,
bind_group: None,
uniform_buf: None,
}
}
pub fn get_color(&self) -> math::Vec4 {
match self.albedo {
Albedo::Color(color) => color,
_ => math::vec4(1.0, 0.0, 1.0, 1.0),
}
}
}
#[repr(C)]
#[derive(Clone, Copy, AsBytes, FromBytes)]
pub struct MaterialUniforms {
pub model: [[f32; 4]; 4],
pub color: [f32; 4],
}
#[repr(C)]
#[derive(Clone, Copy, AsBytes, FromBytes)]
pub struct SimpleMaterialUniforms {
pub position: [f32; 3],
pub color: [f32; 4],
}

View file

@ -1,56 +1,15 @@
pub mod camera;
pub mod instancing;
pub mod render_graph;
pub mod shader;
pub mod shader_reflect;
mod light;
mod material;
mod vertex;
pub use camera::*;
pub use light::*;
pub use material::*;
pub use shader::*;
use std::mem;
pub use vertex::Vertex;
pub struct UniformBuffer {
pub buffer: wgpu::Buffer,
pub size: u64,
}
impl UniformBuffer {
pub fn get_binding_resource<'a>(&'a self) -> wgpu::BindingResource<'a> {
wgpu::BindingResource::Buffer {
buffer: &self.buffer,
range: 0..self.size,
}
}
}
pub fn get_vertex_buffer_descriptor<'a>() -> wgpu::VertexBufferDescriptor<'a> {
let vertex_size = mem::size_of::<Vertex>();
wgpu::VertexBufferDescriptor {
stride: vertex_size as wgpu::BufferAddress,
step_mode: wgpu::InputStepMode::Vertex,
attributes: &[
wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float4,
offset: 0,
shader_location: 0,
},
wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float4,
offset: 4 * 4,
shader_location: 1,
},
wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float2,
offset: 8 * 4,
shader_location: 2,
},
],
}
}
pub struct Instanced;

View file

@ -2,7 +2,7 @@ use crate::{
asset::{AssetStorage, Handle, Mesh},
legion::prelude::*,
render::{
render_graph::{resource_name, RenderPass, Renderable, ShaderUniforms, PipelineDescriptor},
render_graph::{resource_name, RenderPass, Renderable, PipelineDescriptor},
Instanced,
},
};
@ -13,9 +13,9 @@ pub fn meshes_draw_target(world: &World, render_pass: &mut dyn RenderPass, _pipe
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<ShaderUniforms>, Read<Handle<Mesh>>, Read<Renderable>)>::query()
let mesh_query = <(Read<Handle<Mesh>>, Read<Renderable>)>::query()
.filter(!component::<Instanced>());
for (entity, (_shader_uniforms, mesh, renderable)) in mesh_query.iter_entities(world) {
for (entity, (mesh, renderable)) in mesh_query.iter_entities(world) {
if !renderable.is_visible {
continue;
}

View file

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

View file

@ -1,6 +1,3 @@
use legion::prelude::Entity;
use std::collections::HashMap;
pub enum ResourceInfo {
BufferMapped {
size: u64,
@ -18,24 +15,4 @@ pub enum ResourceInfo {
mesh_id: usize,
// pub layout: Option<
},
}
pub struct DynamicUniformBufferInfo {
pub indices: HashMap<usize, Entity>,
pub offsets: HashMap<Entity, u64>,
pub capacity: u64,
pub count: u64,
pub size: u64,
}
impl DynamicUniformBufferInfo {
pub fn new() -> Self {
DynamicUniformBufferInfo {
capacity: 0,
count: 0,
indices: HashMap::new(),
offsets: HashMap::new(),
size: 0,
}
}
}
}

View file

@ -1,6 +1,6 @@
use crate::{
render::render_graph::{
resource::DynamicUniformBufferInfo, AsUniforms, Renderable, Renderer, ResourceProvider,
DynamicUniformBufferInfo, AsUniforms, Renderable, Renderer, ResourceProvider,
},
};
use legion::prelude::*;
@ -36,7 +36,6 @@ where
fn update(&mut self, renderer: &mut dyn Renderer, world: &mut World) {
let query = <(Read<T>, Read<Renderable>)>::query();
// retrieve all uniforms buffers that aren't aleady set. these are "dynamic" uniforms, which are set by the user in ShaderUniforms
// TODO: this breaks down in multiple ways:
// (SOLVED 1) resource_info will be set after the first run so this won't update.
// (2) if we create new buffers, the old bind groups will be invalid

View file

@ -1,12 +1,9 @@
use crate::{
legion::{
borrow::RefMap,
prelude::{Entity, World},
},
math::Vec4,
render::render_graph::{BindType, UniformPropertyType},
};
use legion::storage::Component;
use legion::prelude::Entity;
use std::collections::HashMap;
use zerocopy::AsBytes;
pub trait GetBytes {
@ -70,79 +67,22 @@ pub struct UniformInfo<'a> {
pub bind_type: BindType,
}
pub fn uniform_selector<T>(entity: Entity, world: &World) -> Option<RefMap<&dyn AsUniforms>>
where
T: AsUniforms + Component,
{
world
.get_component::<T>(entity)
.map(|c| c.map_into(|s| s as &dyn AsUniforms))
pub struct DynamicUniformBufferInfo {
pub indices: HashMap<usize, Entity>,
pub offsets: HashMap<Entity, u64>,
pub capacity: u64,
pub count: u64,
pub size: u64,
}
// TODO: Remove these
pub type ShaderUniformSelector = fn(Entity, &World) -> Option<RefMap<&dyn AsUniforms>>;
#[derive(Default)]
pub struct ShaderUniforms {
// used for distinguishing
pub uniform_selectors: Vec<ShaderUniformSelector>,
}
impl ShaderUniforms {
impl DynamicUniformBufferInfo {
pub fn new() -> Self {
ShaderUniforms {
uniform_selectors: Vec::new(),
DynamicUniformBufferInfo {
capacity: 0,
count: 0,
indices: HashMap::new(),
offsets: HashMap::new(),
size: 0,
}
}
pub fn add(&mut self, selector: ShaderUniformSelector) {
self.uniform_selectors.push(selector);
}
pub fn get_uniform_info<'a>(
&'a self,
world: &'a World,
entity: Entity,
uniform_name: &str,
) -> Option<&'a UniformInfo> {
for uniform_selector in self.uniform_selectors.iter().rev() {
let uniforms = uniform_selector(entity, world).unwrap_or_else(|| {
panic!(
"ShaderUniform selector points to a missing component. Uniform: {}",
uniform_name
)
});
let info = uniforms.get_uniform_info(uniform_name);
if let Some(_) = info {
return info;
}
}
None
}
pub fn get_uniform_bytes<'a>(
&'a self,
world: &'a World,
entity: Entity,
uniform_name: &str,
) -> Option<Vec<u8>> {
for uniform_selector in self.uniform_selectors.iter().rev() {
let uniforms = uniform_selector(entity, world).unwrap_or_else(|| {
panic!(
"ShaderUniform selector points to a missing component. Uniform: {}",
uniform_name
)
});
let bytes = uniforms.get_uniform_bytes(uniform_name);
if let Some(_) = bytes {
return bytes;
}
}
None
}
}