Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions crates/processing_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,8 @@ pub extern "C" fn processing_set_blend_mode(graphics_id: u64, mode: u8) {
error::clear_error();
let graphics_entity = Entity::from_bits(graphics_id);
error::check(|| {
let blend_state = processing::prelude::BlendMode::try_from(mode)?.to_blend_state();
graphics_record_command(graphics_entity, DrawCommand::BlendMode(blend_state))
let blend = processing::prelude::BlendMode::try_from(mode)?.to_blend();
graphics_record_command(graphics_entity, DrawCommand::BlendMode(blend))
});
}

Expand All @@ -652,7 +652,10 @@ pub extern "C" fn processing_set_custom_blend_mode(
let blend_state = custom_blend_state(
color_src, color_dst, color_op, alpha_src, alpha_dst, alpha_op,
)?;
graphics_record_command(graphics_entity, DrawCommand::BlendMode(Some(blend_state)))
graphics_record_command(
graphics_entity,
DrawCommand::BlendMode(Some(Blend::Equation(blend_state))),
)
});
}

Expand Down Expand Up @@ -2986,9 +2989,31 @@ pub extern "C" fn processing_material(window_id: u64, mat_id: u64) {
}

#[unsafe(no_mangle)]
pub extern "C" fn processing_material_set_alpha_mode(mat_id: u64, mode: u8, cutoff: f32) {
pub extern "C" fn processing_material_set_mask(mat_id: u64, cutoff: f32) {
error::clear_error();
error::check(|| material_set_mask(Entity::from_bits(mat_id), Some(cutoff)));
}

#[unsafe(no_mangle)]
pub extern "C" fn processing_material_no_mask(mat_id: u64) {
error::clear_error();
error::check(|| material_set_alpha_mode(Entity::from_bits(mat_id), mode, cutoff));
error::check(|| material_set_mask(Entity::from_bits(mat_id), None));
}

#[unsafe(no_mangle)]
pub extern "C" fn processing_material_set_blend_mode(mat_id: u64, mode: u8) {
error::clear_error();
error::check(|| {
let blend = processing::prelude::BlendMode::try_from(mode)?.to_material_blend();
material_set_blend_mode(Entity::from_bits(mat_id), Some(blend))
});
}

/// The material follows the current blend mode again.
#[unsafe(no_mangle)]
pub extern "C" fn processing_material_no_blend_mode(mat_id: u64) {
error::clear_error();
error::check(|| material_set_blend_mode(Entity::from_bits(mat_id), None));
}

#[unsafe(no_mangle)]
Expand Down Expand Up @@ -3024,7 +3049,10 @@ pub extern "C" fn processing_material_set_custom_blend_mode(
let blend_state = custom_blend_state(
color_src, color_dst, color_op, alpha_src, alpha_dst, alpha_op,
)?;
material_set_custom_blend(Entity::from_bits(mat_id), blend_state)
material_set_blend_mode(
Entity::from_bits(mat_id),
Some(Blend::Equation(blend_state)),
)
});
}

Expand Down
37 changes: 30 additions & 7 deletions crates/processing_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,8 @@ pub fn js_shear_y(graphics_id: BigInt, angle: f64) -> Result<()> {
pub fn js_set_blend_mode(graphics_id: BigInt, mode: u8) -> Result<()> {
let graphics = ent(graphics_id);
check((|| {
let blend_state = BlendMode::try_from(mode)?.to_blend_state();
graphics_record_command(graphics, DrawCommand::BlendMode(blend_state))
let blend = BlendMode::try_from(mode)?.to_blend();
graphics_record_command(graphics, DrawCommand::BlendMode(blend))
})())
}

Expand All @@ -505,7 +505,10 @@ pub fn js_set_custom_blend_mode(
let blend_state = custom_blend_state(
color_src, color_dst, color_op, alpha_src, alpha_dst, alpha_op,
)?;
graphics_record_command(graphics, DrawCommand::BlendMode(Some(blend_state)))
graphics_record_command(
graphics,
DrawCommand::BlendMode(Some(Blend::Equation(blend_state))),
)
})())
}

