<blockquote>
  <p>I have about ~200 bytes worth of data that I expect to be mostly constant. That is, I will change them very infrequently.</p>
</blockquote>

<p>If the data changes infrequently, you can use <a href="https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#pipelines-specialization-constants" rel="nofollow noreferrer">Specialization Constants</a>. They are set when the pipeline is created, and a new pipeline must be created if you need to change the values. For an infrequent event, like a window resize, this may be an acceptable cost.</p>

<blockquote>
  <p>what if I have e.g. a float random_seed that I will update every time the shader is run? </p>
</blockquote>

<p>That's a perfect use case for Push Constants. You can keep all the other data in the UBO (or in the Specialization Constants) and change this value with <code>VkCmdPushConstants</code>.</p>

CC BY-SA 3.0
2017-10-06T16:44:43.843Z
2017-10-06T16:44:43.843Z
vazgriz
<blockquote>
  <p>UBOs can be updated at any time with vkCmdUpdateBuffer </p>
</blockquote>

<p>From the specification: "vkCmdUpdateBuffer is only allowed outside of a render pass." So "at any time" is not the case.</p>

<p>Even if it were allowed inside of a render pass, it's still a transfer operation. Which means you need to synchronize the memory transfer with the commands that use it. Which slows down performance.</p>

<p>For the general Push Constant vs. Uniform thing, use your judgment. By "judgment", I mean just look at how they work. Push Constants allow you to change their data at any time without doing heavy-weight processes like memory operations, synchronization, or altering the descriptor state. Clearly, they are for frequently changing data. How frequently is "frequently"? Well, that's a judgment call.</p>

<p>Failing that, profile the performance difference.</p>

CC BY-SA 3.0
2017-08-28T14:33:07.843Z
2017-09-19T21:28:12.283Z
2017-09-19T21:28:12.283Z
Nicol Bolas
<p>I'm sort of struggling to understand the conceptual difference between uniform buffers and push constants. From what I can gather by reading the spec, the main differences are:</p>

<ol>
<li>Uniform buffers can be <em>much</em> larger than push constants.</li>
<li>UBOs use std140, PCs use std430.</li>
<li>UBOs can be updated at any time with vkCmdUpdateBuffer (or host mapping) and persist their values otherwise, PCs have to be re-pushed for each render pass. (Which surprised me - based on the name. I thought I would literally be updating constants in the pipeline in-place, and have those changes persist)</li>
</ol>

<p>In my scenario, I have about ~200 bytes worth of data that I expect to be <strong>mostly constant</strong>. That is, I will change them very infrequently. Would it be better to (assuming the size permits) use push constants even though I have to re-send them in every command buffer? Or would it be better to use a 200-byte UBO and only update it infrequently with vkCmdUpdatebuffer?</p>

<p>Also. what if I have e.g. a <code>float random_seed</code> that I will update every time the shader is run? Assuming I already have a UBO, would it be better to lump that in with the UBO, even though the rest of the UBO is constant, or would I gain a benefit from using push constants for specifically this variable, so I can avoid having to vkCmdUpdateBuffer before every render pass?</p>

CC BY-SA 3.0
2017-08-28T13:29:55.270Z
Welcome to the Computer Graphics Stack Exchange site! I am not familiar with Vulkan but it seems using an UBO would be more efficient in your case (you can try to compare the performances of both approaches in a sample program). Which graphic APIs are you familiar with? Also you might be interested by this GDC 2012 presentation: [Don't Throw it all Away: Efficient Buffer Management](http://gamedevs.org/uploads/efficient-buffer-management.pdf)
wip
CC BY-SA 3.0
2017-08-28T13:44:31.957Z
From [Vulkan documentation](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#vkCmdPushConstants): while an UBO allocates a block of video memory on the GPU (that you can update at a later timing), the Push Constant does not use video memory (which is why it must be provided during every draw/compute call, or else the shader would not know which value to use). I guess it is stored in an other short-term, fast storage area on the GPU, though the details might depend on your GPU vendor.
wip
<vulkan>
Vulkan: Uniform Buffers versus Push Constants for static data
haasn
# Vulkan: Uniform Buffers versus Push Constants for static data

(Asked by: username_0 on 2017-08-28)

I'm sort of struggling to understand the conceptual difference between uniform buffers and push constants. From what I can gather by reading the spec, the main differences are:

1. Uniform buffers can be *much* larger than push constants.
2. UBOs use std140, PCs use std430.
3. UBOs can be updated at any time with vkCmdUpdateBuffer (or host mapping) and persist their values otherwise, PCs have to be re-pushed for each render pass. (Which surprised me - based on the name. I thought I would literally be updating constants in the pipeline in-place, and have those changes persist)

In my scenario, I have about ~200 bytes worth of data that I expect to be **mostly constant**. That is, I will change them very infrequently. Would it be better to (assuming the size permits) use push constants even though I have to re-send them in every command buffer? Or would it be better to use a 200-byte UBO and only update it infrequently with vkCmdUpdatebuffer?

Also. what if I have e.g. a `float random_seed` that I will update every time the shader is run? Assuming I already have a UBO, would it be better to lump that in with the UBO, even though the rest of the UBO is constant, or would I gain a benefit from using push constants for specifically this variable, so I can avoid having to vkCmdUpdateBuffer before every render pass?

**Tags:** vulkan

> **Comments:**
> **username_1**: Welcome to the Computer Graphics Stack Exchange site! I am not familiar with Vulkan but it seems using an UBO would be more efficient in your case (you can try to compare the performances of both approaches in a sample program). Which graphic APIs are you familiar with? Also you might be interested by this GDC 2012 presentation: [Don't Throw it all Away: Efficient Buffer Management](http://gamedevs.org/uploads/efficient-buffer-management.pdf)  
> **username_1**: From [Vulkan documentation](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#vkCmdPushConstants): while an UBO allocates a block of video memory on the GPU (that you can update at a later timing), the Push Constant does not use video memory (which is why it must be provided during every draw/compute call, or else the shader would not know which value to use). I guess it is stored in an other short-term, fast storage area on the GPU, though the details might depend on your GPU vendor.  

---

## 2 answers

### Answer by username_3 on 2017-08-28. Score: 11 | Accepted answer

> UBOs can be updated at any time with vkCmdUpdateBuffer

From the specification: "vkCmdUpdateBuffer is only allowed outside of a render pass." So "at any time" is not the case.

Even if it were allowed inside of a render pass, it's still a transfer operation. Which means you need to synchronize the memory transfer with the commands that use it. Which slows down performance.

For the general Push Constant vs. Uniform thing, use your judgment. By "judgment", I mean just look at how they work. Push Constants allow you to change their data at any time without doing heavy-weight processes like memory operations, synchronization, or altering the descriptor state. Clearly, they are for frequently changing data. How frequently is "frequently"? Well, that's a judgment call.

Failing that, profile the performance difference.


---

### Answer by username_2 on 2017-10-06. Score: 3

> I have about ~200 bytes worth of data that I expect to be mostly constant. That is, I will change them very infrequently.

If the data changes infrequently, you can use [Specialization Constants](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#pipelines-specialization-constants). They are set when the pipeline is created, and a new pipeline must be created if you need to change the values. For an infrequent event, like a window resize, this may be an acceptable cost.

> what if I have e.g. a float random\_seed that I will update every time the shader is run?

That's a perfect use case for Push Constants. You can keep all the other data in the UBO (or in the Specialization Constants) and change this value with `VkCmdPushConstants`.


---

