diff --git a/Docs/Whats_New.md b/Docs/Whats_New.md index 32e33cff..48cf7174 100644 --- a/Docs/Whats_New.md +++ b/Docs/Whats_New.md @@ -21,6 +21,7 @@ Released TBD - Advertise support for `shaderInt64` feature. - Support fast math on MSL compiler via `MVKConfiguration::fastMathEnabled` configuration setting and `MVK_CONFIG_FAST_MATH_ENABLED` environment variable (enabled by default). +- Support compiling MSL with position invariance if indicated in SPIRV shader. - `vkGetMoltenVKConfigurationMVK()` and `vkSetMoltenVKConfigurationMVK()` functions can now be used with a `VkInstance` from another Vulkan layer, or with a `VK_NULL_HANDLE VkInstance`. - `MVKConfiguration` extended to cover all MoltenVK environment variables. diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.h b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.h index 9b4a85c8..dab1c468 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.h +++ b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.h @@ -650,10 +650,12 @@ public: /** * Returns an autoreleased options object to be used when compiling MSL shaders. - * The useFastMath parameteris and-combined with MVKConfiguration::fastMathEnabled + * The useFastMath parameter is and-combined with MVKConfiguration::fastMathEnabled * to determine whether to enable fast math optimizations in the compiled shader. + * The preserveInvariance parameter indicates that the shader requires the position + * output invariance across invocations (typically for the position output). */ - MTLCompileOptions* getMTLCompileOptions(bool useFastMath = true); + MTLCompileOptions* getMTLCompileOptions(bool useFastMath = true, bool preserveInvariance = false); /** Returns the Metal vertex buffer index to use for the specified vertex attribute binding number. */ uint32_t getMetalBufferIndexForVertexAttributeBinding(uint32_t binding); diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm index 8bc47adc..bbe33fc7 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm @@ -3639,10 +3639,13 @@ id MVKDevice::getDefaultMTLSamplerState() { return _defaultMTLSamplerState; } -MTLCompileOptions* MVKDevice::getMTLCompileOptions(bool useFastMath) { +MTLCompileOptions* MVKDevice::getMTLCompileOptions(bool useFastMath, bool preserveInvariance) { MTLCompileOptions* mtlCompOpt = [MTLCompileOptions new]; mtlCompOpt.languageVersion = _pMetalFeatures->mslVersionEnum; mtlCompOpt.fastMathEnabled = useFastMath && mvkGetMVKConfiguration()->fastMathEnabled; + if ([mtlCompOpt respondsToSelector: @selector(setPreserveInvariance:)]) { + [mtlCompOpt setPreserveInvariance: preserveInvariance]; + } return [mtlCompOpt autorelease]; } diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm b/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm index 97547eeb..932b26bc 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm @@ -2114,6 +2114,7 @@ namespace mvk { void serialize(Archive & archive, SPIRVToMSLConversionResults& scr) { archive(scr.entryPoint, scr.isRasterizationDisabled, + scr.isPositionInvariant, scr.needsSwizzleBuffer, scr.needsOutputBuffer, scr.needsPatchOutputBuffer, diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKShaderModule.h b/MoltenVK/MoltenVK/GPUObjects/MVKShaderModule.h index 4ffc6690..7b45d55f 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKShaderModule.h +++ b/MoltenVK/MoltenVK/GPUObjects/MVKShaderModule.h @@ -254,7 +254,8 @@ public: * If the Metal library compiler does not return within MVKConfiguration::metalCompileTimeout * nanoseconds, an error will be generated and logged, and nil will be returned. */ - id newMTLLibrary(NSString* mslSourceCode, bool useFastMath); + id newMTLLibrary(NSString* mslSourceCode, + const SPIRVToMSLConversionResults& shaderConversionResults); #pragma mark Construction diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKShaderModule.mm b/MoltenVK/MoltenVK/GPUObjects/MVKShaderModule.mm index a4517272..b30d924e 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKShaderModule.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKShaderModule.mm @@ -136,7 +136,7 @@ MVKShaderLibrary::MVKShaderLibrary(MVKVulkanAPIDeviceObject* owner, MVKShaderLibraryCompiler* slc = new MVKShaderLibraryCompiler(_owner); NSString* nsSrc = [[NSString alloc] initWithUTF8String: mslSourceCode.c_str()]; // temp retained - _mtlLibrary = slc->newMTLLibrary(nsSrc, shaderConversionResults.entryPoint.supportsFastMath); // retained + _mtlLibrary = slc->newMTLLibrary(nsSrc, shaderConversionResults); // retained [nsSrc release]; // release temp string slc->destroy(); @@ -415,12 +415,14 @@ MVKShaderModule::~MVKShaderModule() { #pragma mark - #pragma mark MVKShaderLibraryCompiler -id MVKShaderLibraryCompiler::newMTLLibrary(NSString* mslSourceCode, bool useFastMath) { +id MVKShaderLibraryCompiler::newMTLLibrary(NSString* mslSourceCode, + const SPIRVToMSLConversionResults& shaderConversionResults) { unique_lock lock(_completionLock); compile(lock, ^{ [_owner->getMTLDevice() newLibraryWithSource: mslSourceCode - options: _owner->getDevice()->getMTLCompileOptions(useFastMath) + options: _owner->getDevice()->getMTLCompileOptions(shaderConversionResults.entryPoint.supportsFastMath, + shaderConversionResults.isPositionInvariant) completionHandler: ^(id mtlLib, NSError* error) { bool isLate = compileComplete(mtlLib, error); if (isLate) { destroy(); } diff --git a/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.cpp b/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.cpp index f2feeab3..996374a2 100644 --- a/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.cpp +++ b/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.cpp @@ -298,6 +298,7 @@ MVK_PUBLIC_SYMBOL bool SPIRVToMSLConverter::convert(SPIRVToMSLConversionConfigur // and mark which vertex attributes and resource bindings are used by the shader populateEntryPoint(pMSLCompiler, context.options); _shaderConversionResults.isRasterizationDisabled = pMSLCompiler && pMSLCompiler->get_is_rasterization_disabled(); + _shaderConversionResults.isPositionInvariant = pMSLCompiler && pMSLCompiler->is_position_invariant(); _shaderConversionResults.needsSwizzleBuffer = pMSLCompiler && pMSLCompiler->needs_swizzle_buffer(); _shaderConversionResults.needsOutputBuffer = pMSLCompiler && pMSLCompiler->needs_output_buffer(); _shaderConversionResults.needsPatchOutputBuffer = pMSLCompiler && pMSLCompiler->needs_patch_output_buffer(); @@ -306,8 +307,6 @@ MVK_PUBLIC_SYMBOL bool SPIRVToMSLConverter::convert(SPIRVToMSLConversionConfigur _shaderConversionResults.needsDispatchBaseBuffer = pMSLCompiler && pMSLCompiler->needs_dispatch_base_buffer(); _shaderConversionResults.needsViewRangeBuffer = pMSLCompiler && pMSLCompiler->needs_view_mask_buffer(); - - for (auto& ctxSI : context.shaderInputs) { ctxSI.isUsedByShader = pMSLCompiler->is_msl_shader_input_used(ctxSI.shaderInput.location); } diff --git a/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.h b/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.h index a7eb8f54..d1d127b2 100644 --- a/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.h +++ b/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.h @@ -204,6 +204,7 @@ namespace mvk { typedef struct SPIRVToMSLConversionResults { SPIRVEntryPoint entryPoint; bool isRasterizationDisabled = false; + bool isPositionInvariant = false; bool needsSwizzleBuffer = false; bool needsOutputBuffer = false; bool needsPatchOutputBuffer = false;