<p>Unfortunately, it seems as though passing images to functions is currently unsupported in GLSL (<a href="https://github.com/KhronosGroup/GLSL/issues/57" rel="nofollow noreferrer">https://github.com/KhronosGroup/GLSL/issues/57</a>). However, in your case you can work around this by simply using the image as global instead, as you have already loaded it in as such. So something like this might work</p>
<pre><code>...
layout(binding = 1, rg32f) uniform readonly image2D uVelocity;


vec2 loadVelocity(ivec2 index) {
    ... // Rename everything called velocity to uVelocity here
}


void main() {
    ...
    vec2 v = loadVelocity(coord);
    ...
}
<span class="math-container">```</span>
</code></pre>

CC BY-SA 4.0
2024-12-07T13:10:53.370Z
2024-12-07T13:10:53.370Z
myname 111
<p>I would like to declare a function which reads from an image2D and modifies the output.</p>
<p>My shader is written like so:</p>
<pre><code>...
layout(binding = 1, rg32f) uniform readonly image2D uVelocity;


vec2 loadVelocity(image2D velocity, ivec2 index) {
    ...
}


void main() {
    ...
    vec2 v = loadVelocity(uVelocity, coord);
    ...
}

</code></pre>
<p>When I try to compile this shader to SPIRV, I get the following errors:</p>
<blockquote>
<p>'readonly' : argument cannot drop memory qualifier when passed to formal parameter</p>
</blockquote>
<blockquote>
<p>'format' : image formats must match</p>
</blockquote>
<p>So it seems that the function declaration is not specific enough to allow this image to be used.</p>
<p>I have tried adding <code>readonly</code> to the function arg declaration, and this silences the first error, but how can I declare the format?</p>

CC BY-SA 4.0
2022-06-19T18:26:32.513Z
did you try adding layout(rg32f) to the function argument? There has been bugs around passing images as parameters and the spec (still?) contradicts itself.  So the full prototype would be something like vec2 loadVelocity( readonly layout(rg32f) image2D velocity...
pmw1234
CC BY-SA 4.0
2022-06-22T06:45:29.437Z
If I try this, I get the following error: cannot use layout qualifiers on a function parameter
sak
<glsl><vulkan>
Problem when passing a readonly image2D to a function
sak
# Problem when passing a readonly image2D to a function

(Asked by: username_0 on 2022-06-19)

I would like to declare a function which reads from an image2D and modifies the output.

My shader is written like so:

```
...
layout(binding = 1, rg32f) uniform readonly image2D uVelocity;

vec2 loadVelocity(image2D velocity, ivec2 index) {
    ...
}

void main() {
    ...
    vec2 v = loadVelocity(uVelocity, coord);
    ...
}

```

When I try to compile this shader to SPIRV, I get the following errors:

> 'readonly' : argument cannot drop memory qualifier when passed to formal parameter

> 'format' : image formats must match

So it seems that the function declaration is not specific enough to allow this image to be used.

I have tried adding `readonly` to the function arg declaration, and this silences the first error, but how can I declare the format?

**Tags:** glsl, vulkan

> **Comments:**
> **username_1**: did you try adding layout(rg32f) to the function argument? There has been bugs around passing images as parameters and the spec (still?) contradicts itself.  So the full prototype would be something like vec2 loadVelocity( readonly layout(rg32f) image2D velocity...  
> **username_0**: If I try this, I get the following error: cannot use layout qualifiers on a function parameter  

---

## 1 answers

### Answer by username_2 on 2024-12-07. Score: 0

Unfortunately, it seems as though passing images to functions is currently unsupported in GLSL (<https://github.com/KhronosGroup/GLSL/issues/57>). However, in your case you can work around this by simply using the image as global instead, as you have already loaded it in as such. So something like this might work

```
...
layout(binding = 1, rg32f) uniform readonly image2D uVelocity;

vec2 loadVelocity(ivec2 index) {
    ... // Rename everything called velocity to uVelocity here
}

void main() {
    ...
    vec2 v = loadVelocity(coord);
    ...
}
```

```


---

