Theme Catppuccin
Make yourself at homeCatppuccin

A softer light for long learning sessions.

Appearance

Loading your preference…

Dark palette

Choose a dark palette to try it.

Light uses Latte. Your choices stay in this browser.

Back to your source shelf

Inside the companion

Metal · Copper Orchard

Connect a Cocoa window to a C++ Metal renderer and verify GPU results.

Project files 7

Own the first Metal objects

A metal-cpp device and command-queue probe using scoped ownership helpers.

22 lines metal_cpp_device.cpp
// Optional metal-cpp ownership probe, separate from the SDK-only .mm program.
// Supply Apple's metal-cpp headers; see README. Define these in ONE translation unit.
#define NS_PRIVATE_IMPLEMENTATION
#define MTL_PRIVATE_IMPLEMENTATION
#include <Foundation/Foundation.hpp>
#include <Metal/Metal.hpp>

#include <iostream>

int main() {
    auto pool = NS::TransferPtr(NS::AutoreleasePool::alloc()->init());
    auto device = NS::TransferPtr(MTL::CreateSystemDefaultDevice());
    if (!device) { std::cerr << "No Metal device\n"; return 1; }
    auto queue = NS::TransferPtr(device->newCommandQueue());
    if (!queue) { std::cerr << "Cannot create command queue\n"; return 1; }
    // commandBuffer() is borrowed/autoreleased. Retain it to express our ownership.
    auto commands = NS::RetainPtr(queue->commandBuffer());
    if (!commands) { std::cerr << "Cannot create command buffer\n"; return 1; }
    std::cout << device->name()->utf8String() << '\n';
    // Nothing submitted; reverse declaration order releases commands, queue, device, pool.
}

This is the file included with the course. Copy and download give you its complete source.

Taking your work offline? Download all course companions