{"id":2364,"date":"2020-09-12T19:27:25","date_gmt":"2020-09-12T11:27:25","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=2364"},"modified":"2020-09-12T21:19:19","modified_gmt":"2020-09-12T13:19:19","slug":"%e8%af%91srp-batcher%ef%bc%9a%e6%8f%90%e5%8d%87%e6%82%a8%e7%9a%84%e6%b8%b2%e6%9f%93%e6%80%a7%e8%83%bd","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=2364","title":{"rendered":"[\u8bd1]SRP Batcher\uff1a\u63d0\u5347\u60a8\u7684\u6e32\u67d3\u6027\u80fd"},"content":{"rendered":"\n<p>\u539f\u6587\u94fe\u63a5\u5730\u5740\uff1a<br><a href=\"https:\/\/blogs.unity3d.com\/2019\/02\/28\/srp-batcher-speed-up-your-rendering\/\">https:\/\/blogs.unity3d.com\/2019\/02\/28\/srp-batcher-speed-up-your-rendering\/<\/a><\/p>\n\n\n\n<p>\u57282018\u5e74\uff0cUnity\u5f15\u5165\u4e86\u4e00\u79cd\u9ad8\u53ef\u5b9a\u5236\u7684\u6e32\u67d3\u6280\u672f\uff0c\u79f0\u4e4b\u4e3aScriptable Render Pipeline\uff08SRP\uff09\u3002<\/p>\n\n\n\n<p>\u5176\u4e2d\u4e00\u90e8\u5206\u662f\u4e00\u4e2a\u540d\u4e3aSRP Batcher\u7684\u65b0\u5e95\u5c42\u6e32\u67d3\u8def\u5f84\uff0c\u5b83\u53ef\u4ee5\u5728\u6e32\u67d3\u8fc7\u7a0b\u4e2d\u63d0\u5347\u6e32\u67d3\u6027\u80fd1.2~4\u500d\u3002<\/p>\n\n\n\n<p>\u53d6\u51b3\u4e8e\u4f7f\u7528\u573a\u666f\uff0c\u5b98\u65b9\u63d0\u4f9b\u4e86\u4e00\u4e2a\u89c6\u9891\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u770b\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/youtu.be\/pUM7ifjGKkM\">https:\/\/youtu.be\/pUM7ifjGKkM<\/a><\/p>\n\n\n\n<p><em>\u89c6\u9891\u8bf7\u81ea\u884c\u722c\u697c\u68af\u89c2\u770b<\/em><\/p>\n\n\n\n<p>\u4ee5\u4e0a\u89c6\u9891\u5c55\u793a\u4e86Unity\u7684\u6700\u574f\u60c5\u51b5\uff1a\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u662f\u52a8\u6001\u7684\uff0c\u5e76\u4f7f\u7528\u4e0d\u540c\u7684\u6750\u8d28\uff08\u989c\u8272\uff0c\u7eb9\u7406\uff09\uff1b\u573a\u666f\u663e\u793a\u4e86\u8bb8\u591a\u76f8\u4f3c\u7684Mesh\uff0c\u4f46\u6bcf\u4e2a\u5bf9\u8c61\u4f7f\u7528\u4e00\u4e2a\u4e0d\u540c\u7684Mesh\uff08\u56e0\u6b64\u4e0d\u80fd\u4f7f\u7528GPU Instance\u6280\u672f\uff09\uff1b \u7ed3\u679c\uff1a\u5728PS4\u4e0a\u6027\u80fd\u63d0\u53474\u500d<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Unity \u548c Materials\uff08\u6750\u8d28\uff09<\/h1>\n\n\n\n<p>Unity\u7f16\u8f91\u5668\u662f\u975e\u5e38\u7075\u6d3b\u7684\u6e32\u67d3\u5f15\u64ce\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u8fd0\u884c\u671f\u95f4\u968f\u65f6\u4fee\u6539\u6750\u8d28\u5c5e\u6027\u3002<\/p>\n\n\n\n<p>Unity\u5386\u53f2\u4e0a\u662f\u57fa\u4e8e&#8211;\u975e\u5e38\u91cf\u7f13\u51b2\u533a\uff08non-constant buffers\uff09\u7684\uff0c\u652f\u6301\u5982DirectX9\u8fd9\u79cd\u56fe\u5f62API\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c \u5f53\u6e32\u67d3\u4f7f\u7528\u4e86\u65b0\u6750\u8d28\u65f6\uff0c\u8fd9\u5c31\u9700\u8981\u5927\u91cf\u7684\u51c6\u5907\u5de5\u4f5c\uff1b\u5373\uff1a\u5728\u573a\u666f\u4e2d\u62e5\u6709\u7684\u6750\u8d28\u8d8a\u591a\uff0cCPU\u63d0\u4ea4\u7ed9GPU\u7684\u6570\u636e\u4e5f\u8d8a\u591a\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2020\/09\/SRP-Batcher-OFF.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u6807\u51c6Unity\u6e32\u67d3\u6d41\u7a0b<\/strong><\/p>\n\n\n\n<p>\u5728Unity\u5185\u90e8\u6e32\u67d3\u8def\u5f84\u4e2d\uff0c\u5f53\u68c0\u6d4b\u5230\u65b0\u6750\u8d28\u65f6\uff0cCPU\u4f1a\u6536\u96c6\u6240\u6709\u5c5e\u6027\u5e76\u5728GPU\u5185\u5b58\u4e2d\u8bbe\u7f6e\u4e0d\u540c\u7684\u5e38\u91cf\u7f13\u51b2\u533a\u3002 GPU\u7f13\u51b2\u533a\u7684\u6570\u91cf\u53d6\u51b3\u4e8eShader\u5982\u4f55\u58f0\u660e\u5176CBUFFER\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">SRP Batcher \u7684\u5de5\u4f5c\u539f\u7406<\/h1>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u4f7f\u7528SRP\u6280\u672f\uff0c\u6211\u4eec\u9700\u8981\u5173\u5fc3\u5e76\u5199\u4e00\u4e9b\u5f15\u64ce\u5e95\u5c42\u4ee3\u7801\u3002\u6211\u4eec\u80fd\u539f\u751f\u5730\u96c6\u6210\u4e00\u4e9b\u65b0\u7684\u8303\u5f0f &#8211; \u6bd4\u5982GPU\u7684\u6570\u636e\u7ba1\u7406\uff08\u751f\u547d\u6743\uff09\uff1b\u76ee\u6807\u662f\u7531\u5927\u91cf\u4e0d\u540c\u6750\u8d28\u3001\u4f46Shader\u53d8\u4f53\u8f83\u5c11\u7684\u573a\u666f\u4e0b\uff0c\u63d0\u5347\u6e32\u67d3\u6027\u80fd\u3002<\/p>\n\n\n\n<p>\u5982\u4e0b\uff0c\u5e95\u5c42\u6e32\u67d3\u8def\u5f84\u53ef\u4ee5\u8ba9\u6750\u8d28\u6570\u636e\u5728GPU\u4e2d\u6301\u4e45\u5b58\u5728\u3002\u5982\u679c\u6750\u8d28\u6ca1\u6709\u53d1\u751f\u53d8\u5316\u7684\u8bdd\uff0c\u5219\u65e0\u9700\u8bbe\u7f6e\u7f13\u51b2\u533a\u5e76\u5c06\u5176\u4e0a\u4f20\u5230GPU\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e13\u7528\u4ee3\u7801\u5feb\u901f\u66f4\u65b0\u6307\u5b9a\u6570\u636e\u81f3\u5927\u91cf\u7684GPU-Buff\u4e0a\u3002\u65b0\u7684\u6d41\u7a0b\u56fe\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2020\/09\/image5-3.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>SRP Batcher \u6e32\u67d3\u6d41\u7a0b<\/strong><\/p>\n\n\n\n<p>\u8fd9\u91cc\uff0cCPU\u53ea\u5904\u7406\u5185\u7f6e\u7684\u5f15\u64ce\u5c5e\u6027\uff0c\u6bd4\u5982\u57fa\u672c\u8f6c\u6362\u77e9\u9635\uff0c\u6240\u6709\u7684\u6750\u8d28\u88ab\u6258\u7ba1\u81f3GPU\u7684CBuffer\u4e0a\uff0c\u968f\u65f6\u53ef\u4ee5\u88ab\u8c03\u7528\uff1b<\/p>\n\n\n\n<p>\u6027\u80fd\u63d0\u5347\u6765\u81ea\u4e8e\uff1a<\/p>\n\n\n\n<p>1\u3001\u6240\u6709\u6750\u8d28\u6570\u636e\u90fd\u88ab\u6258\u7ba1\u81f3GPU\u5185\u5b58\u4e2d<\/p>\n\n\n\n<p>2\u3001\u6bcf\u4e2a\u5bf9\u8c61\u7684\u57fa\u7840\u6570\u636e\uff08\u4f8b\u5982Position\/Scale\/Rotation&#8230;) \u88ab\u6258\u7ba1\u81f3GPU CBuffer\u4e2d<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SRP Batcher \u517c\u5bb9\u6027<\/h2>\n\n\n\n<p>\u6bcf\u4e2a\u7528SRP Batcher\u6e32\u67d3\u7684\u5bf9\u8c61\u67092\u4e2a\u8981\u6c42\uff1a<\/p>\n\n\n\n<p>1\u3001\u53ea\u652f\u6301Mesh\uff0c\u4e0d\u80fd\u662f\u7c92\u5b50\u6216\u8005 SkinnedMesh<br>2\u3001\u7528SRP Batcher\u517c\u5bb9\u7684Shader\uff0c\u4f8b\u5982HDRP\u548cURP(LWRP)\u4e2d\u7684Lit\uff0cUnlit\u7cfb\u5217Shader<\/p>\n\n\n\n<p>\u90a3\u5982\u4f55SRP Batcher\u517c\u5bb9\u5462\uff1f<\/p>\n\n\n\n<p>1\u3001\u5f15\u64ce\u5185\u5efa\u7684\u5c5e\u6027\u5217\u8868\uff0c\u542b\u6709\u540d\u4e3aUnityPerDraw\u7684CBUFFER\u58f0\u660e\uff1b<br>\u4f8b\u5982\uff1a unity_ObjectToWorld, unity_SHAr &#8230;<\/p>\n\n\n\n<p>\u6ce8\uff1a\u5728com.unity.render-pipelines.universal@7.3.1\\ShaderLibrary\\UnityInput.hlsl\u4e2d\uff0c\u53ef\u4ee5\u627e\u5230\u5982\u4e0b\u4ee3\u7801\u7247\u6bb5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Block Layout should be respected due to SRP Batcher\nCBUFFER_START(UnityPerDraw)\n\/\/ Space block Feature\nfloat4x4 unity_ObjectToWorld;\nfloat4x4 unity_WorldToObject;\nfloat4 unity_LODFade; \/\/ x is the fade value ranging within [0,1]. y is x quantized into 16 levels\nreal4 unity_WorldTransformParams; \/\/ w is usually 1.0, or -1.0 for odd-negative scale transforms\n\n\/\/ Light Indices block feature\n\/\/ These are set internally by the engine upon request by RendererConfiguration.\nreal4 unity_LightData;\nreal4 unity_LightIndices[2];\n\nfloat4 unity_ProbesOcclusion;\n\n\/\/ Reflection Probe 0 block feature\n\/\/ HDR environment map decode instructions\nreal4 unity_SpecCube0_HDR;\n\n\/\/ Lightmap block feature\nfloat4 unity_LightmapST;\nfloat4 unity_DynamicLightmapST;\n\n\/\/ SH block feature\nreal4 unity_SHAr;\nreal4 unity_SHAg;\nreal4 unity_SHAb;\nreal4 unity_SHBr;\nreal4 unity_SHBg;\nreal4 unity_SHBb;\nreal4 unity_SHC;\nCBUFFER_END<\/code><\/pre>\n\n\n\n<p>2\u3001Shader\u7684\u5c5e\u6027\u58f0\u660e\uff0c\u542b\u6709\u540d\u4e3a UnityPerMaterial\u7684CBUFFER\u58f0\u660e<br><br>\u6ce8\u610f1\uff1a\u9009\u4e2dShader\u6587\u4ef6\uff0c\u5728Inspector\u9762\u677f\u4e2d\uff0c\u5982\u679cSRP Batcher\u4e3acompatible\uff0c\u5219\u4e3a\u517c\u5bb9\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f2\uff1aUnity\u53ef\u540c\u65f6\u5904\u7406\u5e76\u6e32\u67d3 SRP Batcher\u517c\u5bb9\u4e0e\u4e0d\u517c\u5bb9\u7684\u5bf9\u8c61<br>\u517c\u5bb9\u7684\u4f7f\u7528SRP Batcher\u6e32\u67d3\uff0c\u4e0d\u517c\u5bb9\u7684\u7528\u6807\u51c6SRP\u4ee3\u7801\u6e32\u67d3<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2020\/09\/Screen-Shot-2019-02-27-at-3.47.54-PM.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u4f7f\u7528\u5feb\u901fSRP Batcher \u6e32\u67d3\u7684\u4e3a\uff1a<br>\u4f7f\u7528SRP Batcher\u517c\u5bb9Shader\u6e32\u67d3\u7684Mesh\u5bf9\u8c61<\/p>\n\n\n\n<p>\u4e0d\u4f7f\u7528\u5feb\u901fSRP Batcher \u6e32\u67d3\u7684\u4e3a\uff1a<br>1\u3001\u975eMesh\u5bf9\u8c61\uff08\u4f8b\u5982SkinnedMesh\uff09<br>2\u3001\u4f7f\u7528\u4e86\u975e\u517c\u5bb9Shader\uff08\u7528Inspector\u770b\u4e00\u4e0b\u5c31\u77e5\u9053\u4e86\uff09<br>3\u3001\u4f7f\u7528\u4e86MaterialPropertyBlock\u6280\u672f\u7684\u5bf9\u8c61<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5256\u6790\u7684\u827a\u672f<\/h2>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528SRPBatcherProfiler.cs\u811a\u672c\u6765\u6d4b\u8bd5\uff0c\u5c06\u6b64\u811a\u672c\u653e\u573a\u666f\u4e2d\uff0c\u6309F8\u5207\u6362\u663e\u793a\uff0c\u6216\u8005F9\u6253\u5f00\u3001\u5173\u95ed\u4fe1\u606f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2020\/09\/image4-4.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u6bd4\u5982 1.31ms SRP Batcher\u65f6\uff0c\u53ef\u80fd\u5728\u4e3b\u7ebf\u7a0b\u4e0a\u82b1\u4e860.3ms,\u9001\u5230GPU\u4e0a\u6e32\u67d3\u82b1\u4e861ms<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5256\u6790\u5c55\u793a\u7684\u4fe1\u606f<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2020\/09\/Screen-Shot-2019-02-27-at-3.48.19-PM.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u4ece\u8fd9\u91cc\u4e0b\u8f7d\u8fd9\u4e2a\u811a\u672c<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/Unity-Technologies\/SRPBatcherBenchmark.git\">https:\/\/github.com\/Unity-Technologies\/SRPBatcherBenchmark.git<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6d4b\u8bc4<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2020\/09\/image8.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/unity3d.com\/book-of-the-dead\" target=\"_blank\">Book of the Dead<\/a><\/p>\n\n\n\n<p>HDRP PS4\uff0c 1.47\u500d\u63d0\u5347<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2020\/09\/image2.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/unity.com\/fps-sample?_ga=2.63048879.1641473194.1599909678-1522115383.1583500167\" target=\"_blank\">FPS Sample<\/a>, HDRP, PC DirectX 11. X1.23\u500d\u63d0\u5347<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blogs.unity3d.com\/wp-content\/uploads\/2019\/02\/image13.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/Verasl\/BoatAttack\" target=\"_blank\">Boat Attack<\/a>, LWRP, PlayStation 4. \u63d0\u53472.13\u500d<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u652f\u6301\u7684\u5e73\u53f0<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2020\/09\/Screen-Shot-2019-02-27-at-3.48.40-PM.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u6700\u4f73\u7684\u59ff\u52bf\u63a5\u5165SRP Batcher\uff1f<\/h2>\n\n\n\n<p>\u4f7f\u7528 SRPBatcherProfiler.cs\uff0c\u7136\u540e\u68c0\u67e5SRP Batcher\u662f\u5426\u6253\u5f00\uff0c\u67e5\u770b \u201cStandard codePath\u201d\u6700\u597d\u4e3a0\uff0c\u90fd\u5e94\u5728\u201cSRP Batcher\u201d\u6e32\u67d3\u8def\u5f84\u4e2d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u68c0\u67e5SRP Batcher\u7684\u6548\u7387\uff1f<\/h2>\n\n\n\n<p>\u4e86\u89e3SRP Batcher\u4e0a\u4e0b\u6587\u4e2d\u7684\u201cBatcher\u201d\u975e\u5e38\u91cd\u8981\uff01<br><\/p>\n\n\n\n<p>\u6211\u4eec\u503e\u5411\u4e8e\u51cf\u5c11DrawCall\u7684\u6570\u91cf\uff0c\u771f\u6b63\u7684\u539f\u56e0\u662f\u5f15\u64ce\u5728\u7ed8\u5236\u4e4b\u524d\u9700\u8981\u505a\u8bb8\u591a\u51c6\u5907\u5de5\u4f5c\u3002\u771f\u6b63\u7684CPU\u6210\u672c\u6765\u81ea\u4e8e\u51c6\u5907\u5de5\u4f5c\uff08\u8bbe\u7f6e\u5de5\u4f5c\uff09\uff0c\u800c\u975eDrawCall\u672c\u8eab\uff08\u8fd9\u53ea\u662f\u8981\u653e\u7f6eGPU\u547d\u4ee4\u7f13\u51b2\u533a\u7684\u4e00\u4e9b\u5b57\u8282\u800c\u5df2\uff09<\/p>\n\n\n\n<p>SRP Batcher\u5e76\u4e0d\u4f1a\u51cf\u5c11DrawCall\u6570\u91cf\uff0c\u5b83\u964d\u4f4e\u7684\u662fDrawCall\u4e4b\u95f4CPU\u7684\u5de5\u4f5c\u5de5\u4f5c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2020\/09\/image3-5.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u5de6\u4fa7\u4e3aSRP\u6e32\u67d3\u5faa\u73af\uff0c\u53f3\u4fa7\u4e3aSRP Batcher\u5faa\u73af\u3002\u5728SRP Batcher\u4e0a\u4e0b\u6587\u4e2d\uff0c \u201cBatcher\u201d\u53ea\u662f\u5904\u7406 \u7ed1\u5b9a\u3001\u7ed8\u5236\u3001\u7ed1\u5b9a\u3001\u7ed8\u5236\u7684CPU\u547d\u4ee4\u5e8f\u5217<\/p>\n\n\n\n<p>\u5728\u6807\u51c6SRP\u4e2d\uff0c\u6bcf\u79cd\u65b0\u6750\u8d28\u90fd\u9700\u8981\u8c03\u7528\u5f88\u901f\u7684SetShaderPass<br>\u800c\u5728SRP Batcher\u4e0a\u4e0b\u6587\u4e2d\uff0c\u4f1a\u4e3a\u6bcf\u4e2a\u65b0Shader\u53d8\u4f53\u8c03\u7528SetShaderPass\u3002<br>\uff08 In standard SRP, the slow SetShaderPass is called for each new material. In SRP Batcher context, the SetShaderPass is called for each new shader variant. \uff09<\/p>\n\n\n\n<p>\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u6700\u597d\u8ba9\u8fd9\u4e9bBatcher\u5c3d\u53ef\u80fd\u5730\u5927\uff01<br>\u56e0\u6b64\u5e94\u8be5\u907f\u514d\u4fee\u6539Shader\u53d8\u4f53\uff0c\u5982\u679c\u4ed6\u4eec\u4f7f\u7528\u4e86\u76f8\u540c\u7684Shader\uff0c\u5219\u53ef\u4ee5\u5927\u91cf\u4e0d\u540c\u7684Material\u3002<br>\uff08 To get maximum performance, you need to keep those batches as large as possible. So you need to avoid any shader variant change, but you can use any number of different Materials if they\u2019re using the same shader. \uff09<\/p>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528FrameDebugger\u67e5\u770bSRP Batcher\u4e2d\u201cbatcher\u201d\u7684\u957f\u5ea6<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2020\/09\/image10-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u67e5\u770b\u5de6\u4fa7SRP Batcher\u4e8b\u4ef6<\/p>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u67e5\u770b\u88ab\u4e0a\u4e00\u4e2a\u6279\u6b21\u7834\u574f\u7684\u539f\u56e0\uff0c\u4f8b\u5982\u4f7f\u7528\u4e0d\u540cShader\u7684Keywords<br>\uff08 See the SRP Batch event on the left. See also the size of the batch, which is the number of Draw Calls (109 here). That\u2019s a pretty efficient batch. You also see the reason why the previous batch had been broken (\u201cNode use different shader keywords\u201d). It means the shader keywords used for that batch are different than the keywords in the previous batch. It means that the shader variant has changed, and we have to break the batch. \uff09<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2020\/09\/image12-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u4e0a\u56fe\u6240\u793a\uff0cDrawCalls\u4ec5\u4e3a2\uff0c\u53ef\u80fd\u610f\u5473\u7740\u6709\u592a\u591a\u4e0d\u540c\u7684Shader\u53d8\u4f53\uff0c\u6700\u597d\u7528\u6700\u5c11\u7684Keywords\u7f16\u5199\u901a\u7528\u7684Shader\uff0c\u4e0d\u5fc5\u62c5\u5fc3\u5728\u5c5e\u6027\u53c2\u6570\u4e2d\u8f93\u5165\u4e86\u591a\u5c11\u53c2\u6570\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PreMaterial \u53d8\u91cf<\/h2>\n\n\n\n<p>\u6240\u6709 PreMaterial \u6570\u636e\u5e94\u5728\u4e00\u4e2a\u540d\u4e3a UnityPerMaterial\u7684CBUFFER\u4e2d\u58f0\u660e\u3002<\/p>\n\n\n\n<p>\u4ec0\u4e48\u662fPerMaterial\u6570\u636e\uff1f<\/p>\n\n\n\n<p>\u6bd4\u5982\u5728Shader\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\uff08\u5404\u79cd\u5c5e\u6027\uff09\uff0c\u7f8e\u672f\u5728\u6750\u8d28\u9762\u677f\u8c03\u7528\u7684\u5404\u79cd\u53d8\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Properties\n{\n_Color1 (\"Color 1\", Color) = (1,1,1,1)\n_Color2 (\"Color 2\", Color) = (1,1,1,1)\n}\n\nfloat4 _Color1;\nfloat4 _Color2;<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u7f16\u8bd1\u6b64Shader\uff0c\u5219\u4e3aSRP Batcher\u4e0d\u517c\u5bb9\uff0c\u63d0\u793a\uff1a<\/p>\n\n\n\n<p>SRP Batcher &#8211; not compatible<br>Material property is found in another cbuffer than &#8220;UnityPerMaterial&#8221; (_Color1)<\/p>\n\n\n\n<p>\u8fd9\u6837\u4fee\u6b63\u5b83\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CBUFFER_START(UnityPerMaterial)\n  float4 _Color1; \n  float4 _Color2; \nCBUFFER_END<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">PerObject \u53d8\u91cf<\/h2>\n\n\n\n<p>SRPBatcher\u9700\u8981\u4e00\u4e2a\u7279\u6b8a\u7684CBUFFER\uff1a&#8221;UnityPerDraw&#8221;<br>\u8be5CBUFFER\u9700\u8981\u5305\u542b\u6240\u6709Unity\u5f15\u64ce\u7684\u5185\u5efa\u53d8\u91cf<\/p>\n\n\n\n<p>UnityPerDraw CBUFFER\u53d8\u91cf\u7684\u53d8\u91cf\u58f0\u660e\u6b21\u5e8f\u5f88\u91cd\u8981\uff0c\u6240\u6709\u53d8\u91cf\u8981\u9075\u5b88\u79f0\u4e4b\u4e3a\u3010\u5757\u3011\u7684\u5e03\u5c40 ( block feature layout )<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5e38\u7528\u7684\u4e00\u4e9b\u8f6c\u6362\u77e9\u9635\u9700\u8981\u6309\u4ee5\u4e0b\u6b21\u5e8f\u5305\u542b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>float4x4 unity_ObjectToWorld; \nfloat4x4 unity_WorldToObject; \nfloat4 unity_LODFade; \nfloat4 unity_WorldTransformParams;<\/code><\/pre>\n\n\n\n<p>\u6ce8\uff1a\u719f\u6089OpenGL ES\u4e2d\u7684 \u201c\u7edf\u4e00\u53d8\u91cf\u7f13\u51b2\u533a\u5bf9\u8c61\u201d\u6982\u5ff5\uff0c\u5373\u53ef\u77e5\u9053\uff0c\u6b64\u7f13\u51b2\u533a\u662f\u6309\u5757\u8fdb\u884c\u7684\uff0cUniform \u5757\u5e03\u5c40\u3002<a href=\"https:\/\/learnopengl-cn.github.io\/04%20Advanced%20OpenGL\/08%20Advanced%20GLSL\/\">\u70b9\u6b64\u67e5\u9605<\/a><\/p>\n\n\n\n<p>\u9700\u8981\u4e0d\u9700\u8981\u5219\u4e0d\u9700\u8981\u58f0\u660e\u67d0\u4e9b\u529f\u80fd\uff0c UnityPerDraw\u4e2d\u6240\u6709\u5185\u5efa\u53d8\u91cf\u5e94\u4e3afloat4x4<br>\u4e5f\u53ef\u4ee5\u4f7f\u7528real4x4\uff0816\u4f4d\u7f16\u7801\uff09\u6765\u7701\u4e00\u4e9bGPU\u5e26\u5bbd\uff0c\u4f46\u662f\u5e76\u4e14\u6240\u6709\u7684UnityPerDraw\u53d8\u91cf\u90fd\u53ef\u4ee5\u7528real4x4\uff0c\u53ef\u4ee5\u67e5\u4e0b\u9762\u7684\u8868\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2020\/09\/Screen-Shot-2019-02-27-at-3.50.52-PM.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Tips\uff1a\u5982\u679c\u4e00\u4e2a feature block \u88ab\u58f0\u660e\u4e3a real4(half4)\uff0c\u5219\u6240\u6709\u5176\u5b83\u5757\u4e5f\u5e94\u58f0\u660e\u4e3areal4x4<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5c55\u671b<\/h2>\n\n\n\n<p>\u6211\u4eec\u4ecd\u7136\u7ee7\u7eed\u5728\u6e32\u67d3\u8fc7\u7a0b\uff08\u5c24\u5176\u662fShadow\u548cDepth\uff09\u4e2d\u589e\u52a0Batch\u7684\u5927\u5c0f\u6765\u6539\u8fdbSRP Batcher<\/p>\n\n\n\n<p>\u901a\u8fc7SRP Batcher\u6dfb\u52a0\u81ea\u52a8GPU Instancing\u529f\u80fd\uff0c\u4eceMegaCityDemo\u4e2d\u4f7f\u7528\u65b0\u7684DOTS\u6e32\u67d3\u5668\u5f00\u59cb\uff0cUnity\u7f16\u8f91\u5668\u4ece10FPS\u63d0\u5347\u523050FPS\uff08\u5439\u725b\uff09<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a\u5e26\u6709DOTS\u7684SRP Batcher\u8fd8\u5728\u5f00\u53d1\u4e2d\uff0c\u4e0d\u8981\u6025<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\u5730\u5740\uff1ahttps:\/\/blogs.unity3d.com\/2019\/02\/28\/srp-batcher [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/2364"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2364"}],"version-history":[{"count":9,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/2364\/revisions"}],"predecessor-version":[{"id":2413,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/2364\/revisions\/2413"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}