Sign in to follow this  
Volgogradetzzz

Are GPU constants really constants?

Recommended Posts

Hello everyone! Recently I was told that some GPUs (mobile mostly) doesn't keep constants between render calls. They doesn't have constant's memory. So you can't set constants only once and use this all the time. Is that true? Never heard that before.

Share this post


Link to post
Share on other sites

I don't believe this is a GPU limitation, but rather a limitation of apis like open gl es 2 for example, which do not allow you to bind constants to registers, but rather assign them to "slots" associated with your shader program. So if you don't change shader, you don't need to reset them, but everytime you bind a new shader you will need to reset all constants. D3d9/11/ogles3/ogl4/etc. do not have that limitation.

Share this post


Link to post
Share on other sites

By "GPU constants" I assume you mean the constants that are stored in a constant buffer, and yes, they are really constants - to the GPU at least, but the CPU can modify them whenever it wants between render calls. The GPU can only specify a default value for them, which is used in case the CPU never initializes them.

So they are not true constants, but if you only initialize them once (either in the shader or in the CPU), and never modify them in the CPU after that, then they are just like constants - their initial value will be preserved across render calls.

 

GPUs also have "true constants", tohugh: the numeric literals used in HLSL/GLSL.

 

It makes no difference that the GPU is a "mobile" one.

Share this post


Link to post
Share on other sites

I heard some GPU's simply reserve a section of global memory for constant buffers which is then pushed into the (small) hardware cache so that shaders can access them very quickly. So they are not technically read-only (e.g. they could plausibly get written to through a driver bug) but doing so would violate assumptions made by the driver, shader, and application, so the runtime tries very hard to ensure shaders cannot write to these constants.

 

I'm not sure if that is how it happens anymore, but in any case, this is all abstracted from you by the hardware layer and the graphics API, so you should avoid relying on assumptions about the underlying hardware implementation if you want to ensure your code runs the same everywhere.

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