Exemple #1
0
        public static unsafe SwapchainSource GetSwapchainSource(Sdl2Window window)
        {
            IntPtr        sdlHandle = window.SdlWindowHandle;
            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sysWmInfo);
            switch (sysWmInfo.subsystem)
            {
            case SysWMType.Windows:
                Win32WindowInfo w32Info = Unsafe.Read <Win32WindowInfo>(&sysWmInfo.info);
                return(SwapchainSource.CreateWin32(w32Info.Sdl2Window, w32Info.hinstance));

            case SysWMType.X11:
                X11WindowInfo x11Info = Unsafe.Read <X11WindowInfo>(&sysWmInfo.info);
                return(SwapchainSource.CreateXlib(
                           x11Info.display,
                           x11Info.Sdl2Window));

            case SysWMType.Cocoa:
                CocoaWindowInfo cocoaInfo = Unsafe.Read <CocoaWindowInfo>(&sysWmInfo.info);
                IntPtr          nsWindow  = cocoaInfo.Window;
                return(SwapchainSource.CreateNSWindow(nsWindow));

            default:
                throw new PlatformNotSupportedException("Cannot create a SwapchainSource for " + sysWmInfo.subsystem + ".");
            }
        }
Exemple #2
0
        private static unsafe GraphicsDevice CreateMetalGraphicsDevice(GraphicsDeviceOptions options, Sdl2Window window)
        {
            IntPtr        sdlHandle = window.SdlWindowHandle;
            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sysWmInfo);
            ref CocoaWindowInfo cocoaInfo = ref Unsafe.AsRef <CocoaWindowInfo>(&sysWmInfo.info);
Exemple #3
0
        public static unsafe GraphicsDevice CreateDefaultOpenGLGraphicsDevice(
            GraphicsDeviceOptions options,
            Sdl2Window window,
            GraphicsBackend backend)
        {
            Sdl2Native.SDL_ClearError();

            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(window.SdlWindowHandle, &sysWmInfo);

            SetSDLGLContextAttributes(options, backend);

            IntPtr contextHandle = Sdl2Native.SDL_GL_CreateContext(window.SdlWindowHandle);
            byte * error         = Sdl2Native.SDL_GetError();

            if (error != null)
            {
                string errorString = GetString(error);
                if (!string.IsNullOrEmpty(errorString))
                {
                    throw new VeldridException(
                              $"Unable to create OpenGL Context: \"{errorString}\". This may indicate that the system does not support the requested OpenGL profile, version, or Swapchain format.");
                }
            }

            Sdl2Native.SDL_GL_SetSwapInterval(options.SyncToVerticalBlank ? 1 : 0);

            Veldrid.OpenGL.OpenGLPlatformInfo platformInfo = new Veldrid.OpenGL.OpenGLPlatformInfo(
                contextHandle,
                Sdl2Native.SDL_GL_GetProcAddress,
                context => Sdl2Native.SDL_GL_MakeCurrent(window.SdlWindowHandle, context),
                Sdl2Native.SDL_GL_GetCurrentContext,
                () => Sdl2Native.SDL_GL_MakeCurrent(new SDL_Window(IntPtr.Zero), IntPtr.Zero),
                Sdl2Native.SDL_GL_DeleteContext,
                () => Sdl2Native.SDL_GL_SwapWindow(window.SdlWindowHandle),
                sync => Sdl2Native.SDL_GL_SetSwapInterval(sync ? 1 : 0));

            Veldrid.OpenGLBinding.OpenGLNative.LoadGetString(Sdl2Native.SDL_GL_GetCurrentContext(), Sdl2Native.SDL_GL_GetProcAddress);
            byte *version  = Veldrid.OpenGLBinding.OpenGLNative.glGetString(Veldrid.OpenGLBinding.StringName.Version);
            byte *vendor   = Veldrid.OpenGLBinding.OpenGLNative.glGetString(Veldrid.OpenGLBinding.StringName.Vendor);
            byte *renderer = Veldrid.OpenGLBinding.OpenGLNative.glGetString(Veldrid.OpenGLBinding.StringName.Renderer);

            Logger.CoreVerbose("OpenGl info: ");
            Logger.CoreVerbose("  - Vendor: " + GetString(vendor));
            Logger.CoreVerbose("  - Renderer: " + GetString(renderer));
            Logger.CoreVerbose("  - Version: " + GetString(version));

            return(GraphicsDevice.CreateOpenGL(
                       options,
                       platformInfo,
                       (uint)window.Width,
                       (uint)window.Height));
        }
