2

I have an app that may run either with or without multisampling. I am fetching samples from previously rendered multisample textures in a shader. Is it necessary to compile different versions of the shader, either with a sampler2D or a sampler2DMS to access the texture, depending on whether it is a multisample texture or not?

I tried to fetch from a non-multisampled texture through a sampler2DMS and it works, but I don't know if this is undefined behaviour according to the specification, or how much I can rely on this to work.

Does anyone know whether this is propperly defined behaviour?

2 Answers 2

2

Every texture object you create has target associated with it. When you first bound that object to the context, you passed glBindTexture a specific target enum. GL_TEXTURE_2D, GL_TEXTURE_CUBE_MAP, etc. The target you use in your first bind call becomes inextricably linked to the texture object; if you try to bind it with a different target, glBindTexture will fail.

GL_TEXTURE_2D and GL_TEXTURE_2D_MULTISAMPLE are different targets. If you bind a newly created texture object with glBindTexture(GL_TEXTURE_2D, obj), you cannot later bind it with GL_TEXTURE_2D_MULTISAMPLE. And vice-versa.

GLSL samplers types, as you may have noticed, have suffixes on them. Well, those suffixes denote the type of texture that the particular sampler type works with. sampler2D is a floating-point sampler that works with GL_TEXTURE_2D textures. It will therefore only find a texture with the corresponding target.

If you set a sampler2D uniform to use texture unit 0, and bind somethign to GL_TEXTURE_2D_MULTISAMPLE, that sampler will not see it. It will only look for a GL_TEXTURE_2D bound to that sampler.

Now, you may have heard of view textures. These are textures which share storage with other textures. But the view can have different dimensions and even texture targets. You can view a single layer from a 2D array texture as a 2D texture. And so forth.

Among the conversions that you cannot do is view a multisample texture as a non-multisample texture. Or vice-versa.

So if what you described worked, it was undefined behavior. And you cannot rely on it.

That being said:

Is it necessary to compile different versions of the shader

No; you just can't use one variable. It's perfectly valid to have two sampler variables of different types and conditionally decide to sample one vs. the other. If the condition is dynamically uniform (for GL 4.x), you won't even lose your implicit derivatives

The only thing you have to do is make them use different texture units. Don't try to play games where you assign them the same texture unit and have it decide which to use based on what you bound. It doesn't work (or if it does, it's UB).

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

1 Comment

Thank you for your extensive answer. Do you happen to know if uniform branching in the fragment shader is as performant as compiling two different shaders? (considering that only one version will be used during program execution, so no additional program binds)
0

I tested this and like Nicol said, it seems what you bind to GL_TEXTURE_2D_MULTISAMPLE isn't going to be seen at a uniform that is looking at that texture slot

It seems the only way around this is to blit the MSAA FBO to the screen, or to another (non-multisampled) texture (then you could do PP operations on that regular texture)

To blit an FBO:

glBindFramebuffer( GL_READ_FRAMEBUFFER, srcFBO );

// If destFBO is 0, you blit to the screen
glBindFramebuffer( GL_DRAW_FRAMEBUFFER, destFBO );

glBlitFramebuffer( 0, 0, wSrc, hSrc,   0, 0, wDst, hDst, GL_COLOR_BUFFER_BIT, GL_LINEAR );

// Restore original states just in case
glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 );
glBindFramebuffer( GL_DRAW_FRAMEBUFFER, 0 );

Comments

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.