Ejemplo n.º 1
0
        private void OnGlfwWindowSize(Window *window, int width, int height)
        {
            try
            {
                var oldSize = _framebufferSize;
                GLFW.GetFramebufferSize(window, out var fbW, out var fbH);
                _framebufferSize = (fbW, fbH);
                _windowSize      = (width, height);

                if (fbW == 0 || fbH == 0 || width == 0 || height == 0)
                {
                    return;
                }

                _pixelRatio = _framebufferSize / _windowSize;

                GL.Viewport(0, 0, fbW, fbH);
                if (fbW != 0 && fbH != 0)
                {
                    RegenerateLightingRenderTargets();
                }

                OnWindowResized?.Invoke(new WindowResizedEventArgs(oldSize, _framebufferSize));
            }
            catch (Exception e)
            {
                CatchCallbackException(e);
            }
        }
Ejemplo n.º 2
0
        private void InitWindow()
        {
            GLFW.WindowHint(WindowHintBool.SrgbCapable, true);
            GLFW.WindowHint(WindowHintInt.ContextVersionMajor, MinimumOpenGLVersion.Major);
            GLFW.WindowHint(WindowHintInt.ContextVersionMinor, MinimumOpenGLVersion.Minor);
            GLFW.WindowHint(WindowHintBool.OpenGLForwardCompat, true);
            GLFW.WindowHint(WindowHintOpenGlProfile.OpenGlProfile, OpenGlProfile.Core);
#if DEBUG
            GLFW.WindowHint(WindowHintBool.OpenGLDebugContext, true);
#endif
            GLFW.WindowHint(WindowHintString.X11ClassName, "SS14");
            GLFW.WindowHint(WindowHintString.X11InstanceName, "SS14");

            var width  = _configurationManager.GetCVar <int>("display.width");
            var height = _configurationManager.GetCVar <int>("display.height");

            Monitor *monitor = null;

            if (WindowMode == WindowMode.Fullscreen)
            {
                monitor = GLFW.GetPrimaryMonitor();
                var mode = GLFW.GetVideoMode(monitor);
                width  = mode->Width;
                height = mode->Height;
            }

            _glfwWindow = GLFW.CreateWindow(width, height, string.Empty, monitor, null);

            LoadWindowIcon();

            GLFW.SetCharCallback(_glfwWindow, _charCallback);
            GLFW.SetKeyCallback(_glfwWindow, _keyCallback);
            GLFW.SetWindowCloseCallback(_glfwWindow, _windowCloseCallback);
            GLFW.SetCursorPosCallback(_glfwWindow, _cursorPosCallback);
            GLFW.SetWindowSizeCallback(_glfwWindow, _windowSizeCallback);
            GLFW.SetScrollCallback(_glfwWindow, _scrollCallback);
            GLFW.SetMouseButtonCallback(_glfwWindow, _mouseButtonCallback);
            GLFW.SetWindowContentScaleCallback(_glfwWindow, _windowContentScaleCallback);

            GLFW.MakeContextCurrent(_glfwWindow);

            VSyncChanged();

            GLFW.GetFramebufferSize(_glfwWindow, out var fbW, out var fbH);
            _screenSize = (fbW, fbH);

            GLFW.GetWindowContentScale(_glfwWindow, out var scaleX, out var scaleY);
            _windowScale = (scaleX, scaleY);

            InitGLContext();

            // Initializing OTK 3 seems to mess with the current context, so ensure it's still set.
            // This took me f*****g *forever* to debug because this manifested differently on nvidia drivers vs intel mesa.
            // So I thought it was a calling convention issue with the calli OpenTK emits.
            // Because, in my tests, I had InitGLContext() AFTER the test with a delegate-based invoke of the proc.
            GLFW.MakeContextCurrent(_glfwWindow);

            InitOpenGL();
        }
Ejemplo n.º 3
0
        private static void setupViewport(IntPtr window)
        {
            int width  = 0,
                height = 0;

            GLFW.GetFramebufferSize(window, ref width, ref height);
            OpenGL.Viewport(0, 0, width, height);
        }
Ejemplo n.º 4
0
        private void OnGlfwWindowSize(Window *window, int width, int height)
        {
            var oldSize = _screenSize;

            GLFW.GetFramebufferSize(window, out var fbW, out var fbH);
            _screenSize = (fbW, fbH);

            GL.Viewport(0, 0, fbW, fbH);
            if (fbW != 0 && fbH != 0)
            {
                _regenerateLightRenderTarget();
            }

            OnWindowResized?.Invoke(new WindowResizedEventArgs(oldSize, _screenSize));
        }
