1
$\begingroup$

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.

$\endgroup$

3 Answers 3

1
$\begingroup$

You can set up the VkImage as usual, then in your shaders you can use 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.

$\endgroup$
2
  • $\begingroup$ unorm is correct. $\endgroup$
    – Lily
    Apr 24, 2022 at 2:16
  • $\begingroup$ 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. $\endgroup$
    – Lily
    Apr 24, 2022 at 2:19
1
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – Lily
    Apr 22, 2022 at 1:33
  • $\begingroup$ 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. anki3d.org/vulkan-coordinate-system $\endgroup$
    – pmw1234
    Apr 22, 2022 at 10:20
  • $\begingroup$ I already tried negating the y texture coordinates. Let me check the coordinate system. Thanks for the advice! $\endgroup$
    – Lily
    Apr 24, 2022 at 1:43
  • $\begingroup$ 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 $\endgroup$
    – Lily
    Apr 24, 2022 at 2:01
0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.