0

I want to store normals and tangents given to my shader as 8 byte integers (x, y, z) and then in the shader unpack them to a normalised float. Given that -128 should map to -1.f and 127 should map to +1.f, what do I divide by to get the right normalised float value? I hadn't thought of this before but it seems as if I have to divide it by a different number based on whether the value is negative. I could store the values in the integer from 0-255, which would make the decoding easier, just divide by 255 and then map from -1 to +1, but I'd like to know how it's done if the values are from -128 to +127. This is what glsl does I believe when it reads a format as SNORM.

Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • "*in the shader unpack them to a normalised float*" Can you just use `SNORM` vertex attributes for that? Or are you trying to avoid those? – Nicol Bolas Oct 22 '21 at 17:57
  • Also, take note of how Vulkan (and GLSL) treats the range of signed-normalized integers. It's not [-128, 127]-> [-1,1]. – Nicol Bolas Oct 22 '21 at 17:59
  • @NicolBolas I wanted to have ivec2 with the normal in the first three bytes of ivec2.x, and then the tangent.x in the last bits of ivec2.x, and 16 bits of ivec2.y containing the rest of the tangent, which would leave me 16 bits in ivec2.y for other stuff. I thought the smallest type in glsl was 4 bytes, so I could only have an int as an attribute. If I use R8G8B8A8 as attribute input format, then what type do I declare as 'in'? – Zebrafish Oct 22 '21 at 18:10
  • Normalized values are just a way of [encoding a floating-point value](https://www.khronos.org/opengl/wiki/Normalized_Integer). So the type of the `in` variable would be a floating-point type: `vec4`. For both. How you use those 8 values is up to you. – Nicol Bolas Oct 22 '21 at 19:12
  • @NicolBolas So host side when I encode the normals that are -1.f to +1.f I just multiply them by 127, giving -127 to +127, glsl-side then decodes this correctly back to -1.f to +1.f? On the Ogre3D website the author doesn't do a simple multiply, but adds a 0.5 and clamps. I don't understand what that's for, a simple multiply by 127 should do it no? – Zebrafish Oct 22 '21 at 19:23
  • I can't speak for what a random website says to do. The Vulkan standard outlines how normalization works. Maybe they're storing the values in an unsigned byte in C++ that will be interpreted as signed by GLSL/Vulkan. – Nicol Bolas Oct 22 '21 at 19:28

0 Answers0