[vlc-devel] [PATCH v2 06/13] opengl: replace multi-variables by arrays in GLSL

Romain Vimont rom1v at videolabs.io
Tue Mar 30 11:14:04 UTC 2021


Variables were generated on the fly with an index in the name. For
example:

    uniform sampler2D Texture0;
    uniform mat3 TexCoordsMap0;
    uniform sampler2D Texture1;
    uniform mat3 TexCoordsMap1;
    uniform sampler2D Texture2;
    uniform mat3 TexCoordsMap2;

Replace them by arrays:

    uniform sampler2D Textures[3];
    uniform mat3 TexCoordsMaps[3];

This will enable to expose a GLSL function providing a color for a
particular plane number passed as a parameter.
---
 modules/video_output/opengl/sampler.c | 44 ++++++++++++---------------
 1 file changed, 20 insertions(+), 24 deletions(-)

diff --git a/modules/video_output/opengl/sampler.c b/modules/video_output/opengl/sampler.c
index 58a5185b08..4a928690bc 100644
--- a/modules/video_output/opengl/sampler.c
+++ b/modules/video_output/opengl/sampler.c
@@ -292,19 +292,19 @@ sampler_base_fetch_locations(struct vlc_gl_sampler *sampler, GLuint program)
     assert(priv->tex_count < 10); /* to guarantee variable names length */
     for (unsigned int i = 0; i < priv->tex_count; ++i)
     {
-        char name[sizeof("TexCoordsMapX")];
+        char name[sizeof("TexCoordsMaps[X]")];
 
-        snprintf(name, sizeof(name), "Texture%1u", i);
+        snprintf(name, sizeof(name), "Textures[%1u]", i);
         priv->uloc.Textures[i] = vt->GetUniformLocation(program, name);
         assert(priv->uloc.Textures[i] != -1);
 
-        snprintf(name, sizeof(name), "TexCoordsMap%1u", i);
+        snprintf(name, sizeof(name), "TexCoordsMaps[%1u]", i);
         priv->uloc.TexCoordsMaps[i] = vt->GetUniformLocation(program, name);
         assert(priv->uloc.TexCoordsMaps[i] != -1);
 
         if (priv->tex_target == GL_TEXTURE_RECTANGLE)
         {
-            snprintf(name, sizeof(name), "TexSize%1u", i);
+            snprintf(name, sizeof(name), "TexSizes[%1u]", i);
             priv->uloc.TexSizes[i] = vt->GetUniformLocation(program, name);
             assert(priv->uloc.TexSizes[i] != -1);
         }
@@ -407,7 +407,7 @@ sampler_xyz12_fetch_locations(struct vlc_gl_sampler *sampler, GLuint program)
     struct vlc_gl_sampler_priv *priv = PRIV(sampler);
     const opengl_vtable_t *vt = priv->vt;
 
-    priv->uloc.Textures[0] = vt->GetUniformLocation(program, "Texture0");
+    priv->uloc.Textures[0] = vt->GetUniformLocation(program, "Textures[0]");
     assert(priv->uloc.Textures[0] != -1);
 
     priv->uloc.TransformMatrix =
@@ -419,7 +419,7 @@ sampler_xyz12_fetch_locations(struct vlc_gl_sampler *sampler, GLuint program)
     assert(priv->uloc.OrientationMatrix != -1);
 
     priv->uloc.TexCoordsMaps[0] =
-        vt->GetUniformLocation(program, "TexCoordsMap0");
+        vt->GetUniformLocation(program, "TexCoordsMaps[0]");
     assert(priv->uloc.TexCoordsMaps[0] != -1);
 }
 
@@ -462,7 +462,7 @@ xyz12_shader_init(struct vlc_gl_sampler *sampler)
      *  - reverse RGB gamma correction
      */
     static const char *template =
-        "uniform sampler2D Texture0;"
+        "uniform sampler2D Textures[1];"
         "uniform vec4 xyz_gamma = vec4(2.6);"
         "uniform vec4 rgb_gamma = vec4(1.0/2.2);"
         /* WARN: matrix Is filled column by column (not row !) */
@@ -475,14 +475,14 @@ xyz12_shader_init(struct vlc_gl_sampler *sampler)
 
         "uniform mat4 TransformMatrix;\n"
         "uniform mat4 OrientationMatrix;\n"
