Skip to content

Commit 7e91c4d

Browse files
Fix inconsistent background color in MaterialXView on Metal (#2800)
## Summary Fixes #2606 The `MaterialXView` background color appeared lighter on Mac (Metal) than on Windows (OpenGL) due to a color space mismatch in the Metal rendering pipeline. ## Root Cause The Metal renderer uses an `MTLPixelFormatRGBA16Float` (linear) framebuffer. macOS applies gamma correction when displaying linear float values. The background color stored in `m_background` comes from `DEFAULT_SCREEN_COLOR_SRGB = (0.3, 0.3, 0.32)` — sRGB-encoded values. Passing these directly to `setClearColor` on a linear framebuffer causes macOS to gamma-correct them on display (`sRGB(0.3) ≈ 0.58`), producing a noticeably lighter background. On Windows/OpenGL, `glClearColor` writes values into a non-linear framebuffer which the display interprets as sRGB — so the appearance is correct. The previous code also had a `linearToSrgb` conversion applied in the **wrong direction**, converting already-sRGB values further into sRGB and making the discrepancy worse. ## Fix Replace `linearToSrgb` with `srgbToLinear` in `RenderPipelineMetal.mm`. This converts the sRGB background values to their linear equivalents before storing in the float framebuffer. macOS then gamma-corrects them back on display, producing the intended `(0.3, 0.3, 0.32)` gray — matching the Windows/OpenGL output. This aligns with the finding in the issue that using `DEFAULT_SCREEN_COLOR_LIN_REC709` (which is `DEFAULT_SCREEN_COLOR_SRGB.srgbToLinear()`) resolves the inconsistency.
1 parent 2cecaad commit 7e91c4d

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

source/MaterialXView/RenderPipelineMetal.mm

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,19 @@
467467
{
468468
[renderpassDesc.colorAttachments[0] setTexture:MTL(currentFramebuffer())->getColorTexture()];
469469
}
470+
// The Metal framebuffer stores linear color values (MTLPixelFormatRGBA16Float).
471+
// macOS applies gamma correction when displaying linear float values, so the sRGB
472+
// background color must be converted to linear before being set as the clear color.
473+
// On OpenGL (Windows), the background is stored directly in a non-linear framebuffer
474+
// and no conversion is needed.
475+
mx::Color3 bgLinear = mx::Color3(_viewer->m_background[0],
476+
_viewer->m_background[1],
477+
_viewer->m_background[2]).srgbToLinear();
478+
470479
[renderpassDesc.colorAttachments[0] setClearColor:MTLClearColorMake(
471-
_viewer->m_background[0],
472-
_viewer->m_background[1],
473-
_viewer->m_background[2],
480+
bgLinear[0],
481+
bgLinear[1],
482+
bgLinear[2],
474483
_viewer->m_background[3])];
475484
[renderpassDesc.colorAttachments[0] setLoadAction:MTLLoadActionClear];
476485
[renderpassDesc.colorAttachments[0] setStoreAction:MTLStoreActionStore];

0 commit comments

Comments
 (0)