Exemple #1
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 #2
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 #3
0
        static unsafe void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            IntPtr          window     = Sdl2Native.SDL_CreateWindow("Here is a pretty wacky title.", 50, 50, 1280, 720, SDL_WindowFlags.Shown | SDL_WindowFlags.Resizable | SDL_WindowFlags.OpenGL);
            var             windowInfo = OpenTK.Platform.Utilities.CreateSdl2WindowInfo(window);
            GraphicsContext gc         = new GraphicsContext(GraphicsMode.Default, windowInfo);

            gc.LoadAll();
            gc.MakeCurrent(windowInfo);

            while (true)
            {
                GL.ClearColor(0f, 0f, .75f, 1f);
                GL.Viewport(0, 0, 1280, 720);
                GL.Clear(ClearBufferMask.ColorBufferBit);
                Sdl2Native.SDL_PumpEvents();
                SDL_Event ev;
                while (Sdl2Native.SDL_PollEvent(&ev) != 0)
                {
                    if (ev.type == SDL_EventType.WindowEvent)
                    {
                        SDL_WindowEvent windowEvent = Unsafe.Read <SDL_WindowEvent>(&ev);
                        Console.WriteLine("Window event: " + windowEvent.@event);
                    }
                    else if (ev.type == SDL_EventType.MouseMotion)
                    {
                        SDL_MouseMotionEvent mme = Unsafe.Read <SDL_MouseMotionEvent>(&ev);
                        Console.WriteLine($"X: {mme.x}, Y: {mme.y}, State: {mme.state}");
                    }
                    else if (ev.type == SDL_EventType.Quit)
                    {
                        Sdl2Native.SDL_DestroyWindow(window);
                        return;
                    }
                    else
                    {
                        Console.WriteLine("Event: " + ev.type);
                    }
                }

                Sdl2Native.SDL_GL_SwapWindow(window);
                //gc.SwapBuffers();
            }
        }
Exemple #4
0
        private static OpenGLESRenderContext CreateDefaultOpenGLESRenderContext(Sdl2Window window)
        {
            bool debugContext = false;

            debugContext |= s_allowDebugContexts;

            if (debugContext)
            {
                Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextFlags, (int)SDL_GLContextFlag.Debug);
            }

            IntPtr sdlHandle = window.SdlWindowHandle;

            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextProfileMask, (int)SDL_GLProfile.ES);
            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextMajorVersion, 3);
            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextMinorVersion, 0);
            IntPtr contextHandle = Sdl2Native.SDL_GL_CreateContext(sdlHandle);

            Sdl2Native.SDL_GL_MakeCurrent(sdlHandle, contextHandle);

            if (contextHandle == IntPtr.Zero)
            {
                unsafe
                {
                    byte * error       = Sdl2Native.SDL_GetError();
                    string errorString = Utilities.GetString(error);
                    throw new InvalidOperationException("Unable to create GL Context: " + errorString);
                }
            }

            Sdl2Native.SDL_GL_MakeCurrent(sdlHandle, contextHandle);
            OpenGLPlatformContextInfo ci = new OpenGLPlatformContextInfo(
                contextHandle,
                Sdl2Native.SDL_GL_GetProcAddress,
                Sdl2Native.SDL_GL_GetCurrentContext,
                () => Sdl2Native.SDL_GL_SwapWindow(sdlHandle));
            var rc = new OpenGLESRenderContext(window, ci);

            if (debugContext)
            {
                rc.EnableDebugCallback(OpenTK.Graphics.ES30.DebugSeverity.DebugSeverityNotification);
            }
            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));
        }
