1

I'm using my alpha channel as an 8 bit integer index for something unrelated to blending so I want to carefully control the bit values. In particular, I need for all of the pixels from one FBO-rendered texture with a particular alpha value to match all of the pixels with the same alpha value in the shader. Experience has taught me to be careful when comparing floating point values for equality...

While setting the color values using the floating point vec4 might not cause me issues, and my understanding is that even a half precision 16bit float will be able to differentiate all 8 bit integer (0-255) values. But I would prefer to perform integer operations in the fragment shader so I am certain of the values.

Am I likely to incur a performance hit by performing integer ops in the fragment shader?

How is the output scaled? I read somewhere that it is valid to send integer vectors as color output for a fragment. But how is it scaled? If I send a uvec4 with integers 0-255 will it scale that appropriately? I'd like for it to directly write the integer value into the pixel format, for integer formats I don't want it to do any scaling. Perhaps for RGBA8 sending in an int value above 255 would clamp it to 255, and clamp negative ints to zero, and so on.

This issue is made difficult by the fact that I cannot debug by printing out the color values unless I grab the rendered images and examine them carefully. Perhaps I can draw a bright color if something fails to match.

Here is a relevant thread I found on this topic. It has confused me even more than before.

2
  • Note that this thread is from 2004. Since then a lot of development was done in the area of GPUs. Namely there's now support for integer (image) buffers and bitwise operations. So you are in the clear when using those. Commented Jan 14, 2012 at 18:00
  • Ah I hadn't noticed that. Thanks, that's certainly a very old discussion... So what should I do? Can I use integers all the way through the pipeline or do I still need to convert to float before setting my final fragment color? I'm just particularly interested in setting the alpha channel on my FBO color buffer to the specific integer value I specified in my color attribute with an unsigned byte. Commented Jan 14, 2012 at 18:10

1 Answer 1

1

I suggest not using the color attachment's alpha channel, but an additional render target with an explicit integer format. This is available since at least OpenGL-3.1 (the oldest spec I looked at, for this answer). See the OpenGL function glBindFragDataLocation, which binds a fragments shader out variable. In your case a int out $VARIABLENAME. For input into the next state use a integer sampler. I refer you to the specification of OpenGL-3.1 and GLSL-1.30 for the details.

Sign up to request clarification or add additional context in comments.

3 Comments

Do you think this would fail catastrophically on OpenGL 2.0/2.1 hardware? (The only hardware that can do 3.0+ are Sandy Bridge HD2/3000 and the last 3 or so generations from Nvidia/AMD)
@StevenLu: Since integer operation has been introduced with OpenGL-3 only, I'd say you're out of luck with OpenGL-2 hardware.
Okay. Let's see how well this works with the floating point equality check.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.