[REQUEST] What is a Push Constant in Vulkan? : Khronos

Push constants let us send a small amount of data (it has a limited size) to the shader, in a very simple and performant way. Push constants can send data to any shader stage (both vertex and fragment shaders), and are stored in the command buffer itself.

To use push constants, you first need to set their size (in each stage) when you create a VkPipelineLayout. Then, using the command vkCmdPushConstants, the data will be embedded into the command buffer, and will be accessible from the shader. We are going to use them for transformation matrices, to be able to move objects around and do proper 3d rendering. To start, we are going to use it to move the triangle we are rendering.

What is a push constant?

Is a push constant’s main use for the “camera”?

First, there’s no need to put things like “[REQUEST]” and “[BUG]” or whatever in your title. We can read your title and figure out what kind of thing it is.

As for your question… what is it about the text you quoted that you don’t understand? That is, what do you mean by “what is a push constant”? I could answer that, but my answer would be essentially a differently-worded version of what you quoted.

So it would be better to just say what it is you don’t understand.

It is very helpful for people to search for.

If the intent is to fudge, maybe someone else could explain it?

I don’t understand how to implement push constants.

There are no tutorials or explainations available.

Googling returns “push constant is a small shader block of uniform data stored within a command buffer, to pass data to a shader without the need for uniform buffers.”

So, all I know is push constants are related to shaders and you have to edit your shader to replace the UBO.

So, how do newbie programmers implement push constants after the Vulkan tutorial?

It is useful:

  1. To the extent that everyone uses it.
  2. To the extent that everyone knows that these are keywords to search for.
  3. To the extent that everyone agrees as to what these keywords mean.

Since none of these are true, it’s not helpful.

It’d be easier if you just plainly and directly ask for what you’re looking for.

I searched for “vulkan tutorials push constants” and found this link and this link in my first hit. The second hit is a fully annotated tutorial.

Are you sure you did any searching before asking?

Push constants are a way to transmit data to a shader. So there are two parts to them: the host code that will transmit information to the shader, and the in-shader definition that will receive them.

Shaders in Vulkan usually access information stored in memory through a descriptor resource. Push constants aren’t descriptors though; they live outside of that system.

Instead of having a piece of user-allocated memory storage, push constant storage is ephemeral. When you bind a program pipeline, you are effectively creating a few bytes of push constant storage memory. You can upload CPU data to this memory via vkCmdPushConstants. Rendering or dispatch commands issued after this function can read from this memory through push constant uniform values. No synchronization is needed, as vkCmdPushConstants effectively executes immediately (within the command buffer).

Assuming you’re using GLSL to define your shaders, you can access push constants from your shader with the push_constant layout qualifier applied to a uniform block:

layout(push_constant) uniform my_push_constants
{
  vec4 variable1;
  float variable2;
};

This is a uniform block, so the data you upload needs to match its memory layout. But the layout of a push constant block fits GLSL’s std430 layout rules, not the usual std140 layout rules used by regular UBOs. That doesn’t matter in this case; either way, the above uniform block takes up 20 bytes (16 for variable1, 4 for variable2).

So when you call vkCmdPushConstants, you need to upload memory containing 20 bytes, which represent 5 contiguous float objects. You also need to specify which shader stage(s) can read from that push constant range.

Also, when creating your pipeline (or rather, the pipeline layout your pipeline uses), you need to tell it how many bytes of push constant each shader stage can access. While this is technically part of your shaders, it needs to be stated directly at pipeline creation time.

2 Likes

Well explained, I don’t need to look for it anymore :grin:

You made audacious claims.

Do you have evidence to back that claim up?

Think about it this way.

If you claim that push constants are so clear, how did you find so many queries asking, “What is a push constant?”

If you really want to go into technical jargon and fudge, please go to Stack Overflow.

Alfonse, you are the principal curator of the openGL Wiki.

Wikis are meant to remove technical jargon and explain obtuse concepts as simply as possible.

It is fairly obvious that push constants are still an unclear concept and is a term commonly pointing back to Vulkan.

Perhaps, something easier to ask would be, “What is the equivalent concept of a push constant in OpenGL?”

Is there any intention to create a weeder program for Vulkan? ie remove/bar people from learning Vulkan if they don’t meet some arbitrary benchmark.

That’s not how burden-of-proof works.

You are the one claiming that these tags are useful. As the rest of the forum users don’t use them, the burden of proving that these tags are useful is on you, not on us.

I have claimed no such thing. I asked you what it was you didn’t understand about them. I asked you to clarify your question. That’s not a claim that the term is “clear” or anything of the kind; merely that I do not understand what exactly it is you’re trying to figure out.

Furthermore, you’re dodging my main point: you did not do the searches you claimed you did. The presence of “so many queries” makes it highly unlikely that genuine effort to search for tutorials on the topic would not have led you to them.

Are you saying that your question really was, “please write me a Wiki article/tutorial on push constants”? Because that’s a pretty big thing to ask people for, and if that’s what you want, you should be direct and up-front about it.

The closest thing OpenGL has would be a non-block-scoped uniform (that isn’t of an opaque type, those are descriptors in Vulkan). But that’s primarily relative to the ease of changing their values compared to the alternatives, not the internal implementation, mechanism for specification, or its relative limitations.

So it would be better to say that OpenGL doesn’t really have an “equivalent concept”. There are some similarities between certain OpenGL constructs, but that’s about it.

Asking someone to clarify a broad question is not a “weeder program”. And yes, we have expectations about posting topics here, which include making a reasonable effort on your own to resolve the issue. Namely:

  1. There are many good sources of information on Khronos Technology available on the web. You should check these before posting.
  2. Google is your friend. Or Bing, if you prefer. Or whatever searching service you prefer; it doesn’t matter. It would be best for all involved if your question could not be answered via a simple search based on the terms you’re interested in.

I do not think that this is an unreasonable expectation, nor does it qualify as “remove/bar people from learning Vulkan”.

Here’s another example in the Khronos Forum. I have proof.

Your accusations are nonsensical, there is no suitable option online.

Perhaps, it’s best if you check Sasche Willem’s Github page and compare the time and dates on the posts before you comment on others.

Many individuals really have no idea what is a push constant.

If you can show me a Youtube video that explains what a push constant is in context to Vulkan, I’ll accept it as an answer immediately.

Why you linked a .cpp file instead of explaining a push constant and how to implement it, I have no idea.

That was not the original question, the OP remains the same.

If you would be kind enough to share your knowledge with a Wiki, could you kindly?

“No equivalent concept”.

It’s related to the UBO but it’s not.

See, the issue yet?

Why are you so aggressive against newbie developers?

From your profile, you have a recorded history of fudging answers on the Khronos forum and getting into arguments with newbie programmers ie Gatekeeper Syndrome.

You have a wealth of knowledge about Vulkan, OpenGL and graphics engines that you’ve accumulated.

No one is here to hurt/steal/harm Vulkan.

Yet, instead of sharing and explaining what a push constant is and what it does, you accuse people of not even bothering to Google?

If the answer was as simple as you claimed, use simple English to explain it in the context of Vulkan.

Prove me wrong in front of the entire planet.

When anyone on this planet wants to know what a push constant is, let them Google this question and find Reinhart’s clear and non-technical-jargon explaination and implementation!