Exemple #6
0
        private static OpenGLRenderContext CreateDefaultOpenGLRenderContext(Sdl2Window window)
        {
            bool debugContext = false;

#if DEBUG
            debugContext = Preferences.Instance.AllowOpenGLDebugContexts;
#endif
            IntPtr sdlHandle = window.SdlWindowHandle;
            if (debugContext)
            {
                Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextFlags, (int)SDL_GLContextFlag.Debug);
            }

            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextProfileMask, (int)SDL_GLProfile.Core);
            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextMajorVersion, 4);
            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextMinorVersion, 0);

            IntPtr contextHandle = Sdl2Native.SDL_GL_CreateContext(sdlHandle);
            if (contextHandle == IntPtr.Zero)
            {
                unsafe
                {
                    byte * error       = Sdl2Native.SDL_GetError();
                    string errorString = Utilities.GetString(error);
                    throw new InvalidOperationException("Unable to create GL Context: " + errorString);
                }
            }

            Sdl2Native.SDL_GL_MakeCurrent(sdlHandle, contextHandle);
            OpenGLPlatformContextInfo ci = new OpenGLPlatformContextInfo(
                contextHandle,
                Sdl2Native.SDL_GL_GetProcAddress,
                Sdl2Native.SDL_GL_GetCurrentContext,
                () => Sdl2Native.SDL_GL_SwapWindow(sdlHandle));
            var rc = new OpenGLRenderContext(window, ci);
            if (debugContext)
            {
                // Slows things down significantly -- Only use when debugging something specific.
                rc.EnableDebugCallback(OpenTK.Graphics.OpenGL.DebugSeverity.DebugSeverityNotification);
            }
            return(rc);
        }
Exemple #7
0
 private void DrawLoop()
 {
     PoolTouhou.Logger.Log("开始渲染线程循环");
     try {
         long last = 0;
         while (window.Exists && running)
         {
             long   now   = Watch.ElapsedTicks;
             double delta = Stopwatch.Frequency / (double)(now - last);
             OpenGLNative.glClearColor(0, 1, 1, 0.5f);
             OpenGLNative.glClear(ClearBufferMask.ColorBufferBit);
             PoolTouhou.GameState.Draw(delta);
             Sdl2Native.SDL_GL_SwapWindow(window.SdlWindowHandle);
             GlUtil.CheckGlError();
             last = now;
         }
     } catch (Exception e) {
         running = false;
         PoolTouhou.Logger.Log(e.Message + Environment.NewLine + e.StackTrace);
     }
 }
Exemple #8
0
        //public static OpenGLESRenderContext CreateDefaultOpenGLESRenderContext(ref RenderContextCreateInfo contextCI, Sdl2Window window)
        //{
        //    if (contextCI.DebugContext)
        //    {
        //        Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextFlags, (int)SDL_GLContextFlag.Debug);
        //    }

        //    IntPtr sdlHandle = window.SdlWindowHandle;
        //    Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextProfileMask, (int)SDL_GLProfile.ES);
        //    Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextMajorVersion, 3);
        //    Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextMinorVersion, 0);
        //    IntPtr contextHandle = Sdl2Native.SDL_GL_CreateContext(sdlHandle);
        //    Sdl2Native.SDL_GL_MakeCurrent(sdlHandle, contextHandle);

        //    if (contextHandle == IntPtr.Zero)
        //    {
        //        unsafe
        //        {
        //            byte* error = Sdl2Native.SDL_GetError();
        //            string errorString = Utilities.GetString(error);
        //            throw new VeldridException("Unable to create GL Context: " + errorString);
        //        }
        //    }

        //    OpenGLPlatformContextInfo ci = new OpenGLPlatformContextInfo(
        //        contextHandle,
        //        Sdl2Native.SDL_GL_GetProcAddress,
        //        Sdl2Native.SDL_GL_GetCurrentContext,
        //        () => Sdl2Native.SDL_GL_SwapWindow(sdlHandle));
        //    OpenGLESRenderContext rc = new OpenGLESRenderContext(window, ci);
        //    if (contextCI.DebugContext)
        //    {
        //        rc.EnableDebugCallback(OpenTK.Graphics.ES30.DebugSeverity.DebugSeverityLow);
        //    }
        //    return rc;
        //}

        public static GraphicsDevice CreateDefaultOpenGLRenderContext(ref GraphicsDeviceCreateInfo gdCI, Sdl2Window window)
        {
            IntPtr sdlHandle = window.SdlWindowHandle;

            if (gdCI.DebugDevice)
            {
                Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextFlags, (int)SDL_GLContextFlag.Debug);
            }

            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextProfileMask, (int)SDL_GLProfile.Core);
            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextMajorVersion, 4);
            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextMinorVersion, 0);

            IntPtr contextHandle = Sdl2Native.SDL_GL_CreateContext(sdlHandle);

            if (contextHandle == IntPtr.Zero)
            {
                unsafe
                {
                    byte * error       = Sdl2Native.SDL_GetError();
                    string errorString = GetString(error);
                    throw new VeldridException("Unable to create GL Context: " + errorString);
                }
            }

            int result = Sdl2Native.SDL_GL_SetSwapInterval(0);

            OpenGLPlatformInfo platformInfo = new OpenGLPlatformInfo(
                contextHandle,
                Sdl2Native.SDL_GL_GetProcAddress,
                context => Sdl2Native.SDL_GL_MakeCurrent(sdlHandle, context),
                Sdl2Native.SDL_GL_DeleteContext,
                () => Sdl2Native.SDL_GL_SwapWindow(sdlHandle));

            return(Hacks.CreateOpenGL(
                       platformInfo,
                       (uint)window.Width,
                       (uint)window.Height,
                       gdCI.DebugDevice));
        }
