Beispiel #1
0
static int program_set_uniform_number(lua_State *L)
{
	shader_type *p = (shader_type*)lua_touserdata(L, 1);
	const char *var = luaL_checkstring(L, 2);
	bool change = gl_c_shader != p->shader;

	if (change) tglUseProgramObject(p->shader);

	// Uniform array
	if (lua_istable(L, 3)) {
		int nb = lua_objlen(L, 3);
		int i;
		GLfloat is[nb];
		for (i = 0; i < nb; i++) {
			lua_rawgeti(L, 3, i + 1); is[i*4+0] = lua_tonumber(L, -1); lua_pop(L, 1);
		}
		glUniform1fvARB(glGetUniformLocationARB(p->shader, var), nb, is);
	} else {
		GLfloat i = luaL_checknumber(L, 3);
		glUniform1fvARB(glGetUniformLocationARB(p->shader, var), 1, &i);
	}

	if (change) tglUseProgramObject(0);
	return 0;
}
/*
 * sendUniform1fv
 *
 * parameter name - char*
 * parameter count - GLsizei
 * parameter values - GLfloat*
 * return - bool
 */
bool ShaderObject::sendUniform1fv(const char * name, GLsizei count, GLfloat* values) {
    GLint location = getUniLoc(name);
    if (location == -1)
        return false;
    glUniform1fvARB(location, count, values);
    return true;
} // end sendUniform1fv()
Beispiel #3
0
void GPU::Shader::SetUniform( const std::string& name,
                              const void *value )
{
    UniformMap::iterator uu = m_Uniforms.find( name );
    if( uu != m_Uniforms.end() )
    {
	    GLuint activeProgBackup = glGetHandleARB( GL_PROGRAM_OBJECT_ARB );
	    glUseProgramObjectARB( m_Id );

        Uniform &u = uu->second;
	    switch( u.type )
	    {
		    case GL_FLOAT:          glUniform1fvARB( u.location, u.size, (GLfloat*) value ); break;
		    case GL_FLOAT_VEC2_ARB: glUniform2fvARB( u.location, u.size, (GLfloat*) value ); break;
		    case GL_FLOAT_VEC3_ARB: glUniform3fvARB( u.location, u.size, (GLfloat*) value ); break;
		    case GL_FLOAT_VEC4_ARB: glUniform4fvARB( u.location, u.size, (GLfloat*) value ); break;
		    case GL_INT:            glUniform1ivARB( u.location, u.size, (GLint*) value ); break;
		    case GL_INT_VEC2_ARB:   glUniform2ivARB( u.location, u.size, (GLint*) value ); break;
		    case GL_INT_VEC3_ARB:   glUniform3ivARB( u.location, u.size, (GLint*) value ); break;
		    case GL_INT_VEC4_ARB:   glUniform4ivARB( u.location, u.size, (GLint*) value ); break;
		    case GL_BOOL_ARB:       glUniform1ivARB( u.location, u.size, (GLint*) value ); break;
		    case GL_BOOL_VEC2_ARB:  glUniform2ivARB( u.location, u.size, (GLint*) value ); break;
		    case GL_BOOL_VEC3_ARB:  glUniform3ivARB( u.location, u.size, (GLint*) value ); break;
		    case GL_BOOL_VEC4_ARB:  glUniform4ivARB( u.location, u.size, (GLint*) value ); break;
		    case GL_FLOAT_MAT2_ARB: glUniformMatrix2fvARB( u.location, u.size, GL_FALSE, (GLfloat*) value ); break;
		    case GL_FLOAT_MAT3_ARB: glUniformMatrix3fvARB( u.location, u.size, GL_FALSE, (GLfloat*) value ); break;
		    case GL_FLOAT_MAT4_ARB: glUniformMatrix4fvARB( u.location, u.size, GL_FALSE, (GLfloat*) value ); break;
	    }

	    glUseProgramObjectARB( activeProgBackup );
    }
}
Beispiel #4
0
void useShader(shader_type *p, int x, int y, int w, int h, float tx, float ty, float tw, float th, float r, float g, float b, float a)
{
	tglUseProgramObject(p->shader);
	GLfloat t = cur_frame_tick;
	glUniform1fvARB(p->p_tick, 1, &t);
	GLfloat d[4];
	d[0] = r;
	d[1] = g;
	d[2] = b;
	d[3] = a;
	glUniform4fvARB(p->p_color, 1, d);

	GLfloat c[2];
	c[0] = x;
	c[1] = y;
	glUniform2fvARB(p->p_mapcoord, 1, c);

	c[0] = w;
	c[1] = h;
	glUniform2fvARB(p->p_texsize, 1, c);

	d[0] = tx;
	d[1] = ty;
	d[2] = tw;
	d[3] = th;
	glUniform4fvARB(p->p_texcoord, 1, d);

	shader_reset_uniform *ru = p->reset_uniforms;
	while (ru) {
		switch (ru->kind) {
			case UNIFORM_NUMBER:
				glUniform1fvARB(ru->p, 1, &ru->data.number);
				break;
			case UNIFORM_VEC2:
				glUniform2fvARB(ru->p, 1, ru->data.vec2);
				break;
			case UNIFORM_VEC3:
				glUniform3fvARB(ru->p, 1, ru->data.vec3);
				break;
			case UNIFORM_VEC4:
				glUniform4fvARB(ru->p, 1, ru->data.vec4);
				break;
		}
		ru = ru->next;
	}
}
bool Shader::sendUniform1fv(char* varname, GLsizei count, GLfloat *value)
{
    GLint loc = GetUniformLocation(varname);
    if (loc == -1) return false;
    
    glUniform1fvARB(loc, count, value);

    return true;
}
Beispiel #6
0
	void CShadersManager::SetScaleBiasFactor (GLfloat * factor)
	{
		if(this->Program == ParallaxMappingSPT)
		{
			GLint loc = glGetUniformLocationARB(this->Programs[ParallaxMappingSPT].GetHandle(),
												"ScaleBiasFactor");
			glUniform1fvARB(loc, 2, factor);
		}
	}