Expand Down Expand Up @@ -2088,9 +2091,29 @@ pub fn js_material(graphics_id: BigInt, mat_id: BigInt) -> Result<()> {
record(graphics_id, DrawCommand::Material(ent(mat_id)))
}

#[napi(js_name = "materialSetAlphaMode")]
pub fn js_material_set_alpha_mode(mat_id: BigInt, mode: u8, cutoff: f64) -> Result<()> {
check(material_set_alpha_mode(ent(mat_id), mode, cutoff as f32))
#[napi(js_name = "materialSetMask")]
pub fn js_material_set_mask(mat_id: BigInt, cutoff: f64) -> Result<()> {
check(material_set_mask(ent(mat_id), Some(cutoff as f32)))
}

#[napi(js_name = "materialNoMask")]
pub fn js_material_no_mask(mat_id: BigInt) -> Result<()> {
check(material_set_mask(ent(mat_id), None))
}

#[napi(js_name = "materialSetBlendMode")]
pub fn js_material_set_blend_mode(mat_id: BigInt, mode: u8) -> Result<()> {
let mat = ent(mat_id);
check((|| {
let blend = BlendMode::try_from(mode)?.to_material_blend();
material_set_blend_mode(mat, Some(blend))
})())
}

/// The material follows the current blend mode again.
#[napi(js_name = "materialNoBlendMode")]
pub fn js_material_no_blend_mode(mat_id: BigInt) -> Result<()> {
check(material_set_blend_mode(ent(mat_id), None))
}

#[napi(js_name = "materialSetDoubleSided")]
Expand Down Expand Up @@ -2123,7 +2146,7 @@ pub fn js_material_set_custom_blend_mode(
let blend_state = custom_blend_state(
color_src, color_dst, color_op, alpha_src, alpha_dst, alpha_op,
)?;
material_set_custom_blend(mat, blend_state)
material_set_blend_mode(mat, Some(Blend::Equation(blend_state)))
})())
}

Expand Down
7 changes: 6 additions & 1 deletion crates/processing_pyo3/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add("THRESHOLD", crate::filter::THRESHOLD_U8)?;
m.add("POSTERIZE", crate::filter::POSTERIZE_U8)?;
m.add("BLUR", crate::filter::BLUR_U8)?;
m.add("OPAQUE", crate::filter::OPAQUE_U8)?;
m.add("ERODE", crate::filter::ERODE_U8)?;
m.add("DILATE", crate::filter::DILATE_U8)?;

Expand Down Expand Up @@ -192,6 +191,12 @@ pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add("MULTIPLY", PyBlendMode::from_preset(BlendMode::Multiply))?;
m.add("SCREEN", PyBlendMode::from_preset(BlendMode::Screen))?;
m.add("REPLACE", PyBlendMode::from_preset(BlendMode::Replace))?;
// also the `filter(OPAQUE)` constant
m.add("OPAQUE", PyBlendMode::from_preset(BlendMode::Opaque))?;
m.add(
"PREMULTIPLIED",
PyBlendMode::from_preset(BlendMode::Premultiplied),
)?;

Ok(())
}
11 changes: 10 additions & 1 deletion crates/processing_pyo3/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ pub fn resolve_filter(
return resolve_user_filter(shader.entity, args, kwargs);
}

