Consolidation and fixes for MVK_USE_METAL_PRIVATE_API functionality.

- Expose MTLRenderPipelineDescriptor.sampleMaskMVK and
  MTLSamplerDescriptor.lodBiasMVK properties when
  MVK_USE_METAL_PRIVATE_API build setting is disabled.
- MVKCmdSetDepthBias & MVKCmdSetDepthBounds subclass from MVKSingleValueCommand.
- MVKRenderingCommandEncoderState simplify calls to set depth bias
  and depth bounds, and make consistent with other settings.
- Refactor MVKRenderingCommandEncoderState::setContent() and
  setMTLContent() to remove need for intermediate value copies.
- MVKPipeline remove test for depth bounds support since
  it's checked before GPU encoding.
- MVKDepthBias member order same as in Vulkan calls.
- Whats_New.md consolidate notes about MVK_USE_METAL_PRIVATE_API.
This commit is contained in:
Bill Hollings 2024-02-08 16:13:03 -05:00
parent 885960ac99
commit efaae79d90
9 changed files with 53 additions and 119 deletions

View File

@ -19,11 +19,11 @@ MoltenVK 1.2.8
Released TBD
- Add `MVK_USE_METAL_PRIVATE_API` build setting to allow **MoltenVK** to be built with access to _Metal_ private API calls.
- Add support for `VkPhysicalDeviceFeatures::wideLines` feature when `MVK_USE_METAL_PRIVATE_API` is enabled in a **MoltenVK** build.
- Add support for the `VkPhysicalDeviceFeatures::logicOp` feature when `MVK_USE_METAL_PRIVATE_API` is enabled in a **MoltenVK** build.
- Add support for the `VkPhysicalDeviceFeatures::depthBounds` feature on AMD GPUs when `MVK_USE_METAL_PRIVATE_API` is enabled in a **MoltenVK** build.
- Add support for the `VkPhysicalDevicePortabilitySubsetFeaturesKHR::samplerMipLodBias` feature when `MVK_USE_METAL_PRIVATE_API` is enabled in a **MoltenVK** build.
- Add support for Metal native pipeline sample masks when `MVK_USE_METAL_PRIVATE_API` is enabled in a **MoltenVK** build.
- `VkPhysicalDeviceFeatures::wideLines` enabled when `MVK_USE_METAL_PRIVATE_API` is enabled in a **MoltenVK** build.
- `VkPhysicalDeviceFeatures::logicOp` enabled when `MVK_USE_METAL_PRIVATE_API` is enabled in a **MoltenVK** build.
- `VkPhysicalDeviceFeatures::depthBounds` enabled on AMD GPUs when `MVK_USE_METAL_PRIVATE_API` is enabled in a **MoltenVK** build.
- `VkPhysicalDevicePortabilitySubsetFeaturesKHR::samplerMipLodBias` enabled when `MVK_USE_METAL_PRIVATE_API` is enabled in a **MoltenVK** build.
- _Metal_ native pipeline sample masks supported when `MVK_USE_METAL_PRIVATE_API` is enabled in a **MoltenVK** build.
- Fix potential crash when using multi-planar images.
- Ensure buffers available for buffer addresses in push constants.
- Support `libMoltenVK.dylib` for _iOS Simulator_ architecture.

View File

