Skip to content

Commit 0aa974d

Browse files
Fix inconsistent background color in MaterialXView on Metal
1 parent 9b1e277 commit 0aa974d

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

source/MaterialXView/RenderPipelineMetal.mm

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,22 @@
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+
auto srgbToLinear = [](float srgb) -> float {
476+
if (srgb <= 0.04045f)
477+
return srgb / 12.92f;
478+
else
479+
return std::pow((srgb + 0.055f) / 1.055f, 2.4f);
480+
};
481+
470482
[renderpassDesc.colorAttachments[0] setClearColor:MTLClearColorMake(
471-
_viewer->m_background[0],
472-
_viewer->m_background[1],
473-
_viewer->m_background[2],
483+
srgbToLinear(_viewer->m_background[0]),
484+
srgbToLinear(_viewer->m_background[1]),
485+
srgbToLinear(_viewer->m_background[2]),
474486
_viewer->m_background[3])];
475487
[renderpassDesc.colorAttachments[0] setLoadAction:MTLLoadActionClear];
476488
[renderpassDesc.colorAttachments[0] setStoreAction:MTLStoreActionStore];

0 commit comments

Comments
 (0)