Sending single float to shader

Started by
5 comments, last by Sik_the_hedgehog 10 years, 10 months ago

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?

Advertisement

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.

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.

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);
}

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Thanks for the tips!


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.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

This topic is closed to new replies.

Advertisement