Skip to content

Commit

Permalink
Mesh overhaul with custom vertex attributes #592 (#599)
Browse files Browse the repository at this point in the history
Mesh overhaul with custom vertex attributes
  • Loading branch information
Julian Heinken authored Oct 31, 2020
1 parent ad940fb commit 4645da3
Show file tree
Hide file tree
Showing 25 changed files with 452 additions and 682 deletions.
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ to view all changes since the `0.2.1` release.
## Unreleased

### Added

- [Mesh overhaul with custom vertex attributes][616] for `Mesh`
- Any vertex attribute can now be added over `mesh.attributes.insert()`. For example: `mesh.attributes.insert(Cow::Borrowed(Mesh::ATTRIBUTE_POSITION), points.into())`.
- For missing attributes (requested by shader, but not defined by mesh), Bevy will provide a zero-filled fallback buffer.
- [Touch Input][696]
- [Do not depend on spirv on wasm32 target][689]
- [Another fast compile flag for macOS][552]
- [Mesh overhaul with custom vertex attributes][599]
- [Introduce Mouse capture API][679]
- [`bevy_input::touch`: implement touch input][696]
- [D-pad support on MacOS][653]
Expand Down Expand Up @@ -56,6 +62,12 @@ to view all changes since the `0.2.1` release.
`Color::rgb` and `Color::rgba` will be converted to linear sRGB under-the-hood.
- This allows drop-in use of colors from most applications.
- New methods `Color::rgb_linear` and `Color::rgba_linear` will accept colors already in linear sRGB (the old behavior)
- Individual color-components must now be accessed through setters and getters: `.r`, `.g`, `.b`, `.a`, `.set_r`, `.set_g`, `.set_b`, `.set_a`, and the corresponding methods with the `*_linear` suffix.
- Breaking Change: [Mesh overhaul with custom vertex attributes][616] for `Mesh`
- Removed `VertexAttribute`, `Vertex`, `AsVertexBufferDescriptor`.

