如何在 Moderngl EGL 后端添加深度缓冲区?

How to add a depth buffer in Moderngl EGL backend?

当深度缓冲行 depth_attachment=ctx.depth_texture((512, 512), samples=8) 被注释时,此代码呈现带有抗锯齿 (samples=8) 的彩色三角形。

但是,当我添加深度缓冲区时,它 returns 在绑定 fbo_msaa 帧缓冲区到 GL_READ_FRAMEBUFFER 时出现 GL 错误。你知道如何解决这个错误吗?

import numpy as np
from PIL import Image
import moderngl
import OpenGL.GL as gl

ctx = moderngl.create_standalone_context(backend='egl')

fbo = ctx.framebuffer(
        color_attachments=ctx.texture((512, 512), 4, samples=0)
    )

fbo_msaa = ctx.framebuffer(
        color_attachments=ctx.texture((512, 512), 4, samples=8),
        depth_attachment=ctx.depth_texture((512, 512), samples=8)
    )
fbo_msaa.use()

vertices = np.array([
    -1.0,  -1.0,   1.0, 0.0, 0.0,
     1.0,  -1.0,   0.0, 1.0, 0.0,
     0.0,   1.0,   0.0, 0.0, 1.0],
    dtype='f4',
)

prog = ctx.program(vertex_shader="""
#version 330
in vec2 in_vert;
in vec3 in_color;
out vec3 color;
void main() {
    gl_Position = vec4(in_vert, 0.0, 1.0);
    color = in_color;
}
""",
fragment_shader="""
#version 330
out vec4 fragColor;
in vec3 color;
void main() {
    fragColor = vec4(color, 1.0);
}
""",
)
vao = ctx.simple_vertex_array(prog, ctx.buffer(vertices), 'in_vert', 'in_color')
vao.render(mode=moderngl.TRIANGLES)

gl.glBindFramebuffer(gl.GL_READ_FRAMEBUFFER, fbo_msaa.glo)
gl.glBindFramebuffer(gl.GL_DRAW_FRAMEBUFFER, fbo.glo)
gl.glBlitFramebuffer(0, 0, 512, 512, 0, 0, 512, 512, gl.GL_COLOR_BUFFER_BIT, gl.GL_LINEAR)

image = Image.frombytes('RGBA', (512, 512), fbo.read(components=4))
image = image.transpose(Image.FLIP_TOP_BOTTOM)
image.save('triangle.png', format='png')

GLError                                   Traceback (most recent call last)
<ipython-input-3-b59e4f46148e> in <module>()
     46 vao.render(mode=moderngl.TRIANGLES)
     47 
---> 48 gl.glBindFramebuffer(gl.GL_READ_FRAMEBUFFER, fbo_msaa.glo)
     49 gl.glBindFramebuffer(gl.GL_DRAW_FRAMEBUFFER, fbo.glo)
     50 gl.glBlitFramebuffer(0, 0, 512, 512, 0, 0, 512, 512, gl.GL_COLOR_BUFFER_BIT, gl.GL_LINEAR)

1 frames
/usr/local/lib/python3.6/dist-packages/OpenGL/platform/baseplatform.py in __call__(self, *args, **named)
    413     def __call__( self, *args, **named ):
    414         if self.load():
--> 415             return self( *args, **named )
    416         else:
    417             try:

/usr/local/lib/python3.6/dist-packages/OpenGL/error.py in glCheckError(self, result, baseOperation, cArguments, *args)
    232                         result,
    233                         cArguments = cArguments,
--> 234                         baseOperation = baseOperation,
    235                     )
    236                 return result

GLError: GLError(
    err = 1280,
    description = b'invalid enumerant',
    baseOperation = glBindFramebuffer,
    cArguments = (GL_READ_FRAMEBUFFER, 2)
)

当使用 Renderbuffer rather than a Texture 作为多重采样深度缓冲区时有效:

而不是

fbo_msaa = ctx.framebuffer(
       color_attachments=ctx.texture((512, 512), 4, samples=8),
       depth_attachment=ctx.depth_texture((512, 512), samples=8)
   )

fbo_msaa = ctx.framebuffer(
        color_attachments=ctx.texture((512, 512), 4, samples=8),
        depth_attachment=ctx.depth_renderbuffer((512, 512), samples=8)
    )

这似乎是 ModernGL 中的错误,与多采样纹理深度缓冲区附件有关。
此外错误 1280 (GL_INVALID_ENUM) 没有任何意义,GL_READ_FRAMEBUFFERglBindFramebuffer.

的有效参数