Skip to content

Bevy 0.7 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ version = "0.6.1"
exclude = ["demo.gif", "demo_2.png", "demo_2.webm"]

[dependencies]
bevy = { version = "0.6", default-features = false, features = [ "render" ] }
bevy = { version = "0.7", default-features = false, features = [ "bevy_core_pipeline", "bevy_render", "bevy_pbr", "bevy_sprite" ] }

[features]
example_deps_2d = [
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl DebugLinesPlugin {

impl Plugin for DebugLinesPlugin {
fn build(&self, app: &mut App) {
use bevy::render::{render_resource::SpecializedPipelines, RenderApp, RenderStage};
use bevy::render::{render_resource::SpecializedMeshPipelines, RenderApp, RenderStage};
let mut shaders = app.world.get_resource_mut::<Assets<Shader>>().unwrap();
shaders.set_untracked(
DEBUG_LINES_SHADER_HANDLE,
Expand All @@ -109,7 +109,7 @@ impl Plugin for DebugLinesPlugin {
.add_render_command::<dim::Phase, dim::DrawDebugLines>()
.insert_resource(DebugLinesConfig { depth_test: self.depth_test})
.init_resource::<dim::DebugLinePipeline>()
.init_resource::<SpecializedPipelines<dim::DebugLinePipeline>>()
.init_resource::<SpecializedMeshPipelines<dim::DebugLinePipeline>>()
.add_system_to_stage(RenderStage::Extract, extract)
.add_system_to_stage(RenderStage::Queue, dim::queue);

Expand All @@ -135,11 +135,11 @@ fn setup(mut cmds: Commands, mut meshes: ResMut<Assets<Mesh>>) {
for i in 0..MESH_COUNT {
// Create a new mesh with the number of vertices we need.
let mut mesh = Mesh::new(PrimitiveTopology::LineList);
mesh.set_attribute(
mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
VertexAttributeValues::Float32x3(Vec::with_capacity(MAX_POINTS_PER_MESH)),
);
mesh.set_attribute(
mesh.insert_attribute(
Mesh::ATTRIBUTE_COLOR,
VertexAttributeValues::Float32x4(Vec::with_capacity(MAX_POINTS_PER_MESH)),
);
Expand Down
122 changes: 76 additions & 46 deletions src/render_dim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ pub mod r3d {
},
prelude::*,
render::{
mesh::MeshVertexBufferLayout,
render_asset::RenderAssets,
render_phase::{DrawFunctions, RenderPhase, SetItemPipeline},
render_resource::{
PrimitiveTopology, RenderPipelineCache, RenderPipelineDescriptor, SpecializedPipeline,
SpecializedPipelines, VertexAttribute, VertexBufferLayout, VertexFormat,
VertexStepMode,
PipelineCache, PrimitiveTopology, RenderPipelineDescriptor,
SpecializedMeshPipeline, SpecializedMeshPipelineError, SpecializedMeshPipelines,
VertexAttribute, VertexBufferLayout, VertexFormat, VertexStepMode,
},
view::{ExtractedView, Msaa},
},
};

use crate::{RenderDebugLinesMesh, DebugLinesConfig, DEBUG_LINES_SHADER_HANDLE};
use crate::{DebugLinesConfig, RenderDebugLinesMesh, DEBUG_LINES_SHADER_HANDLE};

pub(crate) struct DebugLinePipeline {
mesh_pipeline: MeshPipeline,
Expand All @@ -35,10 +37,14 @@ pub mod r3d {
}
}

impl SpecializedPipeline for DebugLinePipeline {
impl SpecializedMeshPipeline for DebugLinePipeline {
type Key = (bool, MeshPipelineKey);

fn specialize(&self, (depth_test, key): Self::Key) -> RenderPipelineDescriptor {
fn specialize(
&self,
(depth_test, key): Self::Key,
layout: &MeshVertexBufferLayout,
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
use VertexFormat::{Float32x3, Float32x4};

let mut shader_defs = Vec::new();
Expand All @@ -47,7 +53,7 @@ pub mod r3d {
shader_defs.push("DEPTH_TEST_ENABLED".to_string());
}

let mut descriptor = self.mesh_pipeline.specialize(key);
let mut descriptor = self.mesh_pipeline.specialize(key, layout)?;
descriptor.vertex.shader = self.shader.clone_weak();
descriptor.vertex.shader_defs = shader_defs.clone();
descriptor.vertex.buffers[0] = VertexBufferLayout {
Expand Down Expand Up @@ -78,17 +84,18 @@ pub mod r3d {
//if self.always_in_front {
//descriptor.depth_stencil.as_mut().unwrap().bias.constant = i32::MAX;
//}
descriptor
Ok(descriptor)
}
}

pub(crate) fn queue(
opaque_3d_draw_functions: Res<DrawFunctions<Opaque3d>>,
debug_line_pipeline: Res<DebugLinePipeline>,
mut pipelines: ResMut<SpecializedPipelines<DebugLinePipeline>>,
mut pipeline_cache: ResMut<RenderPipelineCache>,
mut pipelines: ResMut<SpecializedMeshPipelines<DebugLinePipeline>>,
mut pipeline_cache: ResMut<PipelineCache>,
render_meshes: Res<RenderAssets<Mesh>>,
msaa: Res<Msaa>,
material_meshes: Query<(Entity, &MeshUniform), With<RenderDebugLinesMesh>>,
material_meshes: Query<(Entity, &MeshUniform, &Handle<Mesh>), With<RenderDebugLinesMesh>>,
config: Res<DebugLinesConfig>,
mut views: Query<(&ExtractedView, &mut RenderPhase<Opaque3d>)>,
) {
Expand All @@ -100,14 +107,23 @@ pub mod r3d {
for (view, mut transparent_phase) in views.iter_mut() {
let view_matrix = view.transform.compute_matrix();
let view_row_2 = view_matrix.row(2);
for (entity, mesh_uniform) in material_meshes.iter() {
let pipeline = pipelines.specialize(&mut pipeline_cache, &debug_line_pipeline, (config.depth_test, key));
transparent_phase.add(Opaque3d {
entity,
pipeline,
draw_function: draw_custom,
distance: view_row_2.dot(mesh_uniform.transform.col(3)),
});
for (entity, mesh_uniform, mesh_handle) in material_meshes.iter() {
if let Some(mesh) = render_meshes.get(mesh_handle) {
let pipeline = pipelines
.specialize(
&mut pipeline_cache,
&debug_line_pipeline,
(config.depth_test, key),
&mesh.layout,
)
.unwrap();
transparent_phase.add(Opaque3d {
entity,
pipeline,
draw_function: draw_custom,
distance: view_row_2.dot(mesh_uniform.transform.col(3)),
});
}
}
}
}
Expand All @@ -127,11 +143,13 @@ pub mod r2d {
core_pipeline::Transparent2d,
prelude::*,
render::{
mesh::MeshVertexBufferLayout,
render_asset::RenderAssets,
render_phase::{DrawFunctions, RenderPhase, SetItemPipeline},
render_resource::{
PrimitiveTopology, RenderPipelineCache, RenderPipelineDescriptor, Shader,
SpecializedPipeline, SpecializedPipelines, VertexAttribute, VertexBufferLayout,
VertexFormat, VertexStepMode,
PipelineCache, PrimitiveTopology, RenderPipelineDescriptor, Shader,
SpecializedMeshPipeline, SpecializedMeshPipelineError, SpecializedMeshPipelines,
VertexAttribute, VertexBufferLayout, VertexFormat, VertexStepMode,
},
view::{Msaa, VisibleEntities},
},
Expand Down Expand Up @@ -159,16 +177,20 @@ pub mod r2d {
}
}

impl SpecializedPipeline for DebugLinePipeline {
impl SpecializedMeshPipeline for DebugLinePipeline {
type Key = Mesh2dPipelineKey;

fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
fn specialize(
&self,
key: Self::Key,
layout: &MeshVertexBufferLayout,
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
use VertexFormat::{Float32x3, Float32x4};

//let mut shader_defs = Vec::new();
//shader_defs.push("2D".to_string());

let mut descriptor = self.mesh_pipeline.specialize(key);
let mut descriptor = self.mesh_pipeline.specialize(key, layout)?;
descriptor.vertex.shader = self.shader.clone_weak();
//descriptor.vertex.shader_defs = shader_defs.clone();
descriptor.vertex.buffers[0] = VertexBufferLayout {
Expand Down Expand Up @@ -196,39 +218,48 @@ pub mod r2d {
//fragment.shader_defs = shader_defs.clone();
descriptor.primitive.topology = PrimitiveTopology::LineList;
descriptor.primitive.cull_mode = None;
descriptor
Ok(descriptor)
}
}

pub(crate) fn queue(
draw2d_functions: Res<DrawFunctions<Transparent2d>>,
debug_line_pipeline: Res<DebugLinePipeline>,
mut pipeline_cache: ResMut<RenderPipelineCache>,
mut specialized_pipelines: ResMut<SpecializedPipelines<DebugLinePipeline>>,
mut pipeline_cache: ResMut<PipelineCache>,
mut specialized_pipelines: ResMut<SpecializedMeshPipelines<DebugLinePipeline>>,
render_meshes: Res<RenderAssets<Mesh>>,
msaa: Res<Msaa>,
material_meshes: Query<&Mesh2dUniform, With<RenderDebugLinesMesh>>,
material_meshes: Query<(&Mesh2dUniform, &Handle<Mesh>), With<RenderDebugLinesMesh>>,
mut views: Query<(&VisibleEntities, &mut RenderPhase<Transparent2d>)>,
) {
for (view, mut phase) in views.iter_mut() {
let draw_mesh2d = draw2d_functions.read().get_id::<DrawDebugLines>().unwrap();
let mesh_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples);
let msaa_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples);

for visible_entity in &view.entities {
if let Ok(uniform) = material_meshes.get(*visible_entity) {
let mesh2d_key = mesh_key
| Mesh2dPipelineKey::from_primitive_topology(PrimitiveTopology::LineList);
let mesh_z = uniform.transform.w_axis.z;
phase.add(Transparent2d {
entity: *visible_entity,
draw_function: draw_mesh2d,
pipeline: specialized_pipelines.specialize(
&mut pipeline_cache,
&debug_line_pipeline,
mesh2d_key,
),
sort_key: FloatOrd(mesh_z),
batch_range: None,
});
if let Ok((uniform, mesh_handle)) = material_meshes.get(*visible_entity) {
if let Some(mesh) = render_meshes.get(mesh_handle) {
let mesh_key = msaa_key
| Mesh2dPipelineKey::from_primitive_topology(
PrimitiveTopology::LineList,
);
let mesh_z = uniform.transform.w_axis.z;
let pipeline = specialized_pipelines
.specialize(
&mut pipeline_cache,
&debug_line_pipeline,
mesh_key,
&mesh.layout,
)
.unwrap();
phase.add(Transparent2d {
entity: *visible_entity,
draw_function: draw_mesh2d,
pipeline,
sort_key: FloatOrd(mesh_z),
batch_range: None,
});
}
}
}
}
Expand All @@ -241,4 +272,3 @@ pub mod r2d {
DrawMesh2d,
);
}