Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mtl_shader.hh « metal « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 64d9d1cf849eb1b684ac283c282e0d476eb0d1c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup gpu
 */

#pragma once

#include "MEM_guardedalloc.h"

#include "GPU_batch.h"
#include "GPU_capabilities.h"
#include "GPU_shader.h"
#include "GPU_vertex_format.h"

#include <Metal/Metal.h>
#include <QuartzCore/QuartzCore.h>
#include <functional>
#include <unordered_map>

#include <mutex>
#include <thread>

#include "mtl_framebuffer.hh"
#include "mtl_shader_interface.hh"
#include "mtl_shader_shared.h"
#include "mtl_state.hh"
#include "mtl_texture.hh"

#include "gpu_shader_create_info.hh"
#include "gpu_shader_private.hh"

namespace blender::gpu {

class MTLShaderInterface;
class MTLContext;

/* Debug control. */
#define MTL_SHADER_DEBUG_EXPORT_SOURCE 1
#define MTL_SHADER_TRANSLATION_DEBUG_OUTPUT 0

/* Separate print used only during development and debugging. */
#if MTL_SHADER_TRANSLATION_DEBUG_OUTPUT
#  define shader_debug_printf printf
#else
#  define shader_debug_printf(...) /* Null print. */
#endif

/* Desired reflection data for a buffer binding. */
struct MTLBufferArgumentData {
  uint32_t index;
  uint32_t size;
  uint32_t alignment;
  bool active;
};

/* Metal Render Pipeline State Instance. */
struct MTLRenderPipelineStateInstance {
  /* Function instances with specialization.
   * Required for argument encoder construction. */
  id<MTLFunction> vert;
  id<MTLFunction> frag;

  /* PSO handle. */
  id<MTLRenderPipelineState> pso;

  /** Derived information. */
  /* Unique index for PSO variant. */
  uint32_t shader_pso_index;
  /* Base bind index for binding uniform buffers, offset based on other
   * bound buffers such as vertex buffers, as the count can vary. */
  int base_uniform_buffer_index;
  /* buffer bind slot used for null attributes (-1 if not needed). */
  int null_attribute_buffer_index;
  /* buffer bind used for transform feedback output buffer. */
  int transform_feedback_buffer_index;

  /** Reflection Data.
   * Currently used to verify whether uniform buffers of incorrect sizes being bound, due to left
   * over bindings being used for slots that did not need updating for a particular draw. Metal
   * Back-end over-generates bindings due to detecting their presence, though in many cases, the
   * bindings in the source are not all used for a given shader.
   * This information can also be used to eliminate redundant/unused bindings. */
  bool reflection_data_available;
  blender::Vector<MTLBufferArgumentData> buffer_bindings_reflection_data_vert;
  blender::Vector<MTLBufferArgumentData> buffer_bindings_reflection_data_frag;
};

/* #MTLShaderBuilder source wrapper used during initial compilation. */
struct MTLShaderBuilder {
  NSString *msl_source_vert_ = @"";
  NSString *msl_source_frag_ = @"";

  /* Generated GLSL source used during compilation. */
  std::string glsl_vertex_source_ = "";
  std::string glsl_fragment_source_ = "";

  /* Indicates whether source code has been provided via MSL directly. */
  bool source_from_msl_ = false;
};

/**
 * #MTLShader implements shader compilation, Pipeline State Object (PSO)
 * creation for rendering and uniform data binding.
 * Shaders can either be created from native MSL, or generated
 * from a GLSL source shader using #GPUShaderCreateInfo.
 *
 * Shader creation process:
 * - Create #MTLShader:
 *    - Convert GLSL to MSL source if required.
 * - set MSL source.
 * - set Vertex/Fragment function names.
 * - Create and populate #MTLShaderInterface.
 **/
class MTLShader : public Shader {
  friend shader::ShaderCreateInfo;
  friend shader::StageInterfaceInfo;

 public:
  /* Cached SSBO vertex fetch attribute uniform locations. */
  int uni_ssbo_input_prim_type_loc = -1;
  int uni_ssbo_input_vert_count_loc = -1;
  int uni_ssbo_uses_indexed_rendering = -1;
  int uni_ssbo_uses_index_mode_u16 = -1;

 private:
  /* Context Handle. */
  MTLContext *context_ = nullptr;

  /** Transform Feedback. */
  /* Transform feedback mode. */
  eGPUShaderTFBType transform_feedback_type_ = GPU_SHADER_TFB_NONE;
  /* Transform feedback outputs written to TFB buffer. */
  blender::Vector<std::string> tf_output_name_list_;
  /* Whether transform feedback is currently active. */
  bool transform_feedback_active_ = false;
  /* Vertex buffer to write transform feedback data into. */
  GPUVertBuf *transform_feedback_vertbuf_ = nullptr;

  /** Shader source code. */
  MTLShaderBuilder *shd_builder_ = nullptr;
  NSString *vertex_function_name_ = @"";
  NSString *fragment_function_name_ = @"";

  /** Compiled shader resources. */
  id<MTLLibrary> shader_library_vert_ = nil;
  id<MTLLibrary> shader_library_frag_ = nil;
  bool valid_ = false;