Ejemplo n.º 5
0
            private GlfwWindowReg WinThreadSetupWindow(Window *window)
            {
                var reg = new GlfwWindowReg
                {
                    GlfwWindow = window,
                    Id         = new WindowId(_nextWindowId++)
                };
                var handle = new WindowHandle(_clyde, reg);

                reg.Handle = handle;

                LoadWindowIcon(window);

                GLFW.SetCharCallback(window, _charCallback);
                GLFW.SetKeyCallback(window, _keyCallback);
                GLFW.SetWindowCloseCallback(window, _windowCloseCallback);
                GLFW.SetCursorPosCallback(window, _cursorPosCallback);
                GLFW.SetCursorEnterCallback(window, _cursorEnterCallback);
                GLFW.SetWindowSizeCallback(window, _windowSizeCallback);
                GLFW.SetWindowPosCallback(window, _windowPosCallback);
                GLFW.SetScrollCallback(window, _scrollCallback);
                GLFW.SetMouseButtonCallback(window, _mouseButtonCallback);
                GLFW.SetWindowContentScaleCallback(window, _windowContentScaleCallback);
                GLFW.SetWindowIconifyCallback(window, _windowIconifyCallback);
                GLFW.SetWindowFocusCallback(window, _windowFocusCallback);

                GLFW.GetFramebufferSize(window, out var fbW, out var fbH);
                reg.FramebufferSize = (fbW, fbH);

                GLFW.GetWindowContentScale(window, out var scaleX, out var scaleY);
                reg.WindowScale = (scaleX, scaleY);

                GLFW.GetWindowSize(window, out var w, out var h);
                reg.PrevWindowSize = reg.WindowSize = (w, h);

                GLFW.GetWindowPos(window, out var x, out var y);
                reg.PrevWindowPos = (x, y);

                reg.PixelRatio = reg.FramebufferSize / reg.WindowSize;

                return(reg);
            }
