Пример #1
0
        public GlfwPlatform(int width, int height, bool fullscreen = false)
        {
            midi = new MidiConsoleOut();

            // Handle Glfw errors:
            Glfw.SetErrorCallback((code, desc) => throw new Exception(String.Format("GLFW error code {0}: {1}", code, desc)));

            Debug.WriteLine("glfw.Init()");
            Glfw.Init();

            // Disable window resizing
            Glfw.WindowHint(Glfw.Hint.Resizable, false);
            // Enable multi-sampling
            Glfw.WindowHint(Glfw.Hint.Samples, 8);

            Glfw.Monitor monitor = fullscreen ? Glfw.GetPrimaryMonitor() : Glfw.Monitor.None;

            Debug.WriteLine("window = glfw.CreateWindow()");
            window = Glfw.CreateWindow(
                width,
                height,
                "e-sharp-minor",
                monitor,
                Glfw.Window.None
                );

            // Fetch the real window size:
            Glfw.GetWindowSize(window, out width, out height);

            // Window dimensions (aspect ratio) in VG coordinate space (non-retina):
            this.Width  = width;
            this.Height = height;

            // Get the real framebuffer size for OpenGL pixels; should work with Retina:
            int fbWidth, fbHeight;

            Glfw.GetFramebufferSize(window, out fbWidth, out fbHeight);

            // These are needed for vgClear since it works in framebuffer pixels.
            FramebufferWidth  = fbWidth;
            FramebufferHeight = fbHeight;

            Debug.WriteLine("glfw.MakeContextCurrent(window)");
            Glfw.MakeContextCurrent(window);

            // create an OpenVG context
            Debug.WriteLine("vgContext = vgPrivContextCreateAM(0)");
            vgContext = vgPrivContextCreateAM(IntPtr.Zero);

            // create a drawing surface (sRGBA premultiplied color space)
            //vgSurface = vgPrivSurfaceCreateAM(fbWidth, fbHeight, 0, 1, 1);
            vgSurface = vgPrivSurfaceCreateAM(fbWidth, fbHeight, 0, 1, 0);

            // bind context and surface
            vgPrivMakeCurrentAM(vgContext, vgSurface);

            // Create OpenVGContext:
            vg = new OpenVGContext();

            // Apply scale for retina display:
            vg.Seti(ParamType.VG_MATRIX_MODE, (int)MatrixMode.VG_MATRIX_PATH_USER_TO_SURFACE);
            vg.LoadIdentity();
            vg.Scale((float)fbWidth / (float)width, (float)fbHeight / (float)height);
            vg.Translate(0.5f, 0.5f);

            vg.Seti(ParamType.VG_MATRIX_MODE, (int)MatrixMode.VG_MATRIX_GLYPH_USER_TO_SURFACE);
            vg.LoadIdentity();
            vg.Scale((float)fbWidth / (float)width, (float)fbHeight / (float)height);
            vg.Translate(0.5f, 0.5f);

            vg.Seti(ParamType.VG_MATRIX_MODE, (int)MatrixMode.VG_MATRIX_PATH_USER_TO_SURFACE);

            // Disable vsync and show frame immediately after render:
            Glfw.SwapInterval(0);

            Debug.WriteLine("glfw.ShowWindow(window)");
            Glfw.ShowWindow(window);

            Glfw.SetKeyCallback(window, handleKeys);
            Glfw.SetMouseButtonCallback(window, handleMouseButton);
            Glfw.SetCursorPosCallback(window, handleMousePos);
        }