let Ok(kind_u8) = kind.extract::<u8>() else {
// `OPAQUE` is one constant, shared with `blend_mode()`
let opaque = kind
.extract::<PyRef<crate::graphics::PyBlendMode>>()
.is_ok_and(|mode| mode.is_opaque());
let kind_u8 = if opaque {
Ok(OPAQUE_U8)
} else {
kind.extract::<u8>()
};
let Ok(kind_u8) = kind_u8 else {
return Err(PyTypeError::new_err(
"filter(): first argument must be a filter constant (INVERT, GRAY, ...) or a Shader",
));
Expand Down
26 changes: 21 additions & 5 deletions crates/processing_pyo3/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,22 +232,38 @@ fn cuda_import_from_interface(
#[pyclass(name = "BlendMode", from_py_object)]
#[derive(Clone)]
pub struct PyBlendMode {
pub(crate) blend_state: Option<bevy::render::render_resource::BlendState>,
/// `None` is `BLEND`, the default
pub(crate) blend: Option<Blend>,
name: Option<&'static str>,
}

impl PyBlendMode {
pub(crate) fn from_preset(mode: BlendMode) -> Self {
Self {
blend_state: mode.to_blend_state(),
blend: mode.to_blend(),
name: Some(mode.name()),
}
}

/// The composite-shader mode code (the `BlendMode` discriminant) for this
/// blend mode, or `None` for custom modes with no named preset.
pub(crate) fn composite_mode(&self) -> Option<u32> {
self.name.and_then(BlendMode::from_name).map(|m| m as u32)
self.name
.and_then(BlendMode::from_name)
// the compositor has no code past `REPLACE`
.filter(|m| *m as u32 <= BlendMode::Replace as u32)
.map(|m| m as u32)
}

/// What a material set to this mode does; `BLEND` always alpha blends there.
pub(crate) fn material_blend(&self) -> Blend {
self.blend.unwrap_or(Blend::Equation(
bevy::render::render_resource::BlendState::ALPHA_BLENDING,
))
}

pub(crate) fn is_opaque(&self) -> bool {
self.blend == Some(Blend::Opaque)
}
}

Expand All @@ -268,7 +284,7 @@ impl PyBlendMode {
)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
Ok(Self {
blend_state: Some(blend_state),
blend: Some(Blend::Equation(blend_state)),
name: None,
})
}
Expand Down Expand Up @@ -2646,7 +2662,7 @@ impl Graphics {
}

pub fn blend_mode(&self, mode: &PyBlendMode) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::BlendMode(mode.blend_state))
graphics_record_command(self.entity, DrawCommand::BlendMode(mode.blend))
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

Expand Down
25 changes: 9 additions & 16 deletions crates/processing_pyo3/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ fn apply_kwargs(entity: Entity, kwargs: &Bound<'_, PyDict>) -> PyResult<()> {
"unlit" => material_set_unlit(entity, py_truthy(&value)?).map_err(rt)?,
"double_sided" => material_set_double_sided(entity, py_truthy(&value)?).map_err(rt)?,
"depth_write" => material_set_depth_write(entity, py_truthy(&value)?).map_err(rt)?,
"alpha_mode" => {
material_set_alpha_mode(entity, value.extract::<u8>()?, 0.5).map_err(rt)?
}
_ => {
let v = py_to_shader_value(&value)?;
material_set(entity, &name, v).map_err(rt)?;
Expand Down Expand Up @@ -197,18 +194,14 @@ impl Material {
apply_kwargs(self.entity, kwargs)
}

pub fn opaque(&self) -> PyResult<()> {
material_set_alpha_mode(self.entity, 0, 0.0).map_err(rt)
}

/// Discards fragments with alpha below `cutoff`.
#[pyo3(signature = (cutoff=0.5))]
pub fn mask(&self, cutoff: f32) -> PyResult<()> {
material_set_alpha_mode(self.entity, 1, cutoff).map_err(rt)
material_set_mask(self.entity, Some(cutoff)).map_err(rt)
}

pub fn transparent(&self) -> PyResult<()> {
material_set_alpha_mode(self.entity, 2, 0.0).map_err(rt)
pub fn no_mask(&self) -> PyResult<()> {
material_set_mask(self.entity, None).map_err(rt)
}

/// Skips lighting; errors on a custom shader, which does its own.
Expand All @@ -227,12 +220,12 @@ impl Material {
material_set_depth_write(self.entity, value).map_err(rt)
}

/// Blends with what's behind it using a `BlendMode` (`ADD`, `MULTIPLY`, a custom one, ...).
pub fn blend_mode(&self, mode: &crate::graphics::PyBlendMode) -> PyResult<()> {
let state = mode
.blend_state
.unwrap_or(bevy::render::render_resource::BlendState::ALPHA_BLENDING);
material_set_custom_blend(self.entity, state).map_err(rt)
/// How it combines with what's behind it (`OPAQUE`, `BLEND`, `ADD`, a custom `BlendMode`,
/// ...); `None` follows the current `blend_mode()`.
#[pyo3(signature = (mode, /))]
pub fn blend_mode(&self, mode: Option<PyRef<crate::graphics::PyBlendMode>>) -> PyResult<()> {
let blend = mode.map(|mode| mode.material_blend());
material_set_blend_mode(self.entity, blend).map_err(rt)
}
}

Expand Down
6 changes: 1 addition & 5 deletions crates/processing_render/src/gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,7 @@ pub fn material(
.resource_mut::<Assets<ProcessingExtendedMaterial>>()
.add(ExtendedMaterial {
base: standard,
extension: ProcessingMaterial {
blend_state: None,
depth_write: None,
instance_fill: false,
},
extension: ProcessingMaterial::default(),
});
let entity = world.spawn(UntypedMaterial(handle.untyped())).id();
Ok(entity)
Expand Down
12 changes: 7 additions & 5 deletions crates/processing_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2106,10 +2106,11 @@ pub fn material_set(
})
}

pub fn material_set_alpha_mode(entity: Entity, mode: u8, cutoff: f32) -> error::Result<()> {
/// Discards a material's fragments with alpha below `cutoff`; `None` keeps them all.
pub fn material_set_mask(entity: Entity, cutoff: Option<f32>) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(material::set_alpha_mode, (entity, mode, cutoff))
.run_system_cached_with(material::set_mask, (entity, cutoff))
.unwrap()
})
}
Expand Down Expand Up @@ -2138,13 +2139,14 @@ pub fn material_set_depth_write(entity: Entity, value: bool) -> error::Result<()
})
}

