Skip to content

Commit b6b3c38

Browse files
committed
Remove vertex/frag
1 parent e85b02c commit b6b3c38

File tree

5 files changed

+36
-41
lines changed

5 files changed

+36
-41
lines changed

crates/processing_pyo3/examples/custom_material.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ def setup():
77
size(800, 600)
88
mode_3d()
99

10-
frag = Shader.load("shaders/custom_material.wesl")
11-
mat = Material(fragment=frag, color=[1.0, 0.2, 0.4, 1.0])
10+
shader = Shader.load("shaders/custom_material.wesl")
11+
mat = Material(shader, color=[1.0, 0.2, 0.4, 1.0])
1212

1313
def draw():
1414
camera_position(0.0, 0.0, 200.0)

crates/processing_pyo3/src/material.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,10 @@ fn py_to_material_value(value: &Bound<'_, PyAny>) -> PyResult<material::Material
3737
#[pymethods]
3838
impl Material {
3939
#[new]
40-
#[pyo3(signature = (vertex=None, fragment=None, **kwargs))]
41-
pub fn new(
42-
vertex: Option<&Shader>,
43-
fragment: Option<&Shader>,
44-
kwargs: Option<&Bound<'_, PyDict>>,
45-
) -> PyResult<Self> {
46-
let entity = if vertex.is_some() || fragment.is_some() {
47-
material_create_custom(vertex.map(|s| s.entity), fragment.map(|s| s.entity))
40+
#[pyo3(signature = (shader=None, **kwargs))]
41+
pub fn new(shader: Option<&Shader>, kwargs: Option<&Bound<'_, PyDict>>) -> PyResult<Self> {
42+
let entity = if let Some(shader) = shader {
43+
material_create_custom(shader.entity)
4844
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?
4945
} else {
5046
material_create_pbr().map_err(|e| PyRuntimeError::new_err(format!("{e}")))?

crates/processing_render/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,13 +1333,10 @@ pub fn shader_destroy(entity: Entity) -> error::Result<()> {
13331333
})
13341334
}
13351335

1336-
pub fn material_create_custom(
1337-
vertex: Option<Entity>,
1338-
fragment: Option<Entity>,
1339-
) -> error::Result<Entity> {
1336+
pub fn material_create_custom(shader: Entity) -> error::Result<Entity> {
13401337
app_mut(|app| {
13411338
app.world_mut()
1342-
.run_system_cached_with(material::custom::create_custom, (vertex, fragment))
1339+
.run_system_cached_with(material::custom::create_custom, shader)
13431340
.unwrap()
13441341
})
13451342
}

crates/processing_render/src/material/custom.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ use crate::render::material::UntypedMaterial;
5050
#[derive(Asset, TypePath, Clone)]
5151
pub struct CustomMaterial {
5252
pub shader: DynamicShader,
53-
pub vertex_shader: Option<Handle<ShaderAsset>>,
54-
pub fragment_shader: Option<Handle<ShaderAsset>>,
53+
pub shader_handle: Handle<ShaderAsset>,
54+
pub has_vertex: bool,
55+
pub has_fragment: bool,
5556
}
5657

5758
#[derive(Component)]
@@ -197,34 +198,35 @@ pub fn destroy_shader(In(entity): In<Entity>, mut commands: Commands) -> Result<
197198
}
198199

199200
pub fn create_custom(
200-
In((vertex_entity, fragment_entity)): In<(Option<Entity>, Option<Entity>)>,
201+
In(shader_entity): In<Entity>,
201202
mut commands: Commands,
202203
shader_programs: Query<&Shader>,
203204
mut custom_materials: ResMut<Assets<CustomMaterial>>,
204205
) -> Result<Entity> {
205-
let vertex_program = vertex_entity
206-
.map(|e| shader_programs.get(e))
207-
.transpose()
208-
.map_err(|_| ProcessingError::ShaderNotFound)?;
209-
let fragment_program = fragment_entity
210-
.map(|e| shader_programs.get(e))
211-
.transpose()
206+
let program = shader_programs
207+
.get(shader_entity)
212208
.map_err(|_| ProcessingError::ShaderNotFound)?;
213209

214-
// Prefer fragment module for reflection, fall back to vertex.
215-
let reflection_module = fragment_program
216-
.map(|p| &p.module)
217-
.or(vertex_program.map(|p| &p.module))
218-
.ok_or(ProcessingError::ShaderNotFound)?;
219-
220-
let mut shader = DynamicShader::new(reflection_module.clone())
210+
let has_vertex = program
211+
.module
212+
.entry_points
213+
.iter()
214+
.any(|ep| ep.stage == naga::ShaderStage::Vertex);
215+
let has_fragment = program
216+
.module
217+
.entry_points
218+
.iter()
219+
.any(|ep| ep.stage == naga::ShaderStage::Fragment);
220+
221+
let mut shader = DynamicShader::new(program.module.clone())
221222
.map_err(|e| ProcessingError::ShaderCompilationError(e.to_string()))?;
222223
shader.init();
223224

224225
let material = CustomMaterial {
225226
shader,
226-
vertex_shader: vertex_program.map(|p| p.shader_handle.clone()),
227-
fragment_shader: fragment_program.map(|p| p.shader_handle.clone()),
227+
shader_handle: program.shader_handle.clone(),
228+
has_vertex,
229+
has_fragment,
228230
};
229231
let handle = custom_materials.add(material);
230232
Ok(commands.spawn(UntypedMaterial(handle.untyped())).id())
@@ -401,11 +403,11 @@ impl ErasedRenderAsset for CustomMaterial {
401403
..Default::default()
402404
};
403405
properties.add_draw_function(MainPassOpaqueDrawFunction, draw_function);
404-
if let Some(vertex) = &source_asset.vertex_shader {
405-
properties.add_shader(MaterialVertexShader, vertex.clone());
406+
if source_asset.has_vertex {
407+
properties.add_shader(MaterialVertexShader, source_asset.shader_handle.clone());
406408
}
407-
if let Some(fragment) = &source_asset.fragment_shader {
408-
properties.add_shader(MaterialFragmentShader, fragment.clone());
409+
if source_asset.has_fragment {
410+
properties.add_shader(MaterialFragmentShader, source_asset.shader_handle.clone());
409411
}
410412

411413
Ok(PreparedMaterial {

examples/custom_material.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ fn sketch() -> error::Result<()> {
3131
transform_set_position(graphics, 100.0, 100.0, 300.0)?;
3232
transform_look_at(graphics, 0.0, 0.0, 0.0)?;
3333

34-
let frag = shader_load("shaders/custom_material.wesl")?;
35-
let mat = material_create_custom(None, Some(frag))?;
34+
let shader = shader_load("shaders/custom_material.wesl")?;
35+
let mat = material_create_custom(shader)?;
3636
material_set(
3737
mat,
3838
"color",
@@ -61,7 +61,7 @@ fn sketch() -> error::Result<()> {
6161
}
6262

6363
material_destroy(mat)?;
64-
shader_destroy(frag)?;
64+
shader_destroy(shader)?;
6565

6666
Ok(())
6767
}

0 commit comments

Comments
 (0)