Skip to content

Using MetalKit part 8

By now, you might have all noticed I am in love with Swift. I am also an avid user of Xcodeplaygrounds. This week we will take our Metal code and put it into a playground. Huzzah, for Metalprototyping in the playgrounds! Let’s start by creating a new Xcode playground for OS X. Once created, click the Show the Assistant editor button and also press Command + 1 to show the Project navigator as well. Your playground should look like this: First thing we do… Read More »Using MetalKit part 8

Using MetalKit part 7

One of our readers contacted me about an apparently weird behavior he was seeing: when running the code from our tutorial the MTLLibrary returned nil after a few hundred draw calls. That made me realize I was not taking into consideration the fact that some of the Metal objects are transient and some are not, according to the Metal documentation. Thanks Mike for bringing this to my attention! To deal with this matter, we need to re-organize… Read More »Using MetalKit part 7

Using MetalKit part 6

Let’s see what are the differences between using the newer MetalKit framework as opposed to using the earlier Metal framework. They are both still coexisting, however, MetalKit introduces a few powerful features such as: Easy texture loading (even asynchronous loading with a few lines of code). Efficient data transfer between Model I/O meshes and Metal buffers. MTKView – a convenient Metal-aware view (we’ll look at it in more detail). I will start by reminding you how our first program looked like in chapter 1: That… Read More »Using MetalKit part 6

Using MetalKit part 5

Last time we described the graphics pipeline and the Metal pipeline. It is time we looked deeper inside the pipeline, and understand how vertices are really processed at a lower level. For this, we need to learn a few 3D math concepts such as transformations. In the world of 3D graphics we often think in terms of 3 or 4 dimensions for our data. As you remember from our previous episodes, location and color were both of type vector_float4 (4-dimensional). In order to draw 3D geometry on the… Read More »Using MetalKit part 5

Using MetalKit part 4

Last time we looked at the Metal Shading Language basics. Before we look into more advanced topics, I just thought now is a good time to revisit what we have learned so far, especially about the graphics pipeline which I admit, I might have gone too fast on that topic (thank you, anonymous reader, for your suggestion and valuable feedback!) Lets look at the graphics pipeline in more detail, and I’ll start with a piece of… Read More »Using MetalKit part 4

Using MetalKit part 3

In the previous part I promised we will learn more about the Metal shading language. Before that, first let’s do some code cleaning and structuring since we are already getting into the habit of doing this from previous episodes. Start by downloading the source code from the previous episode. We want to refactor the huge render() function, to start with. So let’s take the vertex buffer and the render pipeline state outside of the function, and also create 3 new smaller… Read More »Using MetalKit part 3

Using MetalKit part 2

In the first part of this series we introduced the MetalKit framework. Let’s reuse the project from part 1 and pick up where we left off. If you look again at the render() function, it was looking like this: Let’s improve this code a bit. First, since our class subclasses MTKView, it already has its own device so there is no need to declare another one. This lets us reduce the first two lines to just one:… Read More »Using MetalKit part 2

Using MetalKit part 1

The MetalKit framework was announced at the WWDC 2015 and it brings a great deal of improvements and new features for Metal. Meet MTKView, a subclass of NSView/UIView. It comes with an embedded Metal layer, and it also manages the framebuffer and its render target attachments, as well as takes care of the draw loop. Let’s create a new Cocoa Application (since iOS simulators do not support Metal). Make sure only the Swift and Use Storyboards fields are selected. Next, let’s create a new class named MetalView.swift of type NSView (for now).… Read More »Using MetalKit part 1

Introducing the Metal framework

The Metal framework that was announced at WWDC 2014 for iOS and at WWDC 2015 also for OS X and tvOS. Metal is an interface for programming the Graphics Processing Unit (GPU) in your computer. The main advantages of using Metal are: provides the lowest overhead access to the GPU, hence it reduces all bottlenecks usually caused by data transferring between the CPU and GPU in other frameworks. provides up to 10 times the number of draw calls compared to OpenGL. Metal, however, is not cross-platform like OpenGL is, so it is not meant to be a replacement… Read More »Introducing the Metal framework