gpu-gles: schtruck/fpse merge: don't delay gl init
[pcsx_rearmed.git] / plugins / gpu-gles / gpuDraw.c
index 3d09c33..3842503 100644 (file)
 \r
 #define _IN_DRAW\r
 \r
-#ifdef _WINDOWS\r
-#include "stdafx.h"\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <math.h>\r
-#include "externals.h"\r
-#include "gpu.h"\r
-#include "draw.h"\r
-#include "prim.h"\r
-#include "texture.h"\r
-#else\r
+\r
 #include "gpuExternals.h"\r
-#include "GPUPlugin.h"\r
+#include "gpuPlugin.h"\r
 #include "gpuDraw.h"\r
 #include "gpuPrim.h"\r
 #include "gpuTexture.h"\r
@@ -48,7 +38,6 @@
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include <math.h>\r
-#endif\r
 //#include "menu.h"\r
             \r
 ////////////////////////////////////////////////////////////////////////////////////\r
 ////////////////////////////////////////////////////////////////////////////////////\r
 // draw globals\r
 \r
-#ifdef _WINDOWS\r
-HDC            dcGlobal=NULL;\r
-HWND           hWWindow;\r
-#else\r
 void  glBlendEquationEXT(GLenum mode);\r
 void  glColorTableEXT(GLenum target, GLenum internalFormat, GLsizei width, GLenum format,GLenum type, const GLvoid *data);\r
-#endif\r
 \r
 // draw globals; most will be initialized again later (by config or checks) \r
 \r
@@ -158,50 +142,6 @@ GLbitfield     uiBufferBits=GL_COLOR_BUFFER_BIT;
 // Set OGL pixel format\r
 ////////////////////////////////////////////////////////////////////////\r
  \r
-#ifdef _WINDOWS\r
-BOOL bSetupPixelFormat(HDC hDC)\r
-{\r
- int pixelformat;\r
- static PIXELFORMATDESCRIPTOR pfd = \r
-  {\r
-   sizeof(PIXELFORMATDESCRIPTOR),    // size of this pfd\r
-    1,                               // version number\r
-    PFD_DRAW_TO_WINDOW |             // support window\r
-      PFD_SUPPORT_OPENGL |           // support OpenGL\r
-      PFD_DOUBLEBUFFER,              // double buffered\r
-    PFD_TYPE_RGBA,                   // RGBA type\r
-    16,                              // 16-bit color depth  (adjusted later)\r
-    0, 0, 0, 0, 0, 0,                // color bits ignored\r
-    0,                               // no alpha buffer\r
-    0,                               // shift bit ignored\r
-    0,                               // no accumulation buffer\r
-    0, 0, 0, 0,                      // accum bits ignored\r
-    0,                               // z-buffer    \r
-    0,\r
-    0,                               // no auxiliary buffer\r
-    PFD_MAIN_PLANE,                  // main layer\r
-    0,                               // reserved\r
-    0, 0, 0                          // layer masks ignored\r
-  };\r
\r
- pfd.cColorBits=iColDepth;                             // set user color depth\r
- pfd.cDepthBits=iZBufferDepth;                         // set user zbuffer (by psx mask)\r
-\r
- if((pixelformat=ChoosePixelFormat(hDC,&pfd))==0)     \r
-  {\r
-   MessageBox(NULL,"ChoosePixelFormat failed","Error",MB_OK);\r
-   return FALSE;\r
-  }\r
-\r
- if(SetPixelFormat(hDC,pixelformat, &pfd)==FALSE)\r
-  {\r
-   MessageBox(NULL,"SetPixelFormat failed","Error",MB_OK);\r
-   return FALSE;\r
-  }\r
-\r
- return TRUE;\r
-}\r
-#endif\r
 \r
 ////////////////////////////////////////////////////////////////////////\r
 // Get extension infos (f.e. pal textures / packed pixels)\r
