aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/evas/engines/gl_common/evas_gl_3d_renderer.c
blob: 2b0e7aed3f8c42055ca0527333699e1afca169ec (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "evas_gl_private.h"
#include "evas_gl_3d_private.h"

#define E3D_MAX_TEXTURE_COUNT       8
#define E3D_MAX_VERTEX_ATTRIB_COUNT 8

struct _E3D_Renderer
{
   Eina_List     *programs;

   GLuint         fbo;
   GLuint         program;
   E3D_Texture   *textures[E3D_MAX_TEXTURE_COUNT];

   Eina_Bool      vertex_attrib_enable[E3D_MAX_VERTEX_ATTRIB_COUNT];
   Eina_Bool      depth_test_enable;
   GLuint         texDepth;
   GLint         smap_sampler;
};

static inline GLenum
_gl_assembly_get(Evas_Canvas3D_Vertex_Assembly assembly)
{
   switch (assembly)
     {
      case EVAS_CANVAS3D_VERTEX_ASSEMBLY_POINTS:
         return GL_POINTS;
      case EVAS_CANVAS3D_VERTEX_ASSEMBLY_LINES:
         return GL_LINES;
      case EVAS_CANVAS3D_VERTEX_ASSEMBLY_LINE_STRIP:
         return GL_LINE_STRIP;
      case EVAS_CANVAS3D_VERTEX_ASSEMBLY_LINE_LOOP:
         return GL_LINE_LOOP;
      case EVAS_CANVAS3D_VERTEX_ASSEMBLY_TRIANGLES:
         return GL_TRIANGLES;
      case EVAS_CANVAS3D_VERTEX_ASSEMBLY_TRIANGLE_STRIP:
         return GL_TRIANGLE_STRIP;
      case EVAS_CANVAS3D_VERTEX_ASSEMBLY_TRIANGLE_FAN:
         return GL_TRIANGLE_FAN;
      default:
         return GL_NONE;
     }
}

static inline GLenum
_gl_blend_func_get(Evas_Canvas3D_Blend_Func blend_func)
{
   switch (blend_func)
     {
      case EVAS_CANVAS3D_BLEND_FUNC_ZERO:
         return GL_ZERO;
      case EVAS_CANVAS3D_BLEND_FUNC_ONE:
         return GL_ONE;
      case EVAS_CANVAS3D_BLEND_FUNC_SRC_COLOR:
         return GL_SRC_COLOR;
      case EVAS_CANVAS3D_BLEND_FUNC_ONE_MINUS_SRC_COLOR:
         return GL_ONE_MINUS_SRC_COLOR;
      case EVAS_CANVAS3D_BLEND_FUNC_DST_COLOR:
         return GL_DST_COLOR;
      case EVAS_CANVAS3D_BLEND_FUNC_ONE_MINUS_DST_COLOR:
         return GL_ONE_MINUS_DST_COLOR;
      case EVAS_CANVAS3D_BLEND_FUNC_SRC_ALPHA:
         return GL_SRC_ALPHA;
      case EVAS_CANVAS3D_BLEND_FUNC_ONE_MINUS_SRC_ALPHA:
         return GL_ONE_MINUS_SRC_ALPHA;
      case EVAS_CANVAS3D_BLEND_FUNC_DST_ALPHA:
         return GL_DST_ALPHA;
      case EVAS_CANVAS3D_BLEND_FUNC_ONE_MINUS_DST_ALPHA:
         return GL_ONE_MINUS_DST_ALPHA;
      case EVAS_CANVAS3D_BLEND_FUNC_CONSTANT_COLOR:
         return GL_CONSTANT_COLOR;
      case EVAS_CANVAS3D_BLEND_FUNC_ONE_MINUS_CONSTANT_COLOR:
         return GL_ONE_MINUS_CONSTANT_COLOR;
      case EVAS_CANVAS3D_BLEND_FUNC_CONSTANT_ALPHA:
         return GL_CONSTANT_ALPHA;
      case EVAS_CANVAS3D_BLEND_FUNC_ONE_MINUS_CONSTANT_ALPHA:
         return GL_ONE_MINUS_CONSTANT_ALPHA;
      case EVAS_CANVAS3D_BLEND_FUNC_SRC_ALPHA_SATURATE:
         return GL_SRC_ALPHA_SATURATE;
      default:
         return GL_ZERO;
     }
}

#ifndef GL_GLES
static inline GLenum
_gl_comparison_func_get(Evas_Canvas3D_Comparison comparison_func)
{
   switch (comparison_func)
     {
      case EVAS_CANVAS3D_COMPARISON_NEVER:
         return GL_NEVER;
      case EVAS_CANVAS3D_COMPARISON_LESS:
         return GL_LESS;
      case EVAS_CANVAS3D_COMPARISON_EQUAL:
         return GL_EQUAL;
      case EVAS_CANVAS3D_COMPARISON_LEQUAL:
         return GL_LEQUAL;
      case EVAS_CANVAS3D_COMPARISON_GREATER:
         return GL_GREATER;
      case EVAS_CANVAS3D_COMPARISON_NOTEQUAL:
         return GL_NOTEQUAL;
      case EVAS_CANVAS3D_COMPARISON_GEQUAL:
         return GL_GEQUAL;
      case EVAS_CANVAS3D_COMPARISON_ALWAYS:
         return GL_ALWAYS;
      default:
         return GL_ALWAYS;
     }
}
#endif

static inline void
_renderer_vertex_attrib_array_enable(E3D_Renderer *renderer, int index)
{
   if (renderer->vertex_attrib_enable[index])
     return;

   glEnableVertexAttribArray(index);
   renderer->vertex_attrib_enable[index] = EINA_TRUE;
}

static inline void
_renderer_vertex_attrib_array_disable(E3D_Renderer *renderer, int index)
{
   if (!renderer->vertex_attrib_enable[index])
     return;

   glDisableVertexAttribArray(index);
   renderer->vertex_attrib_enable[index] = EINA_FALSE;
}

static inline void
_renderer_vertex_attrib_pointer_set(E3D_Renderer *renderer EINA_UNUSED, int index,
                                    const Evas_Canvas3D_Vertex_Buffer *buffer)
{
   glVertexAttribPointer(index, buffer->element_count, GL_FLOAT,
                         GL_FALSE, buffer->stride, buffer->data);
}

static inline void
_renderer_elements_draw(E3D_Renderer *renderer EINA_UNUSED, Evas_Canvas3D_Vertex_Assembly assembly,
                        int count, Evas_Canvas3D_Index_Format format, const void *indices)
{
   GLenum mode = _gl_assembly_get(assembly);

   if (format == EVAS_CANVAS3D_INDEX_FORMAT_UNSIGNED_BYTE)
     glDrawElements(mode, count, GL_UNSIGNED_BYTE, indices);
   else if (format == EVAS_CANVAS3D_INDEX_FORMAT_UNSIGNED_SHORT)
     glDrawElements(mode, count, GL_UNSIGNED_SHORT, indices);
}

static inline void
_renderer_array_draw(E3D_Renderer *renderer EINA_UNUSED,
                     Evas_Canvas3D_Vertex_Assembly assembly, int count)
{
   GLenum mode = _gl_assembly_get(assembly);

   glDrawArrays(mode, 0, count);
}

static inline void
_renderer_program_use(E3D_Renderer *renderer ,E3D_Program *program)
{
   GLuint prog = e3d_program_id_get(program);

   if (renderer->program != prog)
     {
        glUseProgram(prog);
        renderer->program = prog;
     }
}

static inline void
_renderer_texture_bind(E3D_Renderer *renderer, E3D_Draw_Data *data)
{
   int i;

   for (i = 0; i < EVAS_CANVAS3D_MATERIAL_ATTRIB_COUNT; i++)
     {
        if (data->materials[i].tex0)
          {
             if (renderer->textures[data->materials[i].sampler0] != data->materials[i].tex0)
               {
                  glActiveTexture(GL_TEXTURE0 + data->materials[i].sampler0);
                  glBindTexture(GL_TEXTURE_2D, data->materials[i].tex0->tex);
                  e3d_texture_param_update(data->materials[i].tex0);

                  renderer->textures[data->materials[i].sampler0] = data->materials[i].tex0;
               }
          }

        if (data->materials[i].tex1)
          {
             if (renderer->textures[data->materials[i].sampler1] != data->materials[i].tex1)
               {
                  glActiveTexture(GL_TEXTURE0 + data->materials[i].sampler1);
                  glBindTexture(GL_TEXTURE_2D, data->materials[i].tex1->tex);
                  e3d_texture_param_update(data->materials[i].tex1);

                  renderer->textures[data->materials[i].sampler1] = data->materials[i].tex1;
               }
          }
     }
   if ((data->flags & E3D_SHADER_FLAG_SHADOWED) && (renderer->smap_sampler != data->smap_sampler))
     {
        glActiveTexture(GL_TEXTURE0 + data->smap_sampler);
        glBindTexture(GL_TEXTURE_2D, renderer->texDepth);
        renderer->smap_sampler = data->smap_sampler;
     }
}

static inline void
_renderer_depth_test_enable(E3D_Renderer *renderer, Eina_Bool enable)
{
   if (renderer->depth_test_enable != enable)
     {
        if (enable)
          {
             glEnable(GL_DEPTH_TEST);
             /* Use default depth func. */
          }
        else
          {
             glDisable(GL_DEPTH_TEST);
          }

        renderer->depth_test_enable = enable;
     }
}

E3D_Renderer *
e3d_renderer_new(void)
{
   E3D_Renderer *renderer = NULL;

   renderer = (E3D_Renderer *)calloc(1, sizeof(E3D_Renderer));

   if (renderer == NULL)
     {
        ERR("Failed to allocate memory.");
        return NULL;
     }

   return renderer;
}

void
e3d_renderer_free(E3D_Renderer *renderer)
{
   Eina_List *l;
   E3D_Program *p;

   EINA_LIST_FOREACH(renderer->programs, l, p)
     {
        e3d_program_free(p);
     }

   eina_list_free(renderer->programs);
}

void
e3d_renderer_target_set(E3D_Renderer *renderer, E3D_Drawable *target)
{
   if (renderer->fbo == target->fbo)
     return;

   glBindFramebuffer(GL_FRAMEBUFFER, target->fbo);
   glViewport(0, 0, target->w, target->h);
   renderer->fbo = target->fbo;
   renderer->texDepth = target->texDepth;
}

void
e3d_renderer_clear(E3D_Renderer *renderer EINA_UNUSED, const Evas_Color *color)
{
   glClearColor(color->r, color->g, color->b, color->a);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void
e3d_renderer_draw(E3D_Renderer *renderer, E3D_Draw_Data *data)
{
   Eina_List *l;
   E3D_Program *program = NULL, *p;
   int i, index = 0;

   _renderer_depth_test_enable(renderer, EINA_TRUE);

   EINA_LIST_FOREACH(renderer->programs, l, p)
     {
        if (e3d_program_shade_mode_get(p) == data->mode &&
            e3d_program_shader_flags_get(p) == data->flags)
          {
             program = p;
             break;
          }
     }

   if (program == NULL)
     {
        program = e3d_program_new(data->mode, data->flags);

        if (program == NULL)
          {
             ERR("Failed to create shader program.");
             return;
          }

        renderer->programs = eina_list_append(renderer->programs, program);
     }

   _renderer_program_use(renderer, program);
   e3d_program_uniform_upload(program, data);
   _renderer_texture_bind(renderer, data);

   /* Set up vertex attrib pointers. */
   index = 0;

   for (i = 0; i < EVAS_CANVAS3D_VERTEX_ATTRIB_COUNT; i++)
     {
        const Evas_Canvas3D_Vertex_Buffer *buffer;

        buffer = &data->vertices[i].vertex0;

        if (buffer->data != NULL)
          {
             _renderer_vertex_attrib_array_enable(renderer, index);
             _renderer_vertex_attrib_pointer_set(renderer, index, buffer);
             index++;
          }

        buffer = &data->vertices[i].vertex1;

        if (buffer->data != NULL)
          {
             _renderer_vertex_attrib_array_enable(renderer, index);
             _renderer_vertex_attrib_pointer_set(renderer, index, buffer);
             index++;
          }
     }

   while (index < E3D_MAX_VERTEX_ATTRIB_COUNT)
     _renderer_vertex_attrib_array_disable(renderer, index++);

   if (data->blending)
     {
        glEnable(GL_BLEND);
        glBlendFunc(_gl_blend_func_get(data->blend_sfactor), _gl_blend_func_get(data->blend_dfactor));
     }
   else glDisable(GL_BLEND);

#ifndef GL_GLES
   if (data->alpha_test_enabled)
     {
        glEnable(GL_ALPHA_TEST);
        glAlphaFunc(_gl_comparison_func_get(data->alpha_comparison),
                    (GLclampf)data->alpha_ref_value);
     }
   else glDisable(GL_ALPHA_TEST);
#endif

   if (data->indices)
     {
        _renderer_elements_draw(renderer, data->assembly, data->index_count,
                                data->index_format, data->indices);
     }
   else
     {
        _renderer_array_draw(renderer, data->assembly, data->vertex_count);
     }
}

void
e3d_renderer_flush(E3D_Renderer *renderer EINA_UNUSED)
{
   glFlush();
}