Failed to draw anything on OpenGL + SDL2 + KMSDRM + Raspberry Pi

I have asked this question on stackoverflow and Raspberry Pi forum, but not obtained any clue. So I ask it here to seek for more specific suggestions.

I made a very simple test program below that fills grey by glClear:

#include <SDL.h>

#include <cstring>
#include <iostream>
#include <thread>

#include <SDL_opengles2.h>

using namespace std::chrono_literals;
using std::clog;
using std::endl;

int main()
{
    auto init_re = SDL_VideoInit("KMSDRM");
    if (init_re != 0)
        exit(EXIT_FAILURE);

    SDL_Window *window = SDL_CreateWindow("SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480,
                                          SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
    if (nullptr == window)
    {
        std::cerr << "SDL_CreateWindow(): " << SDL_GetError() << '\n';
        exit(EXIT_FAILURE);
    }

    auto gl = SDL_GL_CreateContext(window);
    clog << "create OpenGL context " << gl << endl;
    auto make_current_re = SDL_GL_MakeCurrent(window, gl);
    clog << "make current ctx returned " << make_current_re << endl;

    glClearColor(0.5, 0.5, 0.5, 1.0);
    size_t frame_count = 0;
    while (true)
    {
        frame_count += 1;
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        clog << "frame " << frame_count << endl;
        std::this_thread::sleep_for(2ms);
    }

    SDL_GL_DeleteContext(gl);
    SDL_DestroyWindow(window);
    SDL_Quit();
}

As a result, all objects are created without error and frame logs are printed. However, it does not draw a 640x480 piece of grey, only showing a mouse arrow-like stuff on top left of screen.

I have tried to set window coordinates to 0, 0, 1920, 1080 which is the physical size of my attached screen, nothing changed.

I have tried to print OpenGL versions after context creation, it gives me OpenGL ES 2.0 Mesa 20.3.5 and GLSL OpenGL ES GLSL ES 1.0.16 which seems valid.

According to suggestions given by other ones, I have tested MESA’s kmscube program and it runs properly, indicating I have a working KMS environment.

Below are my environments:

  • Raspberry Pi 3B.
  • 32-bit Raspbian bullseye.
  • SDL2 installed from system repository.
  • vc4-kms-v3d driver enabled, 256 MB assigned as video memory.
  • Whole x11 uninstalled.

I’m not an expert on OpenGL so forgive me if I say something stupid, but aren’t gl commands (like glClear) buffered so that you need to call a special function (perhaps SDL_GL_SwapWindow) before you can see the result on the screen?

2 Likes

Exactly that. Add a call to SDL_GL_SwapWindow().

1 Like

I LOVED YOU!!! I’m so stupid that this simple and silly problem has troubled me for more than a week!!!