Silk.net setup -> how can i make this work #483
Replies: 4 comments 3 replies
-
I guess you've worked this out by now? |
Beta Was this translation helpful? Give feedback.
-
I got this working. I had to use Silks Window class. I also added debugging, which is really important when working with DirectX. It gives information in the output window. You'll see that I've...
There are a couple of little things I changed; such is disabling tearing. Moved some code around. Changed the SwapEffect to FlipDiscard (which is just what I usually use). You can fiddle with those things as you need. The main thing that was needed, well, the way I did it anyway. Was change the swapchain creation call to this... (nint winH, nint hdc, nint hInstance)? nativeHooks = Window.Native.Win32;
SwapChain = factory.CreateSwapChainForHwnd(Device.QueryInterface<IDXGIDevice>(), nativeHooks.Value.winH, desc); This method takes the window-handle from the underlying Win32 window - that's the call I use for desktop apps if I'm not using UWP / WinUI. |
Beta Was this translation helpful? Give feedback.
-
And here's the code in full, for reference. using Silk.NET.Windowing;
using System.Diagnostics;
using Vortice.Direct3D;
using Vortice.Direct3D11;
using Vortice.Direct3D11.Debug;
using Vortice.DXGI;
using Vortice.Mathematics;
using static Vortice.Direct3D11.D3D11;
namespace App;
class Program
{
static void Main()
{
var app = new D3D11Application();
//app.Run();
app.Cleanup();
}
}
unsafe public class D3D11Application
{
private IWindow Window;
private ID3D11Device1 Device;
private ID3D11DeviceContext1 DeviceContext;
private IDXGISwapChain1 SwapChain;
private ID3D11Texture2D OutputT2d;
private ID3D11RenderTargetView OutputRtv;
private bool IsTearingSupported;
private Format Format = Format.B8G8R8A8_UNorm;
private bool IsGraphicsReady;
public D3D11Application()
{
WindowOptions options = new WindowOptions()
{
Size = new Silk.NET.Maths.Vector2D<int>(800, 600),
Title = "D3D11 Application",
};
Window = Silk.NET.Windowing.Window.Create(options);
Window.IsVisible = true;
Window.Initialize();
Debug.WriteLine("Window.Handle= " + Window.Handle);
Window.Render += Render;
Window.Run();
}
private void Render(double obj)
{
if (!IsGraphicsReady)
{
InitializeD3D11();
IsGraphicsReady = true;
}
Debug.WriteLine("Render");
DeviceContext.ClearRenderTargetView(OutputRtv!, new Color4(0, 1, 0, 1));
SwapBuffers();
}
private void InitializeD3D11()
{
DXGI.CreateDXGIFactory2(true, out IDXGIFactory2 factory).CheckError();
using (IDXGIFactory5? factory5 = factory.QueryInterfaceOrNull<IDXGIFactory5>())
{
if (factory5 != null)
{
IsTearingSupported = factory5.PresentAllowTearing;
}
}
using var adapter = GetHardwareAdapter(factory);
if (D3D11CreateDevice(
adapter,
DriverType.Unknown,
DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport,
[FeatureLevel.Level_11_0],
out ID3D11Device tempDevice,
out FeatureLevel featureLevel,
out ID3D11DeviceContext tempContext
).Failure)
{
throw new InvalidOperationException("Cannot create D3D11 device");
}
Device = tempDevice.QueryInterface<ID3D11Device1>();
DeviceContext = tempContext.QueryInterface<ID3D11DeviceContext1>();
// setup debug info
{
ID3D11Debug? d3d11Debug = Device.QueryInterfaceOrNull<ID3D11Debug>();
if (d3d11Debug != null)
{
ID3D11InfoQueue? d3d11InfoQueue = d3d11Debug.QueryInterfaceOrNull<ID3D11InfoQueue>();
if (d3d11InfoQueue != null)
{
d3d11InfoQueue.SetBreakOnSeverity(MessageSeverity.Corruption, true);
d3d11InfoQueue.SetBreakOnSeverity(MessageSeverity.Error, true);
MessageId[] hide =
[
MessageId.SetPrivateDataChangingParams,
];
Vortice.Direct3D11.Debug.InfoQueueFilter filter = new()
{
DenyList = new Vortice.Direct3D11.Debug.InfoQueueFilterDescription
{
Ids = hide
}
};
d3d11InfoQueue.AddStorageFilterEntries(filter);
Debug.WriteLine("ID3D11InfoQueue: d3d11InfoQueue.Dispose()");
d3d11InfoQueue.Dispose();
}
Debug.WriteLine("ID3D11Debug: d3d11Debug.Dispose()");
d3d11Debug.Dispose();
}
}
// Create swap chain
CreateSwapChain(factory);
}
private void CreateSwapChain(IDXGIFactory2 factory)
{
var desc = new SwapChainDescription1
{
Width = (uint)Window.Size.X,
Height = (uint)Window.Size.Y,
Format = Format,
Stereo = false,
SampleDescription = new SampleDescription(1, 0),
BufferUsage = Usage.RenderTargetOutput,
BufferCount = 2,
Scaling = Scaling.Stretch,
SwapEffect = SwapEffect.FlipDiscard,
AlphaMode = AlphaMode.Ignore,
Flags = SwapChainFlags.None
};
(nint winH, nint hdc, nint hInstance)? nativeHooks = Window.Native.Win32;
//nint winH = Window.Handle; // this doesn't work; must be a Silk.Net thing
SwapChain = factory.CreateSwapChainForHwnd(
Device.QueryInterface<IDXGIDevice>(),
nativeHooks.Value.winH,
desc);
// get ref to the backbuffer
OutputT2d = SwapChain.GetBuffer<ID3D11Texture2D>(0);
OutputRtv = Device.CreateRenderTargetView(OutputT2d);
}
private static IDXGIAdapter1 GetHardwareAdapter(IDXGIFactory2 factory)
{
for (uint adapterIndex = 0; factory.EnumAdapters1(adapterIndex, out IDXGIAdapter1 adapter).Success; adapterIndex++)
{
var desc = adapter.Description1;
if ((desc.Flags & AdapterFlags.Software) == AdapterFlags.None)
{
return adapter;
}
adapter.Dispose();
}
throw new InvalidOperationException("Cannot detect D3D11 adapter");
}
private void SwapBuffers()
{
if (SwapChain == null)
return;
uint syncInterval = 1;
//var presentFlags = IsTearingSupported ? PresentFlags.AllowTearing : PresentFlags.None;
PresentFlags flags = PresentFlags.None;
SwapChain.Present(syncInterval, flags);
}
public void Cleanup()
{
// Clean up resources
OutputRtv?.Dispose();
OutputT2d?.Dispose();
SwapChain?.Dispose();
DeviceContext?.Dispose();
Device?.Dispose();
}
} |
Beta Was this translation helpful? Give feedback.
-
Can I also suggest we change the title of this post to 'Silk.net setup' - then if will be easier to find for ppl using Silk.Net - which looks pretty cool actually. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
i wasn't able to make Vortice.Framework to work for some reason to use it while following the Vortice.Windows sample so I tried using Silk.Net.Sdl with it but I get this error
Argument type 'Vortice.DXGI.SwapChainDescription1' is not assignable to parameter type 'Vortice.DXGI.SwapChainDescription'
this is the Program.cs code if reference is needed also the windowing is handled in SdlWindow.cs also a question for direct x 11 should I drop sdl for something better or like use wpf idk I never used direct also it's the first time I am using vortice
Beta Was this translation helpful? Give feedback.
All reactions