Ejemplo n.º 6
0
        private void RenderVeldrid()
        {
            GLFW.GetFramebufferSize(_window.WindowPtr, out var fbW, out var fbH);

            if (_vdLastWidth != fbW && _vdLastHeight != fbH)
            {
                _vdGfxDevice.ResizeMainWindow((uint)fbW, (uint)fbH);
                _vdLastWidth  = fbW;
                _vdLastHeight = fbH;
            }

            _vdCommandList.Begin();
            _vdCommandList.SetFramebuffer(_vdGfxDevice.SwapchainFramebuffer);

            _vdCommandList.SetViewport(0, new Viewport(0, 0, fbW, fbH, 0, 1));
            _vdCommandList.ClearColorTarget(0, RgbaFloat.Black);

            var factory = _vdGfxDevice.ResourceFactory;

            var drawData = ImGui.GetDrawData();

            ref var fencedData = ref GetFreeFencedData();
Ejemplo n.º 7
0
        private void RenderOpenGL()
        {
            GLFW.GetFramebufferSize(_window.WindowPtr, out var fbW, out var fbH);
            GL.Viewport(0, 0, fbW, fbH);
            GL.Disable(EnableCap.ScissorTest);
            GL.ClearColor(0, 0, 0, 1);
            GL.Clear(ClearBufferMask.ColorBufferBit);
            GL.Enable(EnableCap.ScissorTest);
            GL.Enable(EnableCap.Texture2D);

            var drawData = ImGui.GetDrawData();

            var l = drawData.DisplayPos.X;
            var r = drawData.DisplayPos.X + drawData.DisplaySize.X;
            var t = drawData.DisplayPos.Y;
            var b = drawData.DisplayPos.Y + drawData.DisplaySize.Y;

            var matrix = Matrix4x4.CreateOrthographicOffCenter(l, r, b, t, -1, 1);

            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix((float *)&matrix);

            var clipOff   = drawData.DisplayPos;
            var clipScale = drawData.FramebufferScale;

            GL.EnableClientState(ArrayCap.VertexArray);
            GL.EnableClientState(ArrayCap.TextureCoordArray);
            GL.EnableClientState(ArrayCap.ColorArray);

            for (var n = 0; n < drawData.CmdListsCount; n++)
            {
                var drawList = drawData.CmdListsRange[n];

                for (var cmdI = 0; cmdI < drawList.CmdBuffer.Size; cmdI++)
                {
                    var cmd = drawList.CmdBuffer[cmdI];

                    GL.BindTexture(TextureTarget.Texture2D, (uint)cmd.TextureId);

                    Vector4 clipRect = default;
                    clipRect.X = (cmd.ClipRect.X - clipOff.X) * clipScale.X;
                    clipRect.Y = (cmd.ClipRect.Y - clipOff.Y) * clipScale.Y;
                    clipRect.Z = (cmd.ClipRect.Z - clipOff.X) * clipScale.X;
                    clipRect.W = (cmd.ClipRect.W - clipOff.Y) * clipScale.Y;

                    GL.Scissor((int)clipRect.X, (int)(fbH - clipRect.W), (int)(clipRect.Z - clipRect.X),
                               (int)(clipRect.W - clipRect.Y));

                    IntPtr adjustedVB = drawList.VtxBuffer.Data + (nint)(sizeof(ImDrawVert) * cmd.VtxOffset);

                    GL.VertexPointer(2, VertexPointerType.Float, sizeof(ImDrawVert), adjustedVB);
                    GL.TexCoordPointer(2, TexCoordPointerType.Float, sizeof(ImDrawVert), adjustedVB + 8);
                    GL.ColorPointer(4, ColorPointerType.UnsignedByte, sizeof(ImDrawVert), adjustedVB + 16);

                    GL.DrawElements(PrimitiveType.Triangles, (int)cmd.ElemCount,
                                    DrawElementsType.UnsignedShort,
                                    drawList.IdxBuffer.Data + (nint)(cmd.IdxOffset * 2));
                }
            }

            _window.SwapBuffers();
        }
Ejemplo n.º 8
0
 private void OnGlfwWindowSize(Window *window, int width, int height)
 {
     GLFW.GetFramebufferSize(window, out var fbW, out var fbH);
     SendEvent(new EventWindowSize((nint)window, width, height, fbW, fbH));
 }
Ejemplo n.º 9
0
        static void Main()
        {
            Console.WriteLine($"Working directory: '{Path.GetFullPath(".")}'.");

            PrepareGLFW();
            PrepareOpenGL();
            PrepareOpenAL();

            //double timePrev = 0d;

            CheckGLErrors();

            float[] points =
            {
                0.0f,   0.5f, 0.0f,
                0.5f,  -0.5f, 0.0f,
                -0.5f, -0.5f, 0.0f
            };

            uint vbo = GL.GenBuffer();

            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            GL.BufferData(BufferTarget.ArrayBuffer, points.Length * sizeof(float), points, BufferUsageHint.StaticDraw);

            uint vao = GL.GenVertexArray();

            GL.BindVertexArray(vao);
            GL.EnableVertexAttribArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero);

            string vertexShader   = @"#version 330 core
in vec3 vertex;
void main() {
	gl_Position = vec4(vertex,1.0);
}";
            string fragmentShader = @"#version 330 core
out vec4 color;
void main() {
	color = vec4(1.0,0.5,0.0,1.0);
}";

            uint vs = GL.CreateShader(ShaderType.VertexShader);

            GL.ShaderSource(vs, vertexShader);
            GL.CompileShader(vs);

            uint fs = GL.CreateShader(ShaderType.FragmentShader);

            GL.ShaderSource(fs, fragmentShader);
            GL.CompileShader(fs);

            uint program = GL.CreateProgram();

            GL.AttachShader(program, vs);
            GL.AttachShader(program, fs);
            GL.LinkProgram(program);

            while (GLFW.WindowShouldClose(window) == 0)
            {
                GLFW.PollEvents();

                double time = GLFW.GetTime();
                //double deltaTime = time-timePrev;
                //timePrev = time;

                GLFW.GetFramebufferSize(window, out int width, out int height);

                GL.Viewport(0, 0, width, height);

                var(colorR, colorG, colorB) = GetRainbowColor((float)time * 0.75f);

                colorR = Math.Max(colorR, 0.9f);
                colorG = Math.Max(colorG, 0.9f);
                colorB = Math.Max(colorB, 0.9f);

                GL.ClearColor(colorR, colorG, colorB);                 //GL.ClearColor(70/255f,130/255f,180/255f);
                GL.Clear(ClearBufferMask.ColorBufferBit);

                GL.UseProgram(program);
                GL.BindVertexArray(vao);
                GL.DrawArrays(PrimitiveType.Triangles, 0, points.Length / 3);

                GLFW.SwapBuffers(window);

                CheckGLErrors();

                Thread.Sleep(1);
            }

            UnloadOpenAL();
            UnloadGLFW();
        }
Ejemplo n.º 10
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();
        }