summaryrefslogtreecommitdiffstats
path: root/opengl/libagl2/src/vertex.cpp
blob: 021b82b76371afd25f67f72c45af1d3616e12841 (plain)
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
#include "gles2context.h"

//#undef LOGD
//#define LOGD(...)

void GLES2Context::InitializeVertices()
{
   vert.vbos = std::map<GLuint, VBO *>(); // the entire struct has been zeroed in constructor
   vert.free = 1;
   vert.vbo = NULL;
   vert.indices = NULL;
   for (unsigned i = 0; i < GGL_MAXVERTEXATTRIBS; i++)
      vert.defaultAttribs[i] = Vector4(0,0,0,1);
}

void GLES2Context::UninitializeVertices()
{
   for (std::map<GLuint, VBO *>::iterator it = vert.vbos.begin(); it != vert.vbos.end(); it++) {
      if (!it->second)
         continue;
      free(it->second->data);
      free(it->second);
   }
}

static inline void FetchElement(const GLES2Context * ctx, const unsigned index,
                                const unsigned maxAttrib, VertexInput * elem)
{
   for (unsigned i = 0; i < maxAttrib; i++) {
      {
         unsigned size = 0;
         if (ctx->vert.attribs[i].enabled) {
            const char * ptr = (const char *)ctx->vert.attribs[i].ptr;
            ptr += ctx->vert.attribs[i].stride * index;
            memcpy(elem->attributes + i, ptr, ctx->vert.attribs[i].size * sizeof(float));
            size = ctx->vert.attribs[i].size;
//            LOGD("agl2: FetchElement %d attribs size=%d %.2f,%.2f,%.2f,%.2f", i, size, elem->attributes[i].x,
//                 elem->attributes[i].y, elem->attributes[i].z, elem->attributes[i].w);
         } else {
//            LOGD("agl2: FetchElement %d default %.2f,%.2f,%.2f,%.2f", i, ctx->vert.defaultAttribs[i].x,
//                 ctx->vert.defaultAttribs[i].y, ctx->vert.defaultAttribs[i].z, ctx->vert.defaultAttribs[i].w);
         }

         switch (size) {
         case 0: // fall through
            elem->attributes[i].x = ctx->vert.defaultAttribs[i].x;
         case 1: // fall through
            elem->attributes[i].y = ctx->vert.defaultAttribs[i].y;
         case 2: // fall through
            elem->attributes[i].z = ctx->vert.defaultAttribs[i].z;
         case 3: // fall through
            elem->attributes[i].w = ctx->vert.defaultAttribs[i].w;
         case 4:
            break;
         default:
            assert(0);
            break;
         }
//         LOGD("agl2: FetchElement %d size=%d %.2f,%.2f,%.2f,%.2f", i, size, elem->attributes[i].x,
//              elem->attributes[i].y, elem->attributes[i].z, elem->attributes[i].w);
      }
   }
}

template<typename IndexT> static void DrawElementsTriangles(const GLES2Context * ctx,
      const unsigned count, const IndexT * indices, const unsigned maxAttrib)
{
   VertexInput v[3];
   if (ctx->vert.indices)
      indices = (IndexT *)((char *)ctx->vert.indices->data + (long)indices);
   for (unsigned i = 0; i < count; i += 3) {
      for (unsigned j = 0; j < 3; j++)
         FetchElement(ctx, indices[i + j], maxAttrib, v + j);
      ctx->iface->DrawTriangle(ctx->iface, v, v + 1, v + 2);
   }
}

static void DrawArraysTriangles(const GLES2Context * ctx, const unsigned first,
                                const unsigned count, const unsigned maxAttrib)
{
//   LOGD("agl: DrawArraysTriangles=%p", DrawArraysTriangles);
   VertexInput v[3];
   for (unsigned i = 2; i < count; i+=3) {
      // TODO: fix order
      FetchElement(ctx, first + i - 2, maxAttrib, v + 0);
      FetchElement(ctx, first + i - 1, maxAttrib, v + 1);
      FetchElement(ctx, first + i - 0, maxAttrib, v + 2);
      ctx->iface->DrawTriangle(ctx->iface, v + 0, v + 1, v + 2);
   }
//   LOGD("agl: DrawArraysTriangles end");
}

