{"id":3178,"date":"2020-10-19T14:13:53","date_gmt":"2020-10-19T21:13:53","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/directx\/?p=3178"},"modified":"2020-10-19T15:45:30","modified_gmt":"2020-10-19T22:45:30","slug":"d3d12-debug-layer-message-callback","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/directx\/d3d12-debug-layer-message-callback\/","title":{"rendered":"D3D12 Debug Layer Message Callback"},"content":{"rendered":"<p>Finally, D3D12 messages that you can easily integrate with your own application logs! The D3D12 InfoQueue Message Callback feature allows developers to capture debug messages, with stack context, providing yet another essential debugging tool for error analysis. To date, D3D12 has had ID3D12InfoQueue API. However, developers always wants there to be message callback feature, which allows them to more easily investigate callstacks of live errors and to instrument customized error accounting and reporting mechanisms. If you have ever felt frustrated when you get DXGI_ERROR_DEVICE_REMOVED HRESULT from some much-later Create* or Present call, now you can probably capture Device-Removal events when they are seen by the D3D12 runtime.<\/p>\n<p>Good news is this feature is now available in Windows 10 Release Preview build 20236. Let&#8217;s look at the API\u00a0first:<\/p>\n<pre><code>interface ID3D12InfoQueue1 <\/code>\r\n<code> \u00a0\u00a0 : ID3D12InfoQueue\u00a0<\/code>\r\n<code>{ <\/code>\r\n<code> \u00a0\u00a0 HRESULT\u00a0RegisterMessageCallback(\u00a0<\/code>\r\n<code> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 D3D12MessageFunc\u00a0CallbackFunc,\u00a0\u00a0<\/code>\r\n<code> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0D3D12_MESSAGE_CALLBACK_FLAGS\u00a0CallbackFilterFlags,\u00a0<\/code>\r\n<code> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 void*\u00a0pContext,\u00a0<\/code>\r\n<code> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DWORD *pCallbackCookie); <\/code>\r\n\r\n<code> \u00a0\u00a0 HRESULT\u00a0UnregisterMessageCallback(\u00a0<\/code>\r\n<code> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DWORD\u00a0CallbackCookie);\u00a0<\/code>\r\n<code>}\u00a0<\/code><\/pre>\n<pre class=\"hljs\"><code class=\"c++\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">typedef<\/span> <span class=\"hljs-title\">void<\/span> <span class=\"hljs-params\">(*D3D12MessageFunc)<\/span><span class=\"hljs-params\">(\r\n    D3D12_MESSAGE_CATEGORY Category, \r\n    D3D12_MESSAGE_SEVERITY Severity, \r\n    D3D12_MESSAGE_ID ID, \r\n    LPCSTR pDescription, \r\n    <span class=\"hljs-keyword\">void<\/span>* pContext)<\/span><\/span>;\r\n\r\n<span class=\"hljs-keyword\">typedef<\/span> <span class=\"hljs-keyword\">enum<\/span> D3D12_MESSAGE_CALLBACK_FLAGS\r\n{\r\n    D3D12_MESSAGE_CALLBACK_FLAG_NONE = <span class=\"hljs-number\">0x00<\/span>,\r\n    D3D12_MESSAGE_CALLBACK_IGNORE_FILTERS = <span class=\"hljs-number\">0x01<\/span>,\r\n} D3D12_MESSAGE_CALLBACK_FLAGS;\r\n<\/code><\/pre>\n<p>To use the message callback feature, it is very simple, you just call RegisterMessageCallback when you want to register a callback and call UnregisterMessageCallback when you no longer wants it. RegisterMessageCallback registers a callback that is called at the time a debug message is produced, and with this method, you can create your own callback function. Besides, this function also takes a void* pContext as input, which means you can now put your own data into the callback function. This function returns the callback cookie to you and you can later use this cookie value to unregister any callbacks you no longer want. Also, we have CallbackFilterFlags as an input, which makes sure that you have the most commonly used two scenarios. 1. If you want your own message filtering mechanism in callback, you can set this value to D3D12_MESSAGE_CALLBACK_IGNORE_FILTERS and this will disable message filtering. 2. If you want current callback to be filtered in the exact same way as what gets logged as debug text. You can set this value to D3D12_MESSAGE_CALLBACK_FLAG_NONE.<\/p>\n<p>Something worth mentioning is that message callback supports multiple callbacks, since supporting multiple callback registration allows a D3D12 component within a process to handle messages without breaking callbacks registered by any other D3D12 components in the same process. And if multiple callbacks are active, they will be called sequentially in the same order that the callbacks were registered.<\/p>\n<p>Before using this new API, there are several things to pay attention to, because this API does have some restrictions. The most important one is don&#8217;t make other D3D calls inside the callback. This restriction is only documented, and it is not strictly validated by the runtime. Callbacks may be invoked with the internal runtimes in states which are unsafe to make other D3D calls, so any D3D API calls from a callback implementation could result in deadlocks and crashes. Another thing to pay attention is that callback may execute on a thread other than the one that originally issued the API call, so you need to ensure that your callbacks can properly handle this.<\/p>\n<p>Hope you&#8217;ll enjoy this feature!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Finally, D3D12 messages that you can easily integrate with your own application logs! The D3D12 InfoQueue Message Callback feature allows developers to capture debug messages, with stack context, providing yet another essential debugging tool for error analysis. To date, D3D12 has had ID3D12InfoQueue API. However, developers always wants there to be message callback feature, which [&hellip;]<\/p>\n","protected":false},"author":42930,"featured_media":12651,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3178","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-directx"],"acf":[],"blog_post_summary":"<p>Finally, D3D12 messages that you can easily integrate with your own application logs! The D3D12 InfoQueue Message Callback feature allows developers to capture debug messages, with stack context, providing yet another essential debugging tool for error analysis. To date, D3D12 has had ID3D12InfoQueue API. However, developers always wants there to be message callback feature, which [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/directx\/wp-json\/wp\/v2\/posts\/3178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/directx\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/directx\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/directx\/wp-json\/wp\/v2\/users\/42930"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/directx\/wp-json\/wp\/v2\/comments?post=3178"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/directx\/wp-json\/wp\/v2\/posts\/3178\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/directx\/wp-json\/wp\/v2\/media\/12651"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/directx\/wp-json\/wp\/v2\/media?parent=3178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/directx\/wp-json\/wp\/v2\/categories?post=3178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/directx\/wp-json\/wp\/v2\/tags?post=3178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}