private static GraphicsDevice CreateOpenGLGraphicsDevice(ILogger logger, Window window, GraphicsDeviceOptions options) { window.GetSize(out var width, out var height); var glContextHandle = SDL.SDL_GL_CreateContext(window.WindowHandle); if (glContextHandle == IntPtr.Zero) { throw new InvalidOperationException("Failed to create SDL Window"); } if (0 != SDL.SDL_GL_GetAttribute(SDL.SDL_GLattr.SDL_GL_RED_SIZE, out var r)) { r = 0; logger.Information("Failed to get GL RED size ({0})", SDL.SDL_GetError()); } if (0 != SDL.SDL_GL_GetAttribute(SDL.SDL_GLattr.SDL_GL_GREEN_SIZE, out var g)) { g = 0; logger.Information("Failed to get GL GREEN size ({0})", SDL.SDL_GetError()); } if (0 != SDL.SDL_GL_GetAttribute(SDL.SDL_GLattr.SDL_GL_BLUE_SIZE, out var b)) { b = 0; logger.Information("Failed to get GL BLUE size ({0})", SDL.SDL_GetError()); } if (0 != SDL.SDL_GL_GetAttribute(SDL.SDL_GLattr.SDL_GL_ALPHA_SIZE, out var a)) { a = 0; logger.Information("Failed to get GL ALPHA size ({0})", SDL.SDL_GetError()); } if (0 != SDL.SDL_GL_GetAttribute(SDL.SDL_GLattr.SDL_GL_DEPTH_SIZE, out var depth)) { depth = 0; logger.Information("Failed to get GL DEPTH size ({0})", SDL.SDL_GetError()); } logger.Information($"GL_SIZES: r:{r} g:{g} b:{b} a:{a} depth:{depth}"); if (r <= 4 || g <= 4 || b <= 4 || depth <= 15 /*|| gl_renderer && Q_strstr(gl_renderer, "GDI Generic")*/) { throw new InvalidOperationException("Failed to create SDL Window, unsupported video mode. A 16-bit color depth desktop is required and a supported GL driver"); } var platformInfo = new OpenGLPlatformInfo( glContextHandle, SDL.SDL_GL_GetProcAddress, context => SDL.SDL_GL_MakeCurrent(window.WindowHandle, context), SDL.SDL_GL_GetCurrentContext, () => SDL.SDL_GL_MakeCurrent(IntPtr.Zero, IntPtr.Zero), SDL.SDL_GL_DeleteContext, () => SDL.SDL_GL_SwapWindow(window.WindowHandle), sync => SDL.SDL_GL_SetSwapInterval(sync ? 1 : 0)); return(GraphicsDevice.CreateOpenGL(options, platformInfo, (uint)width, (uint)height)); }
public static GraphicsDevice CreateOpenGL( OpenGLPlatformInfo platformInfo, uint width, uint height, bool debugDevice) { return(new OpenGLGraphicsDevice(platformInfo, width, height, debugDevice)); }
private void InitializeGraphicsBackend() { switch (Backend) { case GraphicsBackend.Metal: GraphicsDevice = GraphicsDevice.CreateMetal(GraphicsDeviceOptions); break; case GraphicsBackend.Vulkan: GraphicsDevice = GraphicsDevice.CreateVulkan(GraphicsDeviceOptions); break; case GraphicsBackend.Direct3D11: GraphicsDevice = GraphicsDevice.CreateD3D11(GraphicsDeviceOptions); break; case GraphicsBackend.OpenGL: Handler.UpdateWindowInfo(OpenTKGraphicsMode); var glInfo = new OpenGLPlatformInfo( VeldridGL.GetGLContextHandle(), VeldridGL.GetProcAddress, (c) => OpenTKGraphicsContext.MakeCurrent(Handler.WindowInfo), VeldridGL.GetCurrentContext, VeldridGL.ClearCurrentContext, VeldridGL.DeleteContext, VeldridGL.SwapBuffers, VeldridGL.SetVSync, VeldridGL.SetSwapchainFramebuffer, Handler.ResizeSwapchain); GraphicsDevice = GraphicsDevice.CreateOpenGL( GraphicsDeviceOptions, glInfo, (uint)RenderWidth, (uint)RenderHeight); break; default: string message; if (!Enum.IsDefined(typeof(GraphicsBackend), Backend)) { message = "Unrecognized backend!"; } else { message = "Specified backend not supported on this platform!"; } throw new ArgumentException(message); } Swapchain = Handler.CreateSwapchain(); OnVeldridInitialized(EventArgs.Empty); }
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)); }
//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)); }
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 )); }
private void InitVeldrid() { var options = new GraphicsDeviceOptions { #if DEBUG Debug = true, #endif HasMainSwapchain = true, SyncToVerticalBlank = _vsync, PreferStandardClipSpaceYDirection = true, SwapchainSrgbFormat = true }; GLFW.GetFramebufferSize(_window.WindowPtr, out var w, out var h); var hwnd = GLFW.GetWin32Window(_window.WindowPtr); var hinstance = GetModuleHandleA(null); switch (_vdRenderer) { case VeldridRenderer.Vulkan: _vdGfxDevice = GraphicsDevice.CreateVulkan( options, VkSurfaceSource.CreateWin32((nint)hinstance, hwnd), (uint)w, (uint)h); break; case VeldridRenderer.D3D11: _vdGfxDevice = GraphicsDevice.CreateD3D11(options, hwnd, (uint)w, (uint)h); break; case VeldridRenderer.OpenGL: { var platInfo = new OpenGLPlatformInfo( (nint)_window.WindowPtr, GLFW.GetProcAddress, ptr => GLFW.MakeContextCurrent((Window *)ptr), () => (nint)GLFW.GetCurrentContext(), () => GLFW.MakeContextCurrent(null), ptr => GLFW.DestroyWindow((Window *)ptr), () => GLFW.SwapBuffers(_window.WindowPtr), vsync => GLFW.SwapInterval(vsync ? 1 : 0)); _vdGfxDevice = GraphicsDevice.CreateOpenGL(options, platInfo, (uint)w, (uint)h); break; } } var factory = _vdGfxDevice.ResourceFactory; _vdCommandList = factory.CreateCommandList(); _vdCommandList.Name = "Honk"; var vtxLayout = new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate), new VertexElementDescription("UV", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate), new VertexElementDescription("Color", VertexElementFormat.Byte4_Norm, VertexElementSemantic.TextureCoordinate)); var vtxShaderDesc = new ShaderDescription( ShaderStages.Vertex, Encoding.UTF8.GetBytes(VDVertexShader), "main"); var fragShaderDesc = new ShaderDescription( ShaderStages.Fragment, Encoding.UTF8.GetBytes(VDFragmentShader), "main"); _vdShaders = factory.CreateFromSpirv(vtxShaderDesc, fragShaderDesc); _vdShaders[0].Name = "VertexShader"; _vdShaders[1].Name = "FragmentShader"; var layoutTexture = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription( "Texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription( "TextureSampler", ResourceKind.Sampler, ShaderStages.Fragment))); layoutTexture.Name = "LayoutTexture"; var layoutProjMatrix = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription( "ProjMtx", ResourceKind.UniformBuffer, ShaderStages.Vertex))); layoutProjMatrix.Name = "LayoutProjMatrix"; var pipelineDesc = new GraphicsPipelineDescription( new BlendStateDescription( RgbaFloat.White, new BlendAttachmentDescription( true, BlendFactor.SourceAlpha, BlendFactor.InverseSourceAlpha, BlendFunction.Add, BlendFactor.One, BlendFactor.InverseSourceAlpha, BlendFunction.Add) ), DepthStencilStateDescription.Disabled, new RasterizerStateDescription( FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, depthClipEnabled: false, scissorTestEnabled: true), PrimitiveTopology.TriangleList, new ShaderSetDescription(new[] { vtxLayout }, _vdShaders), new[] { layoutProjMatrix, layoutTexture }, new OutputDescription( null, new OutputAttachmentDescription(PixelFormat.B8_G8_R8_A8_UNorm_SRgb)) ); _vdPipeline = factory.CreateGraphicsPipeline(pipelineDesc); _vdPipeline.Name = "MainPipeline"; _vdProjMatrixUniformBuffer = factory.CreateBuffer(new BufferDescription( (uint)sizeof(Matrix4x4), BufferUsage.Dynamic | BufferUsage.UniformBuffer)); _vdProjMatrixUniformBuffer.Name = "_vdProjMatrixUniformBuffer"; _vdSetProjMatrix = factory.CreateResourceSet(new ResourceSetDescription( layoutProjMatrix, _vdProjMatrixUniformBuffer)); _vdSetProjMatrix.Name = "_vdSetProjMatrix"; var io = ImGui.GetIO(); io.Fonts.GetTexDataAsRGBA32(out byte *pixels, out var width, out var height, out _); _vdTexture = factory.CreateTexture(TextureDescription.Texture2D( (uint)width, (uint)height, mipLevels: 1, arrayLayers: 1, PixelFormat.R8_G8_B8_A8_UNorm_SRgb, TextureUsage.Sampled)); _vdTexture.Name = "MainTexture"; _vdSampler = factory.CreateSampler(SamplerDescription.Linear); _vdSampler.Name = "MainSampler"; _vdGfxDevice.UpdateTexture( _vdTexture, (IntPtr)pixels, (uint)(width * height * 4), x: 0, y: 0, z: 0, (uint)width, (uint)height, depth: 1, mipLevel: 0, arrayLayer: 0); _vdSetTexture = factory.CreateResourceSet(new ResourceSetDescription( layoutTexture, _vdTexture, _vdSampler)); _vdSetTexture.Name = "SetTexture"; io.Fonts.SetTexID((nint)0); io.Fonts.ClearTexData(); _vdGfxDevice.ResizeMainWindow((uint)w, (uint)h); _vdGfxDevice.SwapBuffers(); }
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)); }