Sign in to follow this  
Followers 0
Shawn619

Sending single float to shader

6 posts in this topic

Goal: To send a single float, that changes every frame, to the shader.

 

In the update method, i'm guessing I call it like so:

//timeOfDay is a global float
 
//in update
GLint loc = glGetUniformLocation(shaderOne.getShaderIndex(), "TimeOfDay");
glUniform1f(loc, timeOfDay)
 

In the vertex and fragment shader, can I use the passed variables like this:

uniform float TimeOfDay;
 
//do stuff with TimeOfDay

Is this correct?

0

Share this post


Link to post
Share on other sites

Solved!

 

I thought I was supposed to set the shader to NULL after all my draw calls, but setting the shader to NULL and restarting the same shader apparently erases all pre-existing uniform data.

0

Share this post


Link to post
Share on other sites

Yup, looks good.  

 

Just remember, your graphics card only has a limited number of "locations" to keep track of your variables.  A float takes up the same number of locations (1) as a vector4.  So if you end up making a complicated shader that requires lots of data to be passed around, you may want to bundle your float's into vector4's since then you will be able to pass 4 parameters around but still only take up one location.

1

Share this post


Link to post
Share on other sites

Just a tip, if you call glGetUniformLocation every time you update your variable you run the risk of stalling your pipeline which heavily degrades performance.

 

It's better to get the location once after linking your shader and then storing that location to be used everytime you update or use a static variable to cache that data like so:

void updateTimeOfDay()
{
	static bool haveLoc;
	static GLuint loc;

	if(!haveLoc)
	{
		loc = glGetUniformLocation(shaderOne.getShaderIndex(), "TimeOfDay");
		haveLoc = true;
	}

	glUniform1f(loc, timeOfDay);
}
Edited by ic0de
1

Share this post


Link to post
Share on other sites

 

Just a tip, if you call glGetUniformLocation every time you update your variable you run the risk of stalling your pipeline which heavily degrades performance.

 

I'd expect that drivers would store uniform locations locally rather than needing to round-trip to the GPU in order to get them.  OpenGL uniform locations bear absolutely no relation to what's actually happening in hardware, after all, and drivers already need to do a job of translating them to actual GPU registers, so drivers already have a "uniform table" of some kind implemented.

 

The performance hit would be needing to do a string-based lookup on this table, which is not as bad but which should still be avoided; your advice to cache and reuse the location remains valid.

2

Share this post


Link to post
Share on other sites


Just a tip, if you call glGetUniformLocation every time you update your variable you run the risk of stalling your pipeline which heavily degrades performance.

Given he said it changes "every frame", I assume that means it changes only once per frame. The penalty of that is probably minimal, especially since you'd be already waiting for the pipeline to finish in order to swap buffers.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0