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
init()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 m_initialized = true;
32 }
33
GLESv2Context()34 GLESv2Context::GLESv2Context():GLEScontext(), m_att0Array(NULL), m_att0ArrayLength(0), m_att0NeedsDisable(false){};
35
~GLESv2Context()36 GLESv2Context::~GLESv2Context()
37 {
38 delete[] m_att0Array;
39 }
40
setAttribute0value(float x,float y,float z,float w)41 void GLESv2Context::setAttribute0value(float x, float y, float z, float w)
42 {
43 m_attribute0value[0] = x;
44 m_attribute0value[1] = y;
45 m_attribute0value[2] = z;
46 m_attribute0value[3] = w;
47 }
48
validateAtt0PreDraw(unsigned int count)49 void GLESv2Context::validateAtt0PreDraw(unsigned int count)
50 {
51 m_att0NeedsDisable = false;
52
53 if(count == 0)
54 return;
55
56 int enabled = 0;
57 s_glDispatch.glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled);
58 if(enabled)
59 return;
60
61 if(count > m_att0ArrayLength)
62 {
63 delete [] m_att0Array;
64 m_att0Array = new GLfloat[4*count];
65 m_att0ArrayLength = count;
66 }
67
68 for(unsigned int i=0; i<count; i++)
69 memcpy(m_att0Array+i*4, m_attribute0value, 4*sizeof(GLfloat));
70
71 s_glDispatch.glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, m_att0Array);
72 s_glDispatch.glEnableVertexAttribArray(0);
73
74 m_att0NeedsDisable = true;
75 }
76
validateAtt0PostDraw(void)77 void GLESv2Context::validateAtt0PostDraw(void)
78 {
79 if(m_att0NeedsDisable)
80 s_glDispatch.glDisableVertexAttribArray(0);
81
82 m_att0NeedsDisable = false;
83 }
84
setupArraysPointers(GLESConversionArrays & cArrs,GLint first,GLsizei count,GLenum type,const GLvoid * indices,bool direct)85 void GLESv2Context::setupArraysPointers(GLESConversionArrays& cArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct) {
86 ArraysMap::iterator it;
87
88 //going over all clients arrays Pointers
89 for ( it=m_map.begin() ; it != m_map.end(); it++ ) {
90 GLenum array_id = (*it).first;
91 GLESpointer* p = (*it).second;
92 if(!isArrEnabled(array_id)) continue;
93
94 unsigned int size = p->getSize();
95
96 if(needConvert(cArrs,first,count,type,indices,direct,p,array_id)){
97 //conversion has occured
98 ArrayData currentArr = cArrs.getCurrentArray();
99 setupArr(currentArr.data,array_id,currentArr.type,size,currentArr.stride, p->getNormalized());
100 ++cArrs;
101 } else {
102 setupArr(p->getData(),array_id,p->getType(),
103 size,p->getStride(), p->getNormalized());
104 }
105 }
106 }
107
108 //setting client side arr
setupArr(const GLvoid * arr,GLenum arrayType,GLenum dataType,GLint size,GLsizei stride,GLboolean normalized,int index)109 void GLESv2Context::setupArr(const GLvoid* arr,GLenum arrayType,GLenum dataType,GLint size,GLsizei stride,GLboolean normalized, int index){
110 if(arr == NULL) return;
111 s_glDispatch.glVertexAttribPointer(arrayType,size,dataType,normalized,stride,arr);
112 }
113
needConvert(GLESConversionArrays & cArrs,GLint first,GLsizei count,GLenum type,const GLvoid * indices,bool direct,GLESpointer * p,GLenum array_id)114 bool GLESv2Context::needConvert(GLESConversionArrays& cArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLESpointer* p,GLenum array_id) {
115
116 bool usingVBO = p->isVBO();
117 GLenum arrType = p->getType();
118 /*
119 conversion is not necessary in the following cases:
120 (*) array type is not fixed
121 */
122 if(arrType != GL_FIXED) return false;
123
124 if(!usingVBO) {
125 if (direct) {
126 convertDirect(cArrs,first,count,array_id,p);
127 } else {
128 convertIndirect(cArrs,count,type,indices,array_id,p);
129 }
130 } else {
131 if (direct) {
132 convertDirectVBO(cArrs,first,count,array_id,p) ;
133 } else {
134 convertIndirectVBO(cArrs,count,type,indices,array_id,p);
135 }
136 }
137 return true;
138 }
139
initExtensionString()140 void GLESv2Context::initExtensionString() {
141 *s_glExtensions = "GL_OES_EGL_image GL_OES_depth24 GL_OES_depth32 GL_OES_element_index_uint "
142 "GL_OES_texture_float GL_OES_texture_float_linear "
143 "GL_OES_compressed_paletted_texture GL_OES_compressed_ETC1_RGB8_texture GL_OES_depth_texture ";
144 if (s_glSupport.GL_ARB_HALF_FLOAT_PIXEL || s_glSupport.GL_NV_HALF_FLOAT)
145 *s_glExtensions+="GL_OES_texture_half_float GL_OES_texture_half_float_linear ";
146 if (s_glSupport.GL_EXT_PACKED_DEPTH_STENCIL)
147 *s_glExtensions+="GL_OES_packed_depth_stencil ";
148 if (s_glSupport.GL_ARB_HALF_FLOAT_VERTEX)
149 *s_glExtensions+="GL_OES_vertex_half_float ";
150 if (s_glSupport.GL_OES_STANDARD_DERIVATIVES)
151 *s_glExtensions+="GL_OES_standard_derivatives ";
152 }
153
getMaxTexUnits()154 int GLESv2Context::getMaxTexUnits() {
155 return getCaps()->maxTexImageUnits;
156 }
157