[vlc-commits] gl: move GLSL version and precision defines in variables

Thomas Guillem git at videolan.org
Wed Aug 30 13:54:12 CEST 2017


vlc | branch: master | Thomas Guillem <thomas at gllm.fr> | Tue Aug 29 10:04:59 2017 +0200| [524a003fb01e75f81f68ab61bd55b43e97c49fe9] | committer: Thomas Guillem

gl: move GLSL version and precision defines in variables

This removes one more OpenGL / OpenGLES build-time dependency in converters.

Refs #18575

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=524a003fb01e75f81f68ab61bd55b43e97c49fe9
---

 modules/video_output/opengl/converter_android.c | 13 +++++++++----
 modules/video_output/opengl/converters.c        | 17 ++++++++++-------
 modules/video_output/opengl/internal.h          | 11 +++++++----
 modules/video_output/opengl/vout_helper.c       | 11 +++++++++--
 4 files changed, 35 insertions(+), 17 deletions(-)

diff --git a/modules/video_output/opengl/converter_android.c b/modules/video_output/opengl/converter_android.c
index f4ab51886f..5a2453c675 100644
--- a/modules/video_output/opengl/converter_android.c
+++ b/modules/video_output/opengl/converter_android.c
@@ -250,10 +250,10 @@ opengl_tex_converter_anop_init(opengl_tex_converter_t *tc)
             break;
     }
 
-    static const char *code =
-        "#version " GLSL_VERSION "\n"
+    static const char *template =
+        "#version %u\n"
         "#extension GL_OES_EGL_image_external : require\n"
-        PRECISION
+        "%s" /* precision */
         "varying vec2 TexCoord0;"
         "uniform samplerExternalOES sTexture;"
         "uniform mat4 uSTMatrix;"
@@ -261,10 +261,15 @@ opengl_tex_converter_anop_init(opengl_tex_converter_t *tc)
         "{ "
         "  gl_FragColor = texture2D(sTexture, (uSTMatrix * vec4(TexCoord0, 1, 1)).xy);"
         "}";
+
+    char *code;
+    if (asprintf(&code, template, tc->glsl_version, tc->glsl_precision_header) < 0)
+        return 0;
     GLuint fragment_shader = tc->vt->CreateShader(GL_FRAGMENT_SHADER);
-    tc->vt->ShaderSource(fragment_shader, 1, &code, NULL);
+    tc->vt->ShaderSource(fragment_shader, 1, (const char **) &code, NULL);
     tc->vt->CompileShader(fragment_shader);
     tc->fshader = fragment_shader;
+    free(code);
 
     return VLC_SUCCESS;
 }
diff --git a/modules/video_output/opengl/converters.c b/modules/video_output/opengl/converters.c
index 23fb862ada..7138285e19 100644
--- a/modules/video_output/opengl/converters.c
+++ b/modules/video_output/opengl/converters.c
@@ -391,9 +391,9 @@ xyz12_shader_init(opengl_tex_converter_t *tc)
      *  - XYZ to RGB matrix conversion
      *  - reverse RGB gamma correction
      */
-    static const char *code =
-        "#version " GLSL_VERSION "\n"
-        PRECISION
+    static const char *template =
+        "#version %u\n"
+        "%s"
         "uniform sampler2D Texture0;"
         "uniform vec4 xyz_gamma = vec4(2.6);"
         "uniform vec4 rgb_gamma = vec4(1.0/2.2);"
@@ -417,11 +417,14 @@ xyz12_shader_init(opengl_tex_converter_t *tc)
         " gl_FragColor = v_out;"
         "}";
 
-    GLuint fragment_shader = tc->vt->CreateShader(GL_FRAGMENT_SHADER);
-    if (fragment_shader == 0)
+    char *code;
+    if (asprintf(&code, template, tc->glsl_version, tc->glsl_precision_header) < 0)
         return 0;
-    tc->vt->ShaderSource(fragment_shader, 1, &code, NULL);
+
+    GLuint fragment_shader = tc->vt->CreateShader(GL_FRAGMENT_SHADER);
+    tc->vt->ShaderSource(fragment_shader, 1, (const char **) &code, NULL);
     tc->vt->CompileShader(fragment_shader);
