ecere/gfx/GL: Standardized on _GLES for ES 1.1 and _GLES2 for ES 2.0
[sdk] / ecere / src / gfx / drivers / gl3 / glab.ec
1 // GL Array Buffer Manipulation
2
3 #if !defined(_GLES)
4  #define SHADERS
5 #endif
6
7 #if defined(__ANDROID__) || defined(__ODROID__)
8    #include <GLES/gl.h>
9
10    #define GL_INT    0x1404
11    #define GL_DOUBLE 0x140A
12 #elif defined(__EMSCRIPTEN__)
13    #include <GLES2/gl2.h>
14
15 #if !defined(_GLES2)
16    #define _GLES2
17 #endif
18
19    #define GL_INT    0x1404
20    #define GL_DOUBLE 0x140A
21 #else
22 #  if defined(SHADERS)
23 #     include "gl_core_3_3.h"
24 #  else
25 #     include "gl_compat_4_4.h"
26 #  endif
27 #endif
28
29 import "immediate"
30 import "Display"
31 import "OpenGLDisplayDriver"
32
33 // Kept public for now
34 public void GLABDeleteBuffers(int count, GLAB * buffers)
35 {
36    if(vboAvailable)
37    {
38       int i;
39       for(i = 0; i < count; i++)
40       {
41          uint buffer = buffers[i].buffer;
42          if(buffer)
43          {
44             if(buffer == glabCurArrayBuffer)
45                GLABBindBuffer(GL_ARRAY_BUFFER, 0);
46             else if(buffer == glabCurElementBuffer)
47                GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
48          }
49       }
50       if(count && buffers[0].buffer)
51          glDeleteBuffers(count, (GLuint *)buffers);
52    }
53 }
54
55 void GLABBindBuffer(int target, uint buffer)
56 {
57    if(vboAvailable)
58    {
59       glBindBuffer(target, buffer);
60       if(target == GL_ARRAY_BUFFER)
61          glabCurArrayBuffer = buffer;
62       else if(target == GL_ELEMENT_ARRAY_BUFFER)
63          glabCurElementBuffer = buffer;
64    }
65 }
66
67 public enum GLBufferContents { vertex, normal, texCoord, color };
68
69 public define noAB = GLAB { 0 };
70
71 uint glabCurArrayBuffer;
72
73 public struct GLAB
74 {
75    uint buffer;
76
77    void upload(uint size, void * data)
78    {
79       if(this != null)
80       {
81          if(vboAvailable)
82          {
83             if(!buffer)
84                glGenBuffers(1, &buffer);
85             if(glabCurArrayBuffer != buffer)
86                GLABBindBuffer(GL_ARRAY_BUFFER, buffer);
87             glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);  //GL_DYNAMIC_DRAW);
88          }
89          else
90             buffer = 1;
91       }
92    }
93
94    void free()
95    {
96       if(this != null && buffer)
97       {
98          if(vboAvailable)
99             GLABDeleteBuffers(1, this);
100          buffer = 0;
101       }
102    }
103
104    void use(GLBufferContents contents, int n, int type, uint stride, void * pointer)
105    {
106       if(glabCurArrayBuffer != ((this != null) ? buffer : 0) && vboAvailable)
107          GLABBindBuffer(GL_ARRAY_BUFFER, ((this != null) ? buffer : 0));
108 #ifdef SHADERS
109       glVertexAttribPointer(contents, n, type, GL_FALSE, stride, pointer);
110 #else
111       switch(contents)
112       {
113          case normal:   glNormalPointer(type, stride, pointer); break;
114          case vertex:   glVertexPointer(n, type, stride, pointer); break;
115          case texCoord: glTexCoordPointer(n, type, stride, pointer); break;
116          case color:    glColorPointer(n, type, stride, pointer); break;
117       }
118 #endif
119    }
120
121    void useVertTrans(uint count, int n, int type, uint stride, void * pointer)
122    {
123 #if defined(_GLES) || defined(_GLES2)
124       if(glabCurArrayBuffer != ((this != null) ? buffer : 0) && vboAvailable)
125          GLABBindBuffer(GL_ARRAY_BUFFER, ((this != null) ? buffer : 0));
126       if(type == GL_INT)
127          glimtkVertexPointeri(n, stride, pointer, count);
128       else if(type == GL_DOUBLE)
129       {
130 #ifdef SHADERS
131          glVertexAttribPointer(GLBufferContents::vertex, n, GL_DOUBLE, GL_FALSE, stride, pointer);
132 #else
133          glVertexPointer(n, GL_DOUBLE, stride, pointer);
134 #endif
135       }
136 #else
137       use(vertex, n, type, stride, pointer);
138 #endif
139    }
140 };
141
142 uint glabCurElementBuffer;
143
144 public define noEAB = GLEAB { 0 };
145
146 public struct GLEAB
147 {
148    uint buffer;
149
150    void upload(uint size, void * data)
151    {
152       if(this != null)
153       {
154          if(vboAvailable)
155          {
156             if(!buffer)
157                glGenBuffers(1, &buffer);
158
159             if(glabCurElementBuffer != buffer)
160                GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
161             if(size)
162                glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);  //GL_DYNAMIC_DRAW);
163             else
164                ;
165          }
166          else
167             buffer = 1;
168       }
169    }
170
171    void free()
172    {
173       if(this != null && buffer)
174       {
175          if(vboAvailable)
176             GLABDeleteBuffers(1, (GLAB *)this);
177          buffer = 0;
178       }
179    }
180
181    void draw(int primType, int count, int type, void * indices)
182    {
183       if(vboAvailable || !buffer)
184       {
185          if(glabCurElementBuffer != ((this != null) ? buffer : 0))
186             GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ((this != null) ? buffer : 0));
187 #if defined(_GLES) || defined(_GLES2)
188          type = GL_UNSIGNED_SHORT;
189 #endif
190          glDrawElements(primType, count, type, indices);
191       }
192    }
193 };