freeglut+libEGL+libGLU ports

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
User avatar
bogglez
Moderator
Moderator
Posts: 576
Joined: Sun Apr 20, 2014 9:45 am
Has liked: 0
Been liked: 0

freeglut+libEGL+libGLU ports

Post by bogglez » Wed Mar 01, 2017 9:33 am

I'm currently working on support for libglut and potentially libSDL in the future.
In order to get OpenGL support these libraries often use libEGL, so I implemented that first.
This also allows us to use platform-independent code for creating an OpenGL context.

Here is an example program that uses libEGL. It works on both KOS and Linux.

Code: Select all

#include <stdio.h>
#include <string.h>
#include <EGL/egl.h>
#include <GL/gl.h>


#define CLEANUP(n) { ret = (n); goto cleanup; }

static EGLint const gl_config_attribs[] = {
  EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  EGL_RED_SIZE, 1,
  EGL_GREEN_SIZE, 1,
  EGL_BLUE_SIZE, 1,
  EGL_ALPHA_SIZE, 0,

  EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
  EGL_NONE
};

static int init_egl(EGLDisplay * dest_egl_display, EGLSurface * dest_egl_surface, EGLNativeDisplayType display, EGLNativeWindowType surface) {
  int        ret = 0;
  EGLConfig  egl_config;
  EGLContext egl_context;
  EGLDisplay egl_display;
  EGLSurface egl_surface;
  EGLint     n;


  egl_display = eglGetDisplay(display);
  if(egl_display == EGL_NO_DISPLAY) {
    CLEANUP(1);
  }

  if(!eglInitialize(egl_display, 0, 0)) {
    CLEANUP(2);
  }

/* Advertised on Dreamcast, may also be used on Linux without X11/Wayland
  if(!strstr(eglQueryString(egl_display, EGL_EXTENSIONS), "EGL_KHR_surfaceless_opengl")) {
    CLEANUP(3);
  }
*/

  printf("EGL Version \"%s\"\n",    eglQueryString(egl_display, EGL_VERSION));
  printf("EGL Vendor \"%s\"\n",     eglQueryString(egl_display, EGL_VENDOR));
  printf("EGL Extensions \"%s\"\n", eglQueryString(egl_display, EGL_EXTENSIONS));

  if(!eglBindAPI(EGL_OPENGL_API)) {
    CLEANUP(4);
  }

  if(!eglChooseConfig(egl_display, gl_config_attribs, &egl_config, 1, &n) || n != 1) {
    CLEANUP(5);
  }

  egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, 0);
  if(!egl_context) {
    CLEANUP(6);
  }

  egl_surface = eglCreateWindowSurface(egl_display, egl_config, surface, 0);
  if(egl_surface == EGL_NO_SURFACE) {
    CLEANUP(7);
  }

  eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);

  eglSwapBuffers(egl_display, egl_surface);

  *dest_egl_display = egl_display;
  *dest_egl_surface = egl_surface;

cleanup:
  return ret;
}


int main(int argc, char ** argv) {
  EGLDisplay egl_display;
  EGLSurface egl_surface;

  int ret = init_egl(&egl_display, &egl_surface, EGL_DEFAULT_DISPLAY, 0);
  if(ret) {
    printf("Cannot init EGL, error %u, egl error %0x\n", ret, (unsigned)eglGetError());
    return 1;
  }


  for(;;) {
    glClearColor(1.f, 1.f, 0.f, 1.f);
    glClear(GL_COLOR_BUFFER_BIT);
    eglSwapBuffers(egl_display, egl_surface);
  }

  return 0;
}
Due to this change I think the glutSwapBuffers() function should be removed from libGL. It should be implemented in a new libglut port instead.
But because OpenGL needs to swap buffers somehow and we don't have a graphics driver that supports a framebuffer etc. and I don't want to complicate things, I suggest we add a non-standard function glSwapBuffersKOS instead (just a rename of glutSwapBuffers in libGL). Then libEGL can call libGL's glSwapBuffersKOS.

The current example code with glutSwapBuffers is non-standard glut, it doesn't even have a main loop, doesn't use glut for input processing, etc. so I think for porting glut code to Dreamcast it's kind of useless and should be rewritten to use glSwapBuffersKOS instead, or ultimately use proper glut code, when the glut library is done (I've made good progress on this, it's easy with libEGL in place).

The only worry I have is that users use glutSwapBuffers right now and they would be surprised about their program not compiling anymore (not link, the glut.h header would be removed from libGL). All they'd need to do is rename the function, though.
Alternatively we could add the function to gl.h with __attribute__ ((deprecated)) and make it call glSwapBuffersKOS properly. Then I think libglut could #undef glutSwapBuffers and provide one of its own.

