|
| 1 | +/* |
| 2 | + * MTTypes.mm |
| 3 | + * |
| 4 | + * This file is part of the "LLGL" project (Copyright (c) 2015-2018 by Lukas Hermanns) |
| 5 | + * See "LICENSE.txt" for license information. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "MTTypes.h" |
| 9 | +#include <stdexcept> |
| 10 | +#include <string> |
| 11 | + |
| 12 | + |
| 13 | +namespace LLGL |
| 14 | +{ |
| 15 | + |
| 16 | +namespace MTTypes |
| 17 | +{ |
| 18 | + |
| 19 | + |
| 20 | +/* ----- Map functions ----- */ |
| 21 | + |
| 22 | +[[noreturn]] |
| 23 | +void MapFailed(const std::string& typeName, const std::string& mtlTypeName) |
| 24 | +{ |
| 25 | + throw std::invalid_argument("failed to map <LLGL::" + typeName + "> to <" + mtlTypeName + "> Metal parameter"); |
| 26 | +} |
| 27 | + |
| 28 | +MTLDataType Map(const DataType dataType) |
| 29 | +{ |
| 30 | + switch (dataType) |
| 31 | + { |
| 32 | + case DataType::Int8: return MTLDataTypeChar; |
| 33 | + case DataType::UInt8: return MTLDataTypeUChar; |
| 34 | + |
| 35 | + case DataType::Int16: return MTLDataTypeShort; |
| 36 | + case DataType::UInt16: return MTLDataTypeUShort; |
| 37 | + |
| 38 | + case DataType::Int32: return MTLDataTypeInt; |
| 39 | + case DataType::UInt32: return MTLDataTypeUInt; |
| 40 | + |
| 41 | + case DataType::Float: return MTLDataTypeFloat; |
| 42 | + case DataType::Double: break; |
| 43 | + } |
| 44 | + MapFailed("DataType", "MTLDataType"); |
| 45 | +} |
| 46 | + |
| 47 | +MTLDataType Map(const VectorType vectorType) |
| 48 | +{ |
| 49 | + switch (vectorType) |
| 50 | + { |
| 51 | + case VectorType::Float: return MTLDataTypeFloat; |
| 52 | + case VectorType::Float2: return MTLDataTypeFloat2; |
| 53 | + case VectorType::Float3: return MTLDataTypeFloat3; |
| 54 | + case VectorType::Float4: return MTLDataTypeFloat4; |
| 55 | + case VectorType::Double: break; |
| 56 | + case VectorType::Double2: break; |
| 57 | + case VectorType::Double3: break; |
| 58 | + case VectorType::Double4: break; |
| 59 | + case VectorType::Int: return MTLDataTypeInt; |
| 60 | + case VectorType::Int2: return MTLDataTypeInt2; |
| 61 | + case VectorType::Int3: return MTLDataTypeInt3; |
| 62 | + case VectorType::Int4: return MTLDataTypeInt4; |
| 63 | + case VectorType::UInt: return MTLDataTypeUInt; |
| 64 | + case VectorType::UInt2: return MTLDataTypeUInt2; |
| 65 | + case VectorType::UInt3: return MTLDataTypeUInt3; |
| 66 | + case VectorType::UInt4: return MTLDataTypeUInt4; |
| 67 | + } |
| 68 | + MapFailed("VectorType", "MTLDataType"); |
| 69 | +} |
| 70 | + |
| 71 | +MTLPixelFormat Map(const TextureFormat textureFormat) |
| 72 | +{ |
| 73 | + switch (textureFormat) |
| 74 | + { |
| 75 | + case TextureFormat::Unknown: break; |
| 76 | + |
| 77 | + /* --- Color formats --- */ |
| 78 | + case TextureFormat::R8: return MTLPixelFormatR8Unorm; |
| 79 | + case TextureFormat::R8Sgn: return MTLPixelFormatR8Snorm; |
| 80 | + |
| 81 | + case TextureFormat::R16: return MTLPixelFormatR16Unorm; |
| 82 | + case TextureFormat::R16Sgn: return MTLPixelFormatR16Snorm; |
| 83 | + case TextureFormat::R16Float: return MTLPixelFormatR16Float; |
| 84 | + |
| 85 | + case TextureFormat::R32UInt: return MTLPixelFormatR32Uint; |
| 86 | + case TextureFormat::R32SInt: return MTLPixelFormatR32Sint; |
| 87 | + case TextureFormat::R32Float: return MTLPixelFormatR32Float; |
| 88 | + |
| 89 | + case TextureFormat::RG8: return MTLPixelFormatRG8Unorm; |
| 90 | + case TextureFormat::RG8Sgn: return MTLPixelFormatRG8Snorm; |
| 91 | + |
| 92 | + case TextureFormat::RG16: return MTLPixelFormatRG16Unorm; |
| 93 | + case TextureFormat::RG16Sgn: return MTLPixelFormatRG16Snorm; |
| 94 | + case TextureFormat::RG16Float: return MTLPixelFormatRG16Float; |
| 95 | + |
| 96 | + case TextureFormat::RG32UInt: return MTLPixelFormatRG32Uint; |
| 97 | + case TextureFormat::RG32SInt: return MTLPixelFormatRG32Sint; |
| 98 | + case TextureFormat::RG32Float: return MTLPixelFormatRG32Float; |
| 99 | + |
| 100 | + case TextureFormat::RGB8: break; |
| 101 | + case TextureFormat::RGB8Sgn: break; |
| 102 | + |
| 103 | + case TextureFormat::RGB16: break; |
| 104 | + case TextureFormat::RGB16Sgn: break; |
| 105 | + case TextureFormat::RGB16Float: break; |
| 106 | + |
| 107 | + case TextureFormat::RGB32UInt: break; |
| 108 | + case TextureFormat::RGB32SInt: break; |
| 109 | + case TextureFormat::RGB32Float: break; |
| 110 | + |
| 111 | + case TextureFormat::RGBA8: return MTLPixelFormatRGBA8Unorm; |
| 112 | + case TextureFormat::RGBA8Sgn: return MTLPixelFormatRGBA8Snorm; |
| 113 | + |
| 114 | + case TextureFormat::RGBA16: return MTLPixelFormatRGBA16Unorm; |
| 115 | + case TextureFormat::RGBA16Sgn: return MTLPixelFormatRGBA16Snorm; |
| 116 | + case TextureFormat::RGBA16Float: return MTLPixelFormatRGBA16Float; |
| 117 | + |
| 118 | + case TextureFormat::RGBA32UInt: return MTLPixelFormatRGBA32Uint; |
| 119 | + case TextureFormat::RGBA32SInt: return MTLPixelFormatRGBA32Sint; |
| 120 | + case TextureFormat::RGBA32Float: return MTLPixelFormatRGBA32Float; |
| 121 | + |
| 122 | + /* --- Depth-stencil formats --- */ |
| 123 | + case TextureFormat::D32: return MTLPixelFormatDepth32Float; |
| 124 | + case TextureFormat::D24S8: return MTLPixelFormatDepth24Unorm_Stencil8; |
| 125 | + |
| 126 | + /* --- Compressed color formats --- */ |
| 127 | + case TextureFormat::RGB_DXT1: break; |
| 128 | + case TextureFormat::RGBA_DXT1: return MTLPixelFormatBC1_RGBA; |
| 129 | + case TextureFormat::RGBA_DXT3: return MTLPixelFormatBC2_RGBA; |
| 130 | + case TextureFormat::RGBA_DXT5: return MTLPixelFormatBC3_RGBA; |
| 131 | + } |
| 132 | + MapFailed("TextureFormat", "MTLPixelFormat"); |
| 133 | +} |
| 134 | + |
| 135 | +MTLTextureType Map(const TextureType textureType) |
| 136 | +{ |
| 137 | + switch (textureType) |
| 138 | + { |
| 139 | + case TextureType::Texture1D: return MTLTextureType1D; |
| 140 | + case TextureType::Texture2D: return MTLTextureType2D; |
| 141 | + case TextureType::Texture3D: return MTLTextureType3D; |
| 142 | + case TextureType::TextureCube: return MTLTextureTypeCube; |
| 143 | + case TextureType::Texture1DArray: return MTLTextureType1DArray; |
| 144 | + case TextureType::Texture2DArray: return MTLTextureType2DArray; |
| 145 | + case TextureType::TextureCubeArray: return MTLTextureTypeCubeArray; |
| 146 | + case TextureType::Texture2DMS: return MTLTextureType2DMultisample; |
| 147 | + case TextureType::Texture2DMSArray: break;//return MTLTextureType2DMultisampleArray; // Beta |
| 148 | + } |
| 149 | + MapFailed("TextureType", "MTLTextureType"); |
| 150 | +} |
| 151 | + |
| 152 | +MTLPrimitiveType Map(const PrimitiveTopology primitiveTopology) |
| 153 | +{ |
| 154 | + switch (primitiveTopology) |
| 155 | + { |
| 156 | + case PrimitiveTopology::PointList: return MTLPrimitiveTypePoint; |
| 157 | + case PrimitiveTopology::LineList: return MTLPrimitiveTypeLine; |
| 158 | + case PrimitiveTopology::LineStrip: return MTLPrimitiveTypeLineStrip; |
| 159 | + case PrimitiveTopology::TriangleList: return MTLPrimitiveTypeTriangle; |
| 160 | + case PrimitiveTopology::TriangleStrip: return MTLPrimitiveTypeTriangleStrip; |
| 161 | + default: break; |
| 162 | + } |
| 163 | + MapFailed("PrimitiveTopology", "MTLPrimitiveType"); |
| 164 | +} |
| 165 | + |
| 166 | +MTLCullMode Map(const CullMode cullMode) |
| 167 | +{ |
| 168 | + switch (cullMode) |
| 169 | + { |
| 170 | + case CullMode::Disabled: return MTLCullModeNone; |
| 171 | + case CullMode::Front: return MTLCullModeFront; |
| 172 | + case CullMode::Back: return MTLCullModeBack; |
| 173 | + } |
| 174 | + MapFailed("CullMode", "MTLCullMode"); |
| 175 | +} |
| 176 | + |
| 177 | + |
| 178 | +} // /namespace MTTypes |
| 179 | + |
| 180 | +} // /namespace LLGL |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | +// ================================================================================ |
0 commit comments