# How to accurately sample textures in ComputeShader?

**URL:** <https://forum.babylonjs.com/t/how-to-accurately-sample-textures-in-computeshader/53747>\
**Category:** Questions\
**Tags:** compute-shader\
**Created:** [September 25, 2024, 6:14pm UTC](https://forum.babylonjs.com/t/how-to-accurately-sample-textures-in-computeshader/53747 "2024-09-25T18:14:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric.J](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/eric.j/32/34502_2.png) [@Eric.J](https://forum.babylonjs.com/u/Eric.J)\
**Post date:** [September 25, 2024, 6:14pm UTC](https://forum.babylonjs.com/t/how-to-accurately-sample-textures-in-computeshader/53747/1 "2024-09-25T18:14:44Z")

</div>

I want to sample a texture in ComputeShader. But the result is always a little different from the original texture. I tried to use my custom TextureSampler and changed the sampling mode, but still could not get the right color. Please help! 🤣

> **[Babylon.js Playground](https://playground.babylonjs.com/#J6LTJ0%231)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL and WebGPU 3D scenes

This is the input texture.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/6/c/6cae58b92cef4002b1a327e81490debdc8036482.png)

This is the output texture.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/5/8/586d1e03979184eda961c3757b7b912dcd45faf1.png)

By the way, in the latest version of the Playground, I cannot open the Texture Inspector. Just change it to the version 6.49.0.

---

<div class="post-metadata">

**Author:** ![Eric.J](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/eric.j/32/34502_2.png) [@Eric.J](https://forum.babylonjs.com/u/Eric.J)\
**Post date:** [September 25, 2024, 6:19pm UTC](https://forum.babylonjs.com/t/how-to-accurately-sample-textures-in-computeshader/53747/2 "2024-09-25T18:19:13Z")

</div>

@Evgeni_Popov I’m sorry to bother you, but do you have a solution?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [September 25, 2024, 6:27pm UTC](https://forum.babylonjs.com/t/how-to-accurately-sample-textures-in-computeshader/53747/3 "2024-09-25T18:27:37Z")

</div>

You should sample at the center of the texel: `vec2<f32>(global_id.xy) / dims + vec2<f32>(0.5) / dims`.

> **[Babylon.js Playground](https://playground.babylonjs.com/#J6LTJ0%232)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL and WebGPU 3D scenes

But you will make your life a lot easier if you use `textureLoad` instead of `textureSampleLevel`: `let pix : vec4<f32> = textureLoad(src, vec2<u32>(global_id.xy), 0)` (you will have to remove all references to the sampler, though).

---

<div class="post-metadata">

**Author:** ![Eric.J](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/eric.j/32/34502_2.png) [@Eric.J](https://forum.babylonjs.com/u/Eric.J)\
**Post date:** [September 25, 2024, 6:31pm UTC](https://forum.babylonjs.com/t/how-to-accurately-sample-textures-in-computeshader/53747/4 "2024-09-25T18:31:07Z")

</div>

You are great 👍! I know where the problem lies. Sorry for disturbing you multiple times today.
