Adjusting the level of detail using Metal mesh shaders
At a glance
| Item | Summary |
|---|---|
| Purpose | Choose and render meshes with several levels of detail using object and mesh shaders. |
| App architecture | A C++, C/Objective-C header, Metal, Objective-C, Objective-C++, Python sample bundle with entry-bearing project variants MeshShadersMetalCPP, metal-cpp, each leading to Metal / MetalKit APIs. |
| Main patterns | Delegate or data-source callbacks, Adapter |
| Project style | 13 scanned source file(s) across C++, C/Objective-C header, Metal, Objective-C, Objective-C++, Python, organized around ranked entry, type, and file boundaries. |
| Execution model | No structured execution marker indexed; callback threading requires source review. |
| State/event model | No structured observation or publisher-scheduling marker indexed. |
| Key frameworks/packages | TargetConditionals.h, Foundation, simd, CoreGraphics, metal_stdlib; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── MeshShadersMetalCPP/
│ ├── Application/
│ │ ├── main.m
│ │ ├── AAPLAppDelegate.h
│ │ ├── AAPLAppDelegate.m
│ │ ├── AAPLViewController.h
│ │ ├── AAPLViewController.m
│ │ ├── WindowSceneDelegate.h
│ │ └── WindowSceneDelegate.m
│ ├── Adapter/
│ │ ├── AAPLRendererAdapter.mm
│ │ └── AAPLRendererAdapter.h
│ └── Renderer/
│ ├── AAPLShaders.metal
│ └── AAPLRenderer.cpp
└── metal-cpp/
└── SingleHeader/
└── MakeSingleHeader.py
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: C++, C/Objective-C header, Metal, Objective-C, Objective-C++, Python.
- The verified tree contains 8 project/configuration file(s) and 11 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["MeshShadersMetalCPP"]
V2["metal-cpp"]
Boundary["Metal / MetalKit APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
MeshShadersMetalCPP/Application/main.m:13 — architecture anchor
#if TARGET_OS_OSX
int main(int argc, const char* argv[])
{
return NSApplicationMain(argc, argv);
}
#endifInterpretation
The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.
Ownership and state
classDiagram
AAPLAppDelegate o-- PlatformWindow : window
WindowSceneDelegate o-- UIWindow : window
Ownership evidence
MeshShadersMetalCPP/Application/AAPLAppDelegate.h:29 — stored dependency or nearest verified ownership anchor
#if TARGET_OS_OSX
@property (strong, nonatomic) PlatformWindow* window;
#endif| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
AAPLAppDelegate |
PlatformWindow (window) |
retains or copies an assigned value | Header-visible collaborators |
WindowSceneDelegate |
UIWindow (window) |
retains or copies an assigned value | Header-visible collaborators |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
No source-visible execution, scheduling, or synchronization boundary was found in the indexed source.
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
State propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| Source import | TargetConditionals.h |
The cited file imports this module; runtime use and architectural role are not inferred. | MeshShadersMetalCPP/Application/AAPLAppDelegate.h:8 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | MeshShadersMetalCPP/Adapter/AAPLRendererAdapter.h:8 |
| Source import | simd |
The cited file imports this module; runtime use and architectural role are not inferred. | MeshShadersMetalCPP/Renderer/AAPLRenderer.cpp:9 |
| Source import | CoreGraphics |
The cited file imports this module; runtime use and architectural role are not inferred. | MeshShadersMetalCPP/Adapter/AAPLRendererAdapter.h:9 |
| Source import | metal_stdlib |
The cited file imports this module; runtime use and architectural role are not inferred. | MeshShadersMetalCPP/Renderer/AAPLShaders.metal:8 |
| Source import | MetalKit |
The cited file imports this module; runtime use and architectural role are not inferred. | MeshShadersMetalCPP/Renderer/AAPLRenderer.cpp:8 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
MeshShadersMetalCPP/Application/AAPLAppDelegate.h:26 — representative type boundary
@interface AAPLAppDelegate : PlatformAppDelegate
#if TARGET_OS_OSX
@property (strong, nonatomic) PlatformWindow* window;
#endif
@end| Type | Responsibility | Depends on or conforms to |
|---|---|---|
AAPLAppDelegate |
Receives callback-driven events | PlatformAppDelegate |
AAPLRendererAdapter |
Translates one interface or representation into another | NSObject |
AAPLViewController |
View lifecycle, callbacks, and feature coordination | PlatformViewController |
WindowSceneDelegate |
Receives callback-driven events | UIResponder, UIWindowSceneDelegate |
vertexOut |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
pointVertexOut |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
payload_t |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
primOut |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
fragmentIn |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
HeaderPrefix |
Owns feature behavior and collaborator lifecycle | object |
No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
window (MeshShadersMetalCPP/Application/AAPLAppDelegate.h:29) |
header-visible |
The declaration is exposed to translation units that import the header. | Inference: declare a contract needed by other Objective-C/C translation units. |
window (MeshShadersMetalCPP/Application/WindowSceneDelegate.h:17) |
header-visible |
The declaration is exposed to translation units that import the header. | Inference: declare a contract needed by other Objective-C/C translation units. |
MakeSingleHeader (metal-cpp/SingleHeader/MakeSingleHeader.py:249) |
language/file boundary |
Visibility follows the language’s file, module, and export rules rather than Swift access modifiers. | Inference: the language’s file or module boundary is sufficient for this sample collaboration. |
Reference code
MeshShadersMetalCPP/Application/AAPLAppDelegate.h:29 — representative boundary
#if TARGET_OS_OSX
@property (strong, nonatomic) PlatformWindow* window;
#endifSwift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Translates one interface or representation into another | AAPLRendererAdapter |
The source’s Adapter suffix makes this role explicit. |
| View lifecycle, callbacks, and feature coordination | AAPLViewController |
The source’s Controller suffix makes this role explicit. |
| Receives callback-driven events | AAPLAppDelegate, WindowSceneDelegate |
The source’s Delegate suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Delegate or data-source callbacks | MeshShadersMetalCPP/Application/AAPLAppDelegate.h:26 |
Callback protocols invert event delivery back into the sample’s owner. |
| Adapter | MeshShadersMetalCPP/Adapter/AAPLRendererAdapter.h:13 |
An adapter-named type translates interfaces or representations. |
Main application flow
sequenceDiagram
participant AAPLViewController
participant _viewdevice
participant NSView
participant UIView
participant UIColor
participant AAPLRendererAdapter
participant _renderer
AAPLViewController->>AAPLViewController: viewDidLoad()
AAPLViewController->>_viewdevice: supportsFamily()
AAPLViewController->>_viewdevice: supportsFamily()
AAPLViewController->>NSView: initWithFrame()
AAPLViewController->>UIView: initWithFrame()
AAPLViewController->>UIColor: initWithRed()
AAPLViewController->>AAPLRendererAdapter: initWithMtkView()
AAPLViewController->>_renderer: drawableSizeWillChange()
Reference code
MeshShadersMetalCPP/Application/AAPLViewController.m:57 — viewDidLoad()
- (void)viewDidLoad
{
[super viewDidLoad];
_view = (MTKView *)self.view;
_view.device = MTLCreateSystemDefaultDevice();
NSAssert(_view.device, @"The app couldn't get a GPU device.");
NSLog(@"Device: %@", _view.device.name);
// Check whether the GPU device supports mesh shaders.
if (![_view.device supportsFamily:MTLGPUFamilyMac2] &&
![_view.device supportsFamily:MTLGPUFamilyApple7])
{
NSLog(@"This GPU device doesn't support mesh shaders.");
// Create an empty view and return.
#if TARGET_OS_OSX
self.view = [[NSView alloc] initWithFrame:_view.frame];
#elif TARGET_OS_IPHONE
self.view = [[UIView alloc] initWithFrame:_view.frame];
self.view.backgroundColor = [[UIColor alloc] initWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
#endif
return;
}
_renderer = [[AAPLRendererAdapter alloc] initWithMtkView:_view];
NSAssert(_renderer, @"Renderer failed initialization");
[_renderer drawableSizeWillChange:_view.bounds.size];
// Configure the view to use this class instance to handle the draw and resize events.
_view.delegate = self;
}Naming conventions
- Types: Adapter: AAPLRendererAdapter; Controller: AAPLViewController; Delegate: AAPLAppDelegate, WindowSceneDelegate.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
initWithMtkView,dealloc,device,drawInMTKView,drawableSizeWillChange,setRotationSpeed,setTranslation,setLODChoice. - Files:
MeshShadersMetalCPP/Application/AAPLAppDelegate.h,MeshShadersMetalCPP/Adapter/AAPLRendererAdapter.mm,MeshShadersMetalCPP/Application/AAPLAppDelegate.m,MeshShadersMetalCPP/Adapter/AAPLRendererAdapter.h,MeshShadersMetalCPP/Application/AAPLViewController.h,MeshShadersMetalCPP/Application/AAPLViewController.m.
Architecture takeaways
mainis the main source-visible entry or composition anchor for this sample.- Framework work reaches simd, CoreGraphics, Metal, MetalKit through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- The source does not justify labeling the design protocol-oriented.
Source map
| Source file | Relevant symbols |
|---|---|
MeshShadersMetalCPP/Application/main.m |
Cited implementation, Feature implementation |
MeshShadersMetalCPP/Application/AAPLAppDelegate.h |
Cited implementation, AAPLAppDelegate, window, TargetConditionals.h |
MeshShadersMetalCPP/Application/WindowSceneDelegate.h |
window, WindowSceneDelegate |
metal-cpp/SingleHeader/MakeSingleHeader.py |
Cited implementation, HeaderPrefix, SingleHeader |
MeshShadersMetalCPP/Adapter/AAPLRendererAdapter.h |
AAPLRendererAdapter, Foundation, CoreGraphics |
MeshShadersMetalCPP/Renderer/AAPLRenderer.cpp |
simd, MetalKit, Feature implementation |
MeshShadersMetalCPP/Renderer/AAPLShaders.metal |
metal_stdlib, vertexOut, pointVertexOut, payload_t, primOut, fragmentIn |
MeshShadersMetalCPP/Adapter/AAPLRendererAdapter.mm |
AAPLRendererAdapter |
MeshShadersMetalCPP/Application/AAPLAppDelegate.m |
AAPLAppDelegate |
MeshShadersMetalCPP/Application/AAPLViewController.h |
AAPLViewController |
MeshShadersMetalCPP/Application/AAPLViewController.m |
AAPLViewController |
MeshShadersMetalCPP/Application/WindowSceneDelegate.m |
WindowSceneDelegate |
MeshShadersMetalCPP/Renderer/AAPLShaderTypes.h |
Feature implementation |