summaryrefslogtreecommitdiffstats
path: root/opengl/libagl/context.h
blob: c599a55aaf06279ffaf12771cb4e7aec78e49ade (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_OPENGLES_CONTEXT_H
#define ANDROID_OPENGLES_CONTEXT_H

#include <stdint.h>
#include <stddef.h>
#include <sys/types.h>
#include <pthread.h>
#ifdef HAVE_ANDROID_OS
#include <bionic_tls.h>
#endif

#include <private/pixelflinger/ggl_context.h>
#include <hardware/gralloc.h>

#include <GLES/gl.h>
#include <GLES/glext.h>

namespace android {


const unsigned int OGLES_NUM_COMPRESSED_TEXTURE_FORMATS = 10
#ifdef GL_OES_compressed_ETC1_RGB8_texture
        + 1
#endif
        ;

class EGLTextureObject;
class EGLSurfaceManager;
class EGLBufferObjectManager;

namespace gl {

struct ogles_context_t;
struct matrixx_t;
struct transform_t;
struct buffer_t;

ogles_context_t* getGlContext();

template<typename T>
static inline void swap(T& a, T& b) {
    T t(a); a = b; b = t;
}
template<typename T>
inline T max(T a, T b) {
    return a<b ? b : a;
}
template<typename T>
inline T max(T a, T b, T c) {
    return max(a, max(b, c));
}
template<typename T>
inline T min(T a, T b) {
    return a<b ? a : b;
}
template<typename T>
inline T min(T a, T b, T c) {
    return min(a, min(b, c));
}
template<typename T>
inline T min(T a, T b, T c, T d) {
    return min(min(a,b), min(c,d));
}

// ----------------------------------------------------------------------------
// vertices
// ----------------------------------------------------------------------------

struct vec3_t {
    union {
        struct { GLfixed x, y, z; };
        struct { GLfixed r, g, b; };
        struct { GLfixed S, T, R; };
        GLfixed v[3];
    };
};

struct vec4_t {
    union {
        struct { GLfixed x, y, z, w; };
        struct { GLfixed r, g, b, a; };
        struct { GLfixed S, T, R, Q; };
        GLfixed v[4];
    };
};

struct vertex_t {
    enum {
        // these constant matter for our clipping
        CLIP_L          = 0x0001,   // clipping flags
        CLIP_R          = 0x0002,
        CLIP_B          = 0x0004,
        CLIP_T          = 0x0008,
        CLIP_N          = 0x0010,
        CLIP_F          = 0x0020,

        EYE             = 0x0040,
        RESERVED        = 0x0080,

        USER_CLIP_0     = 0x0100,   // user clipping flags
        USER_CLIP_1     = 0x0200,
        USER_CLIP_2     = 0x0400,
        USER_CLIP_3     = 0x0800,
        USER_CLIP_4     = 0x1000,
        USER_CLIP_5     = 0x2000,

        LIT             = 0x4000,   // lighting has been applied
        TT              = 0x8000,   // texture coords transformed

        FRUSTUM_CLIP_ALL= 0x003F,
        USER_CLIP_ALL   = 0x3F00,
        CLIP_ALL        = 0x3F3F,
    };

    // the fields below are arranged to minimize d-cache usage
    // we group together, by cache-line, the fields most likely to be used

    union {
    vec4_t          obj;
    vec4_t          eye;
    };
    vec4_t          clip;

    uint32_t        flags;
    size_t          index;  // cache tag, and vertex index
    GLfixed         fog;
    uint8_t         locked;
    uint8_t         mru;
    uint8_t         reserved[2];
    vec4_t          window;

    vec4_t          color;
    vec4_t          texture[GGL_TEXTURE_UNIT_COUNT];
#ifdef __LP64__
    uint32_t        reserved1[2];
#else
    uint32_t        reserved1[4];
#endif

    inline void clear() {
        flags = index = locked = mru = 0;
    }
};

struct point_size_t {
    GGLcoord    size;
    GLboolean   smooth;
};

struct line_width_t {
    GGLcoord    width;
    GLboolean   smooth;
};

struct polygon_offset_t {
    GLfixed     factor;
    GLfixed     units;
    GLboolean   enable;
};

// ----------------------------------------------------------------------------
// arrays
// ----------------------------------------------------------------------------

struct array_t {
    typedef void (*fetcher_t)(ogles_context_t*, GLfixed*, const GLvoid*);
    fetcher_t       fetch;
    GLvoid const*   physical_pointer;
    GLint           size;
    GLsizei         stride;
    GLvoid const*   pointer;
    buffer_t const* bo;
    uint16_t        type;
    GLboolean       enable;
    GLboolean       pad;
    GLsizei         bounds;
    void init(GLint, GLenum, GLsizei, const GLvoid *, const buffer_t*, GLsizei);
    inline void resolve();
    inline const GLubyte* element(GLint i) const {
        return (const GLubyte*)physical_pointer + i * stride;
    }
};

struct array_machine_t {
    array_t         vertex;
    array_t         normal;
    array_t         color;
    array_t         texture[GGL_TEXTURE_UNIT_COUNT];
    uint8_t         activeTexture;
    uint8_t         tmu;
    uint16_t        cull;
    uint32_t        flags;
    GLenum          indicesType;
    buffer_t const* array_buffer;
    buffer_t const* element_array_buffer;

    void (*compileElements)(ogles_context_t*, vertex_t*, GLint, GLsizei);
    void (*compileElement)(ogles_context_t*, vertex_t*, GLint);

    void (*mvp_transform)(transform_t const*, vec4_t*, vec4_t const*);
    void (*mv_transform)(transform_t const*, vec4_t*, vec4_t const*);
    void (*tex_transform[2])(transform_t const*, vec4_t*, vec4_t const*);
    void (*perspective)(ogles_context_t*c, vertex_t* v);
    void (*clipVertex)(ogles_context_t* c, vertex_t* nv,
            GGLfixed t, const vertex_t* s, const vertex_t* p);
    void (*clipEye)(ogles_context_t* c, vertex_t* nv,
            GGLfixed t, const vertex_t* s, const vertex_t* p);
};

struct vertex_cache_t {
    enum {
        // must be at least 4
        // 3 vertice for triangles
        // or 2 + 2 for indexed triangles w/ cache contention
        VERTEX_BUFFER_SIZE  = 8,
        // must be a power of two and at least 3
        VERTEX_CACHE_SIZE   = 64,   // 8 KB

        INDEX_BITS      = 16,
        INDEX_MASK      = ((1LU<<INDEX_BITS)-1),
        INDEX_SEQ       = 1LU<<INDEX_BITS,
    };
    vertex_t*       vBuffer;
    vertex_t*       vCache;
    uint32_t        sequence;
    void*           base;
    uint32_t        total;
    uint32_t        misses;
    int64_t         startTime;
    void init();
    void uninit();
    void clear();
    void dump_stats(GLenum mode);
};

// ----------------------------------------------------------------------------
// fog
// ----------------------------------------------------------------------------

struct fog_t {
    GLfixed     density;
    GLfixed     start;
    GLfixed     end;
    GLfixed     invEndMinusStart;
    GLenum      mode;
    GLfixed     (*fog)(ogles_context_t* c, GLfixed z);
};

// ----------------------------------------------------------------------------
// user clip planes
// ----------------------------------------------------------------------------

const unsigned int OGLES_MAX_CLIP_PLANES = 6;

struct clip_plane_t {
    vec4_t      equation;
};

struct user_clip_planes_t {
    clip_plane_t    plane[OGLES_MAX_CLIP_PLANES];
    uint32_t        enable;
};

// ----------------------------------------------------------------------------
// lighting
// ----------------------------------------------------------------------------

const unsigned int OGLES_MAX_LIGHTS = 8;

struct light_t {
    vec4_t      ambient;
    vec4_t      diffuse;
    vec4_t      specular;
    vec4_t      implicitAmbient;
    vec4_t      implicitDiffuse;
    vec4_t      implicitSpecular;
    vec4_t      position;       // position in eye space
    vec4_t      objPosition;
    vec4_t      normalizedObjPosition;
    vec4_t      spotDir;
    vec4_t      normalizedSpotDir;
    GLfixed     spotExp;
    GLfixed     spotCutoff;
    GLfixed     spotCutoffCosine;
    GLfixed     attenuation[3];
    GLfixed     rConstAttenuation;
    GLboolean   enable;
};

struct material_t {
    vec4_t      ambient;
    vec4_t      diffuse;
    vec4_t      specular;
    vec4_t      emission;
    GLfixed     shininess;
};

struct light_model_t {
    vec4_t      ambient;
    GLboolean   twoSide;
};

struct color_material_t {
    GLenum      face;
    GLenum      mode;
    GLboolean   enable;
};

struct lighting_t {
    light_t             lights[OGLES_MAX_LIGHTS];
    material_t          front;
    light_model_t       lightModel;
    color_material_t    colorMaterial;
    vec4_t              implicitSceneEmissionAndAmbient;
    vec4_t              objViewer;
    uint32_t            enabledLights;
    GLboolean           enable;
    GLenum              shadeModel;
    typedef void (*light_fct_t)(ogles_context_t*, vertex_t*);
    void (*lightVertex)(ogles_context_t* c, vertex_t* v);
    void (*lightTriangle)(ogles_context_t* c,
            vertex_t* v0, vertex_t* v1, vertex_t* v2);
};

struct culling_t {
    GLenum      cullFace;
    GLenum      frontFace;
    GLboolean   enable;
};

// ----------------------------------------------------------------------------
// textures
// ----------------------------------------------------------------------------

struct texture_unit_t {
    GLuint              name;
    EGLTextureObject*   texture;
    uint8_t             dirty;
};

struct texture_state_t
{
    texture_unit_t      tmu[GGL_TEXTURE_UNIT_COUNT];
    int                 active;     // active tmu
    EGLTextureObject*   defaultTexture;
    GGLContext*         ggl;
    uint8_t             packAlignment;
    uint8_t             unpackAlignment;
};

// ----------------------------------------------------------------------------
// transformation and matrices
// ----------------------------------------------------------------------------

struct matrixf_t;

struct matrixx_t {
    GLfixed m[16];
    void load(const matrixf_t& rhs);
};

struct matrix_stack_t;


struct matrixf_t {
    void loadIdentity();
    void load(const matrixf_t& rhs);

    inline GLfloat* editElements() { return m; }
    inline GLfloat const* elements() const { return m; }

    void set(const GLfixed* rhs);
    void set(const GLfloat* rhs);

    static void multiply(matrixf_t& r,
            const matrixf_t& lhs, const matrixf_t& rhs);

    void dump(const char* what);

private:
    friend struct matrix_stack_t;
    GLfloat     m[16];
    void load(const GLfixed* rhs);
    void load(const GLfloat* rhs);
    void multiply(const matrixf_t& rhs);
    void translate(GLfloat x, GLfloat y, GLfloat z);
    void scale(GLfloat x, GLfloat y, GLfloat z);
    void rotate(GLfloat a, GLfloat x, GLfloat y, GLfloat z);
};

enum {
    OP_IDENTITY         = 0x00,
    OP_TRANSLATE        = 0x01,
    OP_UNIFORM_SCALE    = 0x02,
    OP_SCALE            = 0x05,
    OP_ROTATE           = 0x08,
    OP_SKEW             = 0x10,
    OP_ALL              = 0x1F
};

struct transform_t {
    enum {
        FLAGS_2D_PROJECTION = 0x1
    };
    matrixx_t       matrix;
    uint32_t        flags;
    uint32_t        ops;

    union {
        struct {
            void (*point2)(transform_t const* t, vec4_t*, vec4_t const*);
            void (*point3)(transform_t const* t, vec4_t*, vec4_t const*);
            void (*point4)(transform_t const* t, vec4_t*, vec4_t const*);
        };
        void (*pointv[3])(transform_t const* t, vec4_t*, vec4_t const*);
    };

    void loadIdentity();
    void picker();
    void dump(const char* what);
};

struct mvui_transform_t : public transform_t
{
    void picker();
};

struct matrix_stack_t {
    enum {
        DO_PICKER           = 0x1,
        DO_FLOAT_TO_FIXED   = 0x2
    };
    transform_t     transform;
    uint8_t         maxDepth;
    uint8_t         depth;
    uint8_t         dirty;
    uint8_t         reserved;
    matrixf_t       *stack;
    uint8_t         *ops;
    void init(int depth);
    void uninit();
    void loadIdentity();
    void load(const GLfixed* rhs);
    void load(const GLfloat* rhs);
    void multiply(const matrixf_t& rhs);
    void translate(GLfloat x, GLfloat y, GLfloat z);
    void scale(GLfloat x, GLfloat y, GLfloat z);
    void rotate(GLfloat a, GLfloat x, GLfloat y, GLfloat z);
    GLint push();
    GLint pop();
    void validate();
    matrixf_t& top() { return stack[depth]; }
    const matrixf_t& top() const { return stack[depth]; }
    uint32_t top_ops() const { return ops[depth]; }
    inline bool isRigidBody() const {
        return !(ops[depth] & ~(OP_TRANSLATE|OP_UNIFORM_SCALE|OP_ROTATE));
    }
};

struct vp_transform_t {
    transform_t     transform;
    matrixf_t       matrix;
    GLfloat         zNear;
    GLfloat         zFar;
    void loadIdentity();
};

struct transform_state_t {
    enum {
        MODELVIEW           = 0x01,
        PROJECTION          = 0x02,
        VIEWPORT            = 0x04,
        TEXTURE             = 0x08,
        MVUI                = 0x10,
        MVIT                = 0x20,
        MVP                 = 0x40,
    };
    matrix_stack_t      *current;
    matrix_stack_t      modelview;
    matrix_stack_t      projection;
    matrix_stack_t      texture[GGL_TEXTURE_UNIT_COUNT];

    // modelview * projection
    transform_t         mvp     __attribute__((aligned(32)));
    // viewport transformation
    vp_transform_t      vpt     __attribute__((aligned(32)));
    // same for 4-D vertices
    transform_t         mvp4;
    // full modelview inverse transpose
    transform_t         mvit4;
    // upper 3x3 of mv-inverse-transpose (for normals)
    mvui_transform_t    mvui;

    GLenum              matrixMode;
    GLenum              rescaleNormals;
    uint32_t            dirty;
    void invalidate();
    void update_mvp();
    void update_mvit();
    void update_mvui();
};

struct viewport_t {
    GLint       x;
    GLint       y;
    GLsizei     w;
    GLsizei     h;
    struct {
        GLint       x;
        GLint       y;
    } surfaceport;
    struct {
        GLint       x;
        GLint       y;
        GLsizei     w;
        GLsizei     h;
    } scissor;
};

// ----------------------------------------------------------------------------
// Lerping
// ----------------------------------------------------------------------------

struct compute_iterators_t
{
    void initTriangle(
            vertex_t const* v0,
            vertex_t const* v1,
            vertex_t const* v2);

    void initLine(
            vertex_t const* v0,
            vertex_t const* v1);

    inline void initLerp(vertex_t const* v0, uint32_t enables);

    int iteratorsScale(int32_t it[3],
            int32_t c0, int32_t c1, int32_t c2) const;

    void iterators1616(GGLfixed it[3],
            GGLfixed c0, GGLfixed c1, GGLfixed c2) const;

    void iterators0032(int32_t it[3],
            int32_t c0, int32_t c1, int32_t c2) const;

    void iterators0032(int64_t it[3],
            int32_t c0, int32_t c1, int32_t c2) const;

    GGLcoord area() const { return m_area; }

private:
    // don't change order of members here -- used by iterators.S
    GGLcoord m_dx01, m_dy10, m_dx20, m_dy02;
    GGLcoord m_x0, m_y0;
    GGLcoord m_area;
    uint8_t m_scale;
    uint8_t m_area_scale;
    uint8_t m_reserved[2];

};

// ----------------------------------------------------------------------------
// state
// ----------------------------------------------------------------------------

#ifdef HAVE_ANDROID_OS
    // We have a dedicated TLS slot in bionic
    inline void setGlThreadSpecific(ogles_context_t *value) {
        __get_tls()[TLS_SLOT_OPENGL] = value;
    }
    inline ogles_context_t* getGlThreadSpecific() {
        return static_cast<ogles_context_t*>(__get_tls()[TLS_SLOT_OPENGL]);
    }
#else
    extern pthread_key_t gGLKey;
    inline void setGlThreadSpecific(ogles_context_t *value) {
        pthread_setspecific(gGLKey, value);
    }
    inline ogles_context_t* getGlThreadSpecific() {
        return static_cast<ogles_context_t*>(pthread_getspecific(gGLKey));
    }
#endif


struct prims_t {
    typedef ogles_context_t* GL;
    void (*renderPoint)(GL, vertex_t*);
    void (*renderLine)(GL, vertex_t*, vertex_t*);
    void (*renderTriangle)(GL, vertex_t*, vertex_t*, vertex_t*);
};

struct ogles_context_t {
    context_t               rasterizer;
    array_machine_t         arrays         __attribute__((aligned(32)));
    texture_state_t         textures;
    transform_state_t       transforms;
    vertex_cache_t          vc;
    prims_t                 prims;
    culling_t               cull;
    lighting_t              lighting;
    user_clip_planes_t      clipPlanes;
    compute_iterators_t     lerp;           __attribute__((aligned(32)));
    vertex_t                current;
    vec4_t                  currentColorClamped;
    vec3_t                  currentNormal;
    viewport_t              viewport;
    point_size_t            point;
    line_width_t            line;
    polygon_offset_t        polygonOffset;
    fog_t                   fog;
    uint32_t                perspective : 1;
    uint32_t                transformTextures : 1;
    EGLSurfaceManager*      surfaceManager;
    EGLBufferObjectManager* bufferObjectManager;

    GLenum                  error;

    static inline ogles_context_t* get() {
        return getGlThreadSpecific();
    }

};

}; // namespace gl
}; // namespace android

using namespace android::gl;

#endif // ANDROID_OPENGLES_CONTEXT_H