Skip to content

Commit

Permalink
Adding support for the raytracing. (#252)
Browse files Browse the repository at this point in the history
* It closes #245,
Increases the foreign-type version,
Adds a new example for showing raytracing capabilities in metal 3.
CI action version increase
code improvement in foreign_obj_type macro.
And adding several other needed features in the code.

* Increases the version of the Rust

* Build fix for mps module and a warning has been removed.

* fixing the build for stable

* cargo test fix

* a little doc

* All of the uniquely borrowed refrences removed.

* several warning fixed in the ratracing example, it was due to the recent change in the API

* All the modules now private again and only the mps one is public.
  • Loading branch information
Hossein-Noroozpour authored Dec 28, 2022
1 parent 27dc596 commit b710f05
Show file tree
Hide file tree
Showing 39 changed files with 2,082 additions and 198 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
channel: [stable, nightly]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3

- uses: actions-rs/cargo@v1

Expand All @@ -30,19 +30,19 @@ jobs:
run: rustc --version && cargo --version

- name: Cache cargo registry
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-${{ matrix.channel }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}

- name: Cache cargo index
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: ~/.cargo/git
key: ${{ runner.os }}-${{ matrix.channel }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}

- name: Cache cargo build
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: target
key: ${{ runner.os }}-${{ matrix.channel }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
Expand Down
12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "metal"
version = "0.24.0"
version = "0.25.0"
description = "Rust bindings for Metal"
documentation = "https://docs.rs/crate/metal"
homepage = "https://github.com/gfx-rs/metal-rs"
Expand All @@ -9,7 +9,7 @@ authors = ["GFX Developers"]
readme = "README.md"
keywords = ["metal", "graphics", "bindings"]
license = "MIT OR Apache-2.0"
edition = "2018"
edition = "2021"
exclude = ["guide/**/*", "examples/texture/**/*", "tests/**/*", "Cargo.lock", "target/**/*"]

[package.metadata.docs.rs]
Expand All @@ -25,8 +25,9 @@ core-graphics-types = "0.1"
bitflags = "1"
log = "0.4"
block = "0.1.6"
foreign-types = "0.3.2"
foreign-types = "0.5"
dispatch = { version = "0.2", optional = true }
paste = "1"

[dependencies.objc]
version = "0.2.4"
Expand All @@ -38,6 +39,8 @@ cty = "0.2.1"
winit = "0.27"
sema = "0.1.4"
png = "0.17"
glam = "0.22"
rand = "0.8"

[[example]]
name = "window"
Expand All @@ -48,6 +51,9 @@ name = "headless-render"
[[example]]
name = "library"

[[example]]
name = "raytracing"

[[example]]
name = "reflection"

Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ window:
circle:
xcrun -sdk macosx metal -c examples/circle/shaders.metal -o examples/circle/shaders.air
xcrun -sdk macosx metallib examples/circle/shaders.air -o examples/circle/shaders.metallib

raytracing:
xcrun -sdk macosx metal -c -g examples/raytracing/shaders.metal -o examples/raytracing/shaders.air
xcrun -sdk macosx metallib examples/raytracing/shaders.air -o examples/raytracing/shaders.metallib
18 changes: 9 additions & 9 deletions examples/mps/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ struct Vertex {
xyz: [f32; 3],
}

type Ray = MPSRayOriginMinDistanceDirectionMaxDistance;
type Intersection = MPSIntersectionDistancePrimitiveIndexCoordinates;
type Ray = mps::MPSRayOriginMinDistanceDirectionMaxDistance;
type Intersection = mps::MPSIntersectionDistancePrimitiveIndexCoordinates;

// Original example taken from https://sergeyreznik.github.io/metal-ray-tracer/part-1/index.html
fn main() {
Expand Down Expand Up @@ -61,25 +61,25 @@ fn main() {
);

// Build an acceleration structure using our vertex and index buffers containing the single triangle.
let acceleration_structure = TriangleAccelerationStructure::from_device(&device)
let acceleration_structure = mps::TriangleAccelerationStructure::from_device(&device)
.expect("Failed to create acceleration structure");

acceleration_structure.set_vertex_buffer(Some(&vertex_buffer));
acceleration_structure.set_vertex_stride(vertex_stride as u64);
acceleration_structure.set_index_buffer(Some(&index_buffer));
acceleration_structure.set_index_type(MPSDataType::UInt32);
acceleration_structure.set_index_type(mps::MPSDataType::UInt32);
acceleration_structure.set_triangle_count(1);
acceleration_structure.set_usage(MPSAccelerationStructureUsage::None);
acceleration_structure.set_usage(mps::MPSAccelerationStructureUsage::None);
acceleration_structure.rebuild();

let ray_intersector =
RayIntersector::from_device(&device).expect("Failed to create ray intersector");
mps::RayIntersector::from_device(&device).expect("Failed to create ray intersector");

ray_intersector.set_ray_stride(mem::size_of::<Ray>() as u64);
ray_intersector.set_ray_data_type(MPSRayDataType::OriginMinDistanceDirectionMaxDistance);
ray_intersector.set_ray_data_type(mps::MPSRayDataType::OriginMinDistanceDirectionMaxDistance);
ray_intersector.set_intersection_stride(mem::size_of::<Intersection>() as u64);
ray_intersector
.set_intersection_data_type(MPSIntersectionDataType::DistancePrimitiveIndexCoordinates);
.set_intersection_data_type(mps::MPSIntersectionDataType::DistancePrimitiveIndexCoordinates);

// Create a buffer to hold generated rays and intersection results
let ray_count = 1024;
Expand Down Expand Up @@ -114,7 +114,7 @@ fn main() {
// Intersect rays with triangles inside acceleration structure
ray_intersector.encode_intersection_to_command_buffer(
&command_buffer,
MPSIntersectionType::Nearest,
mps::MPSIntersectionType::Nearest,
&ray_buffer,
0,
&intersection_buffer,
Expand Down
11 changes: 11 additions & 0 deletions examples/raytracing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Raytracing

A good showcase of Metal 3 raytracing features.

![Screenshot of the final render](./screenshot.png)

## To Run

```
cargo run --example raytracing
```
20 changes: 20 additions & 0 deletions examples/raytracing/camera.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use glam::f32::Vec4;

#[repr(C)]
pub struct Camera {
pub position: Vec4,
pub right: Vec4,
pub up: Vec4,
pub forward: Vec4,
}

impl Camera {
pub fn new() -> Self {
Self {
position: Vec4::new(0.0, 3.0, 10.0, 0.0),
right: Vec4::new(1.0, 0.0, 0.0, 0.0),
up: Vec4::new(0.0, 1.0, 0.0, 0.0),
forward: Vec4::new(0.0, 0.0, -1.0, 0.0),
}
}
}
Loading

0 comments on commit b710f05

Please sign in to comment.