BattleFrogue
BattleFrogue
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
Awesome, thanks.
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
Got that function working. At least for evalutating how many layers there are. Now to make it return the actual data. Thanks again
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
True, true. But like I said, I'd rather learn this myself so I can apply it to other libraries. My main ulterior motive right now are some internal libraries at work. We're forced to use C/C++ because they are C libraries. But I personally have fallen out of favour with C++ and want to move to .NET now it has AOT support. I picked Vulkan as a practice library because it's a library I have native familiarity with and because it had the added complexity of needing to load function pointers manually to skip trampolining. I figured if I can get some semi-complex demo running using Vulkan I can map our internal libraries to .NET and use a more palletable language (in my opinion)
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
I see, that does complicate things. In my own research I find that there's either a lot of research on a topic, usually pre-2012. Or 1 page on MS's docs site that doesn't really give me enough information for me to understand how it works
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
I see, I have been lied to by the internet. Colour me shocked 🤣
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
Is there something wrong with IntPtr? I thought that was how C# represented pointers and IntPtr.Zero is the equivalent of null?
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
Assuming that that's correct, I would also typedef the function pointer for better readability
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
Sorry for the ping, but I just wanted to check my use of C# function pointers. Would this be how you expect them to be used?
using System.Runtime.InteropServices;

namespace VulkanSDK.VulkanNative
{
public class ExtensionProperties
{
// TODO: Populate struct
}

public static unsafe partial class Library
{

[LibraryImport("vulkan-1.dll", EntryPoint = "vkGetInstanceProcAddr", StringMarshalling = StringMarshalling.Utf8)]
private static partial IntPtr GetInstanceProcAddr(IntPtr module, string name);

private static delegate* unmanaged[Cdecl]<IntPtr, IntPtr, List<ExtensionProperties>> EnumerateInstanceLayerPropertiesDelegate;

public static void LoadLibrary()
{
EnumerateInstanceLayerPropertiesDelegate = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr, List<ExtensionProperties>>)GetInstanceProcAddr(IntPtr.Zero, "vkEnumerateInstanceLayerProperties");
List<ExtensionProperties> extensionPropertiesList = EnumerateInstanceLayerPropertiesDelegate(IntPtr.Zero, IntPtr.Zero);
}
}
}
using System.Runtime.InteropServices;

namespace VulkanSDK.VulkanNative
{
public class ExtensionProperties
{
// TODO: Populate struct
}

public static unsafe partial class Library
{

[LibraryImport("vulkan-1.dll", EntryPoint = "vkGetInstanceProcAddr", StringMarshalling = StringMarshalling.Utf8)]
private static partial IntPtr GetInstanceProcAddr(IntPtr module, string name);

private static delegate* unmanaged[Cdecl]<IntPtr, IntPtr, List<ExtensionProperties>> EnumerateInstanceLayerPropertiesDelegate;

public static void LoadLibrary()
{
EnumerateInstanceLayerPropertiesDelegate = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr, List<ExtensionProperties>>)GetInstanceProcAddr(IntPtr.Zero, "vkEnumerateInstanceLayerProperties");
List<ExtensionProperties> extensionPropertiesList = EnumerateInstanceLayerPropertiesDelegate(IntPtr.Zero, IntPtr.Zero);
}
}
}
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
Thanks, I'll keep that in mind. Any particular location to shout you from?
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
Right, because vkGetInstanceProcAddr/vkGetDeviceProcAddr require a valid Instance or Device. C get's away with it because it does the trampolining, but if I wanted to do that in C# every function would have to be LibraryLoaded which would be a performance nightmare. Okay maybe I don't do that then 😂
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
I do also plan to have a Raw namepsace which will be just the C api with a C# front cover. So in the event that I want to use the raw API I can, and internall the managed API will use the Raw function calls
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
I will admit I am more familiar with the raw C api and prefer it to say the C++ api. But I think in the context of C/C++ it makes sense to use the raw API. C# is a managed lanugage and I feel like having a more managed approach to it, even when AOT'd, makes sense
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
That sounds very similar to how I wanted to set mine up
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
Oh cool, so I was part way there with my LibraryLoad of vkGetInstanceProcAddr it was just the latter part that was slow and clunky (and broken)
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
I see, thanks for the advice. And just so I'm understanding this correctly. I would be able to use a C# 9 function pointer for a native function pointer returned to me by using vkGetInstanceProcAddr? I would like to follow Khronos' guidelines for reducing driver overhead
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
I do, I have [UnmanagedFunctionPointer(CallingConvention.StdCall)] and I have also tried [UnmanagedFunctionPointer(CallingConvention.Cdecl]. I have seen Silk.NET, and as nice as it looks part of this exercise is me wanting to learn how to do performant native interops. Even as wide spread as the .NET community is I'm not always going to be lucky enough that there is a ready made library that does it for me. It's interesting that you mention that delegates are slow and clunky because I actually asked here in a different thread what the most performance way to do native function calls are and the LibraryLoad attribute was what was suggested to me. Does Silk use C#9 function pointers?
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
If I try to continue the app stops entirely and reports a fatal internal CLR error:
Fatal error. Internal CLR error. (0x80131506)
at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointerInternal(IntPtr, System.Type)
at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](IntPtr)
at VulkanSDK.VulkanNative.Library.LoadLibrary()
at Program.<Main>$(System.String[])
Fatal error. Internal CLR error. (0x80131506)
at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointerInternal(IntPtr, System.Type)
at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](IntPtr)
at VulkanSDK.VulkanNative.Library.LoadLibrary()
at Program.<Main>$(System.String[])
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
I have tried catching both Exceptions and EngineExecutionExceptions. In fact when I tried catching an EngineExecutionException specifically Rider warned me that those exceptions aren't even raised by the runtime anymore.
50 replies
CC#
Created by BattleFrogue on 10/20/2023 in #help
✅ Debugging exceptions not caught by try-catch
No description
50 replies
CC#
Created by BattleFrogue on 9/6/2023 in #help
✅ What's the performant way of calling into native DLLs in .NET 7?
Thanks, I will look into it
11 replies