-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathUtil.cs
More file actions
144 lines (113 loc) · 3.96 KB
/
Copy pathUtil.cs
File metadata and controls
144 lines (113 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace WGPU.NET
{
internal static class Util
{
public static IntPtr AllocHStruct<T>(T structure)
where T : struct
{
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(structure));
Marshal.StructureToPtr(structure, ptr, false);
return ptr;
}
public static unsafe IntPtr AllocHArray(byte[] arr)
{
IntPtr ptr = Marshal.AllocHGlobal(arr.Length);
Span<byte> span = new Span<byte>((void*)ptr, arr.Length);
new Span<byte>(arr).CopyTo(span);
return ptr;
}
public static unsafe IntPtr AllocHArray<T>(T[] arr)
where T : struct
{
int size = sizeof(T);
IntPtr ptr = Marshal.AllocHGlobal(size * arr.Length);
Span<T> span = new Span<T>((void*)ptr, arr.Length);
for (int i = 0; i < arr.Length; i++)
{
span[i] = arr[i];
}
return ptr;
}
public static unsafe IntPtr AllocHArray<T>(int count, IEnumerable<T> items)
where T : struct
{
int size = sizeof(T);
IntPtr ptr = Marshal.AllocHGlobal(size * count);
Span<T> span = new Span<T>((void*)ptr, count);
int i = 0;
foreach (var item in items)
{
span[i] = item;
i++;
}
if (i != count)
throw new ArgumentException($"{nameof(count)} is larger then the supplied enumerable");
return ptr;
}
public static IntPtr Optional<T>(T? optional)
where T : struct
{
if (optional == null) return IntPtr.Zero;
return AllocHStruct(optional.Value);
}
public static void FreePtr(IntPtr ptr) => Marshal.FreeHGlobal(ptr);
//
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TValue GetOrCreate<TKey, TValue>(this Dictionary<TKey, TValue> self, TKey key)
where TValue : new()
{
if (self.TryGetValue(key, out TValue value))
{
return value;
}
TValue newVal = new TValue();
self[key] = newVal;
return newVal;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TValue GetOrCreate<TKey, TValue>(this Dictionary<TKey, TValue> self, TKey key, Func<TValue> creator)
{
if (self.TryGetValue(key, out TValue value))
{
return value;
}
TValue newVal = creator();
self[key] = newVal;
return newVal;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Wgpu.BackendType ToBackend(this Wgpu.InstanceBackend type) => type switch
{
Wgpu.InstanceBackend.All => Wgpu.BackendType.Undefined,
Wgpu.InstanceBackend.Vulkan => Wgpu.BackendType.Vulkan,
Wgpu.InstanceBackend.GL => Wgpu.BackendType.OpenGLES,
Wgpu.InstanceBackend.Metal => Wgpu.BackendType.Metal,
Wgpu.InstanceBackend.DX12 => Wgpu.BackendType.D3D12,
Wgpu.InstanceBackend.DX11 => Wgpu.BackendType.D3D11,
Wgpu.InstanceBackend.BrowserWebGPU => Wgpu.BackendType.WebGPU,
Wgpu.InstanceBackend.Primary => Wgpu.BackendType.Vulkan,
Wgpu.InstanceBackend.Secondary => Wgpu.BackendType.OpenGLES,
Wgpu.InstanceBackend.Force32 => Wgpu.BackendType.Force32,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Wgpu.InstanceBackend ToInstanceBackend(this Wgpu.BackendType type) => type switch
{
Wgpu.BackendType.Undefined => Wgpu.InstanceBackend.All,
Wgpu.BackendType.Null => Wgpu.InstanceBackend.All,
Wgpu.BackendType.WebGPU => Wgpu.InstanceBackend.BrowserWebGPU,
Wgpu.BackendType.D3D11 => Wgpu.InstanceBackend.DX11,
Wgpu.BackendType.D3D12 => Wgpu.InstanceBackend.DX12,
Wgpu.BackendType.Metal => Wgpu.InstanceBackend.Metal,
Wgpu.BackendType.Vulkan => Wgpu.InstanceBackend.Vulkan,
Wgpu.BackendType.OpenGL => Wgpu.InstanceBackend.GL,
Wgpu.BackendType.OpenGLES => Wgpu.InstanceBackend.GL,
Wgpu.BackendType.Force32 => Wgpu.InstanceBackend.Force32,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
}
}