Support OpenGL ES 3.0

This adds `USE_GLES` compile-time flag. If it's defined, gfx_init()
requests an OpenGL ES >=3.0 context.

This also makes the following changes to make it work with OpenGL ES:

- The USE_GLES flag also switches shader's #version directive, which is
  now provided separately from the .glsl file.
- Implicit conversion between int and float is not supported in GLSL ES.
- Use GL_RGB in gfx_copy_main_surface. Because main_surface is in GL_RGB
  format, using GL_RGBA generates GL_INVALID_OPERATION in OpenGL ES.
This commit is contained in:
kichikuou
2021-06-20 17:12:35 +09:00
parent 77a13515eb
commit da5ac4a26c
20 changed files with 31 additions and 49 deletions
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform sampler2D tex;
uniform float threshold;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform sampler2D tex;
uniform vec4 color;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform sampler2D tex;
in vec2 tex_coord;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform sampler2D tex;
uniform vec4 color;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform sampler2D tex;
in vec2 tex_coord;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform sampler2D tex;
uniform vec4 color;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform sampler2D tex;
uniform float threshold;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform sampler2D tex;
uniform float threshold;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform sampler2D tex;
uniform float alpha_mod;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform mat4 local_transform;
uniform mat4 view_transform;
uniform mat4 proj_transform;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform samplerCube tex;
in vec3 tex_coord;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform mat4 view_transform;
uniform mat4 proj_transform;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform vec4 color;
in vec2 tex_coord;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform sampler2D tex;
uniform float threshold;
uniform vec4 color;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform sampler2D tex;
uniform float threshold;
uniform vec4 color;
+2 -4
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform sampler2D tex;
uniform vec2 bot_left;
uniform vec2 top_right;
@@ -29,11 +27,11 @@ float point_in_rect(vec2 p, vec2 bot_left, vec2 top_right) {
}
void main() {
ivec2 size = textureSize(tex, 0);
vec2 size = vec2(textureSize(tex, 0));
vec2 tex_bot_left = bot_left / size;
vec2 tex_top_right = top_right / size;
vec4 texel = texture(tex, tex_coord);
float t = point_in_rect(tex_coord, tex_bot_left, tex_top_right);
frag_color = t * texel + (1 - t) * vec4(0, 0, 0, 0);
frag_color = t * texel + (1.0 - t) * vec4(0, 0, 0, 0);
}
+3 -5
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform sampler2D tex;
uniform vec2 bot_left;
uniform vec2 top_right;
@@ -29,12 +27,12 @@ float point_in_rect(vec2 p, vec2 bot_left, vec2 top_right) {
}
void main() {
ivec2 size = textureSize(tex, 0);
vec2 size = vec2(textureSize(tex, 0));
vec2 tex_bot_left = bot_left / size;
vec2 tex_top_right = top_right / size;
vec4 texel = texture(tex, tex_coord);
texel.a = 1;
texel.a = 1.0;
float t = point_in_rect(tex_coord, tex_bot_left, tex_top_right);
frag_color = t * texel + (1 - t) * vec4(0, 0, 0, 0);
frag_color = t * texel + (1.0 - t) * vec4(0, 0, 0, 0);
}
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform sampler2D tex;
uniform float alpha_mod;
-2
View File
@@ -14,8 +14,6 @@
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#version 140
uniform mat4 world_transform;
uniform mat4 view_transform;
+26 -6
View File
@@ -32,6 +32,19 @@
struct sdl_private sdl;
/*
* Define this to target OpenGL ES 3.0.
*/
//#define USE_GLES
static const GLchar glsl_preamble[] =
#ifdef USE_GLES
"#version 300 es\n"
"precision highp float;\n";
#else
"#version 140\n";
#endif
/*
* Transform from the window coordinate system -> clip-space.
*
@@ -84,16 +97,17 @@ static GLuint load_shader_file(const char *path, GLenum type)
{
GLint shader_compiled;
GLuint shader;
const GLchar *source;
source = read_shader_file(path);
const GLchar *source[2] = {
glsl_preamble,
read_shader_file(path)
};
shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glShaderSource(shader, 2, source, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_compiled);
if (!shader_compiled)
ERROR("Failed to compile shader: %s", path);
free((char*)source);
free((char*)source[1]);
return shader;
}
@@ -164,9 +178,15 @@ int gfx_init(void)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
ERROR("SDL_Init failed: %s", SDL_GetError());
#ifdef USE_GLES
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
#endif
sdl.format = SDL_AllocFormat(SDL_PIXELFORMAT_RGBA32);
sdl.window = SDL_CreateWindow("XSystem4",
@@ -455,7 +475,7 @@ void gfx_init_texture_blank(struct texture *t, int w, int h)
void gfx_copy_main_surface(struct texture *dst)
{
init_texture(dst, main_surface.w, main_surface.h);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, main_surface.w, main_surface.h, 0);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, main_surface.w, main_surface.h, 0);
}
void gfx_delete_texture(struct texture *t)