kakashidinho / metalangle

MetalANGLE: OpenGL ES to Metal API translation layer
Other
461 stars 65 forks source link

Why don't multiple contexts of the same group share FBOs ? #82

Open wxdut opened 2 years ago

wxdut commented 2 years ago

Hi, Le Hoang Quyen, thank you so much for developing MetalANGLE.

I recently used MetalANGLE in my project, found that multiple egl contexts of the same group don't share FBOs, this is inconsistent with standard OpenGLES.

look at this snippet please:

    MGLSharegroup *shareGroup = [[MGLSharegroup alloc] init];
    MGLContext *glCtx1 = [[MGLContext alloc] initWithAPI:kMGLRenderingAPIOpenGLES2 sharegroup:shareGroup];
    MGLContext *glCtx2 = [[MGLContext alloc] initWithAPI:kMGLRenderingAPIOpenGLES2 sharegroup:shareGroup];

    [MGLContext setCurrentContext:glCtx1];
    GLuint fbo1 = 0;
    glGenFramebuffers(1, &fbo1);

    [MGLContext setCurrentContext:glCtx2];
    GLuint fbo2 = 0;
    glGenFramebuffers(1, &fbo2);

    assert(fbo1 + 1 == fbo2);

If you execute this code with MetalANGLE, you'll see that fbo1 and fbo2 are both 1, which is not in line with expectations. If you execute same code with standard OpenGLES, you'll see that fbo1 is 1 and fbo2 are 2.

I read the source code and found some clues here. the 'mFramebufferManager' isn't shared(not using AllocateOrGetSharedResourceManager).

image

Is this a bug or a feature? If it's a bug, I'd be happy to submit a PR to fix it. If it is a feature, could you please explain the reason.

Thank you again!!

kakashidinho commented 2 years ago

Hi, Fbo is not resource. It is a container object. Hence not shareable. Same as VAO. Pls see https://www.khronos.org/opengl/wiki/OpenGL_Context

Most OpenGL objects are sharable, including Sync Objects and GLSL Objects. Container Objects are not sharable, nor are Query Objects.

you can share textures then reattach them to two different fbos in two contexts though.

wxdut commented 2 years ago

Thanks for your quick reply.

But I test this code on iphone, fbos seem to be shared in same group.

    EAGLContext *glCtx1 = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    EAGLContext *glCtx2 = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:glCtx1.sharegroup];

    [EAGLContext setCurrentContext:glCtx1];
    GLuint fbo1 = 0;
    glGenFramebuffers(1, &fbo1);

    [EAGLContext setCurrentContext:glCtx2];
    GLuint fbo2 = 0;
    glGenFramebuffers(1, &fbo2);

    assert(fbo1 + 1 == fbo2); // pass
image

Do you know the reason? Thanks.


Update:

I think I figured it out.

I looked up Apple's documentation, FBOs are shared between contexts of the same group. Apple doesn't seem to follow khronos' specifications exactly.

image

In addition, I also refer to the stackoverflow discussion, there does not have this inconsistency problem on Android platform.