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