From 903fafc590e8a1579364c7d8958f8c3a41de5dd7 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Tue, 17 Mar 2015 13:40:07 +0300 Subject: [PATCH] fix OpenGL initialization in SDL app --- src/dlangui/graphics/glsupport.d | 47 ++++++++++++++++++++---------- src/dlangui/platforms/sdl/sdlapp.d | 27 +++++++++++++++-- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index 05e4b680..ba58e69b 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -21,10 +21,11 @@ module dlangui.graphics.glsupport; version(USE_OPENGL): import dlangui.core.logger; -private import derelict.opengl3.gl3; -private import dlangui.core.types; -private import std.conv; -private import std.string; +import derelict.opengl3.gl3; +import dlangui.core.types; +import std.conv; +import std.string; +import std.array; // utility function to fill 4-float array of vertex colors with converted CR 32bit color private void LVGLFillColor(uint color, float * buf, int count) { @@ -78,24 +79,28 @@ class GLProgram { protected bool initialized; protected bool error; protected string glslversion; + protected int glslversionInt; + protected char[] glslversionString; this() { } + + private void compatibilityFixes(ref char[] code) { + if (glslversionInt < 150) + code = replace(code, " texture(", " texture2D("); + } + private GLuint compileShader(string src, GLuint type) { import core.stdc.stdlib; import std.string; - char[] versionLine; - versionLine ~= "#version "; - foreach(ch; glslversion) { - if (ch >= '0' && ch <= '9') - versionLine ~= ch; - else if (ch != '.') - break; - } - versionLine ~= "\n\n"; - char[] sourceCode = versionLine ~ src; + char[] sourceCode; + sourceCode ~= "#version "; + sourceCode ~= glslversionString; + sourceCode ~= "\n"; + sourceCode ~= src; + compatibilityFixes(sourceCode); + Log.d("compileShader glsl=", glslversion, " type:", (type == GL_VERTEX_SHADER ? "GL_VERTEX_SHADER" : (type == GL_FRAGMENT_SHADER ? "GL_FRAGMENT_SHADER" : "UNKNOWN")), " code:\n", sourceCode); - GLuint shader = glCreateShader(type);//GL_VERTEX_SHADER const char * psrc = sourceCode.toStringz; GLuint len = cast(uint)sourceCode.length; @@ -120,8 +125,20 @@ class GLProgram { return 0; } } + bool compile() { glslversion = std.string.fromStringz(glGetString(GL_SHADING_LANGUAGE_VERSION)).dup; + + glslversionString.length = 0; + glslversionInt = 0; + foreach(ch; glslversion) { + if (ch >= '0' && ch <= '9') { + glslversionString ~= ch; + glslversionInt = glslversionInt * 10 + (ch - '0'); + } else if (ch != '.') + break; + } + vertexShader = compileShader(vertexSource, GL_VERTEX_SHADER); fragmentShader = compileShader(fragmentSource, GL_FRAGMENT_SHADER); if (!vertexShader || !fragmentShader) { diff --git a/src/dlangui/platforms/sdl/sdlapp.d b/src/dlangui/platforms/sdl/sdlapp.d index 1714c5d3..cd7c08a4 100644 --- a/src/dlangui/platforms/sdl/sdlapp.d +++ b/src/dlangui/platforms/sdl/sdlapp.d @@ -100,6 +100,20 @@ class SDLWindow : Window { private SDL_GLContext _context; } + version(USE_OPENGL) { + protected bool createContext(int versionMajor, int versionMinor) { + Log.i("Trying to create OpenGL ", versionMajor, ".", versionMinor, " context"); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, versionMajor); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, versionMinor); + _context = SDL_GL_CreateContext(_win); // Create the actual context and make it current + if (!_context) + Log.e("SDL_GL_CreateContext failed: ", fromStringz(SDL_GetError())); + else + Log.i("Created successfully"); + return _context !is null; + } + } + protected uint _flags; bool create(uint flags) { if (!_dx) @@ -142,14 +156,23 @@ class SDLWindow : Window { Log.e("SDL2: Failed to create window"); return false; } + version(USE_OPENGL) { if (_enableOpengl) { Log.i("Trying to create OpenGL 3.2 context"); _context = SDL_GL_CreateContext(_win); // Create the actual context and make it current if (!_context) { Log.e("SDL_GL_CreateContext failed: ", fromStringz(SDL_GetError())); - _enableOpengl = false; - Log.w("OpenGL support is disabled"); + Log.w("trying other versions of OpenGL"); + bool flg = false; + flg = flg || createContext(3, 3); + flg = flg || createContext(3, 1); + flg = flg || createContext(4, 0); + flg = flg || createContext(2, 1); + if (!flg) { + _enableOpengl = false; + Log.w("OpenGL support is disabled"); + } } if (_context && !_gl3Reloaded) { DerelictGL3.reload();