-
Notifications
You must be signed in to change notification settings - Fork 72
/
mtlpp.mm
4767 lines (4059 loc) · 160 KB
/
mtlpp.mm
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
/*
* Copyright 2016-2017 Nikolay Aleksiev. All rights reserved.
* License: https://github.com/naleksiev/mtlpp/blob/master/LICENSE
*/
#include "mtlpp.hpp"
//////////////////////////////////////
// FILE: argument.mm
//////////////////////////////////////
/*
* Copyright 2016-2017 Nikolay Aleksiev. All rights reserved.
* License: https://github.com/naleksiev/mtlpp/blob/master/LICENSE
*/
// #include "argument.hpp"
#include <Metal/MTLArgument.h>
namespace mtlpp
{
StructMember::StructMember() :
ns::Object(ns::Handle{ (__bridge void*)[[MTLStructMember alloc] init] })
{
}
ns::String StructMember::GetName() const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge MTLStructMember*)m_ptr name] };
}
uint32_t StructMember::GetOffset() const
{
Validate();
return uint32_t([(__bridge MTLStructMember*)m_ptr offset]);
}
DataType StructMember::GetDataType() const
{
Validate();
return DataType([(__bridge MTLStructMember*)m_ptr dataType]);
}
StructType StructMember::GetStructType() const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge MTLStructMember*)m_ptr structType] };
}
ArrayType StructMember::GetArrayType() const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge MTLStructMember*)m_ptr arrayType] };
}
StructType::StructType() :
ns::Object(ns::Handle{ (__bridge void*)[[MTLStructType alloc] init] })
{
}
const ns::Array<StructMember> StructType::GetMembers() const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge MTLStructType*)m_ptr members] };
}
StructMember StructType::GetMember(const ns::String& name) const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge MTLStructType*)m_ptr memberByName:(__bridge NSString*)name.GetPtr()] };
}
ArrayType::ArrayType() :
ns::Object(ns::Handle{ (__bridge void*)[[MTLArrayType alloc] init] })
{
}
uint32_t ArrayType::GetArrayLength() const
{
Validate();
return uint32_t([(__bridge MTLArrayType*)m_ptr arrayLength]);
}
DataType ArrayType::GetElementType() const
{
Validate();
return DataType([(__bridge MTLArrayType*)m_ptr elementType]);
}
uint32_t ArrayType::GetStride() const
{
Validate();
return uint32_t([(__bridge MTLArrayType*)m_ptr stride]);
}
StructType ArrayType::GetElementStructType() const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge MTLArrayType*)m_ptr elementStructType] };
}
ArrayType ArrayType::GetElementArrayType() const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge MTLArrayType*)m_ptr elementArrayType] };
}
Argument::Argument() :
ns::Object(ns::Handle{ (__bridge void*)[[MTLArgument alloc] init] })
{
}
ns::String Argument::GetName() const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge MTLArgument*)m_ptr name] };
}
ArgumentType Argument::GetType() const
{
Validate();
return ArgumentType([(__bridge MTLArgument*)m_ptr type]);
}
ArgumentAccess Argument::GetAccess() const
{
Validate();
return ArgumentAccess([(__bridge MTLArgument*)m_ptr access]);
}
uint32_t Argument::GetIndex() const
{
Validate();
return uint32_t([(__bridge MTLArgument*)m_ptr index]);
}
bool Argument::IsActive() const
{
Validate();
return [(__bridge MTLArgument*)m_ptr isActive];
}
uint32_t Argument::GetBufferAlignment() const
{
Validate();
return uint32_t([(__bridge MTLArgument*)m_ptr bufferAlignment]);
}
uint32_t Argument::GetBufferDataSize() const
{
Validate();
return uint32_t([(__bridge MTLArgument*)m_ptr bufferDataSize]);
}
DataType Argument::GetBufferDataType() const
{
Validate();
return DataType([(__bridge MTLArgument*)m_ptr bufferDataType]);
}
StructType Argument::GetBufferStructType() const
{
Validate();
return StructType(ns::Handle { (__bridge void*)[(__bridge MTLArgument*)m_ptr bufferStructType] });
}
uint32_t Argument::GetThreadgroupMemoryAlignment() const
{
Validate();
return uint32_t([(__bridge MTLArgument*)m_ptr threadgroupMemoryAlignment]);
}
uint32_t Argument::GetThreadgroupMemoryDataSize() const
{
Validate();
return uint32_t([(__bridge MTLArgument*)m_ptr threadgroupMemoryDataSize]);
}
TextureType Argument::GetTextureType() const
{
Validate();
return TextureType([(__bridge MTLArgument*)m_ptr textureType]);
}
DataType Argument::GetTextureDataType() const
{
Validate();
return DataType([(__bridge MTLArgument*)m_ptr textureDataType]);
}
bool Argument::IsDepthTexture() const
{
Validate();
#if MTLPP_IS_AVAILABLE(10_12, 10_0)
return [(__bridge MTLArgument*)m_ptr isDepthTexture];
#else
return false;
#endif
}
}
//////////////////////////////////////
// FILE: blit_command_encoder.mm
//////////////////////////////////////
/*
* Copyright 2016-2017 Nikolay Aleksiev. All rights reserved.
* License: https://github.com/naleksiev/mtlpp/blob/master/LICENSE
*/
// #include "blit_command_encoder.hpp"
#include <Metal/MTLBlitCommandEncoder.h>
namespace mtlpp
{
void BlitCommandEncoder::Synchronize(const Resource& resource)
{
Validate();
#if MTLPP_IS_AVAILABLE_MAC(10_11)
[(__bridge id<MTLBlitCommandEncoder>)m_ptr
synchronizeResource:(__bridge id<MTLResource>)resource.GetPtr()];
#endif
}
void BlitCommandEncoder::Synchronize(const Texture& texture, uint32_t slice, uint32_t level)
{
Validate();
#if MTLPP_IS_AVAILABLE_MAC(10_11)
[(__bridge id<MTLBlitCommandEncoder>)m_ptr
synchronizeTexture:(__bridge id<MTLTexture>)texture.GetPtr()
slice:slice
level:level];
#endif
}
void BlitCommandEncoder::Copy(const Texture& sourceTexture, uint32_t sourceSlice, uint32_t sourceLevel, const Origin& sourceOrigin, const Size& sourceSize, const Texture& destinationTexture, uint32_t destinationSlice, uint32_t destinationLevel, const Origin& destinationOrigin)
{
Validate();
[(__bridge id<MTLBlitCommandEncoder>)m_ptr
copyFromTexture:(__bridge id<MTLTexture>)sourceTexture.GetPtr()
sourceSlice:sourceSlice
sourceLevel:sourceLevel
sourceOrigin:MTLOriginMake(sourceOrigin.X, sourceOrigin.Y, sourceOrigin.Z)
sourceSize:MTLSizeMake(sourceSize.Width, sourceSize.Height, sourceSize.Depth)
toTexture:(__bridge id<MTLTexture>)destinationTexture.GetPtr()
destinationSlice:destinationSlice
destinationLevel:destinationLevel
destinationOrigin:MTLOriginMake(destinationOrigin.X, destinationOrigin.Y, destinationOrigin.Z)];
}
void BlitCommandEncoder::Copy(const Buffer& sourceBuffer, uint32_t sourceOffset, uint32_t sourceBytesPerRow, uint32_t sourceBytesPerImage, const Size& sourceSize, const Texture& destinationTexture, uint32_t destinationSlice, uint32_t destinationLevel, const Origin& destinationOrigin)
{
Validate();
[(__bridge id<MTLBlitCommandEncoder>)m_ptr
copyFromBuffer:(__bridge id<MTLBuffer>)sourceBuffer.GetPtr()
sourceOffset:sourceOffset
sourceBytesPerRow:sourceBytesPerRow
sourceBytesPerImage:sourceBytesPerImage
sourceSize:MTLSizeMake(sourceSize.Width, sourceSize.Height, sourceSize.Depth)
toTexture:(__bridge id<MTLTexture>)destinationTexture.GetPtr()
destinationSlice:destinationSlice
destinationLevel:destinationLevel
destinationOrigin:MTLOriginMake(destinationOrigin.X, destinationOrigin.Y, destinationOrigin.Z)];
}
void BlitCommandEncoder::Copy(const Buffer& sourceBuffer, uint32_t sourceOffset, uint32_t sourceBytesPerRow, uint32_t sourceBytesPerImage, const Size& sourceSize, const Texture& destinationTexture, uint32_t destinationSlice, uint32_t destinationLevel, const Origin& destinationOrigin, BlitOption options)
{
Validate();
[(__bridge id<MTLBlitCommandEncoder>)m_ptr
copyFromBuffer:(__bridge id<MTLBuffer>)sourceBuffer.GetPtr()
sourceOffset:sourceOffset
sourceBytesPerRow:sourceBytesPerRow
sourceBytesPerImage:sourceBytesPerImage
sourceSize:MTLSizeMake(sourceSize.Width, sourceSize.Height, sourceSize.Depth)
toTexture:(__bridge id<MTLTexture>)destinationTexture.GetPtr()
destinationSlice:destinationSlice
destinationLevel:destinationLevel
destinationOrigin:MTLOriginMake(destinationOrigin.X, destinationOrigin.Y, destinationOrigin.Z)
options:MTLBlitOption(options)];
}
void BlitCommandEncoder::Copy(const Texture& sourceTexture, uint32_t sourceSlice, uint32_t sourceLevel, const Origin& sourceOrigin, const Size& sourceSize, const Buffer& destinationBuffer, uint32_t destinationOffset, uint32_t destinationBytesPerRow, uint32_t destinationBytesPerImage)
{
Validate();
[(__bridge id<MTLBlitCommandEncoder>)m_ptr
copyFromTexture:(__bridge id<MTLTexture>)sourceTexture.GetPtr()
sourceSlice:sourceSlice
sourceLevel:sourceLevel
sourceOrigin:MTLOriginMake(sourceOrigin.X, sourceOrigin.Y, sourceOrigin.Z)
sourceSize:MTLSizeMake(sourceSize.Width, sourceSize.Height, sourceSize.Depth)
toBuffer:(__bridge id<MTLBuffer>)destinationBuffer.GetPtr()
destinationOffset:destinationOffset
destinationBytesPerRow:destinationBytesPerRow
destinationBytesPerImage:destinationBytesPerImage];
}
void BlitCommandEncoder::Copy(const Texture& sourceTexture, uint32_t sourceSlice, uint32_t sourceLevel, const Origin& sourceOrigin, const Size& sourceSize, const Buffer& destinationBuffer, uint32_t destinationOffset, uint32_t destinationBytesPerRow, uint32_t destinationBytesPerImage, BlitOption options)
{
Validate();
[(__bridge id<MTLBlitCommandEncoder>)m_ptr
copyFromTexture:(__bridge id<MTLTexture>)sourceTexture.GetPtr()
sourceSlice:sourceSlice
sourceLevel:sourceLevel
sourceOrigin:MTLOriginMake(sourceOrigin.X, sourceOrigin.Y, sourceOrigin.Z)
sourceSize:MTLSizeMake(sourceSize.Width, sourceSize.Height, sourceSize.Depth)
toBuffer:(__bridge id<MTLBuffer>)destinationBuffer.GetPtr()
destinationOffset:destinationOffset
destinationBytesPerRow:destinationBytesPerRow
destinationBytesPerImage:destinationBytesPerImage
options:MTLBlitOption(options)];
}
void BlitCommandEncoder::Copy(const Buffer& sourceBuffer, uint32_t sourceOffset, const Buffer& destinationBuffer, uint32_t destinationOffset, uint32_t size)
{
Validate();
[(__bridge id<MTLBlitCommandEncoder>)m_ptr
copyFromBuffer:(__bridge id<MTLBuffer>)sourceBuffer.GetPtr()
sourceOffset:sourceOffset
toBuffer:(__bridge id<MTLBuffer>)destinationBuffer.GetPtr()
destinationOffset:destinationOffset
size:size];
}
void BlitCommandEncoder::GenerateMipmaps(const Texture& texture)
{
Validate();
[(__bridge id<MTLBlitCommandEncoder>)m_ptr
generateMipmapsForTexture:(__bridge id<MTLTexture>)texture.GetPtr()];
}
void BlitCommandEncoder::Fill(const Buffer& buffer, const ns::Range& range, uint8_t value)
{
Validate();
[(__bridge id<MTLBlitCommandEncoder>)m_ptr
fillBuffer:(__bridge id<MTLBuffer>)buffer.GetPtr()
range:NSMakeRange(range.Location, range.Length)
value:value];
}
void BlitCommandEncoder::UpdateFence(const Fence& fence)
{
Validate();
#if MTLPP_IS_AVAILABLE_IOS(10_0)
[(__bridge id<MTLBlitCommandEncoder>)m_ptr
updateFence:(__bridge id<MTLFence>)fence.GetPtr()];
#endif
}
void BlitCommandEncoder::WaitForFence(const Fence& fence)
{
#if MTLPP_IS_AVAILABLE_IOS(10_0)
[(__bridge id<MTLBlitCommandEncoder>)m_ptr
waitForFence:(__bridge id<MTLFence>)fence.GetPtr()];
#endif
}
}
//////////////////////////////////////
// FILE: buffer.mm
//////////////////////////////////////
/*
* Copyright 2016-2017 Nikolay Aleksiev. All rights reserved.
* License: https://github.com/naleksiev/mtlpp/blob/master/LICENSE
*/
// #include "buffer.hpp"
// #include "texture.hpp"
#include <Metal/MTLBuffer.h>
namespace mtlpp
{
uint32_t Buffer::GetLength() const
{
Validate();
return uint32_t([(__bridge id<MTLBuffer>)m_ptr length]);
}
void* Buffer::GetContents()
{
Validate();
return [(__bridge id<MTLBuffer>)m_ptr contents];
}
void Buffer::DidModify(const ns::Range& range)
{
Validate();
#if MTLPP_IS_AVAILABLE_MAC(10_11)
[(__bridge id<MTLBuffer>)m_ptr didModifyRange:NSMakeRange(range.Location, range.Length)];
#endif
}
Texture Buffer::NewTexture(const TextureDescriptor& descriptor, uint32_t offset, uint32_t bytesPerRow)
{
Validate();
#if MTLPP_IS_AVAILABLE_IOS(8_0)
MTLTextureDescriptor* mtlTextureDescriptor = (__bridge MTLTextureDescriptor*)descriptor.GetPtr();
return ns::Handle{ (__bridge void*)[(__bridge id<MTLBuffer>)m_ptr newTextureWithDescriptor:mtlTextureDescriptor offset:offset bytesPerRow:bytesPerRow] };
#else
return ns::Handle{ nullptr };
#endif
}
void Buffer::AddDebugMarker(const ns::String& marker, const ns::Range& range)
{
#if MTLPP_IS_AVAILABLE(10_12, 10_0)
[(__bridge id<MTLBuffer>)m_ptr addDebugMarker:(__bridge NSString*)marker.GetPtr() range:NSMakeRange(range.Location, range.Length)];
#endif
}
void Buffer::RemoveAllDebugMarkers()
{
#if MTLPP_IS_AVAILABLE(10_12, 10_0)
[(__bridge id<MTLBuffer>)m_ptr removeAllDebugMarkers];
#endif
}
}
//////////////////////////////////////
// FILE: command_buffer.mm
//////////////////////////////////////
/*
* Copyright 2016-2017 Nikolay Aleksiev. All rights reserved.
* License: https://github.com/naleksiev/mtlpp/blob/master/LICENSE
*/
// #include "command_buffer.hpp"
// #include "command_queue.hpp"
// #include "drawable.hpp"
// #include "blit_command_encoder.hpp"
// #include "render_command_encoder.hpp"
// #include "compute_command_encoder.hpp"
// #include "parallel_render_command_encoder.hpp"
// #include "render_pass.hpp"
#include <Metal/MTLCommandBuffer.h>
namespace mtlpp
{
Device CommandBuffer::GetDevice() const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge id<MTLCommandBuffer>)m_ptr device] };
}
CommandQueue CommandBuffer::GetCommandQueue() const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge id<MTLCommandBuffer>)m_ptr commandQueue] };
}
bool CommandBuffer::GetRetainedReferences() const
{
Validate();
return [(__bridge id<MTLCommandBuffer>)m_ptr retainedReferences];
}
ns::String CommandBuffer::GetLabel() const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge id<MTLCommandBuffer>)m_ptr label] };
}
CommandBufferStatus CommandBuffer::GetStatus() const
{
Validate();
return CommandBufferStatus([(__bridge id<MTLCommandBuffer>)m_ptr status]);
}
ns::Error CommandBuffer::GetError() const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge id<MTLCommandBuffer>)m_ptr error] };
}
double CommandBuffer::GetKernelStartTime() const
{
Validate();
#if MTLPP_IS_AVAILABLE_IOS(10_3)
return [(__bridge id<MTLCommandBuffer>)m_ptr kernelStartTime];
#else
return 0.0;
#endif
}
double CommandBuffer::GetKernelEndTime() const
{
Validate();
#if MTLPP_IS_AVAILABLE_IOS(10_3)
return [(__bridge id<MTLCommandBuffer>)m_ptr kernelEndTime];
#else
return 0.0;
#endif
}
double CommandBuffer::GetGpuStartTime() const
{
Validate();
#if MTLPP_IS_AVAILABLE_IOS(10_3)
return [(__bridge id<MTLCommandBuffer>)m_ptr GPUStartTime];
#else
return 0.0;
#endif
}
double CommandBuffer::GetGpuEndTime() const
{
Validate();
#if MTLPP_IS_AVAILABLE_IOS(10_3)
return [(__bridge id<MTLCommandBuffer>)m_ptr GPUEndTime];
#else
return 0.0;
#endif
}
void CommandBuffer::SetLabel(const ns::String& label)
{
Validate();
[(__bridge id<MTLCommandBuffer>)m_ptr setLabel:(__bridge NSString*)label.GetPtr()];
}
void CommandBuffer::Enqueue()
{
Validate();
[(__bridge id<MTLCommandBuffer>)m_ptr enqueue];
}
void CommandBuffer::Commit()
{
Validate();
[(__bridge id<MTLCommandBuffer>)m_ptr commit];
}
void CommandBuffer::AddScheduledHandler(std::function<void(const CommandBuffer&)> handler)
{
Validate();
[(__bridge id<MTLCommandBuffer>)m_ptr addScheduledHandler:^(id <MTLCommandBuffer> mtlCommandBuffer){
CommandBuffer commandBuffer(ns::Handle{ (__bridge void*)mtlCommandBuffer });
handler(commandBuffer);
}];
}
void CommandBuffer::AddCompletedHandler(std::function<void(const CommandBuffer&)> handler)
{
Validate();
[(__bridge id<MTLCommandBuffer>)m_ptr addCompletedHandler:^(id <MTLCommandBuffer> mtlCommandBuffer){
CommandBuffer commandBuffer(ns::Handle{ (__bridge void*)mtlCommandBuffer });
handler(commandBuffer);
}];
}
void CommandBuffer::Present(const Drawable& drawable)
{
Validate();
[(__bridge id<MTLCommandBuffer>)m_ptr presentDrawable:(__bridge id<MTLDrawable>)drawable.GetPtr()];
}
void CommandBuffer::PresentAtTime(const Drawable& drawable, double presentationTime)
{
Validate();
[(__bridge id<MTLCommandBuffer>)m_ptr presentDrawable:(__bridge id<MTLDrawable>)drawable.GetPtr() atTime:presentationTime];
}
void CommandBuffer::PresentAfterMinimumDuration(const Drawable& drawable, double duration)
{
Validate();
#if MTLPP_IS_AVAILABLE_IOS(10_3)
[(__bridge id<MTLCommandBuffer>)m_ptr presentDrawable:(__bridge id<MTLDrawable>)drawable.GetPtr() afterMinimumDuration:duration];
#endif
}
void CommandBuffer::WaitUntilScheduled()
{
Validate();
[(__bridge id<MTLCommandBuffer>)m_ptr waitUntilScheduled];
}
void CommandBuffer::WaitUntilCompleted()
{
Validate();
[(__bridge id<MTLCommandBuffer>)m_ptr waitUntilCompleted];
}
BlitCommandEncoder CommandBuffer::BlitCommandEncoder()
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge id<MTLCommandBuffer>)m_ptr blitCommandEncoder] };
}
RenderCommandEncoder CommandBuffer::RenderCommandEncoder(const RenderPassDescriptor& renderPassDescriptor)
{
Validate();
MTLRenderPassDescriptor* mtlRenderPassDescriptor = (__bridge MTLRenderPassDescriptor*)renderPassDescriptor.GetPtr();
return ns::Handle{ (__bridge void*)[(__bridge id<MTLCommandBuffer>)m_ptr renderCommandEncoderWithDescriptor:mtlRenderPassDescriptor] };
}
ComputeCommandEncoder CommandBuffer::ComputeCommandEncoder()
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge id<MTLCommandBuffer>)m_ptr computeCommandEncoder] };
}
ParallelRenderCommandEncoder CommandBuffer::ParallelRenderCommandEncoder(const RenderPassDescriptor& renderPassDescriptor)
{
Validate();
MTLRenderPassDescriptor* mtlRenderPassDescriptor = (__bridge MTLRenderPassDescriptor*)renderPassDescriptor.GetPtr();
return ns::Handle{ (__bridge void*)[(__bridge id<MTLCommandBuffer>)m_ptr parallelRenderCommandEncoderWithDescriptor:mtlRenderPassDescriptor] };
}
}
//////////////////////////////////////
// FILE: command_encoder.mm
//////////////////////////////////////
/*
* Copyright 2016-2017 Nikolay Aleksiev. All rights reserved.
* License: https://github.com/naleksiev/mtlpp/blob/master/LICENSE
*/
// #include "command_encoder.hpp"
// #include "device.hpp"
#include <Metal/MTLCommandEncoder.h>
namespace mtlpp
{
Device CommandEncoder::GetDevice() const
{
Validate();
return ns::Handle { (__bridge void*)[(__bridge id<MTLCommandEncoder>)m_ptr device] };
}
ns::String CommandEncoder::GetLabel() const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge id<MTLCommandEncoder>)m_ptr label] };
}
void CommandEncoder::SetLabel(const ns::String& label)
{
Validate();
[(__bridge id<MTLCommandEncoder>)m_ptr setLabel:(__bridge NSString*)label.GetPtr()];
}
void CommandEncoder::EndEncoding()
{
Validate();
[(__bridge id<MTLCommandEncoder>)m_ptr endEncoding];
}
void CommandEncoder::InsertDebugSignpost(const ns::String& string)
{
Validate();
[(__bridge id<MTLCommandEncoder>)m_ptr insertDebugSignpost:(__bridge NSString*)string.GetPtr()];
}
void CommandEncoder::PushDebugGroup(const ns::String& string)
{
Validate();
[(__bridge id<MTLCommandEncoder>)m_ptr pushDebugGroup:(__bridge NSString*)string.GetPtr()];
}
void CommandEncoder::PopDebugGroup()
{
Validate();
[(__bridge id<MTLCommandEncoder>)m_ptr popDebugGroup];
}
}
//////////////////////////////////////
// FILE: command_queue.mm
//////////////////////////////////////
/*
* Copyright 2016-2017 Nikolay Aleksiev. All rights reserved.
* License: https://github.com/naleksiev/mtlpp/blob/master/LICENSE
*/
// #include "command_queue.hpp"
// #include "command_buffer.hpp"
// #include "device.hpp"
#include <Metal/MTLCommandQueue.h>
namespace mtlpp
{
ns::String CommandQueue::GetLabel() const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge id<MTLCommandQueue>)m_ptr label] };
}
Device CommandQueue::GetDevice() const
{
Validate();
return ns::Handle { (__bridge void*)[(__bridge id<MTLCommandQueue>)m_ptr device] };
}
void CommandQueue::SetLabel(const ns::String& label)
{
Validate();
[(__bridge id<MTLCommandQueue>)m_ptr setLabel:(__bridge NSString*)label.GetPtr()];
}
CommandBuffer CommandQueue::CommandBufferWithUnretainedReferences()
{
Validate();
return ns::Handle { (__bridge void*)[(__bridge id<MTLCommandQueue>)m_ptr commandBufferWithUnretainedReferences] };
}
CommandBuffer CommandQueue::CommandBuffer()
{
Validate();
return ns::Handle { (__bridge void*)[(__bridge id<MTLCommandQueue>)m_ptr commandBuffer] };
}
void CommandQueue::InsertDebugCaptureBoundary()
{
Validate();
[(__bridge id<MTLCommandQueue>)m_ptr insertDebugCaptureBoundary];
}
}
//////////////////////////////////////
// FILE: compute_command_encoder.mm
//////////////////////////////////////
/*
* Copyright 2016-2017 Nikolay Aleksiev. All rights reserved.
* License: https://github.com/naleksiev/mtlpp/blob/master/LICENSE
*/
// #include "compute_command_encoder.hpp"
// #include "buffer.hpp"
// #include "compute_pipeline.hpp"
// #include "sampler.hpp"
#include <Metal/MTLComputeCommandEncoder.h>
namespace mtlpp
{
void ComputeCommandEncoder::SetComputePipelineState(const ComputePipelineState& state)
{
Validate();
[(__bridge id<MTLComputeCommandEncoder>)m_ptr setComputePipelineState:(__bridge id<MTLComputePipelineState>)state.GetPtr()];
}
void ComputeCommandEncoder::SetBytes(const void* data, uint32_t length, uint32_t index)
{
Validate();
[(__bridge id<MTLComputeCommandEncoder>)m_ptr setBytes:data length:length atIndex:index];
}
void ComputeCommandEncoder::SetBuffer(const Buffer& buffer, uint32_t offset, uint32_t index)
{
Validate();
[(__bridge id<MTLComputeCommandEncoder>)m_ptr setBuffer:(__bridge id<MTLBuffer>)buffer.GetPtr() offset:offset atIndex:index];
}
void ComputeCommandEncoder::SetBufferOffset(uint32_t offset, uint32_t index)
{
Validate();
#if MTLPP_IS_AVAILABLE(10_11, 8_3)
[(__bridge id<MTLComputeCommandEncoder>)m_ptr setBufferOffset:offset atIndex:index];
#endif
}
void ComputeCommandEncoder::SetBuffers(const Buffer* buffers, const uint32_t* offsets, const ns::Range& range)
{
Validate();
const uint32_t maxBuffers = 32;
assert(range.Length <= maxBuffers);
id<MTLBuffer> mtlBuffers[maxBuffers];
NSUInteger nsOffsets[maxBuffers];
for (uint32_t i=0; i<range.Length; i++)
{
mtlBuffers[i] = (__bridge id<MTLBuffer>)buffers[i].GetPtr();
nsOffsets[i] = offsets[i];
}
[(__bridge id<MTLComputeCommandEncoder>)m_ptr setBuffers:mtlBuffers
offsets:nsOffsets
withRange:NSMakeRange(range.Location, range.Length)];
}
void ComputeCommandEncoder::SetTexture(const Texture& texture, uint32_t index)
{
Validate();
[(__bridge id<MTLComputeCommandEncoder>)m_ptr setTexture:(__bridge id<MTLTexture>)texture.GetPtr() atIndex:index];
}
void ComputeCommandEncoder::SetTextures(const Texture* textures, const ns::Range& range)
{
Validate();
const uint32_t maxTextures = 32;
assert(range.Length <= maxTextures);
id<MTLTexture> mtlTextures[maxTextures];
for (uint32_t i=0; i<range.Length; i++)
mtlTextures[i] = (__bridge id<MTLTexture>)textures[i].GetPtr();
[(__bridge id<MTLComputeCommandEncoder>)m_ptr setTextures:mtlTextures
withRange:NSMakeRange(range.Location, range.Length)];
}
void ComputeCommandEncoder::SetSamplerState(const SamplerState& sampler, uint32_t index)
{
Validate();
[(__bridge id<MTLComputeCommandEncoder>)m_ptr setSamplerState:(__bridge id<MTLSamplerState>)sampler.GetPtr() atIndex:index];
}
void ComputeCommandEncoder::SetSamplerStates(const SamplerState* samplers, const ns::Range& range)
{
Validate();
const uint32_t maxStates = 32;
assert(range.Length <= maxStates);
id<MTLSamplerState> mtlStates[maxStates];
for (uint32_t i=0; i<range.Length; i++)
mtlStates[i] = (__bridge id<MTLSamplerState>)samplers[i].GetPtr();
[(__bridge id<MTLComputeCommandEncoder>)m_ptr setSamplerStates:mtlStates
withRange:NSMakeRange(range.Location, range.Length)];
}
void ComputeCommandEncoder::SetSamplerState(const SamplerState& sampler, float lodMinClamp, float lodMaxClamp, uint32_t index)
{
Validate();
[(__bridge id<MTLComputeCommandEncoder>)m_ptr setSamplerState:(__bridge id<MTLSamplerState>)sampler.GetPtr()
lodMinClamp:lodMinClamp
lodMaxClamp:lodMaxClamp
atIndex:index];
}
void ComputeCommandEncoder::SetSamplerStates(const SamplerState* samplers, const float* lodMinClamps, const float* lodMaxClamps, const ns::Range& range)
{
Validate();
const uint32_t maxStates = 32;
assert(range.Length <= maxStates);
id<MTLSamplerState> mtlStates[maxStates];
for (uint32_t i=0; i<range.Length; i++)
mtlStates[i] = (__bridge id<MTLSamplerState>)samplers[i].GetPtr();
[(__bridge id<MTLComputeCommandEncoder>)m_ptr setSamplerStates:mtlStates
lodMinClamps:lodMinClamps
lodMaxClamps:lodMaxClamps
withRange:NSMakeRange(range.Location, range.Length)];
}
void ComputeCommandEncoder::SetThreadgroupMemory(uint32_t length, uint32_t index)
{
Validate();
[(__bridge id<MTLComputeCommandEncoder>)m_ptr setThreadgroupMemoryLength:length atIndex:index];
}
void ComputeCommandEncoder::SetStageInRegion(const Region& region)
{
#if MTLPP_IS_AVAILABLE(10_12, 10_0)
[(__bridge id<MTLComputeCommandEncoder>)m_ptr setStageInRegion:MTLRegionMake3D(region.Origin.X, region.Origin.Y, region.Origin.Z, region.Size.Width, region.Size.Height, region.Size.Depth)];
#endif
}
void ComputeCommandEncoder::DispatchThreadgroups(const Size& threadgroupsPerGrid, const Size& threadsPerThreadgroup)
{
Validate();
MTLSize mtlThreadgroupsPerGrid = MTLSizeMake(threadgroupsPerGrid.Width, threadgroupsPerGrid.Height, threadgroupsPerGrid.Depth);
MTLSize mtlThreadsPerThreadgroup = MTLSizeMake(threadsPerThreadgroup.Width, threadsPerThreadgroup.Height, threadsPerThreadgroup.Depth);
[(__bridge id<MTLComputeCommandEncoder>)m_ptr dispatchThreadgroups:mtlThreadgroupsPerGrid threadsPerThreadgroup:mtlThreadsPerThreadgroup];
}
void ComputeCommandEncoder::DispatchThreadgroupsWithIndirectBuffer(const Buffer& indirectBuffer, uint32_t indirectBufferOffset, const Size& threadsPerThreadgroup)
{
Validate();
MTLSize mtlThreadsPerThreadgroup = MTLSizeMake(threadsPerThreadgroup.Width, threadsPerThreadgroup.Height, threadsPerThreadgroup.Depth);
[(__bridge id<MTLComputeCommandEncoder>)m_ptr dispatchThreadgroupsWithIndirectBuffer:(__bridge id<MTLBuffer>)indirectBuffer.GetPtr()
indirectBufferOffset:indirectBufferOffset
threadsPerThreadgroup:mtlThreadsPerThreadgroup];
}
void ComputeCommandEncoder::UpdateFence(const Fence& fence)
{
Validate();
#if MTLPP_IS_AVAILABLE_IOS(10_0)
[(__bridge id<MTLComputeCommandEncoder>)m_ptr updateFence:(__bridge id<MTLFence>)fence.GetPtr()];
#endif
}
void ComputeCommandEncoder::WaitForFence(const Fence& fence)
{
#if MTLPP_IS_AVAILABLE_IOS(10_0)
[(__bridge id<MTLComputeCommandEncoder>)m_ptr waitForFence:(__bridge id<MTLFence>)fence.GetPtr()];
#endif
}
}
//////////////////////////////////////
// FILE: compute_pipeline.mm
//////////////////////////////////////
/*
* Copyright 2016-2017 Nikolay Aleksiev. All rights reserved.
* License: https://github.com/naleksiev/mtlpp/blob/master/LICENSE
*/
// #include "compute_pipeline.hpp"
#include <Metal/MTLComputePipeline.h>
namespace mtlpp
{
ComputePipelineReflection::ComputePipelineReflection() :
ns::Object(ns::Handle{ (__bridge void*)[[MTLComputePipelineReflection alloc] init] })
{
}
ComputePipelineDescriptor::ComputePipelineDescriptor() :
ns::Object(ns::Handle{ (__bridge void*)[[MTLComputePipelineDescriptor alloc] init] })
{
}
ns::String ComputePipelineDescriptor::GetLabel() const
{
Validate();
return ns::Handle{ (__bridge void*)[(__bridge MTLComputePipelineDescriptor*)m_ptr label] };
}
Function ComputePipelineDescriptor::GetComputeFunction() const
{
Validate();
return ns::Handle { (__bridge void*)[(__bridge MTLComputePipelineDescriptor*)m_ptr computeFunction] };
}
bool ComputePipelineDescriptor::GetThreadGroupSizeIsMultipleOfThreadExecutionWidth() const
{
Validate();
return [(__bridge MTLComputePipelineDescriptor*)m_ptr threadGroupSizeIsMultipleOfThreadExecutionWidth];
}
StageInputOutputDescriptor ComputePipelineDescriptor::GetStageInputDescriptor() const
{
Validate();
#if MTLPP_IS_AVAILABLE(10_12, 10_0)
return ns::Handle { (__bridge void*)[(__bridge MTLComputePipelineDescriptor*)m_ptr stageInputDescriptor] };
#else
return ns::Handle { nullptr };
#endif
}
void ComputePipelineDescriptor::SetLabel(const ns::String& label)
{
Validate();
[(__bridge MTLComputePipelineDescriptor*)m_ptr setLabel:(__bridge NSString*)label.GetPtr()];
}
void ComputePipelineDescriptor::SetComputeFunction(const Function& function)
{
Validate();
[(__bridge MTLComputePipelineDescriptor*)m_ptr setComputeFunction:(__bridge id<MTLFunction>)function.GetPtr()];
}
void ComputePipelineDescriptor::SetThreadGroupSizeIsMultipleOfThreadExecutionWidth(bool value)
{
Validate();
[(__bridge MTLComputePipelineDescriptor*)m_ptr setThreadGroupSizeIsMultipleOfThreadExecutionWidth:value];
}
void ComputePipelineDescriptor::SetStageInputDescriptor(const StageInputOutputDescriptor& stageInputDescriptor) const
{
Validate();
#if MTLPP_IS_AVAILABLE(10_12, 10_0)
[(__bridge MTLComputePipelineDescriptor*)m_ptr setStageInputDescriptor:(__bridge MTLStageInputOutputDescriptor*)stageInputDescriptor.GetPtr()];
#endif
}
Device ComputePipelineState::GetDevice() const
{
Validate();
return ns::Handle { (__bridge void*)[(__bridge id<MTLComputePipelineState>)m_ptr device] };
}
uint32_t ComputePipelineState::GetMaxTotalThreadsPerThreadgroup() const
{
Validate();
return uint32_t([(__bridge id<MTLComputePipelineState>)m_ptr maxTotalThreadsPerThreadgroup]);
}
uint32_t ComputePipelineState::GetThreadExecutionWidth() const
{
Validate();
return uint32_t([(__bridge id<MTLComputePipelineState>)m_ptr threadExecutionWidth]);
}
}
//////////////////////////////////////
// FILE: depth_stencil.mm
//////////////////////////////////////
/*
* Copyright 2016-2017 Nikolay Aleksiev. All rights reserved.
* License: https://github.com/naleksiev/mtlpp/blob/master/LICENSE
*/
// #include "depth_stencil.hpp"
#include <Metal/MTLDepthStencil.h>
namespace mtlpp