Are GPU constants really constants?

Started by
2 comments, last by Bacterius 10 years, 4 months ago

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.

Advertisement

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.

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.

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Some GPUs have some rather limited constant memory, some have another instance of dedicated read-only L1 cache in addition to the "normal" cache (nVidia Kepler is an example of that). Others do not have dedicated constant memory, but simply store constant data in normal memory.

Regardless, constants that you have set certainly retain their value. This is a hardware-independent guarantee given by the API (OpenGL or DirectX or whatever you use). It doesn't mean that the driver isn't possibly moving stuff around on the card and moving constant data in and out as needed, it only means that for all you can tell, things are as promised. It is nevertheless highly likely that the driver will not touch constant data at all (seeing how it's most probably going to be used).

This topic is closed to new replies.

Advertisement