<p>In order for the GPU to work with textures through samplers, the driver needs to know the textures dimensions, and a good implementation will be able to access those dimension much more easily/quickly then passing them around in buffers or as push constants. If the shader is already accessing the texture then those values should be sitting in the cache or a register.</p>
<p>Based on that I would always use the shader function and would only change to another method if profiling data showed otherwise. (It is always best to profile, blanket statements about performance are almost always wrong for some implementation)</p>

CC BY-SA 4.0
2022-06-18T10:47:48.110Z
2022-06-18T10:47:48.110Z
pmw1234
<p>So I need to have a texture size for my compute shader.  I found this function in GLSL to query this inside the shader:</p>
<pre><code>ivec2 textureSize = imageSize(outputImage)
</code></pre>
<p>My question is, is this less efficient than passing the texture size as a push constant for instance?</p>
<p>Also is there an increased cost if for instance I put this inside a loop and call it multiple times in the same shader invocation?</p>

<glsl><vulkan>
Is there a cost to querying a texture size inside a GLSL shader?
sak
# Is there a cost to querying a texture size inside a GLSL shader?

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

So I need to have a texture size for my compute shader. I found this function in GLSL to query this inside the shader:

```
ivec2 textureSize = imageSize(outputImage)

```

My question is, is this less efficient than passing the texture size as a push constant for instance?

Also is there an increased cost if for instance I put this inside a loop and call it multiple times in the same shader invocation?

**Tags:** glsl, vulkan


---

## 1 answers

### Answer by username_1 on 2022-06-18. Score: 3 | Accepted answer

In order for the GPU to work with textures through samplers, the driver needs to know the textures dimensions, and a good implementation will be able to access those dimension much more easily/quickly then passing them around in buffers or as push constants. If the shader is already accessing the texture then those values should be sitting in the cache or a register.

Based on that I would always use the shader function and would only change to another method if profiling data showed otherwise. (It is always best to profile, blanket statements about performance are almost always wrong for some implementation)


---