Exemple #4
0
        private static unsafe RenderContext CreateVulkanRenderContext(Sdl2Window window)
        {
            IntPtr        sdlHandle = window.SdlWindowHandle;
            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sysWmInfo);
            VkSurfaceSource surfaceSource = GetSurfaceSource(sysWmInfo);

            return(new VkRenderContext(surfaceSource, window.Width, window.Height));
        }
Exemple #5
0
        private static unsafe RenderContext CreateVulkanRenderContext(Sdl2Window window)
        {
            IntPtr        sdlHandle = window.SdlWindowHandle;
            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sysWmInfo);
            Win32WindowInfo w32Info = Unsafe.Read <Win32WindowInfo>(&sysWmInfo.info);

            return(new VkRenderContext(VkSurfaceSource.CreateWin32(w32Info.hinstance, w32Info.window), window.Width, window.Height));
        }
Exemple #6
0
        public static unsafe GraphicsDevice CreateVulkanGraphicsDevice(ref GraphicsDeviceCreateInfo contextCI, Sdl2Window window)
        {
            IntPtr        sdlHandle = window.SdlWindowHandle;
            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sysWmInfo);
            VkSurfaceSource surfaceSource = GetSurfaceSource(sysWmInfo);
            GraphicsDevice  gd            = Hacks.CreateVulkan(surfaceSource, (uint)window.Width, (uint)window.Height, contextCI.DebugDevice);

            return(gd);
        }
        private unsafe bool PlatformCreateVkSurface(ImGuiViewportPtr viewport)
        {
#if DEBUG
            using Profiler fullProfiler = new Profiler(GetType());
#endif
            //TODO: Properly avoid memory leak for unreleased Vk surface
            WindowBase    window = (WindowBase)GCHandle.FromIntPtr(viewport.PlatformUserData).Target;
            SDL_SysWMinfo sysWmInfo;
            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(window.SdlWindow.SdlWindowHandle, &sysWmInfo);
            return(GetSurfaceSource(sysWmInfo).CreateSurface(new Vulkan.VkInstance(window.SdlWindow.SdlWindowHandle)) != Vulkan.VkSurfaceKHR.Null);
        }
Exemple #8
0
        public static unsafe GraphicsDevice CreateDefaultOpenGLGraphicsDevice(
            GraphicsDeviceOptions options,
            Sdl2Window window,
            GraphicsBackend backend,
            bool colorSrgb)
        {
            Sdl2Native.SDL_ClearError();
            IntPtr sdlHandle = window.SdlWindowHandle;

            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sysWmInfo);

            SetSDLGLContextAttributes(options, backend, colorSrgb);

            IntPtr contextHandle = Sdl2Native.SDL_GL_CreateContext(sdlHandle);
            byte * error         = Sdl2Native.SDL_GetError();

            if (error != null)
            {
                string errorString = GetString(error);
                if (!string.IsNullOrEmpty(errorString))
                {
                    throw new VeldridException(
                              $"Unable to create OpenGL Context: \"{errorString}\". This may indicate that the system does not support the requested OpenGL profile, version, or Swapchain format.");
                }
            }

            int actualDepthSize;
            int result = Sdl2Native.SDL_GL_GetAttribute(SDL_GLAttribute.DepthSize, &actualDepthSize);
            int actualStencilSize;

            result = Sdl2Native.SDL_GL_GetAttribute(SDL_GLAttribute.StencilSize, &actualStencilSize);

            result = Sdl2Native.SDL_GL_SetSwapInterval(options.SyncToVerticalBlank ? 1 : 0);

            OpenGL.OpenGLPlatformInfo platformInfo = new OpenGL.OpenGLPlatformInfo(
                contextHandle,
                Sdl2Native.SDL_GL_GetProcAddress,
                context => Sdl2Native.SDL_GL_MakeCurrent(sdlHandle, context),
                () => Sdl2Native.SDL_GL_GetCurrentContext(),
                () => Sdl2Native.SDL_GL_MakeCurrent(new SDL_Window(IntPtr.Zero), IntPtr.Zero),
                Sdl2Native.SDL_GL_DeleteContext,
                () => Sdl2Native.SDL_GL_SwapWindow(sdlHandle),
                sync => Sdl2Native.SDL_GL_SetSwapInterval(sync ? 1 : 0));

            return(GraphicsDevice.CreateOpenGL(
                       options,
                       platformInfo,
                       (uint)window.Width,
                       (uint)window.Height));
        }
