| 1 |
use metal::{ |
| 2 |
MTLBlendFactor, MTLBlendOperation, MTLBlitOption, MTLClearColor, MTLColorWriteMask, |
| 3 |
MTLCompareFunction, MTLCullMode, MTLOrigin, MTLPrimitiveTopologyClass, MTLPrimitiveType, |
| 4 |
MTLRenderStages, MTLResourceUsage, MTLSamplerAddressMode, MTLSamplerBorderColor, |
| 5 |
MTLSamplerMinMagFilter, MTLSize, MTLStencilOperation, MTLStoreAction, MTLTextureType, |
| 6 |
MTLTextureUsage, MTLVertexFormat, MTLVertexStepFunction, MTLWinding, NSRange, |
| 7 |
}; |
| 8 |
|
| 9 |
pub fn map_texture_usage(format: wgt::TextureFormat, usage: wgt::TextureUses) -> MTLTextureUsage { |
| 10 |
use wgt::TextureUses as Tu; |
| 11 |
|
| 12 |
let mut mtl_usage = MTLTextureUsage::Unknown; |
| 13 |
|
| 14 |
mtl_usage.set( |
| 15 |
MTLTextureUsage::RenderTarget, |
| 16 |
usage.intersects(Tu::COLOR_TARGET | Tu::DEPTH_STENCIL_READ | Tu::DEPTH_STENCIL_WRITE), |
| 17 |
); |
| 18 |
mtl_usage.set( |
| 19 |
MTLTextureUsage::ShaderRead, |
| 20 |
usage.intersects( |
| 21 |
Tu::RESOURCE | Tu::DEPTH_STENCIL_READ | Tu::STORAGE_READ_ONLY | Tu::STORAGE_READ_WRITE, |
| 22 |
), |
| 23 |
); |
| 24 |
mtl_usage.set( |
| 25 |
MTLTextureUsage::ShaderWrite, |
| 26 |
usage.intersects(Tu::STORAGE_WRITE_ONLY | Tu::STORAGE_READ_WRITE), |
| 27 |
); |
| 28 |
// needed for combined depth/stencil formats since we might |
| 29 |
// create a stencil-only view from them |
| 30 |
mtl_usage.set( |
| 31 |
MTLTextureUsage::PixelFormatView, |
| 32 |
format.is_combined_depth_stencil_format(), |
| 33 |
); |
| 34 |
|
| 35 |
mtl_usage.set( |
| 36 |
MTLTextureUsage::ShaderAtomic, |
| 37 |
usage.intersects(Tu::STORAGE_ATOMIC), |
| 38 |
); |
| 39 |
|
| 40 |
mtl_usage |
| 41 |
} |
| 42 |
|
| 43 |
pub fn map_texture_view_dimension(dim: wgt::TextureViewDimension) -> MTLTextureType { |
| 44 |
use wgt::TextureViewDimension as Tvd; |
| 45 |
use MTLTextureType as MTL; |
| 46 |
match dim { |
| 47 |
Tvd::D1 => MTL::D1, |
| 48 |
Tvd::D2 => MTL::D2, |
| 49 |
Tvd::D2Array => MTL::D2Array, |
| 50 |
Tvd::D3 => MTL::D3, |
| 51 |
Tvd::Cube => MTL::Cube, |
| 52 |
Tvd::CubeArray => MTL::CubeArray, |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
pub fn map_compare_function(fun: wgt::CompareFunction) -> MTLCompareFunction { |
| 57 |
use wgt::CompareFunction as Cf; |
| 58 |
use MTLCompareFunction as MTL; |
| 59 |
match fun { |
| 60 |
Cf::Never => MTL::Never, |
| 61 |
Cf::Less => MTL::Less, |
| 62 |
Cf::LessEqual => MTL::LessEqual, |
| 63 |
Cf::Equal => MTL::Equal, |
| 64 |
Cf::GreaterEqual => MTL::GreaterEqual, |
| 65 |
Cf::Greater => MTL::Greater, |
| 66 |
Cf::NotEqual => MTL::NotEqual, |
| 67 |
Cf::Always => MTL::Always, |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
pub fn map_filter_mode(filter: wgt::FilterMode) -> MTLSamplerMinMagFilter { |
| 72 |
use MTLSamplerMinMagFilter as MTL; |
| 73 |
match filter { |
| 74 |
wgt::FilterMode::Nearest => MTL::Nearest, |
| 75 |
wgt::FilterMode::Linear => MTL::Linear, |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
pub fn map_address_mode(address: wgt::AddressMode) -> MTLSamplerAddressMode { |
| 80 |
use wgt::AddressMode as Fm; |
| 81 |
use MTLSamplerAddressMode as MTL; |
| 82 |
match address { |
| 83 |
Fm::Repeat => MTL::Repeat, |
| 84 |
Fm::MirrorRepeat => MTL::MirrorRepeat, |
| 85 |
Fm::ClampToEdge => MTL::ClampToEdge, |
| 86 |
Fm::ClampToBorder => MTL::ClampToBorderColor, |
| 87 |
//Fm::MirrorClamp => MTL::MirrorClampToEdge, |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
pub fn map_border_color(border_color: wgt::SamplerBorderColor) -> MTLSamplerBorderColor { |
| 92 |
use MTLSamplerBorderColor as MTL; |
| 93 |
match border_color { |
| 94 |
wgt::SamplerBorderColor::TransparentBlack => MTL::TransparentBlack, |
| 95 |
wgt::SamplerBorderColor::OpaqueBlack => MTL::OpaqueBlack, |
| 96 |
wgt::SamplerBorderColor::OpaqueWhite => MTL::OpaqueWhite, |
| 97 |
wgt::SamplerBorderColor::Zero => unreachable!(), |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
pub fn map_primitive_topology( |
| 102 |
topology: wgt::PrimitiveTopology, |
| 103 |
) -> (MTLPrimitiveTopologyClass, MTLPrimitiveType) { |
| 104 |
use wgt::PrimitiveTopology as Pt; |
| 105 |
match topology { |
| 106 |
Pt::PointList => (MTLPrimitiveTopologyClass::Point, MTLPrimitiveType::Point), |
| 107 |
Pt::LineList => (MTLPrimitiveTopologyClass::Line, MTLPrimitiveType::Line), |
| 108 |
Pt::LineStrip => (MTLPrimitiveTopologyClass::Line, MTLPrimitiveType::LineStrip), |
| 109 |
Pt::TriangleList => ( |
| 110 |
MTLPrimitiveTopologyClass::Triangle, |
| 111 |
MTLPrimitiveType::Triangle, |
| 112 |
), |
| 113 |
Pt::TriangleStrip => ( |
| 114 |
MTLPrimitiveTopologyClass::Triangle, |
| 115 |
MTLPrimitiveType::TriangleStrip, |
| 116 |
), |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
pub fn map_color_write(mask: wgt::ColorWrites) -> MTLColorWriteMask { |
| 121 |
let mut raw_mask = MTLColorWriteMask::empty(); |
| 122 |
|
| 123 |
if mask.contains(wgt::ColorWrites::RED) { |
| 124 |
raw_mask |= MTLColorWriteMask::Red; |
| 125 |
} |
| 126 |
if mask.contains(wgt::ColorWrites::GREEN) { |
| 127 |
raw_mask |= MTLColorWriteMask::Green; |
| 128 |
} |
| 129 |
if mask.contains(wgt::ColorWrites::BLUE) { |
| 130 |
raw_mask |= MTLColorWriteMask::Blue; |
| 131 |
} |
| 132 |
if mask.contains(wgt::ColorWrites::ALPHA) { |
| 133 |
raw_mask |= MTLColorWriteMask::Alpha; |
| 134 |
} |
| 135 |
|
| 136 |
raw_mask |
| 137 |
} |
| 138 |
|
| 139 |
pub fn map_blend_factor(factor: wgt::BlendFactor) -> MTLBlendFactor { |
| 140 |
use wgt::BlendFactor as Bf; |
| 141 |
use MTLBlendFactor as MTL; |
| 142 |
|
| 143 |
match factor { |
| 144 |
Bf::Zero => MTL::Zero, |
| 145 |
Bf::One => MTL::One, |
| 146 |
Bf::Src => MTL::SourceColor, |
| 147 |
Bf::OneMinusSrc => MTL::OneMinusSourceColor, |
| 148 |
Bf::Dst => MTL::DestinationColor, |
| 149 |
Bf::OneMinusDst => MTL::OneMinusDestinationColor, |
| 150 |
Bf::SrcAlpha => MTL::SourceAlpha, |
| 151 |
Bf::OneMinusSrcAlpha => MTL::OneMinusSourceAlpha, |
| 152 |
Bf::DstAlpha => MTL::DestinationAlpha, |
| 153 |
Bf::OneMinusDstAlpha => MTL::OneMinusDestinationAlpha, |
| 154 |
Bf::Constant => MTL::BlendColor, |
| 155 |
Bf::OneMinusConstant => MTL::OneMinusBlendColor, |
| 156 |
Bf::SrcAlphaSaturated => MTL::SourceAlphaSaturated, |
| 157 |
Bf::Src1 => MTL::Source1Color, |
| 158 |
Bf::OneMinusSrc1 => MTL::OneMinusSource1Color, |
| 159 |
Bf::Src1Alpha => MTL::Source1Alpha, |
| 160 |
Bf::OneMinusSrc1Alpha => MTL::OneMinusSource1Alpha, |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
pub fn map_blend_op(operation: wgt::BlendOperation) -> MTLBlendOperation { |
| 165 |
use wgt::BlendOperation as Bo; |
| 166 |
use MTLBlendOperation as MTL; |
| 167 |
|
| 168 |
match operation { |
| 169 |
Bo::Add => MTL::Add, |
| 170 |
Bo::Subtract => MTL::Subtract, |
| 171 |
Bo::ReverseSubtract => MTL::ReverseSubtract, |
| 172 |
Bo::Min => MTL::Min, |
| 173 |
Bo::Max => MTL::Max, |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
pub fn map_blend_component( |
| 178 |
component: &wgt::BlendComponent, |
| 179 |
) -> (MTLBlendOperation, MTLBlendFactor, MTLBlendFactor) { |
| 180 |
( |
| 181 |
map_blend_op(component.operation), |
| 182 |
map_blend_factor(component.src_factor), |
| 183 |
map_blend_factor(component.dst_factor), |
| 184 |
) |
| 185 |
} |
| 186 |
|
| 187 |
pub fn map_vertex_format(format: wgt::VertexFormat) -> MTLVertexFormat { |
| 188 |
use wgt::VertexFormat as Vf; |
| 189 |
use MTLVertexFormat as MTL; |
| 190 |
|
| 191 |
match format { |
| 192 |
Vf::Unorm8 => MTL::UCharNormalized, |
| 193 |
Vf::Snorm8 => MTL::CharNormalized, |
| 194 |
Vf::Uint8 => MTL::UChar, |
| 195 |
Vf::Sint8 => MTL::Char, |
| 196 |
Vf::Unorm8x2 => MTL::UChar2Normalized, |
| 197 |
Vf::Snorm8x2 => MTL::Char2Normalized, |
| 198 |
Vf::Uint8x2 => MTL::UChar2, |
| 199 |
Vf::Sint8x2 => MTL::Char2, |
| 200 |
Vf::Unorm8x4 => MTL::UChar4Normalized, |
| 201 |
Vf::Snorm8x4 => MTL::Char4Normalized, |
| 202 |
Vf::Uint8x4 => MTL::UChar4, |
| 203 |
Vf::Sint8x4 => MTL::Char4, |
| 204 |
Vf::Unorm16 => MTL::UShortNormalized, |
| 205 |
Vf::Snorm16 => MTL::ShortNormalized, |
| 206 |
Vf::Uint16 => MTL::UShort, |
| 207 |
Vf::Sint16 => MTL::Short, |
| 208 |
Vf::Float16 => MTL::Half, |
| 209 |
Vf::Unorm16x2 => MTL::UShort2Normalized, |
| 210 |
Vf::Snorm16x2 => MTL::Short2Normalized, |
| 211 |
Vf::Uint16x2 => MTL::UShort2, |
| 212 |
Vf::Sint16x2 => MTL::Short2, |
| 213 |
Vf::Float16x2 => MTL::Half2, |
| 214 |
Vf::Unorm16x4 => MTL::UShort4Normalized, |
| 215 |
Vf::Snorm16x4 => MTL::Short4Normalized, |
| 216 |
Vf::Uint16x4 => MTL::UShort4, |
| 217 |
Vf::Sint16x4 => MTL::Short4, |
| 218 |
Vf::Float16x4 => MTL::Half4, |
| 219 |
Vf::Uint32 => MTL::UInt, |
| 220 |
Vf::Sint32 => MTL::Int, |
| 221 |
Vf::Float32 => MTL::Float, |
| 222 |
Vf::Uint32x2 => MTL::UInt2, |
| 223 |
Vf::Sint32x2 => MTL::Int2, |
| 224 |
Vf::Float32x2 => MTL::Float2, |
| 225 |
Vf::Uint32x3 => MTL::UInt3, |
| 226 |
Vf::Sint32x3 => MTL::Int3, |
| 227 |
Vf::Float32x3 => MTL::Float3, |
| 228 |
Vf::Uint32x4 => MTL::UInt4, |
| 229 |
Vf::Sint32x4 => MTL::Int4, |
| 230 |
Vf::Float32x4 => MTL::Float4, |
| 231 |
Vf::Unorm10_10_10_2 => MTL::UInt1010102Normalized, |
| 232 |
Vf::Unorm8x4Bgra => MTL::UChar4Normalized_BGRA, |
| 233 |
Vf::Float64 | Vf::Float64x2 | Vf::Float64x3 | Vf::Float64x4 => unimplemented!(), |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
pub fn map_step_mode(mode: wgt::VertexStepMode) -> MTLVertexStepFunction { |
| 238 |
match mode { |
| 239 |
wgt::VertexStepMode::Vertex => MTLVertexStepFunction::PerVertex, |
| 240 |
wgt::VertexStepMode::Instance => MTLVertexStepFunction::PerInstance, |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
pub fn map_stencil_op(op: wgt::StencilOperation) -> MTLStencilOperation { |
| 245 |
use wgt::StencilOperation as So; |
| 246 |
use MTLStencilOperation as MTL; |
| 247 |
|
| 248 |
match op { |
| 249 |
So::Keep => MTL::Keep, |
| 250 |
So::Zero => MTL::Zero, |
| 251 |
So::Replace => MTL::Replace, |
| 252 |
So::IncrementClamp => MTL::IncrementClamp, |
| 253 |
So::IncrementWrap => MTL::IncrementWrap, |
| 254 |
So::DecrementClamp => MTL::DecrementClamp, |
| 255 |
So::DecrementWrap => MTL::DecrementWrap, |
| 256 |
So::Invert => MTL::Invert, |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
pub fn map_winding(winding: wgt::FrontFace) -> MTLWinding { |
| 261 |
match winding { |
| 262 |
wgt::FrontFace::Cw => MTLWinding::Clockwise, |
| 263 |
wgt::FrontFace::Ccw => MTLWinding::CounterClockwise, |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
pub fn map_cull_mode(face: Option<wgt::Face>) -> MTLCullMode { |
| 268 |
match face { |
| 269 |
None => MTLCullMode::None, |
| 270 |
Some(wgt::Face::Front) => MTLCullMode::Front, |
| 271 |
Some(wgt::Face::Back) => MTLCullMode::Back, |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
pub fn map_range(range: &crate::MemoryRange) -> NSRange { |
| 276 |
NSRange { |
| 277 |
location: range.start, |
| 278 |
length: range.end - range.start, |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
pub fn map_copy_extent(extent: &crate::CopyExtent) -> MTLSize { |
| 283 |
MTLSize { |
| 284 |
width: extent.width as u64, |
| 285 |
height: extent.height as u64, |
| 286 |
depth: extent.depth as u64, |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
pub fn map_origin(origin: &wgt::Origin3d) -> MTLOrigin { |
| 291 |
MTLOrigin { |
| 292 |
x: origin.x as u64, |
| 293 |
y: origin.y as u64, |
| 294 |
z: origin.z as u64, |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
pub fn map_store_action(store: bool, resolve: bool) -> MTLStoreAction { |
| 299 |
use MTLStoreAction as MTL; |
| 300 |
match (store, resolve) { |
| 301 |
(true, true) => MTL::StoreAndMultisampleResolve, |
| 302 |
(false, true) => MTL::MultisampleResolve, |
| 303 |
(true, false) => MTL::Store, |
| 304 |
(false, false) => MTL::DontCare, |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
pub fn map_clear_color(color: &wgt::Color) -> MTLClearColor { |
| 309 |
MTLClearColor { |
| 310 |
red: color.r, |
| 311 |
green: color.g, |
| 312 |
blue: color.b, |
| 313 |
alpha: color.a, |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
pub fn get_blit_option(format: wgt::TextureFormat, aspect: crate::FormatAspects) -> MTLBlitOption { |
| 318 |
if format.is_combined_depth_stencil_format() { |
| 319 |
match aspect { |
| 320 |
crate::FormatAspects::DEPTH => MTLBlitOption::DepthFromDepthStencil, |
| 321 |
crate::FormatAspects::STENCIL => MTLBlitOption::StencilFromDepthStencil, |
| 322 |
_ => unreachable!(), |
| 323 |
} |
| 324 |
} else { |
| 325 |
MTLBlitOption::None |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
pub fn map_render_stages(stage: wgt::ShaderStages) -> MTLRenderStages { |
| 330 |
let mut raw_stages = MTLRenderStages::empty(); |
| 331 |
|
| 332 |
if stage.contains(wgt::ShaderStages::VERTEX) { |
| 333 |
raw_stages |= MTLRenderStages::Vertex; |
| 334 |
} |
| 335 |
if stage.contains(wgt::ShaderStages::FRAGMENT) { |
| 336 |
raw_stages |= MTLRenderStages::Fragment; |
| 337 |
} |
| 338 |
|
| 339 |
raw_stages |
| 340 |
} |
| 341 |
|
| 342 |
pub fn map_resource_usage(ty: &wgt::BindingType) -> MTLResourceUsage { |
| 343 |
match ty { |
| 344 |
wgt::BindingType::Texture { .. } => MTLResourceUsage::Sample, |
| 345 |
wgt::BindingType::StorageTexture { access, .. } => match access { |
| 346 |
wgt::StorageTextureAccess::WriteOnly => MTLResourceUsage::Write, |
| 347 |
wgt::StorageTextureAccess::ReadOnly => MTLResourceUsage::Read, |
| 348 |
wgt::StorageTextureAccess::Atomic | wgt::StorageTextureAccess::ReadWrite => { |
| 349 |
MTLResourceUsage::Read | MTLResourceUsage::Write |
| 350 |
} |
| 351 |
}, |
| 352 |
wgt::BindingType::Sampler(..) => MTLResourceUsage::empty(), |
| 353 |
_ => unreachable!(), |
| 354 |
} |
| 355 |
} |