locked
Swap chain width and height for high density screens

    Question

  • Hi!

    The "DirectX Postcard App Sample" which can be found among the various Windows 8 examples, uses a Xaml SwapChainBackgroundPanel.

    Inside the class "DirectXBase" of this sample, there is a method CreateWindowSizeDependentResources().

    There, the width and the height of the swap chain is calculated:

    swapChainDesc.Width = static_cast<UINT>(m_window->Bounds.Width * m_dpi / 96.0f);

    swapChainDesc.Height = static_cast<UINT>(m_window->Bounds.Height * m_dpi / 96.0f);

    Let us assume we have a high density screen and Windows therefore uses a 180% scale.

    If we have a 2560x1440 screen, then m_window->Bounds.Width will be 1422.222....   (= 2560 / 1.8)

    And m_dpi will be 172.8  (= 96 * 1.8)

    So the expression on the right hand side is: 2560 / 1.8 * 96 * 1.8 / 96 which equals 2560.

    My question is: the variables used are floats. Could it happen that in some cases there are rounding errors and then the casting to a uint leads to an error of 1 pixel?

    For example: Instead of 2560 the result could be 2559.999999 which is casted to 2559.

    This would lead to a stretching of a 2559 pixel image to a 2560 pixel screen, which would look very ugly.

    So, should we use proper rounding before casting to int? Or is there some magic so there will never be problems?

    Saturday, November 10, 2012 11:52 AM

Answers

  • Hello,

    You are correct. Rounding errors may occur. You can use the following formula to help avoid rounding problems.

    floor((dips * DisplayProperties::LogicalDpi / 96.0f) + 0.5f);

    I hope this helps,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Friday, November 16, 2012 12:58 AM
    Moderator

All replies

  • Hi,

    Maybe you can try rounding function, like

     static_cast<UINT>(m_window->Bounds.Width * m_dpi / 96.0f + 0.5);

    So that you will get the 2560

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, November 12, 2012 3:27 AM
    Moderator
  • Hi Jesse,

    Thank you for your reply! But my question was not how to round, but if rounding is necessary.

    Maybe you could contact the guys who wrote the Postcard example? (Seems like you are from Microsoft)

    I think a DirectX expert could answer the question.

    Monday, November 12, 2012 5:35 PM
  • OK,  I will involve more experts to investigate it.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, November 13, 2012 3:22 AM
    Moderator
  • Hello,

    You are correct. Rounding errors may occur. You can use the following formula to help avoid rounding problems.

    floor((dips * DisplayProperties::LogicalDpi / 96.0f) + 0.5f);

    I hope this helps,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Friday, November 16, 2012 12:58 AM
    Moderator
  • Hello!

    Thank you for your answer!

    The Postcard sample should be updated.

    Tuesday, November 20, 2012 9:01 AM