Problems with Opengl ES under IOS

Hi ,

I have been trying to get SDL and Opengl ES to play nicely under IOS, but I
have a few problems.
I am not sure if my ordering is correct. If I run this simple example the
CPU goes to 160% and memory usage keep increasing like there is a memory
leak. If I put the GL calls inside the event loop seems to reduce the CPU
usage. Am I doing something terribly wrong here ? Thanks in advance.

#include “SDL.h”
#include <OpenGLES/ES1/gl.h>
#include <OpenGLES/ES1/glext.h>

int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf(“Could not initialize SDL\n”);
return 1;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);

SDL_DisplayMode displayMode;
SDL_GetDesktopDisplayMode(0, &displayMode);

SDL_Window* window = SDL_CreateWindow("TESTGL", 0, 0, 640, 1150,

SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE);

SDL_GLContext gl = SDL_GL_CreateContext(window);

glClearColor ( 1.0, 0.0, 0.0, 1.0 );
glClear ( GL_COLOR_BUFFER_BIT );
SDL_GL_SwapWindow(window);

SDL_Event event;
auto done = false;
while (!done)
{

    SDL_PumpEvents();
    while (SDL_PollEvent(&event))
    {

        switch (event.type)
        {
            case SDL_QUIT:
                done = true;
                break;
        }
    }
}

SDL_GL_DeleteContext(gl);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;

}

Thanks,

Alex

2014-05-06 2:07 GMT+02:00 Alexander Chaliovski :

Hi ,

I have been trying to get SDL and Opengl ES to play nicely under IOS, but
I have a few problems.
I am not sure if my ordering is correct. If I run this simple example the
CPU goes to 160% and memory usage keep increasing like there is a memory
leak. If I put the GL calls inside the event loop seems to reduce the CPU
usage. Am I doing something terribly wrong here ? Thanks in advance.

#include “SDL.h”
#include <OpenGLES/ES1/gl.h>
#include <OpenGLES/ES1/glext.h>

int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf(“Could not initialize SDL\n”);
return 1;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);

SDL_DisplayMode displayMode;
SDL_GetDesktopDisplayMode(0, &displayMode);

SDL_Window* window = SDL_CreateWindow("TESTGL", 0, 0, 640, 1150,

SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE);

SDL_GLContext gl = SDL_GL_CreateContext(window);

glClearColor ( 1.0, 0.0, 0.0, 1.0 );
glClear ( GL_COLOR_BUFFER_BIT );
SDL_GL_SwapWindow(window);

SDL_Event event;
auto done = false;
while (!done)
{

    SDL_PumpEvents();
    while (SDL_PollEvent(&event))
    {

        switch (event.type)
        {
            case SDL_QUIT:
                done = true;
                break;
        }
    }
}

SDL_GL_DeleteContext(gl);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;

}

Thanks,

Alex

It seems like you are busy spinning in the while loop. Have you considered
putting either a
SDL_Delay() call or (assuming vsync is turned on) glClear() +
SDL_GL_SwapWindow() inside
to give back some time to the CPU?
You could also use SDL_WaitEvent instead of polling, which might sleep your
process until
new events arrive.

Thanks for the reply. Which of these techniques would you recommend most. I
was following this guide for Opengl 3.3 and I tried to adapt it for IOS but
it seems it gives different behaviour. Do you have a sample code block on
how to use vsync properly with IOS and Opengl ES ?

http://www.opengl.org/wiki/Tutorial1:Creating_a_Cross_Platform_OpenGL_3.2_Context_in_SDL%28C_/_SDL%29

Alex

2014-05-06 10:29 GMT+02:00 Alexander Chaliovski :

Thanks for the reply. Which of these techniques would you recommend most.
I was following this guide for Opengl 3.3 and I tried to adapt it for IOS
but it seems it gives different behaviour. Do you have a sample code block
on how to use vsync properly with IOS and Opengl ES ?

http://www.opengl.org/wiki/Tutorial1:Creating_a_Cross_Platform_OpenGL_3.2_Context_in_SDL%28C_/_SDL%29

After a bit of googling it turns out that vsync is permanently enabled on
iOS, so you don’t even
have to turn it on anywhere. Just make sure to redraw the screen, and the
GL_SwapWindow()
should block long enough for other processes to utilize the CPU (and hence
decrease the CPU
usage of your app).
Basically, redraw the screen in you while loop. The simplest code for that
would just be a
glClear() + SDL_GL_SwapWindow().

Thanks a lot I will try it out tonight.

Yeah you are right. So if I stabilize it to 24 FPS it takes around 90% CPU
for a simple model display on the simulator. I wonder how faster it would
be on the actual device. As I need some resources for physics as well.

The iPhone/iPad Simulator uses software rendering I think, so it uses tons of CPU time and gets poor framerates compared to real iPhones and iPads.On May 6, 2014, at 5:46 PM, Alexander Chaliovski wrote:

Yeah you are right. So if I stabilize it to 24 FPS it takes around 90% CPU for a simple model display on the simulator. I wonder how faster it would be on the actual device. As I need some resources for physics as well.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org