From 2103fa629e98a30b2c5e93bcc002e40cbd3f6d5f Mon Sep 17 00:00:00 2001 From: Austin Eng Date: Tue, 13 Jul 2021 00:07:45 +0000 Subject: [PATCH] Pass preserveInvariance to MSL compiler if necessary Fixed: dawn:977 Change-Id: I7df2080424a4af0b7155ee24f61bb37f0674de82 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/57741 Commit-Queue: Austin Eng Reviewed-by: James Price --- src/dawn_native/metal/ShaderModuleMTL.h | 3 ++- src/dawn_native/metal/ShaderModuleMTL.mm | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/dawn_native/metal/ShaderModuleMTL.h b/src/dawn_native/metal/ShaderModuleMTL.h index 2cfdf49712..da42d4633a 100644 --- a/src/dawn_native/metal/ShaderModuleMTL.h +++ b/src/dawn_native/metal/ShaderModuleMTL.h @@ -58,7 +58,8 @@ namespace dawn_native { namespace metal { const RenderPipeline* renderPipeline, const VertexState* vertexState, std::string* remappedEntryPointName, - bool* needsStorageBufferLength); + bool* needsStorageBufferLength, + bool* hasInvariantAttribute); ResultOrError TranslateToMSLWithSPIRVCross( const char* entryPointName, SingleShaderStage stage, diff --git a/src/dawn_native/metal/ShaderModuleMTL.mm b/src/dawn_native/metal/ShaderModuleMTL.mm index d2475e057c..bbbe86ccff 100644 --- a/src/dawn_native/metal/ShaderModuleMTL.mm +++ b/src/dawn_native/metal/ShaderModuleMTL.mm @@ -59,7 +59,8 @@ namespace dawn_native { namespace metal { const RenderPipeline* renderPipeline, const VertexState* vertexState, std::string* remappedEntryPointName, - bool* needsStorageBufferLength) { + bool* needsStorageBufferLength, + bool* hasInvariantAttribute) { ScopedTintICEHandler scopedICEHandler(GetDevice()); std::ostringstream errorStream; @@ -160,6 +161,7 @@ namespace dawn_native { namespace metal { } *needsStorageBufferLength = result.needs_storage_buffer_sizes; + *hasInvariantAttribute = result.has_invariant_attribute; return std::move(result.msl); } @@ -292,11 +294,12 @@ namespace dawn_native { namespace metal { std::string remappedEntryPointName; std::string msl; + bool hasInvariantAttribute = false; if (GetDevice()->IsToggleEnabled(Toggle::UseTintGenerator)) { - DAWN_TRY_ASSIGN( - msl, TranslateToMSLWithTint(entryPointName, stage, layout, sampleMask, - renderPipeline, vertexState, &remappedEntryPointName, - &out->needsStorageBufferLength)); + DAWN_TRY_ASSIGN(msl, TranslateToMSLWithTint( + entryPointName, stage, layout, sampleMask, renderPipeline, + vertexState, &remappedEntryPointName, + &out->needsStorageBufferLength, &hasInvariantAttribute)); } else { DAWN_TRY_ASSIGN(msl, TranslateToMSLWithSPIRVCross(entryPointName, stage, layout, sampleMask, renderPipeline, @@ -322,11 +325,17 @@ namespace dawn_native { namespace metal { NSRef mslSource = AcquireNSRef([[NSString alloc] initWithUTF8String:msl.c_str()]); + NSRef compileOptions = AcquireNSRef([[MTLCompileOptions alloc] init]); + if (hasInvariantAttribute) { + if (@available(macOS 11.0, iOS 13.0, *)) { + (*compileOptions).preserveInvariance = true; + } + } auto mtlDevice = ToBackend(GetDevice())->GetMTLDevice(); NSError* error = nullptr; NSPRef> library = AcquireNSPRef([mtlDevice newLibraryWithSource:mslSource.Get() - options:nullptr + options:compileOptions.Get() error:&error]); if (error != nullptr) { if (error.code != MTLLibraryErrorCompileWarning) {