Don't give up!

[DirectX 11] Windows SDK 10에서 DxTrace 대체하기 본문

개발서적/DirectX 11을 이용한 3D 게임 프로그래밍 입문

[DirectX 11] Windows SDK 10에서 DxTrace 대체하기

Heang Lee 2022. 12. 23. 17:46

최근 DirectX를 공부하고자 루나의 DirectX 11 책(물방울 책)를 읽으며 공부하고 있습니다.

아무래도 책이 출판된지 오래되었기에 책의 예제 코드를 작성하다보면 오류들이 많이 발생합니다.

루나의 DirectX 11 책은 DirectX SDK를 사용하지만 Windows SDK 8 이후로 DirectX SDK를 사용하지 않기 때문입니다.

DirectX SDK를 직접 받아 사용하는 방법이 있지만, 2010년에 나온 SDK를 그대로 사용하는 것보다 Windows SDK에서 실행가능하도록 코드를 수정해보는 것이 좋겠다고 판단하여 직접 수정해보기로 하였고, 찾기 어려웠던 정보를 기록하려고 합니다.

HR 매크로의 DXTrace를 MessageBox로 대체

DXTrace는 dxerr.h를 포함하여야 하지만, dxerr 라이브러리는 Windows SDK에 존재하지 않습니다.

해결방법을 찾아본결과, DXTrace를 MessageBox로 대체하는 방법이 존재하였습니다.

[Direct3D 11] Window10 SDK로 포함되면서 사라진 DXTrace 함수 :: 베르의 프로그래밍 노트 (tistory.com)

 

[Direct3D 11] Window10 SDK로 포함되면서 사라진 DXTrace 함수

Direct3D 9 때부터 있던 DXTrace 함수가 있다. Direct3D의 대부분의 함수는 HRESULT 타입으로 값을 반환하는데 DirectX의 대부분의 함수는 HRESULT형으로 값을 반환하는데 DXTrace 함수는 이때 발생한 에러에 대

wergia.tistory.com

#ifndef HR(x)
#define HR(x)\
{\
    HRESULT hr = (x);\
	if (FAILED(hr))\
	{\
		LPWSTR output;\
		FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &output, 0, NULL);\
		MessageBox(NULL, output, TEXT("ERROR"), MB_OK);\
	}\
}														
#endif // !HR(x)

이유를 찾아보니, XBOX Senior engineer이신 Chuck Walbourn님의 깃허브 블로그에서 다음과 같은 내용을 볼 수 있었습니다.

Where’s DXERR.LIB? | Games for Windows and the DirectX SDK blog (walbourn.github.io)

 

Where’s DXERR.LIB?

One of the little utility libraries in the DirectX SDK is a static library for converting HRESULTs to text strings for debugging and diagnostics known as DXERR.LIB. There were once even older versions of this library, DXERR8.LIB and DXERR9.LIB, but they we

walbourn.github.io

For the Windows SDK 8.0 this library was not brought forward (see “Where is the DirectX SDK?”).
This is primarily because HRESULTS for DirectX graphics APIs were added to the FormatMessage function when using FORMAT_MESSAGE_FROM_SYSTEM in Windows 8 which already supports most of the system error codes reported by DXERR.
The DirectX SDK version of DXERR.LIB also contained a lot of error codes for legacy components that are no longer relevant to development using the Windows SDK 8.0.

요약하자면, Windows SDK 8 버전부터 Dxerr에서 받을 수 있는 대부분의 운영체제 에러를 FormatMessage에서도 받을 수 있게 되었고, 더 이상 SDK 8을 사용한 개발과 관련이 없는 오류 코드들이 Dxerr에 존재하기 때문에 Dxerr는 레거시가 되었다는 것입니다.

따라서 FormatMessage를 사용하여 관련 오류 코드를 받아 에러 메시지 박스를 발생시키면, DxTrace를 대체할 수 있습니다.