Search code examples
c++openglframebuffer

Render to targets. Some have MSAA, some don't


I have 6 textures. Some were initialized via :-

void glTexImage2DMultisample(   GLenum target,
    GLsizei samples,
    GLenum internalformat,
    GLsizei width,
    GLsizei height,
    GLboolean fixedsamplelocations);

Some (e.g. for stroke / defer shading / special effect) were initialized via :-

glTexImage2D( ... );

(Main question) Is it possible to render to all of them at once? i.e. How to mix MSAA types of render target?

I have tried that, but I got error 36182.
From Multi-sampling Frame Render Objects and Depth Buffers . The error means :-

GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE is also returned if the value of GL_TEXTURE_FIXED_SAMPLE_LOCATIONS is not the same for all attached textures; or, if the attached images are a mix of renderbuffers and textures, the value of GL_TEXTURE_FIXED_SAMPLE_LOCATIONS is not GL_TRUE for all attached textures.

Perhaps, such operation is not allowed? I have searched for some explanation :-

  1. From https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexImage2DMultisample.xhtml :-

fixedsamplelocations = Specifies whether the image will use identical sample locations and the same number of samples for all texels in the image, and the sample locations will not depend on the internal format or size of the image. ,

  1. https://learnopengl.com/Advanced-OpenGL/Anti-Aliasing says that :-

If the last argument (fixedsamplelocations) is set to GL_TRUE, the image will use identical sample locations and the same number of subsamples for each texel.

^ Nevertheless, there are a few of technical words that I don't know.

  • What is a sample location? Does the sample locations mean glLocation in glsl shader?
    I guess it is the same as GLsizei samples, e.g. mostly 1 or 4. If it is 4, 1 pixel =4 samples.
  • Does the internal format means somethings like GL_[components​][size​][type​]?
  • Why the sample locations is related to internal format?
  • Thus I don't understand what fixedsamplelocations actually means.
    Many examples use GL_TRUE. When should it be GL_FALSE?

Solution

  • In a single FBO, images are either all multisampled or all not multisampled. To do otherwise (even if the MS images have a sample count of 1) will lead to a framebuffer completeness error. Not only must all images either be multisampled or non-multisampled, all images must also have the same sample count (the sample count of non-multisampled images is 0).

    So what you want is not permitted.


    The stuff on fixed-sample locations is a completely separate question.

    Multisample images of a particular sample count have the same number of samples in each pixel. However, when rendering for anti-aliasing purposes, it is a good idea to vary where those samples are in terms of the pixel's area of the screen. This improves the quality of the anti-alising produced by multisampling, as it minimizes any coherency between adjacent sample patterns.

    If you turn on fixed sample locations, you're forcing the implementation to not do this. So you will get reduced anti-aliasing quality. You would only need to turn this on if you have some shader computation that relies on a consistent location for the samples. This would typically be because you're doing the multisample resolve manually (rather than with a blit).