Jump to content

View more

Image of the Day

#ld38 #screenshotsaturday Mimosa Fizz action gif #2 https://t.co/TUzdppvfUL
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Sending single float to shader

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
6 replies to this topic

#1 Shawn619   Members   

342
Like
0Likes
Like

Posted 02 July 2013 - 04:31 PM

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?



#2 Shawn619   Members   

342
Like
0Likes
Like

Posted 02 July 2013 - 07:21 PM

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.



#3 MarekKnows.com   Members   

1036
Like
1Likes
Like

Posted 10 July 2013 - 11:16 AM

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.


---
Free C++, OpenGL, and Game Development Video Tutorials @
www.MarekKnows.com
Play my free games: Ghost Toast, Zing, Jewel Thief


#4 ic0de   Members   

974
Like
1Likes
Like

Posted 10 July 2013 - 12:24 PM

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, 10 July 2013 - 12:25 PM.

Currently working on new DOS game, Chuck Jones: Space Cop of the Future, Check out my Dev Blog
 

you know you program too much when you start ending sentences with semicolons;


#5 mhagain   Members   

13162
Like
2Likes
Like

Posted 10 July 2013 - 01:18 PM

 

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.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#6 Shawn619   Members   

342
Like
0Likes
Like

Posted 11 July 2013 - 04:21 PM

Thanks for the tips!



#7 Sik_the_hedgehog   Members   

3002
Like
0Likes
Like

Posted 12 July 2013 - 02:08 PM


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.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.