facticiusvir / sharpvk-samples Goto Github PK
View Code? Open in Web Editor NEWSample SharpVk applications using Vulkan API in C#
Sample SharpVk applications using Vulkan API in C#
I've tested SharpVK on linux, to have it working (debian 10):
After these two simple steps, HelloTriangle works.
Current sample projects use WinForms as the Surface host, which makes them dependent on Windows; GLFW is a cross-platform window library with explicit support for Vulkan Surfaces.
It's just a suggestion, maybe it's worth to reference other repository which use SharpVK.
In this case it's one of mine, it's the Triangle example in F# (instead of C#).
https://github.com/realvictorprm/FSharpVulkanSamples
Will there be an example how to draw text in C#?
Like SaschaWillems´s textoverlay for C++
Thanks in advance for your answer.
Hello,
When attempting to run CreateDebugReportCallback, I encounter a System.ArgumentNullException.
Here is the stack trace:
Value cannot be null. (Parameter 'ptr')
Stack trace: at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(IntPtr ptr, Type t)
at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer[TDelegate](IntPtr ptr)
at SharpVk.CommandCache.GetCommandDelegate[T](String name, String type)
at SharpVk.Multivendor.InstanceExtensions.CreateDebugReportCallback(Instance extendedHandle, DebugReportCallbackDelegate callback, Nullable`1 flags, Nullable`1 userData, Nullable`1 allocator)
at DNA.Application..ctor(Int32 windowWidth, Int32 windowHeight) in C:\Users\Woodensponge\Documents\GitHub\Dust-and-Ashes\Dust and Ashes\Application.cs:line 83
at Program.Run(String[] args) in C:\Users\Woodensponge\Documents\GitHub\Dust-and-Ashes\Dust and Ashes\Program.cs:line 36
at Program.Main(String[] args) in C:\Users\Woodensponge\Documents\GitHub\Dust-and-Ashes\Dust and Ashes\Program.cs:line 12
Here is my code involving the whole process of creation.
Application.cs
using System;
using SharpVk.Glfw;
using SharpVk;
using System.Collections.Generic;
using System.Linq;
using DNA.Game;
using DNA.Game.StateTypes;
using DNA.Rendering;
using DNA.Utility;
using SharpVk.Multivendor;
namespace DNA
{
[Flags]
enum AppFlags
{
Running = 1 << 0,
Minimized = 1 << 1
}
class Application : Singleton<Application>, IDisposable
{
public Application()
: this(640, 480)
{
//TODO: Add configuration functionality, utilizing both the registry AND a config file.
}
public Application(int windowWidth, int windowHeight)
{
Debug.Log("Initiating Application...");
WindowWidth = windowWidth;
WindowHeight = windowHeight;
//Glfw stuff
Glfw3.Init();
Glfw3.SetErrorCallback(_glfwErrorDelegate);
Window = Glfw3.CreateWindow(windowWidth, windowHeight, "Dust and Ashes", IntPtr.Zero, IntPtr.Zero);
int screenWidth, screenHeight;
Glfw3.GetMonitorPhysicalSize(Glfw3.GetPrimaryMonitor(), out screenWidth, out screenHeight);
int windowX = (screenWidth - windowWidth) / 2;
int windowY = (screenHeight - windowHeight) / 2;
if (Window.RawHandle == IntPtr.Zero)
{
Debug.LogError("Glfw could not instance a window. Aborting program.");
}
//Vulkan Stuff
Vulkan.Instance.GetRequiredExtensions();
Vulkan.Instance.AddValidationLayer("VK_LAYER_LUNARG_standard_validation");
Vulkan.Instance.AddExtension(ExtExtensions.DebugReport);
Vulkan.Instance.CreateVulkanInstance(appInfo: new ApplicationInfo()
{
ApiVersion = new SharpVk.Version(1, 0, 0),
ApplicationVersion = new SharpVk.Version(1, 0, 0),
ApplicationName = "Dust and Ashes",
EngineName = "Dust and Ashes Engine",
EngineVersion = new SharpVk.Version(1, 0, 0)
});
/*
Value cannot be null. (Parameter 'ptr')
Stack trace: at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(IntPtr ptr, Type t)
at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer[TDelegate](IntPtr ptr)
at SharpVk.CommandCache.GetCommandDelegate[T](String name, String type)
at SharpVk.Multivendor.InstanceExtensions.CreateDebugReportCallback(Instance extendedHandle, DebugReportCallbackDelegate callback, Nullable`1 flags, Nullable`1 userData, Nullable`1 allocator)
at DNA.Application..ctor(Int32 windowWidth, Int32 windowHeight) in C:\Users\Woodensponge\Documents\GitHub\Dust-and-Ashes\Dust and Ashes\Application.cs:line 74
at Program.Run(String[] args) in C:\Users\Woodensponge\Documents\GitHub\Dust-and-Ashes\Dust and Ashes\Program.cs:line 36
at Program.Main(String[] args) in C:\Users\Woodensponge\Documents\GitHub\Dust-and-Ashes\Dust and Ashes\Program.cs:line 12
*/
#if !DEBUG
Vulkan.Instance.VkInstance.CreateDebugReportCallback(_debugReportVulkan, DebugReportFlags.Error | DebugReportFlags.Warning);
#elif DEBUG
Vulkan.Instance.VkInstance.CreateDebugReportCallback
(
_debugReportVulkan,
DebugReportFlags.Error |
DebugReportFlags.Warning |
DebugReportFlags.PerformanceWarning |
DebugReportFlags.Information |
DebugReportFlags.Debug
);
#endif
Flags |= AppFlags.Running;
CurrentState = new GameState();
}
public void Dispose()
{
CurrentState.Dispose();
Glfw3.Terminate();
}
public void Render()
{
CurrentState.Render();
}
public void Update()
{
PollEvents();
CurrentState.Update();
//State switching
if (CurrentState.StateToBeSwitched != null)
{
State newState = CurrentState.StateToBeSwitched;
Debug.Log($"Switching state from {CurrentState.GetType().Name} to {newState.GetType().Name}.");
CurrentState.Dispose();
CurrentState = newState;
Debug.Log("SWITCHED STATES!");
}
Render();
}
static void GlfwErrorLog(int error, string description)
{
Debug.Log($"GLFW ERROR | CODE:{error} | Message: {description}");
}
static void GlfwWindowSize(WindowHandle window, int width, int height)
{
Instance.WindowWidth = width;
Instance.WindowHeight = height;
Debug.Log($"Width: {width} Height: {height}");
}
static Bool32 DebugLogVulkan(DebugReportFlags flags, DebugReportObjectType objectType, ulong @object, HostSize location, int messageCode, string pLayerPrefix, string pMessage, IntPtr pUserData)
{
Debug.Log($"VULKAN | Report type: {flags} | CODE: {messageCode} | Message: {pMessage}");
return new Bool32(true);
}
void FixedUpdate()
{
//TODO: Add timestep (also actual functionality.)
CurrentState.FixedUpdate();
}
void PollEvents()
{
Glfw3.PollEvents();
if (Glfw3.WindowShouldClose(Window))
{
Flags &= ~AppFlags.Running;
}
}
public int WindowWidth;
public int WindowHeight;
public State CurrentState;
public WindowHandle Window
{
get
{
return _window;
}
private set
{
_window = value;
}
}
public AppFlags Flags;
WindowHandle _window;
static readonly ErrorDelegate _glfwErrorDelegate = GlfwErrorLog;
static readonly WindowSizeDelegate _glfwWindowSizeDelegate = GlfwWindowSize;
static readonly DebugReportCallbackDelegate _debugReportVulkan = DebugLogVulkan;
}
}
Vulkan.cs
using System.Collections.Generic;
using System.Linq;
using SharpVk.Glfw;
using DNA.Utility;
using SharpVk.Multivendor;
using System;
namespace DNA.Rendering
{
class Vulkan : Singleton<Vulkan>
{
public List<string> AddExtension(string extension)
{
extensions.Append(extension);
return extensions;
}
/// <summary>
/// Adds validation layers before the instance is created.
/// </summary>
/// <param name="layerName"></param>
/// <returns>The enabled layers list on success. Null on error.</returns>
public List<string> AddValidationLayer(string layerName)
{
if (SharpVk.Instance.EnumerateLayerProperties().Any(x => x.LayerName == layerName))
{
enabledLayers.Add(layerName);
}
else if (SharpVk.Instance.EnumerateLayerProperties().Any(x => x.LayerName != layerName))
{
return null;
}
return enabledLayers;
}
public SharpVk.Instance CreateVulkanInstance(SharpVk.ApplicationInfo appInfo)
{
VkInstance = SharpVk.Instance.Create(enabledLayers.ToArray(), extensions.ToArray(), SharpVk.InstanceCreateFlags.None, appInfo);
return VkInstance;
}
/// <summary>
/// Gets the required extensions needed to properly run vulkan. Must be run before instance creation.
/// </summary>
/// <returns>The extention list on success. Null on error.</returns>
public List<string> GetRequiredExtensions()
{
extensions = Glfw3.GetRequiredInstanceExtensions().ToList();
Debug.Log($"Loaded required Vulkan extensions: {extensions.Count}");
foreach (string extension in extensions)
{
Debug.Log($" {extension}");
}
return extensions;
}
public SharpVk.Instance VkInstance;
public List<string> extensions = new List<string>();
public List<string> enabledLayers = new List<string>();
}
}
Help would be greatly appreciated.
EDIT: Not only is this the wrong repository, but I've already found what was causing the issue.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.