{"id":470,"date":"2011-05-27T17:02:30","date_gmt":"2011-05-28T00:02:30","guid":{"rendered":"http:\/\/mattfife.net\/wordpress\/?p=470"},"modified":"2022-02-11T23:34:21","modified_gmt":"2022-02-12T06:34:21","slug":"gotchas-of-using-id3d11shaderreflection","status":"publish","type":"post","link":"https:\/\/mattfife.com\/?p=470","title":{"rendered":"Gotcha\u2019s of using ID3D11ShaderReflection"},"content":{"rendered":"\n<p>So, I am working on something that requires I programmatically know what&#8217;s in a DX11 shader file.&nbsp; One of the cool things you can use to figure out how many constant buffers, get string names from position streams, etc &#8211; is the <a href=\"http:\/\/members.gamedev.net\/JasonZ\/Heiroglyph\/D3D11ShaderReflection.pdf\" target=\"_blank\" rel=\"noopener\">DirectX Shader Reflection system<\/a>.&nbsp; It gives you the ability to query a loaded shader blob for names\/types\/etc.&nbsp; However, it&#8217;s not quite as straightforward as one might expect to use.&nbsp; The docs were pretty incomplete up until the DX11 version.&nbsp; But here&#8217;s the basics of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff476590%28v=vs.85%29.aspx\" target=\"_blank\" rel=\"noopener\">ID3D11ShaderReflection <\/a>system:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Load your vertex\/pixel shaders.\u00a0 Get the actual shader blob &#8211; or extract it from the FX system if you used that.<\/li><li>Bind it to a shader reflection object using D3DReflect:<br> <pre><code>pd3dDevice->CreatePixelShader( pPixelShaderBuffer->GetBufferPointer(), pPixelShaderBuffer->GetBufferSize(), g_pPSClassLinkage, &amp;g_pPixelShader );<br>ID3D11ShaderReflection* pReflector = NULL;<br>D3DReflect( pPixelShaderBuffer->GetBufferPointer(), pPixelShaderBuffer->GetBufferSize(), IID_ID3D11ShaderReflection, (void**) &amp;pReflector);<\/code><\/pre> <\/li><li>Query the reflection object for whatever constant buffer, input\/output stream, or other info you want using the <a rel=\"noopener\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff476590%28v=vs.85%29.aspx\" target=\"_blank\">many methods available<\/a>.\u00a0 It&#8217;s a GREAT way to test your shaders at load time so you don&#8217;t get cryptic runtime errors later when you try to actually draw objects.<\/li><\/ol>\n\n\n\n<p>Now, the gotcha&#8217;s:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>You must also include this: <pre>#include &lt;D3DCompiler.h><\/pre>Or you&#8217;ll get a compile error with D3DReflect() &#8211; despite the fact the official Microsoft docs seem to say you should (only) include: <pre>#include &lt;D3DShader.h><\/pre> <\/li><li>Finally, you <em>MUST<\/em> have the DirectX SDK include directory listed <em><strong>before<\/strong><\/em> the windows include directory in the Visual Studio compiler include directory list or you&#8217;ll get compile errors in D3DShader.h. This is apparently because the windows headers that come with Visual Studio actually have a few DX definitions in them that conflict with the DX SDK header definitions.\u00a0\u00a0 <br>In Visual Studio, check: <strong>Tools\/Options -> Project and Solutions -> VC++ Directories<\/strong> and make sure Windows SDK include &amp; library\u00a0paths appear AFTER DirectX include &amp; library paths.\u00a0 See <a rel=\"noopener\" href=\"http:\/\/forums.create.msdn.com\/forums\/p\/76624\/465612.aspx\" target=\"_blank\">here for the thread<\/a>.<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>So, I am working on something that requires I programmatically know what&#8217;s in a DX11 shader file.&nbsp; One of the cool things you can use to figure out how many constant buffers, get string names from position streams, etc &#8211; is the DirectX Shader Reflection system.&nbsp; It gives you the ability to query a loaded shader blob for names\/types\/etc.&nbsp; However, it&#8217;s not quite as straightforward as one might expect to use.&nbsp; The docs were pretty incomplete up until the DX11&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/mattfife.com\/?p=470\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[],"class_list":["post-470","post","type-post","status-publish","format-standard","hentry","category-technicalprogramming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4WECr-7A","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=470"}],"version-history":[{"count":10,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/470\/revisions"}],"predecessor-version":[{"id":6312,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/470\/revisions\/6312"}],"wp:attachment":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}