Skip to content

Commit a7397d5

Browse files
committed
3D strokes, vertex baking and draw pooling.
1 parent 50f5451 commit a7397d5

30 files changed

Lines changed: 1316 additions & 313 deletions

File tree

‎crates/processing_core/src/constants.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ pub const ITALIC: &str = "italic";
3838
pub const BOLD: &str = "bold";
3939
pub const BOLD_ITALIC: &str = "bold_italic";
4040

41+
pub const ENABLE_STROKE_PERSPECTIVE: &str = "enable_stroke_perspective"; // hint
42+
pub const DISABLE_STROKE_PERSPECTIVE: &str = "disable_stroke_perspective";
43+
4144
pub const WORD: &str = "word"; // text wrap mode
4245
pub const CHAR: &str = "char";
4346

‎crates/processing_ffi/src/lib.rs‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,15 @@ pub extern "C" fn processing_circle(graphics_id: u64, cx: f32, cy: f32, d: f32)
714714
pub extern "C" fn processing_line(graphics_id: u64, x1: f32, y1: f32, x2: f32, y2: f32) {
715715
error::clear_error();
716716
let graphics_entity = Entity::from_bits(graphics_id);
717-
error::check(|| graphics_record_command(graphics_entity, DrawCommand::Line { x1, y1, x2, y2 }));
717+
error::check(|| {
718+
graphics_record_command(
719+
graphics_entity,
720+
DrawCommand::Line {
721+
from: Vec3::new(x1, y1, 0.0),
722+
to: Vec3::new(x2, y2, 0.0),
723+
},
724+
)
725+
});
718726
}
719727

720728
/// Draw a triangle.
@@ -782,7 +790,14 @@ pub extern "C" fn processing_quad(
782790
pub extern "C" fn processing_point(graphics_id: u64, x: f32, y: f32) {
783791
error::clear_error();
784792
let graphics_entity = Entity::from_bits(graphics_id);
785-
error::check(|| graphics_record_command(graphics_entity, DrawCommand::Point { x, y }));
793+
error::check(|| {
794+
graphics_record_command(
795+
graphics_entity,
796+
DrawCommand::Point {
797+
at: Vec3::new(x, y, 0.0),
798+
},
799+
)
800+
});
786801
}
787802

788803
/// Draw a square.

‎crates/processing_node/src/lib.rs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,8 @@ pub fn js_line(graphics_id: BigInt, x1: f64, y1: f64, x2: f64, y2: f64) -> Resul
564564
record(
565565
graphics_id,
566566
DrawCommand::Line {
567-
x1: x1 as f32,
568-
y1: y1 as f32,
569-
x2: x2 as f32,
570-
y2: y2 as f32,
567+
from: vec3(x1, y1, 0.0),
568+
to: vec3(x2, y2, 0.0),
571569
},
572570
)
573571
}
@@ -627,8 +625,7 @@ pub fn js_point(graphics_id: BigInt, x: f64, y: f64) -> Result<()> {
627625
record(
628626
graphics_id,
629627
DrawCommand::Point {
630-
x: x as f32,
631-
y: y as f32,
628+
at: vec3(x, y, 0.0),
632629
},
633630
)
634631
}

‎crates/processing_pyo3/src/constants.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
4242
add!(m, TOP, BOTTOM, BASELINE);
4343
add!(m, NORMAL, ITALIC, BOLD, BOLD_ITALIC);
4444
add!(m, IMAGE);
45+
add!(m, ENABLE_STROKE_PERSPECTIVE, DISABLE_STROKE_PERSPECTIVE);
4546
add!(m, WORD, CHAR);
4647
add!(m, NEAREST, CLAMP, REPEAT, MIRROR);
4748
add!(m, SRGB, LINEAR, HSL, HSV, HWB, OKLAB, OKLCH, LAB, LCH, XYZ);

‎crates/processing_pyo3/src/graphics.rs‎

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,8 +1522,28 @@ impl Graphics {
15221522
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
15231523
}
15241524

