Home | History | Annotate | Download | only in GLES_V2
      1 /*
      2 * Copyright (C) 2011 The Android Open Source Project
      3 *
      4 * Licensed under the Apache License, Version 2.0 (the "License");
      5 * you may not use this file except in compliance with the License.
      6 * You may obtain a copy of the License at
      7 *
      8 * http://www.apache.org/licenses/LICENSE-2.0
      9 *
     10 * Unless required by applicable law or agreed to in writing, software
     11 * distributed under the License is distributed on an "AS IS" BASIS,
     12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 * See the License for the specific language governing permissions and
     14 * limitations under the License.
     15 */
     16 
     17 #include "GLESv2Context.h"
     18 
     19 
     20 
     21 void GLESv2Context::init() {
     22     android::Mutex::Autolock mutex(s_lock);
     23     if(!m_initialized) {
     24         s_glDispatch.dispatchFuncs(GLES_2_0);
     25         GLEScontext::init();
     26         for(int i=0; i < s_glSupport.maxVertexAttribs;i++){
     27             m_map[i] = new GLESpointer();
     28         }
     29         setAttribute0value(0.0, 0.0, 0.0, 1.0);
     30 
     31         buildStrings((const char*)dispatcher().glGetString(GL_VENDOR),
     32                      (const char*)dispatcher().glGetString(GL_RENDERER),
     33                      (const char*)dispatcher().glGetString(GL_VERSION),
     34                      "OpenGL ES 2.0");
     35     }
     36     m_initialized = true;
     37 }
     38 
     39 GLESv2Context::GLESv2Context():GLEScontext(), m_att0Array(NULL), m_att0ArrayLength(0), m_att0NeedsDisable(false){};
     40 
     41 GLESv2Context::~GLESv2Context()
     42 {
     43     delete[] m_att0Array;
     44 }
     45 
     46 void GLESv2Context::setAttribute0value(float x, float y, float z, float w)
     47 {
     48     m_attribute0value[0] = x;
     49     m_attribute0value[1] = y;
     50     m_attribute0value[2] = z;
     51     m_attribute0value[3] = w;
     52 }
     53 
     54 void GLESv2Context::validateAtt0PreDraw(unsigned int count)
     55 {
     56     m_att0NeedsDisable = false;
     57 
     58     if(count == 0)
     59         return;
     60 
     61     int enabled = 0;
     62     s_glDispatch.glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled);
     63     if(enabled)
     64         return;
     65 
     66     if(count > m_att0ArrayLength)
     67     {
     68         delete [] m_att0Array;
     69         m_att0Array = new GLfloat[4*count];
     70         m_att0ArrayLength = count;
     71     }
     72 
     73     for(unsigned int i=0; i<count; i++)
     74         memcpy(m_att0Array+i*4, m_attribute0value, 4*sizeof(GLfloat));
     75 
     76     s_glDispatch.glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, m_att0Array);
     77     s_glDispatch.glEnableVertexAttribArray(0);
     78 
     79     m_att0NeedsDisable = true;
     80 }
     81 
     82 void GLESv2Context::validateAtt0PostDraw(void)
     83 {
     84     if(m_att0NeedsDisable)
     85         s_glDispatch.glDisableVertexAttribArray(0);
     86 
     87     m_att0NeedsDisable = false;
     88 }
     89 
     90 void GLESv2Context::setupArraysPointers(GLESConversionArrays& cArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct) {
     91     ArraysMap::iterator it;
     92 
     93     //going over all clients arrays Pointers
     94     for ( it=m_map.begin() ; it != m_map.end(); it++ ) {
     95         GLenum array_id   = (*it).first;
     96         GLESpointer* p = (*it).second;
     97         if(!isArrEnabled(array_id)) continue;
     98 
     99         unsigned int size = p->getSize();
    100 
    101         if(needConvert(cArrs,first,count,type,indices,direct,p,array_id)){
    102             //conversion has occured
    103             ArrayData currentArr = cArrs.getCurrentArray();
    104             setupArr(currentArr.data,array_id,currentArr.type,size,currentArr.stride, p->getNormalized());
    105             ++cArrs;
    106         } else {
    107             setupArr(p->getData(),array_id,p->getType(),
    108                      size,p->getStride(), p->getNormalized());
    109         }
    110     }
    111 }
    112 
    113 //setting client side arr
    114 void GLESv2Context::setupArr(const GLvoid* arr,GLenum arrayType,GLenum dataType,GLint size,GLsizei stride,GLboolean normalized, int index){
    115      if(arr == NULL) return;
    116      s_glDispatch.glVertexAttribPointer(arrayType,size,dataType,normalized,stride,arr);
    117 }
    118 
    119 bool GLESv2Context::needConvert(GLESConversionArrays& cArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLESpointer* p,GLenum array_id) {
    120 
    121     bool usingVBO = p->isVBO();
    122     GLenum arrType = p->getType();
    123     /*
    124      conversion is not necessary in the following cases:
    125       (*) array type is not fixed
    126     */
    127     if(arrType != GL_FIXED) return false;
    128 
    129     if(!usingVBO) {
    130         if (direct) {
    131             convertDirect(cArrs,first,count,array_id,p);
    132         } else {
    133             convertIndirect(cArrs,count,type,indices,array_id,p);
    134         }
    135     } else {
    136         if (direct) {
    137             convertDirectVBO(cArrs,first,count,array_id,p) ;
    138         } else {
    139             convertIndirectVBO(cArrs,count,type,indices,array_id,p);
    140         }
    141     }
    142     return true;
    143 }
    144 
    145 void GLESv2Context::initExtensionString() {
    146     *s_glExtensions = "GL_OES_EGL_image GL_OES_depth24 GL_OES_depth32 GL_OES_element_index_uint "
    147                       "GL_OES_texture_float GL_OES_texture_float_linear "
    148                       "GL_OES_compressed_paletted_texture GL_OES_compressed_ETC1_RGB8_texture GL_OES_depth_texture ";
    149     if (s_glSupport.GL_ARB_HALF_FLOAT_PIXEL || s_glSupport.GL_NV_HALF_FLOAT)
    150         *s_glExtensions+="GL_OES_texture_half_float GL_OES_texture_half_float_linear ";
    151     if (s_glSupport.GL_EXT_PACKED_DEPTH_STENCIL)
    152         *s_glExtensions+="GL_OES_packed_depth_stencil ";
    153     if (s_glSupport.GL_ARB_HALF_FLOAT_VERTEX)
    154         *s_glExtensions+="GL_OES_vertex_half_float ";
    155     if (s_glSupport.GL_OES_STANDARD_DERIVATIVES)
    156         *s_glExtensions+="GL_OES_standard_derivatives ";
    157 }
    158 
    159 int GLESv2Context::getMaxTexUnits() {
    160     return getCaps()->maxTexImageUnits;
    161 }
    162