Fix Metal vertex format lookup logic.

- Remove MVKPixelFormats::_mtlFormatDescIndicesByMTLVertexFormats and
  index into _mtlVertexFormatDescriptions using MTLVertexFormat directly.
- Fix assertion to test MTLVertexFormat < _mtlVertexFormatCount.
This commit is contained in:
Bill Hollings 2023-12-27 18:38:23 -05:00
parent 76233bc654
commit fe65485bfb
2 changed files with 12 additions and 19 deletions

View File

@ -459,6 +459,4 @@ protected:
// Outliers are mapped by a map. // Outliers are mapped by a map.
uint16_t _mtlFormatDescIndicesByMTLPixelFormatsCore[_mtlPixelFormatCoreCount]; uint16_t _mtlFormatDescIndicesByMTLPixelFormatsCore[_mtlPixelFormatCoreCount];
std::unordered_map<NSUInteger, uint32_t> _mtlFormatDescIndicesByMTLPixelFormatsExt; std::unordered_map<NSUInteger, uint32_t> _mtlFormatDescIndicesByMTLPixelFormatsExt;
uint16_t _mtlFormatDescIndicesByMTLVertexFormats[_mtlVertexFormatCount];
}; };

View File

@ -783,8 +783,7 @@ MVKMTLFormatDesc& MVKPixelFormats::getMTLPixelFormatDesc(MTLPixelFormat mtlForma
// Return a reference to the Metal format descriptor corresponding to the MTLVertexFormat. // Return a reference to the Metal format descriptor corresponding to the MTLVertexFormat.
MVKMTLFormatDesc& MVKPixelFormats::getMTLVertexFormatDesc(MTLVertexFormat mtlFormat) { MVKMTLFormatDesc& MVKPixelFormats::getMTLVertexFormatDesc(MTLVertexFormat mtlFormat) {
uint16_t fmtIdx = (mtlFormat < _mtlVertexFormatCount) ? _mtlFormatDescIndicesByMTLVertexFormats[mtlFormat] : 0; return _mtlVertexFormatDescriptions[mtlFormat < _mtlVertexFormatCount ? mtlFormat : 0];
return _mtlVertexFormatDescriptions[fmtIdx];
} }
@ -1312,21 +1311,24 @@ void MVKPixelFormats::initMTLPixelFormatCapabilities() {
} }
#define addMTLVertexFormatDesc(MTL_VTX_FMT, IOS_CAPS, MACOS_CAPS) \ #define addMTLVertexFormatDesc(MTL_VTX_FMT, IOS_CAPS, MACOS_CAPS) \
MVKAssert(fmtIdx < _mtlVertexFormatCount, "Attempting to describe %d MTLVertexFormats, but only have space for %d. Increase the value of _mtlVertexFormatCount", fmtIdx + 1, _mtlVertexFormatCount); \ mtlVtxFmt = MTLVertexFormat ##MTL_VTX_FMT; \
_mtlVertexFormatDescriptions[fmtIdx++] = { .mtlVertexFormat = MTLVertexFormat ##MTL_VTX_FMT, VK_FORMAT_UNDEFINED, \ if (mtlVtxFmt < _mtlVertexFormatCount) { \
mvkSelectPlatformValue<MVKMTLFmtCaps>(kMVKMTLFmtCaps ##MACOS_CAPS, kMVKMTLFmtCaps ##IOS_CAPS), \ _mtlVertexFormatDescriptions[mtlVtxFmt] = { .mtlVertexFormat = mtlVtxFmt, VK_FORMAT_UNDEFINED, \
MVKMTLViewClass::None, MTLPixelFormatInvalid, "MTLVertexFormat" #MTL_VTX_FMT } mvkSelectPlatformValue<MVKMTLFmtCaps>(kMVKMTLFmtCaps ##MACOS_CAPS, kMVKMTLFmtCaps ##IOS_CAPS), \
MVKMTLViewClass::None, MTLPixelFormatInvalid, "MTLVertexFormat" #MTL_VTX_FMT }; \
} else { \
MVKAssert(false, "Attempting to describe at least %lu MTLVertexFormats, but only have space for %d. Increase the value of _mtlVertexFormatCount", mtlVtxFmt + 1, _mtlVertexFormatCount); \
}
void MVKPixelFormats::initMTLVertexFormatCapabilities() { void MVKPixelFormats::initMTLVertexFormatCapabilities() {
mvkClear(_mtlVertexFormatDescriptions, _mtlVertexFormatCount); mvkClear(_mtlVertexFormatDescriptions, _mtlVertexFormatCount);
uint32_t fmtIdx = 0; MTLVertexFormat mtlVtxFmt = MTLVertexFormatInvalid;
// When adding to this list, be sure to ensure _mtlVertexFormatCount is large enough for the format count // When adding to this list, be sure to ensure _mtlVertexFormatCount is large enough for the format count
// MTLVertexFormatInvalid must come first. addMTLVertexFormatDesc( Invalid, None, None ); // MTLVertexFormatInvalid must come first.
addMTLVertexFormatDesc( Invalid, None, None );
addMTLVertexFormatDesc( UChar2Normalized, Vertex, Vertex ); addMTLVertexFormatDesc( UChar2Normalized, Vertex, Vertex );
addMTLVertexFormatDesc( Char2Normalized, Vertex, Vertex ); addMTLVertexFormatDesc( Char2Normalized, Vertex, Vertex );
@ -1399,9 +1401,8 @@ void MVKPixelFormats::initMTLVertexFormatCapabilities() {
// Populates the Metal lookup maps // Populates the Metal lookup maps
void MVKPixelFormats::buildMTLFormatMaps() { void MVKPixelFormats::buildMTLFormatMaps() {
// Set all MTLPixelFormats and MTLVertexFormats to undefined/invalid // Set all MTLPixelFormats lookups to undefined/invalid
mvkClear(_mtlFormatDescIndicesByMTLPixelFormatsCore, _mtlPixelFormatCoreCount); mvkClear(_mtlFormatDescIndicesByMTLPixelFormatsCore, _mtlPixelFormatCoreCount);
mvkClear(_mtlFormatDescIndicesByMTLVertexFormats, _mtlVertexFormatCount);
// Build lookup table for MTLPixelFormat specs. // Build lookup table for MTLPixelFormat specs.
// For most Metal format values, which are small and consecutive, use a simple lookup array. // For most Metal format values, which are small and consecutive, use a simple lookup array.
@ -1416,12 +1417,6 @@ void MVKPixelFormats::buildMTLFormatMaps() {
} }
} }
} }
// Build lookup table for MTLVertexFormat specs
for (uint32_t fmtIdx = 0; fmtIdx < _mtlVertexFormatCount; fmtIdx++) {
MTLVertexFormat fmt = _mtlVertexFormatDescriptions[fmtIdx].mtlVertexFormat;
if (fmt) { _mtlFormatDescIndicesByMTLVertexFormats[fmt] = fmtIdx; }
}
} }
// If the device supports the feature set, add additional capabilities to a MTLPixelFormat // If the device supports the feature set, add additional capabilities to a MTLPixelFormat