Skip to content

Commit 4fb125b

Browse files
Kationmattleibow
andauthored
Compile Skia with Direct3D on Windows platform (#2823)
* Compile Direct3D on Windows platform * Add Direct3D backend api * Fix sln * optimize * commit missing files * ToNative * fix wrapper * vortice warpper * Add Direct3D test * update externals * Rename test function * Update externals * externals * Nano Server may not support Direct 3D * Update build.cake * Update skia * Update externals * Regenerate interop code * Some admin * fix tests --------- Co-authored-by: Matthew Leibowitz <[email protected]>
1 parent e5f74e7 commit 4fb125b

26 files changed

+770
-3
lines changed

binding/SkiaSharp/GRBackendRenderTarget.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public GRBackendRenderTarget (int width, int height, GRVkImageInfo vkImageInfo)
3030
CreateVulkan (width, height, vkImageInfo);
3131
}
3232

33+
public GRBackendRenderTarget (int width, int height, GRD3DTextureResourceInfo d3dTextureInfo)
34+
: this (IntPtr.Zero, true)
35+
{
36+
CreateDirect3D (width, height, d3dTextureInfo);
37+
}
38+
3339
#if __IOS__ || __MACOS__ || __TVOS__
3440

3541
[Obsolete ("Use GRBackendRenderTarget(int width, int height, GRMtlTextureInfo mtlInfo) instead.")]
@@ -69,6 +75,16 @@ private void CreateVulkan (int width, int height, GRVkImageInfo vkImageInfo)
6975
}
7076
}
7177

78+
private void CreateDirect3D (int width, int height, GRD3DTextureResourceInfo d3dTextureInfo)
79+
{
80+
var native = d3dTextureInfo.ToNative ();
81+
Handle = SkiaApi.gr_backendrendertarget_new_direct3d (width, height, &native);
82+
83+
if (Handle == IntPtr.Zero) {
84+
throw new InvalidOperationException ("Unable to create a new GRBackendRenderTarget instance.");
85+
}
86+
}
87+
7288
protected override void Dispose (bool disposing) =>
7389
base.Dispose (disposing);
7490

binding/SkiaSharp/GRBackendTexture.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public GRBackendTexture (int width, int height, GRVkImageInfo vkInfo)
2525
CreateVulkan (width, height, vkInfo);
2626
}
2727

28+
public GRBackendTexture (int width, int height, GRD3DTextureResourceInfo d3dTextureInfo)
29+
: this (IntPtr.Zero, true)
30+
{
31+
CreateDirect3D (width, height, d3dTextureInfo);
32+
}
33+
2834
public GRBackendTexture (int width, int height, bool mipmapped, GRMtlTextureInfo mtlInfo)
2935
: this (IntPtr.Zero, true)
3036
{
@@ -54,6 +60,16 @@ private void CreateVulkan (int width, int height, GRVkImageInfo vkInfo)
5460
}
5561
}
5662

63+
private void CreateDirect3D (int width, int height, GRD3DTextureResourceInfo d3dTextureInfo)
64+
{
65+
var native = d3dTextureInfo.ToNative ();
66+
Handle = SkiaApi.gr_backendtexture_new_direct3d (width, height, &native);
67+
68+
if (Handle == IntPtr.Zero) {
69+
throw new InvalidOperationException ("Unable to create a new GRBackendTexture instance.");
70+
}
71+
}
72+
5773
protected override void Dispose (bool disposing) =>
5874
base.Dispose (disposing);
5975

binding/SkiaSharp/GRContext.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ public static GRContext CreateVulkan (GRVkBackendContext backendContext, GRConte
6363
}
6464
}
6565

66+
public static GRContext CreateDirect3D (GRD3DBackendContext backendContext) =>
67+
CreateDirect3D (backendContext, null);
68+
69+
public static GRContext CreateDirect3D (GRD3DBackendContext backendContext, GRContextOptions options)
70+
{
71+
if (backendContext == null)
72+
throw new ArgumentNullException (nameof (backendContext));
73+
if (options == null) {
74+
return GetObject (SkiaApi.gr_direct_context_make_direct3d (backendContext.ToNative ()));
75+
} else {
76+
var opts = options.ToNative ();
77+
return GetObject (SkiaApi.gr_direct_context_make_direct3d_with_options (backendContext.ToNative (), &opts));
78+
}
79+
}
80+
6681
// CreateMetal
6782

