yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
b118451e3
master
1// metal-util.cpp 2#include "metal-util.h" 3 4#include "core/slang-math.h" 5 6#include <stdio.h> 7#include <stdlib.h> 8 9namespace gfx 10{ 11 12MTL ::PixelFormat MetalUtil ::translatePixelFormat (Format format ) 13{ 14switch (format ) 15 { 16case Format ::R32G32B32A32_TYPELESS : 17return MTL ::PixelFormatRGBA32Float ; 18case Format ::R32G32B32_TYPELESS : 19return MTL ::PixelFormatInvalid ; 20case Format ::R32G32_TYPELESS : 21return MTL ::PixelFormatRG32Float ; 22case Format ::R32_TYPELESS : 23return MTL ::PixelFormatR32Float ; 24 25case Format ::R16G16B16A16_TYPELESS : 26return MTL ::PixelFormatRGBA16Float ; 27case Format ::R16G16_TYPELESS : 28return MTL ::PixelFormatRG16Float ; 29case Format ::R16_TYPELESS : 30return MTL ::PixelFormatR16Float ; 31 32case Format ::R8G8B8A8_TYPELESS : 33return MTL ::PixelFormatRGBA8Unorm ; 34case Format ::R8G8_TYPELESS : 35return MTL ::PixelFormatRG8Unorm ; 36case Format ::R8_TYPELESS : 37return MTL ::PixelFormatR8Unorm ; 38case Format ::B8G8R8A8_TYPELESS : 39return MTL ::PixelFormatBGRA8Unorm ; 40 41case Format ::R32G32B32A32_FLOAT : 42return MTL ::PixelFormatRGBA32Float ; 43case Format ::R32G32B32_FLOAT : 44return MTL ::PixelFormatInvalid ; 45case Format ::R32G32_FLOAT : 46return MTL ::PixelFormatRG32Float ; 47case Format ::R32_FLOAT : 48return MTL ::PixelFormatR32Float ; 49 50case Format ::R16G16B16A16_FLOAT : 51return MTL ::PixelFormatRGBA16Float ; 52case Format ::R16G16_FLOAT : 53return MTL ::PixelFormatRG16Float ; 54case Format ::R16_FLOAT : 55return MTL ::PixelFormatR16Float ; 56 57case Format ::R32G32B32A32_UINT : 58return MTL ::PixelFormatRGBA32Uint ; 59case Format ::R32G32B32_UINT : 60return MTL ::PixelFormatInvalid ; 61case Format ::R32G32_UINT : 62return MTL ::PixelFormatRG32Uint ; 63case Format ::R32_UINT : 64return MTL ::PixelFormatR32Uint ; 65 66case Format ::R16G16B16A16_UINT : 67return MTL ::PixelFormatRGBA16Uint ; 68case Format ::R16G16_UINT : 69return MTL ::PixelFormatRG16Uint ; 70case Format ::R16_UINT : 71return MTL ::PixelFormatR16Uint ; 72 73case Format ::R8G8B8A8_UINT : 74return MTL ::PixelFormatRGBA8Uint ; 75case Format ::R8G8_UINT : 76return MTL ::PixelFormatRG8Uint ; 77case Format ::R8_UINT : 78return MTL ::PixelFormatR8Uint ; 79 80case Format ::R32G32B32A32_SINT : 81return MTL ::PixelFormatRGBA32Sint ; 82case Format ::R32G32B32_SINT : 83return MTL ::PixelFormatInvalid ; 84case Format ::R32G32_SINT : 85return MTL ::PixelFormatRG32Sint ; 86case Format ::R32_SINT : 87return MTL ::PixelFormatR32Sint ; 88 89case Format ::R16G16B16A16_SINT : 90return MTL ::PixelFormatRGBA16Sint ; 91case Format ::R16G16_SINT : 92return MTL ::PixelFormatRG16Sint ; 93case Format ::R16_SINT : 94return MTL ::PixelFormatR16Sint ; 95 96case Format ::R8G8B8A8_SINT : 97return MTL ::PixelFormatRGBA8Sint ; 98case Format ::R8G8_SINT : 99return MTL ::PixelFormatRG8Sint ; 100case Format ::R8_SINT : 101return MTL ::PixelFormatR8Sint ; 102 103case Format ::R16G16B16A16_UNORM : 104return MTL ::PixelFormatRGBA16Unorm ; 105case Format ::R16G16_UNORM : 106return MTL ::PixelFormatRG16Unorm ; 107case Format ::R16_UNORM : 108return MTL ::PixelFormatR16Unorm ; 109 110case Format ::R8G8B8A8_UNORM : 111return MTL ::PixelFormatRGBA8Unorm ; 112case Format ::R8G8B8A8_UNORM_SRGB : 113return MTL ::PixelFormatRGBA8Unorm_sRGB ; 114case Format ::R8G8_UNORM : 115return MTL ::PixelFormatRG8Unorm ; 116case Format ::R8_UNORM : 117return MTL ::PixelFormatR8Unorm ; 118case Format ::B8G8R8A8_UNORM : 119return MTL ::PixelFormatBGRA8Unorm ; 120case Format ::B8G8R8A8_UNORM_SRGB : 121return MTL ::PixelFormatBGRA8Unorm_sRGB ; 122case Format ::B8G8R8X8_UNORM : 123return MTL ::PixelFormatInvalid ; 124case Format ::B8G8R8X8_UNORM_SRGB : 125return MTL ::PixelFormatInvalid ; 126 127case Format ::R16G16B16A16_SNORM : 128return MTL ::PixelFormatRGBA16Snorm ; 129case Format ::R16G16_SNORM : 130return MTL ::PixelFormatRG16Snorm ; 131case Format ::R16_SNORM : 132return MTL ::PixelFormatR16Snorm ; 133 134case Format ::R8G8B8A8_SNORM : 135return MTL ::PixelFormatRGBA8Snorm ; 136case Format ::R8G8_SNORM : 137return MTL ::PixelFormatRG8Snorm ; 138case Format ::R8_SNORM : 139return MTL ::PixelFormatR8Snorm ; 140 141case Format ::D32_FLOAT : 142return MTL ::PixelFormatDepth32Float ; 143case Format ::D16_UNORM : 144return MTL ::PixelFormatDepth16Unorm ; 145case Format ::D32_FLOAT_S8_UINT : 146return MTL ::PixelFormatDepth32Float_Stencil8 ; 147case Format ::R32_FLOAT_X32_TYPELESS : 148return MTL ::PixelFormatInvalid ; 149 150case Format ::B4G4R4A4_UNORM : 151return MTL ::PixelFormatABGR4Unorm ; 152case Format ::B5G6R5_UNORM : 153return MTL ::PixelFormatB5G6R5Unorm ; 154case Format ::B5G5R5A1_UNORM : 155return MTL ::PixelFormatA1BGR5Unorm ; 156 157case Format ::R9G9B9E5_SHAREDEXP : 158return MTL ::PixelFormatRGB9E5Float ; 159case Format ::R10G10B10A2_TYPELESS : 160return MTL ::PixelFormatInvalid ; 161case Format ::R10G10B10A2_UINT : 162return MTL ::PixelFormatRGB10A2Uint ; 163case Format ::R10G10B10A2_UNORM : 164return MTL ::PixelFormatRGB10A2Unorm ; 165case Format ::R11G11B10_FLOAT : 166return MTL ::PixelFormatRG11B10Float ; 167 168case Format ::BC1_UNORM : 169return MTL ::PixelFormatBC1_RGBA ; 170case Format ::BC1_UNORM_SRGB : 171return MTL ::PixelFormatBC1_RGBA_sRGB ; 172case Format ::BC2_UNORM : 173return MTL ::PixelFormatBC2_RGBA ; 174case Format ::BC2_UNORM_SRGB : 175return MTL ::PixelFormatBC2_RGBA_sRGB ; 176case Format ::BC3_UNORM : 177return MTL ::PixelFormatBC3_RGBA ; 178case Format ::BC3_UNORM_SRGB : 179return MTL ::PixelFormatBC3_RGBA_sRGB ; 180case Format ::BC4_UNORM : 181return MTL ::PixelFormatBC4_RUnorm ; 182case Format ::BC4_SNORM : 183return MTL ::PixelFormatBC4_RSnorm ; 184case Format ::BC5_UNORM : 185return MTL ::PixelFormatBC5_RGUnorm ; 186case Format ::BC5_SNORM : 187return MTL ::PixelFormatBC5_RGSnorm ; 188case Format ::BC6H_UF16 : 189return MTL ::PixelFormatBC6H_RGBUfloat ; 190case Format ::BC6H_SF16 : 191return MTL ::PixelFormatBC6H_RGBFloat ; 192case Format ::BC7_UNORM : 193return MTL ::PixelFormatBC7_RGBAUnorm ; 194case Format ::BC7_UNORM_SRGB : 195return MTL ::PixelFormatBC7_RGBAUnorm_sRGB ; 196 197default : 198return MTL ::PixelFormatInvalid ; 199 } 200} 201 202MTL ::VertexFormat MetalUtil ::translateVertexFormat (Format format ) 203{ 204switch (format ) 205 { 206case Format ::R8G8_UINT : 207return MTL ::VertexFormatUChar2 ; 208// VertexFormatUChar3 209case Format ::R8G8B8A8_UINT : 210return MTL ::VertexFormatUChar4 ; 211case Format ::R8G8_SINT : 212return MTL ::VertexFormatChar2 ; 213// return VertexFormatChar3 214case Format ::R8G8B8A8_SINT : 215return MTL ::VertexFormatChar4 ; 216case Format ::R8G8_UNORM : 217return MTL ::VertexFormatUChar2Normalized ; 218// return VertexFormatUChar3Normalized; 219case Format ::R8G8B8A8_UNORM : 220return MTL ::VertexFormatUChar4Normalized ; 221case Format ::R8G8_SNORM : 222return MTL ::VertexFormatChar2Normalized ; 223// return VertexFormatChar3Normalized 224case Format ::R8G8B8A8_SNORM : 225return MTL ::VertexFormatChar4Normalized ; 226case Format ::R16G16_UINT : 227return MTL ::VertexFormatUShort2 ; 228// return VertexFormatUShort3; 229case Format ::R16G16B16A16_UINT : 230return MTL ::VertexFormatUShort4 ; 231case Format ::R16G16_SINT : 232return MTL ::VertexFormatShort2 ; 233// return VertexFormatShort3; 234case Format ::R16G16B16A16_SINT : 235return MTL ::VertexFormatShort4 ; 236case Format ::R16G16_UNORM : 237return MTL ::VertexFormatUShort2Normalized ; 238// return VertexFormatUShort3Normalized; 239case Format ::R16G16B16A16_UNORM : 240return MTL ::VertexFormatUShort4Normalized ; 241case Format ::R16G16_SNORM : 242return MTL ::VertexFormatShort2Normalized ; 243// return VertexFormatShort3Normalized; 244case Format ::R16G16B16A16_SNORM : 245return MTL ::VertexFormatShort4Normalized ; 246case Format ::R16G16_FLOAT : 247return MTL ::VertexFormatHalf2 ; 248// return VertexFormatHalf3; 249case Format ::R16G16B16A16_FLOAT : 250return MTL ::VertexFormatHalf4 ; 251case Format ::R32_FLOAT : 252return MTL ::VertexFormatFloat ; 253case Format ::R32G32_FLOAT : 254return MTL ::VertexFormatFloat2 ; 255case Format ::R32G32B32_FLOAT : 256return MTL ::VertexFormatFloat3 ; 257case Format ::R32G32B32A32_FLOAT : 258return MTL ::VertexFormatFloat4 ; 259case Format ::R32_SINT : 260return MTL ::VertexFormatInt ; 261case Format ::R32G32_SINT : 262return MTL ::VertexFormatInt2 ; 263case Format ::R32G32B32_SINT : 264return MTL ::VertexFormatInt3 ; 265case Format ::R32G32B32A32_SINT : 266return MTL ::VertexFormatInt4 ; 267case Format ::R32_UINT : 268return MTL ::VertexFormatUInt ; 269case Format ::R32G32_UINT : 270return MTL ::VertexFormatUInt2 ; 271case Format ::R32G32B32_UINT : 272return MTL ::VertexFormatUInt3 ; 273case Format ::R32G32B32A32_UINT : 274return MTL ::VertexFormatUInt4 ; 275// return VertexFormatInt1010102Normalized; 276case Format ::R10G10B10A2_UNORM : 277return MTL ::VertexFormatUInt1010102Normalized ; 278case Format ::B4G4R4A4_UNORM : 279return MTL ::VertexFormatUChar4Normalized_BGRA ; 280case Format ::R8_UINT : 281return MTL ::VertexFormatUChar ; 282case Format ::R8_SINT : 283return MTL ::VertexFormatChar ; 284case Format ::R8_UNORM : 285return MTL ::VertexFormatUCharNormalized ; 286case Format ::R8_SNORM : 287return MTL ::VertexFormatCharNormalized ; 288case Format ::R16_UINT : 289return MTL ::VertexFormatUShort ; 290case Format ::R16_SINT : 291return MTL ::VertexFormatShort ; 292case Format ::R16_UNORM : 293return MTL ::VertexFormatUShortNormalized ; 294case Format ::R16_SNORM : 295return MTL ::VertexFormatShortNormalized ; 296case Format ::R16_FLOAT : 297return MTL ::VertexFormatHalf ; 298case Format ::R11G11B10_FLOAT : 299return MTL ::VertexFormatFloatRG11B10 ; 300case Format ::R9G9B9E5_SHAREDEXP : 301return MTL ::VertexFormatFloatRGB9E5 ; 302default : 303return MTL ::VertexFormatInvalid ; 304 } 305} 306 307bool MetalUtil ::isDepthFormat (MTL ::PixelFormat format ) 308{ 309switch (format ) 310 { 311case MTL ::PixelFormatDepth16Unorm : 312case MTL ::PixelFormatDepth32Float : 313case MTL ::PixelFormatDepth24Unorm_Stencil8 : 314case MTL ::PixelFormatDepth32Float_Stencil8 : 315return true; 316default : 317return false; 318 } 319} 320 321bool MetalUtil ::isStencilFormat (MTL ::PixelFormat format ) 322{ 323switch (format ) 324 { 325case MTL ::PixelFormatStencil8 : 326case MTL ::PixelFormatDepth24Unorm_Stencil8 : 327case MTL ::PixelFormatDepth32Float_Stencil8 : 328case MTL ::PixelFormatX32_Stencil8 : 329case MTL ::PixelFormatX24_Stencil8 : 330return true; 331default : 332return false; 333 } 334} 335 336MTL ::SamplerMinMagFilter MetalUtil ::translateSamplerMinMagFilter (TextureFilteringMode mode ) 337{ 338switch (mode ) 339 { 340case TextureFilteringMode ::Point : 341return MTL ::SamplerMinMagFilterNearest ; 342case TextureFilteringMode ::Linear : 343return MTL ::SamplerMinMagFilterLinear ; 344default : 345return MTL ::SamplerMinMagFilter (0 ); 346 } 347} 348 349MTL ::SamplerMipFilter MetalUtil ::translateSamplerMipFilter (TextureFilteringMode mode ) 350{ 351switch (mode ) 352 { 353case TextureFilteringMode ::Point : 354return MTL ::SamplerMipFilterNearest ; 355case TextureFilteringMode ::Linear : 356return MTL ::SamplerMipFilterLinear ; 357default : 358return MTL ::SamplerMipFilter (0 ); 359 } 360} 361 362MTL ::SamplerAddressMode MetalUtil ::translateSamplerAddressMode (TextureAddressingMode mode ) 363{ 364switch (mode ) 365 { 366case TextureAddressingMode ::Wrap : 367return MTL ::SamplerAddressModeRepeat ; 368case TextureAddressingMode ::ClampToEdge : 369return MTL ::SamplerAddressModeClampToEdge ; 370case TextureAddressingMode ::ClampToBorder : 371return MTL ::SamplerAddressModeClampToBorderColor ; 372case TextureAddressingMode ::MirrorRepeat : 373return MTL ::SamplerAddressModeMirrorRepeat ; 374case TextureAddressingMode ::MirrorOnce : 375return MTL ::SamplerAddressModeMirrorClampToEdge ; 376default : 377return MTL ::SamplerAddressMode (0 ); 378 } 379} 380 381MTL ::CompareFunction MetalUtil ::translateCompareFunction (ComparisonFunc func ) 382{ 383switch (func ) 384 { 385case ComparisonFunc ::Never : 386return MTL ::CompareFunctionNever ; 387case ComparisonFunc ::Less : 388return MTL ::CompareFunctionLess ; 389case ComparisonFunc ::Equal : 390return MTL ::CompareFunctionEqual ; 391case ComparisonFunc ::LessEqual : 392return MTL ::CompareFunctionLessEqual ; 393case ComparisonFunc ::Greater : 394return MTL ::CompareFunctionGreater ; 395case ComparisonFunc ::NotEqual : 396return MTL ::CompareFunctionNotEqual ; 397case ComparisonFunc ::GreaterEqual : 398return MTL ::CompareFunctionGreaterEqual ; 399case ComparisonFunc ::Always : 400return MTL ::CompareFunctionAlways ; 401default : 402return MTL ::CompareFunction (0 ); 403 } 404} 405 406MTL ::StencilOperation MetalUtil ::translateStencilOperation (StencilOp op ) 407{ 408switch (op ) 409 { 410case StencilOp ::Keep : 411return MTL ::StencilOperationKeep ; 412case StencilOp ::Zero : 413return MTL ::StencilOperationZero ; 414case StencilOp ::Replace : 415return MTL ::StencilOperationReplace ; 416case StencilOp ::IncrementSaturate : 417return MTL ::StencilOperationIncrementClamp ; 418case StencilOp ::DecrementSaturate : 419return MTL ::StencilOperationDecrementClamp ; 420case StencilOp ::Invert : 421return MTL ::StencilOperationInvert ; 422case StencilOp ::IncrementWrap : 423return MTL ::StencilOperationIncrementWrap ; 424case StencilOp ::DecrementWrap : 425return MTL ::StencilOperationDecrementWrap ; 426default : 427return MTL ::StencilOperation (0 ); 428 } 429} 430 431MTL ::VertexStepFunction MetalUtil ::translateVertexStepFunction (InputSlotClass slotClass ) 432{ 433switch (slotClass ) 434 { 435case InputSlotClass ::PerVertex : 436return MTL ::VertexStepFunctionPerVertex ; 437case InputSlotClass ::PerInstance : 438return MTL ::VertexStepFunctionPerInstance ; 439default : 440return MTL ::VertexStepFunctionPerVertex ; 441 } 442} 443 444MTL ::PrimitiveType MetalUtil ::translatePrimitiveType (PrimitiveTopology topology ) 445{ 446switch (topology ) 447 { 448case PrimitiveTopology ::TriangleList : 449return MTL ::PrimitiveTypeTriangle ; 450case PrimitiveTopology ::TriangleStrip : 451return MTL ::PrimitiveTypeTriangleStrip ; 452case PrimitiveTopology ::PointList : 453return MTL ::PrimitiveTypePoint ; 454case PrimitiveTopology ::LineList : 455return MTL ::PrimitiveTypeLine ; 456case PrimitiveTopology ::LineStrip : 457return MTL ::PrimitiveTypeLineStrip ; 458default : 459return MTL ::PrimitiveType (0 ); 460 } 461} 462 463MTL ::PrimitiveTopologyClass MetalUtil ::translatePrimitiveTopologyClass (PrimitiveType type ) 464{ 465switch (type ) 466 { 467case PrimitiveType ::Point : 468return MTL ::PrimitiveTopologyClassPoint ; 469case PrimitiveType ::Line : 470return MTL ::PrimitiveTopologyClassLine ; 471case PrimitiveType ::Triangle : 472return MTL ::PrimitiveTopologyClassTriangle ; 473case PrimitiveType ::Patch : 474default : 475return MTL ::PrimitiveTopologyClassUnspecified ; 476 } 477} 478 479MTL ::BlendFactor MetalUtil ::translateBlendFactor (BlendFactor factor ) 480{ 481switch (factor ) 482 { 483case BlendFactor ::Zero : 484return MTL ::BlendFactorZero ; 485case BlendFactor ::One : 486return MTL ::BlendFactorOne ; 487case BlendFactor ::SrcColor : 488return MTL ::BlendFactorSourceColor ; 489case BlendFactor ::InvSrcColor : 490return MTL ::BlendFactorOneMinusSourceColor ; 491case BlendFactor ::SrcAlpha : 492return MTL ::BlendFactorSourceAlpha ; 493case BlendFactor ::InvSrcAlpha : 494return MTL ::BlendFactorOneMinusSourceAlpha ; 495case BlendFactor ::DestAlpha : 496return MTL ::BlendFactorDestinationAlpha ; 497case BlendFactor ::InvDestAlpha : 498return MTL ::BlendFactorOneMinusDestinationAlpha ; 499case BlendFactor ::DestColor : 500return MTL ::BlendFactorDestinationColor ; 501case BlendFactor ::InvDestColor : 502return MTL ::BlendFactorOneMinusDestinationColor ; 503case BlendFactor ::SrcAlphaSaturate : 504return MTL ::BlendFactorSourceAlphaSaturated ; 505case BlendFactor ::BlendColor : 506return MTL ::BlendFactorBlendColor ; 507case BlendFactor ::InvBlendColor : 508return MTL ::BlendFactorOneMinusBlendColor ; 509case BlendFactor ::SecondarySrcColor : 510return MTL ::BlendFactorSource1Color ; 511case BlendFactor ::InvSecondarySrcColor : 512return MTL ::BlendFactorOneMinusSource1Color ; 513case BlendFactor ::SecondarySrcAlpha : 514return MTL ::BlendFactorSource1Alpha ; 515case BlendFactor ::InvSecondarySrcAlpha : 516return MTL ::BlendFactorOneMinusSource1Alpha ; 517default : 518return MTL ::BlendFactor (0 ); 519 } 520} 521 522MTL ::BlendOperation MetalUtil ::translateBlendOperation (BlendOp op ) 523{ 524switch (op ) 525 { 526case BlendOp ::Add : 527return MTL ::BlendOperationAdd ; 528case BlendOp ::Subtract : 529return MTL ::BlendOperationSubtract ; 530case BlendOp ::ReverseSubtract : 531return MTL ::BlendOperationReverseSubtract ; 532case BlendOp ::Min : 533return MTL ::BlendOperationMin ; 534case BlendOp ::Max : 535return MTL ::BlendOperationMax ; 536default : 537return MTL ::BlendOperation (0 ); 538 } 539} 540 541MTL ::ColorWriteMask MetalUtil ::translateColorWriteMask (RenderTargetWriteMask ::Type mask ) 542{ 543MTL ::ColorWriteMask result = MTL ::ColorWriteMaskNone ; 544if (mask & RenderTargetWriteMask ::EnableRed ) 545result |=MTL ::ColorWriteMaskRed ; 546if (mask & RenderTargetWriteMask ::EnableGreen ) 547result |=MTL ::ColorWriteMaskGreen ; 548if (mask & RenderTargetWriteMask ::EnableBlue ) 549result |=MTL ::ColorWriteMaskBlue ; 550if (mask & RenderTargetWriteMask ::EnableAlpha ) 551result |=MTL ::ColorWriteMaskAlpha ; 552return result ; 553} 554 555MTL ::Winding MetalUtil ::translateWinding (FrontFaceMode mode ) 556{ 557switch (mode ) 558 { 559case FrontFaceMode ::CounterClockwise : 560return MTL ::WindingCounterClockwise ; 561case FrontFaceMode ::Clockwise : 562return MTL ::WindingClockwise ; 563default : 564return MTL ::Winding (0 ); 565 } 566} 567 568MTL ::CullMode MetalUtil ::translateCullMode (CullMode mode ) 569{ 570switch (mode ) 571 { 572case CullMode ::None : 573return MTL ::CullModeNone ; 574case CullMode ::Front : 575return MTL ::CullModeFront ; 576case CullMode ::Back : 577return MTL ::CullModeBack ; 578default : 579return MTL ::CullMode (0 ); 580 } 581} 582 583MTL ::TriangleFillMode MetalUtil ::translateTriangleFillMode (FillMode mode ) 584{ 585switch (mode ) 586 { 587case FillMode ::Solid : 588return MTL ::TriangleFillModeFill ; 589case FillMode ::Wireframe : 590return MTL ::TriangleFillModeLines ; 591default : 592return MTL ::TriangleFillMode (0 ); 593 } 594} 595 596}// namespace gfx