Skip to content

Commit 82707d2

Browse files
committed
Move MaterialProperties and MaterialPipeline to bevy_material
1 parent 4efe66d commit 82707d2

File tree

2 files changed

+96
-86
lines changed

2 files changed

+96
-86
lines changed

crates/bevy_material/src/material.rs

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
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::{DrawFunctionId, DrawFunctionLabel, InternedDrawFunctionLabel, InternedShaderLabel, ShaderLabel};
6+
use crate::render_resource::{BindGroupLayoutDescriptor, RenderPipelineDescriptor, SpecializedMeshPipelineError};
27
use crate::*;
38
use alloc::sync::Arc;
9+
use bevy_asset::Handle;
10+
use bevy_ecs::resource::Resource;
11+
use bevy_mesh::MeshVertexBufferLayoutRef;
412
use bevy_platform::hash::FixedHasher;
13+
use bevy_shader::Shader;
14+
use smallvec::SmallVec;
515
use core::any::{Any, TypeId};
616
use core::hash::{BuildHasher, Hasher};
717
use core::hash::Hash;
818

919
pub const MATERIAL_BIND_GROUP_INDEX: usize = 3;
1020

21+
/// Render pipeline data for a given [`Material`].
22+
#[derive(Resource, Clone)]
23+
pub struct MaterialPipeline {
24+
pub mesh_pipeline: MeshPipeline,
25+
}
26+
1127
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
1228
pub struct ErasedMaterialPipelineKey {
1329
pub mesh_key: MeshPipelineKey,
@@ -94,7 +110,84 @@ impl Default for ErasedMaterialKey {
94110
}
95111
}
96112

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

99192
#[derive(Clone, Copy, Default)]
100193
pub enum RenderPhaseType {

crates/bevy_pbr/src/material.rs

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use bevy_render::{
5959
};
6060
use bevy_render::{mesh::allocator::MeshAllocator, sync_world::MainEntityHashMap};
6161
use bevy_render::{texture::FallbackImage, view::RenderVisibleEntities};
62-
use bevy_shader::{Shader, ShaderDefVal};
62+
use bevy_shader::ShaderDefVal;
6363
use bevy_utils::Parallel;
6464
use core::any::TypeId;
6565
use core::{hash::Hash, marker::PhantomData};
@@ -431,12 +431,6 @@ pub struct MaterialPipelineKey<M: Material> {
431431
pub bind_group_data: M::Data,
432432
}
433433

434-
/// Render pipeline data for a given [`Material`].
435-
#[derive(Resource, Clone)]
436-
pub struct MaterialPipeline {
437-
pub mesh_pipeline: MeshPipeline,
438-
}
439-
440434
pub struct MaterialPipelineSpecializer {
441435
pub(crate) pipeline: MaterialPipeline,
442436
pub(crate) properties: Arc<MaterialProperties>,
@@ -1274,84 +1268,7 @@ pub struct DeferredDrawFunction;
12741268
#[derive(DrawFunctionLabel, Debug, Hash, PartialEq, Eq, Clone, Default)]
12751269
pub struct ShadowsDrawFunction;
12761270

1277-
/// Common [`Material`] properties, calculated for a specific material instance.
1278-
#[derive(Default)]
1279-
pub struct MaterialProperties {
1280-
/// Is this material should be rendered by the deferred renderer when.
1281-
/// [`AlphaMode::Opaque`] or [`AlphaMode::Mask`]
1282-
pub render_method: OpaqueRendererMethod,
1283-
/// The [`AlphaMode`] of this material.
1284-
pub alpha_mode: AlphaMode,
1285-
/// The bits in the [`MeshPipelineKey`] for this material.
1286-
///
1287-
/// These are precalculated so that we can just "or" them together in
1288-
/// [`queue_material_meshes`].
1289-
pub mesh_pipeline_key_bits: MeshPipelineKey,
1290-
/// Add a bias to the view depth of the mesh which can be used to force a specific render order
1291-
/// for meshes with equal depth, to avoid z-fighting.
1292-
/// The bias is in depth-texture units so large values may be needed to overcome small depth differences.
1293-
pub depth_bias: f32,
1294-
/// Whether the material would like to read from [`ViewTransmissionTexture`](bevy_core_pipeline::core_3d::ViewTransmissionTexture).
1295-
///
1296-
/// This allows taking color output from the [`Opaque3d`] pass as an input, (for screen-space transmission) but requires
1297-
/// rendering to take place in a separate [`Transmissive3d`] pass.
1298-
pub reads_view_transmission_texture: bool,
1299-
pub render_phase_type: RenderPhaseType,
1300-
pub material_layout: Option<BindGroupLayoutDescriptor>,
1301-
/// Backing array is a size of 4 because the `StandardMaterial` needs 4 draw functions by default
1302-
pub draw_functions: SmallVec<[(InternedDrawFunctionLabel, DrawFunctionId); 4]>,
1303-
/// Backing array is a size of 3 because the `StandardMaterial` has 3 custom shaders (`frag`, `prepass_frag`, `deferred_frag`) which is the
1304-
/// most common use case
1305-
pub shaders: SmallVec<[(InternedShaderLabel, Handle<Shader>); 3]>,
1306-
/// Whether this material *actually* uses bindless resources, taking the
1307-
/// platform support (or lack thereof) of bindless resources into account.
1308-
pub bindless: bool,
1309-
pub specialize: Option<
1310-
fn(
1311-
&MaterialPipeline,
1312-
&mut RenderPipelineDescriptor,
1313-
&MeshVertexBufferLayoutRef,
1314-
ErasedMaterialPipelineKey,
1315-
) -> Result<(), SpecializedMeshPipelineError>,
1316-
>,
1317-
/// The key for this material, typically a bitfield of flags that are used to modify
1318-
/// the pipeline descriptor used for this material.
1319-
pub material_key: ErasedMaterialKey,
1320-
/// Whether shadows are enabled for this material
1321-
pub shadows_enabled: bool,
1322-
/// Whether prepass is enabled for this material
1323-
pub prepass_enabled: bool,
1324-
}
1325-
1326-
impl MaterialProperties {
1327-
pub fn get_shader(&self, label: impl ShaderLabel) -> Option<Handle<Shader>> {
1328-
self.shaders
1329-
.iter()
1330-
.find(|(inner_label, _)| inner_label == &label.intern())
1331-
.map(|(_, shader)| shader)
1332-
.cloned()
1333-
}
1334-
1335-
pub fn add_shader(&mut self, label: impl ShaderLabel, shader: Handle<Shader>) {
1336-
self.shaders.push((label.intern(), shader));
1337-
}
1338-
1339-
pub fn get_draw_function(&self, label: impl DrawFunctionLabel) -> Option<DrawFunctionId> {
1340-
self.draw_functions
1341-
.iter()
1342-
.find(|(inner_label, _)| inner_label == &label.intern())
1343-
.map(|(_, shader)| shader)
1344-
.cloned()
1345-
}
1346-
1347-
pub fn add_draw_function(
1348-
&mut self,
1349-
label: impl DrawFunctionLabel,
1350-
draw_function: DrawFunctionId,
1351-
) {
1352-
self.draw_functions.push((label.intern(), draw_function));
1353-
}
1354-
}
1271+
pub use bevy_material::material::MaterialProperties;
13551272

13561273
/// A resource that maps each untyped material ID to its binding.
13571274
///

0 commit comments

Comments
 (0)