Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions dm/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
#import <QuartzCore/CAMetalLayer.h>
#import <QuartzCore/CAMetalDisplayLink.h>
#import <MetalFX/MetalFX.h>
// Xcode 26's headers do not declare these public macOS/iOS 27 setters. Calls remain under the OS 27 availability check below; this declaration only lets an older SDK build the host.
@protocol DmTemporalScalerOffsets <NSObject>
@property (nonatomic) NSUInteger colorContentOffsetX, colorContentOffsetY;
@property (nonatomic) NSUInteger depthContentOffsetX, depthContentOffsetY;
@property (nonatomic) NSUInteger motionContentOffsetX, motionContentOffsetY;
@property (nonatomic) NSUInteger outputOffsetX, outputOffsetY;
@end
#undef I // MetalFX brings in <complex.h>, whose I (the imaginary unit) would shadow the local tables named I below
#import <ImageIO/ImageIO.h>
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
Expand Down Expand Up @@ -327,12 +334,17 @@ static void defer_obj_release_ex(id obj, int keep_resident) {
static void defer_drain(void) { @autoreleasepool {
NSMutableArray<NvDeferredRelease *> *ready = [NSMutableArray array];
pthread_mutex_lock(&g_deferred_mu);
for (NSInteger i = g_deferred.count - 1; i >= 0; i--) {
NvDeferredRelease *d = g_deferred[(NSUInteger)i];
if (d->fence <= g_done) { [ready addObject:d]; [g_deferred removeObjectAtIndex:(NSUInteger)i]; }
// Retire in one linear partition. Removing each ready entry separately
// repeatedly moved the pending tail: Ocarina's scene load queued hundreds
// of thousands of references, spending minutes in NSArray's memmove.
NSMutableArray<NvDeferredRelease *> *pending = [NSMutableArray array];
uint64_t done = g_done;
for (NvDeferredRelease *d in g_deferred) {
[(d->fence <= done ? ready : pending) addObject:d];
}
g_deferred = pending;
pthread_mutex_unlock(&g_deferred_mu);
for (NvDeferredRelease *d in ready) {
for (NvDeferredRelease *d in ready.reverseObjectEnumerator) {
id obj = d->object;
if (!d->keep_resident && [obj conformsToProtocol:@protocol(MTLResource)]) {
pthread_mutex_lock(&g_res_mu);
Expand Down Expand Up @@ -11940,10 +11952,11 @@ static void dlss_eval(const p_dlss *c) {
sc.colorTexture = col; sc.depthTexture = dep; sc.motionTexture = mot; sc.outputTexture = out; sc.exposureTexture = expo;
sc.inputContentWidth = iw; sc.inputContentHeight = ih;
if (@available(macOS 27.0, iOS 27.0, *)) { // the rectangles' corners: all at the origin on the game seen, so an older system loses nothing there
sc.colorContentOffsetX = (NSUInteger)a.base[DLSS_COLOR][0]; sc.colorContentOffsetY = (NSUInteger)a.base[DLSS_COLOR][1];
sc.depthContentOffsetX = (NSUInteger)a.base[DLSS_DEPTH][0]; sc.depthContentOffsetY = (NSUInteger)a.base[DLSS_DEPTH][1];
sc.motionContentOffsetX = (NSUInteger)a.base[DLSS_MOTION][0]; sc.motionContentOffsetY = (NSUInteger)a.base[DLSS_MOTION][1];
sc.outputOffsetX = (NSUInteger)a.base[DLSS_OUTPUT][0]; sc.outputOffsetY = (NSUInteger)a.base[DLSS_OUTPUT][1];
id<DmTemporalScalerOffsets> offsets = (id)sc;
offsets.colorContentOffsetX = (NSUInteger)a.base[DLSS_COLOR][0]; offsets.colorContentOffsetY = (NSUInteger)a.base[DLSS_COLOR][1];
offsets.depthContentOffsetX = (NSUInteger)a.base[DLSS_DEPTH][0]; offsets.depthContentOffsetY = (NSUInteger)a.base[DLSS_DEPTH][1];
offsets.motionContentOffsetX = (NSUInteger)a.base[DLSS_MOTION][0]; offsets.motionContentOffsetY = (NSUInteger)a.base[DLSS_MOTION][1];
offsets.outputOffsetX = (NSUInteger)a.base[DLSS_OUTPUT][0]; offsets.outputOffsetY = (NSUInteger)a.base[DLSS_OUTPUT][1];
}
sc.motionVectorScaleX = a.mv[0]; sc.motionVectorScaleY = a.mv[1];
sc.jitterOffsetX = a.jitter[0]; sc.jitterOffsetY = a.jitter[1];
Expand Down
4 changes: 4 additions & 0 deletions dm/gfx/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,7 @@ t_logic: t_logic.m $(MTL_T_DEP)
clang $(CFLAGS) -fobjc-arc -o $@ t_logic.m $(MTL_T_SRC) $(FW)
check_t_logic: t_logic
@set -e; work=$$(mktemp -d); trap 'rm -rf "$$work"' EXIT; python3 test_dmsh_logic.py --fixture "$$work"; MTL_DEBUG_LAYER=1 ./t_logic "$$work"

t_deferred_release: t_deferred_release.m $(MTL_T_DEP)
clang $(CFLAGS) -fobjc-arc -o $@ t_deferred_release.m $(MTL_T_SRC) $(FW)
check_t_deferred_release: RUN_ENV = MTL_DEBUG_LAYER=1
38 changes: 38 additions & 0 deletions dm/gfx/tests/t_deferred_release.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Interleaved completed/pending fences used to shift a large array once per
// retired object. Check the linear partition, retention and release ordering.
#define nvn_game_fps nvn_game_fps_mtl_default
#include "../nvn/nvn_mtl.m"
#undef nvn_game_fps
uint64_t g_t0_ns;
void *front_metal_layer(void) { return NULL; }
module_t *module_of(const void *p) { (void)p; return NULL; }
module_t *g_mod; int g_nmod;
void svc_thread_dump(void) {}
uint64_t now_ns(void) { return bp_now(); }
static unsigned destroyed;
static uint32_t last_id = UINT32_MAX;
@interface RetiredProbe : NSObject { @public uint32_t number; } @end
@implementation RetiredProbe
- (void)dealloc { assert(number < last_id); last_id = number; destroyed++; }
@end

int main(void) { @autoreleasepool {
enum { N = 100000 };
for (uint32_t i = 0; i < N; i++) {
RetiredProbe *p = [RetiredProbe new]; p->number = i;
g_fence = i % 2 ? 9 : 0;
defer_obj_unref(p);
}
assert(g_deferred.count == N && destroyed == 0);
g_done = 1; uint64_t start = bp_now(); defer_drain();
assert(g_deferred.count == N / 2 && destroyed == N / 2);
for (NSUInteger i = 0; i < g_deferred.count; i++) {
assert(g_deferred[i]->fence == 10);
assert(((RetiredProbe *)g_deferred[i]->object)->number == 2 * i + 1);
}
printf("interleaved deferred release: %u entries, %.1f ms\n", N, (bp_now() - start) / 1e6);
last_id = UINT32_MAX; g_done = 10; defer_drain();
assert(g_deferred.count == 0 && destroyed == N);
defer_drain(); assert(destroyed == N);
puts("deferred fence partition, retention and ordering PASS");
} return 0; }
1 change: 1 addition & 0 deletions docs/agents/symptoms.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
- red stub lines for NVN calls. → [NVN calls that were red stubs](../reference/nvn-objects.md#nvn-calls-that-were-red-stubs)
- `nvnWindowGetPresentInterval`, `nvnCommandBufferSetCopyImageStride` missing. → [SDK 8 window and copy-stride entry points](../reference/nvn-objects.md#sdk-8-window-and-copy-stride-entry-points)
- `[RED] texture bound to an image slot lacks the shader-write usage`, and compute image writes go to a dummy texture. → [Image bindings supply shader-write intent](../reference/nvn-objects.md#image-bindings-supply-shader-write-intent)
- a large mixed queue of completed and pending GPU references stalls cleanup on the CPU. → [Deferred releases repeatedly shift pending entries](../reference/nvn-objects.md#deferred-releases-repeatedly-shift-pending-entries)

## Rendering

Expand Down
8 changes: 8 additions & 0 deletions docs/reference/nvn-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,11 @@ Games read this count as a position in their own command memory and size their h
### `nvnPrivate0`, an inlined command-buffer fast path flag

- `nvnPrivate0`, a new SDK 20 import, needs nothing: it is a flag in `sdk`'s bss that enables an inlined command-buffer fast path when the NVN driver sets it (it stays 0). Found on [The Legend of Zelda: Breath of the Wild](../../dm/games/01007EF00011E000/README.md) (1.9.0, SDK 20.5.22).

### Deferred releases repeatedly shift pending entries

- **Symptom:** a large mixed queue of completed and pending GPU references stalls cleanup on the CPU.
- **Cause:** removing each completed entry from the middle of an array repeatedly shifts the pending tail, with quadratic cost for interleaved fences.
- **Fix:** partition the queue once under its existing mutex, then retire ready entries outside the lock in the original reverse order. Fence eligibility, keep-resident semantics and pending order are unchanged.
- **Found on:** Ocarina of Time (Switch 2, #1299) scene-load investigation. This PR has synthetic lifetime validation, not an isolated gameplay FPS measurement.
- **Check:** `t_deferred_release` queues 100,000 probes with alternating fences, verifies exact release/pending order and lifetimes, then drains the remaining fence and an empty queue.
Loading