<p>It sounds like you want to gather timings for say 100 frames (x frames) then show that data histogram style.</p>
<p>You could store the histogram data directly in the shader as an array of floats and draw directly out of the array using the vertex index id, but to update the histogram the array would have to be rewritten inside the shader, then the shader reloaded and recompiled. Overall it is not a very appealing approach.</p>
<p>A simple approach that is much more flexible, is to write the sample data into a vertex attributes buffer. Bind the vertex buffer and at draw time, the GPU feeds the data to your shader. Each histogram gl_line requires two values, so the first value would be zero and the second value would be the timing value. Repeat that for every timing value. The histogram then becomes a series of lines where the length of the line is determined by the passed in timing, the x position of the line is determined by the vertex index id, and the height of the lines can be uniformly scaled. Finally use a vec3 offset to move the histogram anywhere on the output buffer. gl line width can be used to thicken the lines up so the entire histogram isn't minuscule in size.</p>
<p>Now the histogram draw is just a matter of binding the vertex buffer containing the timing data, and doing the draw call. Changing the number of values is just a matter of writing a bigger vertex buffer and changing the number of vertices in the draw call.</p>
<p>To update the histogram gather more data, push it out to the vertex buffer and draw.</p>
<p>Once this is working it is easy to get fancy with ring buffers, using quads for the histogram lines, animating, etc.</p>
<p><a href="https://i.sstatic.net/IIHeQ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/IIHeQ.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2023-07-01T13:12:11.217Z
He said, he is using directX, so gl line width is not available. There is no directX implementation for it. Alternatively you can do it this was: https://stackoverflow.com/questions/42510542/render-thick-lines-with-instanced-rendering-in-directx-11/43518929#43518929
Thomas
CC BY-SA 4.0
2023-07-01T14:03:26.107Z
line width isn't needed to implement this it is just a nicety.
pmw1234
CC BY-SA 4.0
2023-07-01T14:39:30.190Z
Yes, I know... I only wanted to say, that it is not available...
Thomas
CC BY-SA 4.0
2023-06-29T23:24:53.847Z
2023-07-01T14:08:37.537Z
2023-07-01T14:08:37.537Z
pmw1234
<p>I don't think that you have access to the current time on GPU / pixel shader. So you need to transmit the information from CPU to GPU.</p>
<p>There are many ways to store the framerate results.</p>
<p><strong>Example storing directly into a texture:</strong></p>
<p>You can create a texture with height (framerate max) and width (number of samples) and store directly the new framerate value to this texture. The texture sampler can be set to mode <code>wrap</code> so that you can use the texture as ring buffer.
This example has many disadvantages:</p>
<ol>
<li>You need to define the maximum and minimum framerate which should be visible. This is a disadvantage because the framerate can vary a lot depending on the complexity of the rendered geometry and the complexity of the shader which render the geometry. So when rotating the camera the number of pixel which got hit may vary a lot, so you can have huge jumps within your texture (see figure1 a).</li>
<li>The next disadvantage is that when you render very complex geometries where your framerate drops a lot, your framerate will show the minimum framerate. But the framerate will not be the same. Lets say it changes from 0.01 frames per second to 0.4 frames per second. So each value within this range will be stored at <code>min framerate</code> but the factor from the minimum to the maximum is 40! (see figure1 b)</li>
<li>When rendering only a small triangle, your framerate can increase and be outside the scale of your texture. Therefor it will not be visible inside your texture (see figure1 c).</li>
</ol>
<p><a href="https://i.sstatic.net/JHHEA.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/JHHEA.png" alt="enter image description here" /></a></p>
<p><strong>Figure1:</strong> Displaying the disadvantages of storing the framerate directly to a texture.</p>
<p><strong>Example storing framerate to a buffer:</strong></p>
<p>First create a buffer on GPU where the framerates will be stored to and use it as ring buffer. Create a shader to visualize the histogram by passing an integer value as current position of the buffer and use modulo operator to find the correct position in the buffer when rendering with your pixel shader. This approch will have the following benefits:</p>
<ol>
<li>The buffer is very small in contrast to a texture.</li>
<li>You only need to transfer 2 values (current position in buffer, new framerate value).</li>
<li>You are not restricted to a minimum or maximum framerate which can be displayed.</li>
<li>By passing the additional variables <code>minFramerate</code> and <code>maxFramerate</code> you can scale the histogram. So that in the example case b) will show you the big differences between 0.01 frames per second and 0.4 frames per second. The framerate also can not be outside of the maximal showable framerate (example case c).</li>
<li>you can draw lines from one value to the next value, so there will be no gabs in between.</li>
<li>you can change the shader from drawing connected lines to boxes if you like.</li>
</ol>

CC BY-SA 4.0
2023-06-29T21:21:01.583Z
2023-07-01T10:34:11.607Z
2023-07-01T10:34:11.607Z
Thomas
<p>I am about to implement a framerate histogramm chart for a DirectX Renderer.</p>
<p>Ideally I would do this in pure shader code, but for a a histogramm I need to store the timestamps of the last <code>x</code> frames. I guess I could store these to a texture, but I was wondering if there is a more appropriate way to achieve that?</p>
<p>Thanks!</p>

