Skip to content

Commit 3f4fd6b

Browse files
committed
Move MaterialProperties and MaterialPipeline to bevy_material
1 parent 32c6b4d commit 3f4fd6b

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};
@@ -438,12 +438,6 @@ pub struct MaterialPipelineKey<M: Material> {
438438
pub bind_group_data: M::Data,
439439
}
440440

441-
/// Render pipeline data for a given [`Material`].
442-
#[derive(Resource, Clone)]
443-
pub struct MaterialPipeline {
444-
pub mesh_pipeline: MeshPipeline,
445-
}
446-
447441
pub struct MaterialPipelineSpecializer {
448442
pub(crate) pipeline: MaterialPipeline,
449443
pub(crate) properties: Arc<MaterialProperties>,
@@ -1371,84 +1365,7 @@ pub struct DeferredDrawFunction;
13711365
#[derive(DrawFunctionLabel, Debug, Hash, PartialEq, Eq, Clone, Default)]
13721366
pub struct ShadowsDrawFunction;
13731367

1374-
/// Common [`Material`] properties, calculated for a specific material instance.
1375-
#[derive(Default)]
1376-
pub struct MaterialProperties {
1377-
/// Is this material should be rendered by the deferred renderer when.
1378-
/// [`AlphaMode::Opaque`] or [`AlphaMode::Mask`]
1379-
pub render_method: OpaqueRendererMethod,
1380-
/// The [`AlphaMode`] of this material.
1381-
pub alpha_mode: AlphaMode,
1382-
/// The bits in the [`MeshPipelineKey`] for this material.
1383-
///
1384-
/// These are precalculated so that we can just "or" them together in
1385-
/// [`queue_material_meshes`].
1386-
pub mesh_pipeline_key_bits: MeshPipelineKey,
1387-
/// Add a bias to the view depth of the mesh which can be used to force a specific render order
1388-
/// for meshes with equal depth, to avoid z-fighting.
1389-
/// The bias is in depth-texture units so large values may be needed to overcome small depth differences.
1390-
pub depth_bias: f32,
1391-
/// Whether the material would like to read from [`ViewTransmissionTexture`](bevy_core_pipeline::core_3d::ViewTransmissionTexture).
1392-
///
1393-
/// This allows taking color output from the [`Opaque3d`] pass as an input, (for screen-space transmission) but requires
1394-
/// rendering to take place in a separate [`Transmissive3d`] pass.
1395-
pub reads_view_transmission_texture: bool,
1396-
pub render_phase_type: RenderPhaseType,
1397-
pub material_layout: Option<BindGroupLayoutDescriptor>,
1398-
/// Backing array is a size of 4 because the `StandardMaterial` needs 4 draw functions by default
1399-
pub draw_functions: SmallVec<[(InternedDrawFunctionLabel, DrawFunctionId); 4]>,
1400-
/// Backing array is a size of 3 because the `StandardMaterial` has 3 custom shaders (`frag`, `prepass_frag`, `deferred_frag`) which is the
1401-
/// most common use case
1402-
pub shaders: SmallVec<[(InternedShaderLabel, Handle<Shader>); 3]>,
1403-
/// Whether this material *actually* uses bindless resources, taking the
1404-
/// platform support (or lack thereof) of bindless resources into account.
1405-
pub bindless: bool,
1406-
pub specialize: Option<
1407-
fn(
1408-
&MaterialPipeline,
1409-
&mut RenderPipelineDescriptor,
1410-
&MeshVertexBufferLayoutRef,
1411-
ErasedMaterialPipelineKey,
1412-
) -> Result<(), SpecializedMeshPipelineError>,
1413-
>,
1414-
/// The key for this material, typically a bitfield of flags that are used to modify
1415-
/// the pipeline descriptor used for this material.
1416-
pub material_key: ErasedMaterialKey,
1417-
/// Whether shadows are enabled for this material
1418-
pub shadows_enabled: bool,
1419-
/// Whether prepass is enabled for this material
1420-
pub prepass_enabled: bool,
1421-
}
1422-
1423-
impl MaterialProperties {
1424-
pub fn get_shader(&self, label: impl ShaderLabel) -> Option<Handle<Shader>> {
1425-
self.shaders
1426-
.iter()
1427-
.find(|(inner_label, _)| inner_label == &label.intern())
1428-
.map(|(_, shader)| shader)
1429-
.cloned()
1430-
}
1431-
1432-
pub fn add_shader(&mut self, label: impl ShaderLabel, shader: Handle<Shader>) {
1433-
self.shaders.push((label.intern(), shader));
1434-
}
1435-
1436-
pub fn get_draw_function(&self, label: impl DrawFunctionLabel) -> Option<DrawFunctionId> {
1437-
self.draw_functions
1438-
.iter()
1439-
.find(|(inner_label, _)| inner_label == &label.intern())
1440-
.map(|(_, shader)| shader)
1441-
.cloned()
1442-
}
1443-
1444-
pub fn add_draw_function(
1445-
&mut self,
1446-
label: impl DrawFunctionLabel,
1447-
draw_function: DrawFunctionId,
1448-
) {
1449-
self.draw_functions.push((label.intern(), draw_function));
1450-
}
1451-
}
1368+
pub use bevy_material::material::MaterialProperties;
14521369

14531370
/// A resource that maps each untyped material ID to its binding.
14541371
///

0 commit comments

Comments
 (0)