Skip to content
Merged
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
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,39 @@ let intf = unity_native_plugin::interface::UnityInterfaces::get()
* `unity_native_plugin_vulkan::vulkan::*` → `unity_native_plugin::vulkan::*`
* `unity_native_plugin::d3d11::ComPtr` / `unity_native_plugin::d3d12::ComPtr` → `unity_native_plugin::windows::ComPtr`

### Methods are now provided through `*Interface` traits
### Methods are now provided through traits

The inherent `impl` blocks on `UnityGraphicsD3D11`, `UnityGraphicsD3D12*`, `UnityGraphicsMetal*` and `UnityGraphicsVulkan*` have been replaced with traits that mirror the underlying `IUnityGraphics*` C interfaces. To call any method, bring the matching trait into scope:
The inherent `impl` blocks on the wrapper types have been replaced with traits. Each trait is published under **two names**:

* a Rust-conventional `*Interface` name (the canonical one — e.g. `UnityGraphicsD3D11Interface`)
* an `IUnity*` alias that matches Unity's C header name 1:1 (e.g. `IUnityGraphicsD3D11`)

Use whichever name you prefer; they refer to the same trait. To call any method, bring it into scope:

```rust
// Pick either form per import; both resolve to the same trait.
use unity_native_plugin::graphics::UnityGraphicsInterface; // or IUnityGraphics
use unity_native_plugin::log::UnityLogInterface; // or IUnityLog
use unity_native_plugin::memory_manager::UnityMemoryManagerInterface; // or IUnityMemoryManager
use unity_native_plugin::profiler::{UnityProfilerInterface, UnityProfilerV2Interface};
use unity_native_plugin::profiler_callbacks::{
UnityProfilerCallbacksInterface, UnityProfilerCallbacksV2Interface,
};

use unity_native_plugin::d3d11::UnityGraphicsD3D11Interface;
use unity_native_plugin::d3d12::{
UnityGraphicsD3D12Interface, // for UnityGraphicsD3D12
UnityGraphicsD3D12V2Interface, // for UnityGraphicsD3D12v2
UnityGraphicsD3D12v3Interface, // ... v3 .. v8Interface for the corresponding interface versions
UnityGraphicsD3D12v2Interface, // for UnityGraphicsD3D12v2
UnityGraphicsD3D12v3Interface, // ... v3 .. v8 for the corresponding interface versions
};
use unity_native_plugin::metal::{UnityGraphicsMetalV1Interface, UnityGraphicsMetalV2Interface};
use unity_native_plugin::vulkan::{UnityGraphicsVulkanInterface, UnityGraphicsVulkanV2Interface};
```

Without the corresponding `use`, methods such as `device()`, `command_queue()` or `command_recording_state()` will appear to be missing.
Without the corresponding `use`, methods such as `renderer()`, `log()`, `device()`, `command_queue()`, `emit_event()`, `register_create_marker()` etc. will appear to be missing.

Notes:
* The marker trait `unity_native_plugin::interface::UnityInterface` is **not** a Unity-API wrapper — it is a Rust-only marker used by `UnityInterfaces::interface::<T>()` for GUID-based lookup. It does not have an `IUnity*` alias because it does not correspond 1:1 to any Unity header type.

### Renamed identifiers

Expand Down
2 changes: 1 addition & 1 deletion unity-native-plugin-sample-profiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::*;
use std::num::NonZeroU64;
use std::time::*;
use unity_native_plugin::profiler::*;
use unity_native_plugin::profiler_callbacks::*;
use unity_native_plugin::profiler_callbacks::{IUnityProfilerCallbacks, *};

