Skip to content

Commit 0b135ad

Browse files
j-w-c-bSiegeLord
authored andcommitted
Add support for OpenGL 3 displays on macos
When creating a mac display wth the ALLEGRO_OPENGL_3_0 flag, set the pixel attribute NSOpenGLPFAOpenGLProfile to NSOpenGLProfileVersion3_2Core to enable modern OpenGL. This conflicts with NSOpenGLPFAWindow, so that is dropped. Fix checking for ALLEGRO_RENDER_METHOD in the required options to enable NSOpenGLPFAAccelerated. I'm not sure if that makes a difference, but the old code was using the option as a value and not a bit offset. Duplicate the shaders in GLSL 3.3+ syntax (adding a _gl3 suffix to the static variables). Check the shader language version, and select the _gl3 versions when using a version != 1.20 on macos. When compiling the shaders on macos, check for the supported shader version. If != 1.20, insert a #version line based on the value returned by the OpenGL context.
1 parent dbca3d1 commit 0b135ad

File tree

4 files changed

+128
-7
lines changed

4 files changed

+128
-7
lines changed

src/macosx/osxgl.m

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,6 @@ static void osx_set_opengl_pixelformat_attributes(ALLEGRO_DISPLAY_OSX_WIN *dpy)
802802
// Take over the screen.
803803
*a = NSOpenGLPFAScreenMask; a++;
804804
*a = CGDisplayIDToOpenGLDisplayMask(dpy->display_id); a++;
805-
} else {
806-
*a = NSOpenGLPFAWindow; a++;
807805
}
808806

809807
/* Find the requested colour depth */
@@ -881,9 +879,21 @@ static void osx_set_opengl_pixelformat_attributes(ALLEGRO_DISPLAY_OSX_WIN *dpy)
881879
/* Accelerated is always preferred, so we only set this for required not
882880
* for suggested.
883881
*/
884-
if (extras->required & ALLEGRO_RENDER_METHOD) {
882+
if (extras->required & (1UL << ALLEGRO_RENDER_METHOD)) {
885883
*a++ = NSOpenGLPFAAccelerated;
886884
}
885+
/*
886+
* OpenGL 3+ support
887+
* Newer versions of macos support modern OpenGL, but won't create a view
888+
* that supports it unless explicitly requested to. Note that even though
889+
* this looks like 3.2 core, it is really >= 3.2, and it is quite strict
890+
* about removing support for legacy OpenGL features, and requires versioned
891+
* shaders.
892+
*/
893+
if (dpy->parent.flags & ALLEGRO_OPENGL_3_0) {
894+
*a++ = NSOpenGLPFAOpenGLProfile;
895+
*a++ = (NSOpenGLPixelFormatAttribute) NSOpenGLProfileVersion3_2Core;
896+
}
887897
}
888898

889899

