Try to avoid immediately crashing if glCreateShader fails (Thanks TrueDJSlimeball)

This commit is contained in:
UnknownShadow200 2021-03-15 19:23:53 +11:00
parent 6f7eac87c7
commit 17bd4c60ab

View File

@ -1695,23 +1695,19 @@ static void GenFragmentShader(const struct GLShader* shader, cc_string* dst) {
String_AppendConst(dst, "}"); String_AppendConst(dst, "}");
} }
/* Tries to compile GLSL shader code. */ /* Tries to compile GLSL shader code */
static GLint CompileShader(GLenum type, const cc_string* src, GLuint* obj) { static GLint CompileShader(GLint shader, const cc_string* src) {
GLint temp, shader; const char* str = src->buffer;
int len; int len = src->length;
GLint temp;
shader = glCreateShader(type); glShaderSource(shader, 1, &str, &len);
*obj = shader;
if (!shader) return false;
len = src->length;
glShaderSource(shader, 1, &src->buffer, &len);
glCompileShader(shader); glCompileShader(shader);
glGetShaderiv(shader, _GL_COMPILE_STATUS, &temp); glGetShaderiv(shader, _GL_COMPILE_STATUS, &temp);
return temp; return temp;
} }
/* Logs information then aborts program. */ /* Logs information then aborts program */
static void ShaderFailed(GLint shader) { static void ShaderFailed(GLint shader) {
char logInfo[2048]; char logInfo[2048];
GLint temp; GLint temp;
@ -1728,27 +1724,32 @@ static void ShaderFailed(GLint shader) {
Logger_Abort("Failed to compile shader"); Logger_Abort("Failed to compile shader");
} }
/* Tries to compile vertex and fragment shaders, then link into an OpenGL program. */ /* Tries to compile vertex and fragment shaders, then link into an OpenGL program */
static void CompileProgram(struct GLShader* shader) { static void CompileProgram(struct GLShader* shader) {
char tmpBuffer[2048]; cc_string tmp; char tmpBuffer[2048]; cc_string tmp;
GLuint vs, fs, program; GLuint vs, fs, program;
GLint temp; GLint temp;
vs = glCreateShader(_GL_VERTEX_SHADER);
if (!vs) { Platform_LogConst("Failed to create vertex shader"); return; }
String_InitArray(tmp, tmpBuffer); String_InitArray(tmp, tmpBuffer);
GenVertexShader(shader, &tmp); GenVertexShader(shader, &tmp);
if (!CompileShader(_GL_VERTEX_SHADER, &tmp, &vs)) ShaderFailed(vs); if (!CompileShader(vs, &tmp)) ShaderFailed(vs);
fs = glCreateShader(_GL_FRAGMENT_SHADER);
if (!fs) { Platform_LogConst("Failed to create fragment shader"); glDeleteShader(vs); return; }
tmp.length = 0; tmp.length = 0;
GenFragmentShader(shader, &tmp); GenFragmentShader(shader, &tmp);
if (!CompileShader(_GL_FRAGMENT_SHADER, &tmp, &fs)) { if (!CompileShader(fs, &tmp)) {
/* Sometimes fails 'highp precision is not supported in fragment shader' */ /* Sometimes fails 'highp precision is not supported in fragment shader' */
/* So try compiling shader again without highp precision */ /* So try compiling shader again without highp precision */
glDeleteShader(fs);
shader->features |= FTR_FS_MEDIUMP; shader->features |= FTR_FS_MEDIUMP;
tmp.length = 0; tmp.length = 0;
GenFragmentShader(shader, &tmp); GenFragmentShader(shader, &tmp);
if (!CompileShader(_GL_FRAGMENT_SHADER, &tmp, &fs)) ShaderFailed(fs); if (!CompileShader(fs, &tmp)) ShaderFailed(fs);
} }