Skip to content

Instantly share code, notes, and snippets.

@warrenm
warrenm / MinimalMetal.m
Created May 25, 2022 21:52
A one-file example of rendering into a UIView with Metal and CADisplayLink
#import <UIKit/UIKit.h>
#import <QuartzCore/CAMetalLayer.h>
#import <Metal/Metal.h>
@class MetalView;
@protocol MetalViewDelegate
- (void)drawInView:(MetalView *)view;
@end
@warrenm
warrenm / CreateBiplanarIOSurface.m
Last active April 26, 2022 23:05
Example of creating a biplanar (YUV422) IOSurface
NSUInteger width = 1024;
NSUInteger height = 768;
OSType pixelFormat = kCVPixelFormatType_422YpCbCr8BiPlanarFullRange;
NSUInteger plane0BytesPerPixel = 1;
NSUInteger plane1BytesPerPixel = 1;
NSUInteger plane0BytesPerRow = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, plane0BytesPerPixel * width);
NSUInteger plane0AllocSize = IOSurfaceAlignProperty(kIOSurfaceAllocSize, plane0BytesPerRow * height);
NSUInteger plane1BytesPerRow = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, plane1BytesPerPixel * width);
NSUInteger plane1AllocSize = IOSurfaceAlignProperty(kIOSurfaceAllocSize, plane1BytesPerRow * height);
@warrenm
warrenm / MBEMath.h
Created November 22, 2019 20:45
simd matrix utility prototypes for Obj-C
MBE_INLINE simd_float3 MBE_OVERLOAD project(simd_float3 object, simd_float4x4 model, simd_float4x4 projection, int *viewport);
MBE_INLINE simd_float3 MBE_OVERLOAD unproject(simd_float3 window, simd_float4x4 model, simd_float4x4 projection, int *viewport, bool *success);
MBE_INLINE float MBE_OVERLOAD distance(simd_float2, simd_float2);
MBE_INLINE simd_float2 MBE_OVERLOAD lerp(simd_float2, simd_float2, float);
MBE_INLINE simd_float2 MBE_OVERLOAD project(simd_float2, simd_float2);
MBE_INLINE float MBE_OVERLOAD distance(simd_float3, simd_float3);
MBE_INLINE simd_float3 MBE_OVERLOAD lerp(simd_float3, simd_float3, float);
MBE_INLINE simd_float3 MBE_OVERLOAD project(simd_float3, simd_float3);
@warrenm
warrenm / CommonProfile.metal
Created November 14, 2019 01:29
SceneKit's CommonProfile Shader v2 (macOS 10.15)
////////////////////////////////////////////////
// CommonProfile Shader v2
#import <metal_stdlib>
using namespace metal;
#ifndef __SCNMetalDefines__
#define __SCNMetalDefines__
@warrenm
warrenm / CATransform3D+Decomposition.swift
Created September 20, 2018 18:15
Routine to decompose an affine CATransform3D into to scale, rotate, translate components
//
// Copyright 2018 Warren Moore
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
@warrenm
warrenm / Shapes.metal
Created July 22, 2018 09:25
Drawing 2D shapes with Metal
#include <metal_stdlib>
using namespace metal;
struct VertexIn {
float2 position [[attribute(0)]]; // varies per-vertex
float4 color [[attribute(1)]]; // varies per-instance
float3 center [[attribute(2)]]; // varies per-instance
float radius [[attribute(3)]]; // varies per-instance
};
@warrenm
warrenm / ARQLThumbnailGenerator.swift
Created June 12, 2018 06:31
Generating thumbnail images of 3D model files for use with AR QuickLook
import Foundation
import SceneKit
class ARQLThumbnailGenerator {
private let device = MTLCreateSystemDefaultDevice()!
/// Create a thumbnail image of the asset with the specified URL at the specified
/// animation time. Supports loading of .scn, .usd, .usdz, .obj, and .abc files,
/// and other formats supported by ModelIO.