@@ -211,16 +151,12 @@ void GetExtInfos(void)
 {\r
  BOOL bPacked=FALSE;                                   // default: no packed pixel support\r
 \r
- if(strstr((s8 *)glGetString(GL_EXTENSIONS),         // packed pixels available?\r
+ if(strstr((char *)glGetString(GL_EXTENSIONS),         // packed pixels available?\r
     "GL_EXT_packed_pixels"))                          \r
   bPacked=TRUE;                                        // -> ok\r
 \r
  \r
- #ifdef _WINDOWS\r
- iClampType=GL_CLAMP;\r
-#else\r
  iClampType=GL_CLAMP_TO_EDGE;\r
-#endif\r
 }\r
 \r
 ////////////////////////////////////////////////////////////////////////\r
@@ -304,11 +240,45 @@ void CreateScanLines(void)
 // Initialize OGL\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-#ifdef _WINDOWS    \r
-HGLRC GLCONTEXT=NULL;\r
+#define MODE_RAW 0\r
+#define MODE_X11 1\r
+#define MODE_SDL 2\r
+int use_fsaa = 0;\r
+\r
+EGLDisplay display;\r
+EGLConfig  config;\r
+EGLContext context;\r
+EGLSurface surface;\r
+\r
+#if defined(USE_X11)\r
+#include "X11/Xlib.h"\r
+#include "X11/Xutil.h"\r
+#include "X11/Xatom.h"\r
+\r
+Window                 x11Window       = 0;\r
+Display*               x11Display      = 0;\r
+long                   x11Screen       = 0;\r
+XVisualInfo            x11Visual;\r
+XVisualInfo*   px11Visual      = 0;\r
+Colormap        x11Colormap    = 0;\r
 #endif\r
 \r
-#ifdef MAEMO_CHANGES\r
+EGLint attrib_list_fsaa[] =\r
+{\r
+       EGL_SURFACE_TYPE, EGL_WINDOW_BIT,\r
+       EGL_BUFFER_SIZE,    0,\r
+       EGL_DEPTH_SIZE,     16,\r
+       EGL_SAMPLE_BUFFERS, 1,\r
+       EGL_SAMPLES,        4,\r
+       EGL_NONE\r
+};\r
+\r
+EGLint attrib_list[] =\r
+{\r
+//     EGL_DEPTH_SIZE,   16,\r
+       EGL_NONE\r
+};\r
+\r
 bool TestEGLError(const char* pszLocation)\r
 {\r
        /*\r
@@ -320,98 +290,141 @@ bool TestEGLError(const char* pszLocation)
        if (iErr != EGL_SUCCESS)\r
        {\r
                printf("%s failed (%d).\n", pszLocation, iErr);\r
-               return false;\r
+               return FALSE;\r
        }\r
 \r
-       return true;\r
+       return TRUE;\r
 }\r
 \r
-void maemoGLinit(){\r
-       iResX= 800;\r
-    iResY =480;\r
+static void initEGL(void)\r
+{\r
+       printf ("GL init\n");\r
 \r
+       EGLint numConfigs;\r
+       EGLint majorVersion;\r
+       EGLint minorVersion;\r
+#if defined(USE_X11)\r
+       enum\r
+       {\r
+       _NET_WM_STATE_REMOVE =0,\r
+       _NET_WM_STATE_ADD = 1,\r
+       _NET_WM_STATE_TOGGLE =2\r
+       };\r
        \r
-       printf ("maemo GL init\n");\r
-       long int winxid=GDK_WINDOW_XID(GTK_WIDGET(windowG)->window);\r
-       printf ("%d\n",winxid);\r
-       \r
-       EGLContext                      context = 0;\r
-       EGLConfig                       eglConfig       = 0;\r
-       EGLContext                      eglContext      = 0;\r
-       display = eglGetDisplay( EGL_DEFAULT_DISPLAY );\r
+       Window                          sRootWindow;\r
+       XSetWindowAttributes    sWA;\r
+       unsigned int                ui32Mask;\r
+       int                                 i32Depth;\r
+#endif\r
        \r
-       if( eglInitialize( display, NULL, NULL ) == EGL_FALSE )\r
-            {            \r
-                printf( "EGL Initialize failed!\n" );\r
+       EGLint *attribList = NULL;\r
+       if (use_fsaa)\r
+       {\r
+               printf( "GLES: Using Full Scene Antialiasing\n" );\r
+               attribList = attrib_list_fsaa;\r
+       }\r
+       else\r
+       {\r
+               attribList = attrib_list;\r
+       }\r
+\r
+#if defined(USE_X11)\r
+            // Initializes the display and screen\r
+            x11Display = XOpenDisplay( ":0" );\r
+            if (!x11Display)\r
+            {\r
+                printf("GLES Error: Unable to open X display\n");\r
+            }\r
+            x11Screen = XDefaultScreen( x11Display );\r
                                \r
+            // Gets the display parameters so we can pass the same parameters to the window to be created.\r
+            sRootWindow        = RootWindow(x11Display, x11Screen);\r
+            i32Depth   = DefaultDepth(x11Display, x11Screen);\r
+            px11Visual = &x11Visual;\r
+            XMatchVisualInfo( x11Display, x11Screen, i32Depth, TrueColor, px11Visual);\r
+            if (!px11Visual)\r
+            {\r
+                printf("GLES Error: Unable to acquire visual\n");\r
                        }\r
+            // Colormap of the specified visual type for the display.\r
+            x11Colormap = XCreateColormap( x11Display, sRootWindow, px11Visual->visual, AllocNone );\r
+            sWA.colormap = x11Colormap;\r
+\r
+            // List of events to be handled by the application. Add to these for handling other events.\r
+            sWA.event_mask = StructureNotifyMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask;\r
 \r
-const EGLint attributeList[] = { \r
-                                     EGL_SURFACE_TYPE, EGL_WINDOW_BIT, \r
-                                     /*EGL_BUFFER_SIZE, 32, */\r
-                                     EGL_NONE \r
-                                   };\r
+            // Display capabilities list.\r
+            ui32Mask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;\r
                                                           \r
-       EGLint pi32ConfigAttribs[5];\r
-       pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;\r
-       pi32ConfigAttribs[1] = EGL_WINDOW_BIT;\r
-       pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;\r
-       pi32ConfigAttribs[3] = EGL_OPENGL_ES2_BIT;      \r
-       pi32ConfigAttribs[4] = EGL_NONE;\r
-\r
-       EGLint pi32ContextAttribs[3];\r
-       pi32ContextAttribs[0] = EGL_CONTEXT_CLIENT_VERSION;\r
-       pi32ContextAttribs[1] = 2;\r
-       pi32ContextAttribs[2] = EGL_NONE;\r
-\r
-       int iConfigs;\r
-       if (!eglChooseConfig(display, attributeList, &eglConfig, 1, &iConfigs) || (iConfigs != 1))\r
+            // Creates the X11 window\r
+            x11Window = XCreateWindow( x11Display, RootWindow(x11Display, x11Screen), 0, 0, iResX, iResY,\r
+                                        0, CopyFromParent, InputOutput, CopyFromParent, ui32Mask, &sWA);\r
+\r
+            // Make the window viewable and flush the output buffer.\r
+            XMapWindow(x11Display, x11Window);\r
+            XFlush(x11Display);\r
+\r
+            // Make the window fullscreen\r
+            unsigned char fullScreen = 1;\r
+            Atom wmState = XInternAtom(x11Display, "_NET_WM_STATE", False);\r
+            Atom wmFullScreen = XInternAtom(x11Display,"_NET_WM_STATE_FULLSCREEN", False);\r
+\r
+            XEvent xev;\r
+            xev.xclient.type               = ClientMessage;\r
+            xev.xclient.serial             = 0;\r
+            xev.xclient.send_event      = True;\r
+            xev.xclient.window             = x11Window;\r
+            xev.xclient.message_type    = wmState;\r
+            xev.xclient.format             = 32;\r
+            xev.xclient.data.l[0]              = (fullScreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE);\r
+            xev.xclient.data.l[1]              = wmFullScreen;\r
+            xev.xclient.data.l[2]              = 0;\r
+\r
+            XSendEvent(x11Display, DefaultRootWindow(x11Display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);\r
+\r
+            display = eglGetDisplay( (EGLNativeDisplayType)x11Display );\r
+#else\r
+            display = eglGetDisplay( (EGLNativeDisplayType)0 );\r
+#endif\r
+\r
+       if( display == EGL_NO_DISPLAY )\r
        {\r
-               printf("Error: eglChooseConfig() failed.\n");\r
+               printf( "GLES EGL Error: GL No Display\n" );\r
        }\r
-       printf ("%d\n",iConfigs);\r
-       surface = eglCreateWindowSurface(display, eglConfig, (void*)winxid, NULL);\r
-       printf ("%d\n",surface);\r
-    if (!TestEGLError("eglCreateWindowSurface"))\r
+\r
+       if( !eglInitialize( display, &majorVersion, &minorVersion ) )\r
        {\r
-               printf ("eglCreateWindowSurface fail");\r
+               printf( "GLES EGL Error: eglInitialize failed\n" );\r
        }\r
 \r
-       //context = eglCreateContext(display, eglConfig, NULL, pi32ContextAttribs);\r
-       context =eglCreateContext( display, eglConfig,\r
-                                    EGL_NO_CONTEXT, NULL \r
-                                  );\r
-printf ("%d\n",context);\r
-       if (!TestEGLError("eglCreateContext"))\r
+       if( !eglChooseConfig( display, attribList, &config, 1, &numConfigs ) )\r
        {\r
-               printf("error eglCreateContext");\r
+               printf( "GLES EGL Error: eglChooseConfig failed\n" );\r
        }\r
 \r
-       eglMakeCurrent(display, surface, surface, context);\r
-\r
-       if (!TestEGLError("eglMakeCurrent"))\r
+       context = eglCreateContext( display, config, NULL, NULL );\r
+       if( context==0 )\r
        {\r
-               printf("error eglMakeCurrent");\r
+               printf( "GLES EGL Error: eglCreateContext failed\n" );\r
        }\r
-}\r
+\r
+#if defined(USE_X11)\r
+            surface = eglCreateWindowSurface( display, config, (EGLNativeDisplayType)x11Window, NULL );\r
+#else\r
+            surface = eglCreateWindowSurface( display, config, (EGLNativeDisplayType)0, NULL );\r
 #endif\r
 \r
+    eglMakeCurrent( display, surface, surface, context );\r
+    if (!TestEGLError("eglMakeCurrent"))\r
+        printf("error eglMakeCurrent");\r
+    else\r
+        printf("GLES Window Opened\n");\r
+}\r
+\r
 int GLinitialize() \r
 {\r
- //----------------------------------------------------// \r
-#ifdef _WINDOWS\r
- HGLRC objectRC;\r
- // init\r
- dcGlobal = GetDC(hWWindow);                           // FIRST: dc/rc stuff\r
- objectRC = wglCreateContext(dcGlobal); \r
- GLCONTEXT=objectRC;\r
- wglMakeCurrent(dcGlobal, objectRC);\r
- // CheckWGLExtensions(dcGlobal);\r
- if(bWindowMode) ReleaseDC(hWWindow,dcGlobal);         // win mode: release dc again\r
-#endif\r
-#ifdef MAEMO_CHANGES\r
-        maemoGLinit();\r
-#endif\r
+ initEGL();\r
+\r
  //----------------------------------------------------// \r
 \r
  glViewport(rRatioRect.left,                           // init viewport by ratio rect\r
@@ -499,11 +512,15 @@ void GLcleanup()
 {                                                     \r
  CleanupTextureStore();                                // bye textures\r
 \r
-#ifdef _WINDOWS \r
- wglMakeCurrent(NULL, NULL);                           // bye context\r
- if(GLCONTEXT) wglDeleteContext(GLCONTEXT);\r
- if(!bWindowMode && dcGlobal) \r
-  ReleaseDC(hWWindow,dcGlobal);\r
+       eglMakeCurrent( display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );\r
+       eglDestroySurface( display, surface );\r
+       eglDestroyContext( display, context );\r
+       eglTerminate( display );\r
+\r
+#if defined(USE_X11)\r
+               if (x11Window) XDestroyWindow(x11Display, x11Window);\r
+               if (x11Colormap) XFreeColormap( x11Display, x11Colormap );\r
+               if (x11Display) XCloseDisplay(x11Display);\r
 #endif\r
 }\r
 \r
@@ -1204,9 +1221,7 @@ void assignTexture4(void)
 // render pos / buffers\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-#ifndef _WINDOWS\r
 #define EqualRect(pr1,pr2) ((pr1)->left==(pr2)->left && (pr1)->top==(pr2)->top && (pr1)->right==(pr2)->right && (pr1)->bottom==(pr2)->bottom)\r
-#endif\r
 \r
 ////////////////////////////////////////////////////////////////////////\r
 // SetDisplaySettings: "simply" calcs the new drawing area and updates\r