6883
public static GRContext CreateMetal (GRMtlBackendContext backendContext) =>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace SkiaSharp
6+
{
7+
public class GRD3DBackendContext : IDisposable
8+
{
9+
public nint Adapter { get; set; }
10+
11+
public nint Device { get; set; }
12+
13+
public nint Queue { get; set; }
14+
15+
public bool ProtectedContext { get; set; }
16+
17+
internal GRD3DBackendContextNative ToNative ()
18+
{
19+
return new GRD3DBackendContextNative {
20+
fAdapter = Adapter,
21+
fDevice = Device,
22+
fQueue = Queue,
23+
fProtectedContext = ProtectedContext ? (byte)1 : (byte)0
24+
};
25+
}
26+
27+
protected virtual void Dispose (bool disposing)
28+
{
29+
}
30+
31+
public void Dispose ()
32+
{
33+
Dispose (disposing: true);
34+
GC.SuppressFinalize (this);
35+
}
36+
}
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace SkiaSharp
6+
{
7+
public class GRD3DTextureResourceInfo : IDisposable
8+
{
9+
public nint Resource { get; set; }
10+
11+
public uint ResourceState { get; set; }
12+
13+
public uint Format { get; set; }
14+
15+
public uint SampleCount { get; set; }
16+
17+
public uint LevelCount { get; set; }
18+
19+
public uint SampleQualityPattern { get; set; }
20+
21+
public bool Protected { get; set; }
22+
23+
internal GRD3DTextureResourceInfoNative ToNative ()
24+
{
25+
return new GRD3DTextureResourceInfoNative {
26+
fResource = Resource,
27+
fResourceState = ResourceState,
28+
fFormat = Format,
29+
fSampleCount = SampleCount,
30+
fLevelCount = LevelCount,
31+
fSampleQualityPattern = SampleQualityPattern,
32+
fProtected = Protected ? (byte)1 : (byte)0
33+
};
34+
}
35+
36+
protected virtual void Dispose (bool disposing)
37+
{
38+
}
39+
40+
public void Dispose ()
41+
{
42+
Dispose (disposing: true);
43+
GC.SuppressFinalize (this);
44+
}
45+
}
46+
}

binding/SkiaSharp/SkiaApi.generated.cs

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@
99

1010
#region Class declarations
1111

12+
using d3d_alloc_t = System.IntPtr;
13+
using d3d_d12_command_queue_t = System.IntPtr;
14+
using d3d_d12_device_t = System.IntPtr;
15+
using d3d_d12_resource_t = System.IntPtr;
16+
using d3d_dxgi_adapter_t = System.IntPtr;
1217
using gr_backendrendertarget_t = System.IntPtr;
1318
using gr_backendtexture_t = System.IntPtr;
19+
using gr_d3d_memory_allocator_t = System.IntPtr;
1420
using gr_direct_context_t = System.IntPtr;
1521
using gr_glinterface_t = System.IntPtr;
1622
using gr_recording_context_t = System.IntPtr;
@@ -268,6 +274,25 @@ internal static bool gr_backendrendertarget_is_valid (gr_backendrendertarget_t r
268274
(gr_backendrendertarget_is_valid_delegate ??= GetSymbol<Delegates.gr_backendrendertarget_is_valid> ("gr_backendrendertarget_is_valid")).Invoke (rendertarget);
269275
#endif
270276

277+
// gr_backendrendertarget_t* gr_backendrendertarget_new_direct3d(int width, int height, const gr_d3d_textureresourceinfo_t* d3dInfo)
278+
#if !USE_DELEGATES
279+
#if USE_LIBRARY_IMPORT
280+
[LibraryImport (SKIA)]
281+
internal static partial gr_backendrendertarget_t gr_backendrendertarget_new_direct3d (Int32 width, Int32 height, GRD3DTextureResourceInfoNative* d3dInfo);
282+
#else // !USE_LIBRARY_IMPORT
283+
[DllImport (SKIA, CallingConvention = CallingConvention.Cdecl)]
284+
internal static extern gr_backendrendertarget_t gr_backendrendertarget_new_direct3d (Int32 width, Int32 height, GRD3DTextureResourceInfoNative* d3dInfo);
285+
#endif
286+
#else
287+
private partial class Delegates {
288+
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
289+
internal delegate gr_backendrendertarget_t gr_backendrendertarget_new_direct3d (Int32 width, Int32 height, GRD3DTextureResourceInfoNative* d3dInfo);
290+
}
291+
private static Delegates.gr_backendrendertarget_new_direct3d gr_backendrendertarget_new_direct3d_delegate;
292+
internal static gr_backendrendertarget_t gr_backendrendertarget_new_direct3d (Int32 width, Int32 height, GRD3DTextureResourceInfoNative* d3dInfo) =>
293+
(gr_backendrendertarget_new_direct3d_delegate ??= GetSymbol<Delegates.gr_backendrendertarget_new_direct3d> ("gr_backendrendertarget_new_direct3d")).Invoke (width, height, d3dInfo);
294+
#endif
295+
271296
// gr_backendrendertarget_t* gr_backendrendertarget_new_gl(int width, int height, int samples, int stencils, const gr_gl_framebufferinfo_t* glInfo)
272297
#if !USE_DELEGATES
273298
#if USE_LIBRARY_IMPORT
@@ -467,6 +492,25 @@ internal static bool gr_backendtexture_is_valid (gr_backendtexture_t texture) =>
467492
(gr_backendtexture_is_valid_delegate ??= GetSymbol<Delegates.gr_backendtexture_is_valid> ("gr_backendtexture_is_valid")).Invoke (texture);
468493
#endif
469494

495+
// gr_backendtexture_t* gr_backendtexture_new_direct3d(int width, int height, const gr_d3d_textureresourceinfo_t* d3dInfo)
496+
#if !USE_DELEGATES
497+
#if USE_LIBRARY_IMPORT
498+
[LibraryImport (SKIA)]
499+
internal static partial gr_backendtexture_t gr_backendtexture_new_direct3d (Int32 width, Int32 height, GRD3DTextureResourceInfoNative* d3dInfo);
500+
#else // !USE_LIBRARY_IMPORT
501+
[DllImport (SKIA, CallingConvention = CallingConvention.Cdecl)]
502+
internal static extern gr_backendtexture_t gr_backendtexture_new_direct3d (Int32 width, Int32 height, GRD3DTextureResourceInfoNative* d3dInfo);
503+
#endif
504+
#else
505+
private partial class Delegates {
506+
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
507+
internal delegate gr_backendtexture_t gr_backendtexture_new_direct3d (Int32 width, Int32 height, GRD3DTextureResourceInfoNative* d3dInfo);
508+
}
509+
private static Delegates.gr_backendtexture_new_direct3d gr_backendtexture_new_direct3d_delegate;
510+
internal static gr_backendtexture_t gr_backendtexture_new_direct3d (Int32 width, Int32 height, GRD3DTextureResourceInfoNative* d3dInfo) =>
511+
(gr_backendtexture_new_direct3d_delegate ??= GetSymbol<Delegates.gr_backendtexture_new_direct3d> ("gr_backendtexture_new_direct3d")).Invoke (width, height, d3dInfo);
512+
#endif
513+
470514
// gr_backendtexture_t* gr_backendtexture_new_gl(int width, int height, bool mipmapped, const gr_gl_textureinfo_t* glInfo)
471515
#if !USE_DELEGATES
472516
#if USE_LIBRARY_IMPORT
@@ -717,6 +761,44 @@ internal static bool gr_direct_context_is_abandoned (gr_direct_context_t context
717761
(gr_direct_context_is_abandoned_delegate ??= GetSymbol<Delegates.gr_direct_context_is_abandoned> ("gr_direct_context_is_abandoned")).Invoke (context);
718762
#endif
719763

764+
// gr_direct_context_t* gr_direct_context_make_direct3d(const gr_d3d_backendcontext_t d3dBackendContext)
765+
#if !USE_DELEGATES
766+
#if USE_LIBRARY_IMPORT
767+
[LibraryImport (SKIA)]
768+
internal static partial gr_direct_context_t gr_direct_context_make_direct3d (GRD3DBackendContextNative d3dBackendContext);
769+
#else // !USE_LIBRARY_IMPORT
770+
[DllImport (SKIA, CallingConvention = CallingConvention.Cdecl)]
771+
internal static extern gr_direct_context_t gr_direct_context_make_direct3d (GRD3DBackendContextNative d3dBackendContext);
772+
#endif
773+
#else
774+
private partial class Delegates {
775+
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
776+
internal delegate gr_direct_context_t gr_direct_context_make_direct3d (GRD3DBackendContextNative d3dBackendContext);
777+
}
778+
private static Delegates.gr_direct_context_make_direct3d gr_direct_context_make_direct3d_delegate;
779+
internal static gr_direct_context_t gr_direct_context_make_direct3d (GRD3DBackendContextNative d3dBackendContext) =>
780+
(gr_direct_context_make_direct3d_delegate ??= GetSymbol<Delegates.gr_direct_context_make_direct3d> ("gr_direct_context_make_direct3d")).Invoke (d3dBackendContext);
781+
#endif
782+
783+
// gr_direct_context_t* gr_direct_context_make_direct3d_with_options(const gr_d3d_backendcontext_t d3dBackendContext, const gr_context_options_t* options)
784+
#if !USE_DELEGATES
785+
#if USE_LIBRARY_IMPORT
786+
[LibraryImport (SKIA)]
787+
internal static partial gr_direct_context_t gr_direct_context_make_direct3d_with_options (GRD3DBackendContextNative d3dBackendContext, GRContextOptionsNative* options);
788+
#else // !USE_LIBRARY_IMPORT
789+
[DllImport (SKIA, CallingConvention = CallingConvention.Cdecl)]
790+
internal static extern gr_direct_context_t gr_direct_context_make_direct3d_with_options (GRD3DBackendContextNative d3dBackendContext, GRContextOptionsNative* options);
791+
#endif
792+
#else
793+
private partial class Delegates {
794+
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
795+
internal delegate gr_direct_context_t gr_direct_context_make_direct3d_with_options (GRD3DBackendContextNative d3dBackendContext, GRContextOptionsNative* options);
796+
}
797+
private static Delegates.gr_direct_context_make_direct3d_with_options gr_direct_context_make_direct3d_with_options_delegate;
798+
internal static gr_direct_context_t gr_direct_context_make_direct3d_with_options (GRD3DBackendContextNative d3dBackendContext, GRContextOptionsNative* options) =>
799+
(gr_direct_context_make_direct3d_with_options_delegate ??= GetSymbol<Delegates.gr_direct_context_make_direct3d_with_options> ("gr_direct_context_make_direct3d_with_options")).Invoke (d3dBackendContext, options);
800+
#endif
801+
720802
// gr_direct_context_t* gr_direct_context_make_gl(const gr_glinterface_t* glInterface)
721803
#if !USE_DELEGATES
722804
#if USE_LIBRARY_IMPORT
@@ -17504,6 +17586,108 @@ public readonly override int GetHashCode ()
1750417586

1750517587
}
1750617588

17589+
// gr_d3d_backendcontext_t
17590+
[StructLayout (LayoutKind.Sequential)]
17591+
internal unsafe partial struct GRD3DBackendContextNative : IEquatable<GRD3DBackendContextNative> {
17592+
// public d3d_dxgi_adapter_t* fAdapter
17593+
public d3d_dxgi_adapter_t fAdapter;
17594+
17595+
// public d3d_d12_device_t* fDevice
17596+
public d3d_d12_device_t fDevice;
17597+
17598+
// public d3d_d12_command_queue_t* fQueue
17599+
public d3d_d12_command_queue_t fQueue;
17600+
17601+
// public gr_d3d_memory_allocator_t* fMemoryAllocator
17602+
public gr_d3d_memory_allocator_t fMemoryAllocator;
17603+
17604+
// public bool fProtectedContext
17605+
public Byte fProtectedContext;
17606+
17607+
public readonly bool Equals (GRD3DBackendContextNative obj) =>
17608+
#pragma warning disable CS8909
17609+
fAdapter == obj.fAdapter && fDevice == obj.fDevice && fQueue == obj.fQueue && fMemoryAllocator == obj.fMemoryAllocator && fProtectedContext == obj.fProtectedContext;
17610+
#pragma warning restore CS8909
17611+
17612+
public readonly override bool Equals (object obj) =>
17613+
obj is GRD3DBackendContextNative f && Equals (f);
17614+
17615+
public static bool operator == (GRD3DBackendContextNative left, GRD3DBackendContextNative right) =>
17616+
left.Equals (right);
17617+
17618+
public static bool operator != (GRD3DBackendContextNative left, GRD3DBackendContextNative right) =>
17619+
!left.Equals (right);
17620+
17621+
public readonly override int GetHashCode ()
17622+
{
17623+
var hash = new HashCode ();
17624+
hash.Add (fAdapter);
17625+
hash.Add (fDevice);
17626+
hash.Add (fQueue);
17627+
hash.Add (fMemoryAllocator);
17628+
hash.Add (fProtectedContext);
17629+
return hash.ToHashCode ();
17630+
}
17631+
17632+
}
17633+
17634+
// gr_d3d_textureresourceinfo_t
17635+
[StructLayout (LayoutKind.Sequential)]
17636+
internal unsafe partial struct GRD3DTextureResourceInfoNative : IEquatable<GRD3DTextureResourceInfoNative> {
17637+
// public d3d_d12_resource_t* fResource
17638+
public d3d_d12_resource_t fResource;
17639+
17640+
// public d3d_alloc_t* fAlloc
17641+
public d3d_alloc_t fAlloc;
17642+
17643+
// public uint32_t fResourceState
17644+
public UInt32 fResourceState;
17645+
17646+
// public uint32_t fFormat
17647+
public UInt32 fFormat;
17648+
17649+
// public uint32_t fSampleCount
17650+
public UInt32 fSampleCount;
17651+
17652+
// public uint32_t fLevelCount
17653+
public UInt32 fLevelCount;
17654+
17655+
// public unsigned int fSampleQualityPattern
17656+
public UInt32 fSampleQualityPattern;
17657+
17658+
// public bool fProtected
17659+
public Byte fProtected;
17660+
17661+
public readonly bool Equals (GRD3DTextureResourceInfoNative obj) =>
17662+
#pragma warning disable CS8909
17663+
fResource == obj.fResource && fAlloc == obj.fAlloc && fResourceState == obj.fResourceState && fFormat == obj.fFormat && fSampleCount == obj.fSampleCount && fLevelCount == obj.fLevelCount && fSampleQualityPattern == obj.fSampleQualityPattern && fProtected == obj.fProtected;
17664+
#pragma warning restore CS8909
17665+
17666+
public readonly override bool Equals (object obj) =>
17667+
obj is GRD3DTextureResourceInfoNative f && Equals (f);
17668+
17669+
public static bool operator == (GRD3DTextureResourceInfoNative left, GRD3DTextureResourceInfoNative right) =>
17670+
left.Equals (right);
17671+
17672+
public static bool operator != (GRD3DTextureResourceInfoNative left, GRD3DTextureResourceInfoNative right) =>
17673+
!left.Equals (right);
17674+
17675+
public readonly override int GetHashCode ()
17676+
{
17677+
var hash = new HashCode ();
17678+
hash.Add (fResource);
17679+
hash.Add (fAlloc);
17680+
hash.Add (fResourceState);
17681+
hash.Add (fFormat);
17682+
hash.Add (fSampleCount);
17683+
hash.Add (fLevelCount);
17684+
hash.Add (fSampleQualityPattern);
17685+
hash.Add (fProtected);
17686+
return hash.ToHashCode ();
17687+
}
17688+
17689+
}
17690+
1750717691
// gr_gl_framebufferinfo_t
1750817692
[StructLayout (LayoutKind.Sequential)]
1750917693
public unsafe partial struct GRGlFramebufferInfo : IEquatable<GRGlFramebufferInfo> {

0 commit comments

Comments
 (0)