<p>You can set up the VkImage as usual, then in your shaders you can use <a href="https://docs.gl/sl4/texelFetch" rel="nofollow noreferrer"><code>texelFetch</code></a> to load a specific texel at the coordinates you specify. It still takes a sampler parameter, but addressing modes are not applied and no filtering or automatic mipmapping is done.</p>
<p>Depending on what kind of data you stored in your image, you might also want to use an int or uint image format rather than the typical unorm, so that you'll get the exact bits you stored without any conversion.</p>

CC BY-SA 4.0
2022-04-24T02:16:34.497Z
unorm is correct.
Lily
CC BY-SA 4.0
2022-04-24T02:19:11.283Z
I want to see if I didn't align the image vertically, as I should have. I don't know how to do so though, since the image seems to have been loaded to CPU in the tiny_gltf module. I couldn't find the code that loades a texture in tiny_gltf or check if the loading method supports flipping an image.
Lily
CC BY-SA 4.0
2022-04-23T21:30:59.140Z
2022-04-23T21:30:59.140Z
Nathan Reed
<p>In Vulkan &quot;everything is a buffer&quot;. So the image can be loaded into a buffer and accessed directly. Copies to/from VkBuffer are detailed in section 18.4 of the Vulkan spec. Its fairly straight forward to do. Another way is to use VK_IMAGE_TILING_LINEAR when creating images. But linear tiling has many downsides, both have performance ramifications...</p>
<p>As a way to flip a texture vertically in the shader:</p>
<pre><code>float flipped_y = 1 - texcoord.y;
</code></pre>
<p>This changes texture coordinates in the range [0,1] to coords in the range [1,0]. (this is handy for debugging but should not be used generically)</p>
<p>Vulkan has the texture origin at the top left corner of the image, OpenGL has the texture origin at the bottom left corner. So images created for OpenGL can end up flipped vertically in Vulkan.</p>
<p>Vulkan doesn't do anything to change the actual values. It just handles them differently from OpenGL.</p>

CC BY-SA 4.0
2022-04-22T01:33:54.490Z
Thank you! That sound's valid advice. I think one of my problem for this image is that I loaded it upside down. Can I just handle it in the shaders? or does it matter that I have to load it in the right way in the first place? -- I think I need to flip it vertically.
Lily
CC BY-SA 4.0
2022-04-22T10:20:15.203Z
A quick/easy solution is just to negate the y texture coordinate. That will flip it vertically. But the issue may be coordinate system related(a common issue with folks new to vulkan), here is an article that should help you figure out if that is the case or not. https://anki3d.org/vulkan-coordinate-system/
pmw1234
CC BY-SA 4.0
2022-04-24T01:43:25.623Z
I already tried negating the y texture coordinates. Let me check the coordinate system. Thanks for the advice!
Lily
CC BY-SA 4.0
2022-04-24T02:01:21.650Z
May I ask directly if this new coordinate system only influences the position vector of the vertices or does it effects other values as well? like UV coordinates
Lily
CC BY-SA 4.0
2022-04-21T11:05:08.197Z
2022-04-25T11:09:11.733Z
2022-04-25T11:09:11.733Z
pmw1234
<blockquote>
<p>I wish to access the texels exactly as they are to decode the information I compressed in it.</p>
</blockquote>
<p>If it's not in an image format that Vulkan understands, then it's not an image as far as Vulkan is concerned. It's just a bunch of bytes.</p>
<p>You can access a bunch of bytes stored in device-accessible memory through a shader storage buffer.</p>

CC BY-SA 4.0
2022-04-21T21:40:51.527Z
2022-04-21T21:40:51.527Z
Nicol Bolas
<p>I am loading an image to shaders in Vulkan, but do not wish to use it as a texture. I wish to access the texels exactly as they are to decode the information I compressed in it. How should I do it? More specifically, I do not wish to use the filtering, mipmaping, address modes, and so on that usually accompanies a texture. I just want to load an image.</p>
<p>New to Vulkan and computer graphics, thanks for anyone's help.</p>

<image-processing>
loading images not through sampler
Lily
# loading images not through sampler

(Asked by: username_0 on 2022-04-21)

I am loading an image to shaders in Vulkan, but do not wish to use it as a texture. I wish to access the texels exactly as they are to decode the information I compressed in it. How should I do it? More specifically, I do not wish to use the filtering, mipmaping, address modes, and so on that usually accompanies a texture. I just want to load an image.

New to Vulkan and computer graphics, thanks for anyone's help.

**Tags:** image-processing


---

## 3 answers

### Answer by username_1 on 2022-04-23. Score: 1 | Accepted answer

You can set up the VkImage as usual, then in your shaders you can use [`texelFetch`](https://docs.gl/sl4/texelFetch) to load a specific texel at the coordinates you specify. It still takes a sampler parameter, but addressing modes are not applied and no filtering or automatic mipmapping is done.

Depending on what kind of data you stored in your image, you might also want to use an int or uint image format rather than the typical unorm, so that you'll get the exact bits you stored without any conversion.

> **Comments:**
> **username_0**: unorm is correct.  
> **username_0**: I want to see if I didn't align the image vertically, as I should have. I don't know how to do so though, since the image seems to have been loaded to CPU in the tiny_gltf module. I couldn't find the code that loades a texture in tiny_gltf or check if the loading method supports flipping an image.  

---

### Answer by username_2 on 2022-04-21. Score: 1

In Vulkan "everything is a buffer". So the image can be loaded into a buffer and accessed directly. Copies to/from VkBuffer are detailed in section 18.4 of the Vulkan spec. Its fairly straight forward to do. Another way is to use VK\_IMAGE\_TILING\_LINEAR when creating images. But linear tiling has many downsides, both have performance ramifications...

As a way to flip a texture vertically in the shader:

```
float flipped_y = 1 - texcoord.y;

```

This changes texture coordinates in the range [0,1] to coords in the range [1,0]. (this is handy for debugging but should not be used generically)

Vulkan has the texture origin at the top left corner of the image, OpenGL has the texture origin at the bottom left corner. So images created for OpenGL can end up flipped vertically in Vulkan.

Vulkan doesn't do anything to change the actual values. It just handles them differently from OpenGL.

> **Comments:**
> **username_0**: Thank you! That sound's valid advice. I think one of my problem for this image is that I loaded it upside down. Can I just handle it in the shaders? or does it matter that I have to load it in the right way in the first place? -- I think I need to flip it vertically.  
> **username_2**: A quick/easy solution is just to negate the y texture coordinate. That will flip it vertically. But the issue may be coordinate system related(a common issue with folks new to vulkan), here is an article that should help you figure out if that is the case or not. https://anki3d.org/vulkan-coordinate-system/  
> **username_0**: I already tried negating the y texture coordinates. Let me check the coordinate system. Thanks for the advice!  
> **username_0**: May I ask directly if this new coordinate system only influences the position vector of the vertices or does it effects other values as well? like UV coordinates  

---

### Answer by username_3 on 2022-04-21. Score: 0

> I wish to access the texels exactly as they are to decode the information I compressed in it.

If it's not in an image format that Vulkan understands, then it's not an image as far as Vulkan is concerned. It's just a bunch of bytes.

You can access a bunch of bytes stored in device-accessible memory through a shader storage buffer.


---

