Skip to content

Commit f2d716e

Browse files
committed
Add new interfaces
* log.rs * memory_manager.rs * Update defines * refactor codes
1 parent 72eb581 commit f2d716e

File tree

13 files changed

+177
-48
lines changed

13 files changed

+177
-48
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "unity-native-plugin-sample-profiler"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
authors = ["Yasuhiro Taniuchi"]
55
edition = "2018"
66
publish = false
@@ -11,8 +11,8 @@ crate-type = ["cdylib"]
1111
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1212

1313
[dependencies]
14-
unity-native-plugin = { version = "0.5.0", path = "../unity-native-plugin", features = ["profiler_callbacks"] }
15-
unity-native-plugin-sys = { version = "0.5.0", path = "../unity-native-plugin-sys" }
14+
unity-native-plugin = { version = "0.6.0", path = "../unity-native-plugin", features = ["profiler_callbacks"] }
15+
unity-native-plugin-sys = { version = "0.6.0", path = "../unity-native-plugin-sys" }
1616
log = "0.4"
1717
env_logger = "0.8"
1818
flume = "0.10"

unity-native-plugin-sample/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "unity-native-plugin-sample"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
authors = ["Yasuhiro Taniuchi"]
55
edition = "2018"
66
publish = false
@@ -11,10 +11,10 @@ crate-type = ["cdylib"]
1111
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1212

1313
[dependencies]
14-
unity-native-plugin = { version = "0.5.0", path = "../unity-native-plugin", features = ["d3d11", "d3d12", "profiler"] }
15-
unity-native-plugin-vulkan = { version = "0.5.0", path = "../unity-native-plugin-vulkan" }
14+
unity-native-plugin = { version = "0.6.0", path = "../unity-native-plugin", features = ["d3d11", "d3d12", "profiler"] }
15+
unity-native-plugin-vulkan = { version = "0.6.0", path = "../unity-native-plugin-vulkan" }
1616
winapi = { version = "0.3.9", features = ["winuser", "d3d11", "dxgiformat"] }
1717
wio = "0.2.2"
1818

1919
[dev-dependencies]
20-
unity-native-plugin-tester = { version = "0.5.0", path = "../unity-native-plugin-tester", features = ["d3d11"] }
20+
unity-native-plugin-tester = { version = "0.6.0", path = "../unity-native-plugin-tester", features = ["d3d11"] }

unity-native-plugin-sys/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "unity-native-plugin-sys"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
authors = ["Yasuhiro Taniuchi"]
55
edition = "2018"
66
license = "MIT"

unity-native-plugin-tester/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "unity-native-plugin-tester"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
authors = ["Yasuhiro Taniuchi"]
55
edition = "2018"
66
license = "MIT"
@@ -25,8 +25,8 @@ d3d11 = []
2525
d3d12 = []
2626

2727
[dependencies]
28-
unity-native-plugin-sys = { version = "0.5.0", path = "../unity-native-plugin-sys" }
29-
unity-native-plugin = { version = "0.5.0", path = "../unity-native-plugin", features = ["d3d11", "d3d12"] }
28+
unity-native-plugin-sys = { version = "0.6.0", path = "../unity-native-plugin-sys" }
29+
unity-native-plugin = { version = "0.6.0", path = "../unity-native-plugin", features = ["d3d11", "d3d12"] }
3030
winapi = { version = "0.3.9", features = ["winuser", "dxgi", "d3d11", "dxgiformat", "dxgitype", "d3dcommon"] }
3131
winit = "0.23.0"
3232
wio = "0.2.2"

