From 1e49845629e93d4513b9388c524d7d1e0ac945f8 Mon Sep 17 00:00:00 2001 From: pommicket Date: Thu, 3 Nov 2022 17:47:09 -0400 Subject: custom shaders now use #version 130 if possible like i promised they would --- config.c | 10 +++++----- gl.c | 24 +++++++++++++++++++----- text.c | 6 ++---- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/config.c b/config.c index 612a647..ab68241 100644 --- a/config.c +++ b/config.c @@ -877,21 +877,21 @@ void config_parse(Ted *ted, ConfigPart **pparts) { arr_foreach_ptr(ted->all_settings, Settings, s) { if (*s->bg_shader_text) { // load bg shader - char vshader[] = "#version 110\n\ - attribute vec2 v_pos;\n\ + char vshader[8192] ; + strbuf_printf(vshader, "attribute vec2 v_pos;\n\ varying vec2 t_pos;\n\ void main() { \n\ gl_Position = vec4(v_pos * 2.0 - 1.0, 0.0, 1.0);\n\ t_pos = v_pos;\n\ - }"; + }"); char fshader[8192]; - strbuf_printf(fshader, "#version 110\n\ - varying vec2 t_pos;\n\ + strbuf_printf(fshader, "varying vec2 t_pos;\n\ uniform float t_time;\n\ uniform float t_save_time;\n\ uniform vec2 t_aspect;\n\ #line 1\n\ %s", settings->bg_shader_text); + s->bg_shader = gl_compile_and_link_shaders(ted->error, vshader, fshader); if (s->bg_shader) { GLuint bg_buffer = 0, bg_array = 0; diff --git a/gl.c b/gl.c index 1d6cb14..ddb4ba4 100644 --- a/gl.c +++ b/gl.c @@ -77,10 +77,26 @@ static void gl_get_procs(void) { #undef gl_get_proc } +static GLuint glsl_version(void) { + int v = gl_version_major * 100 + gl_version_minor * 10; + switch (v) { + case 200: return 110; + case 210: return 120; + } + // don't go above 130. i don't want to write two different shaders one using varying and one using whatever new stuff GLSL has these days + return 130; +} + // compile a GLSL shader static GLuint gl_compile_shader(char error_buf[256], char const *code, GLenum shader_type) { GLuint shader = glCreateShader(shader_type); - glShaderSource(shader, 1, &code, NULL); + char header[32]; + strbuf_printf(header, "#version %u\n#line 1\n", glsl_version()); + const char *sources[2] = { + header, + code + }; + glShaderSource(shader, 2, sources, NULL); glCompileShader(shader); GLint status = 0; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); @@ -171,8 +187,7 @@ static GLuint gl_geometry_v_color; static GLuint gl_geometry_vbo, gl_geometry_vao; static void gl_geometry_init(void) { - char const *vshader_code = "#version 110\n\ - attribute vec2 v_pos;\n\ + char const *vshader_code = "attribute vec2 v_pos;\n\ attribute vec4 v_color;\n\ varying vec4 color;\n\ void main() {\n\ @@ -180,8 +195,7 @@ static void gl_geometry_init(void) { color = v_color;\n\ }\n\ "; - char const *fshader_code = "#version 110\n\ - varying vec4 color;\n\ + char const *fshader_code = "varying vec4 color;\n\ void main() {\n\ gl_FragColor = color;\n\ }\n\ diff --git a/text.c b/text.c index 144a7a0..a3ff48e 100644 --- a/text.c +++ b/text.c @@ -90,8 +90,7 @@ static GLuint text_v_pos, text_v_color, text_v_tex_coord; static GLint text_u_sampler; static bool text_init(void) { - char const *vshader_code = "#version 110\n\ -attribute vec4 v_color;\n\ + char const *vshader_code = "attribute vec4 v_color;\n\ attribute vec2 v_pos;\n\ attribute vec2 v_tex_coord;\n\ varying vec4 color;\n\ @@ -102,8 +101,7 @@ void main() {\n\ gl_Position = vec4(v_pos, 0.0, 1.0);\n\ }\n\ "; - char const *fshader_code = "#version 110\n\ -varying vec4 color;\n\ + char const *fshader_code = "varying vec4 color;\n\ varying vec2 tex_coord;\n\ uniform sampler2D sampler;\n\ void main() {\n\ -- cgit v1.2.3