Fix time and space inefficiencies caused by missed
shader cache lookup hits in MVKShaderLibraryCache.
Rename MSLResourceBinding::mtlTextureType, MSLResourceBinding::isUsedByShader, and
MSLShaderInput::isUsedByShader to add a prefix out*, to clarify that those variables
are output from the conversion process, instead of input to the conversion process.
Don't use MSLResourceBinding::outMTLTextureType when looking up cached shaders.
diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm b/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm
index 3070935..1b4cb43 100644
--- a/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm
+++ b/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm
@@ -2137,16 +2137,16 @@
void serialize(Archive & archive, MSLShaderInput& si) {
archive(si.shaderInput,
si.binding,
- si.isUsedByShader);
+ si.outIsUsedByShader);
}
template<class Archive>
void serialize(Archive & archive, MSLResourceBinding& rb) {
archive(rb.resourceBinding,
rb.constExprSampler,
- rb.mtlTextureType,
+ rb.outMTLTextureType,
rb.requiresConstExprSampler,
- rb.isUsedByShader);
+ rb.outIsUsedByShader);
}
template<class Archive>
diff --git a/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.h b/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.h
index 0f50614..899c29b 100644
--- a/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.h
+++ b/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.h
@@ -64,7 +64,7 @@
/**
* Defines MSL characteristics of a vertex attribute at a particular location.
*
- * The isUsedByShader flag is set to true during conversion of SPIR-V to MSL if the shader
+ * The outIsUsedByShader flag is set to true during conversion of SPIR-V to MSL if the shader
* makes use of this vertex attribute. This allows a pipeline to be optimized, and for two
* shader conversion configurations to be compared only against the attributes that are
* actually used by the shader.
@@ -74,13 +74,12 @@
*/
typedef struct MSLShaderInput {
SPIRV_CROSS_NAMESPACE::MSLShaderInput shaderInput;
-
uint32_t binding = 0;
- bool isUsedByShader = false;
+ bool outIsUsedByShader = false;
/**
* Returns whether the specified vertex attribute match this one.
- * It does if all corresponding elements except isUsedByShader are equal.
+ * It does if all corresponding elements except outIsUsedByShader are equal.
*/
bool matches(const MSLShaderInput& other) const;
@@ -90,17 +89,20 @@
* Matches the binding index of a MSL resource for a binding within a descriptor set.
* Taken together, the stage, desc_set and binding combine to form a reference to a resource
* descriptor used in a particular shading stage. Generally, only one of the buffer, texture,
- * or sampler elements will be populated. The isUsedByShader flag is set to true during
+ * or sampler elements will be populated. The outIsUsedByShader flag is set to true during
* compilation of SPIR-V to MSL if the shader makes use of this vertex attribute.
*
* If requiresConstExprSampler is true, the resource is a sampler whose content must be
* hardcoded into the MSL as a constexpr type, instead of passed in as a runtime-bound variable.
* The content of that constexpr sampler is defined in the constExprSampler parameter.
*
- * The isUsedByShader flag is set to true during conversion of SPIR-V to MSL if the shader
+ * The outIsUsedByShader and outMTLTextureType values are set by the shader converter
+ * based on the content of the SPIR-V (and resulting MSL), and provide feedback to the
+ * pipeline about shader content. The outIsUsedByShader value is set to true if the shader
* makes use of this resource binding. This allows a pipeline to be optimized, and for two
* shader conversion configurations to be compared only against the resource bindings that
- * are actually used by the shader.
+ * are actually used by the shader. The outMTLTextureType value provides feedback to the
+ * pipeline regarding the texture type expected by the shader.
*
* THIS STRUCT IS STREAMED OUT AS PART OF THE PIEPLINE CACHE.
* CHANGES TO THIS STRUCT SHOULD BE CAPTURED IN THE STREAMING LOGIC OF THE PIPELINE CACHE.
@@ -108,14 +110,13 @@
typedef struct MSLResourceBinding {
SPIRV_CROSS_NAMESPACE::MSLResourceBinding resourceBinding;
SPIRV_CROSS_NAMESPACE::MSLConstexprSampler constExprSampler;
- MTLTextureType mtlTextureType = MTLTextureType2D;
+ MTLTextureType outMTLTextureType = MTLTextureType2D;
bool requiresConstExprSampler = false;
-
- bool isUsedByShader = false;
+ bool outIsUsedByShader = false;
/**
* Returns whether the specified resource binding match this one.
- * It does if all corresponding elements except isUsedByShader are equal.
+ * It does if all corresponding elements except outMTLTextureType and outIsUsedByShader are equal.
*/
bool matches(const MSLResourceBinding& other) const;
diff --git a/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.mm b/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.mm
index 5e5e86b..fae342b 100644
--- a/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.mm
+++ b/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.mm
@@ -113,8 +113,6 @@
if (resourceBinding.msl_buffer != other.resourceBinding.msl_buffer) { return false; }
if (resourceBinding.msl_texture != other.resourceBinding.msl_texture) { return false; }
if (resourceBinding.msl_sampler != other.resourceBinding.msl_sampler) { return false; }
- if (mtlTextureType != other.mtlTextureType) { return false; }
-
if (requiresConstExprSampler != other.requiresConstExprSampler) { return false; }
// If requiresConstExprSampler is false, constExprSampler can be ignored
@@ -167,7 +165,7 @@
// Check them all in case inactive VA's duplicate locations used by active VA's.
MVK_PUBLIC_SYMBOL bool SPIRVToMSLConversionConfiguration::isShaderInputLocationUsed(uint32_t location) const {
for (auto& si : shaderInputs) {
- if ((si.shaderInput.location == location) && si.isUsedByShader) { return true; }
+ if ((si.shaderInput.location == location) && si.outIsUsedByShader) { return true; }
}
return false;
}
@@ -175,7 +173,7 @@
MVK_PUBLIC_SYMBOL uint32_t SPIRVToMSLConversionConfiguration::countShaderInputsAt(uint32_t binding) const {
uint32_t siCnt = 0;
for (auto& si : shaderInputs) {
- if ((si.binding == binding) && si.isUsedByShader) { siCnt++; }
+ if ((si.binding == binding) && si.outIsUsedByShader) { siCnt++; }
}
return siCnt;
}
@@ -184,7 +182,7 @@
for (auto& rb : resourceBindings) {
auto& rbb = rb.resourceBinding;
if (rbb.desc_set == descSet && rbb.binding == binding) {
- return rb.isUsedByShader;
+ return rb.outIsUsedByShader;
}
}
return false;
@@ -193,16 +191,16 @@
MVK_PUBLIC_SYMBOL MTLTextureType SPIRVToMSLConversionConfiguration::getMTLTextureType(uint32_t descSet, uint32_t binding) const {
for (auto& rb : resourceBindings) {
auto& rbb = rb.resourceBinding;
- if (rb.isUsedByShader && rbb.desc_set == descSet && rbb.binding == binding) {
- return rb.mtlTextureType;
+ if (rb.outIsUsedByShader && rbb.desc_set == descSet && rbb.binding == binding) {
+ return rb.outMTLTextureType;
}
}
return MTLTextureType2D;
}
MVK_PUBLIC_SYMBOL void SPIRVToMSLConversionConfiguration::markAllInputsAndResourcesUsed() {
- for (auto& si : shaderInputs) { si.isUsedByShader = true; }
- for (auto& rb : resourceBindings) { rb.isUsedByShader = true; }
+ for (auto& si : shaderInputs) { si.outIsUsedByShader = true; }
+ for (auto& rb : resourceBindings) { rb.outIsUsedByShader = true; }
}
MVK_PUBLIC_SYMBOL bool SPIRVToMSLConversionConfiguration::matches(const SPIRVToMSLConversionConfiguration& other) const {
@@ -210,11 +208,11 @@
if ( !options.matches(other.options) ) { return false; }
for (const auto& si : shaderInputs) {
- if (si.isUsedByShader && !containsMatching(other.shaderInputs, si)) { return false; }
+ if (si.outIsUsedByShader && !containsMatching(other.shaderInputs, si)) { return false; }
}
for (const auto& rb : resourceBindings) {
- if (rb.isUsedByShader && !containsMatching(other.resourceBindings, rb)) { return false; }
+ if (rb.outIsUsedByShader && !containsMatching(other.resourceBindings, rb)) { return false; }
}
for (uint32_t dsIdx : discreteDescriptorSets) {
@@ -228,18 +226,19 @@
MVK_PUBLIC_SYMBOL void SPIRVToMSLConversionConfiguration::alignWith(const SPIRVToMSLConversionConfiguration& srcContext) {
for (auto& si : shaderInputs) {
- si.isUsedByShader = false;
+ si.outIsUsedByShader = false;
for (auto& srcSI : srcContext.shaderInputs) {
- if (si.matches(srcSI)) { si.isUsedByShader = srcSI.isUsedByShader; }
+ if (si.matches(srcSI)) { si.outIsUsedByShader = srcSI.outIsUsedByShader; }
}
}
for (auto& rb : resourceBindings) {
- rb.isUsedByShader = false;
+ rb.outIsUsedByShader = false;
+ rb.outMTLTextureType = MTLTextureType2D;
for (auto& srcRB : srcContext.resourceBindings) {
if (rb.matches(srcRB)) {
- rb.mtlTextureType = srcRB.mtlTextureType;
- rb.isUsedByShader = srcRB.isUsedByShader;
+ rb.outMTLTextureType = srcRB.outMTLTextureType;
+ rb.outIsUsedByShader = srcRB.outIsUsedByShader;
}
}
}
@@ -355,15 +354,15 @@
_shaderConversionResults.needsViewRangeBuffer = pMSLCompiler && pMSLCompiler->needs_view_mask_buffer();
for (auto& ctxSI : context.shaderInputs) {
- ctxSI.isUsedByShader = pMSLCompiler->is_msl_shader_input_used(ctxSI.shaderInput.location);
+ ctxSI.outIsUsedByShader = pMSLCompiler->is_msl_shader_input_used(ctxSI.shaderInput.location);
}
for (auto& ctxRB : context.resourceBindings) {
- ctxRB.mtlTextureType = getMTLTextureType(pMSLCompiler,
- ctxRB.resourceBinding.desc_set,
- ctxRB.resourceBinding.binding);
- ctxRB.isUsedByShader = pMSLCompiler->is_msl_resource_binding_used(ctxRB.resourceBinding.stage,
- ctxRB.resourceBinding.desc_set,
- ctxRB.resourceBinding.binding);
+ ctxRB.outMTLTextureType = getMTLTextureType(pMSLCompiler,
+ ctxRB.resourceBinding.desc_set,
+ ctxRB.resourceBinding.binding);
+ ctxRB.outIsUsedByShader = pMSLCompiler->is_msl_resource_binding_used(ctxRB.resourceBinding.stage,
+ ctxRB.resourceBinding.desc_set,
+ ctxRB.resourceBinding.binding);
}
delete pMSLCompiler;