Skip to content

Commit

Permalink
Merge pull request #2155 from billhollings/private-api-fixes
Browse files Browse the repository at this point in the history
Consolidation and fixes for MVK_USE_METAL_PRIVATE_API functionality.
  • Loading branch information
billhollings authored Feb 12, 2024
2 parents 885960a + efaae79 commit 4a04f88
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 119 deletions.
10 changes: 5 additions & 5 deletions Docs/Whats_New.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 3 additions & 20 deletions MoltenVK/MoltenVK/Commands/MVKCmdRendering.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "MVKCommand.h"
#include "MVKDevice.h"
#include "MVKSmallVector.h"
#include "MVKCommandEncoderState.h"

#import <Metal/Metal.h>

Expand Down Expand Up @@ -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;
};


Expand Down Expand Up @@ -378,28 +370,19 @@ class MVKCmdSetDepthCompareOp : public MVKSingleValueCommand<VkCompareOp> {
#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:
Expand Down
27 changes: 3 additions & 24 deletions MoltenVK/MoltenVK/Commands/MVKCmdRendering.mm
Original file line number Diff line number Diff line change
Expand Up @@ -261,29 +261,16 @@
#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);
}


#pragma mark -
#pragma mark MVKCmdSetDepthBiasEnable

void MVKCmdSetDepthBiasEnable::encode(MVKCommandEncoder* cmdEncoder) {
cmdEncoder->_renderingState.setDepthBiasEnable(_value);
cmdEncoder->_renderingState.setDepthBiasEnable(_value, true);
}


Expand Down Expand Up @@ -330,16 +317,8 @@
#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);
}


Expand Down
14 changes: 8 additions & 6 deletions MoltenVK/MoltenVK/Commands/MVKCommandEncoderState.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ class MVKDepthStencilCommandEncoderState : public MVKCommandEncoderState {

struct MVKDepthBias {
float depthBiasConstantFactor;
float depthBiasSlopeFactor;
float depthBiasClamp;
float depthBiasSlopeFactor;
};

struct MVKDepthBounds {
Expand Down Expand Up @@ -273,11 +273,10 @@ class MVKRenderingCommandEncoderState : public MVKCommandEncoderState {
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);
Expand Down Expand Up @@ -310,7 +309,7 @@ class MVKRenderingCommandEncoderState : public MVKCommandEncoderState {
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;
Expand All @@ -319,6 +318,9 @@ class MVKRenderingCommandEncoderState : public MVKCommandEncoderState {
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] = {};
Expand Down
78 changes: 23 additions & 55 deletions MoltenVK/MoltenVK/Commands/MVKCommandEncoderState.mm
Original file line number Diff line number Diff line change
Expand Up @@ -303,97 +303,67 @@
#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) {
bool isDynamic = true;
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,
Expand All @@ -408,7 +378,7 @@
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,
Expand All @@ -423,17 +393,15 @@
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.
Expand Down
4 changes: 1 addition & 3 deletions MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,7 @@
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);
Expand Down
6 changes: 4 additions & 2 deletions MoltenVK/MoltenVK/OS/MTLRenderPipelineDescriptor+MoltenVK.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ -(void) setInputPrimitiveTopologyMVK: (MTLPrimitiveTopologyClass) topology {
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
6 changes: 4 additions & 2 deletions MoltenVK/MoltenVK/OS/MTLSamplerDescriptor+MoltenVK.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ -(void) setBorderColorMVK: (NSUInteger) color {
#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
4 changes: 2 additions & 2 deletions MoltenVK/MoltenVK/Vulkan/vulkan.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -1498,7 +1498,7 @@ MVK_PUBLIC_VULKAN_SYMBOL void vkCmdSetDepthBounds(
float maxDepthBounds) {

MVKTraceVulkanCallStart();
MVKAddCmd(SetDepthBounds, commandBuffer, minDepthBounds, maxDepthBounds);
MVKAddCmd(SetDepthBounds, commandBuffer, {minDepthBounds, maxDepthBounds});
MVKTraceVulkanCallEnd();
}

Expand Down

0 comments on commit 4a04f88

Please sign in to comment.