template<typename IndexT> static void DrawElementsTriangleStrip(const GLES2Context * ctx,
      const unsigned count, const IndexT * indices, const unsigned maxAttrib)
{
   VertexInput v[3];
   if (ctx->vert.indices)
      indices = (IndexT *)((char *)ctx->vert.indices->data + (long)indices);
      
//   LOGD("agl2: DrawElementsTriangleStrip");
//   for (unsigned i = 0; i < count; i++)
//      LOGD("indices[%d] = %d", i, indices[i]);

   FetchElement(ctx, indices[0], maxAttrib, v + 0);
   FetchElement(ctx, indices[1], maxAttrib, v + 1);
   for (unsigned i = 2; i < count; i ++) {
      FetchElement(ctx, indices[i], maxAttrib, v + i % 3);
      ctx->iface->DrawTriangle(ctx->iface, v + (i - 2) % 3, v + (i - 1) % 3 , v + (i + 0) % 3);
   }

//   for (unsigned i = 2; i < count; i++) {
//      FetchElement(ctx, indices[i - 2], maxAttrib, v + 0);
//      FetchElement(ctx, indices[i - 1], maxAttrib, v + 1);
//      FetchElement(ctx, indices[i - 0], maxAttrib, v + 2);
//      ctx->iface->DrawTriangle(ctx->iface, v + 0, v + 1, v + 2);
//   }
}

static void DrawArraysTriangleStrip(const GLES2Context * ctx, const unsigned first,
                                    const unsigned count, const unsigned maxAttrib)
{
   VertexInput v[3];
   FetchElement(ctx, first, maxAttrib, v + 0);
   FetchElement(ctx, first + 1, maxAttrib, v + 1);
   for (unsigned i = 2; i < count; i++) {
      // TODO: fix order
      FetchElement(ctx, first + i, maxAttrib, v + i % 3);
      ctx->iface->DrawTriangle(ctx->iface, v + (i - 2) % 3, v + (i - 1) % 3 , v + (i + 0) % 3);
   }
}

void glBindBuffer(GLenum target, GLuint buffer)
{
   GLES2_GET_CONST_CONTEXT(ctx);
   VBO * vbo = NULL;
   if (0 != buffer) {
      std::map<GLuint, VBO *>::iterator it = ctx->vert.vbos.find(buffer);
      if (it != ctx->vert.vbos.end()) {
         vbo = it->second;
         if (!vbo)
            vbo = (VBO *)calloc(1, sizeof(VBO));
         it->second = vbo;
      } else
         assert(0);
   }
   if (GL_ARRAY_BUFFER == target)
      ctx->vert.vbo = vbo;
   else if (GL_ELEMENT_ARRAY_BUFFER == target)
      ctx->vert.indices = vbo;
   else
      assert(0);
   assert(vbo || buffer == 0);
//   LOGD("\n*\n glBindBuffer 0x%.4X=%d ", target, buffer);
}

void glBufferData(GLenum target, GLsizeiptr size, const GLvoid* data, GLenum usage)
{
   GLES2_GET_CONST_CONTEXT(ctx);
   if (GL_ARRAY_BUFFER == target) {
      assert(ctx->vert.vbo);
      ctx->vert.vbo->data = realloc(ctx->vert.vbo->data, size);
      ctx->vert.vbo->size = size;
      ctx->vert.vbo->usage = usage;
      if (data)
         memcpy(ctx->vert.vbo->data, data, size);
   } else if (GL_ELEMENT_ARRAY_BUFFER == target) {
      assert(ctx->vert.indices);
      ctx->vert.indices->data = realloc(ctx->vert.indices->data, size);
      ctx->vert.indices->size = size;
      ctx->vert.indices->usage = usage;
      if (data)
         memcpy(ctx->vert.indices->data, data, size);
   } else
      assert(0);
//   LOGD("\n*\n glBufferData target=0x%.4X size=%u data=%p usage=0x%.4X \n",
//        target, size, data, usage);
}

void glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid* data)
{
   GLES2_GET_CONST_CONTEXT(ctx);
   if (GL_ARRAY_BUFFER == target)
   {
      assert(ctx->vert.vbo);
      assert(0 <= offset);
      assert(0 <= size);
      assert(offset + size <= ctx->vert.vbo->size);
      memcpy((char *)ctx->vert.vbo->data + offset, data, size);
   }
   else
      assert(0);
}

void glDeleteBuffers(GLsizei n, const GLuint* buffers)
{
   GLES2_GET_CONST_CONTEXT(ctx);
   for (unsigned i = 0; i < n; i++) {
      std::map<GLuint, VBO*>::iterator it = ctx->vert.vbos.find(buffers[i]);
      if (it == ctx->vert.vbos.end())
         continue;
      ctx->vert.free = min(ctx->vert.free, buffers[i]);
      if (it->second == ctx->vert.vbo)
         ctx->vert.vbo = NULL;
      else if (it->second == ctx->vert.indices)
         ctx->vert.indices = NULL;
      if (it->second) {
         free(it->second->data);
         free(it->second);
      }
   }
}

void glDisableVertexAttribArray(GLuint index)
{
   GLES2_GET_CONST_CONTEXT(ctx);
   assert(GGL_MAXVERTEXATTRIBS > index);
   ctx->vert.attribs[index].enabled = false;
}

void glDrawArrays(GLenum mode, GLint first, GLsizei count)
{
   GLES2_GET_CONST_CONTEXT(ctx);
//   LOGD("agl2: glDrawArrays=%p", glDrawArrays);
   assert(ctx->rasterizer.CurrentProgram);
   assert(0 <= first);
   int maxAttrib = -1;
   ctx->iface->ShaderProgramGetiv(ctx->rasterizer.CurrentProgram, GL_ACTIVE_ATTRIBUTES, &maxAttrib);
   assert(0 <= maxAttrib && GGL_MAXVERTEXATTRIBS >= maxAttrib);
   switch (mode) {
   case GL_TRIANGLE_STRIP:
      DrawArraysTriangleStrip(ctx, first, count, maxAttrib);
      break;
   case GL_TRIANGLES:
      DrawArraysTriangles(ctx, first, count, maxAttrib);
      break;
   default:
      LOGE("agl2: glDrawArrays unsupported mode: 0x%.4X \n", mode);
      assert(0);
      break;
   }
//   LOGD("agl2: glDrawArrays end");
}

void glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid* indices)
{
   GLES2_GET_CONST_CONTEXT(ctx);
//   LOGD("agl2: glDrawElements=%p mode=0x%.4X count=%d type=0x%.4X indices=%p",
//        glDrawElements, mode, count, type, indices);
   if (!ctx->rasterizer.CurrentProgram)
      return;

   int maxAttrib = -1;
   ctx->iface->ShaderProgramGetiv(ctx->rasterizer.CurrentProgram, GL_ACTIVE_ATTRIBUTES, &maxAttrib);
   assert(0 <= maxAttrib && GGL_MAXVERTEXATTRIBS >= maxAttrib);
//   LOGD("agl2: glDrawElements mode=0x%.4X type=0x%.4X count=%d program=%p indices=%p \n",
//        mode, type, count, ctx->rasterizer.CurrentProgram, indices);
   switch (mode) {
   case GL_TRIANGLES:
      if (GL_UNSIGNED_SHORT == type)
         DrawElementsTriangles<unsigned short>(ctx, count, (unsigned short *)indices, maxAttrib);
      else
         assert(0);
      break;
   case GL_TRIANGLE_STRIP:
      if (GL_UNSIGNED_SHORT == type)
         DrawElementsTriangleStrip<unsigned short>(ctx, count, (unsigned short *)indices, maxAttrib);
      else
         assert(0);
      break;
   default:
      assert(0);
   }
//   LOGD("agl2: glDrawElements end");
}

