Skip to content

Sys bindings update #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 65 commits into
base: wgpu-example-viewer
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
08f6c9d
update sys binding generation script
matthiascy Feb 8, 2023
30693be
update sys bindings generated against v3.13.5
matthiascy Feb 8, 2023
0943a72
thin RTCError wrapper & allow device creation to fail
matthiascy Feb 8, 2023
4198c6c
fix doc tests
matthiascy Feb 9, 2023
65c8397
format sys.rs
matthiascy Feb 9, 2023
2b7b18e
remove RTCError wrapper
matthiascy Feb 9, 2023
b7f73fd
fix typos
matthiascy Feb 9, 2023
a545367
resolve Twinklebear/embree-rs#23
matthiascy Feb 9, 2023
bf678b0
remove Arc from Scene and Device creation
matthiascy Feb 9, 2023
0288aad
update doc test
matthiascy Feb 9, 2023
54b4de2
refactor Scene creation as device method
matthiascy Feb 9, 2023
56b8af9
update doc test
matthiascy Feb 9, 2023
baaa561
start refactoring buffer
matthiascy Feb 9, 2023
10be858
tmp commit
matthiascy Feb 9, 2023
6b73de2
tmp commit
matthiascy Feb 10, 2023
315db83
refactor Buffer/Geometry
matthiascy Feb 13, 2023
8fcbd59
add rustfmt.toml configuration file
matthiascy Feb 13, 2023
61f6a22
minimal example & add all examples in workspace Twinklebear/embree-r#21
matthiascy Feb 14, 2023
074c6b5
update CI embree version
matthiascy Feb 14, 2023
a4f0802
try to include set shared buffer
matthiascy Feb 14, 2023
3d34068
refactor buffer & slice
matthiascy Feb 15, 2023
7360242
update minimal example
matthiascy Feb 15, 2023
6e22e89
fix some clippy warnings
matthiascy Feb 15, 2023
9a2a39c
remove Geometry trait
matthiascy Feb 16, 2023
3007353
try to port dynamic scene
matthiascy Feb 16, 2023
de8b864
add some geometry related methods
matthiascy Feb 17, 2023
bfae8d4
dynamic scene
matthiascy Feb 19, 2023
7f5b1cf
docs
matthiascy Feb 19, 2023
1fd75fc
remove geometry state
matthiascy Feb 19, 2023
0597773
set geometry mask
matthiascy Feb 19, 2023
0883655
implementation of geometry related callback functions
matthiascy Feb 20, 2023
6c62f09
update build test scripts & fix examples
matthiascy Feb 20, 2023
13be2ac
fix minimal
matthiascy Feb 20, 2023
f112cbb
put user/instance related methods under its own struct
matthiascy Feb 20, 2023
61ba9c8
started adopting instance example
matthiascy Feb 20, 2023
5443b89
user geometry payloads for callbacks
matthiascy Feb 21, 2023
5becb5a
instancing example done
matthiascy Feb 21, 2023
2b281e6
add quaternion decomposition
matthiascy Feb 21, 2023
b6bfde9
some intersection methods
matthiascy Feb 21, 2023
59e34f1
separate subdivision & impl safe ray packet access in callback functions
matthiascy Feb 22, 2023
f6abfda
implement all callback functions
matthiascy Feb 23, 2023
024993a
remove portable simd
matthiascy Feb 23, 2023
2b36bc1
remove user geometry
matthiascy Feb 23, 2023
c603dd9
clean displacement callback
matthiascy Feb 23, 2023
1212e30
displacement geometry example
matthiascy Feb 24, 2023
64c8802
temp update
matthiascy Feb 24, 2023
4a74e10
reduce code duplication
matthiascy Feb 27, 2023
405390c
remove geomtry directory & update workflows to use nightly toolchain
matthiascy Feb 27, 2023
7d94a36
update code formatting task
matthiascy Feb 27, 2023
83da23d
update examples
matthiascy Feb 28, 2023
a7822cd
tiled image
matthiascy Feb 28, 2023
1489607
update tiled image for displacement geometry
matthiascy Feb 28, 2023
b3d852e
trait for extending ray
matthiascy Mar 2, 2023
b79391e
poor man's inheritance for ray
matthiascy Mar 2, 2023
a9d5ba1
remove AsRay/AsRayHit
matthiascy Mar 2, 2023
b06d3b3
fix instancing & intersect filter
matthiascy Mar 2, 2023
d62f216
update some callback functions
matthiascy Mar 2, 2023
c170d09
api ergonomics improvement & performace boost & intersection filter
matthiascy Mar 6, 2023
5a7ed57
refactor ray/hit stream
matthiascy Mar 7, 2023
f428d9b
add miri test; fix memory leak & incorrect memory deallocation
matthiascy Mar 8, 2023
9b13930
update ci
matthiascy Mar 8, 2023
d5e06f5
remove marker
matthiascy Mar 8, 2023
18fb8ff
remove marker from aligned vector & add aligned array
matthiascy Mar 8, 2023
4b53172
egui integration
matthiascy Mar 9, 2023
b050b1e
examples performance boost & different shading method
matthiascy Mar 13, 2023
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
32 changes: 26 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,32 @@ name: CI
on: [push, pull_request]
env:
CARGO_TERM_COLOR: always
EMBREE_VERSION: 3.12.1
EMBREE_VERSION: 3.13.5
jobs:
check_format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rustfmt
- name: Format Core
run: cargo fmt -- --check
- name: Format Examples
run: scripts/check-examples-formatting.sh
miri:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Miri
run: |
rustup toolchain install nightly --component miri
rustup override set nightly
cargo miri setup
- name: Test with Miri
run: cargo miri test
build_linux:
runs-on: ubuntu-latest
steps:
Expand All @@ -19,10 +43,6 @@ jobs:
- run: cargo test
- run: cargo doc
- run: scripts/build-examples-linux-mac.sh
- name: Format Core
run: cargo fmt -- --check
- name: Format Examples
run: scripts/check-examples-formatting.sh
build_mac:
runs-on: macos-latest
steps:
Expand All @@ -34,7 +54,7 @@ jobs:
- run: unzip embree-${EMBREE_VERSION}.x86_64.macosx.zip
- run: source embree-${EMBREE_VERSION}.x86_64.macosx/embree-vars.sh
- run: echo "EMBREE_DIR=`pwd`/embree-${EMBREE_VERSION}.x86_64.macosx/" >> $GITHUB_ENV
- run: cp $EMBREE_DIR/lib/* .
- run: cp -r $EMBREE_DIR/lib/* .
- run: cargo build
- run: scripts/build-test-mac.sh
build_windows:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ target
Cargo.lock
*.png
*.dll
.idea

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ exclude = [
"examples/*"
]

[workspace]
members = [
"examples/*",
]
exclude = ["examples/todos"]
3 changes: 1 addition & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::env;
use std::path::PathBuf;
use std::{env, path::PathBuf};

fn main() {
println!("{:?}", env::var("EMBREE_DIR"));
Expand Down
16 changes: 16 additions & 0 deletions examples/displacement_geometry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "displacement_geometry"
authors = ["Yang Chen <matthiasychen@gmail.com>"]
version = "0.1.0"
edition = "2021"

[dependencies]
glam = "0.23.0"
embree = { path = "../.." }
support = { path = "../support" }
rayon = { version = "1.6.1", optional = true }

[features]
smooth_normals = []
rayon = ["dep:rayon"]
default = ["rayon"]
263 changes: 263 additions & 0 deletions examples/displacement_geometry/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
use embree::{
AlignedArray, BufferSlice, BufferUsage, Device, Format, Geometry, GeometryKind,
IntersectContext, Ray, RayHit, Scene, SceneFlags,
};
use glam::{vec3, Vec3};
use support::{
noise, rgba_to_u32, Camera, DebugState, ParallelIterator, RgbaImage, TiledImage,
DEFAULT_DISPLAY_HEIGHT, DEFAULT_DISPLAY_WIDTH, TILE_SIZE_X, TILE_SIZE_Y,
};

const EDGE_LEVEL: f32 = 256.0;
const NUM_INDICES: usize = 24;
const NUM_FACES: usize = 6;
#[allow(dead_code)]
const FACE_SIZE: usize = 4;

const CUBE_VERTICES: AlignedArray<f32, 32> = AlignedArray([
-1.0, -1.0, -1.0, 0.0, // 0
1.0, -1.0, -1.0, 0.0, // 1
1.0, -1.0, 1.0, 0.0, // 2
-1.0, -1.0, 1.0, 0.0, // 3
-1.0, 1.0, -1.0, 0.0, // 4
1.0, 1.0, -1.0, 0.0, // 5
1.0, 1.0, 1.0, 0.0, // 6
-1.0, 1.0, 1.0, 0.0, // 7
]);

const CUBE_INDICES: [u32; NUM_INDICES] = [
0, 4, 5, 1, // 0
1, 5, 6, 2, // 1
2, 6, 7, 3, // 2
0, 3, 7, 4, // 3
4, 7, 6, 5, // 4
0, 1, 2, 3, // 5
];

const CUBE_FACES: [u32; NUM_FACES] = [4; 6];

const LIGHT_DIR: [f32; 3] = [0.57; 3];

fn displacement(p: [f32; 3]) -> f32 {
let mut dn = 0.0;
let mut freq = 1.0;
while freq < 40.0 {
let n = noise([p[0] * freq, p[1] * freq, p[2] * freq]).abs();
dn += 1.4 * n * n / freq;
freq *= 2.0;
}
dn
}

#[cfg(feature = "smooth_normals")]
fn displacement_du_or_dv(p: Vec3, dp_du_or_dp_dv: Vec3) -> f32 {
let du_or_dv = 0.001;
(displacement((p + du_or_dv * dp_du_or_dp_dv).into()) - displacement(p.into())) / du_or_dv
}

fn create_cube(device: &Device) -> Geometry<'static> {
let mut geom = device.create_geometry(GeometryKind::SUBDIVISION).unwrap();
geom.set_buffer(
BufferUsage::VERTEX,
0,
Format::FLOAT3,
BufferSlice::from_slice(&CUBE_VERTICES.0, ..),
4 * std::mem::size_of::<f32>(),
8,
)
.unwrap();
geom.set_buffer(
BufferUsage::INDEX,
0,
Format::UINT,
BufferSlice::from_slice(&CUBE_INDICES, ..),
std::mem::size_of::<u32>(),
NUM_INDICES,
)
.unwrap();
geom.set_buffer(
BufferUsage::FACE,
0,
Format::UINT,
BufferSlice::from_slice(&CUBE_FACES, ..),
std::mem::size_of::<u32>(),
NUM_FACES,
)
.unwrap();

geom.set_new_buffer(
BufferUsage::LEVEL,
0,
Format::FLOAT,
std::mem::size_of::<f32>(),
NUM_INDICES,
)
.unwrap()
.view_mut::<f32>()
.unwrap()
.copy_from_slice(&[EDGE_LEVEL; NUM_INDICES]);
unsafe {
geom.set_displacement_function(
|_raw_geom, vertices, _prim_id, _time_step, _user_data: Option<&mut ()>| {
for (_, ng, p) in vertices.into_iter_mut() {
let disp = displacement([*p[0], *p[1], *p[2]]);
let dp = [disp * ng[0], disp * ng[1], disp * ng[2]];
*p[0] += dp[0];
*p[1] += dp[1];
*p[2] += dp[2];
}
},
);
}
geom.commit();
geom
}

fn create_ground_plane(device: &Device) -> Geometry<'static> {
let mut mesh = device.create_geometry(GeometryKind::QUAD).unwrap();
{
mesh.set_new_buffer(BufferUsage::VERTEX, 0, Format::FLOAT3, 16, 4)
.unwrap()
.view_mut::<[f32; 4]>()
.unwrap()
.copy_from_slice(&[
[-10.0, -2.0, -10.0, 0.0],
[-10.0, -2.0, 10.0, 0.0],
[10.0, -2.0, 10.0, 0.0],
[10.0, -2.0, -10.0, 0.0],
]);
mesh.set_new_buffer(BufferUsage::INDEX, 0, Format::UINT4, 16, 1)
.unwrap()
.view_mut::<[u32; 4]>()
.unwrap()
.copy_from_slice(&[[0, 1, 2, 3]]);
}
mesh.commit();
mesh
}

fn main() {
let device = Device::new().unwrap();
device.set_error_function(|err, msg| {
eprintln!("{}: {}", err, msg);
});
let mut scene = device.create_scene().unwrap();
scene.set_flags(SceneFlags::ROBUST);

let cube = create_cube(&device);
let ground = create_ground_plane(&device);

let ground_id = scene.attach_geometry(&ground);
let cube_id = scene.attach_geometry(&cube);
scene.commit();

let display = support::Display::new(
DEFAULT_DISPLAY_WIDTH,
DEFAULT_DISPLAY_HEIGHT,
"Dynamic Scene",
);

let state = DebugState {
scene: scene.clone(),
user: (),
};

support::display::run(
display,
state,
move |_, _| {},
move |image, camera, time, _| {
render_frame(image, camera, &scene, cube_id, ground_id);
},
|_| {},
);
}

fn render_pixel(
x: u32,
y: u32,
camera: &Camera,
scene: &Scene,
cube_id: u32,
ground_id: u32,
) -> u32 {
let dir = camera.ray_dir((x as f32 + 0.5, y as f32 + 0.5));
let mut ctx = IntersectContext::coherent();
let mut ray_hit = RayHit::from_ray(Ray::segment(
camera.pos.into(),
dir.into(),
0.0,
f32::INFINITY,
));
scene.intersect(&mut ctx, &mut ray_hit);

let mut color = vec3(0.0, 0.0, 0.0);
if ray_hit.hit.is_valid() {
let diffuse = if ray_hit.hit.geomID == cube_id {
vec3(0.9, 0.6, 0.5)
} else {
vec3(1.0, 0.0, 0.0)
};
color += diffuse * 0.5;

let normal = {
let mut n = Vec3::from(ray_hit.hit.unit_normal());
#[cfg(feature = "smooth_normals")]
{
use embree::{InterpolateInput, InterpolateOutput};
let hit_point: Vec3 = ray_hit.ray.hit_point().into();
if ray_hit.hit.geomID != ground_id {
let mut output = InterpolateOutput::new(3, true, true, false);
let cube = scene.get_geometry_unchecked(cube_id).unwrap();
cube.interpolate(
InterpolateInput {
prim_id: ray_hit.hit.primID,
u: ray_hit.hit.u,
v: ray_hit.hit.v,
usage: BufferUsage::VERTEX,
slot: 0,
},
&mut output,
);
let mut dp_du = Vec3::from_slice(output.dp_du().as_ref().unwrap());
let mut dp_dv = Vec3::from_slice(output.dp_dv().as_ref().unwrap());
let ng = dp_du.cross(dp_dv).normalize();
dp_du += ng * displacement_du_or_dv(hit_point, dp_du);
dp_dv += ng * displacement_du_or_dv(hit_point, dp_dv);
n = dp_du.cross(dp_dv).normalize()
}
}
n
};

let mut shadow_ray = Ray::segment(ray_hit.ray.hit_point(), LIGHT_DIR, 0.001, f32::INFINITY);

// Check if the shadow ray is occluded.
if !scene.occluded(&mut ctx, &mut shadow_ray) {
color += diffuse * glam::Vec3::from(LIGHT_DIR).dot(normal).clamp(0.0, 1.0);
}
}

rgba_to_u32(
(color.x * 255.0) as u8,
(color.y * 255.0) as u8,
(color.z * 255.0) as u8,
255,
)
}

fn render_frame(
frame: &mut TiledImage,
camera: &Camera,
scene: &Scene,
cube_id: u32,
ground_id: u32,
) {
frame.par_tiles_mut().for_each(|tile| {
tile.pixels.iter_mut().enumerate().for_each(|(i, pixel)| {
let x = tile.x + (i % tile.w as usize) as u32;
let y = tile.y + (i / tile.w as usize) as u32;
*pixel = render_pixel(x, y, camera, scene, cube_id, ground_id);
});
});
}
11 changes: 11 additions & 0 deletions examples/dynamic_scene/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "dynamic_scene"
version = "0.1.0"
authors = ["Yang Chen <matthiasychen@gmail.com>"]
edition = "2021"

[dependencies]
glam = "0.23.0"
embree = { path = "../.." }
support = { path = "../support" }

Loading