Esempio n. 1
0
    private static void DestroyWindow(ImGuiViewport *viewport)
    {
        ViewportData *viewportData = (ViewportData *)viewport->PlatformUserData;

        if (viewportData is not null)
        {
            if (viewportData->WindowOwned)
            {
                PlatformBackend backend = GetPlatformBackend();

                // Release any keys that were pressed in the window being destroyed and are still held down,
                // because we will not receive any release events after window is destroyed.
                for (int i = 0; i < backend.KeyOwnerWindows.Length; i++)
                {
                    if (backend.KeyOwnerWindows[i] == viewportData->Window)
                    {
                        KeyCallbackManaged(viewportData->Window, (Keys)i, 0, InputAction.Release, 0);
                    }                                                                                 // Later params are only used for main viewport, on which this function is never called.
                }

                GLFW.DestroyWindow(viewportData->Window);
            }

            viewportData->Window = null;
            ViewportData.Free(viewportData);
        }

        viewport->PlatformUserData = viewport->PlatformHandle = null;
    }
Esempio n. 2
0
 public void Dispose()
 {
     imageAvailableSemaphore.Dispose();
     renderFinishedSemaphore.Dispose();
     indexBufferMemory.Dispose();
     indexBuffer.Dispose();
     vertexBufferMemory.Dispose();
     vertexBuffer.Dispose();
     commandPool.Dispose();
     foreach (var fb in swapchainFramebuffers)
     {
         fb.Dispose();
     }
     pipeline.Dispose();
     pipelineLayout.Dispose();
     renderPass.Dispose();
     foreach (var iv in swapchainImageViews)
     {
         iv.Dispose();
     }
     swapchain.Dispose();
     device.Dispose();
     surface.Dispose();
     debugCallbacks.Dispose();
     instance.Dispose();
     GLFW.DestroyWindow(window);
     GLFW.Terminate();
 }
Esempio n. 3
0
        public static void ImGui_ImplGlfw_DestroyWindow(ImGuiViewportPtr viewport)
        {
            var data = GetViewportData(viewport);

            if (data.WindowOwned)
            {
                GLFW.DestroyWindow((Window *)data.Window);
            }

            _data.Remove(viewport.PlatformUserData);

            viewport.PlatformUserData = viewport.PlatformHandle = IntPtr.Zero;
        }
Esempio n. 4
0
            private static void WinThreadWinDestroy(CmdWinDestroy cmd)
            {
                var window = (Window *)cmd.Window;

                if (OperatingSystem.IsWindows() && cmd.hadOwner)
                {
                    // On Windows, closing the child window causes the owner to be minimized, apparently.
                    // Clear owner on close to avoid this.

                    var hWnd = (HWND)GLFW.GetWin32Window(window);
                    DebugTools.Assert(hWnd != HWND.NULL);

                    Windows.SetWindowLongPtrW(
                        hWnd,
                        GWLP.GWLP_HWNDPARENT,
                        0);
                }

                GLFW.DestroyWindow((Window *)cmd.Window);
            }
Esempio n. 5
0
 public virtual void Dispose()
 {
     GLFW.DestroyWindow(_window);
 }
Esempio n. 6
0
 private void WinThreadWinDestroy(CmdWinDestroy cmd)
 {
     GLFW.DestroyWindow((Window *)cmd.Window);
 }
Esempio n. 7
0
        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();
        }