From 9907e03749442f5418d652f4731a4b68e12997e5 Mon Sep 17 00:00:00 2001 From: Daid Date: Tue, 15 Mar 2022 23:04:28 +0100 Subject: [PATCH] Decide on which shader header to use when compiling, not when loading, as the context might not be set yet. --- src/graphics/shader.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index 7a5689a0..8004eed3 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -13,12 +13,8 @@ static const char* vertex_shader_header = "#version 120\n"; static const char* fragment_shader_header = "#version 120\n"; Shader::Shader(const string& name, const string& code, const std::vector& defines, const std::unordered_map& attribute_mapping) -: attribute_mapping{attribute_mapping}, name(name), vertex_code(vertex_shader_header), fragment_code(fragment_shader_header) +: attribute_mapping{attribute_mapping}, name(name) { - if (gl::contextIsES) { - vertex_code = vertex_shader_header_es; - fragment_code = fragment_shader_header_es; - } for(auto str : defines) { vertex_code += "#define " + string(str) + " 1\n"; @@ -58,11 +54,12 @@ Shader::Shader(const string& name, P code_stream, const std::uno :Shader(name, code_stream, {}, attribute_mapping) {} -static unsigned int compileShader(const string& name, const char* code, int type) +static unsigned int compileShader(const string& name, const char* header, const char* code, int type) { int success; unsigned int shader_handle = glCreateShader(type); - glShaderSource(shader_handle, 1, &code, nullptr); + const char* code_ptr[2] = {header, code}; + glShaderSource(shader_handle, 2, code_ptr, nullptr); glCompileShader(shader_handle); glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &success); if (!success) @@ -82,10 +79,11 @@ static unsigned int compileShader(const string& name, const char* code, int type bool Shader::compileShader() { program = 0; - unsigned int vertex_shader_handle = sp::compileShader(name, vertex_code.c_str(), GL_VERTEX_SHADER); + + unsigned int vertex_shader_handle = sp::compileShader(name, gl::contextIsES ? vertex_shader_header_es : vertex_shader_header, vertex_code.c_str(), GL_VERTEX_SHADER); if (!vertex_shader_handle) return false; - unsigned int fragment_shader_handle = sp::compileShader(name, fragment_code.c_str(), GL_FRAGMENT_SHADER); + unsigned int fragment_shader_handle = sp::compileShader(name, gl::contextIsES ? fragment_shader_header_es : fragment_shader_header, fragment_code.c_str(), GL_FRAGMENT_SHADER); if (!fragment_shader_handle) { glDeleteShader(vertex_shader_handle);