{"id":518,"date":"2020-02-12T14:01:05","date_gmt":"2020-02-12T06:01:05","guid":{"rendered":"https:\/\/www.caiqinyi.cn\/?p=518"},"modified":"2025-02-26T11:10:11","modified_gmt":"2025-02-26T03:10:11","slug":"hlsl_statement","status":"publish","type":"post","link":"https:\/\/www.caiqinyi.cn\/index.php\/2020\/02\/12\/hlsl_statement\/","title":{"rendered":"HLSL\u8bed\u6cd5\u5c0f\u8bb0(\u6301\u7eed\u66f4\u65b0~)"},"content":{"rendered":"<p>\u6700\u8fd1\u5728\u4f7f\u7528HLSL\u5b9e\u73b0\u5149\u7167\u6548\u679c, \u6709\u4e00\u4e9b\u8bbe\u7f6e\u4e0e\u8bed\u6cd5\u7cd6\u9700\u8981\u6ce8\u610f\u4e00\u4e0b~<\/p>\n<p><!--more--><\/p>\n<p><strong>\u53c2\u8003\u6750\u6599<\/strong><br \/>\n1. <a href=\"https:\/\/www.cnblogs.com\/X-Jun\/p\/12246859.html\">DirectX11&#8211;HLSL\u8bed\u6cd5\u5165\u95e8<\/a><br \/>\n2. <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/direct3dhlsl\/dx-graphics-hlsl-for\">for Statement<\/a><br \/>\n3. <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/direct3dhlsl\/dx-graphics-hlsl-if\">if Statement<\/a><br \/>\n4. <a href=\"https:\/\/zhuanlan.zhihu.com\/p\/37582101\">C++\u6027\u80fd\u69a8\u6c41\u673a\u4e4b\u5faa\u73af\u5c55\u5f00<\/a><\/p>\n<p><strong>1. \u5982\u4f55\u4e3aVS\u6216\u8005PS\u4f20\u9012Constant Buffers?<\/strong><\/p>\n<p>\u5728stackexchange\u4e0a\u770b\u5230\u4e00\u4e2a\u6bd4\u8f83\u9760\u8c31\u7684\u56de\u7b54: <a href=\"https:\/\/gamedev.stackexchange.com\/questions\/86818\/how-does-direct3d-know-if-a-constant-buffer-is-for-the-vertex-or-pixel-shader\">How does Direct3D know if a constant buffer is for the vertex or pixel shader?<\/a><\/p>\n<p>\u67d0\u7b54\u4e3b\u7684\u56de\u7b54\u5c31\u4e0d\u7ffb\u8bd1\u4e86, \u5426\u5219\u53cd\u800c\u4f1a\u89c9\u5f97\u5f88\u8fdd\u548c:<\/p>\n<blockquote><p>\nThe key step that you&#8217;re missing is the implicit slot assignment that occurs when you compile a shader. When you compile an HLSL shader that contains a bindable object (be it a Texture2D, RWStructuredBuffer, or cbuffer), each object must be assigned a slot number. This corresponds to the UINT StartSlot parameter to e.g. VSSetConstantBuffers. You can optionally declare a slot explicitly using e.g. : register(c0) as a declaration suffix, but if you omit the slot, the compiler will assign the lowest free slot available.<\/p>\n<p>As for your original question, how DidrectX &#8220;knows&#8221; what cbuffer to bind, well it doesn&#8217;t. What&#8217;s actually happening is that since in your vertex shader, you only refer to one cbuffer, that gets assigned slot 0 for the vertex shader when you compile it, and the other is ignored. Likewise, the same occurs for the pixel shader, except the other cbuffer is assigned to slot 0. This means that when you assign buffers to the 0 StartSlot for both the vertex and pixel shader stages, it just so happens that those are the right buffers for those shaders. Basically, it just happens to work because slot 0 is always the right slot. Once you get to more advanced shaders that reference multiple cbuffers, you&#8217;ll want to get into the habit of assigning slots explicitly.\n<\/p><\/blockquote>\n<p>\u603b\u7ed3\u4e00\u4e0b, \u4fbf\u662f\u4f7f\u7528: register(c0)\u8bed\u6cd5\u4e3aConstant Buffer\u5206\u914d\u63d2\u69fd, \u7136\u540e\u4f7f\u7528VSSetConstantBuffers\u6216\u8005PSSetConstantBuffers\u544a\u8bc9VS\u6216\u8005PS\u9700\u8981\u4f20\u9012\u7684Constant Buffer\u662f\u54ea\u4e00\u5757. \u5b83\u548cGLSL\u4e2d\u7684uniform\u53d8\u91cf\u5176\u5b9e\u662f\u6709\u7740\u5f02\u66f2\u540c\u5de5\u4e4b\u5999\u7684: \u56e0\u4e3a\u5728GLSL\u4e2d, \u4e5f\u662f\u9700\u8981\u5206\u522b\u5728VS\u6216\u8005PS\u4e2d\u5206\u522b\u58f0\u660euniform\u53d8\u91cf, \u6765\u544a\u8bc9\u5f53\u524dshader\u9700\u8981\u4f7f\u7528\u7684\u53d8\u91cf. \u53ea\u4e0d\u8fc7\u611f\u89c9D3D\u662f\u628a\u8fd9\u4e2a\u58f0\u660e\u8fc7\u7a0b\u653e\u5230\u4e86CPU\u4e0a\u505a. \u8fd9\u4e2a\u533a\u522b\u89c1\u4ec1\u89c1\u667a\u53ed~<\/p>\n<p><strong>2. \u8bed\u6cd5\u7cd6(\u4f30\u8ba1\u4f1a\u6301\u7eed\u66f4\u65b0)<\/strong><\/p>\n<p><strong>\u6761\u4ef6\u8bed\u53e5<\/strong><br \/>\nHLSL\u4e5f\u652f\u6301if, else, continue, break, switch\u5173\u952e\u5b57, \u6b64\u5916discard\u5173\u952e\u5b57\u7528\u4e8e\u50cf\u7d20\u7740\u8272\u9636\u6bb5\u629b\u5f03\u8be5\u50cf\u7d20.<\/p>\n<p>\u6761\u4ef6\u7684\u5224\u65ad\u4f7f\u7528\u4e00\u4e2a\u5e03\u5c14\u503c\u8fdb\u884c, \u901a\u5e38\u7531\u5404\u79cd\u903b\u8f91\u8fd0\u7b97\u7b26\u6216\u8005\u6bd4\u8f83\u8fd0\u7b97\u7b26\u64cd\u4f5c\u5f97\u5230. \u6ce8\u610f\u5411\u91cf\u4e4b\u95f4\u7684\u6bd4\u8f83\u6216\u8005\u903b\u8f91\u64cd\u4f5c\u662f\u5f97\u5230\u4e00\u4e2a\u5b58\u6709\u5e03\u5c14\u503c\u7684\u5411\u91cf, \u4e0d\u80fd\u591f\u76f4\u63a5\u7528\u4e8e\u6761\u4ef6\u5224\u65ad, \u4e5f\u4e0d\u80fd\u7528\u4e8eswitch\u8bed\u53e5.<\/p>\n<p><strong>\u5224\u65ad\u4e0e\u52a8\u6001\u5206\u652f<\/strong><br \/>\n\u57fa\u4e8e\u503c\u7684\u6761\u4ef6\u5206\u652f\u53ea\u6709\u5728\u7a0b\u5e8f\u6267\u884c\u7684\u65f6\u5019\u88ab\u7f16\u8bd1\u597d\u7684\u7740\u8272\u5668\u6c47\u7f16\u6210\u4e24\u79cd\u65b9\u5f0f: \u5224\u65ad(predication) \u548c\u52a8\u6001\u5206\u652f(dynamic branching).<\/p>\n<p>\u5982\u679c\u4f7f\u7528\u7684\u662f\u5224\u65ad\u7684\u5f62\u5f0f, \u7f16\u8bd1\u5668\u4f1a\u63d0\u524d\u8ba1\u7b97\u4e24\u4e2a\u4e0d\u540c\u5206\u652f\u4e0b\u8868\u8fbe\u5f0f\u7684\u503c, \u7136\u540e\u4f7f\u7528\u6bd4\u8f83\u6307\u4ee4\u6765\u57fa\u4e8e\u6bd4\u8f83\u7ed3\u679c\u6765&#8221;\u9009\u62e9&#8221;\u6b63\u786e\u7684\u503c.<\/p>\n<p>\u800c\u52a8\u6001\u5206\u652f\u4f7f\u7528\u7684\u662f\u8df3\u8f6c\u6307\u4ee4\u6765\u907f\u514d\u4e00\u4e9b\u975e\u5fc5\u8981\u7684\u8ba1\u7b97\u548c\u5185\u5b58\u8bbf\u95ee.<\/p>\n<p>\u7740\u8272\u5668\u7a0b\u5e8f\u5728\u540c\u65f6\u6267\u884c\u7684\u65f6\u5019\u5e94\u5f53\u9009\u62e9\u76f8\u540c\u7684\u5206\u652f, \u4ee5\u9632\u6b62\u786c\u4ef6\u5728\u5206\u652f\u7684\u4e24\u8fb9\u6267\u884c. \u901a\u5e38\u60c5\u51b5\u4e0b, \u786c\u4ef6\u4f1a\u540c\u65f6\u5c06\u4e00\u7cfb\u5217\u8fde\u7eed\u7684\u9876\u70b9\u6570\u636e\u4f20\u5165\u5230\u9876\u70b9\u7740\u8272\u5668\u5e76\u884c\u8ba1\u7b97, \u6216\u8005\u662f\u4e00\u7cfb\u5217\u8fde\u7eed\u7684\u50cf\u7d20\u5355\u5143\u4f20\u5165\u5230\u50cf\u7d20\u7740\u8272\u5668\u540c\u65f6\u8fd0\u7b97\u7b49.<\/p>\n<p>\u52a8\u6001\u5206\u652f\u4f1a\u7531\u4e8e\u6267\u884c\u5206\u652f\u6307\u4ee4\u6240\u5e26\u6765\u7684\u5f00\u9500\u800c\u5bfc\u81f4\u4e00\u5b9a\u7684\u6027\u80fd\u635f\u5931, \u56e0\u6b64\u8981\u6743\u8861\u52a8\u6001\u5206\u652f\u7684\u5f00\u9500\u548c\u53ef\u4ee5\u8df3\u8fc7\u7684\u6307\u4ee4\u6570\u76ee.<\/p>\n<p>\u901a\u5e38\u60c5\u51b5\u4e0b\u7f16\u8bd1\u5668\u4f1a\u81ea\u884c\u9009\u62e9\u4f7f\u7528\u5224\u65ad\u8fd8\u662f\u52a8\u6001\u5206\u652f, \u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u91cd\u5199\u67d0\u4e9b\u5c5e\u6027\u6765\u4fee\u6539\u7f16\u8bd1\u5668\u7684\u884c\u4e3a. \u6211\u4eec\u53ef\u4ee5\u5728\u6761\u4ef6\u8bed\u53e5\u524d\u53ef\u4ee5\u9009\u62e9\u6dfb\u52a0\u4e0b\u9762\u4e24\u4e2a\u5c5e\u6027\u4e4b\u4e00:<br \/>\n<strong>[branch]<\/strong> \u6839\u636e\u6761\u4ef6\u503c\u7684\u7ed3\u679c, \u53ea\u8ba1\u7b97\u5176\u4e2d\u4e00\u8fb9\u7684\u5185\u5bb9, \u4f1a\u4ea7\u751f\u8df3\u8f6c\u6307\u4ee4. \u9ed8\u8ba4\u4e0d\u52a0\u5c5e\u6027\u7684\u6761\u4ef6\u8bed\u53e5\u4e3abranch\u578b.<br \/>\n<strong>[flatten]<\/strong> \u4e24\u8fb9\u7684\u5206\u652f\u5185\u5bb9\u90fd\u4f1a\u8ba1\u7b97, \u7136\u540e\u6839\u636e\u6761\u4ef6\u503c\u9009\u62e9\u5176\u4e2d\u4e00\u8fb9. \u53ef\u4ee5\u907f\u514d\u8df3\u8f6c\u6307\u4ee4\u7684\u4ea7\u751f.<br \/>\n\u7528\u6cd5\u5982\u4e0b:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n[flatten]\r\nif(...){\r\n    ...\r\n}\r\n<\/pre>\n<p><strong>\u5faa\u73af\u8bed\u53e5<\/strong><br \/>\nHLSL\u4e5f\u652f\u6301for,while\u548cdo while\u5faa\u73af.\u548c\u6761\u4ef6\u8bed\u53e5\u4e00\u6837,\u5b83\u53ef\u80fd\u4e5f\u4f1a\u5728\u57fa\u4e8e\u8fd0\u884c\u65f6\u7684\u6761\u4ef6\u503c\u5224\u65ad\u800c\u4ea7\u751f\u52a8\u6001\u5206\u652f,\u4ece\u800c\u5f71\u54cd\u7a0b\u5e8f\u6027\u80fd.\u5982\u679c\u5faa\u73af\u6b21\u6570\u8f83\u5c0f,\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5c5e\u6027[unroll]\u6765\u5c55\u5f00\u5faa\u73af,\u4ee3\u4ef7\u662f\u4ea7\u751f\u66f4\u591a\u7684\u6c47\u7f16\u6307\u4ee4.<\/p>\n<p>\u5faa\u73af\u5c55\u5f00\u5bf9\u7a0b\u5e8f\u6027\u80fd\u6709\u7740\u5f88\u91cd\u8981\u7684\u5f71\u54cd,\u53ef\u4ee5\u51cf\u5c11\u5206\u652f\u9884\u6d4b\u9519\u8bef\u6b21\u6570,\u589e\u52a0\u53d6\u6d88\u6570\u636e\u76f8\u5173\u8fdb\u4e00\u6b65\u5229\u7528\u5e76\u884c\u6267\u884c\u63d0\u9ad8\u901f\u5ea6\u7684\u673a\u4f1a.\u4f46\u662f,\u5e76\u4e0d\u5efa\u8bae\u5927\u5bb6\u8fdb\u884c\u624b\u52a8\u7684\u5faa\u73af\u5c55\u5f00,\u5728\u4ee3\u7801\u4e2d\u8fdb\u884c\u5faa\u73af\u5c55\u5f00\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\u4e0b\u964d,\u4ee3\u7801\u81a8\u80c0.\u5176\u5b9e\u5728\u6211\u4eec\u5f00\u542f\u4e86\u7f16\u8bd1\u5668\u4f18\u5316\u7684\u65f6\u5019,\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5bf9\u6211\u4eec\u7684\u5faa\u73af\u4ee3\u7801\u8fdb\u884c\u5faa\u73af\u5c55\u5f00,\u8ba9\u6211\u4eec\u53ef\u4ee5\u5728\u4fdd\u6301\u4e86\u4ee3\u7801\u53ef\u8bfb\u6027\u7684\u540c\u65f6,\u53c8\u80fd\u4eab\u53d7\u5230\u5faa\u73af\u5c55\u5f00\u5bf9\u6211\u4eec\u7a0b\u5e8f\u6027\u80fd\u7684\u63d0\u9ad8.<\/p>\n<p>PS:\u5728VS\u91cc\u9762\u53ef\u4ee5\u624b\u52a8\u9009\u62e9\u5173\u95ed\u7f16\u8bd1\u5668\u4f18\u5316(\u867d\u7136\u6211\u89c9\u5f97\u4e00\u822c\u6ca1\u4eba\u8fd9\u4e48\u4f5c\u6b7b\u53ed\u2026\u2026),\u5982\u4e0b\u56fe\u6240\u793a:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.caiqinyi.cn\/wp-content\/uploads\/2020\/02\/\u7f16\u8bd1\u5668\u4f18\u5316\u9009\u9879.png\" alt=\"\" width=\"1779\" height=\"1178\" class=\"alignnone size-full wp-image-525\" srcset=\"https:\/\/www.caiqinyi.cn\/wp-content\/uploads\/2020\/02\/\u7f16\u8bd1\u5668\u4f18\u5316\u9009\u9879.png 1779w, https:\/\/www.caiqinyi.cn\/wp-content\/uploads\/2020\/02\/\u7f16\u8bd1\u5668\u4f18\u5316\u9009\u9879-300x199.png 300w, https:\/\/www.caiqinyi.cn\/wp-content\/uploads\/2020\/02\/\u7f16\u8bd1\u5668\u4f18\u5316\u9009\u9879-768x509.png 768w\" sizes=\"(max-width: 1779px) 100vw, 1779px\" \/><\/p>\n<p>\u7528\u6cd5\u5982\u4e0b:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n[unroll]\r\nwhile(...){\r\n    ...\r\n}\r\n<\/pre>\n<p>\u82e5\u6ca1\u6709\u6dfb\u52a0\u5c5e\u6027, \u9ed8\u8ba4\u4f7f\u7528\u7684\u5219\u4e3a[loop].<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u4f7f\u7528HLSL\u5b9e\u73b0\u5149\u7167\u6548\u679c, \u6709\u4e00\u4e9b\u8bbe\u7f6e\u4e0e\u8bed\u6cd5\u7cd6\u9700\u8981\u6ce8\u610f\u4e00\u4e0b~<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/518"}],"collection":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/comments?post=518"}],"version-history":[{"count":10,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/518\/revisions"}],"predecessor-version":[{"id":3616,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/518\/revisions\/3616"}],"wp:attachment":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/media?parent=518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/categories?post=518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/tags?post=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}