summaryrefslogtreecommitdiff
path: root/src/lib/ector/gl/ector_gl_shader.c
blob: acc366e692ac76f918e77227dc43e73363e7075b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <Eina.h>
#include <Ector.h>

#include <dlfcn.h>

#include "gl/Ector_GL.h"
#include "ector_private.h"
#include "ector_gl_private.h"

#include "shader/ector_gl_shaders.x"

static const char *_shader_flags[SHADER_FLAG_COUNT] = {
  "TEX",
  "BGRA",
  "MASK",
  "SAM12",
  "SAM21",
  "SAM22",
  "MASKSAM12",
  "MASKSAM21",
  "MASKSAM22",
  "IMG",
  "BIGENDIAN",
  "YUV",
  "YUY2",
  "NV12",
  "YUV_709",
  "EXTERNAL",
  "AFILL",
  "NOMUL",
  "ALPHA",
  "RGB_A_PAIR"
};

static Eina_Strbuf *
_ector_gl_shader_glsl_get(uint64_t flags, const char *base)
{
   Eina_Strbuf *r;
   unsigned int k;

   r = eina_strbuf_new();
   for (k =0; k < SHADER_FLAG_COUNT; k++)
     {
        if (flags & (1 << k))
          eina_strbuf_append_printf(r, "#define SHD_%s\n", _shader_flags[k]);
     }

   eina_strbuf_append(r, base);

   return r;
}

static GLint
_ector_gl_shader_glsl_compile(GLuint s, const Eina_Strbuf *shader, const char *type)
{
   const char *str;
   GLint ok = 0;

   str = eina_strbuf_string_get(shader);

   GL.glShaderSource(s, 1, &str, NULL);
   GL.glCompileShader(s);
   GL.glGetShaderiv(s, GL_COMPILE_STATUS, &ok);
   if (!ok)
     {
        Eina_Strbuf *err;

        err = eina_strbuf_new();
        if (!err) goto on_error;
        eina_strbuf_append_printf(err, "compile of %s shader", type);

        gl_compile_link_error(s, eina_strbuf_string_get(err), EINA_TRUE);
        ERR("Abort %s:\n%s", eina_strbuf_string_get(err), str);

        eina_strbuf_free(err);
     }

 on_error:
   return ok;
}

static GLint
_ector_gl_shader_glsl_link(uint64_t flags,
                           const Eina_Strbuf *vertex,
                           const Eina_Strbuf *fragment)
{
   GLuint vtx = 0, frg = 0, prg = 0;
   GLint ok = 0;

   vtx = GL.glCreateShader(GL_VERTEX_SHADER);
   frg = GL.glCreateShader(GL_FRAGMENT_SHADER);

   // Compiling vertex shader
   ok = _ector_gl_shader_glsl_compile(vtx, vertex, "vertex");
   if (!ok) goto on_error;

   // Compile fragment shader
   ok = _ector_gl_shader_glsl_compile(frg, fragment, "fragment");
   if (!ok) goto on_error;

   // Link both shader together
   ok = 0;

   prg = GL.glCreateProgram();
#ifndef GL_GLES
   if ((GL.glGetProgramBinary) && (GL.glProgramParameteri))
     GL.glProgramParameteri(prg, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
#endif
   GL.glAttachShader(prg, vtx);
   GL.glAttachShader(prg, frg);

   GL.glBindAttribLocation(prg, SHAD_VERTEX,  "vertex");
   GL.glBindAttribLocation(prg, SHAD_COLOR,   "color");
   GL.glBindAttribLocation(prg, SHAD_TEXUV,   "tex_coord");
   GL.glBindAttribLocation(prg, SHAD_TEXUV2,  "tex_coord2");
   GL.glBindAttribLocation(prg, SHAD_TEXUV3,  "tex_coord3");
   GL.glBindAttribLocation(prg, SHAD_TEXA,    "tex_coorda");
   GL.glBindAttribLocation(prg, SHAD_TEXSAM,  "tex_sample");
   GL.glBindAttribLocation(prg, SHAD_MASK,    "mask_coord");
   GL.glBindAttribLocation(prg, SHAD_MASKSAM, "tex_masksample");

   GL.glLinkProgram(prg);
   GL.glGetProgramiv(prg, GL_LINK_STATUS, &ok);
   if (!ok)
     {
        gl_compile_link_error(prg, "link fragment and vertex shaders", EINA_FALSE);
        ERR("Abort compile of shader (flags: %16lx)", flags);
        GL.glDeleteProgram(prg);
        prg = 0;
        goto on_error;
     }

 on_error:
   if (vtx) GL.glDeleteShader(vtx);
   if (frg) GL.glDeleteShader(frg);

   return prg;
}

GLuint
ector_gl_shader_compile(uint64_t flags)
{
   Eina_Strbuf *vertex, *fragment;
   GLuint shd = 0;

   vertex = _ector_gl_shader_glsl_get(flags, vertex_glsl);
   fragment = _ector_gl_shader_glsl_get(flags, fragment_glsl);
   if (!vertex || !fragment) goto on_error;

   shd = _ector_gl_shader_glsl_link(flags, vertex, fragment);

 on_error:
   eina_strbuf_free(vertex);
   eina_strbuf_free(fragment);

#ifdef GL_GLES
   GL.glReleaseShaderCompiler();
#else
   if (GL.glReleaseShaderCompiler)
     GL.glReleaseShaderCompiler();
#endif

   return shd;
}