yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie HermaszewskaMove switch statement bodies to their own lines (#5493)b118451e3

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