Skip to content

Commit 3dbad88

Browse files
committed
Move MaterialProperties and MaterialPipeline to bevy_material
1 parent 9ee0cb6 commit 3dbad88

File tree

39 files changed

+370
-237
lines changed

39 files changed

+370
-237
lines changed

crates/bevy_core_pipeline/src/core_3d/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,10 @@ pub fn prepare_core_3d_depth_textures(
846846
}
847847
}
848848

849+
/// A material can specify [`MaterialProperties::reads_view_transmission_texture`](`bevy_material::material::MaterialProperties`) to read from [`ViewTransmissionTexture`].
850+
///
851+
/// This allows taking color output from the [`Opaque3d`] pass as an input, (for screen-space transmission) but requires
852+
/// rendering to take place in a separate [`Transmissive3d`] pass.
849853
#[derive(Component)]
850854
pub struct ViewTransmissionTexture {
851855
pub texture: Texture,

crates/bevy_gizmos_render/src/pipeline_3d.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use bevy_ecs::{
2121
system::{Commands, Query, Res, ResMut},
2222
};
2323
use bevy_image::BevyDefault as _;
24-
use bevy_pbr::{MeshPipeline, MeshPipelineKey, SetMeshViewBindGroup};
24+
use bevy_pbr::{init_mesh_pipeline, MeshPipeline, MeshPipelineKey, SetMeshViewBindGroup};
2525
use bevy_render::{
2626
render_asset::{prepare_assets, RenderAssets},
2727
render_phase::{
@@ -56,7 +56,9 @@ impl Plugin for LineGizmo3dPlugin {
5656
)
5757
.add_systems(
5858
RenderStartup,
59-
init_line_gizmo_pipelines.after(init_line_gizmo_uniform_bind_group_layout),
59+
init_line_gizmo_pipelines
60+
.after(init_line_gizmo_uniform_bind_group_layout)
61+
.after(init_mesh_pipeline),
6062
)
6163
.add_systems(
6264
Render,

crates/bevy_internal/Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,7 @@ bevy_window = ["dep:bevy_window", "dep:bevy_a11y", "bevy_image"]
235235
bevy_winit = ["dep:bevy_winit", "bevy_window"]
236236
bevy_camera = ["dep:bevy_camera", "bevy_mesh", "bevy_window"]
237237
bevy_scene = ["dep:bevy_scene", "bevy_asset"]
238-
bevy_material = [
239-
"dep:bevy_material",
240-
"bevy_image",
241-
"bevy_shader",
242-
]
238+
bevy_material = ["dep:bevy_material", "bevy_image", "bevy_shader"]
243239
bevy_light = ["dep:bevy_light", "bevy_camera", "bevy_gizmos?/bevy_light"]
244240
bevy_render = [
245241
"dep:bevy_render",

crates/bevy_material/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Provides a material abstraction for bevy
2-
#![allow(missing_docs)]
2+
#![allow(missing_docs, reason = "Needs docs")]
33

44
extern crate alloc;
55

crates/bevy_material/src/material.rs

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
1+
use crate::alpha::AlphaMode;
2+
use crate::opaque::OpaqueRendererMethod;
3+
use crate::render::MeshPipeline;
14
use crate::render::MeshPipelineKey;
5+
use crate::render_phase::{
6+
DrawFunctionId, DrawFunctionLabel, InternedDrawFunctionLabel, InternedShaderLabel, ShaderLabel,
7+
};
8+
use crate::render_resource::{
9+
BindGroupLayoutDescriptor, RenderPipelineDescriptor, SpecializedMeshPipelineError,
10+
};
211
use crate::*;
312
use alloc::sync::Arc;
13+
use bevy_asset::Handle;
14+
use bevy_ecs::resource::Resource;
15+
use bevy_mesh::MeshVertexBufferLayoutRef;
416
use bevy_platform::hash::FixedHasher;
17+
use bevy_shader::Shader;
518
use core::any::{Any, TypeId};
6-
use core::hash::{BuildHasher, Hasher};
719
use core::hash::Hash;
20+
use core::hash::{BuildHasher, Hasher};
21+
use smallvec::SmallVec;
822

923
pub const MATERIAL_BIND_GROUP_INDEX: usize = 3;
1024

25+
/// Render pipeline data for a given material.
26+
#[derive(Resource, Clone)]
27+
pub struct MaterialPipeline {
28+
pub mesh_pipeline: MeshPipeline,
29+
}
30+
1131
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
1232
pub struct ErasedMaterialPipelineKey {
1333
pub mesh_key: MeshPipelineKey,
@@ -94,7 +114,83 @@ impl Default for ErasedMaterialKey {
94114
}
95115
}
96116

97-
// pub struct MaterialProperties {
117+
/// Common material properties, calculated for a specific material instance.
118+
#[derive(Default)]
119+
pub struct MaterialProperties {
120+
/// Is this material should be rendered by the deferred renderer when.
121+
/// [`AlphaMode::Opaque`] or [`AlphaMode::Mask`]
122+
pub render_method: OpaqueRendererMethod,
123+
/// The [`AlphaMode`] of this material.
124+
pub alpha_mode: AlphaMode,
125+
/// The bits in the [`MeshPipelineKey`] for this material.
126+
///
127+
/// These are precalculated so that we can just "or" them together.
128+
pub mesh_pipeline_key_bits: MeshPipelineKey,
129+
/// Add a bias to the view depth of the mesh which can be used to force a specific render order
130+
/// for meshes with equal depth, to avoid z-fighting.
131+
/// The bias is in depth-texture units so large values may be needed to overcome small depth differences.
132+
pub depth_bias: f32,
133+
/// Whether the material would like to read from a view transmission texture
134+
///
135+
/// This allows taking color output from the opaque 3d pass as an input, (for screen-space transmission) but requires
136+
/// rendering to take place in a separate transmissive 3d pass.
137+
pub reads_view_transmission_texture: bool,
138+
pub render_phase_type: RenderPhaseType,
139+
pub material_layout: Option<BindGroupLayoutDescriptor>,
140+
/// Backing array is a size of 4 because the `StandardMaterial` needs 4 draw functions by default
141+
pub draw_functions: SmallVec<[(InternedDrawFunctionLabel, DrawFunctionId); 4]>,
142+
/// Backing array is a size of 3 because the `StandardMaterial` has 3 custom shaders (`frag`, `prepass_frag`, `deferred_frag`) which is the
143+
/// most common use case
144+
pub shaders: SmallVec<[(InternedShaderLabel, Handle<Shader>); 3]>,
145+
/// Whether this material *actually* uses bindless resources, taking the
146+
/// platform support (or lack thereof) of bindless resources into account.
147+
pub bindless: bool,
148+
pub specialize: Option<
149+
fn(
150+
&MaterialPipeline,
151+
&mut RenderPipelineDescriptor,
152+
&MeshVertexBufferLayoutRef,
153+
ErasedMaterialPipelineKey,
154+
) -> Result<(), SpecializedMeshPipelineError>,
155+
>,
156+
/// The key for this material, typically a bitfield of flags that are used to modify
157+
/// the pipeline descriptor used for this material.
158+
pub material_key: ErasedMaterialKey,
159+
/// Whether shadows are enabled for this material
160+
pub shadows_enabled: bool,
161+
/// Whether prepass is enabled for this material
162+
pub prepass_enabled: bool,
163+
}
164+
165+
impl MaterialProperties {
166+
pub fn get_shader(&self, label: impl ShaderLabel) -> Option<Handle<Shader>> {
167+
self.shaders
168+
.iter()
169+
.find(|(inner_label, _)| inner_label == &label.intern())
170+
.map(|(_, shader)| shader)
171+
.cloned()
172+
}
173+
174+
pub fn add_shader(&mut self, label: impl ShaderLabel, shader: Handle<Shader>) {
175+
self.shaders.push((label.intern(), shader));
176+
}
177+
178+
pub fn get_draw_function(&self, label: impl DrawFunctionLabel) -> Option<DrawFunctionId> {
179+
self.draw_functions
180+
.iter()
181+
.find(|(inner_label, _)| inner_label == &label.intern())
182+
.map(|(_, shader)| shader)
183+
.cloned()
184+
}
185+
186+
pub fn add_draw_function(
187+
&mut self,
188+
label: impl DrawFunctionLabel,
189+
draw_function: DrawFunctionId,
190+
) {
191+
self.draw_functions.push((label.intern(), draw_function));
192+
}
193+
}
98194

99195
#[derive(Clone, Copy, Default)]
100196
pub enum RenderPhaseType {
@@ -104,4 +200,3 @@ pub enum RenderPhaseType {
104200
Transmissive,
105201
Transparent,
106202
}
107-

crates/bevy_material/src/render/mesh.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
use crate::{
2+
render::{
3+
MeshLayouts, MeshPipelineViewLayout, MeshPipelineViewLayoutKey, MeshPipelineViewLayouts,
4+
},
5+
render_resource::*,
6+
};
17
use bevy_asset::Handle;
28
use bevy_ecs::resource::Resource;
39
use bevy_mesh::BaseMeshPipelineKey;
410
use bevy_shader::Shader;
5-
use crate::{render::{MeshLayouts, MeshPipelineViewLayout, MeshPipelineViewLayoutKey, MeshPipelineViewLayouts}, render_resource::*};
611

712
use static_assertions::const_assert_eq;
813

9-
1014
/// How many textures are allowed in the view bind group layout (`@group(0)`) before
1115
/// broader compatibility with WebGL and WebGPU is at risk, due to the minimum guaranteed
1216
/// values for `MAX_TEXTURE_IMAGE_UNITS` (in WebGL) and `maxSampledTexturesPerShaderStage` (in WebGPU),
@@ -18,7 +22,6 @@ use static_assertions::const_assert_eq;
1822
#[cfg(debug_assertions)]
1923
pub const MESH_PIPELINE_VIEW_LAYOUT_SAFE_MAX_TEXTURES: usize = 10;
2024

21-
2225
/// All data needed to construct a pipeline for rendering 3D meshes.
2326
#[derive(Resource, Clone)]
2427
pub struct MeshPipeline {
@@ -223,4 +226,3 @@ const_assert_eq!(
223226
& MeshPipelineKey::ALL_RESERVED_BITS.bits(),
224227
0
225228
);
226-

crates/bevy_material/src/render/mesh_bindings.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
21
use crate::render_resource::BindGroupLayoutDescriptor;
32

4-
5-
/// All possible [`BindGroupLayout`]s in bevy's default mesh shader (`mesh.wgsl`).
3+
/// All possible [`BindGroupLayoutDescriptor`]s in bevy's default mesh shader (`mesh.wgsl`).
64
#[derive(Clone)]
75
pub struct MeshLayouts {
86
/// The mesh model uniform (transform) and nothing else.
@@ -18,19 +16,15 @@ pub struct MeshLayouts {
1816
/// frame's joint matrices, so that we can compute motion vectors.
1917
pub skinned_motion: BindGroupLayoutDescriptor,
2018

21-
/// Also includes the uniform and [`MorphAttributes`] for morph targets.
22-
///
23-
/// [`MorphAttributes`]: bevy_mesh::morph::MorphAttributes
19+
/// Also includes the uniform and [`MorphAttributes`](`bevy_mesh::morph::MorphAttributes`) for morph targets.
2420
pub morphed: BindGroupLayoutDescriptor,
2521

2622
/// Like [`MeshLayouts::morphed`], but includes a slot for the previous
2723
/// frame's morph weights, so that we can compute motion vectors.
2824
pub morphed_motion: BindGroupLayoutDescriptor,
2925

3026
/// Also includes both uniforms for skinning and morph targets, also the
31-
/// morph target [`MorphAttributes`] binding.
32-
///
33-
/// [`MorphAttributes`]: bevy_mesh::morph::MorphAttributes
27+
/// morph target [`MorphAttributes`](`bevy_mesh::morph::MorphAttributes`) binding.
3428
pub morphed_skinned: BindGroupLayoutDescriptor,
3529

3630
/// Like [`MeshLayouts::morphed_skinned`], but includes slots for the

crates/bevy_material/src/render/mesh_view_bindings.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1+
use crate::{render::MeshPipelineKey, render_resource::*};
12
use alloc::sync::Arc;
23
use bevy_derive::{Deref, DerefMut};
3-
use bevy_ecs::{
4-
resource::Resource,
5-
};
6-
use crate::{
7-
render::MeshPipelineKey, render_resource::*
8-
};
4+
use bevy_ecs::resource::Resource;
95

106
#[cfg(debug_assertions)]
117
use {crate::render::MESH_PIPELINE_VIEW_LAYOUT_SAFE_MAX_TEXTURES, bevy_utils::once, tracing::warn};
@@ -135,4 +131,3 @@ impl MeshPipelineViewLayouts {
135131
layout
136132
}
137133
}
138-
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use core::{fmt::Debug, hash::Hash};
22

33
// TODO: make this generic?
4-
/// An identifier for a [`Draw`] function stored in [`DrawFunctions`].
4+
/// An identifier for a draw functions stored.
55
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
66
pub struct DrawFunctionId(pub u32);
7-

crates/bevy_material/src/render_resource/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
mod bind_group_layout_entries;
22
mod pipeline;
3-
// mod pipeline_cache;
43
mod pipeline_specializer;
54

65
pub use bind_group_layout_entries::*;
76
pub use pipeline::*;
8-
// pub use pipeline_cache::*;
97
pub use pipeline_specializer::*;
108

119
// TODO: decide where re-exports should go

0 commit comments

Comments
 (0)