-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGpuSharedDevice.cs
More file actions
118 lines (104 loc) · 3.21 KB
/
Copy pathGpuSharedDevice.cs
File metadata and controls
118 lines (104 loc) · 3.21 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
using System;
using Vortice.Direct3D;
using Vortice.Direct3D11;
using static Vortice.Direct3D11.D3D11;
namespace lifeviz;
internal sealed class GpuSharedDevice
{
private static readonly object InstanceLock = new();
private static GpuSharedDevice? _instance;
private GpuSharedDevice(ID3D11Device1 device, ID3D11DeviceContext1 context, FeatureLevel featureLevel)
{
Device = device;
Context = context;
FeatureLevel = featureLevel;
}
public ID3D11Device1 Device { get; }
public ID3D11DeviceContext1 Context { get; }
public FeatureLevel FeatureLevel { get; }
public object SyncRoot { get; } = new();
public static GpuSharedDevice GetOrCreate()
{
lock (InstanceLock)
{
_instance ??= Create();
return _instance;
}
}
public static void FlushIfCreated()
{
GpuSharedDevice? instance;
lock (InstanceLock)
{
instance = _instance;
}
if (instance == null)
{
return;
}
lock (instance.SyncRoot)
{
instance.Context.Flush();
}
}
private static GpuSharedDevice Create()
{
FeatureLevel[] primaryLevels =
{
FeatureLevel.Level_11_1,
FeatureLevel.Level_11_0
};
try
{
D3D11CreateDevice(
IntPtr.Zero,
DriverType.Hardware,
DeviceCreationFlags.None,
primaryLevels,
out ID3D11Device device,
out FeatureLevel featureLevel,
out ID3D11DeviceContext context);
return Build(device, context, featureLevel);
}
catch
{
FeatureLevel[] fallbackLevels =
{
FeatureLevel.Level_11_0
};
D3D11CreateDevice(
IntPtr.Zero,
DriverType.Hardware,
DeviceCreationFlags.None,
fallbackLevels,
out ID3D11Device device,
out FeatureLevel featureLevel,
out ID3D11DeviceContext context);
return Build(device, context, featureLevel);
}
}
private static GpuSharedDevice Build(ID3D11Device device, ID3D11DeviceContext context, FeatureLevel featureLevel)
{
ID3D11Device1 device1 = device.QueryInterface<ID3D11Device1>();
ID3D11DeviceContext1 context1 = context.QueryInterface<ID3D11DeviceContext1>();
device.Dispose();
context.Dispose();
return new GpuSharedDevice(device1, context1, featureLevel);
}
}
internal sealed class GpuCompositeSurface
{
public GpuCompositeSurface(ID3D11Texture2D texture, ID3D11ShaderResourceView shaderResourceView, IntPtr sharedTextureHandle, int width, int height)
{
Texture = texture;
ShaderResourceView = shaderResourceView;
SharedTextureHandle = sharedTextureHandle;
Width = width;
Height = height;
}
public ID3D11Texture2D Texture { get; }
public ID3D11ShaderResourceView ShaderResourceView { get; }
public IntPtr SharedTextureHandle { get; }
public int Width { get; }
public int Height { get; }
}