Exemple #9
0
        public static unsafe GraphicsDevice CreateVulkanGraphicsDevice(GraphicsDeviceOptions options, Sdl2Window window)
        {
            IntPtr        sdlHandle = window.SdlWindowHandle;
            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sysWmInfo);
            VkSurfaceSource surfaceSource = GetSurfaceSource(sysWmInfo);
            GraphicsDevice  gd            = GraphicsDevice.CreateVulkan(options, surfaceSource, (uint)window.Width, (uint)window.Height);

            return(gd);
        }
        public static unsafe RenderContext CreateVulkanRenderContext(ref RenderContextCreateInfo contextCI, Sdl2Window window)
        {
            IntPtr        sdlHandle = window.SdlWindowHandle;
            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sysWmInfo);
            VkSurfaceSource surfaceSource = GetSurfaceSource(sysWmInfo);
            VkRenderContext rc            = new VkRenderContext(surfaceSource, window.Width, window.Height);

            if (contextCI.DebugContext)
            {
                rc.EnableDebugCallback();
            }

            return(rc);
        }
        public static unsafe GraphicsDevice CreateOpenGLGraphicsDevice(Sdl2Window window, GraphicsDeviceOptions options)
        {
            Sdl2Native.SDL_ClearError();
            var           sdlHandle = window.SdlWindowHandle;
            SDL_SysWMinfo sdlSysWmInfo;

            Sdl2Native.SDL_GetVersion(&sdlSysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sdlSysWmInfo);

            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextFlags, options.Debug ? 3 : 2);
            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextProfileMask, 1);
            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextMajorVersion, 4);
            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextMinorVersion, 1);

            options.SwapchainDepthFormat = PixelFormat.D32_Float_S8_UInt;
            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.DepthSize, 32);
            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.StencilSize, 8);

            var contextHandle = Sdl2Native.SDL_GL_CreateContext(sdlHandle);
            var error         = Sdl2Native.SDL_GetError();

            if ((IntPtr)error != IntPtr.Zero)
            {
                var str = GetString(error);
                if (!string.IsNullOrEmpty(str))
                {
                    throw new VeldridException(string.Format("Unable to create OpenGL Context: \"{0}\". This may indicate that the system does not support the requested OpenGL profile, version, or Swapchain format.", str));
                }
            }
            int num1;

            Sdl2Native.SDL_GL_GetAttribute(SDL_GLAttribute.DepthSize, &num1);
            int num2;

            Sdl2Native.SDL_GL_GetAttribute(SDL_GLAttribute.StencilSize, &num2);
            Sdl2Native.SDL_GL_SetSwapInterval(options.SyncToVerticalBlank ? 1 : 0);
            var getProcAddress = new Func <string, IntPtr>(Sdl2Native.SDL_GL_GetProcAddress);
            var makeCurrent    = (Action <IntPtr>)(context => Sdl2Native.SDL_GL_MakeCurrent(sdlHandle, context));
            var deleteContext  = new Action <IntPtr>(Sdl2Native.SDL_GL_DeleteContext);
            var swapBuffers    = (Action)(() => Sdl2Native.SDL_GL_SwapWindow(sdlHandle));
            var platformInfo   = new OpenGLPlatformInfo(contextHandle, getProcAddress, makeCurrent, Sdl2Native.SDL_GL_GetCurrentContext, () => Sdl2Native.SDL_GL_MakeCurrent(new SDL_Window(IntPtr.Zero), IntPtr.Zero), deleteContext, swapBuffers, sync => Sdl2Native.SDL_GL_SetSwapInterval(sync ? 1 : 0));

            return(GraphicsDevice.CreateOpenGL(options, platformInfo, (uint)window.Width, (uint)window.Height));
        }