Skip to content

Instantly share code, notes, and snippets.

@lostmsu
Created October 14, 2023 17:00
Show Gist options
  • Save lostmsu/a967d68f4a787e99dfe35d977720d1d6 to your computer and use it in GitHub Desktop.
Save lostmsu/a967d68f4a787e99dfe35d977720d1d6 to your computer and use it in GitHub Desktop.
reproduces wglDXOpenDeviceNV ERROR_OPEN_FAILED inside Sandbox with NVidia GPU
using System.ComponentModel;
using System.Runtime.InteropServices;
using SharpDX.Direct3D11;
using Silk.NET.Core.Contexts;
using Silk.NET.GLFW;
using Silk.NET.OpenGL;
using Silk.NET.WGL;
using Silk.NET.WGL.Extensions.NV;
using Silk.NET.Windowing;
var window = Window.Create(WindowOptions.Default with {
Title = "Silk.NET DXInterop Test",
Size = new(1280, 720)
});
window.Load += () => {
var gl = GL.GetApi(window);
var wgl = new WGL(gl.Context);
unsafe {
var glfw = Glfw.GetApi();
var context = new LamdaNativeContext(glfw.GetProcAddress);
var dxInterop = new NVDXInterop(context);
int? index = args.Length > 0 ? int.Parse(args[0]) : null;
Console.WriteLine($"{gl.GetStringS(StringName.Renderer)}\n#{index}\n");
using var factory = new SharpDX.DXGI.Factory1();
Device device;
var adapters = factory.Adapters;
if (index is { } i) adapters = new[] { adapters[i] };
Console.WriteLine($"Adapters: #{index} of {adapters.Length}");
foreach (var adapter in adapters) {
Console.WriteLine($"{adapter.Description.Description}");
device = new(adapter, DeviceCreationFlags.BgraSupport);
Marshal.SetLastSystemError(0);
Marshal.SetLastPInvokeError(0);
nint handle = dxInterop.DxopenDevice((void*)device.NativePointer);
if (handle == 0) {
int error = Marshal.GetLastSystemError();
if (error == 0) error = Marshal.GetLastPInvokeError();
device.Dispose();
Console.Error.WriteLine($"{error}: {new Win32Exception(error).Message}");
} else {
Console.WriteLine($"OK: {handle}");
break;
}
Console.WriteLine();
}
window.Close();
}
};
window.Run();
window.Dispose();
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<RootNamespace>GLDX_Sandbox</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SharpDX.Direct3D11" Version="4.2.0" />
<PackageReference Include="Silk.NET.OpenGL" Version="2.17.1" />
<PackageReference Include="Silk.NET.WGL.Extensions.NV" Version="2.17.1" />
<PackageReference Include="Silk.NET.Windowing.Glfw" Version="2.17.1" />
</ItemGroup>
</Project>
@lostmsu
Copy link
Author

lostmsu commented Oct 14, 2023

Prints:

NVIDIA GeForce RTX 3090
-1073282962: Unknown error (0xc007006e)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment