Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
HDR: the iPhone takes the PQ layer too; DM_HDR_LAYER=float on both, a…
… float source encoded for the PQ layer

Follow-up to the PQ layer (#1). The present code is shared, so the iPhone's
front end only makes the same layer: RGB10A2 in BT.2100 PQ, with DM_HDR_LAYER=float keeping the
RGBA16Float one on either platform (hle_vi.c dm_hdr_layer_float).

- present_mode: a float source (linear BT.709) into the PQ layer is encoded as HDR10 at the paper
  white (mode 4), not decoded as sRGB first; t_present_hdr checks it (24 checks).
- Docs and comments: the metadata's opticalOutputScale counts for the float layer only (Core
  Animation takes 10,000 for a normalized format), the Metal HUD shows at the screen's peak on the
  PQ layer, and the float layer's description moved to DM_HDR_LAYER. Donkey Kong Bananza's note has
  the two layers' times on an M4 Pro.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
seedds and claude committed Nov 7, 2026
commit c95f5f54876dd0386a5140bc6ddd5dbc60163f2f
1 change: 1 addition & 0 deletions dm/games/040074A01BF12000/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ The HDR output setting (Game section; Switch 2 games, Mac and iPhone) sets `DM_H
- The game declares gamut 1 (`SetLayerNextHdrContentGamutMetadata`) for its SDR and HDR windows alike. The host drops the metadata calls and logs the first three of each.
- The status answer's format is 1 with HDR on: this game's SDK sets on to (format == 1) and fails a format above 1 with 0x272 (`nn::vi::detail::DefaultDisplay::QueryHdrStatus`, sdk+0x55bd60; found in pull request #1249). The game imports none of the SDK's HDR status event calls.
- It asks for the HDR status every frame (over 1,000 queries in 90 s at 30 fps), as Mario Kart World does, so it can follow the window to another screen, and queries the paper white (203 nits). With the layer's HDR10 metadata, in a window on the built-in screen of a MacBook Pro (M4 Pro, macOS 26.7.1), from the present call to on screen p50 per 300 frames: SDR 49.69 to 49.75 ms, HDR 49.58 to 49.65. Mode select reached either way, no `[RED]` line.
- The RGB10A2 PQ layer against the RGBA16Float one (`DM_HDR_LAYER=float`), same Mac, 90 s each in a window: the same HDR window (0x3d), present call to on screen p50 49.5 to 49.8 ms with either, no `[RED]` line ([the reference entry](../../../docs/reference/rendering.md#the-status-query-and-the-present-for-an-hdr-window)).
- `present.metal` decodes the PQ codes to linear light (1.0 at the paper white) into an RGBA16Float layer in extended linear BT.2020, whose HDR10 metadata lets Core Animation map the highlights into the screen's headroom.

## Open
Expand Down
5 changes: 4 additions & 1 deletion dm/gfx/hle_vi.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ HLE_FORCE("_ZN2nn2vi8FinalizeEv", "display service shutdown; the host has no vi
// through null from SetLayerNextHdrContentGamutMetadata, main+0x2036f4). No VRR on the host, and SDR unless DM_HDR=1. Paper white is the BT.2408 reference, 203 nits (DM_HDR_PAPER_WHITE overrides it while HDR is on)
// DM_HDR=1 is the HDR mode (Switch 2 games; the HDR output setting of the Mac and iOS launchers sets it): while the window's screen can show EDR (front_hdr_screen), the display reports HDR on, so the
// game takes its HDR path (its window becomes HDR10: RGB10A2 with PQ values in BT.2020). On a screen without EDR it reports SDR, and the game keeps its own SDR picture.
// The present decodes the PQ window to linear light; Core Animation maps it into the screen's headroom by the layer's HDR10 metadata (front_mac.m, front_ios.m)
// The HDR layer takes the PQ window as it is (dm_hdr_layer_float below); Core Animation maps it into the screen's headroom by the layer's HDR10 metadata (front_mac.m, front_ios.m)
// The format is 1 with HDR on and 0 with it off: the SDK's own answer is on = (format == 1), and a format above 1 is an error, 0x272 (Donkey Kong Bananza v458752,
// nn::vi::detail::DefaultDisplay::QueryHdrStatus, sdk+0x55bd60; found in pull request #1249). The peak (DM_HDR_PEAK, default 1000) is the extended luminance range the game declares (its metadata call, below). Neither it nor the paper white has a launcher setting: they are what a console's HDR calibration would set
// Donkey Kong Bananza boots its SDR window (0x25), queries the status, then makes an RGB10A2 window (0x3d) of PQ code values with SDR white at 203 nits.
Expand All @@ -141,6 +141,9 @@ __attribute__((weak)) void front_hdr_content_range(unsigned lo, unsigned hi) { (
static float vi_paper_white_v;
static void vi_paper_white_read(void) { const char *e = getenv("DM_HDR_PAPER_WHITE"); double v = e && *e ? strtod(e, NULL) : 203.0; vi_paper_white_v = (v >= 1.0 && v <= 65535.0) ? (float)v : 203.0f; }
float dm_hdr_paper_white(void) { static pthread_once_t once = PTHREAD_ONCE_INIT; pthread_once(&once, vi_paper_white_read); return vi_paper_white_v; } // pthread_once: every guest thread sees the value written before
// The HDR layer of both front ends (front_mac.m, front_ios.m): RGB10A2 in BT.2100 PQ, which takes the game's HDR10 window as it is, unless DM_HDR_LAYER=float: the RGBA16Float extended linear
// BT.2020 layer it replaced, which showed black blocks over flat areas on a MacBook Air M5 (docs/reference/rendering.md, the status query and the present for an HDR window)
int dm_hdr_layer_float(void) { const char *e = getenv("DM_HDR_LAYER"); return e && !strcmp(e, "float"); }
// Set once the game's status query has answered HDR on: its RGB10A2 window then holds PQ code values (present_mode, nvn_mtl.m); an RGB10A2 window of a game that never asked for HDR is not decoded.
// It stays set when the window later moves to a screen without EDR: the game keeps its HDR window until it asks again, and the present still decodes it
static _Atomic int g_vi_hdr_answered;
Expand Down
13 changes: 8 additions & 5 deletions dm/gfx/msl/present.metal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ using namespace metal;
struct V { float4 p [[position]]; float2 uv; };
vertex V vs(uint id [[vertex_id]]) { float2 uv = float2((id << 1) & 2, id & 2); V o; o.p = float4(uv * float2(2, -2) + float2(-1, 1), 0, 1); o.uv = uv; return o; }
// mode.x (nvn_mtl.m present_mode) says how the source is encoded: 0 as stored (a float source: linear BT.709); 1 a gamma-coded 8-bit BT.709 (sRGB) source for the extended-range BT.2020 layer (fs_linear converts it);
// 2 an HDR10 source (the game's RGB10A2 window: SMPTE ST 2084 code values in BT.2020 primaries), where mode.y is the paper white in nits; 3 a gamma-coded 8-bit BT.709 (sRGB) source for the PQ layer (BT.2100 PQ, DM_HDR): fs encodes it as HDR10 at the paper white. mode.z and mode.w are unused.
// 2 an HDR10 source (the game's RGB10A2 window: SMPTE ST 2084 code values in BT.2020 primaries), where mode.y is the paper white in nits; 3 a gamma-coded BT.709 (sRGB) source for the PQ layer (RGB10A2 in BT.2100 PQ, DM_HDR): fs encodes it as HDR10 at the paper white;
// 4 a float source (linear BT.709) for the PQ layer, encoded the same way (untested: no local game presents a float window). mode.z and mode.w are unused.
struct Crop { float4 rect; float4 edge; float4 mode; };
// Linear BT.709 and BT.2020 (ITU-R BT.2087; each row sums to 1, so white stays white). Metal's float3x3 takes its columns.
constant float3x3 kRec709To2020 = float3x3(float3(0.627404, 0.069097, 0.016391), float3(0.329283, 0.919540, 0.088013), float3(0.043313, 0.011362, 0.895595));
Expand Down Expand Up @@ -39,15 +40,17 @@ static inline float3 present_to_sdr_white(float3 c0) {
float m = max(max(c.r, c.g), c.b);
return m > 1.0 ? c * (1.0 / m) : c;
}
// The SDR drawable (gamma-coded 8-bit, BT.709; a screenshot): an HDR10 source at its paper white, its highlights brought to SDR white with their hue (present_to_sdr_white),
// then converted to BT.709; a colour outside BT.709 is clipped
// Every target but the float HDR layer. The PQ HDR layer (RGB10A2 in BT.2100 PQ): the game's HDR10 window as it is (mode 0), an SDR or float source encoded as HDR10 at the paper white
// (modes 3, 4). The SDR drawable (gamma-coded 8-bit, BT.709; a screenshot): an HDR10 source at its paper white, its highlights brought to SDR white with their hue
// (present_to_sdr_white), then converted to BT.709; a colour outside BT.709 is clipped
fragment float4 fs(V in [[stage_in]], texture2d<float> t [[texture(0)]], sampler s [[sampler(0)]], constant Crop& c [[buffer(0)]]) {
float4 v = t.sample(s, clamp(in.uv * c.rect.zw + c.rect.xy, c.edge.xy, c.edge.zw));
if (c.mode.x > 2.5) v.rgb = present_linear_to_pq(kRec709To2020 * present_srgb_to_linear(v.rgb), c.mode.y); // the PQ layer: an SDR source as HDR10 at the paper white
if (c.mode.x > 3.5) v.rgb = present_linear_to_pq(kRec709To2020 * clamp(v.rgb, 0.0, 65504.0), c.mode.y); // the PQ layer: a float source (linear BT.709) as HDR10 at the paper white
else if (c.mode.x > 2.5) v.rgb = present_linear_to_pq(kRec709To2020 * present_srgb_to_linear(v.rgb), c.mode.y); // the PQ layer: an SDR source as HDR10 at the paper white
else if (c.mode.x > 1.5) v.rgb = present_linear_to_srgb(saturate(kRec2020To709 * present_to_sdr_white(present_pq_to_linear(v.rgb, c.mode.y))));
return v;
}
// The HDR layer (RGBA16Float, extended linear BT.2020, DM_HDR=1): linear light, where 1.0 is the paper white. The layer carries HDR10 metadata (front_mac.m, front_ios.m), so
// The float HDR layer (RGBA16Float, extended linear BT.2020, DM_HDR_LAYER=float; the PQ layer replaced it): linear light, where 1.0 is the paper white. The layer carries HDR10 metadata (front_mac.m, front_ios.m), so
// Core Animation maps the values above 1.0 into the screen's headroom: they are written as they are
fragment float4 fs_linear(V in [[stage_in]], texture2d<float> t [[texture(0)]], sampler s [[sampler(0)]], constant Crop& c [[buffer(0)]]) {
float4 v = t.sample(s, clamp(in.uv * c.rect.zw + c.rect.xy, c.edge.xy, c.edge.zw));
Expand Down
16 changes: 10 additions & 6 deletions dm/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -14051,7 +14051,8 @@ static void present_pipeline(MTLPixelFormat format) {
id<MTLLibrary> lib = host_lib("present", &e);
if (!lib) MDIE("present shader compile failed %s", e.localizedDescription.UTF8String);
MTLRenderPipelineDescriptor *pd = [MTLRenderPipelineDescriptor new];
// An extended-range layer (RGBA16Float, DM_HDR=1, BT.2020) takes the picture in linear light: fs_linear converts gamma-coded 8-bit BT.709 sources to BT.2020 and PQ-decodes HDR10 sources (present_encode and present_encode_m4 set the mode)
// The float HDR layer (RGBA16Float, extended linear BT.2020, DM_HDR_LAYER=float) takes the picture in linear light: fs_linear converts gamma-coded 8-bit BT.709 sources to BT.2020 and PQ-decodes HDR10 sources.
// Every other target, the PQ HDR layer (RGB10A2) included, is drawn by fs (present_encode and present_encode_m4 set the mode)
pd.vertexFunction = [lib newFunctionWithName:@"vs"]; pd.fragmentFunction = [lib newFunctionWithName:format == MTLPixelFormatRGBA16Float ? @"fs_linear" : @"fs"];
pd.colorAttachments[0].pixelFormat = format;
g_blit_pso = [g_dev newRenderPipelineStateWithDescriptor:pd error:&e];
Expand Down Expand Up @@ -14113,10 +14114,13 @@ static int present_crop_rect(id<MTLTexture> src, const int crop[4], NSUInteger r
__attribute__((weak)) int vi_hdr_answered(void) { return 0; } // hle_vi.c; the graphics unit tests have no vi service
static float present_mode(MTLPixelFormat src, MTLPixelFormat dst) {
// An RGB10A2 window is PQ code values only once the game's status query has answered HDR on (vi_hdr_answered); any other RGB10A2 window is read as stored
// Not answered: an SDR 10-bit window, gamma-coded like the 8-bit ones, so it is decoded to linear light for an extended-range layer
// An RGB10A2 drawable is the Mac's HDR layer in BT.2100 PQ (front_mac.m): the game's HDR10 window goes in as it is; any other source is gamma-coded BT.709 and is encoded as HDR10 (mode 3)
if (dst == MTLPixelFormatRGB10A2Unorm) return src == MTLPixelFormatRGB10A2Unorm && vi_hdr_answered() ? 0.0f : 3.0f;
if (src == MTLPixelFormatRGB10A2Unorm) return vi_hdr_answered() ? 2.0f : (dst == MTLPixelFormatRGBA16Float ? 1.0f : 0.0f);
// Not answered: an SDR 10-bit window, gamma-coded like the 8-bit ones, so it is decoded to linear light for an HDR layer
int pq_win = src == MTLPixelFormatRGB10A2Unorm && vi_hdr_answered();
// The PQ HDR layer (RGB10A2 in BT.2100 PQ, front_mac.m and front_ios.m): the game's HDR10 window goes in as it is (0); a float source is linear BT.709, encoded as HDR10 (4);
// any other is gamma-coded BT.709, decoded and encoded as HDR10 (3), its white at the paper white either way
if (dst == MTLPixelFormatRGB10A2Unorm)
return pq_win ? 0.0f : src == MTLPixelFormatRGBA16Float || src == MTLPixelFormatRGBA32Float || src == MTLPixelFormatRG11B10Float || src == MTLPixelFormatRGB9E5Float ? 4.0f : 3.0f;
if (src == MTLPixelFormatRGB10A2Unorm) return pq_win ? 2.0f : (dst == MTLPixelFormatRGBA16Float ? 1.0f : 0.0f);
if (dst != MTLPixelFormatRGBA16Float) return 0.0f;
// Gamma-coded 8-bit sources. The sRGB-typed ones are sampled through a non-sRGB view (present_raw_src), so the shader reads gamma-coded values for them too
return src == MTLPixelFormatRGBA8Unorm || src == MTLPixelFormatBGRA8Unorm || src == MTLPixelFormatRGBA8Unorm_sRGB || src == MTLPixelFormatBGRA8Unorm_sRGB ? 1.0f : 0.0f;
Expand Down Expand Up @@ -14396,7 +14400,7 @@ static int present_encode_m4(id<MTL4CommandBuffer> cb, id<MTLTexture> src, id<MT
float *uv = uvb.contents; NSUInteger tw = tex.width, th = tex.height, cx = r[0], cy = r[1], cw = r[2], ch = r[3];
uv[0] = (float)cx / tw; uv[1] = (float)cy / th; uv[2] = (float)cw / tw; uv[3] = (float)ch / th;
uv[4] = (cx + 0.5f) / tw; uv[5] = (cy + 0.5f) / th; uv[6] = (cx + cw - 0.5f) / tw; uv[7] = (cy + ch - 0.5f) / th;
// Crop.mode.x (present.metal): an extended-range BT.2020 layer decodes gamma-coded 8-bit BT.709 sources to linear light and converts them; HDR10 sources are PQ-decoded
// Crop.mode.x (present.metal present_mode): how the source is encoded, and so how it is converted for the target (the PQ or the float HDR layer, an SDR drawable, a screenshot)
uv[8] = present_mode(src.pixelFormat, dst.pixelFormat); uv[9] = present_paper_white(); uv[10] = uv[11] = 0;
if (atomic_load_explicit(&g_origin_ll, memory_order_relaxed)) { uv[1] += uv[3]; uv[3] = -uv[3]; } // lower-left origin: the window image is stored bottom-up
[g_present_args setAddress:uvb.gpuAddress atIndex:0];
Expand Down
17 changes: 16 additions & 1 deletion dm/gfx/tests/t_present_hdr.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// HDR output (DM_HDR=1): the status query's answer and the present's decode, on the GPU with closed-form expectations (no game, no window).
// The vi answer: HDR on only while the window's screen can show EDR, format 1 with it (the SDK's on = format == 1), the decode latched once answered. The present: PQ code values
// (an RGB10A2 window) decoded with 1.0 at the 203-nit paper white and written as they are above it (Core Animation maps them by the layer's HDR10 metadata), an 8-bit sRGB
// (an RGB10A2 window) written as they are into the PQ layer (RGB10A2 in BT.2100 PQ) and SDR and float sources encoded for it at the paper white; for the float layer,
// decoded with 1.0 at the 203-nit paper white and written as they are above it (Core Animation maps them by the layer's HDR10 metadata), an 8-bit sRGB
// source converted to BT.2020, a screenshot of an HDR frame brought to SDR white with its hue, and a 10-bit window of a game
// that never asked for HDR read as stored. hle_vi.c is linked beside this file; t_present_hdr_front.c stands in for the front end
#include "../nvn/nvn_mtl.m"
Expand Down Expand Up @@ -58,6 +59,15 @@ static double pq_nits(unsigned code) {
[t replaceRegion:MTLRegionMake2D(0, 0, N, 1) mipmapLevel:0 withBytes:b bytesPerRow:sizeof b];
return t;
}
// An N x 1 RGBA16Float source of linear values (a float window)
static id<MTLTexture> src16f(const float c[N][3]) {
MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA16Float width:N height:1 mipmapped:NO];
d.storageMode = MTLStorageModeShared; d.usage = MTLTextureUsageShaderRead;
id<MTLTexture> t = [g_dev newTextureWithDescriptor:d]; __fp16 h[N * 4];
for (int i = 0; i < N; i++) { for (int k = 0; k < 3; k++) h[4 * i + k] = (__fp16)c[i][k]; h[4 * i + 3] = 1; }
[t replaceRegion:MTLRegionMake2D(0, 0, N, 1) mipmapLevel:0 withBytes:h bytesPerRow:sizeof h];
return t;
}
// The present of src into an N x 1 target of the given format; out gets RGB as floats (16-bit float targets), 0-1023 (RGB10A2, the PQ layer) or 0-255 (8-bit targets)
static int run(id<MTLTexture> src, MTLPixelFormat fmt, double out[N][3]) {
MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:fmt width:N height:1 mipmapped:NO];
Expand Down Expand Up @@ -149,6 +159,11 @@ int main(void) { @autoreleasepool {
ok = run(src8(c), MTLPixelFormatRGB10A2Unorm, o);
CHECK("PQ layer: SDR white is the paper white (203 nits), black is code 0", ok && fabs(o[0][0] - pq_code(203)) <= 1.5 && fabs(o[0][2] - pq_code(203)) <= 1.5 && o[5][0] <= 1);
CHECK("PQ layer: SDR mid grey 128 is its linear value at the paper white", ok && fabs(o[1][1] - pq_code(srgb_dec(128 / 255.0) * 203)) <= 1.5);
float f[N][3] = {{1, 1, 1}, {0.5f, 0.5f, 0.5f}, {4, 4, 4}, {0, 0, 0}, {-1, -1, -1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}};
ok = run(src16f(f), MTLPixelFormatRGB10A2Unorm, o);
CHECK("PQ layer: a float source is linear light, 1.0 at the paper white, 4.0 at four times it, not decoded as sRGB", ok && fabs(o[0][1] - pq_code(203)) <= 1.5 && fabs(o[1][1] - pq_code(101.5)) <= 1.5 &&
fabs(o[2][1] - pq_code(812)) <= 1.5);
CHECK("PQ layer: a negative float value is black", ok && o[3][1] <= 1 && o[4][1] <= 1);
}
// A screenshot of an HDR frame (an 8-bit target): as an SDR screen shows it, highlights to SDR white with their hue, then BT.709
{
Expand Down
1 change: 1 addition & 0 deletions dm/host/front.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ int front_screen_variable_refresh(void);
int front_hdr_screen(void);
void front_hdr_content_range(unsigned lo, unsigned hi);
float dm_hdr_paper_white(void); // hle_vi.c: the paper white in nits (DM_HDR_PAPER_WHITE, default 203), the luminance the picture's SDR white has
int dm_hdr_layer_float(void); // hle_vi.c: DM_HDR_LAYER=float, the RGBA16Float extended linear BT.2020 layer; else RGB10A2 in BT.2100 PQ
// The frame rate the display link paces the game at (60, 30, 120; nvn_mtl.m link_sync): in fullscreen, a screen whose refresh rate is not a multiple of it is switched, while the game runs,
// to a mode of the same size whose rate is, and back when the window leaves fullscreen or that screen (front_mac.m; none on iOS). Called from the present thread
void front_screen_match(int fps);
Expand Down
Loading