How to structure variables & functionality in shared files in Metal

Oscar de la Hera Gomez
Three flowers that represent Swift, Metal and Xcode with the text "Shared Variables & Functionality" beneath them.

A guide for learning how to create shared Metal variables and functionality that can be used across Metal files in XCode.

We recommend that you clone our Open Source Swift Starter Project, checking out the main branch and carrying out the steps below. The changes can be found on the tutorial/metal/shared-functionality branch.

git clone git@github.com:delasign/swift-starter-project.git

This tutorial first walks you through how to setup files to work independently (Step One) and then consolidates the functionality into a shared file to reduce code redundancy.

Step One: Setup the Project

A screenshot of an iPhone showing two shapes drawn in metal. One is filled and the other is outlined with a stroke that we set in code. This proves the functionality works.

Follow the tutorials below to learn how to create a polygons, with a color fill or with a variable line width.

Step Two: Create the Shared Files

A screenshot of Xcode showing an empty Shared.metal and Shared.h file.

In the folder that holds the shaders, create two new files:

  • Shared.metal as a Metal Shader Source type.
  • Shared.h as a Default - C Header type.

Step Three: Write the Shared Functionality

A screenshot of Xcode showing the how we have added the shared functionality to the Shared.h and Shared.metal file.

Write the shared functionality into the Shared.h and Shared.metal file.

  • The Shared.h file should hold all the constants, structs and function definitions (not the actual functions).
  • The Shared.metal file should include the Shared.h file at the top and should exclusively hold all the function definitions (i.e. the code within the functions defined in the Shared.h file).

Please note it is strongly recommended that both these files use the #pragma once mark to ensure that this header file is only included once.

Step Four: Include the Shared Header File in all Metal Files

A screenshot of Xcode showing how three files include the shared.h file at the top of the metal file. Please note we removed any library (i.e. metal_stdlib) that was formerly included, as this is now within the Shared.h file.

In all Metal files that use the shared functionality, include the Shared.h file at the top of the file.

Please note that you must remove any functionality that is now shared from the metal file.

Step Five: Test

A screenshot of an iPhone showing two shapes drawn in metal. One is filled and the other is outlined with a stroke that we set in code. This proves the functionality works.

Run the code on a device and confirm that it works as expected.

Looking to learn more about things you can do with Swift, Metal and XCode ?

Search our blog to find educational content on learning how to use Swift, Metal and XCode.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details