Beispiel #7
0
static int program_set_uniform_number_fast(lua_State *L)
{
	shader_type *p = (shader_type*)lua_touserdata(L, 1);
	GLfloat i = luaL_checknumber(L, 2);
	bool change = gl_c_shader != p->shader;

	GLint pos = lua_tonumber(L, lua_upvalueindex(1));

	if (change) tglUseProgramObject(p->shader);
	glUniform1fvARB(pos, 1, &i);
	if (change) tglUseProgramObject(0);
	return 0;
}
Beispiel #8
0
void GPU_shader_uniform_vector(GPUShader *UNUSED(shader), int location, int length, int arraysize, float *value)
{
	if (location == -1)
		return;

	GPU_print_error("Pre Uniform Vector");

	if (length == 1) glUniform1fvARB(location, arraysize, value);
	else if (length == 2) glUniform2fvARB(location, arraysize, value);
	else if (length == 3) glUniform3fvARB(location, arraysize, value);
	else if (length == 4) glUniform4fvARB(location, arraysize, value);
	else if (length == 9) glUniformMatrix3fvARB(location, arraysize, 0, value);
	else if (length == 16) glUniformMatrix4fvARB(location, arraysize, 0, value);

	GPU_print_error("Post Uniform Vector");
}
Beispiel #9
0
static int program_use(lua_State *L)
{
	shader_type *p = (shader_type*)lua_touserdata(L, 1);
	bool active = lua_toboolean(L, 2);

	if (active)
	{
		tglUseProgramObject(p->shader);
		// GLfloat t = SDL_GetTicks();
		GLfloat t = cur_frame_tick;
		glUniform1fvARB(p->p_tick, 1, &t);
	}
	else
	{
		tglUseProgramObject(0);
	}

	return 0;
}
Beispiel #10
0
 bool ShaderProgram::SetVariable(const char* name, int varDim, const float* values, int count)
 {
     if (!_linked) return false;
     if (_id)
     {
         GLint location = glGetUniformLocationARB(_id, name);
         if (location == -1) return false;
         switch (varDim)
         {
             case 1: glUniform1fvARB(location, count, values); break;
             case 2: glUniform2fvARB(location, count, values); break;
             case 3: glUniform3fvARB(location, count, values); break;
             case 4: glUniform4fvARB(location, count, values); break;
             default:
                 return false;
         }
         return true;
     } else
         return false;
 }
