Sample CodeReviewed 2026-07-21View on Apple Developer

Creating a 3D application with hydra rendering

At a glance

Item Summary
Purpose Build a 3D application that integrates with Hydra and USD.
App architecture A C/Objective-C header, Metal, Objective-C++, Python sample with the source-visible chain mainAAPLViewControllerAAPLRendererMetalKit APIs.
Main patterns Delegate or data-source callbacks
Project style 12 scanned source file(s) across C/Objective-C header, Metal, 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 pxr, Cocoa, MetalKit, CoreImage, cmath; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── source/
│   ├── main.mm
│   ├── AAPLAppDelegate.h
│   ├── AAPLAppDelegate.mm
│   ├── AAPLRenderer.h
│   ├── AAPLRenderer.mm
│   ├── AAPLViewController.h
│   ├── AAPLViewController.mm
│   ├── AAPLCamera.h
│   ├── AAPLCamera.mm
│   └── BlitShaders.metal
└── scripts/
    ├── cleanup_schema.py
    └── relocatable_hydraplayer.py

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: C/Objective-C header, Metal, Objective-C++, Python.
  • The verified tree contains 4 project/configuration file(s) and 5 source declaration(s).

Overall architecture

Reference code

source/main.mm:10 — architecture anchor

int main(int argc, const char* argv[])
{
    return NSApplicationMain(argc, argv);
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into Metal.

Ownership and state

Ownership evidence

source/AAPLAppDelegate.h:14 — stored dependency or nearest verified ownership anchor

@interface AAPLAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow* window;

@end
Owner Object or state Relationship Mutation authority
AAPLAppDelegate NSWindow (window) holds a non-owning reference The referenced object’s lifecycle is owned elsewhere
AAPLRenderer bool (isZUp) stores or receives Header-visible collaborators
AAPLRenderer AAPLCamera (viewCamera) stores or receives Header-visible collaborators
AAPLCamera GfVec3d (position) stores or receives 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 pxr The cited file imports this module; runtime use and architectural role are not inferred. source/AAPLCamera.h:12
Source import Cocoa The cited file imports this module; runtime use and architectural role are not inferred. source/AAPLAppDelegate.h:10
Source import MetalKit The cited file imports this module; runtime use and architectural role are not inferred. source/AAPLRenderer.h:10
Source import CoreImage The cited file imports this module; runtime use and architectural role are not inferred. source/AAPLRenderer.mm:32
Source import cmath The cited file imports this module; runtime use and architectural role are not inferred. source/AAPLRenderer.mm:35
Source import CoreFoundation The cited file imports this module; runtime use and architectural role are not inferred. source/AAPLViewController.mm:12

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

source/AAPLAppDelegate.h:12 — representative type boundary

@interface AAPLAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow* window;

@end
Type Responsibility Depends on or conforms to
AAPLAppDelegate Receives callback-driven events NSObject, NSApplicationDelegate
AAPLRenderer Owns drawing, GPU, or presentation processing NSObject, MTKViewDelegate
AAPLViewController View lifecycle, callbacks, and feature coordination NSViewController
AAPLCamera Defines a feature-specific type boundary NSObject, NSMutableCopying
VertexOut Represents a feature value or composable behavior Concrete collaborators/imported frameworks

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 (source/AAPLAppDelegate.h:14) 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.
position (source/AAPLCamera.h:73) 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.
rotation (source/AAPLCamera.h:74) 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.
focus (source/AAPLCamera.h:75) 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.

Reference code

source/AAPLAppDelegate.h:14 — representative boundary

@interface AAPLAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow* window;

@end

Swift 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
View lifecycle, callbacks, and feature coordination AAPLViewController The source’s Controller suffix makes this role explicit.
Receives callback-driven events AAPLAppDelegate The source’s Delegate suffix makes this role explicit.
Owns drawing, GPU, or presentation processing AAPLRenderer The source’s Renderer suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Delegate or data-source callbacks source/AAPLAppDelegate.h:12 Callback protocols invert event delivery back into the sample’s owner.

Main application flow

Reference code

source/AAPLRenderer.mm:379drawMainView()

- (bool)drawMainView:(MTKView*)view timeCode:(double)timeCode
{
    if (!_engine)
    {
        return false;
    }

    // Start the next frame.
    dispatch_semaphore_wait(_inFlightSemaphore, DISPATCH_TIME_FOREVER);
    HgiMetal* hgi = static_cast<HgiMetal*>(_hgi.get());
    hgi->StartFrame();

    // Draw the scene using Hydra, and recast the result to a MTLTexture.
    CGSize viewSize = [view drawableSize];
    HgiTextureHandle hgiTexture = [self drawWithHydraAt:timeCode viewSize:viewSize];
    id<MTLTexture> texture = static_cast<HgiMetalTexture*>(hgiTexture.Get())->GetTextureId();

    // Create a command buffer to blit the texture to the view.
    id<MTLCommandBuffer> commandBuffer = hgi->GetPrimaryCommandBuffer();
    __block dispatch_semaphore_t blockSemaphore = _inFlightSemaphore;
    [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
        dispatch_semaphore_signal(blockSemaphore);
    }];

    // Copy the rendered texture to the view.
    [self blitToView:view commandBuffer:commandBuffer texture:texture];

    // Tell Hydra to commit the command buffer, and complete the work.
    hgi->CommitPrimaryCommandBuffer();
    hgi->EndFrame();

    return true;
}

Naming conventions

  • Types: Controller: AAPLViewController; Delegate: AAPLAppDelegate; Renderer: AAPLRenderer.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: applicationDidFinishLaunching, applicationWillTerminate, applicationShouldTerminateAfterLastWindowClosed, initWithMetalKitView, setupScene, requestFrame, initializeMaterial, dealloc.
  • Files: source/AAPLAppDelegate.h, source/AAPLAppDelegate.mm, source/AAPLRenderer.h, source/AAPLRenderer.mm, source/AAPLViewController.h, source/AAPLViewController.mm.

Architecture takeaways

  • main is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches pxr, Cocoa, MetalKit, CoreImage 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
source/main.mm Cited implementation, Feature implementation
source/AAPLAppDelegate.h Cited implementation, AAPLAppDelegate, window, Cocoa
source/AAPLCamera.h position, rotation, focus, pxr, AAPLCamera
source/AAPLRenderer.h MetalKit, AAPLRenderer
source/AAPLRenderer.mm CoreImage, cmath, AAPLRenderer
source/AAPLViewController.mm CoreFoundation, AAPLViewController
source/AAPLAppDelegate.mm AAPLAppDelegate
source/AAPLViewController.h AAPLViewController
source/AAPLCamera.mm AAPLCamera
source/BlitShaders.metal VertexOut
scripts/cleanup_schema.py Feature implementation
scripts/relocatable_hydraplayer.py Feature implementation