There's also glutCopyBufferToTexture which is non-standard, but it doesn't name clash so I'm not too worried about it. That said, it has been replaced by standards-conformant frame buffer objects in OpenGL, so I think it's not needed anymore.

Thoughts?
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
Chilly Willy
DC Developer
DC Developer
Posts: 414
Joined: Thu Aug 20, 2009 11:00 am
Has liked: 0
Been liked: 2 times

Re: libEGL port

Post by Chilly Willy » Wed Mar 01, 2017 11:52 am

Alternatively we could add the function to gl.h with __attribute__ ((deprecated)) and make it call glSwapBuffersKOS properly. Then I think libglut could #undef glutSwapBuffers and provide one of its own.
That sounds like the best way to handle it.

Nice job on porting libEGL! What limitations does it have compared to the reference? In what ways is it better than libGL? Don't need to go too much in depth - just some hand-waving summary would do.
User avatar
bogglez
Moderator
Moderator
Posts: 576
Joined: Sun Apr 20, 2014 9:45 am
Has liked: 0
Been liked: 0

Re: libEGL port

Post by bogglez » Wed Mar 01, 2017 3:01 pm

Chilly Willy wrote: Nice job on porting libEGL! What limitations does it have compared to the reference?
Thanks!
I used the spec of libEGL 1.4.
I implemented the error checks quite thoroughly.
No support for calling EGL functions from multiple threads.
Only one graphics context.
Only one window (shouldn't matter for games).
No support for pixel buffers (off-screen rendering). We can do this in OpenGL with framebuffer objects so I don't think we want this anyway.
Only OpenGL/OpenGL ES 1.x.
The framebuffer config doesn't parse all settings as of now.
Resolution and color format cannot be changed right now, can change this in the future, but I'm sick of 320x240 homebrew. :)
Chilly Willy wrote:In what ways is it better than libGL?
The issue is that this question doesn't really make sense, so to speak.
libGL can not be used without something like EGL normally. We only get away with it in KOS because we already have a graphics context and we set it up with the vid_set_mode functions etc.
Actually that's EGL's job (or WGL, GLX, etc.).

With libEGL in place libraries that use it have a standard way to ask what OpenGL version is supported, ask for a framebuffer, etc. so we don't need to modify their code.
For example I ported freeglut in a few hours by copying the android folder, s/android/dreamcast, removed android specific functions and I was done. Haven't done anything regarding the input yet.
Right now I'm just wondering how to get this into kos-ports.. I have proper "make install" in place, but kos-ports syntax won't work for me due to limitations.
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
Chilly Willy
DC Developer
DC Developer
Posts: 414
Joined: Thu Aug 20, 2009 11:00 am
Has liked: 0
Been liked: 2 times

Re: libEGL port

Post by Chilly Willy » Wed Mar 01, 2017 5:21 pm

Ah, I see. It works with libGL to take care of many of the fiddly bits we use various things in KOS to handle right now.
User avatar
bogglez
Moderator
Moderator
Posts: 576
Joined: Sun Apr 20, 2014 9:45 am
Has liked: 0
Been liked: 0

Re: libEGL port

Post by bogglez » Wed Mar 01, 2017 5:25 pm

Code dump.

libEGL: https://bitbucket.org/bogglez/libegl
freeglut: https://bitbucket.org/bogglez/freeglut
example for glut usage: https://bitbucket.org/bogglez/kos_glut_ ... ew-default

The example uses my libGL15 https://bitbucket.org/bogglez/libgl15.
To make it work with KOS' libGL the following patch needs to be applied.
Note: libGL doesn't support OpenGL functions I'm using in the example, but if you delete all the OpenGL functions except glClear for example you can see that it works.


Installation:

Code: Select all

source $KOS_BASE/environ.sh
cd to directory
make install # installs to kos-ports/include and lib
Attachments
libGL_glut_compat.patch
(2.24 KiB) Downloaded 47 times
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
User avatar
bogglez
Moderator
Moderator
Posts: 576
Joined: Sun Apr 20, 2014 9:45 am
Has liked: 0
Been liked: 0

Re: freeglut+libEGL port

Post by bogglez » Sat Mar 04, 2017 10:45 am

I have also created a port for libGLU by mesa now.

Right now libGL in KOS implements very few glu functions and therefore most programs using libGLU won't compile in KOS. I doubt anyone is going to implement all the nurbs and tesselation functionality in GLU, for example.

Since libGLU just uses libGL, they are actually two completely different libraries. Luckily for us the mesa libGLU implementation can be used unchanged.

So what I did:
- Removed all glu* functions and glu.h from libGL
- Implemented glMultMatrixd as glMultMatrixf (which already existed) because gluPerspective uses it
- Created a kos-port libGLU which downloads the mesa tarball ftp://ftp.freedesktop.org/pub/mesa/glu/ and compiles it, installs glu.h


So combining all this work of porting libGLU, libEGL and freeglut we can write very standard, cross-platform software now. The weak point is only libGL.

If I can get this merged I'll also rewrite all the OpenGL examples to be more standard. I have tested nehe02 and nehe26 already and they worked properly.
I can also write some documentation for the wiki and point out third party tutorials, because they should work now.

The only thing I need to merge this now is support in kos-ports to install headers of multiple ports to the same directory, i.e. copying headers instead of symlinking them and some post-install to go along with it. Alternatively, a way to do GNU make installs as I pointed out in my other thread on the issue.

So, calling on you, Crabby.
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5387
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has liked: 1 time
Been liked: 12 times
Contact:

Re: freeglut+libEGL+libGLU ports

Post by BlueCrab » Sat Mar 04, 2017 11:17 am

I'm planning out what I want to do with it, so give me time. I'm only one person, after all (and I do have a life too, as hard as that might be to believe :lol: ).
User avatar
bogglez
Moderator
Moderator
Posts: 576
Joined: Sun Apr 20, 2014 9:45 am
Has liked: 0
Been liked: 0

Re: freeglut+libEGL+libGLU ports

Post by bogglez » Sat Mar 04, 2017 11:20 am

Silly crab, you're not a person! (you're our lord)