  /** Render pipeline state and PSO caching. */
  /* Metal API Descriptor used for creation of unique PSOs based on rendering state. */
  MTLRenderPipelineDescriptor *pso_descriptor_ = nil;
  /* Metal backend struct containing all high-level pipeline state parameters
   * which contribute to instantiation of a unique PSO. */
  MTLRenderPipelineStateDescriptor current_pipeline_state_;
  /* Cache of compiled PipelineStateObjects. */
  blender::Map<MTLRenderPipelineStateDescriptor, MTLRenderPipelineStateInstance *> pso_cache_;

  /* True to enable multi-layered rendering support. */
  bool uses_mtl_array_index_ = false;

  /** SSBO Vertex fetch pragma options. */
  /* Indicates whether to pass in VertexBuffer's as regular buffer bindings
   * and perform vertex assembly manually, rather than using Stage-in.
   * This is used to give a vertex shader full access to all of the
   * vertex data.
   * This is primarily used for optimization techniques and
   * alternative solutions for Geometry-shaders which are unsupported
   * by Metal. */
  bool use_ssbo_vertex_fetch_mode_ = false;
  /* Output primitive type when rendering sing ssbo_vertex_fetch. */
  MTLPrimitiveType ssbo_vertex_fetch_output_prim_type_;

  /* Output vertices per original vertex shader instance.
   * This number will be multiplied by the number of input primitives
   * from the source draw call. */
  uint32_t ssbo_vertex_fetch_output_num_verts_ = 0;

  bool ssbo_vertex_attribute_bind_active_ = false;
  int ssbo_vertex_attribute_bind_mask_ = 0;
  bool ssbo_vbo_slot_used_[MTL_SSBO_VERTEX_FETCH_MAX_VBOS];

  struct ShaderSSBOAttributeBinding {
    int attribute_index = -1;
    int uniform_stride;
    int uniform_offset;
    int uniform_fetchmode;
    int uniform_vbo_id;
    int uniform_attr_type;
  };
  ShaderSSBOAttributeBinding cached_ssbo_attribute_bindings_[MTL_MAX_VERTEX_INPUT_ATTRIBUTES] = {};

  /* Metal Shader Uniform data store.
   * This blocks is used to store current shader push_constant
   * data before it is submitted to the GPU. This is currently
   * stored per shader instance, though depending on GPU module
   * functionality, this could potentially be a global data store.
   * This data is associated with the PushConstantBlock, which is
   * always at index zero in the UBO list. */
  void *push_constant_data_ = nullptr;
  bool push_constant_modified_ = false;

 public:
  MTLShader(MTLContext *ctx, const char *name);
  MTLShader(MTLContext *ctx,
            MTLShaderInterface *interface,
            const char *name,
            NSString *input_vertex_source,
            NSString *input_fragment_source,
            NSString *vertex_function_name_,
            NSString *fragment_function_name_);
  ~MTLShader();

  /* Assign GLSL source. */
  void vertex_shader_from_glsl(MutableSpan<const char *> sources) override;
  void geometry_shader_from_glsl(MutableSpan<const char *> sources) override;
  void fragment_shader_from_glsl(MutableSpan<const char *> sources) override;
  void compute_shader_from_glsl(MutableSpan<const char *> sources) override;

  /* Compile and build - Return true if successful. */
  bool finalize(const shader::ShaderCreateInfo *info = nullptr) override;

  /* Utility. */
  bool is_valid()
  {
    return valid_;
  }
  MTLRenderPipelineStateDescriptor &get_current_pipeline_state()
  {
    return current_pipeline_state_;
  }
  MTLShaderInterface *get_interface()
  {
    return static_cast<MTLShaderInterface *>(this->interface);
  }
  void *get_push_constant_data()
  {
    return push_constant_data_;
  }

  /* Shader source generators from create-info.
   * These aren't all used by Metal, as certain parts of source code generation
   * for shader entry-points and resource mapping occur during `finalize`. */
  std::string resources_declare(const shader::ShaderCreateInfo &info) const override;
  std::string vertex_interface_declare(const shader::ShaderCreateInfo &info) const override;
  std::string fragment_interface_declare(const shader::ShaderCreateInfo &info) const override;
  std::string geometry_interface_declare(const shader::ShaderCreateInfo &info) const override;
  std::string geometry_layout_declare(const shader::ShaderCreateInfo &info) const override;
  std::string compute_layout_declare(const shader::ShaderCreateInfo &info) const override;

  void transform_feedback_names_set(Span<const char *> name_list,
                                    const eGPUShaderTFBType geom_type) override;
  bool transform_feedback_enable(GPUVertBuf *buf) override;
  void transform_feedback_disable() override;

  void bind() override;
  void unbind() override;

  void uniform_float(int location, int comp_len, int array_size, const float *data) override;
  void uniform_int(int location, int comp_len, int array_size, const int *data) override;
  bool get_push_constant_is_dirty();
  void push_constant_bindstate_mark_dirty(bool is_dirty);

  void vertformat_from_shader(GPUVertFormat *format) const override;

  /* DEPRECATED: Kept only because of BGL API. (Returning -1 in METAL). */
  int program_handle_get() const override
  {
    return -1;
  }

  bool get_uses_ssbo_vertex_fetch()
  {
    return use_ssbo_vertex_fetch_mode_;
  }
  MTLPrimitiveType get_ssbo_vertex_fetch_output_prim_type()
  {
    return ssbo_vertex_fetch_output_prim_type_;
  }
  uint32_t get_ssbo_vertex_fetch_output_num_verts()
  {
    return ssbo_vertex_fetch_output_num_verts_;
  }
  static int ssbo_vertex_type_to_attr_type(MTLVertexFormat attribute_type);
  void prepare_ssbo_vertex_fetch_metadata();