Exemple #9
0
        public static GraphicsDevice CreateDefaultOpenGlGraphicsDevice(Sdl2Window window)
        {
            SDL_SysWMinfo info;

            var sdlHandle     = window.SdlWindowHandle;
            var contextHandle = Sdl2Native.SDL_GL_CreateContext(sdlHandle);
            var platformInfo  = new OpenGLPlatformInfo(
                contextHandle,
                Sdl2Native.SDL_GL_GetProcAddress,
                context => Sdl2Native.SDL_GL_MakeCurrent(sdlHandle, context),
                Sdl2Native.SDL_GL_GetCurrentContext,
                () => { },
                Sdl2Native.SDL_GL_DeleteContext,
                () => Sdl2Native.SDL_GL_SwapWindow(sdlHandle),
                sync => Sdl2Native.SDL_GL_SetSwapInterval(sync ? 1 : 0)
                );

            return(GraphicsDevice.CreateOpenGL(
                       new GraphicsDeviceOptions(true),
                       platformInfo,
                       (uint)window.Width,
                       (uint)window.Height
                       ));
        }
Exemple #10
0
        public static GraphicsDevice CreateDefaultOpenGLGraphicsDevice(GraphicsDeviceOptions options, Sdl2Window window)
        {
            IntPtr sdlHandle = window.SdlWindowHandle;

            if (options.Debug)
            {
                Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextFlags, (int)SDL_GLContextFlag.Debug);
            }

            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextProfileMask, (int)SDL_GLProfile.Core);
            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextMajorVersion, 4);
            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.ContextMinorVersion, 0);

            int depthBits = 0;

            if (options.SwapchainDepthFormat.HasValue)
            {
                switch (options.SwapchainDepthFormat)
                {
                case PixelFormat.R16_UNorm:
                    depthBits = 16;
                    break;

                case PixelFormat.R32_Float:
                    depthBits = 32;
                    break;

                default:
                    throw new VeldridException("Invalid depth format: " + options.SwapchainDepthFormat.Value);
                }
            }

            Sdl2Native.SDL_GL_SetAttribute(SDL_GLAttribute.DepthSize, depthBits);

            IntPtr contextHandle = Sdl2Native.SDL_GL_CreateContext(sdlHandle);

            if (contextHandle == IntPtr.Zero)
            {
                unsafe
                {
                    byte * error       = Sdl2Native.SDL_GetError();
                    string errorString = GetString(error);
                    throw new VeldridException("Unable to create GL Context: " + errorString);
                }
            }

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

            OpenGLPlatformInfo platformInfo = new 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));
        }