<p>It depends on what you’re doing. If any given thread of your shader is working with data from multiple nearby pixels, like by sampling from fractional between-pixel locations or doing some calculation involving a pixel’s immediate neighbors, then you should probably use a texture: the GPU can do some caching and even load multiple values in a single operation in that case. If your threads are more independent and working on individual values, an array is fine.</p>

CC BY-SA 4.0
2019-03-22T22:06:06.277Z
In my case i'm not using adjacent pixels, but i do a lot texture sampling, im curious if a texture sample is more expensive than just access the float array.
wduk
CC BY-SA 4.0
2019-03-23T00:00:37.257Z
If you’re using texture filtering—i.e. reading from somewhere other than the center of any given pixel—then using the actual texture-sampling hardware for the interpolation will be faster than reading individual values and interpolating them yourself. Otherwise, I’d use an array.
Noah Witherspoon
CC BY-SA 4.0
2019-04-23T02:36:50.980Z
Textures are optimised for accessing neighbouring pixels and are usually encoded in some space filling curve like Morton order. If you are accessing the array in a linear fashion then this optimisation will lower cache efficiency.
PaulHK
CC BY-SA 4.0
2020-07-12T02:32:50.933Z
Just a follow up but by neighbours do you means pixels directly next to each other? What if you want to sample say [x,y] and [x+5,y-5] or something further away? Would arrays then be a wiser choice?
wduk
CC BY-SA 4.0
2020-07-13T19:57:43.757Z
In general the farther away your samples are the less benefit you’ll get from the spatial encoding. Within 5px I would guess a texture would still be optimal; 50px, probably not.
Noah Witherspoon
CC BY-SA 4.0
2019-03-22T19:01:46.110Z
2019-03-22T19:01:46.110Z
Noah Witherspoon
<p>I am wondering is there a difference in performance between using floating point textures as my 2D float4 array vs just passing in 2D float4 arrays? </p>

<p>2D float arrays eliminate the need to do texture look ups to get the colour value, but i am not sure if perhaps textures have some kinda performance boost on GPUs that an array would not due to what ever clever optimisations GPUs can do.</p>

<performance><compute-shader><optimisation><data-structure>
Compute shader textures compared 2D float4 arrays
wduk
# Compute shader textures compared 2D float4 arrays

(Asked by: username_0 on 2019-03-21)

I am wondering is there a difference in performance between using floating point textures as my 2D float4 array vs just passing in 2D float4 arrays?

2D float arrays eliminate the need to do texture look ups to get the colour value, but i am not sure if perhaps textures have some kinda performance boost on GPUs that an array would not due to what ever clever optimisations GPUs can do.

**Tags:** performance, compute-shader, optimisation, data-structure


---

## 1 answers

### Answer by username_1 on 2019-03-22. Score: 1 | Accepted answer

It depends on what you’re doing. If any given thread of your shader is working with data from multiple nearby pixels, like by sampling from fractional between-pixel locations or doing some calculation involving a pixel’s immediate neighbors, then you should probably use a texture: the GPU can do some caching and even load multiple values in a single operation in that case. If your threads are more independent and working on individual values, an array is fine.

> **Comments:**
> **username_0**: In my case i'm not using adjacent pixels, but i do a lot texture sampling, im curious if a texture sample is more expensive than just access the float array.  
> **username_1**: If you’re using texture filtering—i.e. reading from somewhere other than the center of any given pixel—then using the actual texture-sampling hardware for the interpolation will be faster than reading individual values and interpolating them yourself. Otherwise, I’d use an array.  
> **username_2**: Textures are optimised for accessing neighbouring pixels and are usually encoded in some space filling curve like Morton order. If you are accessing the array in a linear fashion then this optimisation will lower cache efficiency.  
> **username_0**: Just a follow up but by neighbours do you means pixels directly next to each other? What if you want to sample say [x,y] and [x+5,y-5] or something further away? Would arrays then be a wiser choice?  
> **username_1**: In general the farther away your samples are the less benefit you’ll get from the spatial encoding. Within 5px I would guess a texture would still be optimal; 50px, probably not.  

---

