zavier
zavier

Reputation: 1

How can I get the number of samples each pixel owns inside a shader?

I'm trying to read each sample of a multi-sampled depth buffer as an storage image, which just generated by my pre-z pass.

However, here is two essential problems in front of me:

  1. How many samples are there in each pixel of the image?

    Of course, I can get this number in application because that was what I specified when multi-sampled depth buffer was created before.

    But the number isn't specified inside a shader. All I know is that the buffer is multi-sampled one by Image2DMS prez;

    Since the number of per texel samples is set dynamically, I cannot just declare it using a macro like #define SAMPLE_PER_TEXAL 16.

    I've looked through the glsl4.5 spec and not found any API which can get the number. Is getting that number in shader really possiable?

  2. How can an specified sample of the image be accessed ?

    An API loadImage(Image2DMS img, ivec2 P, int sample); may be seemingly helpful for me. However, the thrid parameter int sample, which called 'sample number' in the spec, is still not defined throughout the whole spec.

Upvotes: 0

Views: 431

Answers (2)

Nicol Bolas
Nicol Bolas

Reputation: 473667

OpenGL 4.5/ARB_shader_texture_image_samples allows you to query the number of samples in a texture from GLSL, through the function textureSamples. You can also pass it to the shader through a uniform.

The imageLoad, when passed a multisampled sampler type, will take an additional integer parameter that specifies which sample to read from. So the function for 2D multisample textures would look like this:

gvec imageLoad(gimage2DMS image, ivec2 P, int sample);

Upvotes: 3

Stefan Dragnev
Stefan Dragnev

Reputation: 14473

Even if there's no GLSL function that can give you the sample count, since you know it client-side, you can just pass it as a uniform to the shader.

For the second question - as per the Wiki:

When accessing multisample textures, the accessing function has another parameter, an int that defines the sample index to read from or write to.

So yes, using the loadImage(Image2DMS img, ivec2 P, int sample) overload should "just work" with sample = 0,1,2,... up to the number of samples in the image.

Upvotes: 0

Related Questions