//-----------------------------------------------------------------------
void GLSLLinkProgram::updatePassIterationUniforms(GpuProgramParametersSharedPtr params)
{
    if (params->hasPassIterationNumber())
    {
        size_t index = params->getPassIterationNumberIndex();

        GLUniformReferenceIterator currentUniform = mGLUniformReferences.begin();
        GLUniformReferenceIterator endUniform = mGLUniformReferences.end();

        // need to find the uniform that matches the multi pass entry
        for (; currentUniform != endUniform; ++currentUniform)
        {
            // get the index in the parameter real list
            if (index == currentUniform->mConstantDef->physicalIndex)
            {
                glUniform1fvARB( currentUniform->mLocation, 1, params->getFloatPointer(index));
                // there will only be one multipass entry
                return;
            }
        }
    }

}
Beispiel #12
0
void loadFieldShader()
{
	GLhandleARB vertexShaderHandle;
	GLhandleARB fragmentShaderHandle;
	
	vertexShaderHandle   = loadShader("VertexShader.c",GL_VERTEX_SHADER);
	fragmentShaderHandle = loadShader("FragmentShader.c",GL_FRAGMENT_SHADER);
	
	fieldShaderId = glCreateProgramObjectARB();
	
	glAttachObjectARB(fieldShaderId,vertexShaderHandle);
	glAttachObjectARB(fieldShaderId,fragmentShaderHandle);
	glLinkProgramARB(fieldShaderId);

	fieldUniform = glGetUniformLocationARB(fieldShaderId, "field");
    if (fieldUniform == -1) {
        printf("Error getting uniform `%s'.\n", "field");
        exit(1);
    }

	kernelsUniform = glGetUniformLocationARB(fieldShaderId, "kernels");
    if (kernelsUniform == -1) {
        printf("Error getting uniform `%s'.\n", "kernels");
        exit(1);
    }

	gainUniform = glGetUniformLocationARB(fieldShaderId, "gain");
    if (gainUniform == -1) {
        printf("Error getting uniform `%s'.\n", "gain");
        exit(1);
    }

    glUseProgramObjectARB(fieldShaderId);
    glUniform1fvARB(kernelsUniform, 25, kernels);
    glUniform1fARB(gainUniform, convolutionGain);
}
Beispiel #13
0
void ofxShader::setUniformVariable1fv (char * name, int count, float * value){
	if (bLoaded == true){
		glUniform1fvARB(glGetUniformLocationARB(shader, name), count, value);
	}
}
Beispiel #14
0
void ofxShader::setUniform1fv (string name, int count, float * value) {
	if(bLoaded)
		glUniform1fvARB(glGetUniformLocationARB(shader, name.c_str()), count, value);
}
Beispiel #15
0
void ccShaderARB::setTabUniform1fv(const char* uniform, int size, float* val)
{
	int loc = glGetUniformLocationARB(prog,uniform);
	glUniform1fvARB(loc,size,val);
}
Beispiel #16
0
void MGLContext::sendUniformFloat(unsigned int fxId, const char * name, float * values, const int count){
	GLint uValue = glGetUniformLocationARB((GLhandleARB)fxId, name);
	if(uValue != -1) glUniform1fvARB(uValue, count, values);
}
Beispiel #17
0
 ShaderProgram &u1(const char *var, GLfloat *f) { glUniform1fvARB(uniform(var), 1, f); return *this; }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_ARBShaderObjects_nglUniform1fvARB(JNIEnv *env, jclass clazz, jint location, jint count, jobject values, jint values_position, jlong function_pointer) {
	const GLfloat *values_address = ((const GLfloat *)(*env)->GetDirectBufferAddress(env, values)) + values_position;
	glUniform1fvARBPROC glUniform1fvARB = (glUniform1fvARBPROC)((intptr_t)function_pointer);
	glUniform1fvARB(location, count, values_address);
}
Beispiel #19
0
void ofxShader::setUniformVariable1fv (char const *name, int count, float const *value) {
	if (loaded)
		glUniform1fvARB(glGetUniformLocationARB(shader, name), count, value);
}
Beispiel #20
0
void GLSLShader::SetFloatVector(GLint variable, GLsizei count, const float *value) { if (variable!=-1) glUniform1fvARB(variable, count, value);   }
Beispiel #21
0
    void DiGLShaderParam::Bind() const
    {
        if (!mShaderLinker || !mShaderLinker->GetGLHandle())
            return;

        // bind built-in (global) uniforms
        auto env = Driver->GetShaderEnvironment();
        for (auto it = mBuiltinFuncs.begin(); it != mBuiltinFuncs.end(); ++it)
        {
            it->second(env, it->first);
        }

        // bind custom uniforms
        for (uint32 i = 0; i < NUM_VARIABLE_TYPES; ++i)
        {
            for (auto it = mShaderParams[i].begin(); it != mShaderParams[i].end(); ++it)
            {
                const DiAny& data = it->second;
                if (data.isEmpty())
                    continue;

                GLuint location = 0;
                int samplerUnit = 0;

                if (i == VARIABLE_SAMPLER2D || i == VARIABLE_SAMPLERCUBE)
                {
                    auto sampler = mShaderLinker->GetSampler(it->first);
                    if (!sampler)
                        continue;
                    location = sampler->location;
                    samplerUnit = (int)sampler->unit;
                }
                else
                {
                    auto constant = mShaderLinker->GetConstant(it->first);
                    if (!constant)
                        continue;
                    location = constant->location;
                }

                switch (i)
                {
                case DiShaderParameter::VARIABLE_FLOAT:
                    {
                        float val = any_cast<float>(data);
                        glUniform1fvARB(location, 1, &val);
                        break;
                    }
                case DiShaderParameter::VARIABLE_FLOAT2:
                    {
                        DiVec2 vec2 = any_cast<DiVec2>(data);
                        glUniform2fvARB(location, 1, vec2.ptr());
                        break;
                    }
                case DiShaderParameter::VARIABLE_FLOAT3:
                    {
                        DiVec3 vec3 = any_cast<DiVec3>(data);
                        glUniform3fvARB(location, 1, vec3.ptr());
                        break;
                    }
                case DiShaderParameter::VARIABLE_FLOAT4:
                    {
                        DiVec4 vec4 = any_cast<DiVec4>(data);
                        glUniform4fvARB(location, 1, vec4.ptr());
                        break;
                    }
                case DiShaderParameter::VARIABLE_MAT4:
                    {
                        DiMat4 vec4 = any_cast<DiMat4>(data);
                        glUniformMatrix4fvARB(location, 1, GL_TRUE, vec4[0]);
                        break;
                    }
                case DiShaderParameter::VARIABLE_COLOR:
                    {
                        DiColor c = any_cast<DiColor>(data);
                        DiVec4 vec4(c.r,c.g,c.b,c.a);
                        glUniform4fvARB(location, 1, vec4.ptr());
                        break;
                    }
                case DiShaderParameter::VARIABLE_FLOAT4_ARRAY:
                    {
                        DiPair<DiVec4*,uint32> v4Arr = any_cast<DiPair<DiVec4*,uint32>>(data);
                        glUniform4fvARB(location, v4Arr.second, v4Arr.first->ptr());
                        break;
                    }
                case DiShaderParameter::VARIABLE_SAMPLER2D:
                case DiShaderParameter::VARIABLE_SAMPLERCUBE:
                    {
                        glUniform1ivARB(location, 1, &samplerUnit);
                        DiTexture* tex = any_cast<DiTexture*>(data);
                        tex->Bind((uint32)samplerUnit);
                    }
                    break;
                }
            }
        }
    }
