多重采样帧缓冲不完整,我不知道为什么

时间:2014-07-20 01:35:52

标签: opengl opentk multisampling

这是我创建“Surface”的代码:

private void Create(int width, int height, SurfaceFormat format)
{
    bool multisample = format.Multisample > 1;
    if (multisample)
        GL.Enable(EnableCap.Multisample);
    int samples = Math.Max(1, Math.Min(format.Multisample, 4));
    TextureTarget target = multisample ? TextureTarget.Texture2DMultisample : TextureTarget.Texture2D;
    Width = width;
    Height = height;
    textureHandle = GL.GenTexture();
    //bind texture

    GL.BindTexture(target, textureHandle);
    Log.Error("Bound Texture: " + GL.GetError());
    if (!multisample)
    {
        GL.TexParameter(target, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
        GL.TexParameter(target, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
        GL.TexParameter(target, TextureParameterName.TextureWrapS, (int)format.WrapMode);
        GL.TexParameter(target, TextureParameterName.TextureWrapT, (int)format.WrapMode);
    }

    Log.Debug("Created Texture Parameters: " + GL.GetError());
    if (format.Multisample < 2)
        GL.TexImage2D(target, 0, format.InternalFormat, Width, Height, 0, format.PixelFormat, format.SourceType, format.Pixels);
    else
        GL.TexImage2DMultisample(TextureTargetMultisample.Texture2DMultisample, samples, format.InternalFormat, Width, Height, false);
    Log.Debug("Created Image: " + GL.GetError());
    //unbind texture
    GL.BindTexture(target, 0);
    //create depthbuffer
    if (format.DepthBuffer)
    {
        GL.GenRenderbuffers(1, out dbHandle);
        GL.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, dbHandle);

        if(multisample)
            GL.RenderbufferStorageMultisample(RenderbufferTarget.RenderbufferExt, samples, RenderbufferStorage.Rgba8, Width, Height);
        else
            GL.RenderbufferStorage(RenderbufferTarget.RenderbufferExt, RenderbufferStorage.DepthComponent24, Width, Height);
    }

    //create fbo
    fboHandle = GL.GenFramebuffer();
    GL.BindFramebuffer(FramebufferTarget.FramebufferExt, fboHandle);
    GL.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, target, textureHandle, 0);

    if(format.DepthBuffer)
        GL.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.RenderbufferExt, dbHandle);
    Log.Debug("Framebuffer status: " + GL.CheckFramebufferStatus(FramebufferTarget.FramebufferExt));
    Log.Debug("Created Framebuffer: " + GL.GetError());
    GL.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);

}

现在每当我使用大于1的多重采样时,我都会收到错误:

  

[LOG]:创建的纹理参数:NoError
     [日志]:创建图像:无错


     [LOG]:帧缓冲状态:FramebufferIncompleteAttachment

当ms小于2时,一切正常......

1 个答案:

答案 0 :(得分:2)

您在这里使用了无效的渲染缓冲格式:

GL.RenderbufferStorageMultisample(RenderbufferTarget.RenderbufferExt, samples,
       RenderbufferStorage.Rgba8, Width, Height);

您稍后尝试将此缓冲区用作深度附件。 GL_RGBA8不是深度附件的有效格式。常用的深度附件格式为GL_DEPTH_COMPONENT16GL_DEPTH_COMPONENT24。因此,呼叫应更改为:

GL.RenderbufferStorageMultisample(RenderbufferTarget.RenderbufferExt, samples,
       RenderbufferStorage.DepthComponent24, Width, Height);