<p>You should take a look at the layout qualifier identifiers (see <a href="https://registry.khronos.org/OpenGL-Refpages/gl4/html/gl_FragCoord.xhtml" rel="nofollow noreferrer">here</a>). The usual range is [0.5, 1023.5] for a viewport size of 1024, So it is the Pixel center. But when casting it to ivecX to hand it over to the texelfetch command the Pixel positions is correct. Texelfetch returns you the raw data, without filtering, because the input position is in integer form.</p>
<p>But you need other states to be set correctly like viewport size, depth test, stancil test, blend mode, culling...</p>
<p>There is the third parameter missing at the texelfetch command in your shader. It is the level of detail (mipmap level) this should be 0.</p>

CC BY-SA 4.0
2023-05-09T21:26:19.640Z
I assumed it would be vanilla 2D textures....
Chifti Saidi
CC BY-SA 4.0
2023-05-10T07:18:34.557Z
Yes, it works for 2D textures.... Your texelFetch position would then be an ivec2 datatype
Thomas
CC BY-SA 4.0
2023-05-10T07:20:08.123Z
Or was it an indication of the viewport range?
Thomas
CC BY-SA 4.0
2023-05-12T22:59:57.403Z
Yes, it seems to work...
Chifti Saidi
CC BY-SA 4.0
2023-05-09T19:52:25.220Z
2023-05-10T05:55:18.743Z
2023-05-10T05:55:18.743Z
Thomas
<p>Imagine I have two uncompressed, 2D textures A and B with identical sizes and formats. I bind A to texture unit 0, and B to a framebuffer object.</p>
<p>If I draw a full-size polygon in that framebuffer with this pixel shader:</p>
<pre><code>uniform sampler2D iChannel0;
out vec4 fragColor;
void main()
{
   fragColor = texelFetch(iChannel0, gl_FragCoord.xy);
}
</code></pre>
<p>Is this guaranteed to make a perfect copy of texture A?</p>
<p>ie. Will gl_FragCoord.xy give good numbers that map every input texel to every output texel? Will there be any filtering or clamping applied to the texels?</p>
<p>Edit: It should be</p>
<pre><code>fragColor = texelFetch(iChannel0, ivec2(gl_FragCoord.xy), 0);
</code></pre>

<opengl><texture>
Can I copy one texture to another perfectly with texelFetch()
Chifti Saidi
# Can I copy one texture to another perfectly with texelFetch()

(Asked by: username_0 on 2023-05-08)

Imagine I have two uncompressed, 2D textures A and B with identical sizes and formats. I bind A to texture unit 0, and B to a framebuffer object.

If I draw a full-size polygon in that framebuffer with this pixel shader:

```
uniform sampler2D iChannel0;
out vec4 fragColor;
void main()
{
   fragColor = texelFetch(iChannel0, gl_FragCoord.xy);
}

```

Is this guaranteed to make a perfect copy of texture A?

ie. Will gl\_FragCoord.xy give good numbers that map every input texel to every output texel? Will there be any filtering or clamping applied to the texels?

Edit: It should be

```
fragColor = texelFetch(iChannel0, ivec2(gl_FragCoord.xy), 0);

```

**Tags:** opengl, texture


---

## 1 answers

### Answer by username_1 on 2023-05-09. Score: 1 | Accepted answer

You should take a look at the layout qualifier identifiers (see [here](https://registry.khronos.org/OpenGL-Refpages/gl4/html/gl_FragCoord.xhtml)). The usual range is [0.5, 1023.5] for a viewport size of 1024, So it is the Pixel center. But when casting it to ivecX to hand it over to the texelfetch command the Pixel positions is correct. Texelfetch returns you the raw data, without filtering, because the input position is in integer form.

But you need other states to be set correctly like viewport size, depth test, stancil test, blend mode, culling...

There is the third parameter missing at the texelfetch command in your shader. It is the level of detail (mipmap level) this should be 0.

> **Comments:**
> **username_0**: I assumed it would be vanilla 2D textures....  
> **username_1**: Yes, it works for 2D textures.... Your texelFetch position would then be an ivec2 datatype  
> **username_1**: Or was it an indication of the viewport range?  
> **username_0**: Yes, it seems to work...  

---

