Fixed OpenGLES shaders failing after renderer has been created Cache all the shaders up front, so we can verify that they won't silently fail at runtime. If compiling the fragment shaders with the precision hint fails, try again without specifying precision. Fixes https://github.com/libsdl-org/SDL/issues/6166 Fixes https://github.com/libsdl-org/SDL/issues/6174
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index 2585d43..492a0c1 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -495,30 +495,34 @@ GLES2_CacheShader(GLES2_RenderData *data, GLES2_ShaderType type, GLenum shader_t
{
GLuint id;
GLint compileSuccessful = GL_FALSE;
- int num_src = 0;
- const GLchar *shader_src_list[4] = { 0 };
- const GLchar *shader_body = (const GLchar *)GLES2_GetShader(type);
+ int attempt, num_src;
+ const GLchar *shader_src_list[2];
+ const GLchar *shader_body = GLES2_GetShader(type);
if (!shader_body) {
SDL_SetError("No shader body src");
return 0;
}
- if (shader_type == GL_FRAGMENT_SHADER) {
- if (data->texcoord_precision_hint != GLES2_SHADER_FRAGMENT_INCLUDE_UNDEF_PRECISION) {
- shader_src_list[num_src++] = (const GLchar*)GLES2_GetShaderInclude(GLES2_SHADER_FRAGMENT_INCLUDE_DEFAULT);
+ for (attempt = 0; attempt < 2 && !compileSuccessful; ++attempt) {
+ num_src = 0;
+ if (shader_type == GL_FRAGMENT_SHADER) {
+ if (attempt == 0) {
+ shader_src_list[num_src++] = GLES2_GetShaderInclude(data->texcoord_precision_hint);
+ } else {
+ shader_src_list[num_src++] = GLES2_GetShaderInclude(GLES2_SHADER_FRAGMENT_INCLUDE_UNDEF_PRECISION);
+ }
}
- shader_src_list[num_src++] = (const GLchar*)GLES2_GetShaderInclude(data->texcoord_precision_hint);
- }
- shader_src_list[num_src++] = shader_body;
+ shader_src_list[num_src++] = shader_body;
- SDL_assert(num_src < SDL_arraysize(shader_src_list));
+ SDL_assert(num_src <= SDL_arraysize(shader_src_list));
- /* Compile */
- id = data->glCreateShader(shader_type);
- data->glShaderSource(id, num_src, shader_src_list, NULL);
- data->glCompileShader(id);
- data->glGetShaderiv(id, GL_COMPILE_STATUS, &compileSuccessful);
+ /* Compile */
+ id = data->glCreateShader(shader_type);
+ data->glShaderSource(id, num_src, shader_src_list, NULL);
+ data->glCompileShader(id);
+ data->glGetShaderiv(id, GL_COMPILE_STATUS, &compileSuccessful);
+ }
if (!compileSuccessful) {
SDL_bool isstack = SDL_FALSE;
@@ -548,6 +552,27 @@ GLES2_CacheShader(GLES2_RenderData *data, GLES2_ShaderType type, GLenum shader_t
return id;
}
+static int GLES2_CacheShaders(GLES2_RenderData * data)
+{
+ int shader;
+
+ data->texcoord_precision_hint = GLES2_GetTexCoordPrecisionEnumFromHint();
+
+ for (shader = 0; shader < GLES2_SHADER_COUNT; ++shader) {
+ GLenum shader_type;
+
+ if (shader == GLES2_SHADER_VERTEX_DEFAULT) {
+ shader_type = GL_VERTEX_SHADER;
+ } else {
+ shader_type = GL_FRAGMENT_SHADER;
+ }
+ if (!GLES2_CacheShader(data, (GLES2_ShaderType) shader, shader_type)) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
static int
GLES2_SelectProgram(GLES2_RenderData *data, GLES2_ImageSource source, int w, int h)
{
@@ -2084,6 +2109,13 @@ GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
goto error;
}
+ if (GLES2_CacheShaders(data) < 0) {
+ SDL_GL_DeleteContext(data->context);
+ SDL_free(renderer);
+ SDL_free(data);
+ goto error;
+ }
+
#if __WINRT__
/* DLudwig, 2013-11-29: ANGLE for WinRT doesn't seem to work unless VSync
* is turned on. Not doing so will freeze the screen's contents to that
@@ -2107,8 +2139,6 @@ GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
data->debug_enabled = SDL_TRUE;
}
- data->texcoord_precision_hint = GLES2_GetTexCoordPrecisionEnumFromHint();
-
value = 0;
data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
renderer->info.max_texture_width = value;
diff --git a/src/render/opengles2/SDL_shaders_gles2.c b/src/render/opengles2/SDL_shaders_gles2.c
index c61dc83..4d48e36 100644
--- a/src/render/opengles2/SDL_shaders_gles2.c
+++ b/src/render/opengles2/SDL_shaders_gles2.c
@@ -32,35 +32,33 @@
* Vertex/fragment shader source *
*************************************************************************************************/
-static const Uint8 GLES2_Fragment_Include_Default[] = "\n\
- #version 130\n\
- precision mediump float;\n\
-";
-
-static const Uint8 GLES2_Fragment_Include_Best_Texture_Precision[] = "\n\
+static const char GLES2_Fragment_Include_Best_Texture_Precision[] = "\n\
#ifdef GL_FRAGMENT_PRECISION_HIGH\n\
#define SDL_TEXCOORD_PRECISION highp\n\
#else\n\
#define SDL_TEXCOORD_PRECISION mediump\n\
#endif\n\
+ precision mediump float;\n\
";
-static const Uint8 GLES2_Fragment_Include_Medium_Texture_Precision[] = "\n\
+static const char GLES2_Fragment_Include_Medium_Texture_Precision[] = "\n\
#define SDL_TEXCOORD_PRECISION mediump\n\
+ precision mediump float;\n\
";
-static const Uint8 GLES2_Fragment_Include_High_Texture_Precision[] = "\n\
+static const char GLES2_Fragment_Include_High_Texture_Precision[] = "\n\
#define SDL_TEXCOORD_PRECISION highp\n\
+ precision mediump float;\n\
";
-static const Uint8 GLES2_Fragment_Include_Undef_Precision[] = "\n\
+static const char GLES2_Fragment_Include_Undef_Precision[] = "\n\
#define mediump\n\
#define highp\n\
#define lowp\n\
#define SDL_TEXCOORD_PRECISION\n\
";
-static const Uint8 GLES2_Vertex_Default[] = " \
+static const char GLES2_Vertex_Default[] = " \
uniform mat4 u_projection; \
attribute vec2 a_position; \
attribute vec4 a_color; \
@@ -77,7 +75,7 @@ static const Uint8 GLES2_Vertex_Default[] = " \
} \
";
-static const Uint8 GLES2_Fragment_Solid[] = " \
+static const char GLES2_Fragment_Solid[] = " \
varying mediump vec4 v_color; \
\
void main() \
@@ -86,7 +84,7 @@ static const Uint8 GLES2_Fragment_Solid[] = " \
} \
";
-static const Uint8 GLES2_Fragment_TextureABGR[] = " \
+static const char GLES2_Fragment_TextureABGR[] = " \
uniform sampler2D u_texture; \
varying mediump vec4 v_color;\n\
varying SDL_TEXCOORD_PRECISION vec2 v_texCoord;\n\
@@ -99,7 +97,7 @@ static const Uint8 GLES2_Fragment_TextureABGR[] = " \
";
/* ARGB to ABGR conversion */
-static const Uint8 GLES2_Fragment_TextureARGB[] = " \
+static const char GLES2_Fragment_TextureARGB[] = " \
uniform sampler2D u_texture; \
varying mediump vec4 v_color;\n\
varying SDL_TEXCOORD_PRECISION vec2 v_texCoord;\n\
@@ -115,7 +113,7 @@ static const Uint8 GLES2_Fragment_TextureARGB[] = " \
";
/* RGB to ABGR conversion */
-static const Uint8 GLES2_Fragment_TextureRGB[] = " \
+static const char GLES2_Fragment_TextureRGB[] = " \
uniform sampler2D u_texture; \
varying mediump vec4 v_color;\n\
varying SDL_TEXCOORD_PRECISION vec2 v_texCoord;\n\
@@ -132,7 +130,7 @@ static const Uint8 GLES2_Fragment_TextureRGB[] = " \
";
/* BGR to ABGR conversion */
-static const Uint8 GLES2_Fragment_TextureBGR[] = " \
+static const char GLES2_Fragment_TextureBGR[] = " \
uniform sampler2D u_texture; \
varying mediump vec4 v_color;\n\
varying SDL_TEXCOORD_PRECISION vec2 v_texCoord;\n\
@@ -266,61 +264,61 @@ static const Uint8 GLES2_Fragment_TextureBGR[] = " \
"}" \
/* YUV to ABGR conversion */
-static const Uint8 GLES2_Fragment_TextureYUVJPEG[] = \
+static const char GLES2_Fragment_TextureYUVJPEG[] = \
YUV_SHADER_PROLOGUE \
JPEG_SHADER_CONSTANTS \
YUV_SHADER_BODY \
;
-static const Uint8 GLES2_Fragment_TextureYUVBT601[] = \
+static const char GLES2_Fragment_TextureYUVBT601[] = \
YUV_SHADER_PROLOGUE \
BT601_SHADER_CONSTANTS \
YUV_SHADER_BODY \
;
-static const Uint8 GLES2_Fragment_TextureYUVBT709[] = \
+static const char GLES2_Fragment_TextureYUVBT709[] = \
YUV_SHADER_PROLOGUE \
BT709_SHADER_CONSTANTS \
YUV_SHADER_BODY \
;
/* NV12 to ABGR conversion */
-static const Uint8 GLES2_Fragment_TextureNV12JPEG[] = \
+static const char GLES2_Fragment_TextureNV12JPEG[] = \
YUV_SHADER_PROLOGUE \
JPEG_SHADER_CONSTANTS \
NV12_RA_SHADER_BODY \
;
-static const Uint8 GLES2_Fragment_TextureNV12BT601_RA[] = \
+static const char GLES2_Fragment_TextureNV12BT601_RA[] = \
YUV_SHADER_PROLOGUE \
BT601_SHADER_CONSTANTS \
NV12_RA_SHADER_BODY \
;
-static const Uint8 GLES2_Fragment_TextureNV12BT601_RG[] = \
+static const char GLES2_Fragment_TextureNV12BT601_RG[] = \
YUV_SHADER_PROLOGUE \
BT601_SHADER_CONSTANTS \
NV12_RG_SHADER_BODY \
;
-static const Uint8 GLES2_Fragment_TextureNV12BT709_RA[] = \
+static const char GLES2_Fragment_TextureNV12BT709_RA[] = \
YUV_SHADER_PROLOGUE \
BT709_SHADER_CONSTANTS \
NV12_RA_SHADER_BODY \
;
-static const Uint8 GLES2_Fragment_TextureNV12BT709_RG[] = \
+static const char GLES2_Fragment_TextureNV12BT709_RG[] = \
YUV_SHADER_PROLOGUE \
BT709_SHADER_CONSTANTS \
NV12_RG_SHADER_BODY \
;
/* NV21 to ABGR conversion */
-static const Uint8 GLES2_Fragment_TextureNV21JPEG[] = \
+static const char GLES2_Fragment_TextureNV21JPEG[] = \
YUV_SHADER_PROLOGUE \
JPEG_SHADER_CONSTANTS \
NV21_SHADER_BODY \
;
-static const Uint8 GLES2_Fragment_TextureNV21BT601[] = \
+static const char GLES2_Fragment_TextureNV21BT601[] = \
YUV_SHADER_PROLOGUE \
BT601_SHADER_CONSTANTS \
NV21_SHADER_BODY \
;
-static const Uint8 GLES2_Fragment_TextureNV21BT709[] = \
+static const char GLES2_Fragment_TextureNV21BT709[] = \
YUV_SHADER_PROLOGUE \
BT709_SHADER_CONSTANTS \
NV21_SHADER_BODY \
@@ -328,7 +326,7 @@ static const Uint8 GLES2_Fragment_TextureNV21BT709[] = \
#endif
/* Custom Android video format texture */
-static const Uint8 GLES2_Fragment_TextureExternalOES[] = " \
+static const char GLES2_Fragment_TextureExternalOES[] = " \
#extension GL_OES_EGL_image_external : require\n\
uniform samplerExternalOES u_texture; \
varying mediump vec4 v_color;\n\
@@ -346,10 +344,8 @@ static const Uint8 GLES2_Fragment_TextureExternalOES[] = " \
* Shader selector *
*************************************************************************************************/
-const Uint8 *GLES2_GetShaderInclude(GLES2_ShaderIncludeType type) {
+const char *GLES2_GetShaderInclude(GLES2_ShaderIncludeType type) {
switch (type) {
- case GLES2_SHADER_FRAGMENT_INCLUDE_DEFAULT:
- return GLES2_Fragment_Include_Default;
case GLES2_SHADER_FRAGMENT_INCLUDE_UNDEF_PRECISION:
return GLES2_Fragment_Include_Undef_Precision;
case GLES2_SHADER_FRAGMENT_INCLUDE_BEST_TEXCOORD_PRECISION:
@@ -359,13 +355,13 @@ const Uint8 *GLES2_GetShaderInclude(GLES2_ShaderIncludeType type) {
case GLES2_SHADER_FRAGMENT_INCLUDE_HIGH_TEXCOORD_PRECISION:
return GLES2_Fragment_Include_High_Texture_Precision;
default:
- return (Uint8*)"";
+ return "";
}
}
GLES2_ShaderIncludeType GLES2_GetTexCoordPrecisionEnumFromHint() {
const char *texcoord_hint = SDL_GetHint("SDL_RENDER_OPENGLES2_TEXCOORD_PRECISION");
- GLES2_ShaderIncludeType value = GLES2_SHADER_FRAGMENT_INCLUDE_BEST_TEXCOORD_PRECISION; // "best"
+ GLES2_ShaderIncludeType value = GLES2_SHADER_FRAGMENT_INCLUDE_BEST_TEXCOORD_PRECISION;
if (texcoord_hint) {
if (SDL_strcmp(texcoord_hint, "undefined") == 0)
return GLES2_SHADER_FRAGMENT_INCLUDE_UNDEF_PRECISION;
@@ -377,7 +373,7 @@ GLES2_ShaderIncludeType GLES2_GetTexCoordPrecisionEnumFromHint() {
return value;
}
-const Uint8 *GLES2_GetShader(GLES2_ShaderType type)
+const char *GLES2_GetShader(GLES2_ShaderType type)
{
switch (type) {
case GLES2_SHADER_VERTEX_DEFAULT:
diff --git a/src/render/opengles2/SDL_shaders_gles2.h b/src/render/opengles2/SDL_shaders_gles2.h
index e975407..c3d5928 100644
--- a/src/render/opengles2/SDL_shaders_gles2.h
+++ b/src/render/opengles2/SDL_shaders_gles2.h
@@ -28,7 +28,6 @@
typedef enum
{
GLES2_SHADER_FRAGMENT_INCLUDE_NONE = 0,
- GLES2_SHADER_FRAGMENT_INCLUDE_DEFAULT,
GLES2_SHADER_FRAGMENT_INCLUDE_BEST_TEXCOORD_PRECISION,
GLES2_SHADER_FRAGMENT_INCLUDE_MEDIUM_TEXCOORD_PRECISION,
GLES2_SHADER_FRAGMENT_INCLUDE_HIGH_TEXCOORD_PRECISION,
@@ -61,8 +60,8 @@ typedef enum
GLES2_SHADER_COUNT
} GLES2_ShaderType;
-const Uint8 *GLES2_GetShader(GLES2_ShaderType type);
-const Uint8 *GLES2_GetShaderInclude(GLES2_ShaderIncludeType type);
+const char *GLES2_GetShader(GLES2_ShaderType type);
+const char *GLES2_GetShaderInclude(GLES2_ShaderIncludeType type);
GLES2_ShaderIncludeType GLES2_GetTexCoordPrecisionEnumFromHint(void);
#endif /* SDL_VIDEO_RENDER_OGL_ES2 */