-        "uniform mat3 TexCoordsMap0;\n"
+        "uniform mat3 TexCoordsMaps[1];\n"
         "vec4 vlc_texture(vec2 pic_coords)\n"
         "{ "
         " vec4 v_in, v_out;"
         /* Homogeneous (oriented) coordinates */
         " vec3 pic_hcoords = vec3((TransformMatrix * OrientationMatrix * vec4(pic_coords, 0.0, 1.0)).st, 1.0);\n"
-        " vec2 tex_coords = (TexCoordsMap0 * pic_hcoords).st;\n"
-        " v_in  = texture2D(Texture0, tex_coords);\n"
+        " vec2 tex_coords = (TexCoordsMaps[0] * pic_hcoords).st;\n"
+        " v_in  = texture2D(Textures[0], tex_coords);\n"
         " v_in = pow(v_in, xyz_gamma);"
         " v_out = matrix_xyz_rgb * v_in ;"
         " v_out = pow(v_out, rgb_gamma) ;"
@@ -781,9 +781,8 @@ opengl_fragment_shader_init(struct vlc_gl_sampler *sampler, GLenum tex_target,
 
     ADD("uniform mat4 TransformMatrix;\n"
         "uniform mat4 OrientationMatrix;\n");
-    for (unsigned i = 0; i < tex_count; ++i)
-        ADDF("uniform %s Texture%u;\n"
-             "uniform mat3 TexCoordsMap%u;\n", glsl_sampler, i, i);
+    ADDF("uniform %s Textures[%u];\n", glsl_sampler, tex_count);
+    ADDF("uniform mat3 TexCoordsMaps[%u];\n", tex_count);
 
 #ifdef HAVE_LIBPLACEBO
     if (priv->pl_sh) {
@@ -867,10 +866,7 @@ opengl_fragment_shader_init(struct vlc_gl_sampler *sampler, GLenum tex_target,
 #endif
 
     if (tex_target == GL_TEXTURE_RECTANGLE)
-    {
-        for (unsigned i = 0; i < tex_count; ++i)
-            ADDF("uniform vec2 TexSize%u;\n", i);
-    }
+        ADDF("uniform vec2 TexSizes[%u];\n", tex_count);
 
     if (is_yuv)
         ADD("uniform mat4 ConvMatrix;\n");
@@ -890,14 +886,14 @@ opengl_fragment_shader_init(struct vlc_gl_sampler *sampler, GLenum tex_target,
             const char *swizzle = swizzle_per_tex[i];
             assert(swizzle);
             size_t swizzle_count = strlen(swizzle);
-            ADDF(" tex_coords = (TexCoordsMap%u * pic_hcoords).st;\n", i);
+            ADDF(" tex_coords = (TexCoordsMaps[%u] * pic_hcoords).st;\n", i);
             if (tex_target == GL_TEXTURE_RECTANGLE)
             {
                 /* The coordinates are in texels values, not normalized */
-                ADDF(" tex_coords = vec2(tex_coords.x * TexSize%u.x,\n"
-                     "                   tex_coords.y * TexSize%u.y);\n", i, i);
+                ADDF(" tex_coords = vec2(tex_coords.x * TexSizes[%u].x,\n"
+                     "                   tex_coords.y * TexSizes[%u].y);\n", i, i);
             }
-            ADDF(" texel = %s(Texture%u, tex_coords);\n", lookup, i);
+            ADDF(" texel = %s(Textures[%u], tex_coords);\n", lookup, i);
             for (unsigned j = 0; j < swizzle_count; ++j)
             {
                 ADDF(" pixel[%u] = texel.%c;\n", color_idx, swizzle[j]);
@@ -910,11 +906,11 @@ opengl_fragment_shader_init(struct vlc_gl_sampler *sampler, GLenum tex_target,
     }
     else
     {
-        ADD(" tex_coords = (TexCoordsMap0 * pic_hcoords).st;\n");
+        ADD(" tex_coords = (TexCoordsMaps[0] * pic_hcoords).st;\n");
         if (tex_target == GL_TEXTURE_RECTANGLE)
-            ADD(" tex_coords *= TexSize0;\n");
+            ADD(" tex_coords *= TexSizes[0];\n");
 
-        ADDF(" vec4 result = %s(Texture0, tex_coords);\n", lookup);
+        ADDF(" vec4 result = %s(Textures[0], tex_coords);\n", lookup);
         color_count = 1;
     }
     assert(yuv_space == COLOR_SPACE_UNDEF || color_count == 3);
-- 
2.31.0



More information about the vlc-devel mailing list