//-----------------------------------------------------------------------
void GLSLLinkProgram::updateUniforms(GpuProgramParametersSharedPtr params,
                                     uint16 mask, GpuProgramType fromProgType)
{
    // iterate through uniform reference list and update uniform values
    GLUniformReferenceIterator currentUniform = mGLUniformReferences.begin();
    GLUniformReferenceIterator endUniform = mGLUniformReferences.end();

    for (; currentUniform != endUniform; ++currentUniform)
    {
        // Only pull values from buffer it's supposed to be in (vertex or fragment)
        // This method will be called twice, once for vertex program params,
        // and once for fragment program params.
        if (fromProgType == currentUniform->mSourceProgType)
        {
            const GpuConstantDefinition* def = currentUniform->mConstantDef;
            if (def->variability & mask)
            {

                GLsizei glArraySize = (GLsizei)def->arraySize;

                // get the index in the parameter real list
                switch (def->constType)
                {
                case GCT_FLOAT1:
                    glUniform1fvARB(currentUniform->mLocation, glArraySize,
                                    params->getFloatPointer(def->physicalIndex));
                    break;
                case GCT_FLOAT2:
                    glUniform2fvARB(currentUniform->mLocation, glArraySize,
                                    params->getFloatPointer(def->physicalIndex));
                    break;
                case GCT_FLOAT3:
                    glUniform3fvARB(currentUniform->mLocation, glArraySize,
                                    params->getFloatPointer(def->physicalIndex));
                    break;
                case GCT_FLOAT4:
                    glUniform4fvARB(currentUniform->mLocation, glArraySize,
                                    params->getFloatPointer(def->physicalIndex));
                    break;
                case GCT_MATRIX_2X2:
                    glUniformMatrix2fvARB(currentUniform->mLocation, glArraySize,
                                          GL_TRUE, params->getFloatPointer(def->physicalIndex));
                    break;
                case GCT_MATRIX_2X3:
                    if (GLEW_VERSION_2_1)
                    {
                        glUniformMatrix2x3fv(currentUniform->mLocation, glArraySize,
                                             GL_TRUE, params->getFloatPointer(def->physicalIndex));
                    }
                    break;
                case GCT_MATRIX_2X4:
                    if (GLEW_VERSION_2_1)
                    {
                        glUniformMatrix2x4fv(currentUniform->mLocation, glArraySize,
                                             GL_TRUE, params->getFloatPointer(def->physicalIndex));
                    }
                    break;
                case GCT_MATRIX_3X2:
                    if (GLEW_VERSION_2_1)
                    {
                        glUniformMatrix3x2fv(currentUniform->mLocation, glArraySize,
                                             GL_TRUE, params->getFloatPointer(def->physicalIndex));
                    }
                    break;
                case GCT_MATRIX_3X3:
                    glUniformMatrix3fvARB(currentUniform->mLocation, glArraySize,
                                          GL_TRUE, params->getFloatPointer(def->physicalIndex));
                    break;
                case GCT_MATRIX_3X4:
                    if (GLEW_VERSION_2_1)
                    {
                        glUniformMatrix3x4fv(currentUniform->mLocation, glArraySize,
                                             GL_TRUE, params->getFloatPointer(def->physicalIndex));
                    }
                    break;
                case GCT_MATRIX_4X2:
                    if (GLEW_VERSION_2_1)
                    {
                        glUniformMatrix4x2fv(currentUniform->mLocation, glArraySize,
                                             GL_TRUE, params->getFloatPointer(def->physicalIndex));
                    }
                    break;
                case GCT_MATRIX_4X3:
                    if (GLEW_VERSION_2_1)
                    {
                        glUniformMatrix4x3fv(currentUniform->mLocation, glArraySize,
                                             GL_TRUE, params->getFloatPointer(def->physicalIndex));
                    }
                    break;
                case GCT_MATRIX_4X4:
                    glUniformMatrix4fvARB(currentUniform->mLocation, glArraySize,
                                          GL_TRUE, params->getFloatPointer(def->physicalIndex));
                    break;
                case GCT_INT1:
                    glUniform1ivARB(currentUniform->mLocation, glArraySize,
                                    (GLint*)params->getIntPointer(def->physicalIndex));
                    break;
                case GCT_INT2:
                    glUniform2ivARB(currentUniform->mLocation, glArraySize,
                                    (GLint*)params->getIntPointer(def->physicalIndex));
                    break;
                case GCT_INT3:
                    glUniform3ivARB(currentUniform->mLocation, glArraySize,
                                    (GLint*)params->getIntPointer(def->physicalIndex));
                    break;
                case GCT_INT4:
                    glUniform4ivARB(currentUniform->mLocation, glArraySize,
                                    (GLint*)params->getIntPointer(def->physicalIndex));
                    break;
                case GCT_SAMPLER1D:
                case GCT_SAMPLER1DSHADOW:
                case GCT_SAMPLER2D:
                case GCT_SAMPLER2DSHADOW:
                case GCT_SAMPLER3D:
                case GCT_SAMPLERCUBE:
                    // samplers handled like 1-element ints
                    glUniform1ivARB(currentUniform->mLocation, 1,
                                    (GLint*)params->getIntPointer(def->physicalIndex));
                    break;
                case GCT_UNKNOWN:
                    break;

                } // end switch
#if OGRE_DEBUG_MODE
                checkForGLSLError( "GLSLLinkProgram::updateUniforms", "Error updating uniform", 0 );
#endif
            } // variability & mask
        } // fromProgType == currentUniform->mSourceProgType

    } // end for
}