No rush, but I think I'll leave it at this for now before I make any more sweeping changes. Just wanted to get some feedback on how to proceed first. Take your time.
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
Chilly Willy
DC Developer
DC Developer
Posts: 414
Joined: Thu Aug 20, 2009 11:00 am
Has liked: 0
Been liked: 2 times

Re: freeglut+libEGL+libGLU ports

Post by Chilly Willy » Sun Mar 05, 2017 5:31 pm

Real life can be rough on free time. It's been a rough fall/winter here - my dad went into the hospital to get a stint and wound up with complications that killed him a week ago. I won't be (fully) back into the swing of things for a while.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5387
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has liked: 1 time
Been liked: 12 times
Contact:

Re: freeglut+libEGL+libGLU ports

Post by BlueCrab » Sun Mar 05, 2017 5:54 pm

Chilly Willy wrote:Real life can be rough on free time. It's been a rough fall/winter here - my dad went into the hospital to get a stint and wound up with complications that killed him a week ago. I won't be (fully) back into the swing of things for a while.
You have my sincerest sympathy. I can't even imagine how you must be feeling right now...
nymus
DC Developer
DC Developer
Posts: 955
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has liked: 0
Been liked: 1 time

Re: freeglut+libEGL+libGLU ports

Post by nymus » Sun Mar 05, 2017 7:36 pm

My condolences for your loss and best wishes as you cope.
behold the mind
inspired by Dreamcast
User avatar
bogglez
Moderator
Moderator
Posts: 576
Joined: Sun Apr 20, 2014 9:45 am
Has liked: 0
Been liked: 0

Re: freeglut+libEGL+libGLU ports

Post by bogglez » Sun Mar 05, 2017 9:54 pm

I'm so sorry to hear that, Chilly. If you need someone to talk we're here for you!
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
Chilly Willy
DC Developer
DC Developer
Posts: 414
Joined: Thu Aug 20, 2009 11:00 am
Has liked: 0
Been liked: 2 times

Re: freeglut+libEGL+libGLU ports

Post by Chilly Willy » Mon Mar 06, 2017 1:13 pm

Thanks, everyone. It was helpful that my sister and oldest brother flew into town to be with us at the end. He went the way he wanted. In any case, programming (especially on these older system) has always been a comfort, so I'm working on stuff to keep myself busy to avoid becoming depressed.
User avatar
ThePerfectK
DCEmu Freak
DCEmu Freak
Posts: 94
Joined: Thu Apr 27, 2006 10:15 am
Has liked: 6 times
Been liked: 13 times

Re: freeglut+libEGL+libGLU ports

Post by ThePerfectK » Tue Mar 14, 2017 10:06 pm

Chilly Willy wrote:Real life can be rough on free time. It's been a rough fall/winter here - my dad went into the hospital to get a stint and wound up with complications that killed him a week ago. I won't be (fully) back into the swing of things for a while.
My deepest condolences
Still Thinking!~~
User avatar
Neoblast
DC Developer
DC Developer
Posts: 312
Joined: Sat Dec 01, 2007 8:51 am
Has liked: 0
Been liked: 0

Re: freeglut+libEGL+libGLU ports

Post by Neoblast » Sat May 27, 2017 6:08 am