@ -21,6 +21,7 @@
#include "MVKCommand.h"
#include "MVKDevice.h"
#include "MVKSmallVector.h"
#include "MVKCommandEncoderState.h"
#import <Metal/Metal.h>
@ -278,22 +279,13 @@ typedef MVKCmdSetScissor<kMVKMaxViewportScissorCount> MVKCmdSetScissorMulti;
#pragma mark -
#pragma mark MVKCmdSetDepthBias
class MVKCmdSetDepthBias : public MVKCommand {
class MVKCmdSetDepthBias : public MVKSingleValueCommand<MVKDepthBias> {
public:
VkResult setContent(MVKCommandBuffer* cmdBuff,
float depthBiasConstantFactor,
float depthBiasClamp,
float depthBiasSlopeFactor);
void encode(MVKCommandEncoder* cmdEncoder) override;
protected:
MVKCommandTypePool<MVKCommand>* getTypePool(MVKCommandPool* cmdPool) override;
float _depthBiasConstantFactor;
float _depthBiasClamp;
float _depthBiasSlopeFactor;
};
@ -378,28 +370,19 @@ protected:
#pragma mark -
#pragma mark MVKCmdSetDepthBounds
/** Vulkan command to set depth bounds. */
class MVKCmdSetDepthBounds : public MVKCommand {
class MVKCmdSetDepthBounds : public MVKSingleValueCommand<MVKDepthBounds> {
public:
VkResult setContent(MVKCommandBuffer* cmdBuff,
float minDepthBounds,
float maxDepthBounds);
void encode(MVKCommandEncoder* cmdEncoder) override;
protected:
MVKCommandTypePool<MVKCommand>* getTypePool(MVKCommandPool* cmdPool) override;
float _minDepthBounds;
float _maxDepthBounds;
};
#pragma mark -
#pragma mark MVKCmdSetDepthBoundsTestEnable
/** Vulkan command to enable depth bounds testing. */
class MVKCmdSetDepthBoundsTestEnable : public MVKSingleValueCommand<VkBool32> {
public:

View File

@ -261,21 +261,8 @@ template class MVKCmdSetScissor<kMVKMaxViewportScissorCount>;
#pragma mark -
#pragma mark MVKCmdSetDepthBias
VkResult MVKCmdSetDepthBias::setContent(MVKCommandBuffer* cmdBuff,
float depthBiasConstantFactor,
float depthBiasClamp,
float depthBiasSlopeFactor) {
_depthBiasConstantFactor = depthBiasConstantFactor;
_depthBiasSlopeFactor = depthBiasSlopeFactor;
_depthBiasClamp = depthBiasClamp;
return VK_SUCCESS;
}
void MVKCmdSetDepthBias::encode(MVKCommandEncoder* cmdEncoder) {
cmdEncoder->_renderingState.setDepthBias(_depthBiasConstantFactor,
_depthBiasSlopeFactor,
_depthBiasClamp);
cmdEncoder->_renderingState.setDepthBias(_value, true);
}
@ -283,7 +270,7 @@ void MVKCmdSetDepthBias::encode(MVKCommandEncoder* cmdEncoder) {
#pragma mark MVKCmdSetDepthBiasEnable
void MVKCmdSetDepthBiasEnable::encode(MVKCommandEncoder* cmdEncoder) {
cmdEncoder->_renderingState.setDepthBiasEnable(_value);
cmdEncoder->_renderingState.setDepthBiasEnable(_value, true);
}
@ -330,16 +317,8 @@ void MVKCmdSetDepthCompareOp::encode(MVKCommandEncoder* cmdEncoder) {
#pragma mark -
#pragma mark MVKCmdSetDepthBounds
VkResult MVKCmdSetDepthBounds::setContent(MVKCommandBuffer* cmdBuff,
float minDepthBounds,
float maxDepthBounds) {
_minDepthBounds = minDepthBounds;
_maxDepthBounds = maxDepthBounds;
return VK_SUCCESS;
}
void MVKCmdSetDepthBounds::encode(MVKCommandEncoder* cmdEncoder) {
cmdEncoder->_renderingState.setDepthBounds(_minDepthBounds, _maxDepthBounds, true);
cmdEncoder->_renderingState.setDepthBounds(_value, true);
}

View File

@ -235,8 +235,8 @@ protected:
struct MVKDepthBias {
float depthBiasConstantFactor;
float depthBiasSlopeFactor;
float depthBiasClamp;
float depthBiasSlopeFactor;
};
struct MVKDepthBounds {
@ -273,11 +273,10 @@ public:
void setBlendConstants(MVKColor32 blendConstants, bool isDynamic);
void setDepthBias(const VkPipelineRasterizationStateCreateInfo& vkRasterInfo);
void setDepthBias(float depthBiasConstantFactor, float depthBiasSlopeFactor, float depthBiasClamp);
void setDepthBiasEnable(VkBool32 depthBiasEnable);
void setDepthBias(MVKDepthBias depthBias, bool isDynamic);
void setDepthBiasEnable(VkBool32 depthBiasEnable, bool isDynamic);
void setDepthClipEnable(bool depthClip, bool isDynamic);
void setDepthBounds(float minDepthBounds, float maxDepthBounds, bool isDynamic);
void setDepthBounds(MVKDepthBounds depthBounds, bool isDynamic);
void setDepthBoundsTestEnable(VkBool32 depthBoundsTestEnable, bool isDynamic);
void setStencilReferenceValues(const VkPipelineDepthStencilStateCreateInfo& vkDepthStencilInfo);
void setStencilReferenceValues(VkStencilFaceFlags faceMask, uint32_t stencilReference);
@ -310,7 +309,7 @@ public:
protected:
void encodeImpl(uint32_t stage) override;
bool isDrawingTriangles();
template <typename T> void setContent(T* iVarAry, T* pVal, MVKRenderStateType state, bool isDynamic) {
template <typename T> void setContent(MVKRenderStateType state, T* iVarAry, T* pVal, bool isDynamic) {
auto* pIVar = &iVarAry[isDynamic ? StateScope::Dynamic : StateScope::Static];
if( !mvkAreEqual(pVal, pIVar) ) {
*pIVar = *pVal;
@ -319,6 +318,9 @@ protected:
MVKCommandEncoderState::markDirty(); // Avoid local markDirty() as it marks all states dirty.
}
}
template <typename T> void setContent(MVKRenderStateType state, T* iVarAry, T val, bool isDynamic) {
setContent(state, iVarAry, &val, isDynamic);
}
MVKSmallVector<MTLSamplePosition, kMVKMaxSampleCount> _mtlSampleLocations[StateScope::Count] = {};
MVKMTLViewports _mtlViewports[StateScope::Count] = {};

View File

@ -303,89 +303,59 @@ void MVKDepthStencilCommandEncoderState::encodeImpl(uint32_t stage) {
#pragma mark MVKRenderingCommandEncoderState
#define getMTLContent(state) getContent(_mtl##state, state)
#define setMTLContent(state) setContent(_mtl##state, &mtl##state, state, isDynamic)
#define setMTLContent(state, val) setContent(state, _mtl##state, val, isDynamic)
void MVKRenderingCommandEncoderState::setCullMode(VkCullModeFlags cullMode, bool isDynamic) {
auto mtlCullMode = mvkMTLCullModeFromVkCullModeFlags(cullMode);
setMTLContent(CullMode);
setMTLContent(CullMode, mvkMTLCullModeFromVkCullModeFlags(cullMode));
getContent(_cullBothFaces, isDynamic) = (cullMode == VK_CULL_MODE_FRONT_AND_BACK);
}
void MVKRenderingCommandEncoderState::setFrontFace(VkFrontFace frontFace, bool isDynamic) {
auto mtlFrontFace = mvkMTLWindingFromVkFrontFace(frontFace);
setMTLContent(FrontFace);
setMTLContent(FrontFace, mvkMTLWindingFromVkFrontFace(frontFace));
}
void MVKRenderingCommandEncoderState::setPolygonMode(VkPolygonMode polygonMode, bool isDynamic) {
auto mtlPolygonMode = mvkMTLTriangleFillModeFromVkPolygonMode(polygonMode);
setMTLContent(PolygonMode);
setMTLContent(PolygonMode, mvkMTLTriangleFillModeFromVkPolygonMode(polygonMode));
getContent(_isPolygonModePoint, isDynamic) = (polygonMode == VK_POLYGON_MODE_POINT);
}
void MVKRenderingCommandEncoderState::setLineWidth(float lineWidth, bool isDynamic) {
auto mtlLineWidth = lineWidth;
setMTLContent(LineWidth);
setMTLContent(LineWidth, lineWidth);
}
void MVKRenderingCommandEncoderState::setBlendConstants(MVKColor32 blendConstants, bool isDynamic) {
MVKColor32 mtlBlendConstants = blendConstants;
setMTLContent(BlendConstants);
setMTLContent(BlendConstants, blendConstants);
}
void MVKRenderingCommandEncoderState::setDepthBias(const VkPipelineRasterizationStateCreateInfo& vkRasterInfo) {
bool isDynamic = false;
bool mtlDepthBiasEnable = static_cast<bool>(vkRasterInfo.depthBiasEnable);
setMTLContent(DepthBiasEnable);
MVKDepthBias mtlDepthBias = {
.depthBiasConstantFactor = vkRasterInfo.depthBiasConstantFactor,
.depthBiasSlopeFactor = vkRasterInfo.depthBiasSlopeFactor,
.depthBiasClamp = vkRasterInfo.depthBiasClamp
};
setMTLContent(DepthBias);
setDepthBiasEnable(vkRasterInfo.depthBiasEnable, false);
setDepthBias( { vkRasterInfo.depthBiasConstantFactor, vkRasterInfo.depthBiasClamp, vkRasterInfo.depthBiasSlopeFactor } , false);
}
void MVKRenderingCommandEncoderState::setDepthBias(float depthBiasConstantFactor,
float depthBiasSlopeFactor,
float depthBiasClamp) {
bool isDynamic = true;
MVKDepthBias mtlDepthBias = {
.depthBiasConstantFactor = depthBiasConstantFactor,
.depthBiasSlopeFactor = depthBiasSlopeFactor,
.depthBiasClamp = depthBiasClamp
};
setMTLContent(DepthBias);
void MVKRenderingCommandEncoderState::setDepthBias(MVKDepthBias depthBias, bool isDynamic) {
setMTLContent(DepthBias, depthBias);
}
void MVKRenderingCommandEncoderState::setDepthBiasEnable(VkBool32 depthBiasEnable) {
bool isDynamic = true;
bool mtlDepthBiasEnable = static_cast<bool>(depthBiasEnable);
setMTLContent(DepthBiasEnable);
void MVKRenderingCommandEncoderState::setDepthBiasEnable(VkBool32 depthBiasEnable, bool isDynamic) {
setMTLContent(DepthBiasEnable, static_cast<bool>(depthBiasEnable));
}
void MVKRenderingCommandEncoderState::setDepthClipEnable(bool depthClip, bool isDynamic) {
auto mtlDepthClipEnable = depthClip ? MTLDepthClipModeClip : MTLDepthClipModeClamp;
setMTLContent(DepthClipEnable);
setMTLContent(DepthClipEnable, depthClip ? MTLDepthClipModeClip : MTLDepthClipModeClamp);
}
void MVKRenderingCommandEncoderState::setDepthBounds(float minDepthBounds, float maxDepthBounds, bool isDynamic) {
MVKDepthBounds mtlDepthBounds = { minDepthBounds, maxDepthBounds };
setMTLContent(DepthBounds);
void MVKRenderingCommandEncoderState::setDepthBounds(MVKDepthBounds depthBounds, bool isDynamic) {
setMTLContent(DepthBounds, depthBounds);
}
void MVKRenderingCommandEncoderState::setDepthBoundsTestEnable(VkBool32 depthBoundsTestEnable, bool isDynamic) {
auto mtlDepthBoundsTestEnable = static_cast<bool>(depthBoundsTestEnable);
setMTLContent(DepthBoundsTestEnable);
setMTLContent(DepthBoundsTestEnable, static_cast<bool>(depthBoundsTestEnable));
}
void MVKRenderingCommandEncoderState::setStencilReferenceValues(const VkPipelineDepthStencilStateCreateInfo& vkDepthStencilInfo) {
bool isDynamic = false;
MVKStencilReference mtlStencilReference = {
.frontFaceValue = vkDepthStencilInfo.front.reference,
.backFaceValue = vkDepthStencilInfo.back.reference
};
setMTLContent(StencilReference);
MVKStencilReference mtlStencilReference = { vkDepthStencilInfo.front.reference, vkDepthStencilInfo.back.reference };
setMTLContent(StencilReference, &mtlStencilReference);
}
void MVKRenderingCommandEncoderState::setStencilReferenceValues(VkStencilFaceFlags faceMask, uint32_t stencilReference) {
@ -393,7 +363,7 @@ void MVKRenderingCommandEncoderState::setStencilReferenceValues(VkStencilFaceFla
MVKStencilReference mtlStencilReference = _mtlStencilReference[StateScope::Dynamic];
if (shouldUpdateFace(FRONT)) { mtlStencilReference.frontFaceValue = stencilReference; }
if (shouldUpdateFace(BACK)) { mtlStencilReference.backFaceValue = stencilReference; }
setMTLContent(StencilReference);
setMTLContent(StencilReference, &mtlStencilReference);
}
void MVKRenderingCommandEncoderState::setViewports(const MVKArrayRef<VkViewport> viewports,
@ -408,7 +378,7 @@ void MVKRenderingCommandEncoderState::setViewports(const MVKArrayRef<VkViewport>
mtlViewports.viewports[firstViewport + vpIdx] = mvkMTLViewportFromVkViewport(viewports[vpIdx]);
mtlViewports.viewportCount = max(mtlViewports.viewportCount, vpIdx + 1);
}
setMTLContent(Viewports);
setMTLContent(Viewports, &mtlViewports);
}
void MVKRenderingCommandEncoderState::setScissors(const MVKArrayRef<VkRect2D> scissors,
@ -423,17 +393,15 @@ void MVKRenderingCommandEncoderState::setScissors(const MVKArrayRef<VkRect2D> sc
mtlScissors.scissors[firstScissor + sIdx] = mvkMTLScissorRectFromVkRect2D(scissors[sIdx]);
mtlScissors.scissorCount = max(mtlScissors.scissorCount, sIdx + 1);
}
setMTLContent(Scissors);
setMTLContent(Scissors, &mtlScissors);
}
void MVKRenderingCommandEncoderState::setPrimitiveRestartEnable(VkBool32 primitiveRestartEnable, bool isDynamic) {
bool mtlPrimitiveRestartEnable = static_cast<bool>(primitiveRestartEnable);
setMTLContent(PrimitiveRestartEnable);
setMTLContent(PrimitiveRestartEnable, static_cast<bool>(primitiveRestartEnable));
}
void MVKRenderingCommandEncoderState::setRasterizerDiscardEnable(VkBool32 rasterizerDiscardEnable, bool isDynamic) {
bool mtlRasterizerDiscardEnable = static_cast<bool>(rasterizerDiscardEnable);
setMTLContent(RasterizerDiscardEnable);
setMTLContent(RasterizerDiscardEnable, static_cast<bool>(rasterizerDiscardEnable));
}
// This value is retrieved, not encoded, so don't mark this encoder as dirty.

View File

@ -307,9 +307,7 @@ void MVKGraphicsPipeline::encode(MVKCommandEncoder* cmdEncoder, uint32_t stage)
cmdEncoder->_renderingState.setPrimitiveTopology(_vkPrimitiveTopology, false);
cmdEncoder->_renderingState.setPrimitiveRestartEnable(_primitiveRestartEnable, false);
cmdEncoder->_renderingState.setBlendConstants(_blendConstants, false);
if (_device->_enabledFeatures.depthBounds) {
cmdEncoder->_renderingState.setDepthBounds(_depthStencilInfo.minDepthBounds, _depthStencilInfo.maxDepthBounds, false);
}
cmdEncoder->_renderingState.setDepthBounds({_depthStencilInfo.minDepthBounds, _depthStencilInfo.maxDepthBounds}, false);
cmdEncoder->_renderingState.setStencilReferenceValues(_depthStencilInfo);
cmdEncoder->_renderingState.setViewports(_viewports.contents(), 0, false);
cmdEncoder->_renderingState.setScissors(_scissors.contents(), 0, false);

View File

@ -41,15 +41,17 @@
if ([self respondsToSelector: @selector(setInputPrimitiveTopology:)]) { [self setInputPrimitiveTopology:topology]; }
}
#if MVK_USE_METAL_PRIVATE_API
-(NSUInteger) sampleMaskMVK {
#if MVK_USE_METAL_PRIVATE_API
if ( [self respondsToSelector: @selector(sampleMask)] ) { return self.sampleMask; }
#endif
return 0xFFFFFFFFFFFFFFFFULL;
}
-(void) setSampleMaskMVK: (NSUInteger) mask {
#if MVK_USE_METAL_PRIVATE_API
if ([self respondsToSelector: @selector(setSampleMask:)]) { self.sampleMask = mask; }
}
#endif
}
@end

View File

@ -54,15 +54,17 @@
#endif
}
#if MVK_USE_METAL_PRIVATE_API
-(float) lodBiasMVK {
#if MVK_USE_METAL_PRIVATE_API
if ( [self respondsToSelector: @selector(lodBias)] ) { return self.lodBias; }
#endif
return 0.0f;
}
-(void) setLodBiasMVK: (float) bias {
#if MVK_USE_METAL_PRIVATE_API
if ( [self respondsToSelector: @selector(setLodBias:)] ) { self.lodBias = bias; }
}
#endif
}
@end

View File

@ -1477,7 +1477,7 @@ MVK_PUBLIC_VULKAN_SYMBOL void vkCmdSetDepthBias(
float depthBiasSlopeFactor) {
MVKTraceVulkanCallStart();
MVKAddCmd(SetDepthBias, commandBuffer,depthBiasConstantFactor, depthBiasClamp, depthBiasSlopeFactor);
MVKAddCmd(SetDepthBias, commandBuffer, {depthBiasConstantFactor, depthBiasClamp, depthBiasSlopeFactor} );
MVKTraceVulkanCallEnd();
}
@ -1498,7 +1498,7 @@ MVK_PUBLIC_VULKAN_SYMBOL void vkCmdSetDepthBounds(
float maxDepthBounds) {
MVKTraceVulkanCallStart();
MVKAddCmd(SetDepthBounds, commandBuffer, minDepthBounds, maxDepthBounds);
MVKAddCmd(SetDepthBounds, commandBuffer, {minDepthBounds, maxDepthBounds});
MVKTraceVulkanCallEnd();
}