-
Notifications
You must be signed in to change notification settings - Fork 0
/
rjd_gfx_metal.h
1117 lines (908 loc) · 41.9 KB
/
rjd_gfx_metal.h
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
#pragma once
#define RJD_GFX_METAL_H 1
#if RJD_IMPL && RJD_GFX_BACKEND_METAL
#if !RJD_PLATFORM_OSX
#error "Metal backend is only supported on OSX."
#endif
#if !RJD_GFX_H
#error "This header depends on rjd_gfx.h"
#endif
////////////////////////////////////////////////////////////////////////////////
// includes/libs
#import <Metal/Metal.h>
#import <MetalKit/MetalKit.h>
////////////////////////////////////////////////////////////////////////////////
// data
struct rjd_gfx_texture_metal
{
id <MTLTexture> texture;
};
struct rjd_gfx_shader_metal
{
id<MTLLibrary> library;
id<MTLFunction> function;
};
struct rjd_gfx_pipeline_state_metal
{
id<MTLRenderPipelineState> state_render;
id<MTLDepthStencilState> state_depthstencil;
enum rjd_gfx_cull cull_mode;
enum rjd_gfx_winding_order winding_order;
};
struct rjd_gfx_mesh_shader_buffer_metal
{
id<MTLBuffer> buffer;
enum rjd_gfx_mesh_buffer_usage_flags usage_flags;
uint32_t shader_slot;
};
struct rjd_gfx_mesh_index_buffer_metal
{
id<MTLBuffer> buffer;
enum rjd_gfx_index_type type;
uint32_t count_indices;
};
struct rjd_gfx_mesh_metal
{
struct rjd_gfx_mesh_shader_buffer_metal* shader_buffers;
struct rjd_gfx_mesh_index_buffer_metal* index_buffers;
uint32_t count_vertices; // 0 if using index_buffers
uint32_t instance_count; // 0 if it's not instanced
enum rjd_gfx_primitive_type primitive;
};
struct rjd_gfx_command_buffer_metal
{
id <MTLCommandBuffer> buffer;
id<MTLRenderCommandEncoder> encoder;
};
struct rjd_gfx_context_metal
{
struct rjd_gfx_texture_metal* slotmap_textures;
struct rjd_gfx_shader_metal* slotmap_shaders;
struct rjd_gfx_pipeline_state_metal* slotmap_pipeline_states;
struct rjd_gfx_mesh_metal* slotmap_meshes;
struct rjd_gfx_command_buffer_metal* slotmap_command_buffers;
MTKView* view;
id <MTLDevice> device;
id <MTLCommandQueue> command_queue;
MTKMeshBufferAllocator* loader_mesh;
struct rjd_mem_allocator* allocator;
dispatch_semaphore_t wait_for_present_counter;
uint32_t backbuffer_index;
};
RJD_STATIC_ASSERT(sizeof(struct rjd_gfx_context_metal) <= sizeof(struct rjd_gfx_context));
////////////////////////////////////////////////////////////////////////////////
// private helpers
static MTLPixelFormat rjd_gfx_format_color_to_metal(enum rjd_gfx_format format);
static MTLClearColor rjd_gfx_format_value_to_clearcolor(struct rjd_gfx_format_value value);
static bool rjd_gfx_format_isbackbuffercompatible(enum rjd_gfx_format format);
static bool rjd_gfx_mtlformat_is_depth(MTLPixelFormat format);
static bool rjd_gfx_mtlformat_is_stencil(MTLPixelFormat format);
static MTLVertexFormat rjd_gfx_vertex_format_type_to_metal(enum rjd_gfx_vertex_format_type type);
static MTLVertexStepFunction rjd_gfx_vertex_format_step_to_metal(enum rjd_gfx_vertex_format_step step);
static MTLIndexType rjd_gfx_index_type_to_metal(enum rjd_gfx_index_type type);
static MTLPrimitiveType rjd_gfx_primitive_type_to_metal(enum rjd_gfx_primitive_type type);
static MTLCompareFunction rjd_gfx_depth_compare_to_metal(enum rjd_gfx_depth_compare func);
static MTLWinding rjd_gfx_winding_to_metal(enum rjd_gfx_winding_order winding_order);
static MTLCullMode rjd_gfx_cull_to_metal(enum rjd_gfx_cull cull_mode);
static bool rjd_gfx_texture_isbackbuffer(struct rjd_gfx_texture texture);
static inline void rjd_gfx_texture_destroy_metal(struct rjd_gfx_context_metal* context, struct rjd_slot slot);
static inline void rjd_gfx_shader_destroy_metal(struct rjd_gfx_context_metal* context, struct rjd_slot slot);
static inline void rjd_gfx_pipeline_state_destroy_metal(struct rjd_gfx_context_metal* context, struct rjd_slot slot);
static inline void rjd_gfx_mesh_destroy_metal(struct rjd_gfx_context_metal* context, struct rjd_slot slot);
static inline void rjd_gfx_command_buffer_destroy_metal(struct rjd_gfx_context_metal* context, struct rjd_slot slot);
////////////////////////////////
// render control
struct rjd_result rjd_gfx_context_create(struct rjd_gfx_context* out, struct rjd_gfx_context_desc desc)
{
RJD_ASSERT(out);
MTKView* view = desc.osx.view;
if(!view.device) {
return RJD_RESULT("You must pass a view with an already-initialized Metal device");
}
if (!rjd_gfx_format_isbackbuffercompatible(desc.backbuffer_color_format)) {
return RJD_RESULT("Unsupported backbuffer color format. Supported values are:\n"
"\tRJD_GFX_FORMAT_COLOR_U8_BGRA_NORM\n"
"\tRJD_GFX_FORMAT_COLOR_U8_BGRA_NORM_SRGB\n");
}
if (!rjd_gfx_format_isdepthstencil(desc.backbuffer_depth_format)) {
return RJD_RESULT("backbuffer_depth_format must be a depth format. See rjd_gfx_format_isdepthstencil().");
}
if (desc.num_backbuffers != RJD_GFX_NUM_BACKBUFFERS_TRIPLE) {
return RJD_RESULT("The Metal backend currently only supports triple buffering. Use RJD_GFX_NUM_BACKBUFFERS_TRIPLE.");
}
{
const MTLPixelFormat mtl_color_format = rjd_gfx_format_color_to_metal(desc.backbuffer_color_format);
const MTLPixelFormat mtl_depth_format = rjd_gfx_format_color_to_metal(desc.backbuffer_depth_format);
view.colorPixelFormat = mtl_color_format;
view.depthStencilPixelFormat = mtl_depth_format;
view.sampleCount = 1; // users can set this higher later with rjd_gfx_backbuffer_set_msaa_count()
}
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)out;
memset(out, 0, sizeof(*out));
context_metal->slotmap_textures = rjd_slotmap_alloc(struct rjd_gfx_texture_metal, 64, desc.allocator);
context_metal->slotmap_shaders = rjd_slotmap_alloc(struct rjd_gfx_shader_metal, 64, desc.allocator);
context_metal->slotmap_pipeline_states = rjd_slotmap_alloc(struct rjd_gfx_pipeline_state_metal, 64, desc.allocator);
context_metal->slotmap_meshes = rjd_slotmap_alloc(struct rjd_gfx_mesh_metal, 64, desc.allocator);
context_metal->slotmap_command_buffers = rjd_slotmap_alloc(struct rjd_gfx_command_buffer_metal, 16, desc.allocator);
context_metal->view = view;
context_metal->device = view.device;
context_metal->command_queue = [context_metal->device newCommandQueue];
context_metal->loader_mesh = [[MTKMeshBufferAllocator alloc] initWithDevice:context_metal->device];
context_metal->allocator = desc.allocator;
context_metal->wait_for_present_counter = dispatch_semaphore_create(3);
context_metal->backbuffer_index = 0;
return RJD_RESULT_OK();
}
struct rjd_result rjd_gfx_wait_for_frame_begin(struct rjd_gfx_context* context)
{
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
dispatch_semaphore_wait(context_metal->wait_for_present_counter, DISPATCH_TIME_FOREVER);
return RJD_RESULT_OK();
}
struct rjd_result rjd_gfx_present(struct rjd_gfx_context* context)
{
// NOTE: this is a no-op for metal since presenting is handled via presentDrawable()
// which is invoked after the command buffer has finished executing all commands
RJD_UNUSED_PARAM(context);
return RJD_RESULT_OK();
}
uint32_t rjd_gfx_backbuffer_current_index(const struct rjd_gfx_context* context)
{
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
return context_metal->backbuffer_index;
}
struct rjd_result rjd_gfx_backbuffer_msaa_is_count_supported(const struct rjd_gfx_context* context, uint32_t sample_count)
{
RJD_ASSERT(context);
const struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
BOOL supported = [context_metal->device supportsTextureSampleCount:(NSUInteger)sample_count];
return supported ? RJD_RESULT_OK() : RJD_RESULT("The MSAA count is not supported.");
}
struct rjd_result rjd_gfx_backbuffer_set_msaa_count(struct rjd_gfx_context* context, uint32_t sample_count)
{
RJD_ASSERT(context);
struct rjd_result result = rjd_gfx_backbuffer_msaa_is_count_supported(context, sample_count);
if (!rjd_result_isok(result)) {
return result;
}
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
context_metal->view.sampleCount = sample_count;
return RJD_RESULT_OK();
}
void rjd_gfx_context_destroy(struct rjd_gfx_context* context)
{
RJD_ASSERT(context);
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
struct rjd_gfx_texture_metal* textures = context_metal->slotmap_textures;
struct rjd_gfx_shader_metal* shaders = context_metal->slotmap_shaders;
struct rjd_gfx_pipeline_state_metal* states = context_metal->slotmap_pipeline_states;
struct rjd_gfx_mesh_metal* meshes = context_metal->slotmap_meshes;
struct rjd_gfx_command_buffer_metal* commands = context_metal->slotmap_command_buffers;
for (struct rjd_slot s = rjd_slotmap_next(textures, NULL); !rjd_slot_isvalid(s); s = rjd_slotmap_next(textures, &s)) {
rjd_gfx_texture_destroy_metal(context_metal, s);
}
for (struct rjd_slot s = rjd_slotmap_next(shaders, NULL); !rjd_slot_isvalid(s); s = rjd_slotmap_next(shaders, &s)) {
rjd_gfx_shader_destroy_metal(context_metal, s);
}
for (struct rjd_slot s = rjd_slotmap_next(states, NULL); !rjd_slot_isvalid(s); s = rjd_slotmap_next(states, &s)) {
rjd_gfx_pipeline_state_destroy_metal(context_metal, s);
}
for (struct rjd_slot s = rjd_slotmap_next(meshes, NULL); !rjd_slot_isvalid(s); s = rjd_slotmap_next(meshes, &s)) {
rjd_gfx_mesh_destroy_metal(context_metal, s);
}
for (struct rjd_slot s = rjd_slotmap_next(commands, NULL); !rjd_slot_isvalid(s); s = rjd_slotmap_next(commands, &s)) {
rjd_gfx_command_buffer_destroy_metal(context_metal, s);
}
rjd_slotmap_free(textures);
rjd_slotmap_free(shaders);
rjd_slotmap_free(states);
rjd_slotmap_free(meshes);
rjd_slotmap_free(commands);
context_metal->slotmap_textures = NULL;
context_metal->slotmap_shaders = NULL;
context_metal->slotmap_pipeline_states = NULL;
context_metal->slotmap_meshes = NULL;
context_metal->slotmap_command_buffers = NULL;
}
struct rjd_result rjd_gfx_vsync_set(struct rjd_gfx_context* context, enum RJD_GFX_VSYNC_MODE mode)
{
RJD_UNUSED_PARAM(context);
RJD_UNUSED_PARAM(mode);
return RJD_RESULT("unimplemented");
}
struct rjd_result rjd_gfx_command_buffer_create(struct rjd_gfx_context* context, struct rjd_gfx_command_buffer* out)
{
RJD_ASSERT(context);
RJD_ASSERT(out);
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
struct rjd_gfx_command_buffer_metal buffer = {
.buffer = [context_metal->command_queue commandBuffer],
};
rjd_slotmap_insert(context_metal->slotmap_command_buffers, buffer, &out->handle);
return RJD_RESULT_OK();
}
struct rjd_result rjd_gfx_command_pass_begin(struct rjd_gfx_context* context, struct rjd_gfx_command_buffer* cmd_buffer, const struct rjd_gfx_pass_begin_desc* command)
{
RJD_ASSERT(rjd_slot_isvalid(cmd_buffer->handle));
RJD_ASSERT(command);
RJD_ASSERT(context);
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
struct rjd_gfx_command_buffer_metal* cmd_buffer_metal = rjd_slotmap_get(context_metal->slotmap_command_buffers, cmd_buffer->handle);
if (cmd_buffer_metal->encoder != nil) {
[cmd_buffer_metal->encoder endEncoding];
cmd_buffer_metal->encoder = nil;
}
MTLRenderPassDescriptor* render_pass = NULL;
if (rjd_gfx_texture_isbackbuffer(command->render_target))
{
if (context_metal->view.currentDrawable) {
render_pass = context_metal->view.currentRenderPassDescriptor;
} else {
return RJD_RESULT("All backbuffers are in use. The app is likely GPU-bound.");
}
}
else
{
struct rjd_gfx_texture_metal* texture_metal = rjd_slotmap_get(context_metal->slotmap_textures, command->render_target.handle);
id<MTLTexture> texture = texture_metal->texture;
if (texture.usage != MTLTextureUsageRenderTarget) {
return RJD_RESULT("Specified texture is not a render target");
}
render_pass = [MTLRenderPassDescriptor renderPassDescriptor];
render_pass.colorAttachments[0].texture = texture;
}
if (rjd_gfx_format_iscolor(command->clear_color.type)) {
const MTLClearColor color = rjd_gfx_format_value_to_clearcolor(command->clear_color);
render_pass.colorAttachments[0].clearColor = color;
}
if (rjd_gfx_texture_isbackbuffer(command->depthstencil_target) == false) {
struct rjd_gfx_texture_metal* texture_metal = rjd_slotmap_get(context_metal->slotmap_textures, command->depthstencil_target.handle);
id<MTLTexture> texture = texture_metal->texture;
render_pass.depthAttachment.texture = texture;
}
if (rjd_gfx_format_isdepthstencil(command->clear_depthstencil.type)) {
if (render_pass.depthAttachment) {
const double depth = rjd_gfx_format_value_to_depth(command->clear_depthstencil);
render_pass.depthAttachment.clearDepth = depth;
}
if (render_pass.stencilAttachment) {
const uint32_t stencil = rjd_gfx_format_value_to_stencil(command->clear_depthstencil);
render_pass.stencilAttachment.clearStencil = stencil;
}
}
id<MTLRenderCommandEncoder> encoder = [cmd_buffer_metal->buffer renderCommandEncoderWithDescriptor:render_pass];
const char* label = command->debug_label ? command->debug_label : "rjd_gfx_command_pass_begin";
encoder.label = [NSString stringWithUTF8String:label];
cmd_buffer_metal->encoder = encoder;
return RJD_RESULT_OK();
}
struct rjd_result rjd_gfx_command_pass_draw(struct rjd_gfx_context* context, struct rjd_gfx_command_buffer* cmd_buffer, const struct rjd_gfx_pass_draw_desc* command)
{
RJD_ASSERT(context);
RJD_ASSERT(cmd_buffer);
RJD_ASSERT(command);
RJD_ASSERT(command->viewport);
RJD_ASSERT(command->pipeline_state);
RJD_ASSERT(command->meshes);
RJD_ASSERT(rjd_slot_isvalid(cmd_buffer->handle));
RJD_ASSERT(rjd_slot_isvalid(command->pipeline_state->handle));
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
struct rjd_gfx_command_buffer_metal* cmd_buffer_metal = rjd_slotmap_get(context_metal->slotmap_command_buffers, cmd_buffer->handle);
const struct rjd_gfx_pipeline_state_metal* pipeline_metal = rjd_slotmap_get(context_metal->slotmap_pipeline_states, command->pipeline_state->handle);
RJD_ASSERTMSG(cmd_buffer_metal->encoder != nil, "You must call rjd_gfx_command_pass_begin before issuing draw commands.");
id<MTLRenderCommandEncoder> encoder = cmd_buffer_metal->encoder;
[encoder pushDebugGroup:[NSString stringWithUTF8String:command->debug_label]];
const MTLWinding winding_order_metal = rjd_gfx_winding_to_metal(pipeline_metal->winding_order);
const MTLCullMode cull_mode_metal = rjd_gfx_cull_to_metal(pipeline_metal->cull_mode);
[encoder setRenderPipelineState:pipeline_metal->state_render];
[encoder setDepthStencilState:pipeline_metal->state_depthstencil];
[encoder setFrontFacingWinding:winding_order_metal];
[encoder setCullMode:cull_mode_metal];
for (uint32_t i = 0; i < command->count_textures; ++i) {
const struct rjd_gfx_texture* texture = command->textures + i;
RJD_ASSERT(rjd_slot_isvalid(texture->handle));
struct rjd_gfx_texture_metal* texture_metal = rjd_slotmap_get(context_metal->slotmap_textures, texture->handle);
const uint32_t* index = command->texture_indices + i;
[encoder setFragmentTexture:texture_metal->texture atIndex:*index];
}
// TODO add functionality to draw portions of an uploaded buffer. For simplicity, currently you must draw the entire buffer.
for (uint32_t mesh_index = 0; mesh_index < command->count_meshes; ++mesh_index) {
const struct rjd_gfx_mesh* mesh = command->meshes + mesh_index;
RJD_ASSERT(rjd_slot_isvalid(mesh->handle));
const struct rjd_gfx_mesh_metal* mesh_metal = rjd_slotmap_get(context_metal->slotmap_meshes, mesh->handle);
// shader buffers
for (uint32_t buffer_index = 0; buffer_index < rjd_array_count(mesh_metal->shader_buffers); ++buffer_index) {
struct rjd_gfx_mesh_shader_buffer_metal* buffer = mesh_metal->shader_buffers + buffer_index;
const struct rjd_gfx_pass_draw_buffer_offset_desc* buffer_offset_desc = NULL;
for (size_t desc_index = 0; desc_index < command->count_constant_descs; ++desc_index) {
const struct rjd_gfx_pass_draw_buffer_offset_desc* desc = command->buffer_offset_descs + desc_index;
if (desc->mesh_index == mesh_index && desc->buffer_index == buffer_index) {
buffer_offset_desc = desc;
}
}
uint32_t offset = buffer_offset_desc ? buffer_offset_desc->offset_bytes : 0;
if (buffer->usage_flags & RJD_GFX_MESH_BUFFER_USAGE_VERTEX) {
[encoder setVertexBuffer:buffer->buffer offset:offset atIndex:buffer->shader_slot];
}
if (buffer->usage_flags & RJD_GFX_MESH_BUFFER_USAGE_VERTEX_CONSTANT ||
buffer->usage_flags & RJD_GFX_MESH_BUFFER_USAGE_PIXEL_CONSTANT) {
if (buffer->usage_flags & RJD_GFX_MESH_BUFFER_USAGE_VERTEX_CONSTANT) {
[encoder setVertexBuffer:buffer->buffer offset:offset atIndex:buffer->shader_slot];
}
if (buffer->usage_flags & RJD_GFX_MESH_BUFFER_USAGE_PIXEL_CONSTANT) {
[encoder setFragmentBuffer:buffer->buffer offset:offset atIndex:buffer->shader_slot];
}
}
}
const MTLPrimitiveType primitive = rjd_gfx_primitive_type_to_metal(mesh_metal->primitive);
if (mesh_metal->count_vertices > 0) {
if (mesh_metal->instance_count) {
[encoder drawPrimitives:primitive vertexStart:0 vertexCount:mesh_metal->count_vertices instanceCount:mesh_metal->instance_count];
} else {
[encoder drawPrimitives:primitive vertexStart:0 vertexCount:mesh_metal->count_vertices];
}
}
if (mesh_metal->index_buffers) {
for (uint32_t i = 0; i < rjd_array_count(mesh_metal->index_buffers); ++i) {
struct rjd_gfx_mesh_index_buffer_metal* indices = mesh_metal->index_buffers + i;
const MTLIndexType type = rjd_gfx_index_type_to_metal(indices->type);
if (mesh_metal->instance_count) {
[encoder drawIndexedPrimitives:primitive
indexCount:indices->count_indices
indexType:type
indexBuffer:indices->buffer
indexBufferOffset:0
instanceCount:mesh_metal->instance_count];
} else {
[encoder drawIndexedPrimitives:primitive
indexCount:indices->count_indices
indexType:type
indexBuffer:indices->buffer
indexBufferOffset:0];
}
}
}
}
[encoder popDebugGroup];
return RJD_RESULT_OK();
}
struct rjd_result rjd_gfx_command_buffer_commit(struct rjd_gfx_context* context, struct rjd_gfx_command_buffer* cmd_buffer)
{
RJD_ASSERT(cmd_buffer);
RJD_ASSERT(context);
RJD_ASSERT(rjd_slot_isvalid(cmd_buffer->handle));
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
struct rjd_gfx_command_buffer_metal* cmd_buffer_metal = rjd_slotmap_get(context_metal->slotmap_command_buffers, cmd_buffer->handle);
RJD_ASSERTMSG(cmd_buffer_metal->encoder != nil, "You must call rjd_gfx_command_pass_begin before issuing draw commands.");
[cmd_buffer_metal->encoder endEncoding];
[cmd_buffer_metal->buffer presentDrawable:context_metal->view.currentDrawable];
__block dispatch_semaphore_t wait_counter = context_metal->wait_for_present_counter;
[cmd_buffer_metal->buffer addCompletedHandler:^(id<MTLCommandBuffer> buffer)
{
RJD_UNUSED_PARAM(buffer);
dispatch_semaphore_signal(wait_counter);
}];
[cmd_buffer_metal->buffer commit];
rjd_gfx_command_buffer_destroy_metal(context_metal, cmd_buffer->handle);
rjd_slot_invalidate(&cmd_buffer->handle);
context_metal->backbuffer_index = (context_metal->backbuffer_index + 1) % 3; // MTKView has a pool of 3 drawables
return RJD_RESULT_OK();
}
////////////////////////////////
// resources
struct rjd_result rjd_gfx_texture_create(struct rjd_gfx_context* context, struct rjd_gfx_texture* out, struct rjd_gfx_texture_desc desc)
{
RJD_ASSERT(out);
RJD_ASSERT(context);
RJD_RESULT_CHECK(desc.usage == RJD_GFX_TEXTURE_USAGE_RENDERTARGET || desc.data != NULL, "Non-rendertarget texture data must not be NULL");
RJD_RESULT_CHECK(desc.usage == RJD_GFX_TEXTURE_USAGE_RENDERTARGET || desc.data_length != 0, "Non-rendertarget texture data length must not be 0");
RJD_RESULT_CHECK(desc.pixels_width != 0, "Texture width must not be 0");
RJD_RESULT_CHECK(desc.pixels_height != 0, "Texture height must not be 0");
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
id<MTLTexture> texture = nil;
{
MTLTextureDescriptor* descriptor = [[MTLTextureDescriptor alloc] init];
descriptor.textureType = MTLTextureType2D;
descriptor.pixelFormat = rjd_gfx_format_color_to_metal(desc.format);
descriptor.width = desc.pixels_width;
descriptor.height = desc.pixels_height;
descriptor.depth = 1; // Always 1 for 2D textures
descriptor.mipmapLevelCount = 1; // TODO support mipmaps
descriptor.sampleCount = 1; // Always 1 unless this is a MTLTextureType2DMultisample
descriptor.arrayLength = 1;
MTLResourceOptions buffer_options;
switch (desc.access)
{
case RJD_GFX_TEXTURE_ACCESS_CPU_NONE_GPU_READWRITE:
descriptor.cpuCacheMode = MTLCPUCacheModeDefaultCache;
descriptor.storageMode = MTLStorageModePrivate;
descriptor.allowGPUOptimizedContents = YES;
buffer_options = MTLResourceStorageModePrivate;
break;
case RJD_GFX_TEXTURE_ACCESS_CPU_WRITE_GPU_READWRITE:
descriptor.cpuCacheMode = MTLCPUCacheModeWriteCombined;
descriptor.allowGPUOptimizedContents = NO;
buffer_options = MTLResourceStorageModeManaged | MTLResourceCPUCacheModeWriteCombined;
break;
case RJD_GFX_TEXTURE_ACCESS_COUNT:
RJD_ASSERTFAIL("RJD_GFX_TEXTURE_ACCESS_COUNT is not a valid access.")
break;
}
switch (desc.usage)
{
case RJD_GFX_TEXTURE_USAGE_DEFAULT:
descriptor.usage = MTLTextureUsageShaderRead;
break;
case RJD_GFX_TEXTURE_USAGE_RENDERTARGET:
descriptor.usage = MTLTextureUsageRenderTarget;
break;
case RJD_GFX_TEXTURE_USAGE_COUNT:
RJD_ASSERTFAIL("Unhandled case %d", desc.usage);
break;
}
// TODO support no bytes for render targets
id<MTLBuffer> buffer = [context_metal->device newBufferWithBytes:desc.data length:desc.data_length options:buffer_options];
const NSUInteger bytes_per_row = rjd_gfx_format_bytesize(desc.format) * desc.pixels_width;
texture = [buffer newTextureWithDescriptor:descriptor offset:0 bytesPerRow:bytes_per_row];
MTLRegion region = {
.origin = {0,0,0},
.size = { desc.pixels_width, desc.pixels_height, 1 },
};
[texture replaceRegion:region mipmapLevel:0 withBytes:desc.data bytesPerRow:bytes_per_row];
const char* label = desc.debug_label ? desc.debug_label : "anonymous_texture";
texture.label = [NSString stringWithUTF8String:label];
}
struct rjd_gfx_texture_metal texture_metal = {
.texture = texture,
};
rjd_slotmap_insert(context_metal->slotmap_textures, texture_metal, &out->handle);
return RJD_RESULT_OK();
}
void rjd_gfx_texture_destroy(struct rjd_gfx_context* context, struct rjd_gfx_texture* texture)
{
RJD_ASSERT(texture);
RJD_ASSERT(context);
RJD_ASSERT(rjd_slot_isvalid(texture->handle));
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
rjd_gfx_texture_destroy_metal(context_metal, texture->handle);
}
struct rjd_result rjd_gfx_shader_create(struct rjd_gfx_context* context, struct rjd_gfx_shader* out, struct rjd_gfx_shader_desc desc)
{
RJD_ASSERT(out);
RJD_ASSERT(context);
RJD_ASSERTMSG(((char*)desc.data)[desc.count_data] == '\0', "Expected a null-terminated string for the data.");
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
NSString* source = [NSString stringWithUTF8String:desc.data];
MTLCompileOptions* options = [[MTLCompileOptions alloc] init];
NSError* error = NULL;
id<MTLLibrary> library = [context_metal->device newLibraryWithSource:source options:options error:&error];
if (error) {
RJD_GFX_LOG_ERROR("Error compiling shader: %s", error.localizedDescription.UTF8String);
return RJD_RESULT("Error compiling shaders");
}
NSString* function_name = [NSString stringWithUTF8String:desc.function_name];
id<MTLFunction> function = [library newFunctionWithName:function_name];
if (function == nil) {
return RJD_RESULT("The shader source did not have the specified function name.");
}
struct rjd_gfx_shader_metal shader_metal = {
.library = library,
.function = function,
};
rjd_slotmap_insert(context_metal->slotmap_shaders, shader_metal, &out->handle);
return RJD_RESULT_OK();
}
void rjd_gfx_shader_destroy(struct rjd_gfx_context* context, struct rjd_gfx_shader* shader)
{
RJD_ASSERT(shader);
RJD_ASSERT(context);
RJD_ASSERT(rjd_slot_isvalid(shader->handle));
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
rjd_gfx_shader_destroy_metal(context_metal, shader->handle);
}
struct rjd_result rjd_gfx_pipeline_state_create(struct rjd_gfx_context* context, struct rjd_gfx_pipeline_state* out, struct rjd_gfx_pipeline_state_desc desc)
{
RJD_ASSERT(out);
RJD_ASSERT(context);
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
id <MTLRenderPipelineState> pipeline_state = nil;
{
MTLRenderPipelineDescriptor *desc_metal = [[MTLRenderPipelineDescriptor alloc] init];
if (desc.debug_name != NULL) {
desc_metal.label = [NSString stringWithUTF8String:desc.debug_name];
}
if (rjd_slot_isvalid(desc.render_target.handle)) {
struct rjd_gfx_texture_metal* texture_metal = rjd_slotmap_get(context_metal->slotmap_textures, desc.render_target.handle);
id<MTLTexture> target = texture_metal->texture;
desc_metal.sampleCount = target.sampleCount;
desc_metal.colorAttachments[0].pixelFormat = target.pixelFormat;
} else {
MTKView* view = context_metal->view;
desc_metal.sampleCount = view.sampleCount;
desc_metal.colorAttachments[0].pixelFormat = view.colorPixelFormat;
}
{
MTLPixelFormat source_format = MTLPixelFormatInvalid;
if (rjd_gfx_texture_isbackbuffer(desc.depthstencil_target)) {
MTKView* view = context_metal->view;
source_format = view.depthStencilPixelFormat;
} else if (rjd_slot_isvalid(desc.depthstencil_target.handle)) {
struct rjd_gfx_texture_metal* texture_metal = rjd_slotmap_get(context_metal->slotmap_textures, desc.render_target.handle);
id<MTLTexture> target = texture_metal->texture;
source_format = target.pixelFormat;
}
MTLPixelFormat depth_format = MTLPixelFormatInvalid;
MTLPixelFormat stencil_format = MTLPixelFormatInvalid;
if (source_format != MTLPixelFormatInvalid) {
if (rjd_gfx_mtlformat_is_depth(source_format)) {
depth_format = source_format;
}
if (rjd_gfx_mtlformat_is_stencil(source_format)) {
stencil_format = source_format;
}
}
desc_metal.depthAttachmentPixelFormat = depth_format;
desc_metal.stencilAttachmentPixelFormat = stencil_format;
}
if (rjd_slot_isvalid(desc.shader_vertex.handle)) {
struct rjd_gfx_shader_metal* shader = rjd_slotmap_get(context_metal->slotmap_shaders, desc.shader_vertex.handle);
desc_metal.vertexFunction = shader->function;
}
if (rjd_slot_isvalid(desc.shader_pixel.handle)) {
struct rjd_gfx_shader_metal* shader = rjd_slotmap_get(context_metal->slotmap_shaders, desc.shader_pixel.handle);
desc_metal.fragmentFunction = shader->function;
}
// vertex format
{
MTLVertexDescriptor* vertex_descriptor = [[MTLVertexDescriptor alloc] init];
// shader struct input definitions
for (size_t i = 0; i < desc.count_vertex_attributes; ++i) {
const struct rjd_gfx_vertex_format_attribute* attribute = desc.vertex_attributes + i;
uint32_t attribute_index = attribute->attribute_index;
uint32_t shader_slot = attribute->shader_slot_metal;
MTLVertexFormat format = rjd_gfx_vertex_format_type_to_metal(attribute->type);
MTLVertexStepFunction step_function = rjd_gfx_vertex_format_step_to_metal(attribute->step);
vertex_descriptor.attributes[attribute_index].format = format;
vertex_descriptor.attributes[attribute_index].offset = attribute->offset;
vertex_descriptor.attributes[attribute_index].bufferIndex = attribute->shader_slot_metal;
vertex_descriptor.layouts[shader_slot].stride = attribute->stride;
vertex_descriptor.layouts[shader_slot].stepRate = attribute->step_rate;
vertex_descriptor.layouts[shader_slot].stepFunction = step_function;
}
desc_metal.vertexDescriptor = vertex_descriptor;
}
NSError *error = NULL;
pipeline_state = [context_metal->device newRenderPipelineStateWithDescriptor:desc_metal error:&error];
if (pipeline_state == nil) {
RJD_GFX_LOG_ERROR("Failed to create pipeline state: %s", error.localizedDescription.UTF8String);
return RJD_RESULT("Failed to create pipeline state");
}
}
id<MTLDepthStencilState> depth_stencil_state = nil;
{
MTLDepthStencilDescriptor *depth_state_desc = [[MTLDepthStencilDescriptor alloc] init];
depth_state_desc.depthWriteEnabled = desc.depth_write_enabled;
depth_state_desc.depthCompareFunction = rjd_gfx_depth_compare_to_metal(desc.depth_compare);
depth_stencil_state = [context_metal->device newDepthStencilStateWithDescriptor:depth_state_desc];
if (depth_stencil_state == nil) {
RJD_GFX_LOG_ERROR("Failed to create depth stencil state");
return RJD_RESULT("Failed to create depth stencil state");
}
}
struct rjd_gfx_pipeline_state_metal state = {
.state_render = pipeline_state,
.state_depthstencil = depth_stencil_state,
.cull_mode = desc.cull_mode,
.winding_order = desc.winding_order,
};
rjd_slotmap_insert(context_metal->slotmap_pipeline_states, state, &out->handle);
return RJD_RESULT_OK();
}
void rjd_gfx_pipeline_state_destroy(struct rjd_gfx_context* context, struct rjd_gfx_pipeline_state* pipeline_state)
{
RJD_ASSERT(pipeline_state);
RJD_ASSERT(context);
RJD_ASSERT(rjd_slot_isvalid(pipeline_state->handle));
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
rjd_gfx_pipeline_state_destroy_metal(context_metal, pipeline_state->handle);
}
struct rjd_result rjd_gfx_mesh_create_vertexed(struct rjd_gfx_context* context, struct rjd_gfx_mesh* out, struct rjd_gfx_mesh_vertexed_desc desc)
{
RJD_ASSERT(out);
RJD_ASSERT(context);
if (desc.count_buffers == 0) {
return RJD_RESULT("Must have more than 0 vertex buffers to create a mesh");
}
struct rjd_result result = RJD_RESULT_OK();
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
struct rjd_gfx_mesh_shader_buffer_metal* shader_buffers = rjd_array_alloc(struct rjd_gfx_mesh_shader_buffer_metal, desc.count_buffers, context_metal->allocator);
rjd_array_resize(shader_buffers, desc.count_buffers);
for (uint32_t i = 0; i < desc.count_buffers; ++i) {
id<MTLBuffer> buffer = nil;
struct rjd_gfx_mesh_buffer_desc* desc_buffer = desc.buffers + i;
if (desc_buffer->usage_flags & RJD_GFX_MESH_BUFFER_USAGE_VERTEX_CONSTANT ||
desc_buffer->usage_flags & RJD_GFX_MESH_BUFFER_USAGE_PIXEL_CONSTANT) {
NSUInteger length = desc_buffer->common.constant.capacity;
buffer = [context_metal->device newBufferWithLength:length options:MTLResourceStorageModeShared];
} else if (desc_buffer->usage_flags & RJD_GFX_MESH_BUFFER_USAGE_VERTEX) {
const void* bytes = desc_buffer->common.vertex.data;
NSUInteger length = desc_buffer->common.vertex.length;
buffer = [context_metal->device newBufferWithBytes:bytes length:length options:MTLResourceStorageModeManaged];
NSRange range = NSMakeRange(0, length);
[buffer didModifyRange:range];
} else {
result = RJD_RESULT("You must specify the usage_flags for each buffer");
break;
}
if (rjd_result_isok(result) && buffer == nil) {
for (uint32_t k = 0; k < i; ++k) {
shader_buffers[k].buffer = nil;
}
result = RJD_RESULT("Not enough memory available to create buffers");
break;
}
struct rjd_gfx_mesh_shader_buffer_metal mesh_buffer = {
.buffer = buffer,
.usage_flags = desc.buffers[i].usage_flags,
.shader_slot = desc.buffers[i].shader_slot_metal,
};
shader_buffers[i] = mesh_buffer;
}
if (rjd_result_isok(result)) {
struct rjd_gfx_mesh_metal mesh_metal = {
.shader_buffers = shader_buffers,
.count_vertices = desc.count_vertices,
.primitive = desc.primitive,
};
rjd_slotmap_insert(context_metal->slotmap_meshes, mesh_metal, &out->handle);
}
return result;
}
struct rjd_result rjd_gfx_mesh_modify(struct rjd_gfx_context* context, struct rjd_gfx_command_buffer* cmd_buffer, struct rjd_gfx_mesh* mesh, uint32_t buffer_index, uint32_t offset, const void* data, uint32_t length)
{
RJD_ASSERT(context);
RJD_ASSERT(mesh);
RJD_ASSERT(rjd_slot_isvalid(mesh->handle));
RJD_UNUSED_PARAM(cmd_buffer);
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
struct rjd_gfx_mesh_metal* mesh_metal = rjd_slotmap_get(context_metal->slotmap_meshes, mesh->handle);
struct rjd_gfx_mesh_shader_buffer_metal* buffer = NULL;
for (uint32_t i = 0; i < rjd_array_count(mesh_metal->shader_buffers); ++i) {
if (i == buffer_index) {
buffer = mesh_metal->shader_buffers + i;
}
}
if (buffer == NULL) {
return RJD_RESULT("Mesh buffer with specified buffer not found in mesh.");
}
if (buffer->buffer.length < offset + length) {
return RJD_RESULT("Not enough storage available. You must increase the allocated size when the mesh is created.");
}
if (buffer->buffer.contents == NULL) {
return RJD_RESULT("This is an immutable buffer. Modifying its contents is not allowed.");
}
memcpy((uint8_t*)buffer->buffer.contents + offset, data, length);
if (buffer->usage_flags & RJD_GFX_MESH_BUFFER_USAGE_VERTEX) {
NSRange range = NSMakeRange(offset, length);
[buffer->buffer didModifyRange:range];
}
return RJD_RESULT_OK();
}
void rjd_gfx_mesh_destroy(struct rjd_gfx_context* context, struct rjd_gfx_mesh* mesh)
{
RJD_ASSERT(mesh);
RJD_ASSERT(context);
RJD_ASSERT(rjd_slot_isvalid(mesh->handle));
struct rjd_gfx_context_metal* context_metal = (struct rjd_gfx_context_metal*)context;
rjd_gfx_mesh_destroy_metal(context_metal, mesh->handle);
}
////////////////////////////////////////////////////////////////////////////////
// private interface
static MTLPixelFormat rjd_gfx_format_color_to_metal(enum rjd_gfx_format format)
{
switch (format)
{
case RJD_GFX_FORMAT_COLOR_U8_RGBA: return MTLPixelFormatRGBA8Uint;
case RJD_GFX_FORMAT_COLOR_U8_BGRA_NORM: return MTLPixelFormatBGRA8Unorm;
case RJD_GFX_FORMAT_COLOR_U8_BGRA_NORM_SRGB: return MTLPixelFormatBGRA8Unorm_sRGB;
case RJD_GFX_FORMAT_DEPTHSTENCIL_F32_D32: return MTLPixelFormatDepth32Float;
case RJD_GFX_FORMAT_DEPTHSTENCIL_U32_D24_S8: return MTLPixelFormatDepth24Unorm_Stencil8;
case RJD_GFX_FORMAT_COUNT: break;
}
RJD_ASSERTFAIL("Unhandled format %d.", format);
return MTLPixelFormatInvalid;
}
static MTLClearColor rjd_gfx_format_value_to_clearcolor(struct rjd_gfx_format_value value)
{
double red = 0;
double green = 0;
double blue = 0;
double alpha = 1;
switch (value.type)
{
case RJD_GFX_FORMAT_COLOR_U8_RGBA:
red = value.color_u8_rgba[0] / (double)UCHAR_MAX;
green = value.color_u8_rgba[1] / (double)UCHAR_MAX;
blue = value.color_u8_rgba[2] / (double)UCHAR_MAX;
alpha = value.color_u8_rgba[3] / (double)UCHAR_MAX;
break;
case RJD_GFX_FORMAT_COLOR_U8_BGRA_NORM:
case RJD_GFX_FORMAT_COLOR_U8_BGRA_NORM_SRGB:
blue = value.color_u8_rgba[2] / (double)UCHAR_MAX;
green = value.color_u8_rgba[1] / (double)UCHAR_MAX;
red = value.color_u8_rgba[0] / (double)UCHAR_MAX;
alpha = value.color_u8_rgba[3] / (double)UCHAR_MAX;
break;
case RJD_GFX_FORMAT_DEPTHSTENCIL_F32_D32:
case RJD_GFX_FORMAT_DEPTHSTENCIL_U32_D24_S8:
RJD_ASSERTFAIL("depth stencil values shouldn't be passed to this function");
break;
default:
RJD_ASSERTFAIL("Unhandled format %d.", value.type);
break;
}
return MTLClearColorMake(red, green, blue, alpha);
}
static bool rjd_gfx_format_isbackbuffercompatible(enum rjd_gfx_format format)
{
switch (format)
{
case RJD_GFX_FORMAT_COLOR_U8_BGRA_NORM: return true;
case RJD_GFX_FORMAT_COLOR_U8_BGRA_NORM_SRGB: return true;
default: return false;
}
}
static MTLVertexFormat rjd_gfx_vertex_format_type_to_metal(enum rjd_gfx_vertex_format_type type)
{
switch (type)
{
case RJD_GFX_VERTEX_FORMAT_TYPE_FLOAT1: return MTLVertexFormatFloat;
case RJD_GFX_VERTEX_FORMAT_TYPE_FLOAT2: return MTLVertexFormatFloat2;
case RJD_GFX_VERTEX_FORMAT_TYPE_FLOAT3: return MTLVertexFormatFloat3;
case RJD_GFX_VERTEX_FORMAT_TYPE_FLOAT4: return MTLVertexFormatFloat4;
case RJD_GFX_VERTEX_FORMAT_TYPE_COUNT: break;
}
RJD_ASSERTFAIL("unhandled type: %d", type);
return MTLVertexFormatInvalid;
}
static MTLVertexStepFunction rjd_gfx_vertex_format_step_to_metal(enum rjd_gfx_vertex_format_step step)
{
switch (step)
{
case RJD_GFX_VERTEX_FORMAT_STEP_VERTEX: return MTLVertexStepFunctionPerVertex;
case RJD_GFX_VERTEX_FORMAT_STEP_INSTANCE: return MTLVertexStepFunctionPerInstance;
case RJD_GFX_VERTEX_FORMAT_STEP_CONSTANT: return MTLVertexStepFunctionConstant;
}
RJD_ASSERTFAIL("unhandled type: %d", step);
return MTLVertexStepFunctionPerVertex;
}
static MTLIndexType rjd_gfx_index_type_to_metal(enum rjd_gfx_index_type type)
{
switch (type)
{
case RJD_GFX_INDEX_TYPE_UINT16: return MTLIndexTypeUInt16;
case RJD_GFX_INDEX_TYPE_UINT32: return MTLIndexTypeUInt32;
}
RJD_ASSERTFAIL("Unknown index type %d", type);
return MTLIndexTypeUInt16;
}
static MTLPrimitiveType rjd_gfx_primitive_type_to_metal(enum rjd_gfx_primitive_type type)
{
switch (type)
{
case RJD_GFX_PRIMITIVE_TYPE_TRIANGLES: return MTLPrimitiveTypeTriangle;
}
RJD_ASSERTFAIL("Unknown primitive type %d", type);
return MTLPrimitiveTypeTriangle;
}
static MTLCompareFunction rjd_gfx_depth_compare_to_metal(enum rjd_gfx_depth_compare func)
{
switch (func)
{
case RJD_GFX_DEPTH_COMPARE_ALWAYS_SUCCEED: return MTLCompareFunctionAlways;
case RJD_GFX_DEPTH_COMPARE_ALWAYS_FAIL: return MTLCompareFunctionNever;
case RJD_GFX_DEPTH_COMPARE_LESS: return MTLCompareFunctionLess;
case RJD_GFX_DEPTH_COMPARE_LESSEQUAL: return MTLCompareFunctionLessEqual;
case RJD_GFX_DEPTH_COMPARE_GREATER: return MTLCompareFunctionGreater;
case RJD_GFX_DEPTH_COMPARE_GREATEREQUAL: return MTLCompareFunctionGreaterEqual;
case RJD_GFX_DEPTH_COMPARE_EQUAL: return MTLCompareFunctionEqual;
case RJD_GFX_DEPTH_COMPARE_NOTEQUAL: return MTLCompareFunctionNotEqual;
}
RJD_ASSERTFAIL("Unknown compare func %d", func);