mirror of https://github.com/buggins/dlangui.git
fix OpenGL initialization in SDL app
This commit is contained in:
parent
d4def4cadd
commit
903fafc590
|
|
@ -21,10 +21,11 @@ module dlangui.graphics.glsupport;
|
||||||
version(USE_OPENGL):
|
version(USE_OPENGL):
|
||||||
|
|
||||||
import dlangui.core.logger;
|
import dlangui.core.logger;
|
||||||
private import derelict.opengl3.gl3;
|
import derelict.opengl3.gl3;
|
||||||
private import dlangui.core.types;
|
import dlangui.core.types;
|
||||||
private import std.conv;
|
import std.conv;
|
||||||
private import std.string;
|
import std.string;
|
||||||
|
import std.array;
|
||||||
|
|
||||||
// utility function to fill 4-float array of vertex colors with converted CR 32bit color
|
// utility function to fill 4-float array of vertex colors with converted CR 32bit color
|
||||||
private void LVGLFillColor(uint color, float * buf, int count) {
|
private void LVGLFillColor(uint color, float * buf, int count) {
|
||||||
|
|
@ -78,24 +79,28 @@ class GLProgram {
|
||||||
protected bool initialized;
|
protected bool initialized;
|
||||||
protected bool error;
|
protected bool error;
|
||||||
protected string glslversion;
|
protected string glslversion;
|
||||||
|
protected int glslversionInt;
|
||||||
|
protected char[] glslversionString;
|
||||||
this() {
|
this() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void compatibilityFixes(ref char[] code) {
|
||||||
|
if (glslversionInt < 150)
|
||||||
|
code = replace(code, " texture(", " texture2D(");
|
||||||
|
}
|
||||||
|
|
||||||
private GLuint compileShader(string src, GLuint type) {
|
private GLuint compileShader(string src, GLuint type) {
|
||||||
import core.stdc.stdlib;
|
import core.stdc.stdlib;
|
||||||
import std.string;
|
import std.string;
|
||||||
|
|
||||||
char[] versionLine;
|
char[] sourceCode;
|
||||||
versionLine ~= "#version ";
|
sourceCode ~= "#version ";
|
||||||
foreach(ch; glslversion) {
|
sourceCode ~= glslversionString;
|
||||||
if (ch >= '0' && ch <= '9')
|
sourceCode ~= "\n";
|
||||||
versionLine ~= ch;
|
sourceCode ~= src;
|
||||||
else if (ch != '.')
|
compatibilityFixes(sourceCode);
|
||||||
break;
|
|
||||||
}
|
|
||||||
versionLine ~= "\n\n";
|
|
||||||
char[] sourceCode = versionLine ~ src;
|
|
||||||
Log.d("compileShader glsl=", glslversion, " type:", (type == GL_VERTEX_SHADER ? "GL_VERTEX_SHADER" : (type == GL_FRAGMENT_SHADER ? "GL_FRAGMENT_SHADER" : "UNKNOWN")), " code:\n", 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
|
GLuint shader = glCreateShader(type);//GL_VERTEX_SHADER
|
||||||
const char * psrc = sourceCode.toStringz;
|
const char * psrc = sourceCode.toStringz;
|
||||||
GLuint len = cast(uint)sourceCode.length;
|
GLuint len = cast(uint)sourceCode.length;
|
||||||
|
|
@ -120,8 +125,20 @@ class GLProgram {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool compile() {
|
bool compile() {
|
||||||
glslversion = std.string.fromStringz(glGetString(GL_SHADING_LANGUAGE_VERSION)).dup;
|
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);
|
vertexShader = compileShader(vertexSource, GL_VERTEX_SHADER);
|
||||||
fragmentShader = compileShader(fragmentSource, GL_FRAGMENT_SHADER);
|
fragmentShader = compileShader(fragmentSource, GL_FRAGMENT_SHADER);
|
||||||
if (!vertexShader || !fragmentShader) {
|
if (!vertexShader || !fragmentShader) {
|
||||||
|
|
|
||||||
|
|
@ -100,6 +100,20 @@ class SDLWindow : Window {
|
||||||
private SDL_GLContext _context;
|
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;
|
protected uint _flags;
|
||||||
bool create(uint flags) {
|
bool create(uint flags) {
|
||||||
if (!_dx)
|
if (!_dx)
|
||||||
|
|
@ -142,14 +156,23 @@ class SDLWindow : Window {
|
||||||
Log.e("SDL2: Failed to create window");
|
Log.e("SDL2: Failed to create window");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
version(USE_OPENGL) {
|
version(USE_OPENGL) {
|
||||||
if (_enableOpengl) {
|
if (_enableOpengl) {
|
||||||
Log.i("Trying to create OpenGL 3.2 context");
|
Log.i("Trying to create OpenGL 3.2 context");
|
||||||
_context = SDL_GL_CreateContext(_win); // Create the actual context and make it current
|
_context = SDL_GL_CreateContext(_win); // Create the actual context and make it current
|
||||||
if (!_context) {
|
if (!_context) {
|
||||||
Log.e("SDL_GL_CreateContext failed: ", fromStringz(SDL_GetError()));
|
Log.e("SDL_GL_CreateContext failed: ", fromStringz(SDL_GetError()));
|
||||||
_enableOpengl = false;
|
Log.w("trying other versions of OpenGL");
|
||||||
Log.w("OpenGL support is disabled");
|
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) {
|
if (_context && !_gl3Reloaded) {
|
||||||
DerelictGL3.reload();
|
DerelictGL3.reload();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue