public static unsafe void EnsureContext() { Debug.Assert(GLFW.GetCurrentContext() != null); }
private void InitGLContext() { // Initialize the OpenTK 3 GL context with GLFW. _graphicsContext = new GraphicsContext(new ContextHandle((IntPtr)_glfwWindow), GLFW.GetProcAddress, () => new ContextHandle((IntPtr)GLFW.GetCurrentContext())); }
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(); }