Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix D3D12 debug crash due to validation layers SDK bug #3222

Merged
merged 7 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 61 additions & 18 deletions src/bgfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2656,27 +2656,70 @@ namespace bgfx
bool windowsVersionIs(Condition::Enum _op, uint32_t _version)
RazielXYZ marked this conversation as resolved.
Show resolved Hide resolved
{
#if BX_PLATFORM_WINDOWS
static const uint8_t s_condition[] =
RTL_OSVERSIONINFOW ovi;
bx::memSet(&ovi, 0 , sizeof(ovi));
ovi.dwOSVersionInfoSize = sizeof(ovi);
const HMODULE hMod = GetModuleHandleW(L"ntdll.dll");
if (NULL != hMod)
{
VER_LESS_EQUAL,
VER_GREATER_EQUAL,
};
FARPROC (WINAPI* rtlGetVersionPtr) (PRTL_OSVERSIONINFOW) = reinterpret_cast<FARPROC (WINAPI*)(PRTL_OSVERSIONINFOW)>(GetProcAddress(hMod, "RtlGetVersion"));
if (NULL != rtlGetVersionPtr)
{
rtlGetVersionPtr(&ovi);
if (ovi.dwMajorVersion == 0)
{
return false;
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you could do:

ovi.dwBuildNumber =  UINT32_MAX == _build ? UINT32_MAX : ovi.dwBuildNumber;

And then remove those if/else blocks below since test will succeed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea!

}
const DWORD cMajorVersion = HIBYTE(_version);
const DWORD cMinorVersion = LOBYTE(_version);
switch (_op)
{
case Condition::LessEqual:
return ovi.dwMajorVersion <= cMajorVersion && ovi.dwMinorVersion <= cMinorVersion;
case Condition::GreaterEqual:
return ovi.dwMajorVersion >= cMajorVersion && ovi.dwMinorVersion >= cMinorVersion;
default:
return false;
}
#else
BX_UNUSED(_op, _version);
return false;
#endif // BX_PLATFORM_WINDOWS
}

OSVERSIONINFOEXA ovi;
bx::memSet(&ovi, 0, sizeof(ovi) );
bool windowsBuildIs(Condition::Enum _op, uint32_t _build)
{
#if BX_PLATFORM_WINDOWS
RTL_OSVERSIONINFOW ovi;
bx::memSet(&ovi, 0 , sizeof(ovi));
ovi.dwOSVersionInfoSize = sizeof(ovi);
// _WIN32_WINNT_WINBLUE 0x0603
// _WIN32_WINNT_WIN8 0x0602
// _WIN32_WINNT_WIN7 0x0601
// _WIN32_WINNT_VISTA 0x0600
ovi.dwMajorVersion = HIBYTE(_version);
ovi.dwMinorVersion = LOBYTE(_version);
DWORDLONG cond = 0;
VER_SET_CONDITION(cond, VER_MAJORVERSION, s_condition[_op]);
VER_SET_CONDITION(cond, VER_MINORVERSION, s_condition[_op]);
return !!VerifyVersionInfoA(&ovi, VER_MAJORVERSION | VER_MINORVERSION, cond);
const HMODULE hMod = GetModuleHandleW(L"ntdll.dll");
if (NULL != hMod)
{
FARPROC (WINAPI* rtlGetVersionPtr) (PRTL_OSVERSIONINFOW) = reinterpret_cast<FARPROC (WINAPI*)(PRTL_OSVERSIONINFOW)>(GetProcAddress(hMod, "RtlGetVersion"));
if (NULL != rtlGetVersionPtr)
{
rtlGetVersionPtr(&ovi);
if (ovi.dwBuildNumber == 0)
{
return false;
}
}
}

switch (_op)
{
case Condition::LessEqual:
return ovi.dwBuildNumber <= _build;
case Condition::GreaterEqual:
return ovi.dwBuildNumber >= _build;
default:
return false;
}
#else
BX_UNUSED(_op, _version);
BX_UNUSED(_op, _build);
return false;
#endif // BX_PLATFORM_WINDOWS
}
Expand All @@ -2701,7 +2744,7 @@ namespace bgfx

if (BX_ENABLED(BX_PLATFORM_WINDOWS) )
{
if (windowsVersionIs(Condition::GreaterEqual, 0x0602) )
if (windowsVersionIs(Condition::GreaterEqual, 0x0603) )
{
score += RendererType::Direct3D11 == renderer ? 20 : 0;
score += RendererType::Direct3D12 == renderer ? 10 : 0;
Expand Down
2 changes: 2 additions & 0 deletions src/bgfx_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ namespace bgfx

bool windowsVersionIs(Condition::Enum _op, uint32_t _version);

bool windowsBuildIs(Condition::Enum _op, uint32_t _build);

constexpr bool isShaderType(uint32_t _magic, char _type)
{
return uint32_t(_type) == (_magic & BX_MAKEFOURCC(0xff, 0, 0, 0) );
Expand Down
21 changes: 5 additions & 16 deletions src/renderer_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,29 +844,18 @@ namespace bgfx { namespace d3d12

if (SUCCEEDED(hr))
{
//This is the least ugly way to get the windows build that still works past win10 without deprecation warnings or needing app manifests
RTL_OSVERSIONINFOW osver;
bx::memSet(&osver, 0 , sizeof(RTL_OSVERSIONINFOW));
const HMODULE hMod = ::GetModuleHandleW(L"ntdll.dll");
if (hMod) {
FARPROC (WINAPI* rtlGetVersionPtr) (PRTL_OSVERSIONINFOW) = reinterpret_cast<FARPROC (WINAPI*)(PRTL_OSVERSIONINFOW)>(::GetProcAddress(hMod, "RtlGetVersion"));
if (rtlGetVersionPtr != nullptr) {
rtlGetVersionPtr(&osver);
if (osver.dwBuildNumber > 0) {
osver.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOW);
}
}
}

// https://discordapp.com/channels/590611987420020747/593519198995742733/703642988345032804
// D3D12 Bug Number: 26131261
// There is a bug in the D3D12 validation that causes example-21 to fail when using UAV
// Setting SetEnableSynchronizedCommandQueueValidation below to false avoids the bug
// It was fixed in (probably) the first windows 11 sdk, 22000
// However, the fix causes any dx12 context with validation to break if this is set to false, so we can't do that anymore
if (osver.dwOSVersionInfoSize > 0 && osver.dwBuildNumber >= 22000) {
if (windowsBuildIs(Condition::GreaterEqual, 22000))
{
debug1->SetEnableGPUBasedValidation(true);
} else {
}
else
{
debug1->SetEnableSynchronizedCommandQueueValidation(false);
}
}
Expand Down
Loading