The answer is spread across the following links. Due to size constraints / requirements it had to be a Buffer Texture.
Overview listing of possible technologies:
OpenGL reference to Buffer Texture:
Different question but the poster has a nice usage pattern provided:
OpenGL reference how to fetch pixels directly:
- however it will be called samplerBuffer in GLSL.
Details to my solution:
- Using OpenTK and C# BTW.
- I started up loading my Image (DICOM 12 bit) with as ushorts with the internal Format GL_R16 (one channel, normalized float). This is because I want to fetch the texture with interpolation and even mipmapping using texture() in GLSL.
- The Lookup table is declated as ushort with 64k items. Those are actual int values but for the conversion precesision between normalized and ushort it does not matter. The right values will map to the same bit patterns.
- The Buffer Texture is loaded off a Buffer of those ushort (length: 64k). At initialization of the Buffer Texture I use the internal format GL_R16 again. This implies some kind of 'conversion' (but bit patterns are equal) from my ushort to the buffer texture (noop).
- Probably GL_R16I would have worked, too. But At the end in GLSL I want to get a normalized float value in order to fill the final color attribute. So I went right into normalized upload.
My shader source:
uniform sampler2D tex;
uniform samplerBuffer lutData;
in vec2 fragTexCoord;
out vec4 finalColor;
float primaryLvl = texture(tex, fragTexCoord);
int index = int(primaryLvl * 65535);
float gray= texelFetch(lutData, index);
finalColor = vec4(gray, gray, gray, 1.0);
Fetch the texel from the image texture (normalized) so we have to convert to int index. Index in LUT buffer texture - get normalized float again. Use right away for pixel color.
var buf = GL.GenBuffers(1);
GL.BindBuffer( BufferTarget.TextureBuffer, buf);
var handle = GL.GenTexture();
GL.TexBuffer(TextureBufferTarget.TextureBuffer, SizedInternalFormat.R16, buf);
In the main loop you'll use the buffer texture just as a texture:
- Activate texture unit X
- Bind texture
- set uniform value to texture unit X