+    free(code);
     return fragment_shader;
 }
 
@@ -472,7 +475,7 @@ opengl_fragment_shader_init(opengl_tex_converter_t *tc, GLenum tex_target,
 #define ADD(x) vlc_memstream_puts(&ms, x)
 #define ADDF(x, ...) vlc_memstream_printf(&ms, x, ##__VA_ARGS__)
 
-    ADD("#version " GLSL_VERSION "\n" PRECISION);
+    ADDF("#version %u\n%s", tc->glsl_version, tc->glsl_precision_header);
 
     for (unsigned i = 0; i < tc->tex_count; ++i)
         ADDF("uniform %s Texture%u;"
diff --git a/modules/video_output/opengl/internal.h b/modules/video_output/opengl/internal.h
index 1e92bb84c9..4652487a77 100644
--- a/modules/video_output/opengl/internal.h
+++ b/modules/video_output/opengl/internal.h
@@ -26,18 +26,14 @@
 #define VLCGL_PICTURE_MAX 128
 
 #if defined(USE_OPENGL_ES2)
-#   define GLSL_VERSION "100"
-#   define PRECISION "precision highp float;"
 #   define VLCGL_HAS_PBO /* PBO present as an OpenGlES 2 extension */
 #else
-#   define GLSL_VERSION "120"
 #   ifdef GL_VERSION_2_0
 #       define VLCGL_HAS_PBO
 #   endif
 #   ifdef GL_VERSION_4_4
 #       define VLCGL_HAS_MAP_PERSISTENT
 #   endif
-#   define PRECISION ""
 #   if defined(__APPLE__)
 #       define GL_TEXTURE_RECTANGLE 0x84F5
 #   endif
@@ -204,9 +200,16 @@ struct opengl_tex_converter_t
 
     /* Function pointers to OpenGL functions, set by the caller */
     const opengl_vtable_t *vt;
+
     /* Available gl extensions (from GL_EXTENSIONS) */
     const char *glexts;
 
+    /* GLSL version, set by the caller. 100 for GLSL ES, 120 for desktop GLSL */
+    unsigned glsl_version;
+    /* Precision header, set by the caller. In OpenGLES, the fragment language
+     * has no default precision qualifier for floating point types. */
+    const char *glsl_precision_header;
+
     /* Can only be changed from the module open function */
     video_format_t fmt;
 
diff --git a/modules/video_output/opengl/vout_helper.c b/modules/video_output/opengl/vout_helper.c
index a60f73a7eb..35934ba521 100644
--- a/modules/video_output/opengl/vout_helper.c
+++ b/modules/video_output/opengl/vout_helper.c
@@ -352,7 +352,7 @@ static GLuint BuildVertexShader(const opengl_tex_converter_t *tc,
 {
     /* Basic vertex shader */
     static const char *template =
-        "#version " GLSL_VERSION "\n"
+        "#version %u\n"
         "varying vec2 TexCoord0;attribute vec4 MultiTexCoord0;"
         "%s%s"
         "attribute vec3 VertexPosition;"
@@ -378,7 +378,7 @@ static GLuint BuildVertexShader(const opengl_tex_converter_t *tc,
         " TexCoord2 = vec4(OrientationMatrix * MultiTexCoord2).st;" : "";
 
     char *code;
-    if (asprintf(&code, template, coord1_header, coord2_header,
+    if (asprintf(&code, template, tc->glsl_version, coord1_header, coord2_header,
                  coord1_code, coord2_code) < 0)
         return 0;
 
@@ -557,6 +557,13 @@ opengl_init_program(vout_display_opengl_t *vgl, struct prgm *prgm,
         .gl = vgl->gl,
         .vt = &vgl->vt,
         .glexts = glexts,
+#if defined(USE_OPENGL_ES2)
+        .glsl_version = 100,
+        .glsl_precision_header = "precision highp float;\n",
+#else
+        .glsl_version = 120,
+        .glsl_precision_header = "",
+#endif
         .fmt = *fmt,
     };
 



More information about the vlc-commits mailing list