Ejemplo n.º 1
0
static GLint get_uniform_vector(const char *name, GLuint program, GLenum *type)
{
  glGetError();  // flush current error state.

  GLint size;
  GLint location;

  // Get the position for the uniform var.
  if(GLSLProgram::m_bGLUseARB) {
    location=glGetUniformLocationARB(program, name);
  } else {
    location=glGetUniformLocation(program, name);
  }
  GLenum gl_err = glGetError();
  if(gl_err != GL_NO_ERROR || location == -1) {
    throw GL_ERROR(gl_err);
  }

  if (GLSLProgram::m_bGLUseARB) {
    glGetActiveUniformARB(program, location, 0, NULL, &size, type, NULL);
  } else {
    glGetActiveUniform(program, location, 1, &AtiHackLen, &size, type,
                       &AtiHackChar);
  }

  gl_err = glGetError();
  if(gl_err != GL_NO_ERROR) {
    T_ERROR("Error getting type.");
    throw GL_ERROR(gl_err);
  }

  return location;
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_ARBShaderObjects_nglGetActiveUniformARB(JNIEnv *env, jclass clazz, jint programObj, jint index, jint maxLength, jobject length, jint length_position, jobject size, jint size_position, jobject type, jint type_position, jobject name, jint name_position, jlong function_pointer) {
	GLsizei *length_address = ((GLsizei *)safeGetBufferAddress(env, length)) + length_position;
	GLint *size_address = ((GLint *)(*env)->GetDirectBufferAddress(env, size)) + size_position;
	GLenum *type_address = ((GLenum *)(*env)->GetDirectBufferAddress(env, type)) + type_position;
	GLcharARB *name_address = ((GLcharARB *)(*env)->GetDirectBufferAddress(env, name)) + name_position;
	glGetActiveUniformARBPROC glGetActiveUniformARB = (glGetActiveUniformARBPROC)((intptr_t)function_pointer);
	glGetActiveUniformARB(programObj, index, maxLength, length_address, size_address, type_address, name_address);
}
Ejemplo n.º 3
0
/////////////////////////////////////////////////////////
// getVariables
//
/////////////////////////////////////////////////////////
void glsl_program :: getVariables()
{
  if(!m_linked)return;
  int i;
  //
  // Allocate arrays to store the answers in. For simplicity, the return
  // from malloc is not checked for NULL.
  //
  destroyArrays();
  //
  // Get the number of uniforms, and the length of the longest name.
  //
  if(GLEW_VERSION_2_0) {
    glGetProgramiv( m_program,
                    GL_ACTIVE_UNIFORM_MAX_LENGTH,
                    &m_maxLength);
    glGetProgramiv( m_program, GL_ACTIVE_UNIFORMS,
                    &m_uniformCount);
  } else if (GLEW_ARB_shader_objects) {
    glGetObjectParameterivARB( m_programARB,
                               GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH_ARB,
                               &m_maxLength);
    glGetObjectParameterivARB( m_programARB, GL_OBJECT_ACTIVE_UNIFORMS_ARB,
                               &m_uniformCount);
  }
  createArrays();

  //
  // Loop over the ActiveUniform's and store the results away.
  //
  GLchar *name=new GLchar[m_maxLength];
  GLcharARB *nameARB=new GLcharARB[m_maxLength];
  GLsizei    length=0;
  for (i = 0; i < m_uniformCount; i++)
    {
      if(GLEW_VERSION_2_0) {
        glGetActiveUniform(m_program, i, m_maxLength, &length, &m_size[i], &m_type[i], name);
        m_loc[i] = glGetUniformLocation( m_program, name );
        m_symname[i]=gensym(name);
      } else if (GLEW_ARB_shader_objects) {
        glGetActiveUniformARB(m_programARB, i, m_maxLength, &length, &m_size[i], &m_type[i], nameARB);
        m_loc[i] = glGetUniformLocationARB( m_programARB, nameARB );
        m_symname[i]=gensym(nameARB);
      }
    }
  delete[]name;
  delete[]nameARB;
}
Ejemplo n.º 4
0
void GPU::Shader::RecoverActiveUniforms()
{
    m_Uniforms.clear();
    m_Samplers.clear();

	GLint count;
	GLcharARB nameBuffer[512];

    glGetObjectParameterivARB( m_Id, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &count );

	for( GLint i=0; i<count; ++i )
	{
		GLsizei length;
        GLint   size;
        GLenum  type;

		glGetActiveUniformARB(
            m_Id,
            i,
            sizeof(nameBuffer),
            &length,
            &size,
            &type,
            nameBuffer
            );

		std::string name( nameBuffer );

        if( type<GL_SAMPLER_1D_ARB || type>GL_SAMPLER_2D_RECT_SHADOW_ARB )
        {
            m_Uniforms[name].location = glGetUniformLocationARB( m_Id, nameBuffer );
		    m_Uniforms[name].size = size;
		    m_Uniforms[name].type = type;
        }
        else
            m_Samplers[name] = glGetUniformLocationARB( m_Id, nameBuffer );
    }
}
Ejemplo n.º 5
0
static int program_compile(lua_State *L)
{
	shader_type *p = (shader_type*)lua_touserdata(L, 1);

	glLinkProgramARB(p->shader);
	CHECKGLSLLINK(p->shader);

	CHECKGLSLVALID(p->shader);

	char buffer[256];
	int count;
	int dummysize;
	int length;
	GLenum dummytype;

	// New metatable -- stack index 2
	lua_newtable(L);

	// Add GC method
	lua_pushstring(L, "__gc");
	lua_pushcfunction(L, program_free);
	lua_rawset(L, -3);
	
	// New Index -- stack index 4
	lua_pushstring(L, "__index");
	lua_newtable(L);

	// Grab current index table -- stack index 5
	lua_getmetatable(L, 1);
	lua_pushstring(L, "__index");
	lua_rawget(L, -2);
	lua_remove(L, -2);

	// Iterate old index table and copy to new table
	lua_pushnil(L);
	while (lua_next(L, 5) != 0) {
		lua_pushvalue(L, -2);
		lua_pushvalue(L, -2);
		lua_rawset(L, 4);
		lua_pop(L, 1);
	}

	// Pop the old index
	lua_pop(L, 1);

	glGetObjectParameterivARB(p->shader, GL_ACTIVE_UNIFORMS, &count);
	int i;
	for(i = 0; i<count;++i)
	{
		GLint uniLoc;
		glGetActiveUniformARB(p->shader, i, 256, &length, &dummysize, &dummytype, buffer);
		uniLoc = glGetUniformLocationARB(p->shader, buffer);
		if(uniLoc>=0)	// Test for valid uniform location
		{
			printf("*p %i: Uniform: %i: %X %s\n", p->shader,uniLoc, dummytype, buffer);
			// Add a C closure to define the uniform
			if (dummytype == GL_FLOAT) {
				// Compute the name
				lua_pushstring(L, "uni");
				buffer[0] = toupper(buffer[0]);
				lua_pushstring(L, buffer);
				lua_concat(L, 2);
				// Push a closure with the uniform location
				lua_pushnumber(L, uniLoc);
				lua_pushcclosure(L, program_set_uniform_number_fast, 1);
				// Set it in the index table
				lua_rawset(L, 4);
			} else if (dummytype == GL_FLOAT_VEC2) {
				// Compute the name
				lua_pushstring(L, "uni");
				buffer[0] = toupper(buffer[0]);
				lua_pushstring(L, buffer);
				lua_concat(L, 2);
				// Push a closure with the uniform location
				lua_pushnumber(L, uniLoc);
				lua_pushcclosure(L, program_set_uniform_number2_fast, 1);
				// Set it in the index table
				lua_rawset(L, 4);
			} else if (dummytype == GL_FLOAT_VEC3) {
				// Compute the name
				lua_pushstring(L, "uni");
				buffer[0] = toupper(buffer[0]);
				lua_pushstring(L, buffer);
				lua_concat(L, 2);
				// Push a closure with the uniform location
				lua_pushnumber(L, uniLoc);
				lua_pushcclosure(L, program_set_uniform_number3_fast, 1);
				// Set it in the index table
				lua_rawset(L, 4);
			} else if (dummytype == GL_FLOAT_VEC4) {
				// Compute the name
				lua_pushstring(L, "uni");
				buffer[0] = toupper(buffer[0]);
				lua_pushstring(L, buffer);
				lua_concat(L, 2);
				// Push a closure with the uniform location
				lua_pushnumber(L, uniLoc);
				lua_pushcclosure(L, program_set_uniform_number4_fast, 1);
				// Set it in the index table
				lua_rawset(L, 4);
			} else if (dummytype == GL_SAMPLER_2D) {
				// Compute the name
				lua_pushstring(L, "uni");
				buffer[0] = toupper(buffer[0]);
				lua_pushstring(L, buffer);
				lua_concat(L, 2);
				// Push a closure with the uniform location
				lua_pushnumber(L, uniLoc);
				lua_pushcclosure(L, program_set_uniform_texture_fast, 1);
				// Set it in the index table
				lua_rawset(L, 4);
			}
		}
	}

	// Set the index in the metatable
	lua_rawset(L, 2);

	// Set it up (the metatable)
	lua_setmetatable(L, 1);

	p->p_tick = glGetUniformLocationARB(p->shader, "tick");
	p->p_color = glGetUniformLocationARB(p->shader, "displayColor");
	p->p_mapcoord = glGetUniformLocationARB(p->shader, "mapCoord");
	p->p_texsize = glGetUniformLocationARB(p->shader, "texSize");
	p->p_texcoord = glGetUniformLocationARB(p->shader, "texCoord");

	lua_pushboolean(L, TRUE);
	return 1;
}
Ejemplo n.º 6
0
    //---------------------------------------------------------------------
    void GLSLLinkProgramManager::extractUniforms(GLhandleARB programObject, 
        const GpuConstantDefinitionMap* vertexConstantDefs, 
        const GpuConstantDefinitionMap* geometryConstantDefs,
        const GpuConstantDefinitionMap* fragmentConstantDefs,
        GLUniformReferenceList& list)
    {
        // scan through the active uniforms and add them to the reference list
        GLint uniformCount = 0;

        #define BUFFERSIZE 200
        char   uniformName[BUFFERSIZE] = "";
        //GLint location;
        GLUniformReference newGLUniformReference;

        // get the number of active uniforms
        glGetObjectParameterivARB(programObject, GL_OBJECT_ACTIVE_UNIFORMS_ARB,
            &uniformCount);

        // Loop over each of the active uniforms, and add them to the reference container
        // only do this for user defined uniforms, ignore built in gl state uniforms
        for (int index = 0; index < uniformCount; index++)
        {
            GLint arraySize = 0;
            GLenum glType;
            glGetActiveUniformARB(programObject, index, BUFFERSIZE, NULL, 
                &arraySize, &glType, uniformName);
            // don't add built in uniforms
            newGLUniformReference.mLocation = glGetUniformLocationARB(programObject, uniformName);
            if (newGLUniformReference.mLocation >= 0)
            {
                // user defined uniform found, add it to the reference list
                String paramName = String( uniformName );

                // Current ATI drivers (Catalyst 7.2 and earlier) and older NVidia drivers will include all array elements as uniforms but we only want the root array name and location
                // Also note that ATI Catalyst 6.8 to 7.2 there is a bug with glUniform that does not allow you to update a uniform array past the first uniform array element
                // ie you can't start updating an array starting at element 1, must always be element 0.

                // if the uniform name has a "[" in it then its an array element uniform.
                String::size_type arrayStart = paramName.find("[");
                if (arrayStart != String::npos)
                {
                    // if not the first array element then skip it and continue to the next uniform
                    if (paramName.compare(arrayStart, paramName.size() - 1, "[0]") != 0) continue;
                    paramName = paramName.substr(0, arrayStart);
                }

                // find out which params object this comes from
                bool foundSource = completeParamSource(paramName,
                        vertexConstantDefs, geometryConstantDefs, fragmentConstantDefs, newGLUniformReference);

                // only add this parameter if we found the source
                if (foundSource)
                {
                    assert(size_t (arraySize) == newGLUniformReference.mConstantDef->arraySize
                            && "GL doesn't agree with our array size!");
                    list.push_back(newGLUniformReference);
                }

                // Don't bother adding individual array params, they will be
                // picked up in the 'parent' parameter can copied all at once
                // anyway, individual indexes are only needed for lookup from
                // user params
            } // end if
        } // end for

    }
Ejemplo n.º 7
0
void GLSL_ShaderPair::showActiveUniforms ( FILE *fd )
{
  GLint maxlen   = 0 ;
  GLint maxattrs = 0 ;

  if ( fd == NULL ) fd = stderr ;

  glGetObjectParameterivARB ( handle,
                              GL_OBJECT_ACTIVE_UNIFORMS_ARB,
                              &maxattrs ) ;

  if ( maxattrs == 0 )
  {
    fprintf ( fd, "No Active Uniforms.\n" ) ;
    return ;
  }

  glGetObjectParameterivARB ( handle,
                              GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH_ARB,
                              &maxlen ) ;

  char *name = new char [ maxlen+1 ] ;

  fprintf ( fd, "Active Uniforms:\n" ) ;

  for ( int i = 0 ; i < maxattrs ; i++ )
  {
    GLsizei len  ;
    GLint   size ;
    GLenum  vartype ;
    char *vartypename ;
    GLint   location ;

    glGetActiveUniformARB ( handle, i,
                            maxlen+1, &len, &size, &vartype, name ) ;

    location = glGetUniformLocationARB ( handle, name ) ;

    switch ( vartype )
    {
      case GL_FLOAT          : vartypename = "float    " ; break ;
      case GL_FLOAT_VEC2_ARB : vartypename = "vec2     " ; break ;
      case GL_FLOAT_VEC3_ARB : vartypename = "vec3     " ; break ;
      case GL_FLOAT_VEC4_ARB : vartypename = "vec4     " ; break ;
      case GL_INT            : vartypename = "int      " ; break ;
      case GL_INT_VEC2_ARB   : vartypename = "intvec2  " ; break ;
      case GL_INT_VEC3_ARB   : vartypename = "intvec3  " ; break ;
      case GL_INT_VEC4_ARB   : vartypename = "intvec4  " ; break ;
      case GL_BOOL           : vartypename = "bool     " ; break ;
      case GL_BOOL_VEC2_ARB  : vartypename = "boolvec2 " ; break ;
      case GL_BOOL_VEC3_ARB  : vartypename = "boolvec3 " ; break ;
      case GL_BOOL_VEC4_ARB  : vartypename = "boolvec4 " ; break ;
      case GL_FLOAT_MAT2_ARB : vartypename = "mat2     " ; break ;
      case GL_FLOAT_MAT3_ARB : vartypename = "mat3     " ; break ;
      case GL_FLOAT_MAT4_ARB : vartypename = "mat4     " ; break ;
      case GL_SAMPLER_1D_ARB : vartypename = "sampler1D" ; break ;
      case GL_SAMPLER_2D_ARB : vartypename = "sampler2D" ; break ;
      case GL_SAMPLER_3D_ARB : vartypename = "sampler3D" ; break ;
      default                : vartypename = "?????????" ; break ;
    }

    if ( size == 1 )
      fprintf ( fd, "%2d) %s %s ; // @%d\n", i,
                              vartypename, name, location ) ;
    else
      fprintf ( fd, "%2d) %s %s [ %d ] ; // @%d\n", i,
                              vartypename, name, size, location ) ;
  }

  fprintf ( fd, "\n" ) ;
  delete name ;
}
Ejemplo n.º 8
0
/**
 * Sets an uniform array.
 * Sets the entire array at once. Single positions can still be set using the other SetUniform*() methods.
 * \warning uses glGetError();
 * \param name - name of the parameter
 * \param a - a bool array containing enough floats to fill the entire uniform array.
 * \return void
 * \author <a href="mailto:[email protected]">Jens Schneider</a>
 * \date Mar.2005
 */
void GLSLProgram::SetUniformArray(const char *name,const bool  *a) const {
  assert(m_bEnabled);
  CheckGLError();

  GLint iSize;
  GLenum iType;
  GLint iLocation;

  if (m_bGLUseARB) {
    iLocation=glGetUniformLocationARB(m_hProgram,name);
  } else {
    iLocation=glGetUniformLocation(m_hProgram,name);
  }

  if (CheckGLError("SetUniformVector(%s,float,...) [getting adress]",name)) {
    return;
  }

  if(iLocation==-1) {
    T_ERROR("Error getting address for %s in '%s' or '%s'.", name,
            m_sVS.c_str(), m_sFS.c_str());
    return;
  }

  if (m_bGLUseARB) {
    glGetActiveUniformARB(m_hProgram,iLocation,0,NULL,&iSize,&iType,NULL);
  } else {
    glGetActiveUniform(m_hProgram,iLocation,1,&AtiHackLen,&iSize,&iType,
                       &AtiHackChar);
  }

  if (CheckGLError("SetUniformVector(%s,float,...) [getting type]",name)) {
    return;
  }

#ifdef GLSL_ALLOW_IMPLICIT_CASTS
  float *fArray;
#endif
  GLint *iArray;
  switch (iType) {
    case GL_BOOL:
      iArray=new GLint[iSize];
      for (int i=0; i<iSize; i++) iArray[i]=(a[i] ? 1 : 0);
      glUniform1iv(iLocation,iSize,iArray);
      delete[] iArray;
      break;
    case GL_BOOL_VEC2:
      iArray=new GLint[2*iSize];
      for (int i=0; i<2*iSize; i++) iArray[i]=(a[i] ? 1 : 0);
      glUniform2iv(iLocation,iSize,iArray);
      delete[] iArray;
      break;
    case GL_BOOL_VEC3:
      iArray=new GLint[3*iSize];
      for (int i=0; i<3*iSize; i++) iArray[i]=(a[i] ? 1 : 0);
      glUniform3iv(iLocation,iSize,iArray);
      delete[] iArray;
      break;
    case GL_BOOL_VEC4:
      iArray=new GLint[4*iSize];
      for (int i=0; i<4*iSize; i++) iArray[i]=(a[i] ? 1 : 0);
      glUniform4iv(iLocation,iSize,iArray);
      delete[] iArray;
      break;

#ifdef GLSL_ALLOW_IMPLICIT_CASTS
    case GL_INT:
      iArray=new GLint[iSize];
      for (int i=0; i<iSize; i++) iArray[i]=(a[i] ? 1 : 0);
      glUniform1iv(iLocation,iSize,iArray);
      delete[] iArray;
      break;
    case GL_INT_VEC2:
      iArray=new GLint[2*iSize];
      for (int i=0; i<2*iSize; i++) iArray[i]=(a[i] ? 1 : 0);
      glUniform2iv(iLocation,iSize,iArray);
      delete[] iArray;
      break;
    case GL_INT_VEC3:
      iArray=new GLint[3*iSize];
      for (int i=0; i<3*iSize; i++) iArray[i]=(a[i] ? 1 : 0);
      glUniform3iv(iLocation,iSize,iArray);
      delete[] iArray;
      break;
    case GL_INT_VEC4:
      iArray=new GLint[4*iSize];
      for (int i=0; i<4*iSize; i++) iArray[i]=(a[i] ? 1 : 0);
      glUniform4iv(iLocation,iSize,iArray);
      delete[] iArray;
      break;
    case GL_FLOAT:
      fArray=new float[iSize];
      for (int i=0; i<iSize; i++) fArray[i]=(a[i] ? 1.0f : 0.0f);
      glUniform1fv(iLocation,iSize,fArray);
      delete[] fArray;
      break;
    case GL_FLOAT_VEC2:
      fArray=new float[2*iSize];
      for (int i=0; i<2*iSize; i++) fArray[i]=(a[i] ? 1.0f : 0.0f);
      glUniform2fv(iLocation,iSize,fArray);
      delete[] fArray;
      break;
    case GL_FLOAT_VEC3:
      fArray=new float[3*iSize];
      for (int i=0; i<3*iSize; i++) fArray[i]=(a[i] ? 1.0f : 0.0f);
      glUniform3fv(iLocation,iSize,fArray);
      delete[] fArray;
      break;
    case GL_FLOAT_VEC4:
      fArray=new float[4*iSize];
      for (int i=0; i<4*iSize; i++) fArray[i]=(a[i] ? 1.0f : 0.0f);
      glUniform4fv(iLocation,iSize,fArray);
      delete[] fArray;
      break;
#endif

    default:
      T_ERROR("Unknown type (%d) for %s.", iType, name);
      break;
  }
#ifdef GLSL_DEBUG
  CheckGLError("SetUniformArray(%s,bool*)",name);
#endif
}