Skip to content

Commit d0d64c7

Browse files
committed
feat: add initialize function
1 parent 367c249 commit d0d64c7

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Source/ShaderCompiler.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,19 @@ namespace DirectXShaderCompiler.NET;
1616
/// </summary>
1717
public static partial class ShaderCompiler
1818
{
19-
private static readonly unsafe NativeDxcCompiler* nativeCompiler = DXCNative.DxcInitialize();
19+
private static unsafe NativeDxcCompiler* nativeCompiler = null;
20+
21+
/// <summary>
22+
/// Initializes the native compiler library immediately.
23+
/// This method is implicitly called when using any of the compilation methods.
24+
/// </summary>
25+
public static unsafe void Initialize()
26+
{
27+
if (nativeCompiler != null)
28+
return;
29+
30+
nativeCompiler = DXCNative.DxcInitialize();
31+
}
2032

2133
private static readonly unsafe void* includePtr = (void*)Marshal.GetFunctionPointerForDelegate<NativeDxcIncludeFunction>(Include);
2234
private static unsafe NativeDxcIncludeResult* Include(IntPtr nativeContext, byte* headerUtf8)
@@ -124,6 +136,8 @@ public static CompilationResult Compile(string code, string[] compilerArgs, File
124136
/// <returns>A CompilationResult structure containing the resulting bytecode and possible errors.</returns>
125137
public unsafe static CompilationResult Compile(byte[] code, byte[][] compilerArgs, FileIncludeHandler? includeHandler = null)
126138
{
139+
Initialize();
140+
127141
// Ideally the pins would use the fixed() keyword, but nested arrays can only be pinned with GCHandles, so for consistency, we use those.
128142
GCHandle codePinned = GCHandle.Alloc(code, GCHandleType.Pinned);
129143

0 commit comments

Comments
 (0)