Skip to content
Merged
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: CI

on:
pull_request:
push:
branches: [master]

env:
CARGO_TERM_COLOR: always
RUSTFLAGS: -D warnings

jobs:
test:
name: ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- uses: actions/checkout@v4

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: clippy

- uses: Swatinem/rust-cache@v2

- name: Clippy
run: cargo clippy --workspace --all-targets

- name: Build
run: cargo build --workspace --all-targets

- name: Test
run: cargo test --workspace
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ members = [
"unity-native-plugin-sample",
"unity-native-plugin-sample-profiler",
]
resolver = "2"
resolver = "3"

12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ Unity Native Plugin API for Rust

## Notice

* WIP
* Currently supports D3D11, D3D12, Vulkan
* Currently supports D3D11, D3D12, Vulkan, Metal
* API is not stable.

## How to use
Expand All @@ -19,16 +18,13 @@ unity-native-plugin = { version = "*", features = ["d3d11"] }
# * Support features
# * d3d11 - IUnityGraphicsD3D11
# * d3d12 - IUnityGraphicsD3D12
# * vulkan - IUnityGraphicsVulkan
# * metal - IUnityGraphicsMetal
# * profiler - IUnityProfiler
# * profiler_callbacks - IUnityProfilerCallbacks
```

* If you use Vulkan, define "unity-native-plugin-vulkan" in your dependencies.
```cargo
[dependencies]
unity-native-plugin = "*"
unity-native-plugin-vulkan = "*"
```
* Vulkan support has been integrated into `unity-native-plugin`. No separate crate needs to be added to your dependencies.

* Use a macro in lib.rs to define your entry points. Without this definition, UnityInterfaces cannot be used.
```rust
Expand Down
8 changes: 6 additions & 2 deletions unity-native-plugin-sample-profiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,17 @@ fn plugin_load(interfaces: &unity_native_plugin::interface::UnityInterfaces) {
}
};
let pid = 1;
let tid: NonZeroU64 = unsafe { std::mem::transmute(std::thread::current().id()) };
let tid: NonZeroU64 = unsafe {
std::mem::transmute::<std::thread::ThreadId, NonZeroU64>(
std::thread::current().id(),
)
};
let ts = Instant::now();
let dt = ts - start_ts;
let cat = desc.desc.category_id();
let cat: BuiltinProfilerCategory =
if cat <= BuiltinProfilerCategory::VirtualTexturing as u16 {
unsafe { std::mem::transmute(cat) }
unsafe { std::mem::transmute::<u16, BuiltinProfilerCategory>(cat) }
} else {
BuiltinProfilerCategory::Other
};
Expand Down
1 change: 1 addition & 0 deletions unity-native-plugin-sample/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub extern "system" fn GetFillTextureCallback() -> extern "system" fn(c_int, *mu

#[cfg(windows)]
#[test]
#[ignore = "Requires Desktop"]
fn test() {
let instant = std::time::Instant::now();
unity_native_plugin_tester::d3d11::test_plugin_d3d11(
Expand Down
20 changes: 18 additions & 2 deletions unity-native-plugin-sample/src/vulkan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,29 @@ pub fn fill_texture(unity_texture: *mut c_void, x: f32, y: f32, z: f32, w: f32)

let pfn = unwrap_pfn(vk_instance.get_instance_proc_addr(c"vkGetDeviceProcAddr".as_ptr()));
let vk_get_device_proc_addr: vk::PFN_vkGetDeviceProcAddr = match pfn {
Some(f) => std::mem::transmute(f),
Some(f) => std::mem::transmute::<
unsafe extern "system" fn(),
unsafe extern "system" fn(
vk::Device,
*const std::os::raw::c_char,
) -> Option<unsafe extern "system" fn()>,
>(f),
None => return,
};

let pfn = vk_get_device_proc_addr(vk_instance.device(), c"vkCmdClearColorImage".as_ptr());
let vk_cmd_clear_color_image: vk::PFN_vkCmdClearColorImage = match pfn {
Some(f) => std::mem::transmute(f),
Some(f) => std::mem::transmute::<
unsafe extern "system" fn(),
unsafe extern "system" fn(
vk::CommandBuffer,
vk::Image,
vk::ImageLayout,
*const vk::ClearColorValue,
u32,
*const vk::ImageSubresourceRange,
),
>(f),
None => return,
};

Expand Down
4 changes: 4 additions & 0 deletions unity-native-plugin-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(clippy::all)]
#![allow(clippy::pedantic)]
#![allow(unnecessary_transmutes)]
#![allow(unsafe_op_in_unsafe_fn)]
include!("plugin_api.rs");

include!("metal.rs");
Expand Down
2 changes: 2 additions & 0 deletions unity-native-plugin-tester/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ d3d12 = []
[dependencies]
unity-native-plugin-sys = { version = "0.9.0", path = "../unity-native-plugin-sys" }
unity-native-plugin = { version = "0.9.0", path = "../unity-native-plugin", features = ["d3d11", "d3d12"] }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.9", features = ["winuser", "dxgi", "d3d11", "dxgiformat", "dxgitype", "d3dcommon"] }
winit = "0.23.0"
wio = "0.2.2"
Expand Down
2 changes: 1 addition & 1 deletion unity-native-plugin-tester/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl crate::interface::UnityInterfaceBase for TesterContextGraphics {
}

fn get_unity_interface(&self) -> *mut IUnityInterface {
unsafe { std::mem::transmute::<_, _>(&self.interfaces) }
&self.interfaces as *const IUnityGraphics as *mut IUnityInterface
}
}

Expand Down
10 changes: 8 additions & 2 deletions unity-native-plugin-tester/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ impl Debug for TesterContextInterfaces {
unsafe impl Send for TesterContextInterfaces {}
unsafe impl Sync for TesterContextInterfaces {}

impl Default for TesterContextInterfaces {
fn default() -> Self {
Self::new()
}
}

impl TesterContextInterfaces {
pub fn new() -> Self {
TesterContextInterfaces {
Expand All @@ -50,7 +56,7 @@ impl TesterContextInterfaces {
}

pub fn interfaces(&self) -> *mut IUnityInterfaces {
unsafe { std::mem::transmute::<_, _>(&self.interfaces) }
&self.interfaces as *const IUnityInterfaces as *mut IUnityInterfaces
}

pub fn get_interface(&self, guid: UnityInterfaceGUID) -> Option<Rc<dyn UnityInterfaceBase>> {
Expand Down Expand Up @@ -133,7 +139,7 @@ pub unsafe fn get_unity_interface<T: UnityInterfaceBase + UnityInterfaceID + 'st

// Downcast the inner value of Rc and create a new Rc
let any_ref = interface_rc.as_any();
if let Some(_) = any_ref.downcast_ref::<T>() {
if any_ref.downcast_ref::<T>().is_some() {
// Use Rc::clone to safely create an Rc<T>
// First, get a raw pointer from the original Rc
let ptr = Rc::as_ptr(&interface_rc);
Expand Down
5 changes: 4 additions & 1 deletion unity-native-plugin-tester/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![allow(clippy::missing_safety_doc)]

pub mod graphics;
pub mod interface;
#[cfg(windows)]
pub mod window;

#[cfg(all(feature = "d3d11"))]
#[cfg(all(windows, feature = "d3d11"))]
pub mod d3d11;
6 changes: 4 additions & 2 deletions unity-native-plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ profiler = []
profiler_callbacks = ["profiler"]

[dependencies]
ash = { version = "0.38.0+1.3.281.1", optional = true }
ash = { version = "0.38.0", optional = true }
unity-native-plugin-sys = { version = "0.9.0", path = "../unity-native-plugin-sys" }

[target.'cfg(target_vendor = "apple")'.dependencies]
objc2 = { version = "0.6.3", optional = true }
objc2-foundation = { version = "0.3.2", optional = true }
objc2-metal = { version = "0.3.2", optional = true }
unity-native-plugin-sys = { version = "0.9.0", path = "../unity-native-plugin-sys" }
6 changes: 3 additions & 3 deletions unity-native-plugin/src/bitflag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ macro_rules! bitflag {
}
}

impl Into<$flag_value_type> for $flag_type {
fn into(self) -> $flag_value_type {
self.flag as $flag_value_type
impl From<$flag_type> for $flag_value_type {
fn from(value: $flag_type) -> Self {
value.flag as $flag_value_type
}
}

Expand Down
14 changes: 10 additions & 4 deletions unity-native-plugin/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ define_unity_interface!(
0x8C5AD4926EB17B11_u64
);

pub type GraphicsDeviceEventCallback = extern "system" fn(eventType: GfxDeviceEventType);
pub type GraphicsDeviceEventCallback = extern "system" fn(event_type: GfxDeviceEventType);

impl UnityGraphics {
pub fn renderer(&self) -> GfxRenderer {
unsafe {
match self.interface().GetRenderer {
Some(intf) => std::mem::transmute(intf()),
Some(intf) => std::mem::transmute::<UnityGfxRenderer, GfxRenderer>(intf()),
None => GfxRenderer::Null,
}
}
Expand All @@ -61,15 +61,21 @@ impl UnityGraphics {
pub fn register_device_event_callback(&self, callback: Option<GraphicsDeviceEventCallback>) {
unsafe {
if let Some(intf) = self.interface().RegisterDeviceEventCallback {
intf(std::mem::transmute(callback));
intf(std::mem::transmute::<
Option<GraphicsDeviceEventCallback>,
IUnityGraphicsDeviceEventCallback,
>(callback));
}
}
}

pub fn unregister_device_event_callback(&self, callback: Option<GraphicsDeviceEventCallback>) {
unsafe {
if let Some(intf) = self.interface().UnregisterDeviceEventCallback {
intf(std::mem::transmute(callback));
intf(std::mem::transmute::<
Option<GraphicsDeviceEventCallback>,
IUnityGraphicsDeviceEventCallback,
>(callback));
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions unity-native-plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::missing_safety_doc)]

#[cfg(feature = "d3d11")]
pub mod d3d11;

Expand Down
2 changes: 1 addition & 1 deletion unity-native-plugin/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum LogType {
define_unity_interface!(
UnityLog,
IUnityLog,
0x9E7507fA5B444D5D_u64,
0x9E7507FA5B444D5D_u64,
0x92FB979515EA83FC_u64
);

Expand Down
7 changes: 3 additions & 4 deletions unity-native-plugin/src/memory_manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::define_unity_interface;
use crate::interface::UnityInterface;
use std::ffi::{CStr, c_void};
use std::ptr::null_mut;
use unity_native_plugin_sys::*;

define_unity_interface!(
Expand Down Expand Up @@ -71,10 +70,10 @@ impl UnityMemoryManager {
area_name.as_ptr(),
object_name.as_ptr(),
);
if allocator != null_mut() {
if !allocator.is_null() {
Some(UnityAllocator {
allocator: allocator,
memory_manager: self.clone(),
allocator,
memory_manager: *self,
})
} else {
None
Expand Down
19 changes: 10 additions & 9 deletions unity-native-plugin/src/profiler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::bitflag;
use crate::define_unity_interface;
use crate::interface::UnityInterface;
use std::ptr::null_mut;
use unity_native_plugin_sys::*;

define_unity_interface!(
Expand Down Expand Up @@ -92,7 +91,7 @@ impl ProfilerMarkerEventType {
pub fn from(value: u16) -> Option<Self> {
use ProfilerMarkerEventType::*;
if value <= Single as u16 {
Some(unsafe { std::mem::transmute(value) })
Some(unsafe { std::mem::transmute::<u16, ProfilerMarkerEventType>(value) })
} else {
None
}
Expand Down Expand Up @@ -160,7 +159,7 @@ impl ProfilerMarkerDataType {
#[allow(unused)]
pub(crate) fn from(value: u8) -> Option<Self> {
if value <= ProfilerMarkerDataType::Blob8 as u8 {
Some(unsafe { std::mem::transmute(value) })
Some(unsafe { std::mem::transmute::<u8, ProfilerMarkerDataType>(value) })
} else {
None
}
Expand All @@ -183,7 +182,7 @@ impl ProfilerMarkerDataUnit {
#[allow(unused)]
pub(crate) fn from(value: u8) -> Option<Self> {
if value <= ProfilerMarkerDataUnit::FrequencyHz as u8 {
Some(unsafe { std::mem::transmute(value) })
Some(unsafe { std::mem::transmute::<u8, ProfilerMarkerDataUnit>(value) })
} else {
None
}
Expand Down Expand Up @@ -213,7 +212,7 @@ impl ProfilerMarkerData<'_> {
}

pub fn data_type(&self) -> ProfilerMarkerDataType {
unsafe { std::mem::transmute(self.native.type_) }
unsafe { std::mem::transmute::<u8, ProfilerMarkerDataType>(self.native.type_) }
}

pub fn data(&self) -> &'_ [u8] {
Expand All @@ -232,7 +231,7 @@ pub enum ProfilerFlowEventType {
impl ProfilerFlowEventType {
pub fn from(value: u8) -> Option<Self> {
if value <= ProfilerFlowEventType::End as u8 {
Some(unsafe { std::mem::transmute(value) })
Some(unsafe { std::mem::transmute::<u8, ProfilerFlowEventType>(value) })
} else {
None
}
Expand Down Expand Up @@ -284,8 +283,8 @@ macro_rules! impl_profiler {
unsafe { self.interface().IsAvailable.expect("IsAvailable")() != 0 }
}

pub fn create_marker<'a>(
&'a self,
pub fn create_marker(
&self,
name: &std::ffi::CStr,
category: ProfilerCategoryId,
flags: ProfilerMarkerFlags,
Expand Down Expand Up @@ -412,6 +411,7 @@ macro_rules! impl_profiler_v2 {
}
}

#[allow(clippy::too_many_arguments)]
pub unsafe fn create_counter_value(
&self,
category: ProfilerCategoryId,
Expand Down Expand Up @@ -451,6 +451,7 @@ macro_rules! impl_profiler_v2 {
}
}

#[allow(clippy::too_many_arguments)]
pub unsafe fn create_counter<T>(
&self,
category: ProfilerCategoryId,
Expand All @@ -476,7 +477,7 @@ macro_rules! impl_profiler_v2 {
deactivate_func,
user_data,
);
if r != null_mut() {
if !r.is_null() {
Some(ProfilerCounter::<T> {
counter: r as *mut T,
})
Expand Down
Loading
Loading