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.
// 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
