Home | History | Annotate | Download | only in pixelflinger2
      1 /**
      2  **
      3  ** Copyright 2010, The Android Open Source Project
      4  **
      5  ** Licensed under the Apache License, Version 2.0 (the "License");
      6  ** you may not use this file except in compliance with the License.
      7  ** You may obtain a copy of the License at
      8  **
      9  **     http://www.apache.org/licenses/LICENSE-2.0
     10  **
     11  ** Unless required by applicable law or agreed to in writing, software
     12  ** distributed under the License is distributed on an "AS IS" BASIS,
     13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  ** See the License for the specific language governing permissions and
     15  ** limitations under the License.
     16  */
     17 
     18 #ifndef _PIXELFLINGER2_INTERFACE_H_
     19 #define _PIXELFLINGER2_INTERFACE_H_
     20 
     21 #include "GLES2/gl2.h"
     22 #include "pixelflinger2/pixelflinger2_format.h"
     23 #include "pixelflinger2/pixelflinger2_constants.h"
     24 #include "pixelflinger2/pixelflinger2_vector4.h"
     25 
     26 typedef struct gl_shader gl_shader_t;
     27 typedef struct gl_shader_program gl_shader_program_t;
     28 
     29 typedef struct VertexInput {
     30    Vector4 attributes[GGL_MAXVERTEXATTRIBS]; // vert input
     31 }
     32 #ifndef __arm__
     33 __attribute__ ((aligned (16))) // LLVM generates movaps on X86, needs 16 bytes align
     34 #endif
     35 VertexInput_t;
     36 
     37 // the layout must NOT change, and must match the #defines in constants.h
     38 typedef struct VertexOutput {
     39    Vector4 pointSize; // vert output
     40    Vector4 position; // vert output and frag input gl_FragCoord
     41    Vector4 varyings[GGL_MAXVARYINGVECTORS];
     42    Vector4 frontFacingPointCoord; // frag input, gl_FrontFacing gl_PointCoord yzw
     43    Vector4 fragColor[GGL_MAXDRAWBUFFERS]; // frag output, gl_FragData
     44 }
     45 #ifndef __arm__
     46 __attribute__ ((aligned (16)))
     47 #endif
     48 VertexOutput_t ;
     49 
     50 typedef struct GGLSurface {
     51    unsigned width, height;
     52    enum GGLPixelFormat format;
     53    void * data;
     54    unsigned stride, version;
     55 } GGLSurface_t;
     56 
     57 typedef struct GGLTexture {
     58    unsigned type; // GL_TEXTURE_2D, or GL_TEXTURE_CUBE_MAP
     59 
     60    // currently only support RGBA_8888, RGBX_8888 and RGB_565
     61    // storage uses either int or short
     62    enum GGLPixelFormat format; // affects vs/fs jit
     63 
     64    unsigned width, height; // base level dimension
     65    unsigned levelCount; // mipmapped texture requires power-of-2 width and height
     66 
     67    // data layout is level 0 of first surface (cubemap +x), level 0 of second surface (for cube map, -x),
     68    // level 0 of 3rd surface (cubemap +y), cubemap level 0 -y, cubemap level 0 +z,
     69    // cubemap level 0 -z, level 1 of first surface,
     70    // then level 1 of 1st surface, level 1 of 2nd surface ....
     71    void * levels;
     72 
     73    // the following affects vs/fs jit; must fit in byte; size used in GetShaderKey
     74    enum GGLTextureWrap {
     75       GGL_REPEAT = 0, GGL_CLAMP_TO_EDGE = 1, GGL_MIRRORED_REPEAT = 2
     76 } wrapS :
     77 2, wrapT :
     78    2;
     79 
     80    enum GGLTextureMinFilter {
     81       GGL_NEAREST = 0, GGL_LINEAR, /*GGL_NEAREST_MIPMAP_NEAREST = 2,
     82       GGL_LINEAR_MIPMAP_NEAREST, GGL_NEAREST_MIPMAP_LINEAR, GGL_LINEAR_MIPMAP_LINEAR = 5*/
     83 } minFilter :
     84 1, magFilter :
     85    1; // only GGL_NEAREST and GGL_LINEAR
     86 } GGLTexture_t;
     87 
     88 typedef struct GGLStencilState {
     89    unsigned char ref, mask; // ref is masked during StencilFuncSeparate
     90 
     91    // GL_NEVER = 0, GL_LESS, GL_EQUAL, GL_LEQUAL, GL_GREATER, GL_NOTEQUAL, GL_GEQUAL,
     92    // GL_ALWAYS; value = GLenum  & 0x7 (GLenum is 0x200-0x207)
     93    unsigned char func; // compare function
     94 
     95    // GL_ZERO = 0, GL_KEEP = 1, GL_REPLACE, GL_INCR, GL_DECR, GL_INVERT, GL_INCR_WRAP,
     96    // GL_DECR_WRAP = 7; value = 0 | GLenum - GL_KEEP | GL_INVERT | GLenum - GL_INCR_WRAP
     97    unsigned char sFail, dFail, dPass; // operations
     98 }  GGLStencilState_t;
     99 
    100 typedef struct GGLActiveStencil { // do not change layout, used in GenerateScanLine
    101    unsigned char face; // FRONT = 0, BACK = 1
    102    unsigned char ref, mask;
    103 } GGLActiveStencil_t;
    104 
    105 typedef struct GGLBufferState { // all affect scanline jit
    106    enum GGLPixelFormat colorFormat, depthFormat, stencilFormat;
    107 unsigned stencilTest :
    108    1;
    109 unsigned depthTest :
    110    1;
    111    // same as sf/bFunc; GL_NEVER = 0, GL_LESS, GL_EQUAL, GL_LEQUAL, GL_GREATER, GL_NOTEQUAL,
    112    // GL_GEQUAL, GL_ALWAYS = 7; value = GLenum  & 0x7 (GLenum is 0x200-0x207)
    113 unsigned depthFunc :
    114    3;
    115 } GGLBufferState_t;
    116 
    117 typedef struct GGLBlendState { // all values affect scanline jit
    118    unsigned char color[4]; // rgba[0,255]
    119 
    120    // value = 0,1 | GLenum - GL_SRC_COLOR + 2 | GLenum - GL_CONSTANT_COLOR + 11
    121    enum GGLBlendFactor {
    122       GGL_ZERO = 0, GGL_ONE, GGL_SRC_COLOR = 2, GGL_ONE_MINUS_SRC_COLOR, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA,
    123       GGL_DST_ALPHA, GGL_ONE_MINUS_DST_ALPHA, GGL_DST_COLOR, GGL_ONE_MINUS_DST_COLOR,
    124       GGL_SRC_ALPHA_SATURATE, GGL_CONSTANT_COLOR = 11, GGL_ONE_MINUS_CONSTANT_COLOR,
    125       GGL_CONSTANT_ALPHA, GGL_ONE_MINUS_CONSTANT_ALPHA
    126 } scf :
    127 4, saf :
    128 4, dcf :
    129 4, daf :
    130    4;
    131 
    132    //value = GLenum - GL_FUNC_ADD
    133    enum GGLBlendFunc {
    134       GGL_FUNC_ADD = 0, GGL_FUNC_SUBTRACT = 4,
    135       GGL_FUNC_REVERSE_SUBTRACT = 5
    136 } ce :
    137 3, ae :
    138    3;
    139 
    140 unsigned enable :
    141    1;
    142 } GGLBlendState_t;
    143 
    144 typedef struct GGLTextureState {
    145    // format affects vs and fs jit
    146    GGLTexture_t textures[GGL_MAXCOMBINEDTEXTUREIMAGEUNITS]; // the active samplers
    147    // array of pointers to texture surface data synced to textures; used by LLVM generated texture sampler
    148    void * textureData[GGL_MAXCOMBINEDTEXTUREIMAGEUNITS];
    149    // array of texture dimensions synced to textures; by LLVM generated texture sampler
    150    unsigned textureDimensions[GGL_MAXCOMBINEDTEXTUREIMAGEUNITS * 2];
    151 } GGLTextureState_t;
    152 
    153 typedef struct GGLState {
    154    GGLStencilState_t frontStencil, backStencil; // all affect scanline jit
    155 
    156    GGLBufferState_t bufferState; // all affect scanline jit
    157 
    158    GGLBlendState_t blendState; // all affect scanline jit
    159 
    160    GGLTextureState_t textureState; // most affect vs/fs jit
    161 
    162 } GGLState_t;
    163 
    164 // most functions are according to GL ES 2.0 spec and uses GLenum values
    165 // there is some error checking for invalid GLenum
    166 typedef struct GGLInterface GGLInterface_t;
    167 struct GGLInterface {
    168    // these 5 should be moved into libAgl2
    169    void (* CullFace)(GGLInterface_t * iface, GLenum mode);
    170    void (* FrontFace)(GGLInterface_t * iface, GLenum mode);
    171    void (* DepthRangef)(GGLInterface_t * iface, GLclampf zNear, GLclampf zFar);
    172    void (* Viewport)(GGLInterface_t * iface, GLint x, GLint y, GLsizei width, GLsizei height);
    173    void (* ViewportTransform)(const GGLInterface_t * iface, Vector4 * v);
    174 
    175 
    176    void (* BlendColor)(GGLInterface_t * iface, GLclampf red, GLclampf green,
    177                        GLclampf blue, GLclampf alpha);
    178    void (* BlendEquationSeparate)(GGLInterface_t * iface, GLenum modeRGB, GLenum modeAlpha);
    179    void (* BlendFuncSeparate)(GGLInterface_t * iface, GLenum srcRGB, GLenum dstRGB,
    180                               GLenum srcAlpha, GLenum dstAlpha);
    181    void (* EnableDisable)(GGLInterface_t * iface, GLenum cap, GLboolean enable);
    182 
    183    void (* DepthFunc)(GGLInterface_t * iface, GLenum func);
    184    void (* StencilFuncSeparate)(GGLInterface_t * iface, GLenum face, GLenum func,
    185                                 GLint ref, GLuint mask);
    186    void (* StencilOpSeparate)(GGLInterface_t * iface, GLenum face, GLenum sfail,
    187                               GLenum dpfail, GLenum dppass);
    188    // select GL_FRONT or GL_BACK stencil state before raster/scanline
    189    void (* StencilSelect)(const GGLInterface_t * iface, GLenum face);
    190    void (* ClearStencil)(GGLInterface_t * iface, GLint s);
    191    void (* ClearColor)(GGLInterface_t * iface, GLclampf r, GLclampf g, GLclampf b, GLclampf a);
    192    void (* ClearDepthf)(GGLInterface_t * iface, GLclampf d);
    193    void (* Clear)(const GGLInterface_t * iface, GLbitfield buf);
    194 
    195    // shallow copy, surface data pointed to must be valid until texture is set to another texture
    196    // libAgl2 needs to check ret of ShaderUniform to detect assigning to sampler unit
    197    void (* SetSampler)(GGLInterface_t * iface, const unsigned sampler, GGLTexture_t * texture);
    198 
    199    // shallow copy, surface data must remain valid; use GL_COLOR_BUFFER_BIT,
    200    // GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT; format must be RGBA_8888, Z_32 or S_8
    201    void (* SetBuffer)(GGLInterface_t * iface, const GLenum type, GGLSurface_t * surface);
    202 
    203 
    204    // runs active vertex shader using currently set program; no error checking
    205    void (* ProcessVertex)(const GGLInterface_t * iface, const VertexInput_t * input,
    206                           VertexOutput_t * output);
    207    // draws a triangle given 3 unprocessed vertices; should be moved into libAgl2
    208    void (* DrawTriangle)(const GGLInterface_t * iface, const VertexInput_t * v0,
    209                          const VertexInput_t * v1, const VertexInput_t * v2);
    210    // rasters a vertex processed triangle using active program; scizors to frame surface
    211    void (* RasterTriangle)(const GGLInterface_t * iface, const VertexOutput_t * v1,
    212                            const VertexOutput_t * v2, const VertexOutput_t * v3);
    213    // rasters a vertex processed trapezoid using active program; scizors to frame surface
    214    void (* RasterTrapezoid)(const GGLInterface_t * iface, const VertexOutput_t * tl,
    215                             const VertexOutput_t * tr, const VertexOutput_t * bl,
    216                             const VertexOutput_t * br);
    217 
    218    // scan line given left and right processed and scizored vertices
    219    void (* ScanLine)(const GGLInterface_t * iface, const VertexOutput_t * v1,
    220                      const VertexOutput_t * v2);
    221 
    222    // creates empty shader
    223    gl_shader_t * (* ShaderCreate)(const GGLInterface_t * iface, GLenum type);
    224 
    225    void (* ShaderSource)(gl_shader_t * shader, GLsizei count, const char ** string, const int * length);
    226 
    227    // compiles a shader given glsl; returns GL_TRUE on success; glsl only used during call
    228    GLboolean (* ShaderCompile)(const GGLInterface_t * iface, gl_shader_t * shader,
    229                                const char * glsl, const char ** infoLog);
    230 
    231    void (* ShaderDelete)(const GGLInterface_t * iface, gl_shader_t * shader);
    232 
    233    // creates empty program
    234    gl_shader_program_t * (* ShaderProgramCreate)(const GGLInterface_t * iface);
    235 
    236    // attaches a shader to program
    237    void (* ShaderAttach)(const GGLInterface_t * iface, gl_shader_program_t * program,
    238                          gl_shader_t * shader);
    239 
    240    // detaches a shader from program
    241    void (* ShaderDetach)(const GGLInterface_t * iface, gl_shader_program_t * program,
    242                          gl_shader_t * shader);
    243 
    244    // duplicates shaders to program, and links varyings / attributes
    245    GLboolean (* ShaderProgramLink)(gl_shader_program_t * program, const char ** infoLog);
    246    // frees program
    247    void (* ShaderProgramDelete)(GGLInterface_t * iface, gl_shader_program_t * program);
    248 
    249    // LLVM JIT and set as active program
    250    void (* ShaderUse)(GGLInterface_t * iface, gl_shader_program_t * program);
    251 
    252    void (* ShaderGetiv)(const gl_shader_t * shader, const GLenum pname, GLint * params);
    253 
    254    void (* ShaderGetInfoLog)(const gl_shader_t * shader, GLsizei bufsize, GLsizei* length, GLchar* infolog);
    255 
    256    void (* ShaderProgramGetiv)(const gl_shader_program_t * program, const GLenum pname, GLint * params);
    257 
    258    void (* ShaderProgramGetInfoLog)(const gl_shader_program_t * program, GLsizei bufsize, GLsizei* length, GLchar* infolog);
    259 
    260    // bind attribute location before linking
    261    void (* ShaderAttributeBind)(const gl_shader_program_t * program,
    262                                 GLuint index, const GLchar * name);
    263    GLint (* ShaderAttributeLocation)(const gl_shader_program_t * program,
    264                                      const char * name);
    265    // returns fragment input location and vertex output location for varying of linked program
    266    GLint (* ShaderVaryingLocation)(const gl_shader_program_t * program,
    267                                    const char * name, GLint * vertexOutputLocation);
    268    // gets uniform location for linked program
    269    GLint (* ShaderUniformLocation)(const gl_shader_program_t * program,
    270                                    const char * name);
    271    void (* ShaderUniformGetfv)(gl_shader_program_t * program,
    272                                GLint location, GLfloat * params);
    273    void (* ShaderUniformGetiv)(gl_shader_program_t * program,
    274                                GLint location, GLint * params);
    275 
    276    // retrieves the tmu each sampler is set to, sampler2tmu[sampler] == -1 means not used
    277    void (* ShaderUniformGetSamplers)(const gl_shader_program_t * program,
    278                                      int sampler2tmu[GGL_MAXCOMBINEDTEXTUREIMAGEUNITS]);
    279 
    280    // updates linked program uniform value by location; return >= 0 indicates sampler assigned
    281    GLint (* ShaderUniform)(gl_shader_program_t * program,
    282                            GLint location, GLsizei count, const GLvoid *values, GLenum type);
    283 
    284    // updates linked program uniform matrix value by location
    285    void (* ShaderUniformMatrix)(gl_shader_program_t * program, GLint cols,
    286                                 GLint rows, GLint location, GLsizei count,
    287                                 GLboolean transpose, const GLfloat *values);
    288 };
    289 
    290 #ifdef __cplusplus
    291 extern "C"
    292 {
    293 #endif
    294 
    295    GGLInterface_t * CreateGGLInterface();
    296 
    297    void DestroyGGLInterface(GGLInterface_t * interface);
    298 
    299    // creates empty shader
    300    gl_shader_t * GGLShaderCreate(GLenum type);
    301 
    302    // compiles a shader given glsl; returns GL_TRUE on success; glsl only used during call; use infoLog to retrieve status
    303    GLboolean GGLShaderCompile(gl_shader_t * shader, const char * glsl, const char ** infoLog);
    304 
    305    void GGLShaderDelete(gl_shader_t * shader);
    306 
    307    // creates empty program
    308    gl_shader_program_t * GGLShaderProgramCreate();
    309 
    310    // attaches a shader to program
    311    unsigned GGLShaderAttach(gl_shader_program_t * program, gl_shader_t * shader);
    312 
    313    // detaches a shader from program
    314    unsigned GGLShaderDetach(gl_shader_program_t * program, gl_shader_t * shader);
    315 
    316    // duplicates shaders to program, and links varyings / attributes;
    317    GLboolean GGLShaderProgramLink(gl_shader_program_t * program, const char ** infoLog);
    318 
    319    // frees program
    320    void GGLShaderProgramDelete(gl_shader_program_t * program);
    321 
    322    // LLVM JIT and set as active program, also call after gglState change to re-JIT
    323    void GGLShaderUse(void * llvmCtx, const GGLState_t * gglState, gl_shader_program_t * program);
    324 
    325    void GGLShaderGetiv(const gl_shader_t * shader, const GLenum pname, GLint * params);
    326 
    327    void GGLShaderGetInfoLog(const gl_shader_t * shader, GLsizei bufsize, GLsizei* length, GLchar* infolog);
    328 
    329    void GGLShaderProgramGetiv(const gl_shader_program_t * program, const GLenum pname, GLint * params);
    330 
    331    void GGLShaderProgramGetInfoLog(const gl_shader_program_t * program, GLsizei bufsize, GLsizei* length, GLchar* infolog);
    332 
    333    // bind attribute location before linking
    334    void GGLShaderAttributeBind(const gl_shader_program_t * program,
    335                                GLuint index, const GLchar * name);
    336    GLint GGLShaderAttributeLocation(const gl_shader_program_t * program,
    337                                     const char * name);
    338    // returns fragment input location and vertex output location for varying of linked program
    339    GLint GGLShaderVaryingLocation(const gl_shader_program_t * program,
    340                                   const char * name, GLint * vertexOutputLocation);
    341    // gets uniform location for linked program
    342    GLint GGLShaderUniformLocation(const gl_shader_program_t * program,
    343                                   const char * name);
    344 
    345    void GGLShaderUniformMatrix(gl_shader_program_t * program, GLint cols, GLint rows,
    346                             GLint location, GLsizei count, GLboolean transpose, const GLfloat *values);
    347 
    348    // retrieves the tmu each sampler is set to, sampler2tmu[sampler] == -1 means not used
    349    void GGLShaderUniformGetSamplers(const gl_shader_program_t * program,
    350                                     int sampler2tmu[GGL_MAXCOMBINEDTEXTUREIMAGEUNITS]);
    351 
    352    void GGLProcessVertex(const gl_shader_program_t * program, const VertexInput_t * input,
    353                          VertexOutput_t * output, const float (*constants)[4]);
    354 
    355    // scan line given left and right processed and scizored vertices
    356    // depth value bitcast float->int, if negative then ^= 0x7fffffff
    357    void GGLScanLine(const gl_shader_program_t * program, const enum GGLPixelFormat colorFormat,
    358                     void * frameBuffer, int * depthBuffer, unsigned char * stencilBuffer,
    359                     unsigned bufferWidth, unsigned bufferHeight, GGLActiveStencil_t * activeStencil,
    360                     const VertexOutput_t * start, const VertexOutput_t * end, const float (*constants)[4]);
    361 
    362 //   void GGLProcessFragment(const VertexOutput_t * inputs, VertexOutput_t * outputs,
    363 //                           const float (*constants[4]));
    364 
    365 #ifdef __cplusplus
    366 }
    367 #endif
    368 
    369 #endif // #ifndef _PIXELFLINGER2_INTERFACE_H_
    370