+#ifdef SUPPORTS_SHADERS
+static void BuildVertexShader(vout_display_opengl_t *vgl,
+ GLint *shader)
+{
+ /* Basic vertex shader */
+ const char *vertexShader =
+ "#version " GLSL_VERSION "\n"
+ PRECISION
+ "varying vec4 TexCoord0,TexCoord1, TexCoord2;"
+ "attribute vec4 MultiTexCoord0,MultiTexCoord1,MultiTexCoord2;"
+ "attribute vec2 VertexPosition;"
+ "uniform mat4 RotationMatrix;"
+ "void main() {"
+ " TexCoord0 = MultiTexCoord0;"
+ " TexCoord1 = MultiTexCoord1;"
+ " TexCoord2 = MultiTexCoord2;"
+ " gl_Position = RotationMatrix * vec4(VertexPosition, 0.0, 1.0);"
+ "}";
+
+ *shader = vgl->CreateShader(GL_VERTEX_SHADER);
+ vgl->ShaderSource(*shader, 1, &vertexShader, NULL);
+ vgl->CompileShader(*shader);
+}
+
+static void BuildYUVFragmentShader(vout_display_opengl_t *vgl,
+ GLint *shader,
+ int *local_count,
+ GLfloat *local_value,
+ const video_format_t *fmt,
+ float yuv_range_correction)
+
+{
+ /* [R/G/B][Y U V O] from TV range to full range
+ * XXX we could also do hue/brightness/constrast/gamma
+ * by simply changing the coefficients
+ */
+ const float matrix_bt601_tv2full[12] = {
+ 1.164383561643836, 0.0000, 1.596026785714286, -0.874202217873451 ,
+ 1.164383561643836, -0.391762290094914, -0.812967647237771, 0.531667823499146 ,
+ 1.164383561643836, 2.017232142857142, 0.0000, -1.085630789302022 ,
+ };
+ const float matrix_bt709_tv2full[12] = {
+ 1.164383561643836, 0.0000, 1.792741071428571, -0.972945075016308 ,
+ 1.164383561643836, -0.21324861427373, -0.532909328559444, 0.301482665475862 ,
+ 1.164383561643836, 2.112401785714286, 0.0000, -1.133402217873451 ,
+ };
+ const float (*matrix) = fmt->i_height > 576 ? matrix_bt709_tv2full
+ : matrix_bt601_tv2full;
+
+ /* Basic linear YUV -> RGB conversion using bilinear interpolation */
+ const char *template_glsl_yuv =
+ "#version " GLSL_VERSION "\n"
+ PRECISION
+ "uniform sampler2D Texture0;"
+ "uniform sampler2D Texture1;"
+ "uniform sampler2D Texture2;"
+ "uniform vec4 Coefficient[4];"
+ "varying vec4 TexCoord0,TexCoord1,TexCoord2;"
+
+ "void main(void) {"
+ " vec4 x,y,z,result;"
+ " x = texture2D(Texture0, TexCoord0.st);"
+ " %c = texture2D(Texture1, TexCoord1.st);"
+ " %c = texture2D(Texture2, TexCoord2.st);"
+
+ " result = x * Coefficient[0] + Coefficient[3];"
+ " result = (y * Coefficient[1]) + result;"
+ " result = (z * Coefficient[2]) + result;"
+ " gl_FragColor = result;"
+ "}";
+ bool swap_uv = fmt->i_chroma == VLC_CODEC_YV12 ||
+ fmt->i_chroma == VLC_CODEC_YV9;
+
+ char *code;
+ if (asprintf(&code, template_glsl_yuv,
+ swap_uv ? 'z' : 'y',
+ swap_uv ? 'y' : 'z') < 0)
+ code = NULL;
+
+ for (int i = 0; i < 4; i++) {
+ float correction = i < 3 ? yuv_range_correction : 1.f;
+ /* We place coefficient values for coefficient[4] in one array from matrix values.
+ Notice that we fill values from top down instead of left to right.*/
+ for (int j = 0; j < 4; j++)
+ local_value[*local_count + i*4+j] = j < 3 ? correction * matrix[j*4+i]
+ : 0.f;
+ }
+ (*local_count) += 4;
+
+
+ *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
+ vgl->ShaderSource(*shader, 1, (const char **)&code, NULL);
+ vgl->CompileShader(*shader);
+
+ free(code);
+}
+
+#if 0
+static void BuildRGBFragmentShader(vout_display_opengl_t *vgl,
+ GLint *shader)
+{
+ // Simple shader for RGB
+ const char *code =
+ "#version " GLSL_VERSION "\n"
+ PRECISION
+ "uniform sampler2D Texture[3];"
+ "varying vec4 TexCoord0,TexCoord1,TexCoord2;"
+ "void main()"
+ "{ "
+ " gl_FragColor = texture2D(Texture[0], TexCoord0.st);"
+ "}";
+ *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
+ vgl->ShaderSource(*shader, 1, &code, NULL);
+ vgl->CompileShader(*shader);
+}
+#endif
+
+static void BuildRGBAFragmentShader(vout_display_opengl_t *vgl,
+ GLint *shader)
+{
+ // Simple shader for RGBA
+ const char *code =
+ "#version " GLSL_VERSION "\n"
+ PRECISION
+ "uniform sampler2D Texture;"
+ "uniform vec4 FillColor;"
+ "varying vec4 TexCoord0;"
+ "void main()"
+ "{ "
+ " gl_FragColor = texture2D(Texture, TexCoord0.st) * FillColor;"
+ "}";
+ *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
+ vgl->ShaderSource(*shader, 1, &code, NULL);
+ vgl->CompileShader(*shader);
+}
+
+static void BuildXYZFragmentShader(vout_display_opengl_t *vgl,
+ GLint *shader)
+{
+ /* Shader for XYZ to RGB correction
+ * 3 steps :
+ * - XYZ gamma correction
+ * - XYZ to RGB matrix conversion
+ * - reverse RGB gamma correction
+ */
+ const char *code =
+ "#version " GLSL_VERSION "\n"
+ PRECISION
+ "uniform sampler2D Texture0;"
+ "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 !)
+ "uniform mat4 matrix_xyz_rgb = mat4("
+ " 3.240454 , -0.9692660, 0.0556434, 0.0,"
+ " -1.5371385, 1.8760108, -0.2040259, 0.0,"
+ " -0.4985314, 0.0415560, 1.0572252, 0.0,"
+ " 0.0, 0.0, 0.0, 1.0 "
+ " );"
+
+ "varying vec4 TexCoord0;"
+ "void main()"
+ "{ "
+ " vec4 v_in, v_out;"
+ " v_in = texture2D(Texture0, TexCoord0.st);"
+ " v_in = pow(v_in, xyz_gamma);"
+ " v_out = matrix_xyz_rgb * v_in ;"
+ " v_out = pow(v_out, rgb_gamma) ;"
+ " v_out = clamp(v_out, 0.0, 1.0) ;"
+ " gl_FragColor = v_out;"
+ "}";
+ *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
+ vgl->ShaderSource(*shader, 1, &code, NULL);
+ vgl->CompileShader(*shader);
+}
+
+#endif
+