// any getUniform(WebGLProgram? program, WebGLUniformLocation? location); JSBool JSB_glGetUniformfv(JSContext *cx, uint32_t argc, jsval *vp) { JSB_PRECONDITION2( argc == 2, cx, JS_FALSE, "JSB_glGetUniformfv: Invalid number of arguments" ); jsval *argvp = JS_ARGV(cx,vp); JSBool ok = JS_TRUE; uint32_t arg0, arg1; ok &= jsval_to_uint( cx, *argvp++, &arg0 ); ok &= jsval_to_uint( cx, *argvp++, &arg1 ); JSB_PRECONDITION2(ok, cx, JS_FALSE, "JSB_glGetUniformfv: Error processing arguments"); GLsizei length; glGetProgramiv(arg0, GL_ACTIVE_UNIFORM_MAX_LENGTH, &length); GLchar* namebuffer = new GLchar[length+1]; GLint size = -1; GLenum type = -1; glGetActiveUniform(arg0, arg1, length, NULL, &size, &type, namebuffer); CC_SAFE_DELETE_ARRAY(namebuffer); int usize = 0; int utype = 0; switch(type) { // float case GL_FLOAT: usize = 1; utype = GL_FLOAT; break; case GL_FLOAT_MAT2: usize = 2 * 2; utype = GL_FLOAT; break; case GL_FLOAT_MAT3: usize = 3 * 3; utype = GL_FLOAT; break; case GL_FLOAT_MAT4: usize = 4 * 4; utype = GL_FLOAT; break; case GL_FLOAT_VEC2: usize = 2; utype = GL_FLOAT; break; case GL_FLOAT_VEC3: usize = 3; utype = GL_FLOAT; break; case GL_FLOAT_VEC4: usize = 4; utype = GL_FLOAT; break; // int case GL_INT: usize = 1; utype = GL_INT; break; case GL_INT_VEC2: usize = 1; utype = GL_INT; break; case GL_INT_VEC3: usize = 1; utype = GL_INT; break; case GL_INT_VEC4: usize = 1; utype = GL_INT; break; default: JSB_PRECONDITION2(false, cx, JS_FALSE, "JSB_glGetUniformfv: Uniform Type not supported"); } JSObject *typedArray = NULL; if( utype == GL_FLOAT) { // FIXME: glew on windows will cause array overflow after invoking glGetUniformfv. // It seems that glGetUniformfv re-assign the memeroy with a wrong size which is 4x than we pass in. // For temporary solution, we allocate 4x array. GLfloat* param = new GLfloat[usize*4]; glGetUniformfv(arg0, arg1, param); typedArray = JS_NewFloat32Array(cx, usize); float *buffer = (float*)JS_GetArrayBufferViewData(typedArray); memcpy( buffer, param, sizeof(float) * usize); CC_SAFE_DELETE_ARRAY(param); } else if( utype == GL_INT ) { // FIXME: glew on windows will cause array overflow after invoking glGetUniformfv. // It seems that glGetUniformfv re-assign the memeroy with a wrong size which is 4x than we pass in. // For temporary solution, we allocate 4x array. GLint* param = new GLint[usize*4]; glGetUniformiv(arg0, arg1, param); typedArray = JS_NewInt32Array(cx, usize); GLint *buffer = (GLint*)JS_GetArrayBufferViewData(typedArray); memcpy( buffer, param, sizeof(GLint) * usize); CC_SAFE_DELETE_ARRAY(param); } JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(typedArray)); return JS_TRUE; }
// any getUniform(WebGLProgram? program, WebGLUniformLocation? location); bool JSB_glGetUniformfv(JSContext *cx, uint32_t argc, jsval *vp) { JSB_PRECONDITION2( argc == 2, cx, false, "JSB_glGetUniformfv: Invalid number of arguments" ); JS::CallArgs args = JS::CallArgsFromVp(argc, vp); bool ok = true; uint32_t arg0, arg1; ok &= jsval_to_uint( cx, args.get(0), &arg0 ); ok &= jsval_to_uint( cx, args.get(1), &arg1 ); JSB_PRECONDITION2(ok, cx, false, "JSB_glGetUniformfv: Error processing arguments"); GLint activeUniforms; glGetProgramiv(arg0, GL_ACTIVE_UNIFORMS, &activeUniforms); GLsizei length; glGetProgramiv(arg0, GL_ACTIVE_UNIFORM_MAX_LENGTH, &length); GLchar* namebuffer = new (std::nothrow) GLchar[length+1]; GLint size = -1; GLenum type = -1; bool isLocationFound = false; for(int i = 0; i < activeUniforms; ++i) { glGetActiveUniform(arg0, i, length, NULL, &size, &type, namebuffer); if(arg1 == glGetUniformLocation(arg0, namebuffer)) { isLocationFound = true; break; } } if(!isLocationFound) { size = -1; type = -1; } CC_SAFE_DELETE_ARRAY(namebuffer); int usize = 0; int utype = 0; switch(type) { // float case GL_FLOAT: usize = 1; utype = GL_FLOAT; break; case GL_FLOAT_MAT2: usize = 2 * 2; utype = GL_FLOAT; break; case GL_FLOAT_MAT3: usize = 3 * 3; utype = GL_FLOAT; break; case GL_FLOAT_MAT4: usize = 4 * 4; utype = GL_FLOAT; break; case GL_FLOAT_VEC2: usize = 2; utype = GL_FLOAT; break; case GL_FLOAT_VEC3: usize = 3; utype = GL_FLOAT; break; case GL_FLOAT_VEC4: usize = 4; utype = GL_FLOAT; break; // int case GL_INT: usize = 1; utype = GL_INT; break; case GL_INT_VEC2: usize = 1; utype = GL_INT; break; case GL_INT_VEC3: usize = 1; utype = GL_INT; break; case GL_INT_VEC4: usize = 1; utype = GL_INT; break; default: JSB_PRECONDITION2(false, cx, false, "JSB_glGetUniformfv: Uniform Type not supported"); } JSObject *typedArray = NULL; if( utype == GL_FLOAT) { // FIXME: glew on windows will cause array overflow after invoking glGetUniformfv. // It seems that glGetUniformfv re-assign the memeroy with a wrong size which is 4x than we pass in. // For temporary solution, we allocate 4x array. GLfloat* param = new (std::nothrow) GLfloat[usize*4]; glGetUniformfv(arg0, arg1, param); typedArray = JS_NewFloat32Array(cx, usize); float *buffer = (float*)JS_GetArrayBufferViewData(typedArray); memcpy( buffer, param, sizeof(float) * usize); CC_SAFE_DELETE_ARRAY(param); } else if( utype == GL_INT ) { // FIXME: glew on windows will cause array overflow after invoking glGetUniformfv. // It seems that glGetUniformfv re-assign the memeroy with a wrong size which is 4x than we pass in. // For temporary solution, we allocate 4x array. GLint* param = new (std::nothrow) GLint[usize*4]; glGetUniformiv(arg0, arg1, param); typedArray = JS_NewInt32Array(cx, usize); GLint *buffer = (GLint*)JS_GetArrayBufferViewData(typedArray); memcpy( buffer, param, sizeof(GLint) * usize); CC_SAFE_DELETE_ARRAY(param); } args.rval().set(OBJECT_TO_JSVAL(typedArray)); return true; }
// any getUniform(WebGLProgram? program, WebGLUniformLocation? location); JSBool JSB_glGetUniformfv(JSContext *cx, uint32_t argc, jsval *vp) { JSB_PRECONDITION2( argc == 2, cx, JS_FALSE, "JSB_glGetUniformfv: Invalid number of arguments" ); jsval *argvp = JS_ARGV(cx,vp); JSBool ok = JS_TRUE; uint32_t arg0, arg1; ok &= jsval_to_uint( cx, *argvp++, &arg0 ); ok &= jsval_to_uint( cx, *argvp++, &arg1 ); JSB_PRECONDITION2(ok, cx, JS_FALSE, "JSB_glGetUniformfv: Error processing arguments"); GLsizei length; glGetProgramiv(arg0, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &length); GLchar* namebuffer = new GLchar[length]; GLint size = -1; GLenum type = -1; glGetActiveUniform(arg0, arg1, length, NULL, &size, &type, namebuffer); CC_SAFE_DELETE_ARRAY(namebuffer); int usize = 0; int utype = 0; switch(type) { // float case GL_FLOAT: usize = 1; utype = GL_FLOAT; break; case GL_FLOAT_MAT2: usize = 2 * 2; utype = GL_FLOAT; break; case GL_FLOAT_MAT3: usize = 3 * 3; utype = GL_FLOAT; break; case GL_FLOAT_MAT4: usize = 4 * 4; utype = GL_FLOAT; break; case GL_FLOAT_VEC2: usize = 2; utype = GL_FLOAT; break; case GL_FLOAT_VEC3: usize = 3; utype = GL_FLOAT; break; case GL_FLOAT_VEC4: usize = 4; utype = GL_FLOAT; break; // int case GL_INT: usize = 1; utype = GL_INT; break; case GL_INT_VEC2: usize = 1; utype = GL_INT; break; case GL_INT_VEC3: usize = 1; utype = GL_INT; break; case GL_INT_VEC4: usize = 1; utype = GL_INT; break; default: JSB_PRECONDITION2(false, cx, JS_FALSE, "JSB_glGetUniformfv: Uniform Type not supported"); } JSObject *typedArray = NULL; if( utype == GL_FLOAT) { GLfloat* param = new GLfloat[usize]; glGetUniformfv(arg0, arg1, param); typedArray = JS_NewFloat32Array(cx, usize); float *buffer = (float*)JS_GetArrayBufferViewData(typedArray); memcpy( buffer, param, sizeof(float) * usize); CC_SAFE_DELETE_ARRAY(param); } else if( utype == GL_INT ) { GLint* param = new GLint[usize]; glGetUniformiv(arg0, arg1, param); typedArray = JS_NewInt32Array(cx, usize); GLint *buffer = (GLint*)JS_GetArrayBufferViewData(typedArray); memcpy( buffer, param, sizeof(GLint) * usize); CC_SAFE_DELETE_ARRAY(param); } JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(typedArray)); return JS_TRUE; }