keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 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 "GL2Encoder.h" |
| 18 | #include <assert.h> |
| 19 | #include <ctype.h> |
| 20 | |
Lingfeng Yang | 6906660 | 2016-04-12 09:29:11 -0700 | [diff] [blame] | 21 | #include <GLES2/gl2.h> |
| 22 | #include <GLES2/gl2ext.h> |
| 23 | #include <GLES2/gl2platform.h> |
| 24 | |
| 25 | #include <GLES3/gl3.h> |
| 26 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 27 | #ifndef MIN |
| 28 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 29 | #endif |
| 30 | |
| 31 | static GLubyte *gVendorString= (GLubyte *) "Android"; |
| 32 | static GLubyte *gRendererString= (GLubyte *) "Android HW-GLES 2.0"; |
| 33 | static GLubyte *gVersionString= (GLubyte *) "OpenGL ES 2.0"; |
Nicolas Capens | fe2d51a | 2015-11-20 15:10:09 -0500 | [diff] [blame] | 34 | static GLubyte *gExtensionsString= (GLubyte *) "GL_OES_EGL_image_external "; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 35 | |
| 36 | #define SET_ERROR_IF(condition,err) if((condition)) { \ |
| 37 | ALOGE("%s:%s:%d GL error 0x%x\n", __FILE__, __FUNCTION__, __LINE__, err); \ |
| 38 | ctx->setError(err); \ |
| 39 | return; \ |
| 40 | } |
| 41 | |
| 42 | |
| 43 | #define RET_AND_SET_ERROR_IF(condition,err,ret) if((condition)) { \ |
| 44 | ALOGE("%s:%s:%d GL error 0x%x\n", __FILE__, __FUNCTION__, __LINE__, err); \ |
| 45 | ctx->setError(err); \ |
| 46 | return ret; \ |
| 47 | } |
| 48 | |
| 49 | |
Yahan Zhou | b7f0908 | 2016-03-10 11:45:02 -0800 | [diff] [blame] | 50 | GL2Encoder::GL2Encoder(IOStream *stream, ChecksumCalculator *protocol) |
| 51 | : gl2_encoder_context_t(stream, protocol) |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 52 | { |
| 53 | m_initialized = false; |
| 54 | m_state = NULL; |
| 55 | m_error = GL_NO_ERROR; |
| 56 | m_num_compressedTextureFormats = 0; |
Lizhe Liu | e41f4ef | 2015-05-27 11:13:23 +0800 | [diff] [blame^] | 57 | m_max_cubeMapTextureSize = 0; |
| 58 | m_max_renderBufferSize = 0; |
| 59 | m_max_textureSize = 0; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 60 | m_compressedTextureFormats = NULL; |
David 'Digit' Turner | 3afd44b | 2014-10-30 18:15:04 +0100 | [diff] [blame] | 61 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 62 | //overrides |
David 'Digit' Turner | 3afd44b | 2014-10-30 18:15:04 +0100 | [diff] [blame] | 63 | #define OVERRIDE(name) m_##name##_enc = this-> name ; this-> name = &s_##name |
bohu | 56bf82f | 2014-10-17 15:35:48 -0700 | [diff] [blame] | 64 | |
David 'Digit' Turner | 3afd44b | 2014-10-30 18:15:04 +0100 | [diff] [blame] | 65 | OVERRIDE(glFlush); |
| 66 | OVERRIDE(glPixelStorei); |
| 67 | OVERRIDE(glGetString); |
| 68 | OVERRIDE(glBindBuffer); |
| 69 | OVERRIDE(glBufferData); |
| 70 | OVERRIDE(glBufferSubData); |
| 71 | OVERRIDE(glDeleteBuffers); |
| 72 | OVERRIDE(glDrawArrays); |
| 73 | OVERRIDE(glDrawElements); |
| 74 | OVERRIDE(glGetIntegerv); |
| 75 | OVERRIDE(glGetFloatv); |
| 76 | OVERRIDE(glGetBooleanv); |
| 77 | OVERRIDE(glVertexAttribPointer); |
| 78 | OVERRIDE(glEnableVertexAttribArray); |
| 79 | OVERRIDE(glDisableVertexAttribArray); |
| 80 | OVERRIDE(glGetVertexAttribiv); |
| 81 | OVERRIDE(glGetVertexAttribfv); |
| 82 | OVERRIDE(glGetVertexAttribPointerv); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 83 | |
Bo Hu | 73568cd | 2015-01-20 16:29:50 -0800 | [diff] [blame] | 84 | this->glShaderBinary = &s_glShaderBinary; |
David 'Digit' Turner | 3afd44b | 2014-10-30 18:15:04 +0100 | [diff] [blame] | 85 | this->glShaderSource = &s_glShaderSource; |
| 86 | this->glFinish = &s_glFinish; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 87 | |
David 'Digit' Turner | 3afd44b | 2014-10-30 18:15:04 +0100 | [diff] [blame] | 88 | OVERRIDE(glGetError); |
| 89 | OVERRIDE(glLinkProgram); |
| 90 | OVERRIDE(glDeleteProgram); |
| 91 | OVERRIDE(glGetUniformiv); |
| 92 | OVERRIDE(glGetUniformfv); |
| 93 | OVERRIDE(glCreateProgram); |
| 94 | OVERRIDE(glCreateShader); |
| 95 | OVERRIDE(glDeleteShader); |
| 96 | OVERRIDE(glAttachShader); |
| 97 | OVERRIDE(glDetachShader); |
| 98 | OVERRIDE(glGetAttachedShaders); |
| 99 | OVERRIDE(glGetShaderSource); |
| 100 | OVERRIDE(glGetShaderInfoLog); |
| 101 | OVERRIDE(glGetProgramInfoLog); |
| 102 | |
| 103 | OVERRIDE(glGetUniformLocation); |
| 104 | OVERRIDE(glUseProgram); |
| 105 | |
| 106 | OVERRIDE(glUniform1f); |
| 107 | OVERRIDE(glUniform1fv); |
| 108 | OVERRIDE(glUniform1i); |
| 109 | OVERRIDE(glUniform1iv); |
| 110 | OVERRIDE(glUniform2f); |
| 111 | OVERRIDE(glUniform2fv); |
| 112 | OVERRIDE(glUniform2i); |
| 113 | OVERRIDE(glUniform2iv); |
| 114 | OVERRIDE(glUniform3f); |
| 115 | OVERRIDE(glUniform3fv); |
| 116 | OVERRIDE(glUniform3i); |
| 117 | OVERRIDE(glUniform3iv); |
| 118 | OVERRIDE(glUniform4f); |
| 119 | OVERRIDE(glUniform4fv); |
| 120 | OVERRIDE(glUniform4i); |
| 121 | OVERRIDE(glUniform4iv); |
| 122 | OVERRIDE(glUniformMatrix2fv); |
| 123 | OVERRIDE(glUniformMatrix3fv); |
| 124 | OVERRIDE(glUniformMatrix4fv); |
| 125 | |
| 126 | OVERRIDE(glActiveTexture); |
| 127 | OVERRIDE(glBindTexture); |
| 128 | OVERRIDE(glDeleteTextures); |
| 129 | OVERRIDE(glGetTexParameterfv); |
| 130 | OVERRIDE(glGetTexParameteriv); |
| 131 | OVERRIDE(glTexParameterf); |
| 132 | OVERRIDE(glTexParameterfv); |
| 133 | OVERRIDE(glTexParameteri); |
| 134 | OVERRIDE(glTexParameteriv); |
bohu | 26a9298 | 2014-11-25 16:50:37 -0800 | [diff] [blame] | 135 | OVERRIDE(glTexImage2D); |
Lingfeng Yang | 57cb41b | 2016-04-08 14:42:34 -0700 | [diff] [blame] | 136 | |
| 137 | OVERRIDE(glGenRenderbuffers); |
| 138 | OVERRIDE(glDeleteRenderbuffers); |
| 139 | OVERRIDE(glBindRenderbuffer); |
Lingfeng Yang | 6906660 | 2016-04-12 09:29:11 -0700 | [diff] [blame] | 140 | OVERRIDE(glRenderbufferStorage); |
Lingfeng Yang | 57cb41b | 2016-04-08 14:42:34 -0700 | [diff] [blame] | 141 | OVERRIDE(glFramebufferRenderbuffer); |
| 142 | |
| 143 | OVERRIDE(glGenFramebuffers); |
| 144 | OVERRIDE(glDeleteFramebuffers); |
| 145 | OVERRIDE(glBindFramebuffer); |
| 146 | OVERRIDE(glFramebufferTexture2D); |
| 147 | OVERRIDE(glFramebufferTexture3DOES); |
| 148 | OVERRIDE(glGetFramebufferAttachmentParameteriv); |
Lingfeng Yang | 6906660 | 2016-04-12 09:29:11 -0700 | [diff] [blame] | 149 | |
| 150 | OVERRIDE(glCheckFramebufferStatus); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | GL2Encoder::~GL2Encoder() |
| 154 | { |
| 155 | delete m_compressedTextureFormats; |
| 156 | } |
| 157 | |
| 158 | GLenum GL2Encoder::s_glGetError(void * self) |
| 159 | { |
| 160 | GL2Encoder *ctx = (GL2Encoder *)self; |
| 161 | GLenum err = ctx->getError(); |
| 162 | if(err != GL_NO_ERROR) { |
| 163 | ctx->setError(GL_NO_ERROR); |
| 164 | return err; |
| 165 | } |
| 166 | |
| 167 | return ctx->m_glGetError_enc(self); |
| 168 | |
| 169 | } |
| 170 | |
| 171 | void GL2Encoder::s_glFlush(void *self) |
| 172 | { |
| 173 | GL2Encoder *ctx = (GL2Encoder *) self; |
| 174 | ctx->m_glFlush_enc(self); |
| 175 | ctx->m_stream->flush(); |
| 176 | } |
| 177 | |
| 178 | const GLubyte *GL2Encoder::s_glGetString(void *self, GLenum name) |
| 179 | { |
David 'Digit' Turner | e01d5f4 | 2014-10-30 21:32:28 +0100 | [diff] [blame] | 180 | (void)self; |
| 181 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 182 | GLubyte *retval = (GLubyte *) ""; |
| 183 | switch(name) { |
| 184 | case GL_VENDOR: |
| 185 | retval = gVendorString; |
| 186 | break; |
| 187 | case GL_RENDERER: |
| 188 | retval = gRendererString; |
| 189 | break; |
| 190 | case GL_VERSION: |
| 191 | retval = gVersionString; |
| 192 | break; |
| 193 | case GL_EXTENSIONS: |
| 194 | retval = gExtensionsString; |
| 195 | break; |
| 196 | } |
| 197 | return retval; |
| 198 | } |
| 199 | |
| 200 | void GL2Encoder::s_glPixelStorei(void *self, GLenum param, GLint value) |
| 201 | { |
| 202 | GL2Encoder *ctx = (GL2Encoder *)self; |
| 203 | ctx->m_glPixelStorei_enc(ctx, param, value); |
| 204 | assert(ctx->m_state != NULL); |
| 205 | ctx->m_state->setPixelStore(param, value); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | void GL2Encoder::s_glBindBuffer(void *self, GLenum target, GLuint id) |
| 210 | { |
| 211 | GL2Encoder *ctx = (GL2Encoder *) self; |
| 212 | assert(ctx->m_state != NULL); |
| 213 | ctx->m_state->bindBuffer(target, id); |
| 214 | // TODO set error state if needed; |
| 215 | ctx->m_glBindBuffer_enc(self, target, id); |
| 216 | } |
| 217 | |
| 218 | void GL2Encoder::s_glBufferData(void * self, GLenum target, GLsizeiptr size, const GLvoid * data, GLenum usage) |
| 219 | { |
| 220 | GL2Encoder *ctx = (GL2Encoder *) self; |
bohu | b0f0cdf | 2014-11-06 18:08:07 -0800 | [diff] [blame] | 221 | SET_ERROR_IF(!(target == GL_ARRAY_BUFFER || target == GL_ELEMENT_ARRAY_BUFFER), GL_INVALID_ENUM); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 222 | GLuint bufferId = ctx->m_state->getBuffer(target); |
| 223 | SET_ERROR_IF(bufferId==0, GL_INVALID_OPERATION); |
| 224 | SET_ERROR_IF(size<0, GL_INVALID_VALUE); |
| 225 | |
| 226 | ctx->m_shared->updateBufferData(bufferId, size, (void*)data); |
| 227 | ctx->m_glBufferData_enc(self, target, size, data, usage); |
| 228 | } |
| 229 | |
| 230 | void GL2Encoder::s_glBufferSubData(void * self, GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid * data) |
| 231 | { |
| 232 | GL2Encoder *ctx = (GL2Encoder *) self; |
bohu | b0f0cdf | 2014-11-06 18:08:07 -0800 | [diff] [blame] | 233 | SET_ERROR_IF(!(target == GL_ARRAY_BUFFER || target == GL_ELEMENT_ARRAY_BUFFER), GL_INVALID_ENUM); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 234 | GLuint bufferId = ctx->m_state->getBuffer(target); |
| 235 | SET_ERROR_IF(bufferId==0, GL_INVALID_OPERATION); |
| 236 | |
| 237 | GLenum res = ctx->m_shared->subUpdateBufferData(bufferId, offset, size, (void*)data); |
| 238 | SET_ERROR_IF(res, res); |
| 239 | |
| 240 | ctx->m_glBufferSubData_enc(self, target, offset, size, data); |
| 241 | } |
| 242 | |
| 243 | void GL2Encoder::s_glDeleteBuffers(void * self, GLsizei n, const GLuint * buffers) |
| 244 | { |
| 245 | GL2Encoder *ctx = (GL2Encoder *) self; |
| 246 | SET_ERROR_IF(n<0, GL_INVALID_VALUE); |
| 247 | for (int i=0; i<n; i++) { |
| 248 | ctx->m_shared->deleteBufferData(buffers[i]); |
bohu | b0f0cdf | 2014-11-06 18:08:07 -0800 | [diff] [blame] | 249 | ctx->m_state->unBindBuffer(buffers[i]); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 250 | ctx->m_glDeleteBuffers_enc(self,1,&buffers[i]); |
| 251 | } |
| 252 | } |
| 253 | |
Evan Birenbaum | d51386b | 2016-04-13 12:53:09 -0700 | [diff] [blame] | 254 | static bool isValidVertexAttribIndex(void *self, GLuint indx) |
| 255 | { |
| 256 | GL2Encoder *ctx = (GL2Encoder *) self; |
| 257 | GLint maxIndex; |
| 258 | ctx->glGetIntegerv(self, GL_MAX_VERTEX_ATTRIBS, &maxIndex); |
| 259 | return indx < maxIndex; |
| 260 | } |
| 261 | |
| 262 | static bool isValidVertexAttribType(GLenum type) |
| 263 | { |
| 264 | bool retval = false; |
| 265 | switch (type) { |
| 266 | case GL_BYTE: |
| 267 | case GL_UNSIGNED_BYTE: |
| 268 | case GL_SHORT: |
| 269 | case GL_UNSIGNED_SHORT: |
| 270 | case GL_FIXED: |
| 271 | case GL_FLOAT: |
| 272 | // The following are technically only available if certain GLES2 extensions are. |
| 273 | // However, they are supported by desktop GL3, which is a reasonable requirement |
| 274 | // for the desktop GL version. Therefore, consider them valid. |
| 275 | case GL_INT: |
| 276 | case GL_UNSIGNED_INT: |
| 277 | case GL_HALF_FLOAT_OES: |
| 278 | retval = true; |
| 279 | break; |
| 280 | } |
| 281 | return retval; |
| 282 | } |
| 283 | |
David 'Digit' Turner | 02fdb69 | 2014-10-30 18:07:56 +0100 | [diff] [blame] | 284 | void GL2Encoder::s_glVertexAttribPointer(void *self, GLuint indx, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid * ptr) |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 285 | { |
| 286 | GL2Encoder *ctx = (GL2Encoder *)self; |
| 287 | assert(ctx->m_state != NULL); |
Evan Birenbaum | d51386b | 2016-04-13 12:53:09 -0700 | [diff] [blame] | 288 | SET_ERROR_IF(!isValidVertexAttribIndex(self, indx), GL_INVALID_VALUE); |
| 289 | SET_ERROR_IF((size < 1 || size > 4), GL_INVALID_VALUE); |
| 290 | SET_ERROR_IF(!isValidVertexAttribType(type), GL_INVALID_ENUM); |
| 291 | SET_ERROR_IF(stride < 0, GL_INVALID_VALUE); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 292 | ctx->m_state->setState(indx, size, type, normalized, stride, ptr); |
| 293 | } |
| 294 | |
| 295 | void GL2Encoder::s_glGetIntegerv(void *self, GLenum param, GLint *ptr) |
| 296 | { |
| 297 | GL2Encoder *ctx = (GL2Encoder *) self; |
| 298 | assert(ctx->m_state != NULL); |
| 299 | GLClientState* state = ctx->m_state; |
| 300 | |
| 301 | switch (param) { |
| 302 | case GL_NUM_SHADER_BINARY_FORMATS: |
| 303 | *ptr = 0; |
| 304 | break; |
| 305 | case GL_SHADER_BINARY_FORMATS: |
| 306 | // do nothing |
| 307 | break; |
| 308 | |
| 309 | case GL_COMPRESSED_TEXTURE_FORMATS: { |
| 310 | GLint *compressedTextureFormats = ctx->getCompressedTextureFormats(); |
| 311 | if (ctx->m_num_compressedTextureFormats > 0 && |
| 312 | compressedTextureFormats != NULL) { |
| 313 | memcpy(ptr, compressedTextureFormats, |
| 314 | ctx->m_num_compressedTextureFormats * sizeof(GLint)); |
| 315 | } |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: |
| 320 | case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: |
| 321 | case GL_MAX_TEXTURE_IMAGE_UNITS: |
| 322 | ctx->m_glGetIntegerv_enc(self, param, ptr); |
| 323 | *ptr = MIN(*ptr, GLClientState::MAX_TEXTURE_UNITS); |
| 324 | break; |
| 325 | |
| 326 | case GL_TEXTURE_BINDING_2D: |
| 327 | *ptr = state->getBoundTexture(GL_TEXTURE_2D); |
| 328 | break; |
| 329 | case GL_TEXTURE_BINDING_EXTERNAL_OES: |
| 330 | *ptr = state->getBoundTexture(GL_TEXTURE_EXTERNAL_OES); |
| 331 | break; |
| 332 | |
Lingfeng Yang | b017698 | 2016-03-01 21:27:49 -0800 | [diff] [blame] | 333 | case GL_MAX_VERTEX_ATTRIBS: |
| 334 | if (!ctx->m_state->getClientStateParameter<GLint>(param, ptr)) { |
| 335 | ctx->m_glGetIntegerv_enc(self, param, ptr); |
| 336 | ctx->m_state->setMaxVertexAttribs(*ptr); |
Lizhe Liu | e41f4ef | 2015-05-27 11:13:23 +0800 | [diff] [blame^] | 337 | case GL_MAX_CUBE_MAP_TEXTURE_SIZE: |
| 338 | if (ctx->m_max_cubeMapTextureSize != 0) { |
| 339 | *ptr = ctx->m_max_cubeMapTextureSize; |
| 340 | } else { |
| 341 | ctx->m_glGetIntegerv_enc(self, param, ptr); |
| 342 | ctx->m_max_cubeMapTextureSize = *ptr; |
| 343 | } |
| 344 | break; |
| 345 | case GL_MAX_RENDERBUFFER_SIZE: |
| 346 | if (ctx->m_max_renderBufferSize != 0) { |
| 347 | *ptr = ctx->m_max_renderBufferSize; |
| 348 | } else { |
| 349 | ctx->m_glGetIntegerv_enc(self, param, ptr); |
| 350 | ctx->m_max_renderBufferSize = *ptr; |
| 351 | } |
| 352 | break; |
| 353 | case GL_MAX_TEXTURE_SIZE: |
| 354 | if (ctx->m_max_textureSize != 0) { |
| 355 | *ptr = ctx->m_max_textureSize; |
| 356 | } else { |
| 357 | ctx->m_glGetIntegerv_enc(self, param, ptr); |
| 358 | ctx->m_max_textureSize = *ptr; |
Lingfeng Yang | b017698 | 2016-03-01 21:27:49 -0800 | [diff] [blame] | 359 | } |
| 360 | break; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 361 | default: |
| 362 | if (!ctx->m_state->getClientStateParameter<GLint>(param, ptr)) { |
| 363 | ctx->m_glGetIntegerv_enc(self, param, ptr); |
| 364 | } |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | |
| 370 | void GL2Encoder::s_glGetFloatv(void *self, GLenum param, GLfloat *ptr) |
| 371 | { |
| 372 | GL2Encoder *ctx = (GL2Encoder *)self; |
| 373 | assert(ctx->m_state != NULL); |
| 374 | GLClientState* state = ctx->m_state; |
| 375 | |
| 376 | switch (param) { |
| 377 | case GL_NUM_SHADER_BINARY_FORMATS: |
| 378 | *ptr = 0; |
| 379 | break; |
| 380 | case GL_SHADER_BINARY_FORMATS: |
| 381 | // do nothing |
| 382 | break; |
| 383 | |
| 384 | case GL_COMPRESSED_TEXTURE_FORMATS: { |
| 385 | GLint *compressedTextureFormats = ctx->getCompressedTextureFormats(); |
| 386 | if (ctx->m_num_compressedTextureFormats > 0 && |
| 387 | compressedTextureFormats != NULL) { |
| 388 | for (int i = 0; i < ctx->m_num_compressedTextureFormats; i++) { |
| 389 | ptr[i] = (GLfloat) compressedTextureFormats[i]; |
| 390 | } |
| 391 | } |
| 392 | break; |
| 393 | } |
| 394 | |
| 395 | case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: |
| 396 | case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: |
| 397 | case GL_MAX_TEXTURE_IMAGE_UNITS: |
| 398 | ctx->m_glGetFloatv_enc(self, param, ptr); |
| 399 | *ptr = MIN(*ptr, (GLfloat)GLClientState::MAX_TEXTURE_UNITS); |
| 400 | break; |
| 401 | |
| 402 | case GL_TEXTURE_BINDING_2D: |
| 403 | *ptr = (GLfloat)state->getBoundTexture(GL_TEXTURE_2D); |
| 404 | break; |
| 405 | case GL_TEXTURE_BINDING_EXTERNAL_OES: |
| 406 | *ptr = (GLfloat)state->getBoundTexture(GL_TEXTURE_EXTERNAL_OES); |
| 407 | break; |
| 408 | |
| 409 | default: |
| 410 | if (!ctx->m_state->getClientStateParameter<GLfloat>(param, ptr)) { |
| 411 | ctx->m_glGetFloatv_enc(self, param, ptr); |
| 412 | } |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | |
| 418 | void GL2Encoder::s_glGetBooleanv(void *self, GLenum param, GLboolean *ptr) |
| 419 | { |
| 420 | GL2Encoder *ctx = (GL2Encoder *)self; |
| 421 | assert(ctx->m_state != NULL); |
| 422 | GLClientState* state = ctx->m_state; |
| 423 | |
| 424 | switch (param) { |
| 425 | case GL_NUM_SHADER_BINARY_FORMATS: |
| 426 | *ptr = GL_FALSE; |
| 427 | break; |
| 428 | case GL_SHADER_BINARY_FORMATS: |
| 429 | // do nothing |
| 430 | break; |
| 431 | |
| 432 | case GL_COMPRESSED_TEXTURE_FORMATS: { |
| 433 | GLint *compressedTextureFormats = ctx->getCompressedTextureFormats(); |
| 434 | if (ctx->m_num_compressedTextureFormats > 0 && |
| 435 | compressedTextureFormats != NULL) { |
| 436 | for (int i = 0; i < ctx->m_num_compressedTextureFormats; i++) { |
| 437 | ptr[i] = compressedTextureFormats[i] != 0 ? GL_TRUE : GL_FALSE; |
| 438 | } |
| 439 | } |
| 440 | break; |
| 441 | } |
| 442 | |
| 443 | case GL_TEXTURE_BINDING_2D: |
| 444 | *ptr = state->getBoundTexture(GL_TEXTURE_2D) != 0 ? GL_TRUE : GL_FALSE; |
| 445 | break; |
| 446 | case GL_TEXTURE_BINDING_EXTERNAL_OES: |
| 447 | *ptr = state->getBoundTexture(GL_TEXTURE_EXTERNAL_OES) != 0 |
| 448 | ? GL_TRUE : GL_FALSE; |
| 449 | break; |
| 450 | |
| 451 | default: |
| 452 | if (!ctx->m_state->getClientStateParameter<GLboolean>(param, ptr)) { |
| 453 | ctx->m_glGetBooleanv_enc(self, param, ptr); |
| 454 | } |
bohu | 05101d2 | 2014-11-17 16:28:42 -0800 | [diff] [blame] | 455 | *ptr = (*ptr != 0) ? GL_TRUE : GL_FALSE; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 456 | break; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | |
| 461 | void GL2Encoder::s_glEnableVertexAttribArray(void *self, GLuint index) |
| 462 | { |
| 463 | GL2Encoder *ctx = (GL2Encoder *)self; |
| 464 | assert(ctx->m_state); |
Evan Birenbaum | d51386b | 2016-04-13 12:53:09 -0700 | [diff] [blame] | 465 | SET_ERROR_IF(!isValidVertexAttribIndex(self, index), GL_INVALID_VALUE); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 466 | ctx->m_state->enable(index, 1); |
| 467 | } |
| 468 | |
| 469 | void GL2Encoder::s_glDisableVertexAttribArray(void *self, GLuint index) |
| 470 | { |
| 471 | GL2Encoder *ctx = (GL2Encoder *)self; |
| 472 | assert(ctx->m_state); |
Evan Birenbaum | d51386b | 2016-04-13 12:53:09 -0700 | [diff] [blame] | 473 | SET_ERROR_IF(!isValidVertexAttribIndex(self, index), GL_INVALID_VALUE); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 474 | ctx->m_state->enable(index, 0); |
| 475 | } |
| 476 | |
| 477 | |
| 478 | void GL2Encoder::s_glGetVertexAttribiv(void *self, GLuint index, GLenum pname, GLint *params) |
| 479 | { |
| 480 | GL2Encoder *ctx = (GL2Encoder *)self; |
| 481 | assert(ctx->m_state); |
Lingfeng Yang | 0f50808 | 2016-04-08 12:38:35 -0700 | [diff] [blame] | 482 | GLint maxIndex; |
| 483 | ctx->glGetIntegerv(self, GL_MAX_VERTEX_ATTRIBS, &maxIndex); |
| 484 | SET_ERROR_IF(!(index < maxIndex), GL_INVALID_VALUE); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 485 | |
| 486 | if (!ctx->m_state->getVertexAttribParameter<GLint>(index, pname, params)) { |
| 487 | ctx->m_glGetVertexAttribiv_enc(self, index, pname, params); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | void GL2Encoder::s_glGetVertexAttribfv(void *self, GLuint index, GLenum pname, GLfloat *params) |
| 492 | { |
| 493 | GL2Encoder *ctx = (GL2Encoder *)self; |
| 494 | assert(ctx->m_state); |
Lingfeng Yang | 0f50808 | 2016-04-08 12:38:35 -0700 | [diff] [blame] | 495 | GLint maxIndex; |
| 496 | ctx->glGetIntegerv(self, GL_MAX_VERTEX_ATTRIBS, &maxIndex); |
| 497 | SET_ERROR_IF(!(index < maxIndex), GL_INVALID_VALUE); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 498 | |
| 499 | if (!ctx->m_state->getVertexAttribParameter<GLfloat>(index, pname, params)) { |
| 500 | ctx->m_glGetVertexAttribfv_enc(self, index, pname, params); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | void GL2Encoder::s_glGetVertexAttribPointerv(void *self, GLuint index, GLenum pname, GLvoid **pointer) |
| 505 | { |
| 506 | GL2Encoder *ctx = (GL2Encoder *)self; |
| 507 | if (ctx->m_state == NULL) return; |
Lingfeng Yang | 5e10043 | 2016-04-08 12:46:42 -0700 | [diff] [blame] | 508 | GLint maxIndex; |
| 509 | ctx->glGetIntegerv(self, GL_MAX_VERTEX_ATTRIBS, &maxIndex); |
| 510 | SET_ERROR_IF(!(index < maxIndex), GL_INVALID_VALUE); |
| 511 | SET_ERROR_IF(pname != GL_VERTEX_ATTRIB_ARRAY_POINTER, GL_INVALID_ENUM); |
David 'Digit' Turner | e01d5f4 | 2014-10-30 21:32:28 +0100 | [diff] [blame] | 512 | (void)pname; |
| 513 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 514 | const GLClientState::VertexAttribState *va_state = ctx->m_state->getState(index); |
| 515 | if (va_state != NULL) { |
| 516 | *pointer = va_state->data; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | |
| 521 | void GL2Encoder::sendVertexAttributes(GLint first, GLsizei count) |
| 522 | { |
| 523 | assert(m_state); |
| 524 | |
Lingfeng Yang | 398162b | 2016-05-26 14:18:33 -0700 | [diff] [blame] | 525 | GLuint lastBoundVbo = m_state->currentArrayVbo(); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 526 | for (int i = 0; i < m_state->nLocations(); i++) { |
| 527 | bool enableDirty; |
| 528 | const GLClientState::VertexAttribState *state = m_state->getStateAndEnableDirty(i, &enableDirty); |
| 529 | |
| 530 | if (!state) { |
| 531 | continue; |
| 532 | } |
| 533 | |
| 534 | if (!enableDirty && !state->enabled) { |
| 535 | continue; |
| 536 | } |
| 537 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 538 | if (state->enabled) { |
Lingfeng Yang | 398162b | 2016-05-26 14:18:33 -0700 | [diff] [blame] | 539 | if (lastBoundVbo != state->bufferObject) { |
| 540 | this->m_glBindBuffer_enc(this, GL_ARRAY_BUFFER, state->bufferObject); |
| 541 | lastBoundVbo = state->bufferObject; |
| 542 | } |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 543 | m_glEnableVertexAttribArray_enc(this, i); |
| 544 | |
| 545 | unsigned int datalen = state->elementSize * count; |
| 546 | int stride = state->stride == 0 ? state->elementSize : state->stride; |
| 547 | int firstIndex = stride * first; |
| 548 | |
| 549 | if (state->bufferObject == 0) { |
| 550 | this->glVertexAttribPointerData(this, i, state->size, state->type, state->normalized, state->stride, |
| 551 | (unsigned char *)state->data + firstIndex, datalen); |
| 552 | } else { |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 553 | this->glVertexAttribPointerOffset(this, i, state->size, state->type, state->normalized, state->stride, |
Tina Zhang | 163119f | 2014-03-21 08:14:41 +0800 | [diff] [blame] | 554 | (uintptr_t) state->data + firstIndex); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 555 | } |
| 556 | } else { |
| 557 | this->m_glDisableVertexAttribArray_enc(this, i); |
| 558 | } |
| 559 | } |
Lingfeng Yang | 398162b | 2016-05-26 14:18:33 -0700 | [diff] [blame] | 560 | |
| 561 | if (lastBoundVbo != m_state->currentArrayVbo()) { |
| 562 | this->m_glBindBuffer_enc(this, GL_ARRAY_BUFFER, m_state->currentArrayVbo()); |
| 563 | } |
| 564 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Evan Birenbaum | d51386b | 2016-04-13 12:53:09 -0700 | [diff] [blame] | 567 | static bool isValidDrawMode(GLenum mode) |
| 568 | { |
| 569 | bool retval = false; |
| 570 | switch (mode) { |
| 571 | case GL_POINTS: |
| 572 | case GL_LINE_STRIP: |
| 573 | case GL_LINE_LOOP: |
| 574 | case GL_LINES: |
| 575 | case GL_TRIANGLE_STRIP: |
| 576 | case GL_TRIANGLE_FAN: |
| 577 | case GL_TRIANGLES: |
| 578 | retval = true; |
| 579 | } |
| 580 | return retval; |
| 581 | } |
| 582 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 583 | void GL2Encoder::s_glDrawArrays(void *self, GLenum mode, GLint first, GLsizei count) |
| 584 | { |
| 585 | GL2Encoder *ctx = (GL2Encoder *)self; |
Evan Birenbaum | d51386b | 2016-04-13 12:53:09 -0700 | [diff] [blame] | 586 | assert(ctx->m_state != NULL); |
| 587 | SET_ERROR_IF(!isValidDrawMode(mode), GL_INVALID_ENUM); |
| 588 | SET_ERROR_IF(count < 0, GL_INVALID_VALUE); |
Yahan Zhou | d6217a5 | 2016-02-10 16:10:46 -0800 | [diff] [blame] | 589 | |
| 590 | bool has_arrays = false; |
| 591 | int nLocations = ctx->m_state->nLocations(); |
| 592 | for (int i = 0; i < nLocations; i++) { |
| 593 | const GLClientState::VertexAttribState *state = ctx->m_state->getState(i); |
| 594 | if (state->enabled) { |
| 595 | if (state->bufferObject || state->data) { |
| 596 | has_arrays = true; |
| 597 | } |
| 598 | else { |
| 599 | ALOGE("glDrawArrays: a vertex attribute array is enabled with no data bound\n"); |
| 600 | ctx->setError(GL_INVALID_OPERATION); |
| 601 | return; |
| 602 | } |
| 603 | } |
| 604 | } |
Yahan Zhou | d6217a5 | 2016-02-10 16:10:46 -0800 | [diff] [blame] | 605 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 606 | ctx->sendVertexAttributes(first, count); |
| 607 | ctx->m_glDrawArrays_enc(ctx, mode, 0, count); |
| 608 | } |
| 609 | |
| 610 | |
| 611 | void GL2Encoder::s_glDrawElements(void *self, GLenum mode, GLsizei count, GLenum type, const void *indices) |
| 612 | { |
| 613 | |
| 614 | GL2Encoder *ctx = (GL2Encoder *)self; |
| 615 | assert(ctx->m_state != NULL); |
Evan Birenbaum | d51386b | 2016-04-13 12:53:09 -0700 | [diff] [blame] | 616 | SET_ERROR_IF(!isValidDrawMode(mode), GL_INVALID_ENUM); |
| 617 | SET_ERROR_IF(count < 0, GL_INVALID_VALUE); |
Yahan Zhou | 4520fc9 | 2016-04-20 16:02:10 -0700 | [diff] [blame] | 618 | SET_ERROR_IF(!(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT || type == GL_UNSIGNED_INT), GL_INVALID_ENUM); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 619 | |
| 620 | bool has_immediate_arrays = false; |
| 621 | bool has_indirect_arrays = false; |
| 622 | int nLocations = ctx->m_state->nLocations(); |
| 623 | |
| 624 | for (int i = 0; i < nLocations; i++) { |
| 625 | const GLClientState::VertexAttribState *state = ctx->m_state->getState(i); |
| 626 | if (state->enabled) { |
| 627 | if (state->bufferObject != 0) { |
| 628 | has_indirect_arrays = true; |
Yahan Zhou | d6217a5 | 2016-02-10 16:10:46 -0800 | [diff] [blame] | 629 | } else if (state->data) { |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 630 | has_immediate_arrays = true; |
Yahan Zhou | d6217a5 | 2016-02-10 16:10:46 -0800 | [diff] [blame] | 631 | } else { |
| 632 | ALOGW("glDrawElements: a vertex attribute array is enabled with no data bound\n"); |
| 633 | ctx->setError(GL_INVALID_OPERATION); |
| 634 | return; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | if (!has_immediate_arrays && !has_indirect_arrays) { |
| 640 | ALOGE("glDrawElements: no data bound to the command - ignoring\n"); |
Evan Birenbaum | 82885a6 | 2016-04-28 12:47:57 -0700 | [diff] [blame] | 641 | GLenum status = ctx->m_glCheckFramebufferStatus_enc(self, GL_FRAMEBUFFER); |
| 642 | SET_ERROR_IF(status != GL_FRAMEBUFFER_COMPLETE, GL_INVALID_FRAMEBUFFER_OPERATION); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 643 | return; |
| 644 | } |
| 645 | |
| 646 | bool adjustIndices = true; |
| 647 | if (ctx->m_state->currentIndexVbo() != 0) { |
| 648 | if (!has_immediate_arrays) { |
| 649 | ctx->sendVertexAttributes(0, count); |
| 650 | ctx->m_glBindBuffer_enc(self, GL_ELEMENT_ARRAY_BUFFER, ctx->m_state->currentIndexVbo()); |
Tina Zhang | 163119f | 2014-03-21 08:14:41 +0800 | [diff] [blame] | 651 | ctx->glDrawElementsOffset(ctx, mode, count, type, (uintptr_t)indices); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 652 | adjustIndices = false; |
| 653 | } else { |
| 654 | BufferData * buf = ctx->m_shared->getBufferData(ctx->m_state->currentIndexVbo()); |
| 655 | ctx->m_glBindBuffer_enc(self, GL_ELEMENT_ARRAY_BUFFER, 0); |
| 656 | indices = (void*)((GLintptr)buf->m_fixedBuffer.ptr() + (GLintptr)indices); |
| 657 | } |
| 658 | } |
| 659 | if (adjustIndices) { |
| 660 | void *adjustedIndices = (void*)indices; |
| 661 | int minIndex = 0, maxIndex = 0; |
| 662 | |
| 663 | switch(type) { |
| 664 | case GL_BYTE: |
| 665 | case GL_UNSIGNED_BYTE: |
| 666 | GLUtils::minmax<unsigned char>((unsigned char *)indices, count, &minIndex, &maxIndex); |
| 667 | if (minIndex != 0) { |
| 668 | adjustedIndices = ctx->m_fixedBuffer.alloc(glSizeof(type) * count); |
| 669 | GLUtils::shiftIndices<unsigned char>((unsigned char *)indices, |
| 670 | (unsigned char *)adjustedIndices, |
| 671 | count, -minIndex); |
| 672 | } |
| 673 | break; |
| 674 | case GL_SHORT: |
| 675 | case GL_UNSIGNED_SHORT: |
| 676 | GLUtils::minmax<unsigned short>((unsigned short *)indices, count, &minIndex, &maxIndex); |
| 677 | if (minIndex != 0) { |
| 678 | adjustedIndices = ctx->m_fixedBuffer.alloc(glSizeof(type) * count); |
| 679 | GLUtils::shiftIndices<unsigned short>((unsigned short *)indices, |
| 680 | (unsigned short *)adjustedIndices, |
| 681 | count, -minIndex); |
| 682 | } |
| 683 | break; |
Yahan Zhou | 5950852 | 2016-02-09 17:42:33 -0800 | [diff] [blame] | 684 | case GL_INT: |
| 685 | case GL_UNSIGNED_INT: |
| 686 | GLUtils::minmax<unsigned int>((unsigned int *)indices, count, &minIndex, &maxIndex); |
| 687 | if (minIndex != 0) { |
| 688 | adjustedIndices = ctx->m_fixedBuffer.alloc(glSizeof(type) * count); |
| 689 | GLUtils::shiftIndices<unsigned int>((unsigned int *)indices, |
| 690 | (unsigned int *)adjustedIndices, |
| 691 | count, -minIndex); |
| 692 | } |
| 693 | break; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 694 | default: |
| 695 | ALOGE("unsupported index buffer type %d\n", type); |
| 696 | } |
| 697 | if (has_indirect_arrays || 1) { |
| 698 | ctx->sendVertexAttributes(minIndex, maxIndex - minIndex + 1); |
| 699 | ctx->glDrawElementsData(ctx, mode, count, type, adjustedIndices, |
| 700 | count * glSizeof(type)); |
| 701 | // XXX - OPTIMIZATION (see the other else branch) should be implemented |
| 702 | if(!has_indirect_arrays) { |
| 703 | //ALOGD("unoptimized drawelements !!!\n"); |
| 704 | } |
| 705 | } else { |
| 706 | // we are all direct arrays and immidate mode index array - |
| 707 | // rebuild the arrays and the index array; |
| 708 | ALOGE("glDrawElements: direct index & direct buffer data - will be implemented in later versions;\n"); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | |
| 714 | GLint * GL2Encoder::getCompressedTextureFormats() |
| 715 | { |
| 716 | if (m_compressedTextureFormats == NULL) { |
| 717 | this->glGetIntegerv(this, GL_NUM_COMPRESSED_TEXTURE_FORMATS, |
| 718 | &m_num_compressedTextureFormats); |
| 719 | if (m_num_compressedTextureFormats > 0) { |
| 720 | // get number of texture formats; |
| 721 | m_compressedTextureFormats = new GLint[m_num_compressedTextureFormats]; |
| 722 | this->glGetCompressedTextureFormats(this, m_num_compressedTextureFormats, m_compressedTextureFormats); |
| 723 | } |
| 724 | } |
| 725 | return m_compressedTextureFormats; |
| 726 | } |
| 727 | |
| 728 | // Replace uses of samplerExternalOES with sampler2D, recording the names of |
| 729 | // modified shaders in data. Also remove |
| 730 | // #extension GL_OES_EGL_image_external : require |
| 731 | // statements. |
| 732 | // |
| 733 | // This implementation assumes the input has already been pre-processed. If not, |
| 734 | // a few cases will be mishandled: |
| 735 | // |
| 736 | // 1. "mySampler" will be incorrectly recorded as being a samplerExternalOES in |
| 737 | // the following code: |
| 738 | // #if 1 |
| 739 | // uniform sampler2D mySampler; |
| 740 | // #else |
| 741 | // uniform samplerExternalOES mySampler; |
| 742 | // #endif |
| 743 | // |
| 744 | // 2. Comments that look like sampler declarations will be incorrectly modified |
| 745 | // and recorded: |
| 746 | // // samplerExternalOES hahaFooledYou |
| 747 | // |
| 748 | // 3. However, GLSL ES does not have a concatentation operator, so things like |
| 749 | // this (valid in C) are invalid and not a problem: |
| 750 | // #define SAMPLER(TYPE, NAME) uniform sampler#TYPE NAME |
| 751 | // SAMPLER(ExternalOES, mySampler); |
| 752 | // |
| 753 | static bool replaceSamplerExternalWith2D(char* const str, ShaderData* const data) |
| 754 | { |
| 755 | static const char STR_HASH_EXTENSION[] = "#extension"; |
| 756 | static const char STR_GL_OES_EGL_IMAGE_EXTERNAL[] = "GL_OES_EGL_image_external"; |
| 757 | static const char STR_SAMPLER_EXTERNAL_OES[] = "samplerExternalOES"; |
| 758 | static const char STR_SAMPLER2D_SPACE[] = "sampler2D "; |
| 759 | |
| 760 | // -- overwrite all "#extension GL_OES_EGL_image_external : xxx" statements |
| 761 | char* c = str; |
| 762 | while ((c = strstr(c, STR_HASH_EXTENSION))) { |
| 763 | char* start = c; |
| 764 | c += sizeof(STR_HASH_EXTENSION)-1; |
| 765 | while (isspace(*c) && *c != '\0') { |
| 766 | c++; |
| 767 | } |
| 768 | if (strncmp(c, STR_GL_OES_EGL_IMAGE_EXTERNAL, |
| 769 | sizeof(STR_GL_OES_EGL_IMAGE_EXTERNAL)-1) == 0) |
| 770 | { |
| 771 | // #extension statements are terminated by end of line |
| 772 | c = start; |
| 773 | while (*c != '\0' && *c != '\r' && *c != '\n') { |
| 774 | *c++ = ' '; |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | // -- replace "samplerExternalOES" with "sampler2D" and record name |
| 780 | c = str; |
| 781 | while ((c = strstr(c, STR_SAMPLER_EXTERNAL_OES))) { |
| 782 | // Make sure "samplerExternalOES" isn't a substring of a larger token |
| 783 | if (c == str || !isspace(*(c-1))) { |
| 784 | c++; |
| 785 | continue; |
| 786 | } |
| 787 | char* sampler_start = c; |
| 788 | c += sizeof(STR_SAMPLER_EXTERNAL_OES)-1; |
| 789 | if (!isspace(*c) && *c != '\0') { |
| 790 | continue; |
| 791 | } |
| 792 | |
| 793 | // capture sampler name |
| 794 | while (isspace(*c) && *c != '\0') { |
| 795 | c++; |
| 796 | } |
| 797 | if (!isalpha(*c) && *c != '_') { |
| 798 | // not an identifier |
| 799 | return false; |
| 800 | } |
| 801 | char* name_start = c; |
| 802 | do { |
| 803 | c++; |
| 804 | } while (isalnum(*c) || *c == '_'); |
| 805 | data->samplerExternalNames.push_back( |
| 806 | android::String8(name_start, c - name_start)); |
| 807 | |
| 808 | // memcpy instead of strcpy since we don't want the NUL terminator |
| 809 | memcpy(sampler_start, STR_SAMPLER2D_SPACE, sizeof(STR_SAMPLER2D_SPACE)-1); |
| 810 | } |
| 811 | |
| 812 | return true; |
| 813 | } |
| 814 | |
Bo Hu | 73568cd | 2015-01-20 16:29:50 -0800 | [diff] [blame] | 815 | void GL2Encoder::s_glShaderBinary(void *self, GLsizei n, const GLuint *shaders, GLenum binaryformat, const void* binary, GLsizei length) |
| 816 | { |
| 817 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 818 | // Although it is not supported, need to set proper error code. |
| 819 | SET_ERROR_IF(1, GL_INVALID_ENUM); |
| 820 | } |
| 821 | |
David 'Digit' Turner | 02fdb69 | 2014-10-30 18:07:56 +0100 | [diff] [blame] | 822 | void GL2Encoder::s_glShaderSource(void *self, GLuint shader, GLsizei count, const GLchar * const *string, const GLint *length) |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 823 | { |
| 824 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 825 | ShaderData* shaderData = ctx->m_shared->getShaderData(shader); |
Tina Zhang | 7a84f65 | 2014-12-04 12:37:30 +0800 | [diff] [blame] | 826 | SET_ERROR_IF(!ctx->m_shared->isObject(shader), GL_INVALID_VALUE); |
| 827 | SET_ERROR_IF(!shaderData, GL_INVALID_OPERATION); |
| 828 | SET_ERROR_IF((count<0), GL_INVALID_VALUE); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 829 | |
| 830 | int len = glUtilsCalcShaderSourceLen((char**)string, (GLint*)length, count); |
| 831 | char *str = new char[len + 1]; |
| 832 | glUtilsPackStrings(str, (char**)string, (GLint*)length, count); |
| 833 | |
| 834 | // TODO: pre-process str before calling replaceSamplerExternalWith2D(). |
| 835 | // Perhaps we can borrow Mesa's pre-processor? |
| 836 | |
| 837 | if (!replaceSamplerExternalWith2D(str, shaderData)) { |
Adam Buchbinder | 9ae1469 | 2016-05-20 15:25:59 -0700 | [diff] [blame] | 838 | delete[] str; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 839 | ctx->setError(GL_OUT_OF_MEMORY); |
| 840 | return; |
| 841 | } |
| 842 | |
| 843 | ctx->glShaderString(ctx, shader, str, len + 1); |
Adam Buchbinder | 9ae1469 | 2016-05-20 15:25:59 -0700 | [diff] [blame] | 844 | delete[] str; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | void GL2Encoder::s_glFinish(void *self) |
| 848 | { |
| 849 | GL2Encoder *ctx = (GL2Encoder *)self; |
| 850 | ctx->glFinishRoundTrip(self); |
| 851 | } |
| 852 | |
| 853 | void GL2Encoder::s_glLinkProgram(void * self, GLuint program) |
| 854 | { |
| 855 | GL2Encoder *ctx = (GL2Encoder *)self; |
| 856 | ctx->m_glLinkProgram_enc(self, program); |
| 857 | |
| 858 | GLint linkStatus = 0; |
| 859 | ctx->glGetProgramiv(self,program,GL_LINK_STATUS,&linkStatus); |
| 860 | if (!linkStatus) |
| 861 | return; |
| 862 | |
| 863 | //get number of active uniforms in the program |
| 864 | GLint numUniforms=0; |
| 865 | ctx->glGetProgramiv(self, program, GL_ACTIVE_UNIFORMS, &numUniforms); |
| 866 | ctx->m_shared->initProgramData(program,numUniforms); |
| 867 | |
| 868 | //get the length of the longest uniform name |
| 869 | GLint maxLength=0; |
| 870 | ctx->glGetProgramiv(self, program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLength); |
| 871 | |
| 872 | GLint size; |
| 873 | GLenum type; |
| 874 | GLchar *name = new GLchar[maxLength+1]; |
| 875 | GLint location; |
| 876 | //for each active uniform, get its size and starting location. |
| 877 | for (GLint i=0 ; i<numUniforms ; ++i) |
| 878 | { |
| 879 | ctx->glGetActiveUniform(self, program, i, maxLength, NULL, &size, &type, name); |
| 880 | location = ctx->m_glGetUniformLocation_enc(self, program, name); |
| 881 | ctx->m_shared->setProgramIndexInfo(program, i, location, size, type, name); |
| 882 | } |
| 883 | ctx->m_shared->setupLocationShiftWAR(program); |
| 884 | |
| 885 | delete[] name; |
| 886 | } |
| 887 | |
| 888 | void GL2Encoder::s_glDeleteProgram(void *self, GLuint program) |
| 889 | { |
| 890 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 891 | ctx->m_glDeleteProgram_enc(self, program); |
| 892 | |
| 893 | ctx->m_shared->deleteProgramData(program); |
| 894 | } |
| 895 | |
| 896 | void GL2Encoder::s_glGetUniformiv(void *self, GLuint program, GLint location, GLint* params) |
| 897 | { |
| 898 | GL2Encoder *ctx = (GL2Encoder*)self; |
Lingfeng Yang | 35b0fda | 2016-04-08 12:21:13 -0700 | [diff] [blame] | 899 | SET_ERROR_IF(!ctx->m_shared->isObject(program), GL_INVALID_VALUE); |
| 900 | SET_ERROR_IF(!ctx->m_shared->isProgram(program), GL_INVALID_OPERATION); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 901 | SET_ERROR_IF(!ctx->m_shared->isProgramInitialized(program), GL_INVALID_OPERATION); |
| 902 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(program, location); |
| 903 | SET_ERROR_IF(ctx->m_shared->getProgramUniformType(program,hostLoc)==0, GL_INVALID_OPERATION); |
| 904 | ctx->m_glGetUniformiv_enc(self, program, hostLoc, params); |
| 905 | } |
| 906 | void GL2Encoder::s_glGetUniformfv(void *self, GLuint program, GLint location, GLfloat* params) |
| 907 | { |
| 908 | GL2Encoder *ctx = (GL2Encoder*)self; |
Lingfeng Yang | 35b0fda | 2016-04-08 12:21:13 -0700 | [diff] [blame] | 909 | SET_ERROR_IF(!ctx->m_shared->isObject(program), GL_INVALID_VALUE); |
| 910 | SET_ERROR_IF(!ctx->m_shared->isProgram(program), GL_INVALID_OPERATION); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 911 | SET_ERROR_IF(!ctx->m_shared->isProgramInitialized(program), GL_INVALID_OPERATION); |
| 912 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(program,location); |
| 913 | SET_ERROR_IF(ctx->m_shared->getProgramUniformType(program,hostLoc)==0, GL_INVALID_OPERATION); |
| 914 | ctx->m_glGetUniformfv_enc(self, program, hostLoc, params); |
| 915 | } |
| 916 | |
| 917 | GLuint GL2Encoder::s_glCreateProgram(void * self) |
| 918 | { |
| 919 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 920 | GLuint program = ctx->m_glCreateProgram_enc(self); |
| 921 | if (program!=0) |
| 922 | ctx->m_shared->addProgramData(program); |
| 923 | return program; |
| 924 | } |
| 925 | |
| 926 | GLuint GL2Encoder::s_glCreateShader(void *self, GLenum shaderType) |
| 927 | { |
| 928 | GL2Encoder *ctx = (GL2Encoder*)self; |
Tina Zhang | a45cd9a | 2015-03-26 14:45:29 +0800 | [diff] [blame] | 929 | RET_AND_SET_ERROR_IF(((shaderType != GL_VERTEX_SHADER) && (shaderType != GL_FRAGMENT_SHADER)), |
| 930 | GL_INVALID_ENUM, 0); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 931 | GLuint shader = ctx->m_glCreateShader_enc(self, shaderType); |
| 932 | if (shader != 0) { |
| 933 | if (!ctx->m_shared->addShaderData(shader)) { |
| 934 | ctx->m_glDeleteShader_enc(self, shader); |
| 935 | return 0; |
| 936 | } |
| 937 | } |
| 938 | return shader; |
| 939 | } |
| 940 | |
bohu | 56bf82f | 2014-10-17 15:35:48 -0700 | [diff] [blame] | 941 | void GL2Encoder::s_glGetAttachedShaders(void *self, GLuint program, GLsizei maxCount, |
| 942 | GLsizei* count, GLuint* shaders) |
| 943 | { |
| 944 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 945 | SET_ERROR_IF(maxCount < 0, GL_INVALID_VALUE); |
| 946 | ctx->m_glGetAttachedShaders_enc(self, program, maxCount, count, shaders); |
| 947 | } |
| 948 | |
| 949 | void GL2Encoder::s_glGetShaderSource(void *self, GLuint shader, GLsizei bufsize, |
| 950 | GLsizei* length, GLchar* source) |
| 951 | { |
| 952 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 953 | SET_ERROR_IF(bufsize < 0, GL_INVALID_VALUE); |
| 954 | ctx->m_glGetShaderSource_enc(self, shader, bufsize, length, source); |
| 955 | } |
| 956 | |
| 957 | void GL2Encoder::s_glGetShaderInfoLog(void *self, GLuint shader, GLsizei bufsize, |
| 958 | GLsizei* length, GLchar* infolog) |
| 959 | { |
| 960 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 961 | SET_ERROR_IF(bufsize < 0, GL_INVALID_VALUE); |
| 962 | ctx->m_glGetShaderInfoLog_enc(self, shader, bufsize, length, infolog); |
| 963 | } |
| 964 | |
| 965 | void GL2Encoder::s_glGetProgramInfoLog(void *self, GLuint program, GLsizei bufsize, |
| 966 | GLsizei* length, GLchar* infolog) |
| 967 | { |
| 968 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 969 | SET_ERROR_IF(bufsize < 0, GL_INVALID_VALUE); |
| 970 | ctx->m_glGetProgramInfoLog_enc(self, program, bufsize, length, infolog); |
| 971 | } |
| 972 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 973 | void GL2Encoder::s_glDeleteShader(void *self, GLenum shader) |
| 974 | { |
| 975 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 976 | ctx->m_glDeleteShader_enc(self,shader); |
| 977 | ctx->m_shared->unrefShaderData(shader); |
| 978 | } |
| 979 | |
| 980 | void GL2Encoder::s_glAttachShader(void *self, GLuint program, GLuint shader) |
| 981 | { |
| 982 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 983 | ctx->m_glAttachShader_enc(self, program, shader); |
| 984 | ctx->m_shared->attachShader(program, shader); |
| 985 | } |
| 986 | |
| 987 | void GL2Encoder::s_glDetachShader(void *self, GLuint program, GLuint shader) |
| 988 | { |
| 989 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 990 | ctx->m_glDetachShader_enc(self, program, shader); |
| 991 | ctx->m_shared->detachShader(program, shader); |
| 992 | } |
| 993 | |
| 994 | int GL2Encoder::s_glGetUniformLocation(void *self, GLuint program, const GLchar *name) |
| 995 | { |
| 996 | if (!name) return -1; |
| 997 | |
| 998 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 999 | |
| 1000 | // if we need the uniform location WAR |
| 1001 | // parse array index from the end of the name string |
| 1002 | int arrIndex = 0; |
| 1003 | bool needLocationWAR = ctx->m_shared->needUniformLocationWAR(program); |
| 1004 | if (needLocationWAR) { |
| 1005 | int namelen = strlen(name); |
| 1006 | if (name[namelen-1] == ']') { |
| 1007 | char *brace = strrchr(name,'['); |
| 1008 | if (!brace || sscanf(brace+1,"%d",&arrIndex) != 1) { |
| 1009 | return -1; |
| 1010 | } |
| 1011 | |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | int hostLoc = ctx->m_glGetUniformLocation_enc(self, program, name); |
| 1016 | if (hostLoc >= 0 && needLocationWAR) { |
| 1017 | return ctx->m_shared->locationWARHostToApp(program, hostLoc, arrIndex); |
| 1018 | } |
| 1019 | return hostLoc; |
| 1020 | } |
| 1021 | |
| 1022 | bool GL2Encoder::updateHostTexture2DBinding(GLenum texUnit, GLenum newTarget) |
| 1023 | { |
| 1024 | if (newTarget != GL_TEXTURE_2D && newTarget != GL_TEXTURE_EXTERNAL_OES) |
| 1025 | return false; |
| 1026 | |
| 1027 | m_state->setActiveTextureUnit(texUnit); |
| 1028 | |
| 1029 | GLenum oldTarget = m_state->getPriorityEnabledTarget(GL_TEXTURE_2D); |
| 1030 | if (newTarget != oldTarget) { |
| 1031 | if (newTarget == GL_TEXTURE_EXTERNAL_OES) { |
| 1032 | m_state->disableTextureTarget(GL_TEXTURE_2D); |
| 1033 | m_state->enableTextureTarget(GL_TEXTURE_EXTERNAL_OES); |
| 1034 | } else { |
| 1035 | m_state->disableTextureTarget(GL_TEXTURE_EXTERNAL_OES); |
| 1036 | m_state->enableTextureTarget(GL_TEXTURE_2D); |
| 1037 | } |
| 1038 | m_glActiveTexture_enc(this, texUnit); |
| 1039 | m_glBindTexture_enc(this, GL_TEXTURE_2D, |
| 1040 | m_state->getBoundTexture(newTarget)); |
| 1041 | return true; |
| 1042 | } |
| 1043 | |
| 1044 | return false; |
| 1045 | } |
| 1046 | |
| 1047 | void GL2Encoder::s_glUseProgram(void *self, GLuint program) |
| 1048 | { |
| 1049 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1050 | GLClientState* state = ctx->m_state; |
| 1051 | GLSharedGroupPtr shared = ctx->m_shared; |
| 1052 | |
| 1053 | ctx->m_glUseProgram_enc(self, program); |
| 1054 | ctx->m_state->setCurrentProgram(program); |
| 1055 | |
| 1056 | GLenum origActiveTexture = state->getActiveTextureUnit(); |
| 1057 | GLenum hostActiveTexture = origActiveTexture; |
| 1058 | GLint samplerIdx = -1; |
| 1059 | GLint samplerVal; |
| 1060 | GLenum samplerTarget; |
| 1061 | while ((samplerIdx = shared->getNextSamplerUniform(program, samplerIdx, &samplerVal, &samplerTarget)) != -1) { |
| 1062 | if (samplerVal < 0 || samplerVal >= GLClientState::MAX_TEXTURE_UNITS) |
| 1063 | continue; |
| 1064 | if (ctx->updateHostTexture2DBinding(GL_TEXTURE0 + samplerVal, |
| 1065 | samplerTarget)) |
| 1066 | { |
| 1067 | hostActiveTexture = GL_TEXTURE0 + samplerVal; |
| 1068 | } |
| 1069 | } |
| 1070 | state->setActiveTextureUnit(origActiveTexture); |
| 1071 | if (hostActiveTexture != origActiveTexture) { |
| 1072 | ctx->m_glActiveTexture_enc(self, origActiveTexture); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | void GL2Encoder::s_glUniform1f(void *self , GLint location, GLfloat x) |
| 1077 | { |
| 1078 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1079 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1080 | ctx->m_glUniform1f_enc(self, hostLoc, x); |
| 1081 | } |
| 1082 | |
| 1083 | void GL2Encoder::s_glUniform1fv(void *self , GLint location, GLsizei count, const GLfloat* v) |
| 1084 | { |
| 1085 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1086 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1087 | ctx->m_glUniform1fv_enc(self, hostLoc, count, v); |
| 1088 | } |
| 1089 | |
| 1090 | void GL2Encoder::s_glUniform1i(void *self , GLint location, GLint x) |
| 1091 | { |
| 1092 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1093 | GLClientState* state = ctx->m_state; |
| 1094 | GLSharedGroupPtr shared = ctx->m_shared; |
| 1095 | |
| 1096 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1097 | ctx->m_glUniform1i_enc(self, hostLoc, x); |
| 1098 | |
| 1099 | GLenum target; |
| 1100 | if (shared->setSamplerUniform(state->currentProgram(), location, x, &target)) { |
| 1101 | GLenum origActiveTexture = state->getActiveTextureUnit(); |
| 1102 | if (ctx->updateHostTexture2DBinding(GL_TEXTURE0 + x, target)) { |
| 1103 | ctx->m_glActiveTexture_enc(self, origActiveTexture); |
| 1104 | } |
| 1105 | state->setActiveTextureUnit(origActiveTexture); |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | void GL2Encoder::s_glUniform1iv(void *self , GLint location, GLsizei count, const GLint* v) |
| 1110 | { |
| 1111 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1112 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1113 | ctx->m_glUniform1iv_enc(self, hostLoc, count, v); |
| 1114 | } |
| 1115 | |
| 1116 | void GL2Encoder::s_glUniform2f(void *self , GLint location, GLfloat x, GLfloat y) |
| 1117 | { |
| 1118 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1119 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1120 | ctx->m_glUniform2f_enc(self, hostLoc, x, y); |
| 1121 | } |
| 1122 | |
| 1123 | void GL2Encoder::s_glUniform2fv(void *self , GLint location, GLsizei count, const GLfloat* v) |
| 1124 | { |
| 1125 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1126 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1127 | ctx->m_glUniform2fv_enc(self, hostLoc, count, v); |
| 1128 | } |
| 1129 | |
| 1130 | void GL2Encoder::s_glUniform2i(void *self , GLint location, GLint x, GLint y) |
| 1131 | { |
| 1132 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1133 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1134 | ctx->m_glUniform2i_enc(self, hostLoc, x, y); |
| 1135 | } |
| 1136 | |
| 1137 | void GL2Encoder::s_glUniform2iv(void *self , GLint location, GLsizei count, const GLint* v) |
| 1138 | { |
| 1139 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1140 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1141 | ctx->m_glUniform2iv_enc(self, hostLoc, count, v); |
| 1142 | } |
| 1143 | |
| 1144 | void GL2Encoder::s_glUniform3f(void *self , GLint location, GLfloat x, GLfloat y, GLfloat z) |
| 1145 | { |
| 1146 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1147 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1148 | ctx->m_glUniform3f_enc(self, hostLoc, x, y, z); |
| 1149 | } |
| 1150 | |
| 1151 | void GL2Encoder::s_glUniform3fv(void *self , GLint location, GLsizei count, const GLfloat* v) |
| 1152 | { |
| 1153 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1154 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1155 | ctx->m_glUniform3fv_enc(self, hostLoc, count, v); |
| 1156 | } |
| 1157 | |
| 1158 | void GL2Encoder::s_glUniform3i(void *self , GLint location, GLint x, GLint y, GLint z) |
| 1159 | { |
| 1160 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1161 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1162 | ctx->m_glUniform3i_enc(self, hostLoc, x, y, z); |
| 1163 | } |
| 1164 | |
| 1165 | void GL2Encoder::s_glUniform3iv(void *self , GLint location, GLsizei count, const GLint* v) |
| 1166 | { |
| 1167 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1168 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1169 | ctx->m_glUniform3iv_enc(self, hostLoc, count, v); |
| 1170 | } |
| 1171 | |
| 1172 | void GL2Encoder::s_glUniform4f(void *self , GLint location, GLfloat x, GLfloat y, GLfloat z, GLfloat w) |
| 1173 | { |
| 1174 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1175 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1176 | ctx->m_glUniform4f_enc(self, hostLoc, x, y, z, w); |
| 1177 | } |
| 1178 | |
| 1179 | void GL2Encoder::s_glUniform4fv(void *self , GLint location, GLsizei count, const GLfloat* v) |
| 1180 | { |
| 1181 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1182 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1183 | ctx->m_glUniform4fv_enc(self, hostLoc, count, v); |
| 1184 | } |
| 1185 | |
| 1186 | void GL2Encoder::s_glUniform4i(void *self , GLint location, GLint x, GLint y, GLint z, GLint w) |
| 1187 | { |
| 1188 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1189 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1190 | ctx->m_glUniform4i_enc(self, hostLoc, x, y, z, w); |
| 1191 | } |
| 1192 | |
| 1193 | void GL2Encoder::s_glUniform4iv(void *self , GLint location, GLsizei count, const GLint* v) |
| 1194 | { |
| 1195 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1196 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1197 | ctx->m_glUniform4iv_enc(self, hostLoc, count, v); |
| 1198 | } |
| 1199 | |
| 1200 | void GL2Encoder::s_glUniformMatrix2fv(void *self , GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) |
| 1201 | { |
| 1202 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1203 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1204 | ctx->m_glUniformMatrix2fv_enc(self, hostLoc, count, transpose, value); |
| 1205 | } |
| 1206 | |
| 1207 | void GL2Encoder::s_glUniformMatrix3fv(void *self , GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) |
| 1208 | { |
| 1209 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1210 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1211 | ctx->m_glUniformMatrix3fv_enc(self, hostLoc, count, transpose, value); |
| 1212 | } |
| 1213 | |
| 1214 | void GL2Encoder::s_glUniformMatrix4fv(void *self , GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) |
| 1215 | { |
| 1216 | GL2Encoder *ctx = (GL2Encoder*)self; |
| 1217 | GLint hostLoc = ctx->m_shared->locationWARAppToHost(ctx->m_state->currentProgram(),location); |
| 1218 | ctx->m_glUniformMatrix4fv_enc(self, hostLoc, count, transpose, value); |
| 1219 | } |
| 1220 | |
| 1221 | void GL2Encoder::s_glActiveTexture(void* self, GLenum texture) |
| 1222 | { |
| 1223 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1224 | GLClientState* state = ctx->m_state; |
| 1225 | GLenum err; |
| 1226 | |
| 1227 | SET_ERROR_IF((err = state->setActiveTextureUnit(texture)) != GL_NO_ERROR, err); |
| 1228 | |
| 1229 | ctx->m_glActiveTexture_enc(ctx, texture); |
| 1230 | } |
| 1231 | |
| 1232 | void GL2Encoder::s_glBindTexture(void* self, GLenum target, GLuint texture) |
| 1233 | { |
| 1234 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1235 | GLClientState* state = ctx->m_state; |
| 1236 | GLenum err; |
| 1237 | GLboolean firstUse; |
| 1238 | |
| 1239 | SET_ERROR_IF((err = state->bindTexture(target, texture, &firstUse)) != GL_NO_ERROR, err); |
| 1240 | |
| 1241 | if (target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES) { |
| 1242 | ctx->m_glBindTexture_enc(ctx, target, texture); |
| 1243 | return; |
| 1244 | } |
| 1245 | |
| 1246 | GLenum priorityTarget = state->getPriorityEnabledTarget(GL_TEXTURE_2D); |
| 1247 | |
| 1248 | if (target == GL_TEXTURE_EXTERNAL_OES && firstUse) { |
| 1249 | ctx->m_glBindTexture_enc(ctx, GL_TEXTURE_2D, texture); |
| 1250 | ctx->m_glTexParameteri_enc(ctx, GL_TEXTURE_2D, |
| 1251 | GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 1252 | ctx->m_glTexParameteri_enc(ctx, GL_TEXTURE_2D, |
| 1253 | GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 1254 | ctx->m_glTexParameteri_enc(ctx, GL_TEXTURE_2D, |
| 1255 | GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 1256 | |
| 1257 | if (target != priorityTarget) { |
| 1258 | ctx->m_glBindTexture_enc(ctx, GL_TEXTURE_2D, |
| 1259 | state->getBoundTexture(GL_TEXTURE_2D)); |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | if (target == priorityTarget) { |
| 1264 | ctx->m_glBindTexture_enc(ctx, GL_TEXTURE_2D, texture); |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | void GL2Encoder::s_glDeleteTextures(void* self, GLsizei n, const GLuint* textures) |
| 1269 | { |
| 1270 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1271 | GLClientState* state = ctx->m_state; |
| 1272 | |
| 1273 | state->deleteTextures(n, textures); |
| 1274 | ctx->m_glDeleteTextures_enc(ctx, n, textures); |
| 1275 | } |
| 1276 | |
| 1277 | void GL2Encoder::s_glGetTexParameterfv(void* self, |
| 1278 | GLenum target, GLenum pname, GLfloat* params) |
| 1279 | { |
| 1280 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1281 | const GLClientState* state = ctx->m_state; |
| 1282 | |
| 1283 | if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) { |
| 1284 | ctx->override2DTextureTarget(target); |
| 1285 | ctx->m_glGetTexParameterfv_enc(ctx, GL_TEXTURE_2D, pname, params); |
| 1286 | ctx->restore2DTextureTarget(); |
| 1287 | } else { |
| 1288 | ctx->m_glGetTexParameterfv_enc(ctx, target, pname, params); |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | void GL2Encoder::s_glGetTexParameteriv(void* self, |
| 1293 | GLenum target, GLenum pname, GLint* params) |
| 1294 | { |
| 1295 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1296 | const GLClientState* state = ctx->m_state; |
| 1297 | |
| 1298 | switch (pname) { |
| 1299 | case GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES: |
| 1300 | *params = 1; |
| 1301 | break; |
| 1302 | |
| 1303 | default: |
| 1304 | if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) { |
| 1305 | ctx->override2DTextureTarget(target); |
| 1306 | ctx->m_glGetTexParameteriv_enc(ctx, GL_TEXTURE_2D, pname, params); |
| 1307 | ctx->restore2DTextureTarget(); |
| 1308 | } else { |
| 1309 | ctx->m_glGetTexParameteriv_enc(ctx, target, pname, params); |
| 1310 | } |
| 1311 | break; |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | static bool isValidTextureExternalParam(GLenum pname, GLenum param) |
| 1316 | { |
| 1317 | switch (pname) { |
| 1318 | case GL_TEXTURE_MIN_FILTER: |
| 1319 | case GL_TEXTURE_MAG_FILTER: |
| 1320 | return param == GL_NEAREST || param == GL_LINEAR; |
| 1321 | |
| 1322 | case GL_TEXTURE_WRAP_S: |
| 1323 | case GL_TEXTURE_WRAP_T: |
| 1324 | return param == GL_CLAMP_TO_EDGE; |
| 1325 | |
| 1326 | default: |
| 1327 | return true; |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | void GL2Encoder::s_glTexParameterf(void* self, |
| 1332 | GLenum target, GLenum pname, GLfloat param) |
| 1333 | { |
| 1334 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1335 | const GLClientState* state = ctx->m_state; |
| 1336 | |
| 1337 | SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES && |
| 1338 | !isValidTextureExternalParam(pname, (GLenum)param)), |
| 1339 | GL_INVALID_ENUM); |
| 1340 | |
| 1341 | if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) { |
| 1342 | ctx->override2DTextureTarget(target); |
| 1343 | ctx->m_glTexParameterf_enc(ctx, GL_TEXTURE_2D, pname, param); |
| 1344 | ctx->restore2DTextureTarget(); |
| 1345 | } else { |
| 1346 | ctx->m_glTexParameterf_enc(ctx, target, pname, param); |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | void GL2Encoder::s_glTexParameterfv(void* self, |
| 1351 | GLenum target, GLenum pname, const GLfloat* params) |
| 1352 | { |
| 1353 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1354 | const GLClientState* state = ctx->m_state; |
| 1355 | |
| 1356 | SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES && |
| 1357 | !isValidTextureExternalParam(pname, (GLenum)params[0])), |
| 1358 | GL_INVALID_ENUM); |
| 1359 | |
| 1360 | if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) { |
| 1361 | ctx->override2DTextureTarget(target); |
| 1362 | ctx->m_glTexParameterfv_enc(ctx, GL_TEXTURE_2D, pname, params); |
| 1363 | ctx->restore2DTextureTarget(); |
| 1364 | } else { |
| 1365 | ctx->m_glTexParameterfv_enc(ctx, target, pname, params); |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | void GL2Encoder::s_glTexParameteri(void* self, |
| 1370 | GLenum target, GLenum pname, GLint param) |
| 1371 | { |
| 1372 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1373 | const GLClientState* state = ctx->m_state; |
| 1374 | |
| 1375 | SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES && |
| 1376 | !isValidTextureExternalParam(pname, (GLenum)param)), |
| 1377 | GL_INVALID_ENUM); |
| 1378 | |
| 1379 | if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) { |
| 1380 | ctx->override2DTextureTarget(target); |
| 1381 | ctx->m_glTexParameteri_enc(ctx, GL_TEXTURE_2D, pname, param); |
| 1382 | ctx->restore2DTextureTarget(); |
| 1383 | } else { |
| 1384 | ctx->m_glTexParameteri_enc(ctx, target, pname, param); |
| 1385 | } |
| 1386 | } |
| 1387 | |
bohu | 26a9298 | 2014-11-25 16:50:37 -0800 | [diff] [blame] | 1388 | void GL2Encoder::s_glTexImage2D(void* self, GLenum target, GLint level, |
| 1389 | GLint internalformat, GLsizei width, GLsizei height, GLint border, |
| 1390 | GLenum format, GLenum type, const GLvoid* pixels) |
| 1391 | { |
| 1392 | GL2Encoder* ctx = (GL2Encoder*)self; |
Lingfeng Yang | 6906660 | 2016-04-12 09:29:11 -0700 | [diff] [blame] | 1393 | GLClientState* state = ctx->m_state; |
bohu | 26a9298 | 2014-11-25 16:50:37 -0800 | [diff] [blame] | 1394 | if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) { |
| 1395 | ctx->override2DTextureTarget(target); |
Lingfeng Yang | 6906660 | 2016-04-12 09:29:11 -0700 | [diff] [blame] | 1396 | state->setBoundTextureInternalFormat(target, internalformat); |
| 1397 | state->setBoundTextureFormat(target, format); |
| 1398 | state->setBoundTextureType(target, type); |
| 1399 | |
bohu | 26a9298 | 2014-11-25 16:50:37 -0800 | [diff] [blame] | 1400 | ctx->m_glTexImage2D_enc(ctx, target, level, internalformat, width, |
| 1401 | height, border, format, type, pixels); |
| 1402 | ctx->restore2DTextureTarget(); |
| 1403 | } else { |
| 1404 | ctx->m_glTexImage2D_enc(ctx, target, level, internalformat, width, |
| 1405 | height, border, format, type, pixels); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 1410 | void GL2Encoder::s_glTexParameteriv(void* self, |
| 1411 | GLenum target, GLenum pname, const GLint* params) |
| 1412 | { |
| 1413 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1414 | const GLClientState* state = ctx->m_state; |
| 1415 | |
| 1416 | SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES && |
| 1417 | !isValidTextureExternalParam(pname, (GLenum)params[0])), |
| 1418 | GL_INVALID_ENUM); |
| 1419 | |
| 1420 | if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) { |
| 1421 | ctx->override2DTextureTarget(target); |
| 1422 | ctx->m_glTexParameteriv_enc(ctx, GL_TEXTURE_2D, pname, params); |
| 1423 | ctx->restore2DTextureTarget(); |
| 1424 | } else { |
| 1425 | ctx->m_glTexParameteriv_enc(ctx, target, pname, params); |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | void GL2Encoder::override2DTextureTarget(GLenum target) |
| 1430 | { |
| 1431 | if ((target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) && |
| 1432 | target != m_state->getPriorityEnabledTarget(GL_TEXTURE_2D)) { |
| 1433 | m_glBindTexture_enc(this, GL_TEXTURE_2D, |
| 1434 | m_state->getBoundTexture(target)); |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | void GL2Encoder::restore2DTextureTarget() |
| 1439 | { |
| 1440 | GLenum priorityTarget = m_state->getPriorityEnabledTarget(GL_TEXTURE_2D); |
| 1441 | m_glBindTexture_enc(this, GL_TEXTURE_2D, |
| 1442 | m_state->getBoundTexture(priorityTarget)); |
| 1443 | } |
Lingfeng Yang | 57cb41b | 2016-04-08 14:42:34 -0700 | [diff] [blame] | 1444 | |
| 1445 | void GL2Encoder::s_glGenRenderbuffers(void* self, |
| 1446 | GLsizei n, GLuint* renderbuffers) { |
| 1447 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1448 | GLClientState* state = ctx->m_state; |
| 1449 | |
| 1450 | SET_ERROR_IF(n < 0, GL_INVALID_VALUE); |
| 1451 | |
| 1452 | ctx->m_glGenFramebuffers_enc(self, n, renderbuffers); |
| 1453 | state->addRenderbuffers(n, renderbuffers); |
| 1454 | } |
| 1455 | |
| 1456 | void GL2Encoder::s_glDeleteRenderbuffers(void* self, |
| 1457 | GLsizei n, const GLuint* renderbuffers) { |
| 1458 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1459 | GLClientState* state = ctx->m_state; |
| 1460 | |
| 1461 | SET_ERROR_IF(n < 0, GL_INVALID_VALUE); |
| 1462 | |
| 1463 | ctx->m_glDeleteRenderbuffers_enc(self, n, renderbuffers); |
| 1464 | state->removeRenderbuffers(n, renderbuffers); |
| 1465 | } |
| 1466 | |
| 1467 | void GL2Encoder::s_glBindRenderbuffer(void* self, |
| 1468 | GLenum target, GLuint renderbuffer) { |
| 1469 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1470 | GLClientState* state = ctx->m_state; |
| 1471 | |
| 1472 | SET_ERROR_IF((target != GL_RENDERBUFFER), |
| 1473 | GL_INVALID_ENUM); |
| 1474 | |
| 1475 | ctx->m_glBindRenderbuffer_enc(self, target, renderbuffer); |
| 1476 | state->bindRenderbuffer(target, renderbuffer); |
| 1477 | } |
| 1478 | |
Lingfeng Yang | 6906660 | 2016-04-12 09:29:11 -0700 | [diff] [blame] | 1479 | void GL2Encoder::s_glRenderbufferStorage(void* self, |
| 1480 | GLenum target, GLenum internalformat, |
| 1481 | GLsizei width, GLsizei height) { |
| 1482 | GL2Encoder* ctx = (GL2Encoder*) self; |
| 1483 | GLClientState* state = ctx->m_state; |
| 1484 | |
| 1485 | SET_ERROR_IF(target != GL_RENDERBUFFER, GL_INVALID_ENUM); |
| 1486 | switch (internalformat) { |
| 1487 | // Funny internal formats |
| 1488 | // that will cause an incomplete framebuffer |
| 1489 | // attachment error. For dEQP, |
| 1490 | // we can also just abort early here in |
| 1491 | // RenderbufferStorage with a GL_INVALID_ENUM. |
| 1492 | case GL_DEPTH_COMPONENT32F: |
| 1493 | case GL_R8: |
| 1494 | case GL_R8UI: |
| 1495 | case GL_R8I: |
| 1496 | case GL_R16UI: |
| 1497 | case GL_R16I: |
| 1498 | case GL_R32UI: |
| 1499 | case GL_R32I: |
| 1500 | case GL_RG8: |
| 1501 | case GL_RG8UI: |
| 1502 | case GL_RG8I: |
| 1503 | case GL_RG16UI: |
| 1504 | case GL_RG16I: |
| 1505 | case GL_RG32UI: |
| 1506 | case GL_RG32I: |
| 1507 | case GL_SRGB8_ALPHA8: |
Lingfeng Yang | 6906660 | 2016-04-12 09:29:11 -0700 | [diff] [blame] | 1508 | case GL_RGBA8UI: |
| 1509 | case GL_RGBA8I: |
| 1510 | case GL_RGB10_A2UI: |
| 1511 | case GL_RGBA16UI: |
| 1512 | case GL_RGBA16I: |
| 1513 | case GL_RGBA32I: |
| 1514 | case GL_RGBA32UI: |
| 1515 | case GL_R11F_G11F_B10F: |
| 1516 | case GL_R32F: |
| 1517 | case GL_RG32F: |
| 1518 | case GL_RGB32F: |
| 1519 | case GL_RGBA32F: |
| 1520 | SET_ERROR_IF(true, GL_INVALID_ENUM); |
| 1521 | break; |
| 1522 | // These 4 formats are still not OK, |
| 1523 | // but dEQP expects GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT or |
| 1524 | // GL_FRAMEBUFFER_UNSUPPORTED, |
| 1525 | // not a GL_INVALID_ENUM from earlier on. |
| 1526 | // So let's forward these to the rest of |
| 1527 | // FBO initialization |
| 1528 | case GL_R16F: |
| 1529 | case GL_RG16F: |
| 1530 | case GL_RGB16F: |
| 1531 | case GL_RGBA16F: |
Yahan Zhou | afbb4f8 | 2016-05-11 10:45:31 -0700 | [diff] [blame] | 1532 | // dEQP expects GL_FRAMEBUFFER_UNSUPPORTED or GL_FRAMEBUFFER_COMPLETE |
| 1533 | // for this format |
| 1534 | case GL_RGB10_A2: |
Lingfeng Yang | 6906660 | 2016-04-12 09:29:11 -0700 | [diff] [blame] | 1535 | // These formats are OK |
| 1536 | case GL_DEPTH_COMPONENT16: |
| 1537 | case GL_DEPTH_COMPONENT24: |
| 1538 | case GL_DEPTH_COMPONENT32_OES: |
| 1539 | case GL_RGBA4: |
| 1540 | case GL_RGB5_A1: |
| 1541 | case GL_RGB565: |
| 1542 | case GL_RGB8_OES: |
| 1543 | case GL_RGBA8_OES: |
| 1544 | case GL_STENCIL_INDEX8: |
| 1545 | case GL_DEPTH32F_STENCIL8: |
| 1546 | case GL_DEPTH24_STENCIL8_OES: |
| 1547 | break; |
| 1548 | // Everything else: still not OK, |
| 1549 | // and they need the GL_INVALID_ENUM |
| 1550 | default: |
| 1551 | SET_ERROR_IF(true, GL_INVALID_ENUM); |
| 1552 | } |
| 1553 | |
| 1554 | state->setBoundRenderbufferFormat(internalformat); |
| 1555 | ctx->m_glRenderbufferStorage_enc(self, target, internalformat, |
| 1556 | width, height); |
| 1557 | } |
| 1558 | |
Lingfeng Yang | 57cb41b | 2016-04-08 14:42:34 -0700 | [diff] [blame] | 1559 | void GL2Encoder::s_glFramebufferRenderbuffer(void* self, |
| 1560 | GLenum target, GLenum attachment, |
| 1561 | GLenum renderbuffertarget, GLuint renderbuffer) { |
| 1562 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1563 | GLClientState* state = ctx->m_state; |
| 1564 | |
| 1565 | state->attachRbo(attachment, renderbuffer); |
| 1566 | |
| 1567 | ctx->m_glFramebufferRenderbuffer_enc(self, target, attachment, renderbuffertarget, renderbuffer); |
| 1568 | } |
| 1569 | |
| 1570 | void GL2Encoder::s_glGenFramebuffers(void* self, |
| 1571 | GLsizei n, GLuint* framebuffers) { |
| 1572 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1573 | GLClientState* state = ctx->m_state; |
| 1574 | |
| 1575 | SET_ERROR_IF(n < 0, GL_INVALID_VALUE); |
| 1576 | |
| 1577 | ctx->m_glGenFramebuffers_enc(self, n, framebuffers); |
| 1578 | state->addFramebuffers(n, framebuffers); |
| 1579 | } |
| 1580 | |
| 1581 | void GL2Encoder::s_glDeleteFramebuffers(void* self, |
| 1582 | GLsizei n, const GLuint* framebuffers) { |
| 1583 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1584 | GLClientState* state = ctx->m_state; |
| 1585 | |
| 1586 | SET_ERROR_IF(n < 0, GL_INVALID_VALUE); |
| 1587 | |
| 1588 | ctx->m_glDeleteFramebuffers_enc(self, n, framebuffers); |
| 1589 | state->removeFramebuffers(n, framebuffers); |
| 1590 | } |
| 1591 | |
| 1592 | void GL2Encoder::s_glBindFramebuffer(void* self, |
| 1593 | GLenum target, GLuint framebuffer) { |
| 1594 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1595 | GLClientState* state = ctx->m_state; |
| 1596 | |
| 1597 | SET_ERROR_IF((target != GL_FRAMEBUFFER), |
| 1598 | GL_INVALID_ENUM); |
| 1599 | |
| 1600 | state->bindFramebuffer(target, framebuffer); |
| 1601 | |
| 1602 | ctx->m_glBindFramebuffer_enc(self, target, framebuffer); |
| 1603 | } |
| 1604 | |
| 1605 | void GL2Encoder::s_glFramebufferTexture2D(void* self, |
| 1606 | GLenum target, GLenum attachment, |
| 1607 | GLenum textarget, GLuint texture, GLint level) { |
| 1608 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1609 | GLClientState* state = ctx->m_state; |
| 1610 | |
| 1611 | state->attachTextureObject(attachment, texture); |
| 1612 | |
| 1613 | ctx->m_glFramebufferTexture2D_enc(self, target, attachment, textarget, texture, level); |
| 1614 | } |
| 1615 | |
| 1616 | void GL2Encoder::s_glFramebufferTexture3DOES(void* self, |
| 1617 | GLenum target, GLenum attachment, |
| 1618 | GLenum textarget, GLuint texture, GLint level, GLint zoffset) { |
| 1619 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1620 | GLClientState* state = ctx->m_state; |
| 1621 | |
| 1622 | state->attachTextureObject(attachment, texture); |
| 1623 | |
| 1624 | ctx->m_glFramebufferTexture3DOES_enc(self, target, attachment, textarget, texture, level, zoffset); |
| 1625 | } |
| 1626 | |
| 1627 | void GL2Encoder::s_glGetFramebufferAttachmentParameteriv(void* self, |
| 1628 | GLenum target, GLenum attachment, GLenum pname, GLint* params) { |
| 1629 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1630 | const GLClientState* state = ctx->m_state; |
| 1631 | |
| 1632 | SET_ERROR_IF(state->boundFramebuffer() == 0, |
| 1633 | GL_INVALID_OPERATION); |
| 1634 | SET_ERROR_IF((pname != GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE) && |
| 1635 | (!state->attachmentHasObject(attachment)), |
| 1636 | GL_INVALID_ENUM); |
| 1637 | |
| 1638 | ctx->m_glGetFramebufferAttachmentParameteriv_enc(self, target, attachment, pname, params); |
| 1639 | } |
Lingfeng Yang | 6906660 | 2016-04-12 09:29:11 -0700 | [diff] [blame] | 1640 | |
| 1641 | bool GL2Encoder::isCompleteFbo(const GLClientState* state, |
| 1642 | GLenum attachment) const { |
| 1643 | FboFormatInfo fbo_format_info; |
| 1644 | state->getBoundFramebufferFormat(attachment, &fbo_format_info); |
| 1645 | |
| 1646 | bool res; |
| 1647 | switch (fbo_format_info.type) { |
| 1648 | case FBO_ATTACHMENT_RENDERBUFFER: |
| 1649 | switch (fbo_format_info.rb_format) { |
| 1650 | case GL_R16F: |
| 1651 | case GL_RG16F: |
| 1652 | case GL_RGB16F: |
| 1653 | case GL_RGBA16F: |
| 1654 | res = false; |
| 1655 | break; |
| 1656 | case GL_STENCIL_INDEX8: |
| 1657 | if (attachment == GL_STENCIL_ATTACHMENT) { |
| 1658 | res = true; |
| 1659 | } else { |
| 1660 | res = false; |
| 1661 | } |
| 1662 | break; |
| 1663 | default: |
| 1664 | res = true; |
| 1665 | } |
| 1666 | break; |
| 1667 | case FBO_ATTACHMENT_TEXTURE: |
Yahan Zhou | 68bfd8c | 2016-05-11 15:06:34 -0700 | [diff] [blame] | 1668 | switch (fbo_format_info.tex_internalformat) { |
| 1669 | case GL_RED: |
| 1670 | case GL_RG: |
| 1671 | case GL_R16F: |
| 1672 | case GL_RG16F: |
| 1673 | case GL_RGBA16F: |
| 1674 | case GL_RGB16F: |
| 1675 | case GL_R11F_G11F_B10F: |
| 1676 | res = false; |
| 1677 | break; |
Lingfeng Yang | 6906660 | 2016-04-12 09:29:11 -0700 | [diff] [blame] | 1678 | // No float/half-float formats allowed for RGB(A) |
Yahan Zhou | 68bfd8c | 2016-05-11 15:06:34 -0700 | [diff] [blame] | 1679 | case GL_RGB: |
| 1680 | case GL_RGBA: |
Lingfeng Yang | 6906660 | 2016-04-12 09:29:11 -0700 | [diff] [blame] | 1681 | switch (fbo_format_info.tex_type) { |
| 1682 | case GL_FLOAT: |
| 1683 | case GL_HALF_FLOAT_OES: |
Yahan Zhou | 68bfd8c | 2016-05-11 15:06:34 -0700 | [diff] [blame] | 1684 | case GL_UNSIGNED_INT_10F_11F_11F_REV: |
Lingfeng Yang | 6906660 | 2016-04-12 09:29:11 -0700 | [diff] [blame] | 1685 | res = false; |
| 1686 | break; |
| 1687 | default: |
| 1688 | res = true; |
| 1689 | } |
Yahan Zhou | 68bfd8c | 2016-05-11 15:06:34 -0700 | [diff] [blame] | 1690 | break; |
| 1691 | default: |
Lingfeng Yang | 6906660 | 2016-04-12 09:29:11 -0700 | [diff] [blame] | 1692 | res = true; |
| 1693 | } |
| 1694 | break; |
| 1695 | case FBO_ATTACHMENT_NONE: |
| 1696 | res = true; |
| 1697 | break; |
| 1698 | default: |
| 1699 | res = true; |
| 1700 | } |
| 1701 | return res; |
| 1702 | } |
| 1703 | |
| 1704 | GLenum GL2Encoder::s_glCheckFramebufferStatus(void* self, GLenum target) { |
| 1705 | GL2Encoder* ctx = (GL2Encoder*)self; |
| 1706 | GLClientState* state = ctx->m_state; |
| 1707 | |
| 1708 | bool complete = ctx->isCompleteFbo(state, GL_COLOR_ATTACHMENT0) && |
| 1709 | ctx->isCompleteFbo(state, GL_DEPTH_ATTACHMENT) && |
| 1710 | ctx->isCompleteFbo(state, GL_STENCIL_ATTACHMENT); |
| 1711 | if (!complete) { |
| 1712 | state->setCheckFramebufferStatus(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT); |
| 1713 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 1714 | } else { |
| 1715 | GLenum host_checkstatus = ctx->m_glCheckFramebufferStatus_enc(self, target); |
| 1716 | state->setCheckFramebufferStatus(host_checkstatus); |
| 1717 | return host_checkstatus; |
| 1718 | } |
| 1719 | } |