1525-
pub fn line(&self, x1: f32, y1: f32, x2: f32, y2: f32) -> PyResult<()> {
1526-
graphics_record_command(self.entity, DrawCommand::Line { x1, y1, x2, y2 })
1525+
/// `line(x1, y1, x2, y2)`, or in 3D `line(x1, y1, z1, x2, y2, z2)`.
1526+
#[pyo3(signature = (x1, y1, x2, y2, *args))]
1527+
pub fn line(
1528+
&self,
1529+
x1: f32,
1530+
y1: f32,
1531+
x2: f32,
1532+
y2: f32,
1533+
args: &Bound<'_, PyTuple>,
1534+
) -> PyResult<()> {
1535+
let rest: Vec<f32> = args.extract()?;
1536+
let (from, to) = match rest[..] {
1537+
[] => (Vec3::new(x1, y1, 0.0), Vec3::new(x2, y2, 0.0)),
1538+
// six numbers read as two points
1539+
[a, b] => (Vec3::new(x1, y1, x2), Vec3::new(y2, a, b)),
1540+
_ => {
1541+
return Err(pyo3::exceptions::PyTypeError::new_err(
1542+
"line() takes 4 numbers, or 6 in 3D",
1543+
));
1544+
}
1545+
};
1546+
graphics_record_command(self.entity, DrawCommand::Line { from, to })
15271547
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
15281548
}
15291549

@@ -1569,9 +1589,15 @@ impl Graphics {
15691589
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
15701590
}
15711591

1572-
pub fn point(&self, x: f32, y: f32) -> PyResult<()> {
1573-
graphics_record_command(self.entity, DrawCommand::Point { x, y })
1574-
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
1592+
#[pyo3(signature = (x, y, z=0.0))]
1593+
pub fn point(&self, x: f32, y: f32, z: f32) -> PyResult<()> {
1594+
graphics_record_command(
1595+
self.entity,
1596+
DrawCommand::Point {
1597+
at: Vec3::new(x, y, z),
1598+
},
1599+
)
1600+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
15751601
}
15761602

15771603
pub fn square(&self, x: f32, y: f32, s: f32) -> PyResult<()> {
@@ -1710,6 +1736,22 @@ impl Graphics {
17101736
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
17111737
}
17121738

1739+
/// Changes a renderer setting (Processing `hint`).
1740+
pub fn hint(&self, which: &str) -> PyResult<()> {
1741+
use processing::prelude::constants as consts;
1742+
let perspective = match () {
1743+
_ if which.eq_ignore_ascii_case(consts::ENABLE_STROKE_PERSPECTIVE) => true,
1744+
_ if which.eq_ignore_ascii_case(consts::DISABLE_STROKE_PERSPECTIVE) => false,
1745+
_ => {
1746+
return Err(PyValueError::new_err(format!(
1747+
"unsupported hint: {which:?}"
1748+
)));
1749+
}
1750+
};
1751+
graphics_record_command(self.entity, DrawCommand::StrokePerspective(perspective))
1752+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
1753+
}
1754+
17131755
/// `IMAGE` reads `vertex()` texture coordinates in pixels of the texture, `NORMAL` from 0 to 1.
17141756
pub fn texture_mode(&self, mode: &str) -> PyResult<()> {
17151757
let mode = processing::prelude::TextureMode::parse(mode)

‎crates/processing_pyo3/src/lib.rs‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,9 +2498,16 @@ pub mod processing_py {
24982498
}
24992499

25002500
#[pyfunction]
2501-
#[pyo3(pass_module)]
2502-
fn line(module: &Bound<'_, PyModule>, x1: f32, y1: f32, x2: f32, y2: f32) -> PyResult<()> {
2503-
graphics!(module).line(x1, y1, x2, y2)
2501+
#[pyo3(pass_module, signature = (x1, y1, x2, y2, *args))]
2502+
fn line(
2503+
module: &Bound<'_, PyModule>,
2504+
x1: f32,
2505+
y1: f32,
2506+
x2: f32,
2507+
y2: f32,
2508+
args: &Bound<'_, PyTuple>,
2509+
) -> PyResult<()> {
2510+
graphics!(module).line(x1, y1, x2, y2, args)
25042511
}
25052512

25062513
#[pyfunction]
@@ -2534,9 +2541,9 @@ pub mod processing_py {
25342541
}
25352542

25362543
#[pyfunction]
2537-
#[pyo3(pass_module)]
2538-
fn point(module: &Bound<'_, PyModule>, x: f32, y: f32) -> PyResult<()> {
2539-
graphics!(module).point(x, y)
2544+
#[pyo3(pass_module, signature = (x, y, z=0.0))]
2545+
fn point(module: &Bound<'_, PyModule>, x: f32, y: f32, z: f32) -> PyResult<()> {
2546+
graphics!(module).point(x, y, z)
25402547
}
25412548

25422549
#[pyfunction]
@@ -2621,6 +2628,12 @@ pub mod processing_py {
26212628
graphics!(module).normal(args)
26222629
}
26232630

2631+
#[pyfunction]
2632+
#[pyo3(pass_module)]
2633+
fn hint(module: &Bound<'_, PyModule>, which: &str) -> PyResult<()> {
2634+
graphics!(module).hint(which)
2635+
}
2636+
26242637
#[pyfunction]
26252638
#[pyo3(pass_module)]
26262639
fn texture_mode(module: &Bound<'_, PyModule>, mode: &str) -> PyResult<()> {

‎crates/processing_render/src/gltf.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ pub fn material(
334334
extension: ProcessingMaterial {
335335
blend_state: None,
336336
depth_write: None,
337+
instance_fill: false,
337338
},
338339
});
339340
let entity = world.spawn(UntypedMaterial(handle.untyped())).id();

‎crates/processing_render/src/graphics.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,10 @@ pub fn create(
339339
CommandBuffer::new(),
340340
RenderState::default(),
341341
crate::image::BackgroundBlit::default(),
342-
crate::color::ColorMode::default(),
342+
(
343+
crate::color::ColorMode::default(),
344+
crate::render::pool::DrawPool::default(),
345+
),
343346
SurfaceSize(width, height),
344347
Graphics {
345348
readback_buffer,

‎crates/processing_render/src/lib.rs‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ use crate::render::command::DrawCommand;
6969
#[derive(Component)]
7070
pub struct Flush;
7171

72+
/// Registers one of the renderer's own shaders under `$handle` while the app is built.
73+
macro_rules! internal_shader {
74+
($app:expr, $handle:expr, $path:literal) => {{
75+
let _ = $app
76+
.world_mut()
77+
.resource_mut::<bevy::asset::Assets<bevy::shader::Shader>>()
78+
.insert(
79+
$handle.id(),
80+
bevy::shader::Shader::from_wgsl(include_str!($path), concat!(file!(), "/", $path)),
81+
);
82+
}};
83+
}
84+
pub(crate) use internal_shader;
85+
7286
pub struct ProcessingRenderPlugin;
7387

7488
impl Plugin for ProcessingRenderPlugin {
@@ -756,10 +770,13 @@ pub fn graphics_get_color_mode(graphics_entity: Entity) -> error::Result<color::
756770

757771
/// Record a drawing command for a window
758772
pub fn graphics_record_command(graphics_entity: Entity, cmd: DrawCommand) -> error::Result<()> {
773+
// no system dispatch: called once per draw command
759774
app_mut(|app| {
760775
app.world_mut()
761-
.run_system_cached_with(graphics::record_command, (graphics_entity, cmd))
762-
.unwrap()
776+
.get_mut::<render::command::CommandBuffer>(graphics_entity)
777+
.ok_or(error::ProcessingError::GraphicsNotFound)?
778+
.push(cmd);
779+
Ok(())
763780
})
764781
}
765782

‎crates/processing_render/src/material/mod.rs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ use bevy::shader::ShaderRef;
1616
use bevy_naga_reflect::reflect::ParameterCategory;
1717
use processing_core::error::{self, ProcessingError};
1818

19+
/// The StandardMaterial fragment shader with the per-instance fill.
20+
const PROCESSING_SHADER: Handle<Shader> =
21+
bevy::asset::uuid_handle!("6f1f3c1e-2b7a-4d0e-9a53-8c2f4be7a901");
22+
1923
pub struct ProcessingMaterialPlugin;
2024

2125
impl Plugin for ProcessingMaterialPlugin {
2226
fn build(&self, app: &mut App) {
27+
crate::internal_shader!(app, PROCESSING_SHADER, "processing.wgsl");
2328
app.add_plugins(bevy::pbr::MaterialPlugin::<
2429
ExtendedMaterial<StandardMaterial, ProcessingMaterial>,
2530
>::default());
@@ -54,6 +59,7 @@ pub fn create_pbr(
5459
extension: ProcessingMaterial {
5560
blend_state: None,
5661
depth_write: None,
62+
instance_fill: false,
5763
},
5864
});
5965
commands.spawn(UntypedMaterial(handle.untyped())).id()
@@ -387,20 +393,24 @@ pub fn destroy(
387393
pub struct ProcessingMaterial {
388394
pub blend_state: Option<BlendState>,
389395
pub depth_write: Option<bool>,
396+
/// the fill is the sRGB8 RGBA color packed in each instance's `MeshTag`
397+
pub instance_fill: bool,
390398
}
391399

392400
#[repr(C)]
393401
#[derive(Eq, PartialEq, Hash, Copy, Clone)]
394402
pub struct ProcessingMaterialKey {
395403
blend_state: Option<BlendState>,
396404
depth_write: Option<bool>,
405+
instance_fill: bool,
397406
}
398407

399408
impl From<&ProcessingMaterial> for ProcessingMaterialKey {
400409
fn from(mat: &ProcessingMaterial) -> Self {
401410
ProcessingMaterialKey {
402411
blend_state: mat.blend_state,
403412
depth_write: mat.depth_write,
413+
instance_fill: mat.instance_fill,
404414
}
405415
}
406416
}
@@ -411,7 +421,7 @@ impl MaterialExtension for ProcessingMaterial {
411421
}
412422

413423
fn fragment_shader() -> ShaderRef {
414-
<StandardMaterial as Material>::fragment_shader()
424+
PROCESSING_SHADER.into()
415425
}
416426

417427
fn specialize(
@@ -425,6 +435,11 @@ impl MaterialExtension for ProcessingMaterial {
425435
key.bind_group_data.blend_state,
426436
key.bind_group_data.depth_write,
427437
);
438+
if key.bind_group_data.instance_fill
439+
&& let Some(fragment) = &mut descriptor.fragment
440+
{
441+
fragment.shader_defs.push("INSTANCE_FILL".into());
442+
}
428443
Ok(())
429444
}
430445
}

0 commit comments

Comments
 (0)