void glEnableVertexAttribArray(GLuint index)
{
   GLES2_GET_CONST_CONTEXT(ctx);
   ctx->vert.attribs[index].enabled = true;
//   LOGD("agl2: glEnableVertexAttribArray %d \n", index);
}

void glGenBuffers(GLsizei n, GLuint* buffers)
{
   GLES2_GET_CONST_CONTEXT(ctx);
   for (unsigned i = 0; i < n; i++) {
      buffers[i] = 0;
      for (ctx->vert.free; ctx->vert.free < 0xffffffffu; ctx->vert.free++) {
         if (ctx->vert.vbos.find(ctx->vert.free) == ctx->vert.vbos.end()) {
            ctx->vert.vbos[ctx->vert.free] = NULL;
            buffers[i] = ctx->vert.free;
//            LOGD("glGenBuffers %d \n", buffers[i]);
            ctx->vert.free++;
            break;
         }
      }
      assert(buffers[i]);
   }
}

void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized,
                           GLsizei stride, const GLvoid* ptr)
{
   GLES2_GET_CONST_CONTEXT(ctx);
   assert(GL_FLOAT == type);
   assert(0 < size && 4 >= size);
   ctx->vert.attribs[index].size = size;
   ctx->vert.attribs[index].type = type;
   ctx->vert.attribs[index].normalized = normalized;
   if (0 == stride)
      ctx->vert.attribs[index].stride = size * sizeof(float);
   else if (stride > 0)
      ctx->vert.attribs[index].stride = stride;
   else
      assert(0);
//   LOGD("\n*\n*\n* agl2: glVertexAttribPointer program=%u index=%d size=%d stride=%d ptr=%p \n*\n*",
//        unsigned(ctx->rasterizer.CurrentProgram) ^ 0x04dc18f9, index, size, stride, ptr);
   if (ctx->vert.vbo)
      ctx->vert.attribs[index].ptr = (char *)ctx->vert.vbo->data + (long)ptr;
   else
      ctx->vert.attribs[index].ptr = ptr;
//   const float * attrib = (const float *)ctx->vert.attribs[index].ptr;
//   for (unsigned i = 0; i < 3; i++)
//      if (3 == size)
//         LOGD("%.2f %.2f %.2f", attrib[i * 3 + 0], attrib[i * 3 + 1], attrib[i * 3 + 2]);
//      else if (2 == size)
//         LOGD("%.2f %.2f", attrib[i * 3 + 0], attrib[i * 3 + 1]);
   
}

void glVertexAttrib1f(GLuint indx, GLfloat x)
{
   glVertexAttrib4f(indx, x,0,0,1);
}

void glVertexAttrib1fv(GLuint indx, const GLfloat* values)
{
   glVertexAttrib4f(indx, values[0],0,0,1);
}

void glVertexAttrib2f(GLuint indx, GLfloat x, GLfloat y)
{
   glVertexAttrib4f(indx, x,y,0,1);
}

void glVertexAttrib2fv(GLuint indx, const GLfloat* values)
{
   glVertexAttrib4f(indx, values[0],values[1],0,1);
}

void glVertexAttrib3f(GLuint indx, GLfloat x, GLfloat y, GLfloat z)
{
   glVertexAttrib4f(indx, x,y,z,1);
}

void glVertexAttrib3fv(GLuint indx, const GLfloat* values)
{
   glVertexAttrib4f(indx, values[0],values[1],values[2],1);
}

void glVertexAttrib4f(GLuint indx, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
{
   assert(GGL_MAXVERTEXATTRIBS > indx);
   GLES2_GET_CONST_CONTEXT(ctx);
//   LOGD("\n*\n*\n agl2: glVertexAttrib4f %d %.2f,%.2f,%.2f,%.2f \n*\n*", indx, x, y, z, w);
   ctx->vert.defaultAttribs[indx] = Vector4(x,y,z,w);
   assert(0);
}

void glVertexAttrib4fv(GLuint indx, const GLfloat* values)
{
   glVertexAttrib4f(indx, values[0], values[1], values[2], values[3]);
}