| 1 |
// Copyright 2022 Dolphin Emulator Project |
| 2 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
|
| 4 |
#include "VideoBackends/Metal/MTLVertexFormat.h" |
| 5 |
|
| 6 |
#include "VideoCommon/VertexShaderGen.h" |
| 7 |
|
| 8 |
static MTLVertexFormat ConvertFormat(ComponentFormat format, int count, bool int_format) |
| 9 |
{ |
| 10 |
// clang-format off |
| 11 |
if (int_format) |
| 12 |
{ |
| 13 |
switch (format) |
| 14 |
{ |
| 15 |
case ComponentFormat::UByte: |
| 16 |
switch (count) |
| 17 |
{ |
| 18 |
case 1: return MTLVertexFormatUChar; |
| 19 |
case 2: return MTLVertexFormatUChar2; |
| 20 |
case 3: return MTLVertexFormatUChar3; |
| 21 |
case 4: return MTLVertexFormatUChar4; |
| 22 |
default: return MTLVertexFormatInvalid; |
| 23 |
} |
| 24 |
case ComponentFormat::Byte: |
| 25 |
switch (count) |
| 26 |
{ |
| 27 |
case 1: return MTLVertexFormatChar; |
| 28 |
case 2: return MTLVertexFormatChar2; |
| 29 |
case 3: return MTLVertexFormatChar3; |
| 30 |
case 4: return MTLVertexFormatChar4; |
| 31 |
default: return MTLVertexFormatInvalid; |
| 32 |
} |
| 33 |
case ComponentFormat::UShort: |
| 34 |
switch (count) |
| 35 |
{ |
| 36 |
case 1: return MTLVertexFormatUShort; |
| 37 |
case 2: return MTLVertexFormatUShort2; |
| 38 |
case 3: return MTLVertexFormatUShort3; |
| 39 |
case 4: return MTLVertexFormatUShort4; |
| 40 |
default: return MTLVertexFormatInvalid; |
| 41 |
} |
| 42 |
case ComponentFormat::Short: |
| 43 |
switch (count) |
| 44 |
{ |
| 45 |
case 1: return MTLVertexFormatShort; |
| 46 |
case 2: return MTLVertexFormatShort2; |
| 47 |
case 3: return MTLVertexFormatShort3; |
| 48 |
case 4: return MTLVertexFormatShort4; |
| 49 |
default: return MTLVertexFormatInvalid; |
| 50 |
} |
| 51 |
case ComponentFormat::Float: |
| 52 |
case ComponentFormat::InvalidFloat5: |
| 53 |
case ComponentFormat::InvalidFloat6: |
| 54 |
case ComponentFormat::InvalidFloat7: |
| 55 |
switch (count) |
| 56 |
{ |
| 57 |
case 1: return MTLVertexFormatFloat; |
| 58 |
case 2: return MTLVertexFormatFloat2; |
| 59 |
case 3: return MTLVertexFormatFloat3; |
| 60 |
case 4: return MTLVertexFormatFloat4; |
| 61 |
default: return MTLVertexFormatInvalid; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
else |
| 66 |
{ |
| 67 |
switch (format) |
| 68 |
{ |
| 69 |
case ComponentFormat::UByte: |
| 70 |
switch (count) |
| 71 |
{ |
| 72 |
case 1: return MTLVertexFormatUCharNormalized; |
| 73 |
case 2: return MTLVertexFormatUChar2Normalized; |
| 74 |
case 3: return MTLVertexFormatUChar3Normalized; |
| 75 |
case 4: return MTLVertexFormatUChar4Normalized; |
| 76 |
default: return MTLVertexFormatInvalid; |
| 77 |
} |
| 78 |
case ComponentFormat::Byte: |
| 79 |
switch (count) |
| 80 |
{ |
| 81 |
case 1: return MTLVertexFormatCharNormalized; |
| 82 |
case 2: return MTLVertexFormatChar2Normalized; |
| 83 |
case 3: return MTLVertexFormatChar3Normalized; |
| 84 |
case 4: return MTLVertexFormatChar4Normalized; |
| 85 |
default: return MTLVertexFormatInvalid; |
| 86 |
} |
| 87 |
case ComponentFormat::UShort: |
| 88 |
switch (count) |
| 89 |
{ |
| 90 |
case 1: return MTLVertexFormatUShortNormalized; |
| 91 |
case 2: return MTLVertexFormatUShort2Normalized; |
| 92 |
case 3: return MTLVertexFormatUShort3Normalized; |
| 93 |
case 4: return MTLVertexFormatUShort4Normalized; |
| 94 |
default: return MTLVertexFormatInvalid; |
| 95 |
} |
| 96 |
case ComponentFormat::Short: |
| 97 |
switch (count) |
| 98 |
{ |
| 99 |
case 1: return MTLVertexFormatShortNormalized; |
| 100 |
case 2: return MTLVertexFormatShort2Normalized; |
| 101 |
case 3: return MTLVertexFormatShort3Normalized; |
| 102 |
case 4: return MTLVertexFormatShort4Normalized; |
| 103 |
default: return MTLVertexFormatInvalid; |
| 104 |
} |
| 105 |
case ComponentFormat::Float: |
| 106 |
case ComponentFormat::InvalidFloat5: |
| 107 |
case ComponentFormat::InvalidFloat6: |
| 108 |
case ComponentFormat::InvalidFloat7: |
| 109 |
switch (count) |
| 110 |
{ |
| 111 |
case 1: return MTLVertexFormatFloat; |
| 112 |
case 2: return MTLVertexFormatFloat2; |
| 113 |
case 3: return MTLVertexFormatFloat3; |
| 114 |
case 4: return MTLVertexFormatFloat4; |
| 115 |
default: return MTLVertexFormatInvalid; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
// clang-format on |
| 120 |
} |
| 121 |
|
| 122 |
static void SetAttribute(MTLVertexDescriptor* desc, ShaderAttrib attribute, |
| 123 |
const AttributeFormat& format) |
| 124 |
{ |
| 125 |
if (!format.enable) |
| 126 |
return; |
| 127 |
MTLVertexAttributeDescriptor* attr_desc = |
| 128 |
[[desc attributes] objectAtIndexedSubscript:(u32)attribute]; |
| 129 |
[attr_desc setFormat:ConvertFormat(format.type, format.components, format.integer)]; |
| 130 |
[attr_desc setOffset:format.offset]; |
| 131 |
[attr_desc setBufferIndex:0]; |
| 132 |
} |
| 133 |
|
| 134 |
template <size_t N> |
| 135 |
static void SetAttributes(MTLVertexDescriptor* desc, ShaderAttrib attribute, |
| 136 |
const std::array<AttributeFormat, N>& format) |
| 137 |
{ |
| 138 |
for (size_t i = 0; i < N; ++i) |
| 139 |
SetAttribute(desc, attribute + i, format[i]); |
| 140 |
} |
| 141 |
|
| 142 |
Metal::VertexFormat::VertexFormat(const PortableVertexDeclaration& vtx_decl) |
| 143 |
: NativeVertexFormat(vtx_decl), m_desc(MRCTransfer([MTLVertexDescriptor new])) |
| 144 |
{ |
| 145 |
[[[m_desc layouts] objectAtIndexedSubscript:0] setStride:vtx_decl.stride]; |
| 146 |
SetAttribute(m_desc, ShaderAttrib::Position, vtx_decl.position); |
| 147 |
SetAttributes(m_desc, ShaderAttrib::Normal, vtx_decl.normals); |
| 148 |
SetAttributes(m_desc, ShaderAttrib::Color0, vtx_decl.colors); |
| 149 |
SetAttributes(m_desc, ShaderAttrib::TexCoord0, vtx_decl.texcoords); |
| 150 |
SetAttribute(m_desc, ShaderAttrib::PositionMatrix, vtx_decl.posmtx); |
| 151 |
} |