  /* SSBO Vertex Bindings Utility functions. */
  void ssbo_vertex_fetch_bind_attributes_begin();
  void ssbo_vertex_fetch_bind_attribute(const MTLSSBOAttribute &ssbo_attr);
  void ssbo_vertex_fetch_bind_attributes_end(id<MTLRenderCommandEncoder> active_encoder);

  /* Metal shader properties and source mapping. */
  void set_vertex_function_name(NSString *vetex_function_name);
  void set_fragment_function_name(NSString *fragment_function_name_);
  void shader_source_from_msl(NSString *input_vertex_source, NSString *input_fragment_source);
  void set_interface(MTLShaderInterface *interface);
  MTLRenderPipelineStateInstance *bake_current_pipeline_state(MTLContext *ctx,
                                                              MTLPrimitiveTopologyClass prim_type);

  /* Transform Feedback. */
  GPUVertBuf *get_transform_feedback_active_buffer();
  bool has_transform_feedback_varying(std::string str);

 private:
  /* Generate MSL shader from GLSL source. */
  bool generate_msl_from_glsl(const shader::ShaderCreateInfo *info);

  MEM_CXX_CLASS_ALLOC_FUNCS("MTLShader");
};

/* Vertex format conversion.
 * Determines whether it is possible to resize a vertex attribute type
 * during input assembly. A conversion is implied by the  difference
 * between the input vertex descriptor (from MTLBatch/MTLImmediate)
 * and the type specified in the shader source.
 *
 * e.g. vec3 to vec4 expansion, or vec4 to vec2 truncation.
 * NOTE: Vector expansion will replace empty elements with the values
 * (0,0,0,1).
 *
 * If implicit format resize is not possible, this function
 * returns false.
 *
 * Implicitly supported conversions in Metal are described here:
 * https://developer.apple.com/documentation/metal/mtlvertexattributedescriptor/1516081-format?language=objc
 */
inline bool mtl_vertex_format_resize(MTLVertexFormat mtl_format,
                                     uint32_t components,
                                     MTLVertexFormat *r_convertedFormat)
{
  MTLVertexFormat out_vert_format = MTLVertexFormatInvalid;
  switch (mtl_format) {
    /* Char. */
    case MTLVertexFormatChar:
    case MTLVertexFormatChar2:
    case MTLVertexFormatChar3:
    case MTLVertexFormatChar4:
      switch (components) {
        case 1:
          out_vert_format = MTLVertexFormatChar;
          break;
        case 2:
          out_vert_format = MTLVertexFormatChar2;
          break;
        case 3:
          out_vert_format = MTLVertexFormatChar3;
          break;
        case 4:
          out_vert_format = MTLVertexFormatChar4;
          break;
      }
      break;

    /* Normalized Char. */
    case MTLVertexFormatCharNormalized:
    case MTLVertexFormatChar2Normalized:
    case MTLVertexFormatChar3Normalized:
    case MTLVertexFormatChar4Normalized:
      switch (components) {
        case 1:
          out_vert_format = MTLVertexFormatCharNormalized;
          break;
        case 2:
          out_vert_format = MTLVertexFormatChar2Normalized;
          break;
        case 3:
          out_vert_format = MTLVertexFormatChar3Normalized;
          break;
        case 4:
          out_vert_format = MTLVertexFormatChar4Normalized;
          break;
      }
      break;

    /* Unsigned Char. */
    case MTLVertexFormatUChar:
    case MTLVertexFormatUChar2:
    case MTLVertexFormatUChar3:
    case MTLVertexFormatUChar4:
      switch (components) {
        case 1:
          out_vert_format = MTLVertexFormatUChar;
          break;
        case 2:
          out_vert_format = MTLVertexFormatUChar2;
          break;
        case 3:
          out_vert_format = MTLVertexFormatUChar3;
          break;
        case 4:
          out_vert_format = MTLVertexFormatUChar4;
          break;
      }
      break;

    /* Normalized Unsigned char */
    case MTLVertexFormatUCharNormalized:
    case MTLVertexFormatUChar2Normalized:
    case MTLVertexFormatUChar3Normalized:
    case MTLVertexFormatUChar4Normalized:
      switch (components) {
        case 1:
          out_vert_format = MTLVertexFormatUCharNormalized;
          break;
        case 2:
          out_vert_format = MTLVertexFormatUChar2Normalized;
          break;
        case 3:
          out_vert_format = MTLVertexFormatUChar3Normalized;
          break;
        case 4:
          out_vert_format = MTLVertexFormatUChar4Normalized;
          break;
      }
      break;

    /* Short. */
    case MTLVertexFormatShort:
    case MTLVertexFormatShort2:
    case MTLVertexFormatShort3:
    case MTLVertexFormatShort4:
      switch (components) {
        case 1:
          out_vert_format = MTLVertexFormatShort;
          break;
        case 2:
          out_vert_format = MTLVertexFormatShort2;
          break;
        case 3:
          out_vert_format = MTLVertexFormatShort3;
          break;
        case 4:
          out_vert_format = MTLVertexFormatShort4;
          break;
      }
      break;

    /* Normalized Short. */
    case MTLVertexFormatShortNormalized:
    case MTLVertexFormatShort2Normalized:
    case MTLVertexFormatShort3Normalized:
    case MTLVertexFormatShort4Normalized:
      switch (components) {
        case 1:
          out_vert_format = MTLVertexFormatShortNormalized;
          break;
        case 2:
          out_vert_format = MTLVertexFormatShort2Normalized;
          break;
        case 3:
          out_vert_format = MTLVertexFormatShort3Normalized;
          break;
        case 4:
          out_vert_format = MTLVertexFormatShort4Normalized;
          break;
      }
      break;

    /* Unsigned Short. */
    case MTLVertexFormatUShort:
    case MTLVertexFormatUShort2:
    case MTLVertexFormatUShort3:
    case MTLVertexFormatUShort4:
      switch (components) {
        case 1:
          out_vert_format = MTLVertexFormatUShort;
          break;
        case 2:
          out_vert_format = MTLVertexFormatUShort2;
          break;
        case 3:
          out_vert_format = MTLVertexFormatUShort3;
          break;
        case 4:
          out_vert_format = MTLVertexFormatUShort4;
          break;
      }
      break;

    /* Normalized Unsigned Short. */
    case MTLVertexFormatUShortNormalized:
    case MTLVertexFormatUShort2Normalized:
    case MTLVertexFormatUShort3Normalized:
    case MTLVertexFormatUShort4Normalized:
      switch (components) {
        case 1:
          out_vert_format = MTLVertexFormatUShortNormalized;
          break;
        case 2:
          out_vert_format = MTLVertexFormatUShort2Normalized;
          break;
        case 3:
          out_vert_format = MTLVertexFormatUShort3Normalized;
          break;
        case 4:
          out_vert_format = MTLVertexFormatUShort4Normalized;
          break;
      }
      break;

    /* Integer. */
    case MTLVertexFormatInt:
    case MTLVertexFormatInt2:
    case MTLVertexFormatInt3:
    case MTLVertexFormatInt4:
      switch (components) {
        case 1:
          out_vert_format = MTLVertexFormatInt;
          break;
        case 2:
          out_vert_format = MTLVertexFormatInt2;
          break;
        case 3:
          out_vert_format = MTLVertexFormatInt3;
          break;
        case 4:
          out_vert_format = MTLVertexFormatInt4;
          break;
      }
      break;

    /* Unsigned Integer. */
    case MTLVertexFormatUInt:
    case MTLVertexFormatUInt2:
    case MTLVertexFormatUInt3:
    case MTLVertexFormatUInt4:
      switch (components) {
        case 1:
          out_vert_format = MTLVertexFormatUInt;
          break;
        case 2:
          out_vert_format = MTLVertexFormatUInt2;
          break;
        case 3:
          out_vert_format = MTLVertexFormatUInt3;
          break;
        case 4:
          out_vert_format = MTLVertexFormatUInt4;
          break;
      }
      break;

    /* Half. */
    case MTLVertexFormatHalf:
    case MTLVertexFormatHalf2:
    case MTLVertexFormatHalf3:
    case MTLVertexFormatHalf4:
      switch (components) {
        case 1:
          out_vert_format = MTLVertexFormatHalf;
          break;
        case 2:
          out_vert_format = MTLVertexFormatHalf2;
          break;
        case 3:
          out_vert_format = MTLVertexFormatHalf3;
          break;
        case 4:
          out_vert_format = MTLVertexFormatHalf4;
          break;
      }
      break;

    /* Float. */
    case MTLVertexFormatFloat:
    case MTLVertexFormatFloat2:
    case MTLVertexFormatFloat3:
    case MTLVertexFormatFloat4:
      switch (components) {
        case 1:
          out_vert_format = MTLVertexFormatFloat;
          break;
        case 2:
          out_vert_format = MTLVertexFormatFloat2;
          break;
        case 3:
          out_vert_format = MTLVertexFormatFloat3;
          break;
        case 4:
          out_vert_format = MTLVertexFormatFloat4;
          break;
      }
      break;

    /* Other formats */
    default:
      out_vert_format = mtl_format;
      break;
  }
  *r_convertedFormat = out_vert_format;
  return out_vert_format != MTLVertexFormatInvalid;
}

/**
 * Returns whether the METAL API can internally convert between the input type of data in the
 * incoming vertex buffer and the format used by the vertex attribute inside the shader.
 *
 * - Returns TRUE if the type can be converted internally, along with returning the appropriate
 *   type to be passed into the #MTLVertexAttributeDescriptorPSO.
 *
 * - Returns FALSE if the type cannot be converted internally e.g. casting Int4 to Float4.
 *
 * If implicit conversion is not possible, then we can fallback to performing manual attribute
 * conversion using the special attribute read function specializations in the shader.
 * These functions selectively convert between types based on the specified vertex
 * attribute `GPUVertFetchMode fetch_mode` e.g. `GPU_FETCH_INT`.
 */
inline bool mtl_convert_vertex_format(MTLVertexFormat shader_attrib_format,
                                      GPUVertCompType component_type,
                                      uint32_t component_length,
                                      GPUVertFetchMode fetch_mode,
                                      MTLVertexFormat *r_convertedFormat)
{
  bool normalized = (fetch_mode == GPU_FETCH_INT_TO_FLOAT_UNIT);
  MTLVertexFormat out_vert_format = MTLVertexFormatInvalid;

  switch (component_type) {

    case GPU_COMP_I8:
      switch (fetch_mode) {
        case GPU_FETCH_INT:
          if (shader_attrib_format == MTLVertexFormatChar ||
              shader_attrib_format == MTLVertexFormatChar2 ||
              shader_attrib_format == MTLVertexFormatChar3 ||
              shader_attrib_format == MTLVertexFormatChar4) {

            /* No conversion Needed (as type matches) - Just a vector resize if needed. */
            bool can_convert = mtl_vertex_format_resize(
                shader_attrib_format, component_type, &out_vert_format);

            /* Ensure format resize successful. */
            BLI_assert(can_convert);
            UNUSED_VARS_NDEBUG(can_convert);
          }
          else if (shader_attrib_format == MTLVertexFormatInt4 && component_length == 4) {
            /* Allow type expansion - Shader expects MTLVertexFormatInt4, we can supply a type
             * with fewer bytes if component count is the same. Sign must also match original type
             *  -- which is not a problem in this case. */
            out_vert_format = MTLVertexFormatChar4;
          }
          else if (shader_attrib_format == MTLVertexFormatInt3 && component_length == 3) {
            /* Same as above case for matching length and signage (Len=3)*/
            out_vert_format = MTLVertexFormatChar3;
          }
          else if (shader_attrib_format == MTLVertexFormatInt2 && component_length == 2) {
            /* Same as above case for matching length and signage (Len=2)*/
            out_vert_format = MTLVertexFormatChar2;
          }
          else if (shader_attrib_format == MTLVertexFormatInt && component_length == 1) {
            /* Same as above case for matching length and signage (Len=1)*/
            out_vert_format = MTLVertexFormatChar;
          }
          else if (shader_attrib_format == MTLVertexFormatInt && component_length == 4) {
            /* Special case here, format has been specified as GPU_COMP_U8 with 4 components, which
             * is equivalent to an Int -- so data will be compatible with the shader interface. */
            out_vert_format = MTLVertexFormatInt;
          }
          else {
            BLI_assert_msg(false,
                           "Source vertex data format is either Char, Char2, Char3, Char4 but "
                           "format in shader interface is NOT compatible.\n");
            out_vert_format = MTLVertexFormatInvalid;
          }
          break;

        /* Source vertex data is integer type, but shader interface type is floating point.
         * If the input attribute is specified as normalized, we can convert. */
        case GPU_FETCH_FLOAT:
        case GPU_FETCH_INT_TO_FLOAT:
        case GPU_FETCH_INT_TO_FLOAT_UNIT:
          if (normalized) {
            switch (component_length) {
              case 1:
                out_vert_format = MTLVertexFormatCharNormalized;
                break;
              case 2:
                out_vert_format = MTLVertexFormatChar2Normalized;
                break;
              case 3:
                out_vert_format = MTLVertexFormatChar3Normalized;
                break;
              case 4:
                out_vert_format = MTLVertexFormatChar4Normalized;
                break;
              default:
                BLI_assert_msg(false, "invalid vertex format");
                out_vert_format = MTLVertexFormatInvalid;
            }
          }
          else {
            /* Cannot convert. */
            out_vert_format = MTLVertexFormatInvalid;
          }
          break;
      }
      break;

    case GPU_COMP_U8:
      switch (fetch_mode) {
        /* Fetching INT: Check backing shader format matches source input. */
        case GPU_FETCH_INT:
          if (shader_attrib_format == MTLVertexFormatUChar ||
              shader_attrib_format == MTLVertexFormatUChar2 ||
              shader_attrib_format == MTLVertexFormatUChar3 ||
              shader_attrib_format == MTLVertexFormatUChar4) {

            /* No conversion Needed (as type matches) - Just a vector resize if needed. */
            bool can_convert = mtl_vertex_format_resize(
                shader_attrib_format, component_length, &out_vert_format);

            /* Ensure format resize successful. */
            BLI_assert(can_convert);
            UNUSED_VARS_NDEBUG(can_convert);
            /* TODO(Metal): Add other format conversions if needed. Currently no attributes hit
             * this path. */
          }
          else if (shader_attrib_format == MTLVertexFormatUInt4 && component_length == 4) {
            /* Allow type expansion - Shader expects MTLVertexFormatUInt4, we can supply a type
             * with fewer bytes if component count is the same. */
            out_vert_format = MTLVertexFormatUChar4;
          }
          else if (shader_attrib_format == MTLVertexFormatUInt3 && component_length == 3) {
            /* Same as above case for matching length and signage (Len=3)*/
            out_vert_format = MTLVertexFormatUChar3;
          }
          else if (shader_attrib_format == MTLVertexFormatUInt2 && component_length == 2) {
            /* Same as above case for matching length and signage (Len=2)*/
            out_vert_format = MTLVertexFormatUChar2;
          }
          else if (shader_attrib_format == MTLVertexFormatUInt && component_length == 1) {
            /* Same as above case for matching length and signage (Len=1)*/
            out_vert_format = MTLVertexFormatUChar;
          }
          else if (shader_attrib_format == MTLVertexFormatInt && component_length == 4) {
            /* Special case here, format has been specified as GPU_COMP_U8 with 4 components, which
             * is equivalent to an Int-- so data will be compatible with shader interface. */
            out_vert_format = MTLVertexFormatInt;
          }
          else if (shader_attrib_format == MTLVertexFormatUInt && component_length == 4) {
            /* Special case here, format has been specified as GPU_COMP_U8 with 4 components, which
             *is equivalent to a UInt-- so data will be compatible with shader interface. */
            out_vert_format = MTLVertexFormatUInt;
          }
          else {
            BLI_assert_msg(false,
                           "Source vertex data format is either UChar, UChar2, UChar3, UChar4 but "
                           "format in shader interface is NOT compatible.\n");
            out_vert_format = MTLVertexFormatInvalid;
          }
          break;

        /* Source vertex data is integral type, but shader interface type is floating point.
         * If the input attribute is specified as normalized, we can convert. */
        case GPU_FETCH_FLOAT:
        case GPU_FETCH_INT_TO_FLOAT:
        case GPU_FETCH_INT_TO_FLOAT_UNIT:
          if (normalized) {
            switch (component_length) {
              case 1:
                out_vert_format = MTLVertexFormatUCharNormalized;
                break;
              case 2:
                out_vert_format = MTLVertexFormatUChar2Normalized;
                break;
              case 3:
                out_vert_format = MTLVertexFormatUChar3Normalized;
                break;
              case 4:
                out_vert_format = MTLVertexFormatUChar4Normalized;
                break;
              default:
                BLI_assert_msg(false, "invalid vertex format");
                out_vert_format = MTLVertexFormatInvalid;
            }
          }
          else {
            /* Cannot convert. */
            out_vert_format = MTLVertexFormatInvalid;
          }
          break;
      }
      break;

    case GPU_COMP_I16:
      switch (fetch_mode) {
        case GPU_FETCH_INT:
          if (shader_attrib_format == MTLVertexFormatShort ||
              shader_attrib_format == MTLVertexFormatShort2 ||
              shader_attrib_format == MTLVertexFormatShort3 ||
              shader_attrib_format == MTLVertexFormatShort4) {
            /* No conversion Needed (as type matches) - Just a vector resize if needed. */
            bool can_convert = mtl_vertex_format_resize(
                shader_attrib_format, component_length, &out_vert_format);

            /* Ensure conversion successful. */
            BLI_assert(can_convert);
            UNUSED_VARS_NDEBUG(can_convert);
          }
          else {
            BLI_assert_msg(false,
                           "Source vertex data format is either Short, Short2, Short3, Short4 but "
                           "format in shader interface is NOT compatible.\n");
            out_vert_format = MTLVertexFormatInvalid;
          }
          break;

        /* Source vertex data is integral type, but shader interface type is floating point.
         * If the input attribute is specified as normalized, we can convert. */
        case GPU_FETCH_FLOAT:
        case GPU_FETCH_INT_TO_FLOAT:
        case GPU_FETCH_INT_TO_FLOAT_UNIT:
          if (normalized) {
            switch (component_length) {
              case 1:
                out_vert_format = MTLVertexFormatShortNormalized;
                break;
              case 2:
                out_vert_format = MTLVertexFormatShort2Normalized;
                break;
              case 3:
                out_vert_format = MTLVertexFormatShort3Normalized;
                break;
              case 4:
                out_vert_format = MTLVertexFormatShort4Normalized;
                break;
              default:
                BLI_assert_msg(false, "invalid vertex format");
                out_vert_format = MTLVertexFormatInvalid;
            }
          }
          else {
            /* Cannot convert. */
            out_vert_format = MTLVertexFormatInvalid;
          }
          break;
      }
      break;

    case GPU_COMP_U16:
      switch (fetch_mode) {
        case GPU_FETCH_INT:
          if (shader_attrib_format == MTLVertexFormatUShort ||
              shader_attrib_format == MTLVertexFormatUShort2 ||
              shader_attrib_format == MTLVertexFormatUShort3 ||
              shader_attrib_format == MTLVertexFormatUShort4) {
            /* No conversion Needed (as type matches) - Just a vector resize if needed. */
            bool can_convert = mtl_vertex_format_resize(
                shader_attrib_format, component_length, &out_vert_format);

            /* Ensure format resize successful. */
            BLI_assert(can_convert);
            UNUSED_VARS_NDEBUG(can_convert);
          }
          else {
            BLI_assert_msg(false,
                           "Source vertex data format is either UShort, UShort2, UShort3, UShort4 "
                           "but format in shader interface is NOT compatible.\n");
            out_vert_format = MTLVertexFormatInvalid;
          }
          break;

        /* Source vertex data is integral type, but shader interface type is floating point.
         * If the input attribute is specified as normalized, we can convert. */
        case GPU_FETCH_FLOAT:
        case GPU_FETCH_INT_TO_FLOAT:
        case GPU_FETCH_INT_TO_FLOAT_UNIT:
          if (normalized) {
            switch (component_length) {
              case 1:
                out_vert_format = MTLVertexFormatUShortNormalized;
                break;
              case 2:
                out_vert_format = MTLVertexFormatUShort2Normalized;
                break;
              case 3:
                out_vert_format = MTLVertexFormatUShort3Normalized;
                break;
              case 4:
                out_vert_format = MTLVertexFormatUShort4Normalized;
                break;
              default:
                BLI_assert_msg(false, "invalid vertex format");
                out_vert_format = MTLVertexFormatInvalid;
            }
          }
          else {
            /* Cannot convert. */
            out_vert_format = MTLVertexFormatInvalid;
          }
          break;
      }
      break;

    case GPU_COMP_I32:
      switch (fetch_mode) {
        case GPU_FETCH_INT:
          if (shader_attrib_format == MTLVertexFormatInt ||
              shader_attrib_format == MTLVertexFormatInt2 ||
              shader_attrib_format == MTLVertexFormatInt3 ||
              shader_attrib_format == MTLVertexFormatInt4) {
            /* No conversion Needed (as type matches) - Just a vector resize if needed. */
            bool can_convert = mtl_vertex_format_resize(
                shader_attrib_format, component_length, &out_vert_format);

            /* Verify conversion successful. */
            BLI_assert(can_convert);
            UNUSED_VARS_NDEBUG(can_convert);
          }
          else {
            BLI_assert_msg(false,
                           "Source vertex data format is either Int, Int2, Int3, Int4 but format "
                           "in shader interface is NOT compatible.\n");
            out_vert_format = MTLVertexFormatInvalid;
          }
          break;
        case GPU_FETCH_FLOAT:
        case GPU_FETCH_INT_TO_FLOAT:
        case GPU_FETCH_INT_TO_FLOAT_UNIT:
          /* Unfortunately we cannot implicitly convert between Int and Float in METAL. */
          out_vert_format = MTLVertexFormatInvalid;
          break;
      }
      break;

    case GPU_COMP_U32:
      switch (fetch_mode) {
        case GPU_FETCH_INT:
          if (shader_attrib_format == MTLVertexFormatUInt ||
              shader_attrib_format == MTLVertexFormatUInt2 ||
              shader_attrib_format == MTLVertexFormatUInt3 ||
              shader_attrib_format == MTLVertexFormatUInt4) {
            /* No conversion Needed (as type matches) - Just a vector resize if needed. */
            bool can_convert = mtl_vertex_format_resize(
                shader_attrib_format, component_length, &out_vert_format);

            /* Verify conversion successful. */
            BLI_assert(can_convert);
            UNUSED_VARS_NDEBUG(can_convert);
          }
          else {
            BLI_assert_msg(false,
                           "Source vertex data format is either UInt, UInt2, UInt3, UInt4 but "
                           "format in shader interface is NOT compatible.\n");
            out_vert_format = MTLVertexFormatInvalid;
          }
          break;
        case GPU_FETCH_FLOAT:
        case GPU_FETCH_INT_TO_FLOAT:
        case GPU_FETCH_INT_TO_FLOAT_UNIT:
          /* Unfortunately we cannot convert between UInt and Float in METAL */
          out_vert_format = MTLVertexFormatInvalid;
          break;
      }
      break;

    case GPU_COMP_F32:
      switch (fetch_mode) {

        /* Source data is float. This will be compatible
         * if type specified in shader is also float. */
        case GPU_FETCH_FLOAT:
        case GPU_FETCH_INT_TO_FLOAT:
        case GPU_FETCH_INT_TO_FLOAT_UNIT:
          if (shader_attrib_format == MTLVertexFormatFloat ||
              shader_attrib_format == MTLVertexFormatFloat2 ||
              shader_attrib_format == MTLVertexFormatFloat3 ||
              shader_attrib_format == MTLVertexFormatFloat4) {
            /* No conversion Needed (as type matches) - Just a vector resize, if needed. */
            bool can_convert = mtl_vertex_format_resize(
                shader_attrib_format, component_length, &out_vert_format);

            /* Verify conversion successful. */
            BLI_assert(can_convert);
            UNUSED_VARS_NDEBUG(can_convert);
          }
          else {
            BLI_assert_msg(false,
                           "Source vertex data format is either Float, Float2, Float3, Float4 but "
                           "format in shader interface is NOT compatible.\n");
            out_vert_format = MTLVertexFormatInvalid;
          }
          break;

        case GPU_FETCH_INT:
          /* Unfortunately we cannot convert between Float and Int implicitly in METAL. */
          out_vert_format = MTLVertexFormatInvalid;
          break;
      }
      break;

    case GPU_COMP_I10:
      out_vert_format = MTLVertexFormatInt1010102Normalized;
      break;
  }
  *r_convertedFormat = out_vert_format;
  return (out_vert_format != MTLVertexFormatInvalid);
}

inline uint comp_count_from_vert_format(MTLVertexFormat vert_format)
{
  switch (vert_format) {
    case MTLVertexFormatFloat:
    case MTLVertexFormatInt:
    case MTLVertexFormatUInt:
    case MTLVertexFormatShort:
    case MTLVertexFormatUChar:
    case MTLVertexFormatUCharNormalized:
      return 1;
    case MTLVertexFormatUChar2:
    case MTLVertexFormatUInt2:
    case MTLVertexFormatFloat2:
    case MTLVertexFormatInt2:
    case MTLVertexFormatUChar2Normalized:
      return 2;
    case MTLVertexFormatUChar3:
    case MTLVertexFormatUInt3:
    case MTLVertexFormatFloat3:
    case MTLVertexFormatInt3:
    case MTLVertexFormatShort3Normalized:
    case MTLVertexFormatUChar3Normalized:
      return 3;
    case MTLVertexFormatUChar4:
    case MTLVertexFormatFloat4:
    case MTLVertexFormatUInt4:
    case MTLVertexFormatInt4:
    case MTLVertexFormatUChar4Normalized:
    case MTLVertexFormatInt1010102Normalized:

    default:
      BLI_assert_msg(false, "Unrecognized attribute type. Add types to switch as needed.");
      return 0;
  }
}

inline GPUVertFetchMode fetchmode_from_vert_format(MTLVertexFormat vert_format)
{
  switch (vert_format) {
    case MTLVertexFormatFloat:
    case MTLVertexFormatFloat2:
    case MTLVertexFormatFloat3:
    case MTLVertexFormatFloat4:
      return GPU_FETCH_FLOAT;

    case MTLVertexFormatUChar:
    case MTLVertexFormatUChar2:
    case MTLVertexFormatUChar3:
    case MTLVertexFormatUChar4:
    case MTLVertexFormatChar:
    case MTLVertexFormatChar2:
    case MTLVertexFormatChar3:
    case MTLVertexFormatChar4:
    case MTLVertexFormatUShort:
    case MTLVertexFormatUShort2:
    case MTLVertexFormatUShort3:
    case MTLVertexFormatUShort4:
    case MTLVertexFormatShort:
    case MTLVertexFormatShort2:
    case MTLVertexFormatShort3:
    case MTLVertexFormatShort4:
    case MTLVertexFormatUInt:
    case MTLVertexFormatUInt2:
    case MTLVertexFormatUInt3:
    case MTLVertexFormatUInt4:
    case MTLVertexFormatInt:
    case MTLVertexFormatInt2:
    case MTLVertexFormatInt3:
    case MTLVertexFormatInt4:
      return GPU_FETCH_INT;

    case MTLVertexFormatUCharNormalized:
    case MTLVertexFormatUChar2Normalized:
    case MTLVertexFormatUChar3Normalized:
    case MTLVertexFormatUChar4Normalized:
    case MTLVertexFormatCharNormalized:
    case MTLVertexFormatChar2Normalized:
    case MTLVertexFormatChar3Normalized:
    case MTLVertexFormatChar4Normalized:
    case MTLVertexFormatUShortNormalized:
    case MTLVertexFormatUShort2Normalized:
    case MTLVertexFormatUShort3Normalized:
    case MTLVertexFormatUShort4Normalized:
    case MTLVertexFormatShortNormalized:
    case MTLVertexFormatShort2Normalized:
    case MTLVertexFormatShort3Normalized:
    case MTLVertexFormatShort4Normalized:
    case MTLVertexFormatInt1010102Normalized:
      return GPU_FETCH_INT_TO_FLOAT_UNIT;

    default:
      BLI_assert_msg(false, "Unrecognized attribute type. Add types to switch as needed.");
      return GPU_FETCH_FLOAT;
  }
}

inline GPUVertCompType comp_type_from_vert_format(MTLVertexFormat vert_format)
{
  switch (vert_format) {
    case MTLVertexFormatUChar:
    case MTLVertexFormatUChar2:
    case MTLVertexFormatUChar3:
    case MTLVertexFormatUChar4:
    case MTLVertexFormatUCharNormalized:
    case MTLVertexFormatUChar2Normalized:
    case MTLVertexFormatUChar3Normalized:
    case MTLVertexFormatUChar4Normalized:
      return GPU_COMP_U8;

    case MTLVertexFormatChar:
    case MTLVertexFormatChar2:
    case MTLVertexFormatChar3:
    case MTLVertexFormatChar4:
    case MTLVertexFormatCharNormalized:
    case MTLVertexFormatChar2Normalized:
    case MTLVertexFormatChar3Normalized:
    case MTLVertexFormatChar4Normalized:
      return GPU_COMP_I8;

    case MTLVertexFormatShort:
    case MTLVertexFormatShort2:
    case MTLVertexFormatShort3:
    case MTLVertexFormatShort4:
    case MTLVertexFormatShortNormalized:
    case MTLVertexFormatShort2Normalized:
    case MTLVertexFormatShort3Normalized:
    case MTLVertexFormatShort4Normalized:
      return GPU_COMP_I16;

    case MTLVertexFormatUShort:
    case MTLVertexFormatUShort2:
    case MTLVertexFormatUShort3:
    case MTLVertexFormatUShort4:
    case MTLVertexFormatUShortNormalized:
    case MTLVertexFormatUShort2Normalized:
    case MTLVertexFormatUShort3Normalized:
    case MTLVertexFormatUShort4Normalized:
      return GPU_COMP_U16;

    case MTLVertexFormatInt:
    case MTLVertexFormatInt2:
    case MTLVertexFormatInt3:
    case MTLVertexFormatInt4:
      return GPU_COMP_I32;

    case MTLVertexFormatUInt:
    case MTLVertexFormatUInt2:
    case MTLVertexFormatUInt3:
    case MTLVertexFormatUInt4:
      return GPU_COMP_U32;

    case MTLVertexFormatFloat:
    case MTLVertexFormatFloat2:
    case MTLVertexFormatFloat3:
    case MTLVertexFormatFloat4:
      return GPU_COMP_F32;

    case MTLVertexFormatInt1010102Normalized:
      return GPU_COMP_I10;

    default:
      BLI_assert_msg(false, "Unrecognized attribute type. Add types to switch as needed.");
      return GPU_COMP_F32;
  }
}

}  // namespace blender::gpu