我正在尝试使用 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 / 其他一些位的代码,因为它只是噪音,但如果需要可以提供)