Chilly Willy wrote:Thanks, everyone. It was helpful that my sister and oldest brother flew into town to be with us at the end. He went the way he wanted. In any case, programming (especially on these older system) has always been a comfort, so I'm working on stuff to keep myself busy to avoid becoming depressed.
My condolences man :(

If you need someone to talk or anything, you can count on us.
User avatar
DCDayDreamer
DCEmu Respected
DCEmu Respected
Posts: 445
Joined: Tue Mar 16, 2004 6:59 pm
Has liked: 0
Been liked: 0

Re: freeglut+libEGL+libGLU ports

Post by DCDayDreamer » Sat May 27, 2017 9:18 am

Chilly Willy wrote:Real life can be rough on free time. It's been a rough fall/winter here - my dad went into the hospital to get a stint and wound up with complications that killed him a week ago.
I've only just spotted this and really can't apologize enough for not responding sooner, I'm not a coder so this forum usually just gets a passing glance from me. Terribly sorry for your loss, I've been there, a long time ago now, and with both parents.
Chilly Willy wrote:In any case, programming (especially on these older system) has always been a comfort, so I'm working on stuff to keep myself busy to avoid becoming depressed.
Please don't take this the wrong way, but doing something you enjoy (whatever it is) is quite possibly the best form of therapy in situations like this, it's not there to help you forget the hard times, but I found that my favorite pastime back then helped to ease me through the grieving process.
Across the Universe
User avatar
bogglez
Moderator
Moderator
Posts: 576
Joined: Sun Apr 20, 2014 9:45 am
Has liked: 0
Been liked: 0

Re: freeglut+libEGL+libGLU ports

Post by bogglez » Sun May 28, 2017 2:17 am

Getting back on topic, since this thread is at the top again I wanted to post an update:

as I stated before the code has already been finished, I just need a way to install the packages.
kos-ports doesn't do that job right now, and I don't really feel like working on yet another NIH package manager.
So I'm currently looking into using meson wraps for packaging http://mesonbuild.com/Wrap-dependency-s ... anual.html
Meson supports downloading tar balls, cloning from git, cross-compilation as well as patching, so it seems to be a proper replacement for kos-ports.
I'll experiment with it a bit and try to get some ports working with it.

Some upstream packages (like glut) use CMake, and kos-ports also doesn't support that.
I was able to invoke CMake with the proper flags to compile my glut port with an updated upstream CMakeLists.txt, but I'm not entirely sure how that would integrate with meson or whether I'll introduce CMake as a dependency. I could also just write a new meson build file. We'll see.

Something else I plan to do is remove all the GLU functions out of the libGL implementation by Ph3nom and introduce the libGLU port. Then there will be a full libGLU (Ph3nom only implemented a few functions).
That will also make it possible for me to use the same libGLU in my libgl15 on which I want to work more in the future after the package problem is solved.
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
User avatar
GyroVorbis
Elysian Shadows Developer
Elysian Shadows Developer
Posts: 1808
Joined: Mon Mar 22, 2004 4:55 pm
Location: #%^&*!!!11one Super Sonic
Has liked: 0
Been liked: 0
Contact:

Re: freeglut+libEGL+libGLU ports

Post by GyroVorbis » Sun May 28, 2017 2:49 am

Jesus. Sorry to just jump in and be all off-topic, but I just wanted to say HOLY SHIT. I didn't realize how far you guys had come on all this stuff and the level of completion you guys were nearing. Great work, guys!
Elysian Shadows - "Next-Gen" 2D/3D RPG coming to Sega Dreamcast, Steam, OUYA, and Smartphones
Image
http://www.elysianshadows.com
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5387
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has liked: 1 time
Been liked: 12 times
Contact:

Re: freeglut+libEGL+libGLU ports

Post by BlueCrab » Sun May 28, 2017 2:17 pm

@bogglez: Ahem...

When I said I'd work on it, I did mean I would work on it. And now, it's done. :wink:

I will request that if you're going to do a bunch of stuff to GL and splitting it apart, that it might be a good idea to keep around the minimal implementations of GLU/GLUT that we have now (as a separate port). Sometimes minimal is all that's needed, after all. Also, be sure to mark yourself as the maintainer in the portfiles. :wink:
User avatar
lerabot
Insane DCEmu
Insane DCEmu
Posts: 118
Joined: Sun Nov 01, 2015 8:25 pm
Has liked: 0
Been liked: 1 time

Re: freeglut+libEGL+libGLU ports

Post by lerabot » Mon May 29, 2017 11:14 am

Sorry for my dumb question, but is EGL main purpose is to provide OS-free implementation of openGL?

Just trying to see what is EGL about...
Post Reply