Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Sending single float to shader


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   -  Reputation: 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?



Sponsor:

#2 Shawn619   Members   -  Reputation: 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   -  Reputation: 730

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   -  Reputation: 909

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.

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


#5 mhagain   Crossbones+   -  Reputation: 8275

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   -  Reputation: 342

Like
0Likes
Like

Posted 11 July 2013 - 04:21 PM

Thanks for the tips!



#7 Sik_the_hedgehog   Crossbones+   -  Reputation: 1833

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.



PARTNERS