unity_native_plugin::unity_native_plugin_entry_point! {
fn unity_plugin_load(interfaces: &unity_native_plugin::interface::UnityInterfaces) {
Expand Down
2 changes: 1 addition & 1 deletion unity-native-plugin-sample/src/d3d11.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use unity_native_plugin::d3d11::UnityGraphicsD3D11Interface;
use unity_native_plugin::d3d11::IUnityGraphicsD3D11;
use winapi::shared::dxgiformat;
use winapi::um::{d3d11, unknwnbase::IUnknown};
use wio::com::ComPtr;
Expand Down
2 changes: 1 addition & 1 deletion unity-native-plugin-sample/src/d3d12.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use unity_native_plugin::d3d12::{
UnityGraphicsD3D12V2Interface, UnityGraphicsD3D12v6Interface, UnityGraphicsD3D12v8Interface,
IUnityGraphicsD3D12v2, IUnityGraphicsD3D12v6, IUnityGraphicsD3D12v8,
};
use winapi::Interface;
use winapi::um::d3d12::*;
Expand Down
6 changes: 3 additions & 3 deletions unity-native-plugin-sample/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use std::ffi::c_void;
use std::os::raw::c_int;

#[cfg(windows)]
use unity_native_plugin::d3d12::UnityGraphicsD3D12v6Interface;
use unity_native_plugin::graphics::GfxRenderer;
use unity_native_plugin::vulkan::UnityGraphicsVulkanInterface;
use unity_native_plugin::d3d12::IUnityGraphicsD3D12v6;
use unity_native_plugin::graphics::{GfxRenderer, IUnityGraphics};
use unity_native_plugin::vulkan::IUnityGraphicsVulkan;

const FILL_TEXTURE_EVENT_ID: c_int = 0;

Expand Down
39 changes: 20 additions & 19 deletions unity-native-plugin-sample/src/metal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use objc2_metal::{
};

use unity_native_plugin::interface::UnityInterfaces;
use unity_native_plugin::metal::{
UnityGraphicsMetalV1, UnityGraphicsMetalV1Interface, UnityGraphicsMetalV2,
};
use unity_native_plugin::metal::IUnityGraphicsMetalV1;

pub fn fill_texture(unity_texture: *mut c_void, x: f32, y: f32, z: f32, w: f32) {
if unity_texture.is_null() {
Expand All @@ -20,22 +18,25 @@ pub fn fill_texture(unity_texture: *mut c_void, x: f32, y: f32, z: f32, w: f32)
unsafe {
let interfaces = UnityInterfaces::get();

let cmd_buffer: Retained<ProtocolObject<dyn MTLCommandBuffer>> =
if let Some(intf) = interfaces.interface::<UnityGraphicsMetalV2>() {
intf.end_current_command_encoder();
match intf.current_command_buffer() {
Some(cb) => cb,
None => return,
}
} else if let Some(intf) = interfaces.interface::<UnityGraphicsMetalV1>() {
intf.end_current_command_encoder();
match intf.current_command_buffer() {
Some(cb) => cb,
None => return,
}
} else {
return;
};
let cmd_buffer: Retained<ProtocolObject<dyn MTLCommandBuffer>> = if let Some(intf) =
interfaces.interface::<unity_native_plugin::metal::UnityGraphicsMetalV2>()
{
intf.end_current_command_encoder();
match intf.current_command_buffer() {
Some(cb) => cb,
None => return,
}
} else if let Some(intf) =
interfaces.interface::<unity_native_plugin::metal::UnityGraphicsMetalV1>()
{
intf.end_current_command_encoder();
match intf.current_command_buffer() {
Some(cb) => cb,
None => return,
}
} else {
return;
};

// Unity returns id<MTLTexture> directly from GetNativeTexturePtr() under Metal.
let texture: Retained<ProtocolObject<dyn MTLTexture>> =
Expand Down
5 changes: 2 additions & 3 deletions unity-native-plugin-sample/src/vulkan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::ffi::c_void;
use unity_native_plugin::ash::vk;
use unity_native_plugin::ash::vk::Handle;
use unity_native_plugin::vulkan::{
UnityGraphicsVulkan, UnityGraphicsVulkanInterface, VulkanGraphicsQueueAccess,
VulkanResourceAccessMode,
IUnityGraphicsVulkan, VulkanGraphicsQueueAccess, VulkanResourceAccessMode,
};

pub fn fill_texture(unity_texture: *mut c_void, x: f32, y: f32, z: f32, w: f32) {
Expand All @@ -13,7 +12,7 @@ pub fn fill_texture(unity_texture: *mut c_void, x: f32, y: f32, z: f32, w: f32)
}

let intf = match unity_native_plugin::interface::UnityInterfaces::get()
.interface::<UnityGraphicsVulkan>()
.interface::<unity_native_plugin::vulkan::UnityGraphicsVulkan>()
{
Some(i) => i,
None => return,
Expand Down
2 changes: 2 additions & 0 deletions unity-native-plugin-tester/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub fn initialize_interface(renderer: unity_native_plugin::graphics::GfxRenderer

#[test]
fn register_graphics() {
use unity_native_plugin::graphics::UnityGraphicsInterface;

crate::interface::initialize_unity_interfaces();
crate::graphics::initialize_interface(unity_native_plugin::graphics::GfxRenderer::D3D11);

Expand Down
2 changes: 2 additions & 0 deletions unity-native-plugin/src/d3d11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub trait UnityGraphicsD3D11Interface {
fn present_flags(&self) -> u32;
}

pub use UnityGraphicsD3D11Interface as IUnityGraphicsD3D11;

macro_rules! impl_d3d11 {
($intf:ty) => {
impl UnityGraphicsD3D11Interface for $intf {
Expand Down
61 changes: 47 additions & 14 deletions unity-native-plugin/src/d3d12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ pub struct PluginEventConfig {
pub ensure_active_render_texture_is_bound: bool,
}

pub trait UnityGraphicsD3D12V2Interface {
pub trait UnityGraphicsD3D12v2Interface {
unsafe fn device(&self) -> ComPtr;
unsafe fn frame_fence(&self) -> ComPtr;
fn next_frame_fence_value(&self) -> u64;
}

pub use UnityGraphicsD3D12v2Interface as IUnityGraphicsD3D12v2;

macro_rules! impl_d3d12_v2 {
($intf:ty) => {
impl UnityGraphicsD3D12V2Interface for $intf {
impl UnityGraphicsD3D12v2Interface for $intf {
unsafe fn device(&self) -> ComPtr {
unsafe { self.interface().GetDevice.expect("GetDevice")() as ComPtr }
}
Expand All @@ -65,27 +67,46 @@ macro_rules! impl_d3d12_v2 {

define_unity_interface!(
UnityGraphicsD3D12v2,
IUnityGraphicsD3D12v2,
unity_native_plugin_sys::IUnityGraphicsD3D12v2,
0xEC39D2F18446C745_u64,
0xB1A2626641D6B11F_u64
);
impl_d3d12_v2!(UnityGraphicsD3D12v2);

pub trait UnityGraphicsD3D12Interface: UnityGraphicsD3D12V2Interface {
pub trait UnityGraphicsD3D12Interface {
unsafe fn device(&self) -> ComPtr;
unsafe fn command_queue(&self) -> ComPtr;
unsafe fn frame_fence(&self) -> ComPtr;
fn next_frame_fence_value(&self) -> u64;
fn resource_state(&self, resource: ComPtr) -> Option<i32>;
fn set_resource_state(&self, resource: ComPtr, state: i32);
}

pub use UnityGraphicsD3D12Interface as IUnityGraphicsD3D12;

macro_rules! impl_d3d12 {
($intf:ty) => {
impl_d3d12_v2!($intf);

impl UnityGraphicsD3D12Interface for $intf {
unsafe fn device(&self) -> ComPtr {
unsafe { self.interface().GetDevice.expect("GetDevice")() as ComPtr }
}

unsafe fn command_queue(&self) -> ComPtr {
unsafe { self.interface().GetCommandQueue.expect("GetCommandQueue")() as ComPtr }
}

unsafe fn frame_fence(&self) -> ComPtr {
unsafe { self.interface().GetFrameFence.expect("GetFrameFence")() as ComPtr }
}

fn next_frame_fence_value(&self) -> u64 {
unsafe {
self.interface()
.GetNextFrameFenceValue
.expect("GetNextFrameFenceValue")() as u64
}
}

fn resource_state(&self, resource: ComPtr) -> Option<i32> {
unsafe {
let mut ret: D3D12_RESOURCE_STATES = D3D12_RESOURCE_STATES::default();
Expand Down Expand Up @@ -114,16 +135,18 @@ macro_rules! impl_d3d12 {

define_unity_interface!(
UnityGraphicsD3D12,
IUnityGraphicsD3D12,
unity_native_plugin_sys::IUnityGraphicsD3D12,
0xEF4CEC88A45F4C4C_u64,
0xBD295B6F2A38D9DE_u64
);
impl_d3d12!(UnityGraphicsD3D12);

pub trait UnityGraphicsD3D12v3Interface: UnityGraphicsD3D12V2Interface {
pub trait UnityGraphicsD3D12v3Interface: UnityGraphicsD3D12v2Interface {
fn set_physical_video_memory_control_values(&self, mem_info: &PhysicalVideoMemoryControlValues);
}

pub use UnityGraphicsD3D12v3Interface as IUnityGraphicsD3D12v3;

macro_rules! impl_d3d12_v3 {
($intf:ty) => {
impl_d3d12_v2!($intf);
Expand All @@ -147,7 +170,7 @@ macro_rules! impl_d3d12_v3 {

define_unity_interface!(
UnityGraphicsD3D12v3,
IUnityGraphicsD3D12v3,
unity_native_plugin_sys::IUnityGraphicsD3D12v3,
0x57C3FAFE59E5E843_u64,
0xBF4F5998474BB600_u64
);
Expand All @@ -157,6 +180,8 @@ pub trait UnityGraphicsD3D12v4Interface: UnityGraphicsD3D12v3Interface {
unsafe fn command_queue(&self) -> ComPtr;
}

pub use UnityGraphicsD3D12v4Interface as IUnityGraphicsD3D12v4;

macro_rules! impl_d3d12_v4 {
($intf:ty) => {
impl_d3d12_v3!($intf);
Expand All @@ -171,7 +196,7 @@ macro_rules! impl_d3d12_v4 {

define_unity_interface!(
UnityGraphicsD3D12v4,
IUnityGraphicsD3D12v4,
unity_native_plugin_sys::IUnityGraphicsD3D12v4,
0x498FFCC13EC94006_u64,
0xB18F8B0FF67778C8_u64
);
Expand All @@ -181,6 +206,8 @@ pub trait UnityGraphicsD3D12v5Interface: UnityGraphicsD3D12v4Interface {
unsafe fn texture_from_render_buffer(&self, rb: graphics::RenderBuffer) -> ComPtr;
}

pub use UnityGraphicsD3D12v5Interface as IUnityGraphicsD3D12v5;

macro_rules! impl_d3d12_v5 {
($intf:ty) => {
impl_d3d12_v4!($intf);
Expand All @@ -199,7 +226,7 @@ macro_rules! impl_d3d12_v5 {

define_unity_interface!(
UnityGraphicsD3D12v5,
IUnityGraphicsD3D12v5,
unity_native_plugin_sys::IUnityGraphicsD3D12v5,
0xF5C8D8A37D37BC42_u64,
0xB02DFE93B5064A27_u64
);
Expand All @@ -210,6 +237,8 @@ pub trait UnityGraphicsD3D12v6Interface: UnityGraphicsD3D12v5Interface {
unsafe fn command_recording_state(&self) -> Option<ComPtr>;
}

pub use UnityGraphicsD3D12v6Interface as IUnityGraphicsD3D12v6;

macro_rules! impl_d3d12_v6 {
($intf:ty) => {
impl_d3d12_v5!($intf);
Expand Down Expand Up @@ -248,7 +277,7 @@ macro_rules! impl_d3d12_v6 {

define_unity_interface!(
UnityGraphicsD3D12v6,
IUnityGraphicsD3D12v6,
unity_native_plugin_sys::IUnityGraphicsD3D12v6,
0xA396DCE58CAC4D78_u64,
0xAFDD9B281F20B840_u64
);
Expand All @@ -260,6 +289,8 @@ pub trait UnityGraphicsD3D12v7Interface: UnityGraphicsD3D12v6Interface {
fn present_flags(&self) -> u32;
}

pub use UnityGraphicsD3D12v7Interface as IUnityGraphicsD3D12v7;

macro_rules! impl_d3d12_v7 {
($intf:ty) => {
impl_d3d12_v6!($intf);
Expand All @@ -282,7 +313,7 @@ macro_rules! impl_d3d12_v7 {

define_unity_interface!(
UnityGraphicsD3D12v7,
IUnityGraphicsD3D12v7,
unity_native_plugin_sys::IUnityGraphicsD3D12v7,
0x4624B0DA41B64AAC_u64,
0x915AABCB9BC3F0D3_u64
);
Expand All @@ -293,6 +324,8 @@ pub trait UnityGraphicsD3D12v8Interface: UnityGraphicsD3D12v7Interface {
fn notify_resource_state(&self, resource: ComPtr, state: i32, uav_access: bool);
}

pub use UnityGraphicsD3D12v8Interface as IUnityGraphicsD3D12v8;

macro_rules! impl_d3d12_v8 {
($intf:ty) => {
impl_d3d12_v7!($intf);
Expand Down Expand Up @@ -325,7 +358,7 @@ macro_rules! impl_d3d12_v8 {

define_unity_interface!(
UnityGraphicsD3D12v8,
IUnityGraphicsD3D12v8,
unity_native_plugin_sys::IUnityGraphicsD3D12v8,
0x9D303045D00D4CFD_u64,
0x8FEBB42968B423B6_u64
);
Expand Down
Loading
Loading