how can i fetch a texel from a multisampled texture beyond the number of samples it has

970 views Asked by At

Suppose I created a multisampled texture with 8 samples.

And my fragment shader is

out vec4 color;
uniform sampler2DMS tex;
in vec2 txcoords;

void main()
{
    vec4 col;
    ivec2 txSize = ivec2(textureSize(tex)* txcoords);
    for(int i=0;i<8;i++)
        col += texelFetch(tex, txSize, i); 
    color = col/8;
}

This works fine. However if I replace the for loop above with

    for(int i=10050;i<10058;i++)

this still works. (By works, I mean i still get the right image)

Why? Shouldnt OpenGL have generated a invalid operation error.

Similarly, even though the texture has 8 samples, I can still fetch 9th sample, if i = 9? Doesnt sound right to me. The Fragment shader should have failed at run time.

1

There are 1 answers

0
Andon M. Coleman On BEST ANSWER

I should point out that ordinarily buffer accesses that are out-of-range have undefined behavior in OpenGL. However, with the recent push in OpenGL 4.x for improved robustness, there is an extension that will allow you to define the behavior in these cases.

GL_ARB_robust_buffer_access_behavior

This extension works by passing a new flag to your OpenGL context when you create it, and then buffer accesses that are out of range will result in well-defined values.

In your case, a texelFetch (...) that is out of range will return vec4 (0.0, 0.0, 0.0, 0.0)