unity-native-plugin-vulkan/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "unity-native-plugin-vulkan"
3-
version = "0.5.1"
3+
version = "0.6.0"
44
authors = ["Yasuhiro Taniuchi"]
55
license = "MIT"
66
description = "Unity Native Plugin API (Vulkan) for Rust"
@@ -19,6 +19,6 @@ include = [
1919
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
2020

2121
[dependencies]
22-
unity-native-plugin = { version = "0.5.0", path = "../unity-native-plugin" }
23-
unity-native-plugin-sys = { version = "0.5.0", path = "../unity-native-plugin-sys" }
24-
ash = "0.33.1"
22+
unity-native-plugin = { version = "0.6.0", path = "../unity-native-plugin" }
23+
unity-native-plugin-sys = { version = "0.6.0", path = "../unity-native-plugin-sys" }
24+
ash = "0.38.0+1.3.281.1"

unity-native-plugin/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "unity-native-plugin"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
authors = ["Yasuhiro Taniuchi"]
55
license = "MIT"
66
description = "Unity Native Plugin API for Rust"
@@ -26,4 +26,4 @@ profiler = []
2626
profiler_callbacks = ["profiler"]
2727

2828
[dependencies]
29-
unity-native-plugin-sys = { version = "0.5.0", path = "../unity-native-plugin-sys" }
29+
unity-native-plugin-sys = { version = "0.6.0", path = "../unity-native-plugin-sys" }

unity-native-plugin/src/d3d11.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,24 @@ impl UnityGraphicsD3D11 {
3939
.SRVFromNativeTexture
4040
.expect("SRVFromNativeTexture")(texture) as ComPtr
4141
}
42+
43+
pub unsafe fn swap_chain(&self) -> ComPtr {
44+
self.interface().GetSwapChain.expect("GetSwapChain")() as ComPtr
45+
}
46+
47+
pub fn sync_interval(&self) -> u32 {
48+
unsafe {
49+
self.interface()
50+
.GetSyncInterval
51+
.expect("GetSyncInterval")()
52+
}
53+
}
54+
55+
pub fn present_flags(&self) -> u32 {
56+
unsafe {
57+
self.interface()
58+
.GetPresentFlags
59+
.expect("GetPresentFlags")()
60+
}
61+
}
4262
}

unity-native-plugin/src/enums.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,5 @@
11
use unity_native_plugin_sys::*;
22

