OpenGLES, how to use glTexStorage2DMultisample()?

I want implement off-screen Anti-aliasing.

First init Texture and FBO :

int[] textures = new int[1];
GLES31.glGenTextures(1,textures,0);
textureFBO.handle = textures[0];
GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D_MULTISAMPLE,textures[0]);
GLES31.glTexStorage2DMultisample(GLES31.GL_TEXTURE_2D_MULTISAMPLE,4, GLES31.GL_RGBA8, frameWidth, frameHeight, false);
             ... 
int[] frameBuffers = new int[1];
GLES31.glGenFramebuffers(1, frameBuffers,0);
GLES31.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0,GLES31.GL_TEXTURE_2D_MULTISAMPLE, textureFBO.handle, 0);
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D_MULTISAMPLE, 0);

Then render to FBO :

GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, frameBuffers[0]);
        ...
GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, glTextureOriginal.handle);
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);

Finally, sample multiTexture, render it to FBO0

GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
        ...
GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D_MULTISAMPLE, textureFBO.handle);
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);

But the render result is black.
If i replace glTexStorage2DMultisample() using glTexStorage2D(), it works fine.

There is no code you can write where changing the type of a texture without changing the shader reading that texture will just work. 2D textures are not interchangeable with 2D multisample textures. They have different GLSL sampler types, and you can’t even use the same functions in GLSL to access them, as you have to access a specific sample of a specific texel of the texture.

What you probably want to do is render to a multisample image and then resolve it down to a single-sample one, which your shader will then read from.

1 Like

Thanks very much , man.

I seems like read a blog about using sampler2D instead of sampler2DMS in OpenGLES, so i kept using sampler2D instead of sampler2DMS when read GL_TEXTURE_2D_MULTISAMPLE .

Now i have changed the second fragment shader to :

public static final String fragmentShaderMulti ="#version 310 es \n" +
            "precision highp float; \n" +
            "in vec2 vTexCoord;\n" +
            "uniform highp sampler2DMS sTexture;\n" +
            "out vec4 out_FragColor;\n" +
            "void main() {\n" +
            "    out_FragColor = texelFetch(sTexture, ivec2(int(vTexCoord.x * 1080.0), int(vTexCoord.y * 1080.0)), 0); \n" +
            "}";

This is vertex shader :

    public static final String vertexShader ="#version 310 es \n" +
                    "in vec4 aPosition;\n" +
                    "in vec2 aTexCoord;\n" +
                    "out vec2 vTexCoord;\n" +
                    "void main() {\n" +
                    "    vTexCoord=vec2(aTexCoord.x, 1.0 - aTexCoord.y);\n" +
                    "    gl_Position = aPosition;\n" +
                    "}";

There are no gl error report, but render result is black, maybe i need to understand:

now i need to have a sleep. thanks very much.

Works now, Thanks very much ! The most import is change sampler2D to sampler2DMS when sampling MultiTexture。