Skip to content

Commit

Permalink
Material -> ShaderModel
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed Sep 27, 2024
1 parent ebc74c8 commit bbbb640
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 58 deletions.
10 changes: 0 additions & 10 deletions bevy_nannou_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ pub fn shader_model(attr: TokenStream, item: TokenStream) -> TokenStream {
#fragment_shader_impl
}
}

impl #impl_generics ::nannou::prelude::Material for #name #ty_generics #where_clause {
fn vertex_shader() -> ::nannou::prelude::ShaderRef {
<Self as ::nannou::prelude::render::ShaderModel>::vertex_shader()
}

fn fragment_shader() -> ::nannou::prelude::ShaderRef {
<Self as ::nannou::prelude::render::ShaderModel>::fragment_shader()
}
}
};

TokenStream::from(expanded)
Expand Down
24 changes: 12 additions & 12 deletions bevy_nannou_derive/tests/shader_model_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,58 @@ use bevy_nannou_derive::shader_model;
use bevy_nannou_draw::render::ShaderModel;

#[shader_model]
struct TestMaterial {}
struct TestShaderModel {}

#[test]
fn test_default_shaders() {
assert!(matches!(TestMaterial::vertex_shader(), ShaderRef::Default));
assert!(matches!(TestShaderModel::vertex_shader(), ShaderRef::Default));
assert!(matches!(
TestMaterial::fragment_shader(),
TestShaderModel::fragment_shader(),
ShaderRef::Default
));
}

#[shader_model(vertex = "custom_vertex.wgsl")]
struct TestVertexMaterial {}
struct TestVertexShaderModel {}

#[test]
fn test_custom_vertex_shader() {
assert!(matches!(
TestVertexMaterial::vertex_shader(),
TestVertexShaderModel::vertex_shader(),
ShaderRef::Path(_)
));
assert!(matches!(
TestVertexMaterial::fragment_shader(),
TestVertexShaderModel::fragment_shader(),
ShaderRef::Default
));
}

#[shader_model(fragment = "custom_fragment.wgsl")]
struct TestFragmentMaterial {}
struct TestFragmentShaderModel {}

#[test]
fn test_custom_fragment_shader() {
assert!(matches!(
TestFragmentMaterial::vertex_shader(),
TestFragmentShaderModel::vertex_shader(),
ShaderRef::Default
));
assert!(matches!(
TestFragmentMaterial::fragment_shader(),
TestFragmentShaderModel::fragment_shader(),
ShaderRef::Path(_)
));
}

#[shader_model(vertex = "custom_vertex.wgsl", fragment = "custom_fragment.wgsl")]
struct TestBothMaterial {}
struct TestBothShaderModel {}

#[test]
fn test_both_custom_shaders() {
assert!(matches!(
TestBothMaterial::vertex_shader(),
TestBothShaderModel::vertex_shader(),
ShaderRef::Path(_)
));
assert!(matches!(
TestBothMaterial::fragment_shader(),
TestBothShaderModel::fragment_shader(),
ShaderRef::Path(_)
));
}
2 changes: 1 addition & 1 deletion bevy_nannou_draw/src/draw/background.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::prelude::{Color, Material};
use bevy::prelude::{Color};

use crate::draw::{Draw, DrawCommand};
use crate::render::ShaderModel;
Expand Down
1 change: 0 additions & 1 deletion bevy_nannou_draw/src/draw/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::any::TypeId;
use std::marker::PhantomData;

use bevy::asset::UntypedAssetId;
use bevy::pbr::{ExtendedMaterial, MaterialExtension};
use bevy::prelude::*;
use lyon::path::PathEvent;
use lyon::tessellation::{FillOptions, LineCap, LineJoin, StrokeOptions};
Expand Down
14 changes: 7 additions & 7 deletions bevy_nannou_draw/src/draw/indirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,36 +98,36 @@ where
#[derive(Component, ExtractComponent, Clone)]
pub struct IndirectMesh;

pub struct IndirectMaterialPlugin<SM>(PhantomData<SM>);
pub struct IndirectShaderModelPlugin<SM>(PhantomData<SM>);

impl<SM> Default for IndirectMaterialPlugin<SM>
impl<SM> Default for IndirectShaderModelPlugin<SM>
where
SM: Default,
{
fn default() -> Self {
IndirectMaterialPlugin(PhantomData)
IndirectShaderModelPlugin(PhantomData)
}
}

impl<SM> Plugin for IndirectMaterialPlugin<SM>
impl<SM> Plugin for IndirectShaderModelPlugin<SM>
where
SM: ShaderModel,
SM::Data: PartialEq + Eq + Hash + Clone,
{
fn build(&self, app: &mut App) {
app
.sub_app_mut(RenderApp)
.add_render_command::<Transparent3d, DrawIndirectMaterial<SM>>()
.add_render_command::<Transparent3d, DrawIndirectShaderModel<SM>>()
.add_systems(
Render,
queue_shader_model::<SM, With<IndirectMesh>, DrawIndirectMaterial<SM>>
queue_shader_model::<SM, With<IndirectMesh>, DrawIndirectShaderModel<SM>>
.after(prepare_assets::<PreparedShaderModel<SM>>)
.in_set(RenderSet::QueueMeshes),
);
}
}