3-
#[repr(u16)]
4-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
5-
pub enum ProfilerMarkerFlag {
6-
ScriptEnterLeave = UnityProfilerMarkerFlag__kUnityProfilerMarkerFlagScriptEnterLeave as u16,
7-
AvailabilityEditor = UnityProfilerMarkerFlag__kUnityProfilerMarkerFlagAvailabilityEditor as u16,
8-
AvailabilityNonDev = UnityProfilerMarkerFlag__kUnityProfilerMarkerFlagAvailabilityNonDev as u16,
9-
Warning = UnityProfilerMarkerFlag__kUnityProfilerMarkerFlagWarning as u16,
10-
VerbosityDebug = UnityProfilerMarkerFlag__kUnityProfilerMarkerFlagVerbosityDebug as u16,
11-
VerbosityInternal = UnityProfilerMarkerFlag__kUnityProfilerMarkerFlagVerbosityInternal as u16,
12-
VerbosityAdvanced = UnityProfilerMarkerFlag__kUnityProfilerMarkerFlagVerbosityAdvanced as u16,
13-
}
14-
15-
#[allow(dead_code)]
16-
pub struct ProfilerMarkerFlags(u16);
17-
18-
#[repr(u8)]
19-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
20-
pub enum ProfilerMarkerDataType {
21-
None = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeNone as u8,
22-
InstanceId = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeInstanceId as u8,
23-
Int32 = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeInt32 as u8,
24-
UInt32 = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeUInt32 as u8,
25-
Int64 = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeInt64 as u8,
26-
UInt64 = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeUInt64 as u8,
27-
Float = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeFloat as u8,
28-
Double = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeDouble as u8,
29-
String = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeString as u8,
30-
String16 = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeString16 as u8,
31-
Blob8 = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeBlob8 as u8,
32-
}
33-
343
#[repr(u32)]
354
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
365
pub enum RenderingExtEventType {

unity-native-plugin/src/graphics.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub type RenderingEventAndData = unity_native_plugin_sys::UnityRenderingEventAnd
1212
pub enum GfxRenderer {
1313
D3D11 = UnityGfxRenderer_kUnityGfxRendererD3D11,
1414
Null = UnityGfxRenderer_kUnityGfxRendererNull,
15+
OpenGLES20 = 8, // OpenGL ES 2.0, removed
1516
OpenGLES30 = UnityGfxRenderer_kUnityGfxRendererOpenGLES30,
1617
PS4 = UnityGfxRenderer_kUnityGfxRendererPS4,
1718
XboxOne = UnityGfxRenderer_kUnityGfxRendererXboxOne,
@@ -21,6 +22,11 @@ pub enum GfxRenderer {
2122
Vulkan = UnityGfxRenderer_kUnityGfxRendererVulkan,
2223
Nvn = UnityGfxRenderer_kUnityGfxRendererNvn,
2324
XboxOneD3D12 = UnityGfxRenderer_kUnityGfxRendererXboxOneD3D12,
25+
GameCoreXboxOne = UnityGfxRenderer_kUnityGfxRendererGameCoreXboxOne,
26+
GameCoreXboxSeries = UnityGfxRenderer_kUnityGfxRendererGameCoreXboxSeries,
27+
PS5 = UnityGfxRenderer_kUnityGfxRendererPS5,
28+
PS5NGGC = UnityGfxRenderer_kUnityGfxRendererPS5NGGC,
29+
ReservedCFE = UnityGfxRenderer_kUnityGfxRendererReservedCFE,
2430
}
2531

2632
#[repr(u32)]

unity-native-plugin/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pub mod profiler_callbacks;
1313
pub mod enums;
1414
pub mod graphics;
1515
pub mod interface;
16+
pub mod log;
17+
pub mod memory_manager;
18+
1619
pub type IUnityInterfaces = unity_native_plugin_sys::IUnityInterfaces;
1720

1821
#[macro_export]

unity-native-plugin/src/log.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use crate::define_unity_interface;
2+
use crate::interface::UnityInterface;
3+
use std::ffi::CStr;
4+
use unity_native_plugin_sys::*;
5+
6+
#[repr(u32)]
7+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
8+
pub enum LogType
9+
{
10+
Error = UnityLogType_kUnityLogTypeError,
11+
Warning = UnityLogType_kUnityLogTypeWarning,
12+
Log = UnityLogType_kUnityLogTypeLog,
13+
Exception = UnityLogType_kUnityLogTypeException,
14+
}
15+
16+
define_unity_interface!(
17+
UnityLog,
18+
IUnityLog,
19+
0x9E7507fA5B444D5D_u64,
20+
0x92FB979515EA83FC_u64
21+
);
22+
23+
impl UnityLog {
24+
pub fn log(&self, log_type: LogType, message: &CStr, file_name: &CStr, file_line: i32) {
25+
unsafe {
26+
self.interface().Log.expect("Log")(log_type as UnityLogType, message.as_ptr(), file_name.as_ptr(), file_line);
27+
}
28+
}
29+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use crate::define_unity_interface;
2+
use crate::interface::UnityInterface;
3+
use std::ffi::{c_void, CStr};
4+
use std::ptr::null_mut;
5+
use unity_native_plugin_sys::*;
6+
7+
8+
define_unity_interface!(
9+
UnityMemoryManager,
10+
IUnityMemoryManager,
11+
0xBAF9E57C61A811EC_u64,
12+
0xC5A7CC7861A811EC_u64
13+
);
14+
15+
pub struct UnityAllocator {
16+
allocator: *mut unity_native_plugin_sys::UnityAllocator,
17+
memory_manager: UnityMemoryManager,
18+
}
19+
20+
impl Drop for UnityAllocator {
21+
fn drop(&mut self) {
22+
unsafe {
23+
self.memory_manager.destroy_allocator(self.allocator);
24+
}
25+
}
26+
}
27+
28+
impl UnityAllocator {
29+
pub unsafe fn allocate(&self,
30+
size: usize,
31+
align: usize,
32+
file: &CStr,
33+
line: i32) -> *mut c_void {
34+
self.memory_manager.allocate(self.allocator, size, align, file, line)
35+
}
36+
37+
pub unsafe fn deallocate(&self,
38+
ptr: *mut c_void,
39+
file: &CStr,
40+
line: i32) {
41+
self.memory_manager.deallocate(self.allocator, ptr, file, line)
42+
}
43+
44+
pub unsafe fn reallocate(&self,
45+
ptr: *mut c_void,
46+
size: usize,
47+
align: usize,
48+
file: &CStr,
49+
line: i32) -> *mut c_void {
50+
self.memory_manager.reallocate(self.allocator, ptr, size, align, file, line)
51+
}
52+
}
53+
54+
impl UnityMemoryManager {
55+
pub unsafe fn create_allocator(&self, area_name: &CStr, object_name: &CStr) -> Option<UnityAllocator> {
56+
let allocator = self.interface().CreateAllocator.expect("CreateAllocator")(area_name.as_ptr(), object_name.as_ptr());
57+
if allocator != null_mut() {
58+
Some(UnityAllocator { allocator: allocator, memory_manager: self.clone() })
59+
} else {
60+
None
61+
}
62+
}
63+
64+
pub(crate) unsafe fn destroy_allocator(&self, allocator: *mut unity_native_plugin_sys::UnityAllocator) {
65+
self.interface().DestroyAllocator.expect("DestroyAllocator")(allocator)
66+
}
67+
68+
pub(crate) unsafe fn allocate(&self,
69+
allocator: *mut unity_native_plugin_sys::UnityAllocator,
70+
size: usize,
71+
align: usize,
72+
file: &CStr,
73+
line: i32) -> *mut c_void {
74+
self.interface().Allocate.expect("Allocate")(allocator, size, align, file.as_ptr(), line)
75+
}
76+
77+
pub(crate) unsafe fn deallocate(&self,
78+
allocator: *mut unity_native_plugin_sys::UnityAllocator,
79+
ptr: *mut c_void,
80+
file: &CStr,
81+
line: i32) {
82+
self.interface().Deallocate.expect("Deallocate")(allocator, ptr, file.as_ptr(), line)
83+
}
84+
85+
pub(crate) unsafe fn reallocate(&self,
86+
allocator: *mut unity_native_plugin_sys::UnityAllocator,
87+
ptr: *mut c_void,
88+
size: usize,
89+
align: usize,
90+
file: &CStr,
91+
line: i32) -> *mut c_void {
92+
self.interface().Reallocate.expect("Reallocate")(allocator, ptr, size, align, file.as_ptr(), line)
93+
}
94+
}

unity-native-plugin/src/profiler.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ pub enum BuiltinProfilerCategory {
4444
BuildInterface = UnityBuiltinProfilerCategory__kUnityProfilerCategoryBuildInterface as u16,
4545
Input = UnityBuiltinProfilerCategory__kUnityProfilerCategoryInput as u16,
4646
VirtualTexturing = UnityBuiltinProfilerCategory__kUnityProfilerCategoryVirtualTexturing as u16,
47+
GPU = UnityBuiltinProfilerCategory__kUnityProfilerCategoryGPU as u16,
48+
Physics2D = UnityBuiltinProfilerCategory__kUnityProfilerCategoryPhysics2D as u16,
49+
NetworkOperations = UnityBuiltinProfilerCategory__kUnityProfilerCategoryNetworkOperations as u16,
50+
UIDetails = UnityBuiltinProfilerCategory__kUnityProfilerCategoryUIDetails as u16,
51+
Debug = UnityBuiltinProfilerCategory__kUnityProfilerCategoryDebug as u16,
52+
Jobs = UnityBuiltinProfilerCategory__kUnityProfilerCategoryJobs as u16,
53+
Text = UnityBuiltinProfilerCategory__kUnityProfilerCategoryText as u16,
4754
}
4855

4956
pub type ProfilerCategoryId = UnityProfilerCategoryId;
@@ -186,6 +193,7 @@ pub enum ProfilerMarkerDataType {
186193
String = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeString as u8,
187194
String16 = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeString16 as u8,
188195
Blob8 = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeBlob8 as u8,
196+
GfxResourceId = UnityProfilerMarkerDataType__kUnityProfilerMarkerDataTypeGfxResourceId as u8,
189197
}
190198

191199
impl ProfilerMarkerDataType {

0 commit comments

Comments
 (0)