<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/Shader/compile(as:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI6ShaderV7compile2asyAC9UsageTypeV_tYaKF"
  },
  "title" : "compile(as:)"
}
-->

# compile(as:)

Attempts to asynchronously compile a shader function, to
minimize the chance of stalling when it is first used for
rendering.

```
func compile(as type: Shader.UsageType) async throws
```

## Parameters

`type`

how the shader will eventually be used.

## Discussion

Before being available for rendering each shader must be
compiled for the current GPU. By default this happens on first
use, but that may cause that frame to be delayed waiting for
the compiler, i.e. cause a visible “glitch” in animations.

Calling this method for each shader before it’s first used
allows the necessary compilation to happen ahead of time,
eliminating the delay on the first actual use (provided
`compile()` completes before the first use of the shader).

For compilation to be successful the specified usage type must
match how the shader is eventually used to render, and its
current argument values must match the types of the arguments
used when rendering (however array and data sizes may be
empty).

For example, to compile a fill shader asynchronously when your
app launches:

```
Task {
    let shader = ShaderLibrary.example(.color(.clear), .float(0))
    try! await shader.compile(as: .shapeStyle)
}
```

Here the MSL shader function `example` takes two uniform
arguments: a color and a numeric value. The placeholder values
are replaced with actual values when using the shader, in this
case to fill a circle:

```
Circle().fill(
    ShaderLibrary.example(.color(.orange), .float(32)))
```

> Throws: an error describing why compilation failed.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)