type DrawIndirectMaterial<SM> = (
type DrawIndirectShaderModel<SM> = (
SetItemPipeline,
SetMeshViewBindGroup<0>,
SetMeshBindGroup<1>,
Expand Down
15 changes: 7 additions & 8 deletions bevy_nannou_draw/src/draw/instanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use bevy::{
core_pipeline::core_3d::Transparent3d,
ecs::system::{lifetimeless::*, SystemParamItem},
pbr::{
MaterialPipeline, MaterialPipelineKey, MeshPipeline, MeshPipelineKey, PreparedMaterial,
RenderMeshInstances, SetMeshBindGroup, SetMeshViewBindGroup,
},
prelude::*,
Expand Down Expand Up @@ -103,35 +102,35 @@ pub struct InstancedMesh;
#[derive(Component, ExtractComponent, Clone)]
pub struct InstanceRange(pub Range<u32>);

pub struct InstancedMaterialPlugin<SM>(PhantomData<SM>);
pub struct InstancedShaderModelPlugin<SM>(PhantomData<SM>);

impl<SM> Default for InstancedMaterialPlugin<SM>
impl<SM> Default for InstancedShaderModelPlugin<SM>
where
SM: Default,
{
fn default() -> Self {
InstancedMaterialPlugin(PhantomData)
InstancedShaderModelPlugin(PhantomData)
}
}

impl<SM> Plugin for InstancedMaterialPlugin<SM>
impl<SM> Plugin for InstancedShaderModelPlugin<SM>
where
SM: ShaderModel,
SM::Data: PartialEq + Eq + Hash + Clone,
{
fn build(&self, app: &mut App) {
app.sub_app_mut(RenderApp)
.add_render_command::<Transparent3d, DrawInstancedMaterial<SM>>()
.add_render_command::<Transparent3d, DrawInstancedShaderModel<SM>>()
.add_systems(
Render,
queue_shader_model::<SM, With<InstancedMesh>, DrawInstancedMaterial<SM>>
queue_shader_model::<SM, With<InstancedMesh>, DrawInstancedShaderModel<SM>>
.after(prepare_assets::<PreparedShaderModel<SM>>)
.in_set(RenderSet::QueueMeshes),
);
}
}

type DrawInstancedMaterial<SM> = (
type DrawInstancedShaderModel<SM> = (
SetItemPipeline,
SetMeshViewBindGroup<0>,
SetMeshBindGroup<1>,
Expand Down
21 changes: 10 additions & 11 deletions bevy_nannou_draw/src/render.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
draw::{
indirect::{IndirectMaterialPlugin, IndirectMesh},
instanced::{InstanceRange, InstancedMaterialPlugin, InstancedMesh},
indirect::{IndirectShaderModelPlugin, IndirectMesh},
instanced::{InstanceRange, InstancedShaderModelPlugin, InstancedMesh},
mesh::MeshExt,
render::{RenderContext, RenderPrimitive},
DrawCommand, DrawContext,
Expand All @@ -16,10 +16,9 @@ use bevy::{
system::{lifetimeless::SRes, SystemParamItem},
},
pbr::{
DefaultOpaqueRendererMethod, DrawMesh, ExtendedMaterial, MaterialBindGroupId,
MaterialExtension, MaterialExtensionKey, MaterialExtensionPipeline, MaterialPipeline,
MaterialProperties, MeshPipeline, MeshPipelineKey, OpaqueRendererMethod, PreparedMaterial,
RenderMeshInstances, SetMeshBindGroup, SetMeshViewBindGroup, StandardMaterial,
DefaultOpaqueRendererMethod, DrawMesh,
MeshPipeline, MeshPipelineKey, OpaqueRendererMethod,
RenderMeshInstances, SetMeshBindGroup, SetMeshViewBindGroup,
},
prelude::{TypePath, *},
render::{
Expand Down Expand Up @@ -134,8 +133,8 @@ where
.add_plugins((
ExtractInstancesPlugin::<AssetId<SM>>::extract_visible(),
RenderAssetPlugin::<PreparedShaderModel<SM>>::default(),
IndirectMaterialPlugin::<SM>::default(),
InstancedMaterialPlugin::<SM>::default(),
IndirectShaderModelPlugin::<SM>::default(),
InstancedShaderModelPlugin::<SM>::default(),
))
.add_systems(PostUpdate, update_shader_model::<SM>.after(update_draw_mesh));

Expand Down Expand Up @@ -249,7 +248,7 @@ bitflags::bitflags! {
}

#[derive(Asset, AsBindGroup, TypePath, Debug, Clone, Default)]
#[bind_group_data(NannouMaterialKey)]
#[bind_group_data(NannouBindGroupData)]
#[uniform(0, NannouShaderModelUniform)]
pub struct NannouShaderModel {
pub color: Color,
Expand Down Expand Up @@ -284,12 +283,12 @@ impl AsBindGroupShaderType<NannouShaderModelUniform> for NannouShaderModel {
}

#[derive(Eq, PartialEq, Hash, Clone)]
pub struct NannouMaterialKey {
pub struct NannouBindGroupData {
polygon_mode: PolygonMode,
blend: Option<BlendState>,
}

impl From<&NannouShaderModel> for NannouMaterialKey {
impl From<&NannouShaderModel> for NannouBindGroupData {
fn from(shader_model: &NannouShaderModel) -> Self {
Self {
polygon_mode: shader_model.polygon_mode,
Expand Down
8 changes: 0 additions & 8 deletions nannou/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,6 @@ where
self
}

/// Load a fragment shader asset from the given path for use with the nannou `Draw` API.
#[cfg(feature = "nightly")]
pub fn init_fragment_shader<const SHADER: &'static str>(mut self) -> Self {
self.app
.add_plugins(NannouShaderModelPlugin::<ExtendedNannouMaterial<"", SHADER>>::default());
self
}

pub fn compute<CM: Compute>(mut self, compute_fn: ComputeUpdateFn<M, CM>) -> Self {
let render_app = self.app.sub_app_mut(bevy::render::RenderApp);
render_app.insert_resource(ComputeShaderHandle(CM::shader()));
Expand Down

0 comments on commit bbbb640

Please sign in to comment.