默认情况下,Unity 的运行时着色器加载行为如下:
这种行为的好处是着色器变体没有前期的 GPU 内存使用或加载时间。缺点是第一次使用变体时可能会出现明显的停顿,因为图形驱动程序必须在 GPU 上创建着色器程序并执行任何额外的工作。
为避免在性能开销大时出现明显的停顿,Unity 可以要求图形驱动程序在首次需要着色器变体之前创建它们的 GPU 表示形式。这称为预热。
警告:在选择如何执行预热之前,请查看有关图形 API 支持的说明。在 DX12、Vulkan 和 Metal 等现代图形 API 上,只有实验性的 ShaderWarmup API 完全支持,因为它允许您指定顶点格式。使用其他方法可能会导致浪费工作和 GPU 内存,而不会避免停顿。
您可以按照以下方式执行预热:
注意:
性能分析器标记 Shader.Parse
用于表示 Unity 将创建的着色器变量数据表示发送到 GPU。性能分析器标记 CreateGPUProgram
用于表示将着色器程序上传到 GPU 并等待 GPU 执行任何所需工作。
有关使用 Unity 性能分析器的信息,请参阅对应用程序进行性能分析。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.