CC BY-SA 4.0
2023-06-30T10:57:39.393Z
There are many ways to visualize such a histogram. Please add a picture of how your result should look like
Thomas
<hlsl><fragment-shader><directx>
Storing array of floats within shader
user20154
# Storing array of floats within shader

(Asked by: username_0 on 2023-06-29)

I am about to implement a framerate histogramm chart for a DirectX Renderer.

Ideally I would do this in pure shader code, but for a a histogramm I need to store the timestamps of the last `x` frames. I guess I could store these to a texture, but I was wondering if there is a more appropriate way to achieve that?

Thanks!

**Tags:** hlsl, fragment-shader, directx

> **Comments:**
> **username_1**: There are many ways to visualize such a histogram. Please add a picture of how your result should look like  

---

## 2 answers

### Answer by username_2 on 2023-06-29. Score: 0

It sounds like you want to gather timings for say 100 frames (x frames) then show that data histogram style.

You could store the histogram data directly in the shader as an array of floats and draw directly out of the array using the vertex index id, but to update the histogram the array would have to be rewritten inside the shader, then the shader reloaded and recompiled. Overall it is not a very appealing approach.

A simple approach that is much more flexible, is to write the sample data into a vertex attributes buffer. Bind the vertex buffer and at draw time, the GPU feeds the data to your shader. Each histogram gl\_line requires two values, so the first value would be zero and the second value would be the timing value. Repeat that for every timing value. The histogram then becomes a series of lines where the length of the line is determined by the passed in timing, the x position of the line is determined by the vertex index id, and the height of the lines can be uniformly scaled. Finally use a vec3 offset to move the histogram anywhere on the output buffer. gl line width can be used to thicken the lines up so the entire histogram isn't minuscule in size.

Now the histogram draw is just a matter of binding the vertex buffer containing the timing data, and doing the draw call. Changing the number of values is just a matter of writing a bigger vertex buffer and changing the number of vertices in the draw call.

To update the histogram gather more data, push it out to the vertex buffer and draw.

Once this is working it is easy to get fancy with ring buffers, using quads for the histogram lines, animating, etc.

[![enter image description here](https://i.sstatic.net/IIHeQ.png)](https://i.sstatic.net/IIHeQ.png)

> **Comments:**
> **username_1**: He said, he is using directX, so gl line width is not available. There is no directX implementation for it. Alternatively you can do it this was: https://stackoverflow.com/questions/42510542/render-thick-lines-with-instanced-rendering-in-directx-11/43518929#43518929  
> **username_2**: line width isn't needed to implement this it is just a nicety.  
> **username_1**: Yes, I know... I only wanted to say, that it is not available...  

---

### Answer by username_1 on 2023-06-29. Score: 0

I don't think that you have access to the current time on GPU / pixel shader. So you need to transmit the information from CPU to GPU.

There are many ways to store the framerate results.

**Example storing directly into a texture:**

You can create a texture with height (framerate max) and width (number of samples) and store directly the new framerate value to this texture. The texture sampler can be set to mode `wrap` so that you can use the texture as ring buffer.
This example has many disadvantages:

1. You need to define the maximum and minimum framerate which should be visible. This is a disadvantage because the framerate can vary a lot depending on the complexity of the rendered geometry and the complexity of the shader which render the geometry. So when rotating the camera the number of pixel which got hit may vary a lot, so you can have huge jumps within your texture (see figure1 a).
2. The next disadvantage is that when you render very complex geometries where your framerate drops a lot, your framerate will show the minimum framerate. But the framerate will not be the same. Lets say it changes from 0.01 frames per second to 0.4 frames per second. So each value within this range will be stored at `min framerate` but the factor from the minimum to the maximum is 40! (see figure1 b)
3. When rendering only a small triangle, your framerate can increase and be outside the scale of your texture. Therefor it will not be visible inside your texture (see figure1 c).

[![enter image description here](https://i.sstatic.net/JHHEA.png)](https://i.sstatic.net/JHHEA.png)

**Figure1:** Displaying the disadvantages of storing the framerate directly to a texture.

**Example storing framerate to a buffer:**

First create a buffer on GPU where the framerates will be stored to and use it as ring buffer. Create a shader to visualize the histogram by passing an integer value as current position of the buffer and use modulo operator to find the correct position in the buffer when rendering with your pixel shader. This approch will have the following benefits:

1. The buffer is very small in contrast to a texture.
2. You only need to transfer 2 values (current position in buffer, new framerate value).
3. You are not restricted to a minimum or maximum framerate which can be displayed.
4. By passing the additional variables `minFramerate` and `maxFramerate` you can scale the histogram. So that in the example case b) will show you the big differences between 0.01 frames per second and 0.4 frames per second. The framerate also can not be outside of the maximal showable framerate (example case c).
5. you can draw lines from one value to the next value, so there will be no gabs in between.
6. you can change the shader from drawing connected lines to boxes if you like.


---

