Exemplo n.º 1
0
            public nint?WindowGetWin32Window(WindowReg window)
            {
                if (!OperatingSystem.IsWindows())
                {
                    return(null);
                }

                var reg = (GlfwWindowReg)window;

                try
                {
                    return(GLFW.GetWin32Window(reg.GlfwWindow));
                }
                catch (EntryPointNotFoundException)
                {
                    return(null);
                }
            }
Exemplo n.º 2
0
    private static void CreateWindow(ImGuiViewport *viewport)
    {
        PlatformBackend backend      = GetPlatformBackend();
        ViewportData *  viewportData = ViewportData.Allocate();

        viewport->PlatformUserData = viewportData;

        GLFW.WindowHint(WindowHintBool.Visible, false);
        GLFW.WindowHint(WindowHintBool.Focused, false);
        GLFW.WindowHint(WindowHintBool.FocusOnShow, false);
        GLFW.WindowHint(WindowHintBool.Decorated, !viewport->Flags.HasFlag(ImGuiViewportFlags.NoDecoration));
        GLFW.WindowHint(WindowHintBool.Floating, viewport->Flags.HasFlag(ImGuiViewportFlags.TopMost));

        *viewportData = new ViewportData()
        {
            Window      = GLFW.CreateWindow((int)viewport->Size.X, (int)viewport->Size.Y, "No Title Yet", null, backend.Window),
            WindowOwned = true,
        };

        viewport->PlatformHandle = viewportData->Window;

        if (OperatingSystem.IsWindows())
        {
            viewport->PlatformHandleRaw = (void *)GLFW.GetWin32Window(viewportData->Window);
        }

        GLFW.SetWindowPos(viewportData->Window, (int)viewport->Pos.X, (int)viewport->Pos.Y);

        // Install GLFW callbacks for secondary viewports
        GlfwNative.glfwSetWindowFocusCallback(viewportData->Window, &WindowFocusCallback);
        GlfwNative.glfwSetCursorEnterCallback(viewportData->Window, &CursorEnterCallback);
        GlfwNative.glfwSetCursorPosCallback(viewportData->Window, &CursorPosCallback);
        GlfwNative.glfwSetMouseButtonCallback(viewportData->Window, &MouseButtonCallback);
        GlfwNative.glfwSetScrollCallback(viewportData->Window, &ScrollCallback);
        GlfwNative.glfwSetKeyCallback(viewportData->Window, &KeyCallback);
        GlfwNative.glfwSetCharCallback(viewportData->Window, &CharCallback);
        GlfwNative.glfwSetWindowCloseCallback(viewportData->Window, &WindowCloseCallback);
        GlfwNative.glfwSetWindowPosCallback(viewportData->Window, &WindowPosCallback);
        GlfwNative.glfwSetWindowSizeCallback(viewportData->Window, &WindowSizeCallback);
        GLFW.MakeContextCurrent(viewportData->Window);
        GLFW.SwapInterval(0);
    }
Exemplo n.º 3
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);
            }
