示例#1
0
        public static unsafe GraphicsDevice CreateDefaultOpenGLGraphicsDevice(
            GraphicsDeviceOptions options,
            Sdl2Window window,
            GraphicsBackend backend)
        {
            Sdl2Native.SDL_ClearError();
            IntPtr sdlHandle = window.SdlWindowHandle;

            SDL_SysWMinfo sysWmInfo;

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

            SetSDLGLContextAttributes(options, backend);

            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));
        }
示例#2
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));
        }