Пример #2
0
        public void InitRenderThread()
        {
            uint success;

            // TODO: validate this assumption that display number goes here (what other values to use besides EGL_DEFAULT_DISPLAY?)
            Debug.WriteLine("eglGetDisplay(...)");
            egldisplay = eglGetDisplay(bcmDisplay);
            //egldisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
            throwIfError();
            Debug.WriteLine("egldisplay = {0}", egldisplay);

            Debug.WriteLine("eglInitialize(egldisplay)");
            success = eglInitialize(egldisplay, out majorVersion, out minorVersion);
            if (success == 0)
            {
                throwIfError();
                throw new Exception("eglInitialize returned FALSE");
            }
            Debug.WriteLine("egl majorVersion={0} minorVersion={1}", majorVersion, minorVersion);

#if AMANITH_GLE
            Debug.WriteLine("eglBindAPI(EGL_OPENGL_ES_API)");
            success = eglBindAPI(EGL.EGL_OPENGL_ES_API);
            if (success == 0)
            {
                throwIfError();
                throw new Exception("eglBindAPI returned FALSE");
            }
#else
            Debug.WriteLine("eglBindAPI(EGL_OPENVG_API)");
            success = eglBindAPI(EGL.EGL_OPENVG_API);
            if (success == 0)
            {
                throwIfError();
                throw new Exception("eglBindAPI returned FALSE");
            }
#endif

#if false // AMANITH_GLE
            // NOTE: this selecting GLES version 2 fails on RPI.
            int[] context_attribs = new int[] {
                (int)EGL_ATTRIBUTES.EGL_CONTEXT_CLIENT_VERSION, 2,
                (int)EGL_ATTRIBUTES.EGL_NONE
            };
#else
            int[] context_attribs = null;
#endif

            int[] attribs =
            {
                (int)EGL_ATTRIBUTES.EGL_RED_SIZE,                                      8,
                (int)EGL_ATTRIBUTES.EGL_GREEN_SIZE,                                    8,
                (int)EGL_ATTRIBUTES.EGL_BLUE_SIZE,                                     8,
                (int)EGL_ATTRIBUTES.EGL_ALPHA_SIZE,                                    8,
                //(int)EGL_ATTRIBUTES.EGL_LUMINANCE_SIZE,     (int)EGL_ATTRIBUTES.EGL_DONT_CARE,
                (int)EGL_ATTRIBUTES.EGL_SURFACE_TYPE, (int)EGL_ATTRIBUTES.EGL_WINDOW_BIT,
                //(int)EGL_ATTRIBUTES.EGL_COLOR_BUFFER_TYPE,  (int)EGL.EGL_RGB_BUFFER,
                (int)EGL_ATTRIBUTES.EGL_SAMPLES,                                       0,
#if AMANITH_GLE
                (int)EGL_ATTRIBUTES.EGL_DEPTH_SIZE,                                    8,
                (int)EGL_ATTRIBUTES.EGL_STENCIL_SIZE,                                  8,
#endif
                (int)EGL_ATTRIBUTES.EGL_NONE
            };
            int  numconfigs;
            uint eglconfig;

            Debug.WriteLine("eglChooseConfig(egldisplay, ...)");
            success = eglChooseConfig(egldisplay, attribs, out eglconfig, 1, out numconfigs);
            if (success == 0)
            {
                throwIfError();
                throw new Exception("eglChooseConfig returned FALSE");
            }
            if (numconfigs != 1)
            {
                throw new Exception("numconfigs != 1");
            }
            Debug.WriteLine("eglconfig = {0}", eglconfig);

            window.element = dispman_element;
            window.width   = Width;
            window.height  = Height;
            var windowHandle = GCHandle.Alloc(window, GCHandleType.Pinned);

            Debug.WriteLine("eglCreateWindowSurface(egldisplay, ...)");
            eglsurface = eglCreateWindowSurface(egldisplay, eglconfig, windowHandle.AddrOfPinnedObject(), null);
            if (eglsurface == 0)
            {
                throwIfError();
                throw new Exception("eglCreateWindowSurface returned FALSE");
            }
            Debug.WriteLine("eglsurface = {0}", eglsurface);
            Debug.WriteLine("eglCreateContext(egldisplay, ...)");
            eglcontext = eglCreateContext(egldisplay, eglconfig, 0, context_attribs);
            if (eglcontext == 0)
            {
                throwIfError();
                throw new Exception("eglCreateContext returned FALSE");
            }
            Debug.WriteLine("eglcontext = {0}", eglcontext);

            Debug.WriteLine("eglMakeCurrent(egldisplay, eglsurface, eglsurface, eglcontext)");
            success = eglMakeCurrent(egldisplay, eglsurface, eglsurface, eglcontext);
            if (success == 0)
            {
                throwIfError();
                throw new Exception("eglMakeCurrent returned FALSE");
            }

            Debug.WriteLine("eglSwapInterval(egldisplay, 0)");
            success = eglSwapInterval(egldisplay, 0);
            if (success == 0)
            {
                throwIfError();
                throw new Exception("eglSwapInterval returned FALSE");
            }

#if AMANITH_GLE
            // Now build the AmanithVG context:
            unsafe
            {
                Debug.WriteLine("mztContext = vgPrivContextCreateAM(null)");
                var mztContext = vgPrivContextCreateAM(null);
                Debug.WriteLine($"mztContext = {(uint)mztContext}");

                Debug.WriteLine("mztSurface = vgPrivSurfaceCreateAM()");
                var mztSurface = vgPrivSurfaceCreateAM(Width, Height, 0U, 0U, 0U);
                Debug.WriteLine($"mztSurface = {(uint)mztSurface}");

                Debug.WriteLine("success = vgPrivMakeCurrentAM(mztContext, mztSurface)");
                success = vgPrivMakeCurrentAM(mztContext, mztSurface);
                Debug.WriteLine($"success = {success}");
                if (success == 0)
                {
                    Console.Error.WriteLine("vgPrivMakeCurrentAM failed");
                }
            }
#endif

            // Translate to pixel-perfect offset:
            Debug.WriteLine("vgSeti(VG_MATRIX_MODE)");
            vg.Seti(OpenVG.ParamType.VG_MATRIX_MODE, (int)OpenVG.MatrixMode.VG_MATRIX_PATH_USER_TO_SURFACE);
            Debug.WriteLine("vgLoadIdentity()");
            vg.LoadIdentity();
            Debug.WriteLine("vgTranslate()");
            vg.Translate(0.5f, 0.5f);
        }