Page 1 of 1

Adjust DirectX9 Flip Que

Posted: Mon Sep 13, 2010 9:46 am
by AndyMK
I dont know if this is possible without an update but any serious game development needs this. When using OpenScreen() under DirectX9, DirectX defaults to 3 pre rendered frames. If you enable vsync, this equates to a 50ms delay + mouse delay (8ms standard) before seeing any movement. It feels like input lag. Many games have an option to change the amount of pre rendered frames. You can test this below. Also take in to account that on most systems, the mouse pointer is also vsynced with the desktop, you can really see the difference.

Code: Select all

InitSprite()
InitSprite3D()
ExamineDesktops()

OpenScreen(DesktopWidth(0),DesktopHeight(0),32,"",#PB_Screen_WaitSynchronization)

SetClassLong_(ScreenID(),#GCL_HCURSOR, LoadCursor_(0, #IDC_ARROW))
ShowCursor_(1)

ClearScreen(RGB(255,255,255))
GrabSprite(0, 0, 0, 100, 100,#PB_Sprite_Texture)
CreateSprite3D(0,0)

Repeat
  Start3D()
  DisplaySprite3D(0, DesktopMouseX(), DesktopMouseY())
  Stop3D()
  FlipBuffers()
  ClearScreen(RGB(0, 0, 0))
Until GetAsyncKeyState_(#VK_ESCAPE)

Re: Adjust DirectX9 Flip Que

Posted: Mon Sep 13, 2010 4:34 pm
by Thorium
I saw this option sometimes in drivers.

But what is prerendering frames exactly doing and why?
Does it return from the flip call 3 times befor actualy flipping the frame or what?

Re: Adjust DirectX9 Flip Que

Posted: Mon Sep 13, 2010 5:01 pm
by AndyMK
It's a DirectX9 thing. I think 3 frames are rendered ahead of the first flip to make things smoother on screen but the delay is horrible. Some GPU drivers have an option under DirectX settings to change it but not all. Most games have an option to change it. Under Nvidia drivers, it's "Max Frames to Render Ahead" I have read that BackBufferCount on DirectX changes this. I wouldn't know how to change this though.

Code: Select all

typedef struct _D3DPRESENT_PARAMETERS_ {
    UINT                    BackBufferWidth;
    UINT                    BackBufferHeight;
    D3DFORMAT               BackBufferFormat;
    UINT                    BackBufferCount;  <------------------------------------------------------------
    D3DMULTISAMPLE_TYPE     MultiSampleType;
    D3DSWAPEFFECT           SwapEffect;
    HWND                    hDeviceWindow;
    BOOL                    Windowed;
    BOOL                    EnableAutoDepthStencil;
    D3DFORMAT               AutoDepthStencilFormat;
    DWORD                   Flags;
    UINT                    FullScreen_RefreshRateInHz;
    UINT                    PresentationInterval;
} D3DPRESENT_PARAMETERS;

Re: Adjust DirectX9 Flip Que

Posted: Mon Sep 13, 2010 5:14 pm
by Fred
IIRC, PB initialize the buffercount to 2. May be your driver override this setting ?

Re: Adjust DirectX9 Flip Que

Posted: Mon Sep 13, 2010 5:38 pm
by AndyMK
Hi Fred, Im am testing this on a Intel X3100 GPU .. there are no DX Settings in the driver control panel. Games such as Counterstrike Source seem to override this with an option. Apparently DX9 allows up to 8 back buffers. If i set a game to 3 or 4 i get similar results to the above code.

Re: Adjust DirectX9 Flip Que

Posted: Mon Sep 13, 2010 8:01 pm
by Trond
Software cursors always feel like they are lagging. Most games use a hardware cursor, which doesn't lag. If you turn off hardware cursor in the game setting, the cursor starts lagging.

Re: Adjust DirectX9 Flip Que

Posted: Mon Sep 13, 2010 9:04 pm
by AndyMK
Trond, Did you run the example? The sprite should be lagging behind the cursor but it depends on your graphics card drivers. Fred has already stated that PB uses 2 buffers but i think my GPU bumps this up to 4 or more. The system cursor is always ahead of the 3D rendering.

Re: Adjust DirectX9 Flip Que

Posted: Mon Sep 13, 2010 9:16 pm
by Trond
I ran the example and it is very strange. Assuming wait for vsync caps the frame rate to 60, this GDI example should have about the same amount of cursor lag, but it doesn't. You have to go down to a frame rate of about 10 to get the same amount of lag (except with the lower frame rate the movement is of course not smooth).

Code: Select all

OpenWindow(0, 0, 0, 100, 100, "",  #PB_Window_BorderLess | #PB_Window_Maximize)

W = WindowWidth(0)
h = WindowHeight(0)

AddWindowTimer(0, 0, 1000/60) ; Timer about the same speed as normal vsync wait = no cursor lag
; AddWindowTimer(0, 0, 1000/10)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_Timer
      StartDrawing(WindowOutput(0))
        Box(0, 0, w, h, #Black)
        Box(DesktopMouseX(), DesktopMouseY(), 100, 100, #White)
      StopDrawing()
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver

Re: Adjust DirectX9 Flip Que

Posted: Tue Sep 14, 2010 9:15 am
by AndyMK
Trond, i modified your example. This is how i would like it to run with openscreen() There is a big difference between the first bit of code and this code in terms of lag.

Code: Select all

ExamineDesktops()
w = DesktopWidth(0)
h = DesktopHeight(0)

OpenWindow(0, 0, 0, w, h, "",  #PB_Window_BorderLess | #PB_Window_Maximize)

Repeat
  WaitWindowEvent()
  
  StartDrawing(WindowOutput(0))
  Box(0, 0, w, h, #Black)
  Box(DesktopMouseX(), DesktopMouseY(), 100, 100, #White)
  StopDrawing()
  
Until GetAsyncKeyState_(#VK_ESCAPE)