Exemplo n.º 4
0
            private Window *CreateGlfwWindowForRenderer(
                GLContextSpec?spec,
                WindowCreateParameters parameters,
                Window *contextShare,
                Window *ownerWindow)
            {
                GLFW.WindowHint(WindowHintString.X11ClassName, "RobustToolbox");
                GLFW.WindowHint(WindowHintString.X11InstanceName, "RobustToolbox");
                GLFW.WindowHint(WindowHintBool.ScaleToMonitor, true);

                if (spec == null)
                {
                    // No OpenGL context requested.
                    GLFW.WindowHint(WindowHintClientApi.ClientApi, ClientApi.NoApi);
                }
                else
                {
                    var s = spec.Value;

#if DEBUG
                    GLFW.WindowHint(WindowHintBool.OpenGLDebugContext, true);
#endif

                    GLFW.WindowHint(WindowHintInt.ContextVersionMajor, s.Major);
                    GLFW.WindowHint(WindowHintInt.ContextVersionMinor, s.Minor);
                    GLFW.WindowHint(WindowHintBool.OpenGLForwardCompat, s.Profile != GLContextProfile.Compatibility);
                    GLFW.WindowHint(WindowHintBool.SrgbCapable, true);

                    switch (s.Profile)
                    {
                    case GLContextProfile.Compatibility:
                        GLFW.WindowHint(WindowHintOpenGlProfile.OpenGlProfile, OpenGlProfile.Any);
                        GLFW.WindowHint(WindowHintClientApi.ClientApi, ClientApi.OpenGlApi);
                        break;

                    case GLContextProfile.Core:
                        GLFW.WindowHint(WindowHintOpenGlProfile.OpenGlProfile, OpenGlProfile.Core);
                        GLFW.WindowHint(WindowHintClientApi.ClientApi, ClientApi.OpenGlApi);
                        break;

                    case GLContextProfile.Es:
                        GLFW.WindowHint(WindowHintOpenGlProfile.OpenGlProfile, OpenGlProfile.Any);
                        GLFW.WindowHint(WindowHintClientApi.ClientApi, ClientApi.OpenGlEsApi);
                        break;
                    }

                    GLFW.WindowHint(WindowHintContextApi.ContextCreationApi,
                                    s.CreationApi == GLContextCreationApi.Egl
                            ? ContextApi.EglContextApi
                            : ContextApi.NativeContextApi);

#if !FULL_RELEASE
                    if (s.CreationApi == GLContextCreationApi.Egl && !_eglLoaded && OperatingSystem.IsWindows())
                    {
                        // On non-published builds (so, development), GLFW can't find libEGL.dll
                        // because it'll be in runtimes/<rid>/native/ instead of next to the actual executable.
                        // We manually preload the library here so that GLFW will find it when it does its thing.
                        NativeLibrary.TryLoad(
                            "libEGL.dll",
                            typeof(Clyde).Assembly,
                            DllImportSearchPath.SafeDirectories,
                            out _);

                        _eglLoaded = true;
                    }
#endif
                }

                Monitor *monitor = null;
                if (parameters.Monitor != null &&
                    _winThreadMonitors.TryGetValue(parameters.Monitor.Id, out var monitorReg))
                {
                    monitor = monitorReg.Ptr;
                    var mode = GLFW.GetVideoMode(monitor);
                    // Set refresh rate to monitor's so that GLFW doesn't manually select one.
                    GLFW.WindowHint(WindowHintInt.RefreshRate, mode->RefreshRate);
                }
                else
                {
                    GLFW.WindowHint(WindowHintInt.RefreshRate, -1);
                }

                GLFW.WindowHint(WindowHintBool.Visible, false);

                GLFW.WindowHint(WindowHintInt.RedBits, 8);
                GLFW.WindowHint(WindowHintInt.GreenBits, 8);
                GLFW.WindowHint(WindowHintInt.BlueBits, 8);
                GLFW.WindowHint(WindowHintInt.AlphaBits, 8);
                GLFW.WindowHint(WindowHintInt.StencilBits, 8);

                var window = GLFW.CreateWindow(
                    parameters.Width, parameters.Height,
                    parameters.Title,
                    parameters.Fullscreen ? monitor : null,
                    contextShare);

                // Check if window failed to create.
                if (window == null)
                {
                    return(null);
                }

                if (parameters.Maximized)
                {
                    GLFW.GetMonitorPos(monitor, out var x, out var y);
                    GLFW.SetWindowPos(window, x, y);
                    GLFW.MaximizeWindow(window);
                }

                if ((parameters.Styles & OSWindowStyles.NoTitleOptions) != 0)
                {
                    if (OperatingSystem.IsWindows())
                    {
                        var hWnd = (HWND)GLFW.GetWin32Window(window);
                        DebugTools.Assert(hWnd != HWND.NULL);

                        Windows.SetWindowLongPtrW(
                            hWnd,
                            GWL.GWL_STYLE,
                            // Cast to long here to work around a bug in rider with nint bitwise operators.
                            (nint)((long)Windows.GetWindowLongPtrW(hWnd, GWL.GWL_STYLE) & ~WS.WS_SYSMENU));
                    }
                    else
                    {
                        _sawmill.Warning("OSWindowStyles.NoTitleOptions not implemented on this platform");
                    }
                }

                if (ownerWindow != null)
                {
                    if (OperatingSystem.IsWindows())
                    {
                        var hWnd      = (HWND)GLFW.GetWin32Window(window);
                        var ownerHWnd = (HWND)GLFW.GetWin32Window(ownerWindow);
                        DebugTools.Assert(hWnd != HWND.NULL);

                        Windows.SetWindowLongPtrW(
                            hWnd,
                            GWLP.GWLP_HWNDPARENT,
                            ownerHWnd);
                    }
                    else
                    {
                        _sawmill.Warning("owner windows not implemented on this platform");
                    }


                    if (parameters.StartupLocation == WindowStartupLocation.CenterOwner)
                    {
                        // TODO: Maybe include window frames in size calculations here?
                        // Figure out frame sizes of both windows.
                        GLFW.GetWindowPos(ownerWindow, out var ownerX, out var ownerY);
                        GLFW.GetWindowSize(ownerWindow, out var ownerW, out var ownerH);

                        // Re-fetch this in case DPI scaling is changing it I guess.
                        GLFW.GetWindowSize(window, out var thisW, out var thisH);

                        GLFW.SetWindowPos(window, ownerX + (ownerW - thisW) / 2, ownerY + (ownerH - thisH) / 2);
                    }
                }

                if (parameters.Visible)
                {
                    GLFW.ShowWindow(window);
                }

                return(window);
            }
Exemplo n.º 5
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();
        }