Skip to content

Instantly share code, notes, and snippets.

@CAMongrel
Created September 18, 2019 20:37
Show Gist options
  • Save CAMongrel/c0530211e552303b7ed6b650184f1460 to your computer and use it in GitHub Desktop.
Save CAMongrel/c0530211e552303b7ed6b650184f1460 to your computer and use it in GitHub Desktop.
[PATCH] New Marshalling code for StateObjectDescription
From 6a418e70a072199ebedb474760d1e8559ceb5518 Mon Sep 17 00:00:00 2001
From: CAMongrel <camongrel@gmail.com>
Date: Wed, 18 Sep 2019 22:27:57 +0200
Subject: [PATCH] New Marshalling code for StateObjectDescription
---
.../DxilLibraryDescription.cs | 3 ++-
.../DxilSubObjectToExportsAssociation.cs | 3 ++-
.../ExistingCollectionDescription.cs | 3 ++-
src/Vortice.Direct3D12/GlobalRootSignature.cs | 3 ++-
src/Vortice.Direct3D12/HitGroupDescription.cs | 3 ++-
.../IStateSubObjectDescription.cs | 3 ++-
src/Vortice.Direct3D12/LocalRootSignature.cs | 3 ++-
src/Vortice.Direct3D12/NodeMask.cs | 3 ++-
.../RaytracingPipelineConfig.cs | 3 ++-
.../RaytracingShaderConfig.cs | 3 ++-
src/Vortice.Direct3D12/StateObjectConfig.cs | 3 ++-
.../StateObjectDescription.cs | 11 +++++++++-
src/Vortice.Direct3D12/StateSubObject.cs | 5 +++--
.../SubObjectToExportsAssociation.cs | 20 ++++++++++++-------
14 files changed, 48 insertions(+), 21 deletions(-)
diff --git a/src/Vortice.Direct3D12/DxilLibraryDescription.cs b/src/Vortice.Direct3D12/DxilLibraryDescription.cs
index 20095e1..098f590 100644
--- a/src/Vortice.Direct3D12/DxilLibraryDescription.cs
+++ b/src/Vortice.Direct3D12/DxilLibraryDescription.cs
@@ -2,6 +2,7 @@
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using System;
+using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Vortice.DirectX;
@@ -45,7 +46,7 @@ namespace Vortice.Direct3D12
public ExportDescription.__Native* pExports;
}
- unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc()
+ unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc(Dictionary<StateSubObject, IntPtr> subObjectLookup)
{
__Native* native = (__Native*)Marshal.AllocHGlobal(sizeof(__Native));
diff --git a/src/Vortice.Direct3D12/DxilSubObjectToExportsAssociation.cs b/src/Vortice.Direct3D12/DxilSubObjectToExportsAssociation.cs
index 0ef7ac2..8be6269 100644
--- a/src/Vortice.Direct3D12/DxilSubObjectToExportsAssociation.cs
+++ b/src/Vortice.Direct3D12/DxilSubObjectToExportsAssociation.cs
@@ -2,6 +2,7 @@
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using System;
+using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -40,7 +41,7 @@ namespace Vortice.Direct3D12
}
- unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc()
+ unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc(Dictionary<StateSubObject, IntPtr> subObjectLookup)
{
__Native* native = (__Native*)Marshal.AllocHGlobal(sizeof(__Native));
native->pSubobjectToAssociate = Marshal.StringToHGlobalUni(SubObjectToAssociate);
diff --git a/src/Vortice.Direct3D12/ExistingCollectionDescription.cs b/src/Vortice.Direct3D12/ExistingCollectionDescription.cs
index cc7e3b7..d563846 100644
--- a/src/Vortice.Direct3D12/ExistingCollectionDescription.cs
+++ b/src/Vortice.Direct3D12/ExistingCollectionDescription.cs
@@ -2,6 +2,7 @@
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using System;
+using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SharpGen.Runtime;
@@ -41,7 +42,7 @@ namespace Vortice.Direct3D12
public ExportDescription.__Native* pExports;
}
- unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc()
+ unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc(Dictionary<StateSubObject, IntPtr> subObjectLookup)
{
__Native* native = (__Native*)Marshal.AllocHGlobal(sizeof(__Native));
diff --git a/src/Vortice.Direct3D12/GlobalRootSignature.cs b/src/Vortice.Direct3D12/GlobalRootSignature.cs
index 9a3caae..eebe08b 100644
--- a/src/Vortice.Direct3D12/GlobalRootSignature.cs
+++ b/src/Vortice.Direct3D12/GlobalRootSignature.cs
@@ -2,6 +2,7 @@
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using System;
+using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SharpGen.Runtime;
@@ -25,7 +26,7 @@ namespace Vortice.Direct3D12
}
#region Marshal
- unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc()
+ unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc(Dictionary<StateSubObject, IntPtr> subObjectLookup)
{
__Native* native = (__Native*)Marshal.AllocHGlobal(sizeof(__Native));
native->RootSignature = CppObject.ToCallbackPtr<ID3D12RootSignature>(RootSignature);
diff --git a/src/Vortice.Direct3D12/HitGroupDescription.cs b/src/Vortice.Direct3D12/HitGroupDescription.cs
index 1f74f9d..90a1e01 100644
--- a/src/Vortice.Direct3D12/HitGroupDescription.cs
+++ b/src/Vortice.Direct3D12/HitGroupDescription.cs
@@ -2,6 +2,7 @@
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using System;
+using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -37,7 +38,7 @@ namespace Vortice.Direct3D12
}
#region Marshal
- unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc()
+ unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc(Dictionary<StateSubObject, IntPtr> subObjectLookup)
{
__Native* native = (__Native*)Marshal.AllocHGlobal(sizeof(__Native));
native->HitGroupExport = Marshal.StringToHGlobalUni(HitGroupExport);
diff --git a/src/Vortice.Direct3D12/IStateSubObjectDescription.cs b/src/Vortice.Direct3D12/IStateSubObjectDescription.cs
index 663b0e9..e77b710 100644
--- a/src/Vortice.Direct3D12/IStateSubObjectDescription.cs
+++ b/src/Vortice.Direct3D12/IStateSubObjectDescription.cs
@@ -2,6 +2,7 @@
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using System;
+using System.Collections.Generic;
namespace Vortice.Direct3D12
{
@@ -12,7 +13,7 @@ namespace Vortice.Direct3D12
internal interface IStateSubObjectDescriptionMarshal
{
- IntPtr __MarshalAlloc();
+ IntPtr __MarshalAlloc(Dictionary<StateSubObject, IntPtr> subObjectLookup);
void __MarshalFree(ref IntPtr pDesc);
}
}
diff --git a/src/Vortice.Direct3D12/LocalRootSignature.cs b/src/Vortice.Direct3D12/LocalRootSignature.cs
index 358534a..c7eda81 100644
--- a/src/Vortice.Direct3D12/LocalRootSignature.cs
+++ b/src/Vortice.Direct3D12/LocalRootSignature.cs
@@ -2,6 +2,7 @@
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using System;
+using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SharpGen.Runtime;
@@ -26,7 +27,7 @@ namespace Vortice.Direct3D12
}
#region Marshal
- unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc()
+ unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc(Dictionary<StateSubObject, IntPtr> subObjectLookup)
{
__Native* native = (__Native*)Marshal.AllocHGlobal(sizeof(__Native));
native->RootSignature = CppObject.ToCallbackPtr<ID3D12RootSignature>(RootSignature);
diff --git a/src/Vortice.Direct3D12/NodeMask.cs b/src/Vortice.Direct3D12/NodeMask.cs
index 392f92a..d40a484 100644
--- a/src/Vortice.Direct3D12/NodeMask.cs
+++ b/src/Vortice.Direct3D12/NodeMask.cs
@@ -2,6 +2,7 @@
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using System;
+using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SharpGen.Runtime;
@@ -26,7 +27,7 @@ namespace Vortice.Direct3D12
}
#region Marshal
- unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc()
+ unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc(Dictionary<StateSubObject, IntPtr> subObjectLookup)
{
var native = Marshal.AllocHGlobal(sizeof(NodeMask));
Unsafe.Write(native.ToPointer(), Mask);
diff --git a/src/Vortice.Direct3D12/RaytracingPipelineConfig.cs b/src/Vortice.Direct3D12/RaytracingPipelineConfig.cs
index 7d479ed..4e480d4 100644
--- a/src/Vortice.Direct3D12/RaytracingPipelineConfig.cs
+++ b/src/Vortice.Direct3D12/RaytracingPipelineConfig.cs
@@ -2,6 +2,7 @@
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using System;
+using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -28,7 +29,7 @@ namespace Vortice.Direct3D12
}
#region Marshal
- unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc()
+ unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc(Dictionary<StateSubObject, IntPtr> subObjectLookup)
{
var native = Marshal.AllocHGlobal(sizeof(RaytracingPipelineConfig));
Unsafe.WriteUnaligned(native.ToPointer(), this);
diff --git a/src/Vortice.Direct3D12/RaytracingShaderConfig.cs b/src/Vortice.Direct3D12/RaytracingShaderConfig.cs
index e3321a8..107239b 100644
--- a/src/Vortice.Direct3D12/RaytracingShaderConfig.cs
+++ b/src/Vortice.Direct3D12/RaytracingShaderConfig.cs
@@ -2,6 +2,7 @@
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using System;
+using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -31,7 +32,7 @@ namespace Vortice.Direct3D12
}
#region Marshal
- unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc()
+ unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc(Dictionary<StateSubObject, IntPtr> subObjectLookup)
{
var native = Marshal.AllocHGlobal(sizeof(RaytracingShaderConfig));
Unsafe.WriteUnaligned(native.ToPointer(), this);
diff --git a/src/Vortice.Direct3D12/StateObjectConfig.cs b/src/Vortice.Direct3D12/StateObjectConfig.cs
index 9297a86..1aacf64 100644
--- a/src/Vortice.Direct3D12/StateObjectConfig.cs
+++ b/src/Vortice.Direct3D12/StateObjectConfig.cs
@@ -2,6 +2,7 @@
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using System;
+using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -24,7 +25,7 @@ namespace Vortice.Direct3D12
}
#region Marshal
- unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc()
+ unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc(Dictionary<StateSubObject, IntPtr> subObjectLookup)
{
var description = Marshal.AllocHGlobal(sizeof(StateObjectConfig));
Unsafe.WriteUnaligned(description.ToPointer(), this);
diff --git a/src/Vortice.Direct3D12/StateObjectDescription.cs b/src/Vortice.Direct3D12/StateObjectDescription.cs
index 8586eac..44bb7b5 100644
--- a/src/Vortice.Direct3D12/StateObjectDescription.cs
+++ b/src/Vortice.Direct3D12/StateObjectDescription.cs
@@ -2,6 +2,7 @@
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using System;
+using System.Collections.Generic;
using System.Runtime.InteropServices;
using Vortice.DirectX;
@@ -56,10 +57,18 @@ namespace Vortice.Direct3D12
@ref.NumSubobjects = SubObjects?.Length ?? 0;
if (@ref.NumSubobjects > 0)
{
+ var subObjectLookup = new Dictionary<StateSubObject, IntPtr>();
var nativeSubObjects = (StateSubObject.__Native*)Interop.Alloc<StateSubObject.__Native>(@ref.NumSubobjects);
+
+ // Create lookup table first
+ for (int i = 0; i < @ref.NumSubobjects; i++)
+ {
+ subObjectLookup.Add(SubObjects[i], new IntPtr(&nativeSubObjects[i]));
+ }
+
for (int i = 0; i < @ref.NumSubobjects; i++)
{
- SubObjects[i].__MarshalTo(ref nativeSubObjects[i]);
+ SubObjects[i].__MarshalTo(ref nativeSubObjects[i], subObjectLookup);
}
@ref.pSubobjects = nativeSubObjects;
diff --git a/src/Vortice.Direct3D12/StateSubObject.cs b/src/Vortice.Direct3D12/StateSubObject.cs
index a7405c5..f7adc06 100644
--- a/src/Vortice.Direct3D12/StateSubObject.cs
+++ b/src/Vortice.Direct3D12/StateSubObject.cs
@@ -2,6 +2,7 @@
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using System;
+using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Vortice.Direct3D12
@@ -37,12 +38,12 @@ namespace Vortice.Direct3D12
}
}
- internal unsafe void __MarshalTo(ref __Native @ref)
+ internal unsafe void __MarshalTo(ref __Native @ref, Dictionary<StateSubObject, IntPtr> subObjectLookup)
{
@ref.Type = Type;
if (Description is IStateSubObjectDescriptionMarshal descriptionMarshal)
{
- @ref.pDesc = descriptionMarshal.__MarshalAlloc();
+ @ref.pDesc = descriptionMarshal.__MarshalAlloc(subObjectLookup);
}
}
#endregion
diff --git a/src/Vortice.Direct3D12/SubObjectToExportsAssociation.cs b/src/Vortice.Direct3D12/SubObjectToExportsAssociation.cs
index cd93b6a..d3d2e38 100644
--- a/src/Vortice.Direct3D12/SubObjectToExportsAssociation.cs
+++ b/src/Vortice.Direct3D12/SubObjectToExportsAssociation.cs
@@ -2,6 +2,7 @@
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using System;
+using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Vortice.DirectX;
@@ -35,17 +36,21 @@ namespace Vortice.Direct3D12
[StructLayout(LayoutKind.Sequential, Pack = 0)]
internal unsafe struct __Native
{
- public StateSubObject.__Native pSubobjectToAssociate;
+ public StateSubObject.__Native* pSubobjectToAssociate;
public int NumExports;
public IntPtr* pExports;
}
- unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc()
+ unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc(Dictionary<StateSubObject, IntPtr> subObjectLookup)
{
__Native* native = (__Native*)Marshal.AllocHGlobal(sizeof(__Native));
- SubObjectToAssociate?.__MarshalTo(ref native->pSubobjectToAssociate);
+ if (subObjectLookup.ContainsKey(SubObjectToAssociate) == false)
+ {
+ throw new InvalidOperationException("Associated StateSubObject must be a reference to an element of the array in StateObjectDescription");
+ }
+ native->pSubobjectToAssociate = (StateSubObject.__Native*)subObjectLookup[SubObjectToAssociate].ToPointer();
native->NumExports = Exports?.Length ?? 0;
if (native->NumExports > 0)
{
@@ -61,11 +66,12 @@ namespace Vortice.Direct3D12
return (IntPtr)native;
}
- unsafe void IStateSubObjectDescriptionMarshal.__MarshalFree(ref IntPtr pDesc)
+ unsafe void IStateSubObjectDescriptionMarshal.__MarshalFree(ref IntPtr pNative)
{
- ref __Native native = ref Unsafe.AsRef<__Native>(pDesc.ToPointer());
- SubObjectToAssociate?.__MarshalFree(ref native.pSubobjectToAssociate);
+ ref __Native native = ref Unsafe.AsRef<__Native>(pNative.ToPointer());
+ // native->pSubobjectToAssociate will be freed in StateObjectDescription
+ native.pSubobjectToAssociate = null;
if (native.NumExports > 0)
{
for (int i = 0; i < native.NumExports; i++)
@@ -76,7 +82,7 @@ namespace Vortice.Direct3D12
Marshal.FreeHGlobal(new IntPtr(native.pExports));
}
- Marshal.FreeHGlobal(pDesc);
+ Marshal.FreeHGlobal(pNative);
}
#endregion
}
--
2.22.0.windows.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment