View the elapsed execution time of individual statements in your shader to understand where it spends the most time.
Using the Shader Profiler, you can prioritize your optimization efforts by reducing the time taken by your longest running shader statements. The shader profiler helps you optimize your shader by showing you how long each statement took to complete. On devices with a Family 4 or later GPU, a pie chart details which GPU activity your shader does most which provides additional hints about improving performance. With the Update Shaders feature, you can change your shader source live and quickly see how well your shader performs after the change.
Open the Shader Profiler
To use the shader profiler on your project, the
.metallib file must contain source file information that allows for debugging. To properly set up the
In Xcode, navigate to your project's build settings.
Set the Produce debugging information setting for Debug to "Yes, include source code".
Use the shader profiler within a captured
Metal frame. Most commonly, you capture a
Metal frame by clicking the camera button on Xcode's debug bar as covered in Performing a GPU Capture from the Debug Bar.
From the captured frame, open the shader profiler using the steps in Figure 1:
In the Debug navigator, choose View Frame By Performance.
View your render pipelines populated in the list.
Observe the amount of time each one took during the frame.
Profile a Shader
Profile a shader using the follwoing steps, and as annotated in Figure 2:
Expand the render pipeline.
Select the shader you want to profile.
View the shader source in the center pane with the function entry point highlighted.
Examine the times and percentages column.
Because profiling is for performance tuning, most often you'll inspect the render pipeline and shader that took the longest to complete.
In the times and percentages column, the time marking the function entry point is the shader's total elapsed time. Inside of the shader function, a percentage marks each statement and indicates what time (as a percent) of the elapsed time that statement took.
Interpret the GPU Activity Metrics
Next to the percentage of time taken, a pie chart details which activity the GPU is doing most during the statement.
Place your mouse pointer over the dot to bring up the pie chart, as shown in Figure 3:
A high percentage in one GPU activity can indicate a performance bottleneck, and an opportunity for optimization. See the following recommendations based on state:
Time spent in the GPU's arithmatic logic unit. Changing floats to half floats where you can afford it can offer one way to reduce time spent in the ALU.
Time spent waiting on the access of texture memory. You can relieve time spent in Memory by down-sampling textures. Alternatively, if you're not spending much time in Memory, you could have the opportunity to improve the detail of your textures.
Time spent in control structures, for example, a loop in your shader.
Time spent waiting on a required resource before execution could begin.
Update Shaders Live
After making a change to a shader you can apply those updates live using the Update Shaders button highlighted in Figure 4:
The Update Shaders button applies the source changes you make to the same captured
Metal frame. The updates reflect as follows:
The application window is redrawn.
Elapsed time and percentage metrics are recalculated.
Attachments in the Assistant Editor are redrawn.
Because Updating Shaders maintains your view in the captured
Metal frame, you can easily make successive changes to your shader source for iterative optimization.