|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <MetalKit/MetalKit.h> |
| 4 | + |
| 5 | +#include <cassert> |
| 6 | + |
| 7 | +#include "../base.h" |
| 8 | + |
| 9 | +namespace Pathfinder { |
| 10 | + |
| 11 | +inline MTLPixelFormat to_mtl_pixel_format(const TextureFormat tf) { |
| 12 | + switch (tf) { |
| 13 | + case TextureFormat::R8: { |
| 14 | + return MTLPixelFormatR8Unorm; |
| 15 | + } |
| 16 | + case TextureFormat::Rg8: { |
| 17 | + return MTLPixelFormatRG8Unorm; |
| 18 | + } |
| 19 | + case TextureFormat::Rgba8Unorm: { |
| 20 | + return MTLPixelFormatRGBA8Unorm; |
| 21 | + } |
| 22 | + case TextureFormat::Bgra8Unorm: { |
| 23 | + return MTLPixelFormatBGRA8Unorm; |
| 24 | + } |
| 25 | + case TextureFormat::Rgba8Srgb: { |
| 26 | + return MTLPixelFormatRGBA8Unorm_sRGB; |
| 27 | + } |
| 28 | + case TextureFormat::Bgra8Srgb: { |
| 29 | + return MTLPixelFormatBGRA8Unorm_sRGB; |
| 30 | + } |
| 31 | + case TextureFormat::Rgba16Float: { |
| 32 | + return MTLPixelFormatRGBA16Float; |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +inline TextureFormat from_mtl_pixel_format(const MTLPixelFormat mtl_pf) { |
| 38 | + TextureFormat fm = TextureFormat::R8; |
| 39 | + switch (mtl_pf) { |
| 40 | + case MTLPixelFormatR8Unorm: |
| 41 | + fm = TextureFormat::R8; |
| 42 | + break; |
| 43 | + case MTLPixelFormatRG8Unorm: |
| 44 | + fm = TextureFormat::Rg8; |
| 45 | + break; |
| 46 | + case MTLPixelFormatRGBA8Unorm: |
| 47 | + fm = TextureFormat::Rgba8Unorm; |
| 48 | + break; |
| 49 | + case MTLPixelFormatRGBA8Unorm_sRGB: |
| 50 | + fm = TextureFormat::Rgba8Srgb; |
| 51 | + break; |
| 52 | + case MTLPixelFormatBGRA8Unorm: |
| 53 | + fm = TextureFormat::Bgra8Unorm; |
| 54 | + break; |
| 55 | + case MTLPixelFormatBGRA8Unorm_sRGB: |
| 56 | + fm = TextureFormat::Bgra8Srgb; |
| 57 | + break; |
| 58 | + case MTLPixelFormatRGBA16Float: |
| 59 | + fm = TextureFormat::Rgba16Float; |
| 60 | + break; |
| 61 | + default: |
| 62 | + abort(); |
| 63 | + } |
| 64 | + return fm; |
| 65 | +} |
| 66 | + |
| 67 | +inline MTLBlendFactor to_mtl_blend_factor(const BlendFactor factor) { |
| 68 | + switch (factor) { |
| 69 | + case BlendFactor::One: |
| 70 | + return MTLBlendFactorOne; |
| 71 | + case BlendFactor::OneMinusSrcAlpha: |
| 72 | + return MTLBlendFactorOneMinusSourceAlpha; |
| 73 | + default: |
| 74 | + abort(); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +inline MTLBlendOperation to_mtl_blend_op(const BlendOperation op) { |
| 79 | + switch (op) { |
| 80 | + case BlendOperation::Add: |
| 81 | + return MTLBlendOperationAdd; |
| 82 | + default: |
| 83 | + abort(); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +static const MTLVertexFormat MTL_FORMATS[] = { |
| 88 | + MTLVertexFormatChar, MTLVertexFormatChar2, MTLVertexFormatChar3, MTLVertexFormatChar4, // |
| 89 | + MTLVertexFormatUChar, MTLVertexFormatUChar2, MTLVertexFormatUChar3, MTLVertexFormatUChar4, // |
| 90 | + MTLVertexFormatShort, MTLVertexFormatShort2, MTLVertexFormatShort3, MTLVertexFormatShort4, // |
| 91 | + MTLVertexFormatUShort, MTLVertexFormatUShort2, MTLVertexFormatUShort3, MTLVertexFormatUShort4, // |
| 92 | + MTLVertexFormatInt, MTLVertexFormatInt2, MTLVertexFormatInt3, MTLVertexFormatInt4, // |
| 93 | + MTLVertexFormatUInt, MTLVertexFormatUInt2, MTLVertexFormatUInt3, MTLVertexFormatUInt4, // |
| 94 | + MTLVertexFormatFloat, MTLVertexFormatFloat2, MTLVertexFormatFloat3, MTLVertexFormatFloat4, // |
| 95 | + MTLVertexFormatHalf, MTLVertexFormatHalf2, MTLVertexFormatHalf3, MTLVertexFormatHalf4, // |
| 96 | +}; |
| 97 | + |
| 98 | +inline MTLVertexFormat to_mtl_vertex_format(DataType type, uint32_t count) { |
| 99 | + switch (type) { |
| 100 | + case DataType::i8: |
| 101 | + return MTL_FORMATS[count - 1]; |
| 102 | + case DataType::u8: |
| 103 | + return MTL_FORMATS[4 + count - 1]; |
| 104 | + case DataType::i16: |
| 105 | + return MTL_FORMATS[8 + count - 1]; |
| 106 | + case DataType::u16: |
| 107 | + return MTL_FORMATS[12 + count - 1]; |
| 108 | + case DataType::i32: |
| 109 | + return MTL_FORMATS[16 + count - 1]; |
| 110 | + case DataType::u32: |
| 111 | + return MTL_FORMATS[20 + count - 1]; |
| 112 | + case DataType::f32: |
| 113 | + return MTL_FORMATS[24 + count - 1]; |
| 114 | + case DataType::f16: |
| 115 | + return MTL_FORMATS[28 + count - 1]; |
| 116 | + default: |
| 117 | + return MTLVertexFormatInvalid; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +inline MTLResourceOptions to_mtl_resource_option(const MemoryProperty memory_property) { |
| 122 | + switch (memory_property) { |
| 123 | + case MemoryProperty::HostVisibleAndCoherent: |
| 124 | + return MTLResourceStorageModeShared; |
| 125 | + case MemoryProperty::DeviceLocal: |
| 126 | + return MTLResourceStorageModePrivate; |
| 127 | + default: |
| 128 | + abort(); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/// For textures. |
| 133 | +inline MTLStorageMode to_mtl_storage_mode(const MemoryProperty memory_property) { |
| 134 | + switch (memory_property) { |
| 135 | + case MemoryProperty::HostVisibleAndCoherent: |
| 136 | + return MTLStorageModeShared; |
| 137 | + case MemoryProperty::DeviceLocal: |
| 138 | + return MTLStorageModePrivate; |
| 139 | + default: |
| 140 | + abort(); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +inline MTLSamplerMinMagFilter to_mtl_sampler_filter(const SamplerFilter filter) { |
| 145 | + return filter == SamplerFilter::Nearest ? MTLSamplerMinMagFilterNearest : MTLSamplerMinMagFilterLinear; |
| 146 | +} |
| 147 | + |
| 148 | +inline MTLSamplerAddressMode to_mtl_sampler_address_mode(const SamplerAddressMode address_mode) { |
| 149 | + switch (address_mode) { |
| 150 | + case SamplerAddressMode::Repeat: |
| 151 | + return MTLSamplerAddressModeRepeat; |
| 152 | + case SamplerAddressMode::MirroredRepeat: |
| 153 | + return MTLSamplerAddressModeMirrorRepeat; |
| 154 | + case SamplerAddressMode::ClampToEdge: |
| 155 | + return MTLSamplerAddressModeClampToEdge; |
| 156 | + case SamplerAddressMode::ClampToBorder: |
| 157 | + return MTLSamplerAddressModeClampToBorderColor; |
| 158 | + default: |
| 159 | + abort(); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +inline MTLRenderStages to_mtl_render_stage(const ShaderStage stage) { |
| 164 | + switch (stage) { |
| 165 | + case ShaderStage::Vertex: |
| 166 | + return MTLRenderStageVertex; |
| 167 | + case ShaderStage::Fragment: |
| 168 | + return MTLRenderStageFragment; |
| 169 | + default: |
| 170 | + assert(false); |
| 171 | + abort(); |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +} // namespace Pathfinder |
0 commit comments