pub fn material_set_custom_blend(
/// How a material combines with what is behind it; `None` follows the current blend mode.
pub fn material_set_blend_mode(
entity: Entity,
blend: bevy::render::render_resource::BlendState,
blend: Option<render::command::Blend>,
) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(material::set_custom_blend, (entity, blend))
.run_system_cached_with(material::set_blend_mode, (entity, blend))
.unwrap()
})
}
Expand Down
7 changes: 7 additions & 0 deletions crates/processing_render/src/material/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ pub struct CustomMaterial {
/// the vertex input locations the vertex stage reads, as bits
pub vertex_inputs: u32,
pub blend_state: Option<BlendState>,
/// no blend mode of its own, so it follows the current one when drawn
pub inherit_blend: bool,
pub mask: Option<f32>,
pub alpha_mode: AlphaMode,
pub double_sided: Option<bool>,
pub depth_write: Option<bool>,
Expand Down Expand Up @@ -657,6 +660,8 @@ pub fn create_custom(
has_fragment,
vertex_inputs: stage_locations(&program.module, naga::ShaderStage::Vertex, false),
blend_state: None,
inherit_blend: true,
mask: None,
alpha_mode: AlphaMode::Opaque,
double_sided: None,
depth_write: None,
Expand Down Expand Up @@ -1192,6 +1197,8 @@ mod tests {
has_fragment: true,
vertex_inputs: 0,
blend_state: None,
inherit_blend: true,
mask: None,
alpha_mode: AlphaMode::Opaque,
double_sided: None,
depth_write: None,
Expand Down
Loading
Loading