117 #ifdef BEATMUP_OPENGLVERSION_GLES
119 if ((eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY)
123 if (!eglInitialize(eglDisplay, 0, 0)) {
125 #ifdef BEATMUP_GLES_ALLOW_DRM_FALLBACK
129 const char* DRI_DEVICE = std::getenv(
"BEATMUP_DRI_DEVICE");
130 static const char* DEFAULT_DRI_DEVICE =
"/dev/dri/card1";
131 DRM::Device device(DRI_DEVICE ? DRI_DEVICE : DEFAULT_DRI_DEVICE);
140 eglDisplay = eglGetDisplay(gbmDevice.getPointer());
146 if (!eglInitialize(eglDisplay, 0, 0)) {
151 this->drmDevice = std::move(device);
152 this->drmConnector = std::move(connector);
153 this->drmCrtc = std::move(crtc);
154 this->gbmDevice = std::move(gbmDevice);
155 this->gbmSurface = std::move(gbmSurface);
157 auto err = eglGetError();
158 if (err == EGL_NOT_INITIALIZED)
166 eglBindAPI(EGL_OPENGL_ES_API);
169 static const int CONFIG_ATTRIBUTES_LEN = 5;
170 EGLint configAttributes[CONFIG_ATTRIBUTES_LEN] = {
171 #ifdef BEATMUP_OPENGLVERSION_GLES20
172 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
173 #elif defined BEATMUP_OPENGLVERSION_GLES31
174 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR,
176 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
180 #ifdef BEATMUP_GLES_ALLOW_DRM_FALLBACK
183 configAttributes[CONFIG_ATTRIBUTES_LEN - 3] = EGL_NONE;
188 int totalConfigCount;
189 eglGetConfigs(eglDisplay,
nullptr, 0, &totalConfigCount);
190 std::vector<EGLConfig> configs;
191 configs.resize(totalConfigCount);
195 if (!eglChooseConfig(eglDisplay, configAttributes, configs.data(), totalConfigCount, &numConfigs))
199 #ifdef BEATMUP_GLES_ALLOW_DRM_FALLBACK
203 for (
int i = 0; i < totalConfigCount && !found; ++i) {
205 if (EGL_TRUE != eglGetConfigAttrib(eglDisplay, configs[i], EGL_RED_SIZE, &
val) ||
val != 8)
207 if (EGL_TRUE != eglGetConfigAttrib(eglDisplay, configs[i], EGL_GREEN_SIZE, &
val) ||
val != 8)
209 if (EGL_TRUE != eglGetConfigAttrib(eglDisplay, configs[i], EGL_BLUE_SIZE, &
val) ||
val != 8)
211 if (EGL_TRUE != eglGetConfigAttrib(eglDisplay, configs[i], EGL_ALPHA_SIZE, &
val) ||
val != 8)
213 eglConfig = configs[i];
217 throw GpuOperationError(
"EGL/DRM: no config matching the required surface format");
222 eglConfig = configs[0];
227 EGLint contextAttributes[] = {
228 EGL_CONTEXT_CLIENT_VERSION,
229 #ifdef BEATMUP_OPENGLVERSION_GLES20
231 #elif defined BEATMUP_OPENGLVERSION_GLES31
236 eglContext = eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, contextAttributes);
237 if (eglContext == EGL_NO_CONTEXT)
242 eglDefaultSurface = eglSurface = EGL_NO_SURFACE;
243 #ifdef BEATMUP_GLES_ALLOW_DRM_FALLBACK
245 eglDefaultSurface = eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, gbmSurface.getPointer(),
nullptr);
250 EGLint surfaceAttributes[] = {
256 eglDefaultSurface = eglSurface = eglCreatePbufferSurface(eglDisplay, eglConfig, surfaceAttributes);
259 if (eglSurface == EGL_NO_SURFACE)
260 throw GpuOperationError(
"EGL: window surface creation failed when init", eglGetError());
263 if (!eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext))
266 #ifdef BEATMUP_OPENGLVERSION_GLES20
272 #elif BEATMUP_PLATFORM_WINDOWS
273 PIXELFORMATDESCRIPTOR pfd;
274 memset(&pfd, 0,
sizeof(PIXELFORMATDESCRIPTOR));
275 pfd.nSize =
sizeof(PIXELFORMATDESCRIPTOR);
277 pfd.dwFlags = PFD_SUPPORT_OPENGL;
278 pfd.iPixelType = PFD_TYPE_RGBA;
281 pfd.iLayerType = PFD_MAIN_PLANE;
282 hwnd = CreateWindowEx(WS_EX_TOOLWINDOW,
288 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
289 0, 0, 1, 1, 0, 0, GetModuleHandle(NULL), 0);
293 ShowWindow(hwnd, SW_HIDE);
294 HDC hdc = GetDC(hwnd);
297 hglrc = wglCreateContext(hdc);
298 wglMakeCurrent(hdc, hglrc);
299 if (!wglGetCurrentContext())
303 glewExperimental = GL_TRUE;
304 GLenum err = glewInit();
320 static int bootstapVisualAttrs[] = { GLX_RGBA, None };
321 XVisualInfo* vi = glXChooseVisual(
xDisplay, 0, bootstapVisualAttrs);
326 glewExperimental = GL_TRUE;
327 GLenum err = glewInit();
334 static int visualAttrs[] = {
335 GLX_DOUBLEBUFFER,
false,
340 visualAttrs, &numFbc);
345 static int pbufferAttrs[] = {
346 GLX_PBUFFER_WIDTH, 1,
347 GLX_PBUFFER_HEIGHT, 1,
354 vi = glXGetVisualFromFBConfig(
xDisplay, config[0]);
361 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &
glLimits.maxTextureImageUnits);
362 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &
glLimits.maxFragmentUniformVectors);
363 #ifndef BEATMUP_OPENGLVERSION_GLES20
364 glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 0,
glLimits.maxWorkGroupCount + 0);
365 glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 1,
glLimits.maxWorkGroupCount + 1);
366 glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 2,
glLimits.maxWorkGroupCount + 2);
367 glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 0,
glLimits.maxWorkGroupSize + 0);
368 glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 1,
glLimits.maxWorkGroupSize + 1);
369 glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 2,
glLimits.maxWorkGroupSize + 2);
370 glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &
glLimits.maxTotalWorkGroupSize);
371 glGetIntegerv(GL_MAX_COMPUTE_SHARED_MEMORY_SIZE, (GLint*)&
glLimits.maxSharedMemSize);
382 *vendor = (
char*)glGetString(GL_VENDOR),
383 *
renderer = (
char*)glGetString(GL_RENDERER),
384 *glslVersionStr = (
char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
385 BEATMUP_DEBUG_I(
"__________________________________________________________");
387 #ifndef BEATMUP_OPENGLVERSION_GLES20
394 BEATMUP_DEBUG_I(
"__________________________________________________________");
401 const char* FORCED_GLSL_VERSION = std::getenv(
"BEATMUP_GLSL_VERSION");
402 std::string versionStr(FORCED_GLSL_VERSION ? FORCED_GLSL_VERSION : (
const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
405 static const std::string ES_PREFIX =
"OpenGL ES GLSL ES ";
407 isGlEs = versionStr.substr(0, ES_PREFIX.length()) == ES_PREFIX;
409 versionStr = versionStr.substr(ES_PREFIX.length());
412 auto spacePos = versionStr.find(
" ");
413 if (spacePos == std::string::npos)
414 spacePos = versionStr.length();
415 versionStr = versionStr.substr(0, spacePos);
416 auto dotPos = versionStr.find(
".");
417 if (dotPos == std::string::npos)
418 throw GL::GLException(
"Cannot determine GLSL version from string '" + versionStr +
"'");
419 const int majVer = std::stoi(versionStr.substr(0, dotPos));
420 const int minVer = std::stoi(versionStr.substr(dotPos + 1));
426 GL::GLException::check(
"initialization");
437 glEnableVertexAttribArray(GraphicPipeline::ATTRIB_VERTEX_COORD);
438 glEnableVertexAttribArray(GraphicPipeline::ATTRIB_TEXTURE_COORD);
439 glVertexAttribPointer(GraphicPipeline::ATTRIB_VERTEX_COORD, 2, GL_FLOAT, GL_FALSE,
sizeof(VertexAttribBufferElement),
nullptr);
440 glVertexAttribPointer(GraphicPipeline::ATTRIB_TEXTURE_COORD, 2, GL_FLOAT, GL_FALSE,
sizeof(VertexAttribBufferElement), (
void*)(2 *
sizeof(GLfloat)));
441 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
445 glDisable(GL_DEPTH_TEST);
446 glDisable(GL_CULL_FACE);
447 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
448 glBlendColor(1.0f, 1.0f, 1.0f, 1.0f);
450 GL::GLException::check(
"initial GPU setup");
bool isGlEs
if true, the use OpenGL context is GL ES-compliant
bool isRectangularTextureCoordinates
if true, the texture coordinates is a rectangle
void switchMode(GraphicPipeline::Mode mode)
GLuint hVertexAttribBuffer
buffer used when rendering
VertexAttribBufferElement vertexAttribBuffer[4]
struct GraphicPipeline::Impl::@15 glLimits
int glslVersion
GLSL language version to put into shaders code, e.g. 100 for 1.00.
#define BEATMUP_DEBUG_I(...)
jlong jint jint jint jint pixelFormat
return(jlong) new Beatmup jlong jstring jint val
Beatmup::SceneRenderer * renderer