<!--
{
  "availability" : [
    "macOS: 26.0.0 -",
    "visionOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CompositorServices",
  "identifier" : "/documentation/CompositorServices/LayerRenderer/Drawable/addRenderContext()",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Compositor Services",
      "CompositorServices"
    ],
    "preciseIdentifier" : "c:@F@cp_drawable_add_mtl4_render_context"
  },
  "title" : "addRenderContext()"
}
-->

# addRenderContext()

Adds and returns a render context to a `LayerRenderer.Drawable` that draws any content required by the compositor.

```
func addRenderContext() -> LayerRenderer.Drawable.RenderContext
```

## Discussion

Call `renderContext.drawMaskOnStencilAttachment(self:commandEncoder:value:)` on the returned [`LayerRenderer.Drawable.RenderContext`](/documentation/CompositorServices/LayerRenderer/Drawable/RenderContext) to render only the necessary pixels.
You can only use the [`LayerRenderer.Drawable.RenderContext`](/documentation/CompositorServices/LayerRenderer/Drawable/RenderContext) API in the context of layered layout rendering or on platforms that only render one view, such as the Simulator.
The `RenderContext` uses the [`deviceAnchor`](/documentation/CompositorServices/LayerRenderer/Drawable/deviceAnchor) set in [`cp_drawable_set_device_anchor`](/documentation/CompositorServices/cp_drawable_set_device_anchor).
Set the `deviceAnchor` in the layer renderer drawable before calling [`addRenderContext(commandBuffer:)`](/documentation/CompositorServices/LayerRenderer/Drawable/addRenderContext(commandBuffer:)).

---

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)