PDA

View Full Version : How to query sample count of RBO



remdul
05-01-2015, 07:03 AM
Is there a way to query the number of samples of a RBO, that is, the number passed as the second argument to glRenderbufferStorageMultisample ?

IonutCava
05-01-2015, 08:02 AM
I assume the RBO is used by a FBO, so maybe a glGet with GL_SAMPLES with the FBO bound might work?

L.E.: @remdul, you are correct, sorry. Haven't used RBOs in a while. The docs say that it should return the sample count for the currently BOUND (https://www.khronos.org/opengles/sdk/docs/man/xhtml/glGet.xml) framebuffer but also, as I found out later, that user created framebuffers would return 0 (https://www.khronos.org/opengles/sdk/docs/man/xhtml/glBindFramebuffer.xml).

Alfonse Reinheart
05-01-2015, 08:11 AM
Use glGetRenderbufferParameter (https://www.opengl.org/wiki/GLAPI/glGetRenderbufferParameter). Though really, there's no reason to do so, unless you're using a renderbuffer that you didn't actually create. Because if you created it, you should know how many samples it was created with.

arekkusu
05-01-2015, 09:59 AM
there's no reason to do so
If you ask for three samples, it might be created with four.

remdul
05-01-2015, 10:22 AM
@IonutCava: AFAIK that only works for the FB of the GL context. glGet(GL_SAMPLES, ...) doesn't seem to work on bound RBO/FBOs.

@Alfonse Reinheart: thanks! See comment by arekkusu as to why. IIRC I've seen sample counts clamped to GL_MAX_SAMPLES, and thus one may not be sure of the exact RBO sample count. Although one should ideally query the MAX_SAMPLES before setting up the RBO, I also prefer to query the RBO afterwards to be sure. It is important to know the exact sample count to calculate the coverage ratio when using occlusion queries.

Alfonse Reinheart
05-01-2015, 10:40 AM
It is important to know the exact sample count to calculate the coverage ratio when using occlusion queries.

That only makes me more curious: what are you doing with occlusion queries that you need an accurate count of passed samples?

Also, the specification on such things is... kinda wishy-washy. The spec allows implementations to count either just the samples in a pixel covered by the primitive, or all the samples in a pixel even if the primitive only covers one sample in that pixel. So it would be impossible to get a truly accurate sample count.

remdul
05-01-2015, 02:01 PM
That only makes me more curious: what are you doing with occlusion queries that you need an accurate count of passed samples?

For example when rendering a glow over sun disk in the sky, and gradually fading it when obscured. With MSAA enabled, the occlusion query result will be multiplied by the number of samples, which is why I need to be certain about the number of samples per pixel. I pre-calculate the number of pixels of the rasterized occludee on the CPU (trivial for 2d primitive shapes), and divide the occlusion result by that to get the ratio, which is then used to blend/scale the glow sprite. Much more elegant than a ray-test with temporal fade, and it works with alpha-tested and alpha-to-coverage surfaces. :)

A little conformance test (e.g. perform occlusion query on one 1px GL_POINTS) should reveal what convention the driver uses. I previously avoided that by rendering the occludee twice, once right after clearing the depth buffer to obtain the unobscured pixel count, and again later to get for the occluded pixel count, but I'd like to avoid unnecessary potential pipeline stalls (and messy code) if possible.

Well, it would have been nice if the spec was more strict and the numbers actually meant something.

IonutCava
05-02-2015, 01:19 AM
IIRC I've seen sample counts clamped to GL_MAX_SAMPLES, and thus one may not be sure of the exact RBO sample count.
That should throw an GL_INVALID_VALUE (https://www.opengl.org/sdk/docs/man/html/glRenderbufferStorageMultisample.xhtml) error (and an GL_INVALID_OPERATION if it's not supported by the format) upon creating the storage.

I'm actually curios about arekkusu's comment. In what situations does rounding occur and wouldn't it round down if it happened?

Alfonse Reinheart
05-02-2015, 08:17 AM
I'm actually curios about arekkusu's comment. In what situations does rounding occur and wouldn't it round down if it happened?

It's not really "rounding" so much as "that's what the implementation supports". Some multisample implementations only handle powers-of-two sample counts. Also, note that you can just ask the implementation what sample counts it supports (https://www.opengl.org/wiki/Image_Format#Image_format_queries), for a particular image format/texture target.

IonutCava
05-03-2015, 03:23 AM
It's not really "rounding" so much as "that's what the implementation supports". Some multisample implementations only handle powers-of-two sample counts. Also, note that you can just ask the implementation what sample counts it supports (https://www.opengl.org/wiki/Image_Format#Image_format_queries), for a particular image format/texture target.

Yes, but specifying an unsupported value for a specific format would surely throw an error, right? Multisampling seems solid enough to at least tell you when you specify an invalid count. Or would that make too much sense to actually implement? :)

Alfonse Reinheart
05-03-2015, 08:12 AM
Yes, but specifying an unsupported value for a specific format would surely throw an error, right?

No. If you ask for 5 samples, there's nothing necessarily wrong with giving you 8 instead. The only places where an exact sample count is needed is when your shader is directly interacting with the samples in an image. Either by reading/modifying the fragment's output sample mask or when you're from a multisample image in a shader.