<p>Look at this vertex shader:</p>

<pre><code>#version 450

layout(location = 0) in vec3 inPos;

out gl_PerVertex {
    vec4 gl_Position;
};


layout (std430, binding = 1) buffer depth_range_t {
    int min;
    int max;
} depth_range;

vec3 project(vec4 pt)
{
// somehow projects to (camera_x, camera_y, distance_to_camera)
}

void main()
{
    vec4 translated = ubo.worldToCamera * vec4(inPos.xyz, 1.0);
    vec3 projected = project(vec4(inPos.xyz, 1.0));

    atomicMin(depth_range.min, int(projected.z));
    atomicMax(depth_range.max, int(projected.z));
}
</code></pre>

<p>As you see, it uses two atomic operations, which occur to be embarrassingly slow. Using compute shaders I'd be able to utilize hierarchical architecture of a GPU by atomic operations on shared memory, but this in turn, as I see, would introduce hassle with separate writable buffer for vertices.</p>

<p><strong>Is there any way to leverage device caches directly in vertex shader?</strong></p>

<p>I'm especially interested to find if it's possible using Vulkan API (maybe, SPIR-V tricks?), but would also appreciate any information about possible solutions using other popluar graphics API. </p>

CC BY-SA 4.0
2019-07-12T17:33:08.110Z
"*Is there any way to leverage device caches directly in vertex shader?*" Leverage them to do... what? I mean sure, it's obvious that your shader is attempting to compute the minimum and maximum values for the depth of the vertices in the rendering operation(s), but how exactly would "device caches" help here?
Nicol Bolas
CC BY-SA 4.0
2019-07-15T12:33:11.530Z
@NicolBolas, from my understanding, atomics on local memory are meant to be faster, so I hoped to just use them.
Vladimir Nazarenko
<glsl><vulkan>
Communication between vertex shader executions
Vladimir Nazarenko
# Communication between vertex shader executions

(Asked by: username_0 on 2019-07-11)

Look at this vertex shader:

```
#version 450

layout(location = 0) in vec3 inPos;

out gl_PerVertex {
    vec4 gl_Position;
};

layout (std430, binding = 1) buffer depth_range_t {
    int min;
    int max;
} depth_range;

vec3 project(vec4 pt)
{
// somehow projects to (camera_x, camera_y, distance_to_camera)
}

void main()
{
    vec4 translated = ubo.worldToCamera * vec4(inPos.xyz, 1.0);
    vec3 projected = project(vec4(inPos.xyz, 1.0));

    atomicMin(depth_range.min, int(projected.z));
    atomicMax(depth_range.max, int(projected.z));
}

```

As you see, it uses two atomic operations, which occur to be embarrassingly slow. Using compute shaders I'd be able to utilize hierarchical architecture of a GPU by atomic operations on shared memory, but this in turn, as I see, would introduce hassle with separate writable buffer for vertices.

**Is there any way to leverage device caches directly in vertex shader?**

I'm especially interested to find if it's possible using Vulkan API (maybe, SPIR-V tricks?), but would also appreciate any information about possible solutions using other popluar graphics API.

**Tags:** glsl, vulkan

> **Comments:**
> **username_1**: "*Is there any way to leverage device caches directly in vertex shader?*" Leverage them to do... what? I mean sure, it's obvious that your shader is attempting to compute the minimum and maximum values for the depth of the vertices in the rendering operation(s), but how exactly would "device caches" help here?  
> **username_0**: @username_1, from my understanding, atomics on local memory are meant to be faster, so I hoped to just use them.  

---

## 0 answers

