【问题标题】:Using double-buffering with SlimDX在 SlimDX 中使用双缓冲
【发布时间】:2013-10-17 21:21:23
【问题描述】:

我正在尝试使用 SlimDX 创建 DirectX9 应用程序。

如果我使用.PresentationInterval = PresentInterval.Default,它会以~我的显示器的刷新率呈现,并且看起来不错。

如果我使用.PresentationInterval = PresentInterval.Immediate,我会得到大约 6,000 FPS,但会出现严重的闪烁 - 大概是因为在立即演示发生时设备正在更新,因此它可能正确绘制也可能不正确。

谁能告诉我如何使用后台缓冲区,以便立即不闪烁并且在我完成绘图时交换缓冲区?

显然,我实际上并不想要 6K FPS,但我确实想要控制帧速率上限,并且对缓冲有更好的了解。

设备初始化

PresentParameters = New PresentParameters()

With PresentParameters
    .BackBufferFormat = Format.X8R8G8B8
    .BackBufferCount = 2
    .Multisample = MultisampleType.None
    .SwapEffect = SwapEffect.Discard
    .EnableAutoDepthStencil = True
    .AutoDepthStencilFormat = Format.D24S8
    .PresentFlags = PresentFlags.DiscardDepthStencil
    .PresentationInterval = PresentInterval.Default '' or PresentInterval.Immediate
    Select Case Settings.Display.Mode
        Case WindowMode.FullScreen
            .BackBufferWidth = Settings.Display.Width
            .BackBufferHeight = Settings.Display.Height
            .Windowed = False
        Case WindowMode.Windowed Or WindowMode.WindowedNoBorder
            .BackBufferWidth = Settings.Display.Width
            .BackBufferHeight = Settings.Display.Height
            .Windowed = True
    End Select
    .DeviceWindowHandle = Handle
End With

Direct3D = New Direct3D()
Device = New Device(Direct3D,
                    Settings.Display.Adapter,
                    DeviceType.Hardware,
                    Handle,
                    CreateFlags.HardwareVertexProcessing,
                    PresentParameters)

最小渲染示例...

Context9.Device.BeginScene()
Context9.Device.Clear(Direct3D9.ClearFlags.Target Or Direct3D9.ClearFlags.ZBuffer,
                Color.Black,
                1.0F,
                0)

Game.Render(Context9)

Using Sprite As New Sprite(Context9.Device)
    Sprite.Begin(SpriteFlags.AlphaBlend)
    Dim Mtx = Matrix.Translation(125, 200, 0)
    Dim Scaling = Matrix.Scaling(0.5, 0.5, 1)
    Matrix.Multiply(Mtx, Scaling, Mtx)
    Sprite.Transform = Mtx

    Dim Fade As Single = CSng(Math.Min(1, Math.Sin(FrameI / 30) * 0.5 + 0.5))
    Sprite.Draw(TestTex,
                Nothing,
                Nothing,
                Nothing,
                New Color4(Fade, Fade, Fade))


    Sprite.End()
End Using

Context9.Device.EndScene()

Context9.Device.Present()

窗口创建/主循环

Private Sub Run()
    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)

    Window = New RenderForm("Test")

    InitializeDevice()

    ''Add lots of key handlers, etc here

    LoadResources()

    Clock.Start()
    MessagePump.Run(Window, AddressOf MessageLoop)
    Cleanup()

    Window.Dispose()
End Sub

Sub MessageLoop()
    Update()
    If Not IsResizing Then
        Render()
    End If
End Sub

(我省略了显示 FPS / 其他一些位的代码,因为它只是噪音,但如果需要可以提供)

【问题讨论】:

  • 如果你不使用vsync,无论你使用多少个backbuffer,总会有一些闪烁。
  • @Vertexwahn 如果图像发生剧烈/快速变化,我会同意并期待锯齿状/等,但这会使图像淡入淡出 - 在最糟糕的情况下,我预计会有一半图像比其他图像稍微不透明 - 但在这种情况下可能不可见。就目前而言,它像闪光灯一样闪烁

标签: .net directx slimdx doublebuffered


【解决方案1】:

我可能是错的,但我相信这与 DirectX 无关。您必须在创建窗口时启用双缓冲。

  • 如果您使用System.Windows.Forms,请按照here 的描述调用Control.SetStyle()

    public void EnableDoubleBuffering()
    {
          // Set the value of the double-buffering style bits to true. 
          this.SetStyle(ControlStyles.DoubleBuffer | 
          ControlStyles.UserPaint | 
          ControlStyles.AllPaintingInWmPaint,
          true);
          this.UpdateStyles();
    }
    
  • 或者更好,只需使用SlimDX.Windows.RenderForm。它源自Form,默认启用双缓冲。

闪烁的另一个原因可能是错误的主循环结构。您没有向我们展示您的主循环以及如何调用渲染函数。您可以在tutorials 中找到典型的渲染循环。

希望对你有帮助!

【讨论】:

  • 感谢您的指点 - 我的错没有明确,我实际上是使用 Window = New RenderForm("Test") 创建一个窗口。我将使用循环信息编辑我的 Q
【解决方案2】:

DirectX 总是使用双缓冲,你不能禁用它(你可以使用三重缓冲,但到目前为止我还没有看到这方面的用例)。

使用默认值时,后台缓冲区会在监视器的两帧之间复制到屏幕上。使用立即,这会直接发生。这总是会闪烁一点,特别是如果新图像与旧图像有很大不同,因为屏幕的上半部分会显示一个图像,而下半部分会显示下一个图像。在大多数情况下,渲染速度快于显示器的刷新率是没有用的。

【讨论】:

    【解决方案3】:

    所以....事实证明这是一个愚蠢的案例。

    Dim Fade As Single = CSng(Math.Min(1, Math.Sin(FrameI / 30) * 0.5 + 0.5))
    

    您会注意到,褪色是基于帧数而不是经过的时间。所以当帧速率上升时,淡入淡出速度也随之上升......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 2017-06-21
      • 1970-01-01
      相关资源
      最近更新 更多