src/opengl/ogl_shader.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,22 @@ static bool glsl_attach_shader_source(ALLEGRO_SHADER *shader,
124124
if ((*handle) == 0) {
125125
return false;
126126
}
127+
#ifdef ALLEGRO_MACOSX
128+
{
129+
const char *version_string = glGetString(GL_SHADING_LANGUAGE_VERSION);
130+
int version[2];
131+
sscanf(version_string, "%d.%d", &version[0], &version[1]);
132+
char version_line[] = "#version XXXXXXXXXX";
133+
ASSERT(version[0] < 10 && version[1] < 100);
134+
snprintf(version_line, sizeof(version_line), "#version %d%d\n", version[0], version[1]);
135+
136+
const char *sources[] = {version_line, source};
137+
138+
glShaderSource(*handle, 2, sources, NULL);
139+
}
140+
#else
127141
glShaderSource(*handle, 1, &source, NULL);
142+
#endif
128143
glCompileShader(*handle);
129144
glGetShaderiv(*handle, GL_COMPILE_STATUS, &status);
130145
if (status == 0) {

src/shader.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,24 +415,36 @@ char const *al_get_default_shader_source(ALLEGRO_SHADER_PLATFORM platform,
415415
ALLEGRO_SHADER_TYPE type)
416416
{
417417
(void)type;
418+
bool use_gl3_shader = false;
419+
#ifdef ALLEGRO_MACOSX
420+
{
421+
/* Apple's glsl implementation supports either 1.20 shaders, or strictly
422+
* versioned 3.2+ shaders which do not use deprecated features.
423+
*/
424+
const char * shader_language = glGetString(GL_SHADING_LANGUAGE_VERSION);
425+
if (strcmp(shader_language, "1.20") != 0) {
426+
use_gl3_shader = true;
427+
}
428+
}
429+
#endif
418430
switch (resolve_platform(al_get_current_display(), platform)) {
419431
case ALLEGRO_SHADER_GLSL:
420432
#ifdef ALLEGRO_CFG_SHADER_GLSL
421433
switch (type) {
422434
case ALLEGRO_VERTEX_SHADER:
423-
return default_glsl_vertex_source;
435+
return use_gl3_shader ? default_glsl_vertex_source_gl3 : default_glsl_vertex_source;
424436
case ALLEGRO_PIXEL_SHADER:
425-
return default_glsl_pixel_source;
437+
return use_gl3_shader ? default_glsl_pixel_source_gl3 : default_glsl_pixel_source;
426438
}
427439
#endif
428440
break;
429441
case ALLEGRO_SHADER_GLSL_MINIMAL:
430442
#ifdef ALLEGRO_CFG_SHADER_GLSL
431443
switch (type) {
432444
case ALLEGRO_VERTEX_SHADER:
433-
return default_glsl_vertex_source;
445+
return use_gl3_shader ? default_glsl_vertex_source_gl3 : default_glsl_vertex_source;
434446
case ALLEGRO_PIXEL_SHADER:
435-
return default_glsl_minimal_pixel_source;
447+
return use_gl3_shader ? default_glsl_minimal_pixel_source_gl3 : default_glsl_minimal_pixel_source;
436448
}
437449
#endif
438450
break;

src/shader_source.inc

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,90 @@ static const char *default_glsl_minimal_pixel_source =
9898
" gl_FragColor = c;\n"
9999
"}\n";
100100

101+
static const char *default_glsl_vertex_source_gl3 =
102+
"in vec4 " ALLEGRO_SHADER_VAR_POS ";\n"
103+
"in vec4 " ALLEGRO_SHADER_VAR_COLOR ";\n"
104+
"in vec2 " ALLEGRO_SHADER_VAR_TEXCOORD ";\n"
105+
"uniform mat4 " ALLEGRO_SHADER_VAR_PROJVIEW_MATRIX ";\n"
106+
"uniform bool " ALLEGRO_SHADER_VAR_USE_TEX_MATRIX ";\n"
107+
"uniform mat4 " ALLEGRO_SHADER_VAR_TEX_MATRIX ";\n"
108+
"out vec4 varying_color;\n"
109+
"out vec2 varying_texcoord;\n"
110+
"void main()\n"
111+
"{\n"
112+
" varying_color = " ALLEGRO_SHADER_VAR_COLOR ";\n"
113+
" if (" ALLEGRO_SHADER_VAR_USE_TEX_MATRIX ") {\n"
114+
" vec4 uv = " ALLEGRO_SHADER_VAR_TEX_MATRIX " * vec4(" ALLEGRO_SHADER_VAR_TEXCOORD ", 0, 1);\n"
115+
" varying_texcoord = vec2(uv.x, uv.y);\n"
116+
" }\n"
117+
" else\n"
118+
" varying_texcoord = " ALLEGRO_SHADER_VAR_TEXCOORD";\n"
119+
" gl_Position = " ALLEGRO_SHADER_VAR_PROJVIEW_MATRIX " * " ALLEGRO_SHADER_VAR_POS ";\n"
120+
"}\n";
121+
122+
static const char *default_glsl_pixel_source_gl3 =
123+
"#ifdef GL_ES\n"
124+
"precision lowp float;\n"
125+
"#endif\n"
126+
"uniform sampler2D " ALLEGRO_SHADER_VAR_TEX ";\n"
127+
"uniform bool " ALLEGRO_SHADER_VAR_USE_TEX ";\n"
128+
"uniform bool " ALLEGRO_SHADER_VAR_ALPHA_TEST ";\n"
129+
"uniform int " ALLEGRO_SHADER_VAR_ALPHA_FUNCTION ";\n"
130+
"uniform float " ALLEGRO_SHADER_VAR_ALPHA_TEST_VALUE ";\n"
131+
"in vec4 varying_color;\n"
132+
"in vec2 varying_texcoord;\n"
133+
"layout(location = 0) out vec4 diffuseColor;\n"
134+
"\n"
135+
"bool alpha_test_func(float x, int op, float compare);\n"
136+
"\n"
137+
"void main()\n"
138+
"{\n"
139+
" vec4 c;\n"
140+
" if (" ALLEGRO_SHADER_VAR_USE_TEX ")\n"
141+
" c = varying_color * texture(" ALLEGRO_SHADER_VAR_TEX ", varying_texcoord);\n"
142+
" else\n"
143+
" c = varying_color;\n"
144+
" if (!" ALLEGRO_SHADER_VAR_ALPHA_TEST " || alpha_test_func(c.a, " ALLEGRO_SHADER_VAR_ALPHA_FUNCTION ", "
145+
ALLEGRO_SHADER_VAR_ALPHA_TEST_VALUE "))\n"
146+
" diffuseColor = c;\n"
147+
" else\n"
148+
" discard;\n"
149+
"}\n"
150+
"\n"
151+
"bool alpha_test_func(float x, int op, float compare)\n"
152+
"{\n"
153+
// Note: These must be aligned with the ALLEGRO_RENDER_FUNCTION enum values.
154+
" if (op == 0) return false;\n" // ALLEGRO_RENDER_NEVER
155+
" else if (op == 1) return true;\n" // ALLEGRO_RENDER_ALWAYS
156+
" else if (op == 2) return x < compare;\n" // ALLEGRO_RENDER_LESS
157+
" else if (op == 3) return x == compare;\n" // ALLEGRO_RENDER_EQUAL
158+
" else if (op == 4) return x <= compare;\n" // ALLEGRO_RENDER_LESS_EQUAL
159+
" else if (op == 5) return x > compare;\n" // ALLEGRO_RENDER_GREATER
160+
" else if (op == 6) return x != compare;\n" // ALLEGRO_RENDER_NOT_EQUAL
161+
" else if (op == 7) return x >= compare;\n" // ALLEGRO_RENDER_GREATER_EQUAL
162+
" return false;\n"
163+
"}\n";
164+
165+
static const char *default_glsl_minimal_pixel_source_gl3 =
166+
"#ifdef GL_ES\n"
167+
"precision lowp float;\n"
168+
"#endif\n"
169+
"uniform sampler2D " ALLEGRO_SHADER_VAR_TEX ";\n"
170+
"uniform bool " ALLEGRO_SHADER_VAR_USE_TEX ";\n"
171+
"in vec4 varying_color;\n"
172+
"in vec2 varying_texcoord;\n"
173+
"layout(location = 0) out vec4 diffuseColor;\n"
174+
"\n"
175+
"void main()\n"
176+
"{\n"
177+
" vec4 c;\n"
178+
" if (" ALLEGRO_SHADER_VAR_USE_TEX ")\n"
179+
" c = varying_color * texture2D(" ALLEGRO_SHADER_VAR_TEX ", varying_texcoord);\n"
180+
" else\n"
181+
" c = varying_color;\n"
182+
" diffuseColor = c;\n"
183+
"}\n";
184+
101185
#endif /* ALLEGRO_CFG_SHADER_GLSL */
102186

103187

0 commit comments

Comments
 (0)