- Despawning an entity multiple times causes a debug-level log message to be emitted instead of a panic [649] [651]
- Breaking Change: Migrated to rodio 0.12, this means:
- Individual color-components must now be accessed through setters and getters:
`.r`, `.g`, `.b`, `.a`, `.set_r`, `.set_g`, `.set_b`, `.set_a`, and the corresponding methods with the `*_linear` suffix.
- Despawning an entity multiple times causes a debug-level log message to be emitted instead of a panic: [#649][649], [#651][651]
Expand All @@ -65,6 +77,9 @@ to view all changes since the `0.2.1` release.

### Fixed

[599]: https://github.com/bevyengine/bevy/pull/599
[696]: https://github.com/bevyengine/bevy/pull/696
[689]: https://github.com/bevyengine/bevy/pull/689
- [Properly update bind group ids when setting dynamic bindings][560]
- [Properly exit the app on AppExit event][610]
- [Fix FloatOrd hash being different for different NaN values][618]
Expand Down
124 changes: 0 additions & 124 deletions crates/bevy_derive/src/as_vertex_buffer_descriptor.rs

This file was deleted.

7 changes: 0 additions & 7 deletions crates/bevy_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern crate proc_macro;

mod app_plugin;
mod as_vertex_buffer_descriptor;
mod bytes;
mod modules;
mod render_resource;
Expand Down Expand Up @@ -45,12 +44,6 @@ pub fn derive_shader_defs(input: TokenStream) -> TokenStream {
shader_defs::derive_shader_defs(input)
}

/// Derives the AsVertexBufferDescriptor trait.
#[proc_macro_derive(AsVertexBufferDescriptor, attributes(vertex, as_crate))]
pub fn derive_as_vertex_buffer_descriptor(input: TokenStream) -> TokenStream {
as_vertex_buffer_descriptor::derive_as_vertex_buffer_descriptor(input)
}

/// Generates a dynamic plugin entry point function for the given `Plugin` type.
#[proc_macro_derive(DynamicPlugin)]
pub fn derive_dynamic_plugin(input: TokenStream) -> TokenStream {
Expand Down
19 changes: 11 additions & 8 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy_ecs::{bevy_utils::BoxedFuture, World, WorldBuilderSource};
use bevy_math::Mat4;
use bevy_pbr::prelude::{PbrComponents, StandardMaterial};
use bevy_render::{
mesh::{Indices, Mesh, VertexAttribute},
mesh::{Indices, Mesh, VertexAttributeValues},
pipeline::PrimitiveTopology,
prelude::{Color, Texture},
texture::{AddressMode, FilterMode, SamplerDescriptor, TextureFormat},
Expand All @@ -20,7 +20,7 @@ use gltf::{
Primitive,
};
use image::{GenericImageView, ImageFormat};
use std::path::Path;
use std::{borrow::Cow, path::Path};
use thiserror::Error;

/// An error that occurs when loading a GLTF file
Expand Down Expand Up @@ -88,23 +88,26 @@ async fn load_gltf<'a, 'b>(

if let Some(vertex_attribute) = reader
.read_positions()
.map(|v| VertexAttribute::position(v.collect()))
.map(|v| VertexAttributeValues::Float3(v.collect()))
{
mesh.attributes.push(vertex_attribute);
mesh.attributes
.insert(Cow::Borrowed(Mesh::ATTRIBUTE_POSITION), vertex_attribute);
}

if let Some(vertex_attribute) = reader
.read_normals()
.map(|v| VertexAttribute::normal(v.collect()))
.map(|v| VertexAttributeValues::Float3(v.collect()))
{
mesh.attributes.push(vertex_attribute);
mesh.attributes
.insert(Cow::Borrowed(Mesh::ATTRIBUTE_NORMAL), vertex_attribute);
}

if let Some(vertex_attribute) = reader
.read_tex_coords(0)
.map(|v| VertexAttribute::uv(v.into_f32().collect()))
.map(|v| VertexAttributeValues::Float2(v.into_f32().collect()))
{
mesh.attributes.push(vertex_attribute);
mesh.attributes
.insert(Cow::Borrowed(Mesh::ATTRIBUTE_UV_0), vertex_attribute);
}

if let Some(indices) = reader.read_indices() {
Expand Down
38 changes: 16 additions & 22 deletions crates/bevy_render/src/draw.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
pipeline::{
PipelineCompiler, PipelineDescriptor, PipelineLayout, PipelineSpecialization,
VertexBufferDescriptors,
VERTEX_FALLBACK_LAYOUT_NAME,
},
renderer::{
BindGroup, BindGroupId, BufferId, BufferUsage, RenderResource, RenderResourceBinding,
Expand Down Expand Up @@ -131,7 +131,6 @@ pub struct DrawContext<'a> {
pub shaders: ResMut<'a, Assets<Shader>>,
pub pipeline_compiler: ResMut<'a, PipelineCompiler>,
pub render_resource_context: Res<'a, Box<dyn RenderResourceContext>>,
pub vertex_buffer_descriptors: Res<'a, VertexBufferDescriptors>,
pub shared_buffers: Res<'a, SharedBuffers>,
pub current_pipeline: Option<Handle<PipelineDescriptor>>,
}
Expand All @@ -143,7 +142,6 @@ impl<'a> UnsafeClone for DrawContext<'a> {
shaders: self.shaders.unsafe_clone(),
pipeline_compiler: self.pipeline_compiler.unsafe_clone(),
render_resource_context: self.render_resource_context.unsafe_clone(),
vertex_buffer_descriptors: self.vertex_buffer_descriptors.unsafe_clone(),
shared_buffers: self.shared_buffers.unsafe_clone(),
current_pipeline: self.current_pipeline.clone(),
}
Expand All @@ -166,7 +164,6 @@ impl<'a> FetchResource<'a> for FetchDrawContext {
resources.borrow_mut::<Assets<Shader>>();
resources.borrow_mut::<PipelineCompiler>();
resources.borrow::<Box<dyn RenderResourceContext>>();
resources.borrow::<VertexBufferDescriptors>();
resources.borrow::<SharedBuffers>();
}

Expand All @@ -175,7 +172,6 @@ impl<'a> FetchResource<'a> for FetchDrawContext {
resources.release_mut::<Assets<Shader>>();
resources.release_mut::<PipelineCompiler>();
resources.release::<Box<dyn RenderResourceContext>>();
resources.release::<VertexBufferDescriptors>();
resources.release::<SharedBuffers>();
}

Expand Down Expand Up @@ -205,9 +201,6 @@ impl<'a> FetchResource<'a> for FetchDrawContext {
render_resource_context: Res::new(
resources.get_unsafe_ref::<Box<dyn RenderResourceContext>>(ResourceIndex::Global),
),
vertex_buffer_descriptors: Res::new(
resources.get_unsafe_ref::<VertexBufferDescriptors>(ResourceIndex::Global),
),
shared_buffers: Res::new(
resources.get_unsafe_ref::<SharedBuffers>(ResourceIndex::Global),
),
Expand All @@ -221,7 +214,6 @@ impl<'a> FetchResource<'a> for FetchDrawContext {
access.add_write(TypeId::of::<Assets<Shader>>());
access.add_write(TypeId::of::<PipelineCompiler>());
access.add_read(TypeId::of::<Box<dyn RenderResourceContext>>());
access.add_read(TypeId::of::<VertexBufferDescriptors>());
access.add_read(TypeId::of::<SharedBuffers>());
access
}
Expand Down Expand Up @@ -262,7 +254,6 @@ impl<'a> DrawContext<'a> {
&mut self.pipelines,
&mut self.shaders,
pipeline_handle,
&self.vertex_buffer_descriptors,
specialization,
)
};
Expand Down Expand Up @@ -358,18 +349,21 @@ impl<'a> DrawContext<'a> {
let layout = pipeline_descriptor
.get_layout()
.ok_or(DrawError::PipelineHasNoLayout)?;
for (slot, vertex_buffer_descriptor) in layout.vertex_buffer_descriptors.iter().enumerate()
{
for bindings in render_resource_bindings.iter() {
if let Some((vertex_buffer, index_buffer)) =
bindings.get_vertex_buffer(&vertex_buffer_descriptor.name)
{
draw.set_vertex_buffer(slot as u32, vertex_buffer, 0);
if let Some(index_buffer) = index_buffer {
draw.set_index_buffer(index_buffer, 0);
}

break;
// figure out if the fallback buffer is needed
let need_fallback_buffer = layout
.vertex_buffer_descriptors
.iter()
.any(|x| x.name == VERTEX_FALLBACK_LAYOUT_NAME);
for bindings in render_resource_bindings.iter() {
if let Some(index_buffer) = bindings.index_buffer {
draw.set_index_buffer(index_buffer, 0);
}
if let Some(main_vertex_buffer) = bindings.vertex_attribute_buffer {
draw.set_vertex_buffer(0, main_vertex_buffer, 0);
}
if need_fallback_buffer {
if let Some(fallback_vertex_buffer) = bindings.vertex_fallback_buffer {
draw.set_vertex_buffer(1, fallback_vertex_buffer, 0);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use camera::{
};
use pipeline::{
DynamicBinding, IndexFormat, PipelineCompiler, PipelineDescriptor, PipelineSpecialization,
PrimitiveTopology, ShaderSpecialization, VertexBufferDescriptors,
PrimitiveTopology, ShaderSpecialization,
};
use render_graph::{
base::{self, BaseRenderGraphBuilder, BaseRenderGraphConfig},
Expand Down Expand Up @@ -119,7 +119,6 @@ impl Plugin for RenderPlugin {
.init_resource::<RenderGraph>()
.init_resource::<PipelineCompiler>()
.init_resource::<RenderResourceBindings>()
.init_resource::<VertexBufferDescriptors>()
.init_resource::<TextureResourceSystemState>()
.init_resource::<AssetRenderResourceBindings>()
.init_resource::<ActiveCameras>()
Expand Down
Loading

0 comments on commit 4645da3

Please sign in to comment.