-
Notifications
You must be signed in to change notification settings - Fork 47
/
amp_graphics.h
executable file
·4967 lines (4504 loc) · 228 KB
/
amp_graphics.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
/***
* ==++==
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* ==--==
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*
* amp_graphics.h
*
* C++ AMP Graphics Library
*
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
****/
#pragma once
#ifndef _SILENCE_AMP_DEPRECATION_WARNINGS
#error <amp_graphics.h> is part of C++ AMP which is deprecated by Microsoft and will be REMOVED. \
You can define _SILENCE_AMP_DEPRECATION_WARNINGS to acknowledge that you have received this warning.
#endif // _SILENCE_AMP_DEPRECATION_WARNINGS
#include <amp_short_vectors.h>
#include <array>
#include <dxgiformat.h>
#include <sstream>
#define _AMP_GRAPHICS_H
#pragma warning( push )
#pragma warning( disable : 4127 ) // conditional expression is constant
#pragma warning( disable : 4996 ) // writeonly_texture_view is deprecated
#pragma warning( disable : 6326 ) // Potential comparison of a constant with another constant
namespace Concurrency
{
namespace graphics
{
namespace details
{
template<typename _Ty>
struct _Short_vector_type_traits
{
typedef void _Scalar_type;
static const bool _Is_valid_SVT_for_texture = false;
static const _Short_vector_base_type_id _Format_base_type_id = _Invalid_type;
static const unsigned int _Num_channels = 0;
static const unsigned int _Default_bits_per_channel = 0;
};
template<>
struct _Short_vector_type_traits<unsigned int>
{
typedef unsigned int _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Uint_type;
static const unsigned int _Num_channels = 1;
static const unsigned int _Default_bits_per_channel = 32;
};
template<>
struct _Short_vector_type_traits<uint_2>
{
typedef uint_2::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Uint_type;
static const unsigned int _Num_channels = 2;
static const unsigned int _Default_bits_per_channel = 32;
};
template<>
struct _Short_vector_type_traits<uint_3>
{
typedef uint_3::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Uint_type;
static const unsigned int _Num_channels = 3;
static const unsigned int _Default_bits_per_channel = 32;
};
template<>
struct _Short_vector_type_traits<uint_4>
{
typedef uint_4::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Uint_type;
static const unsigned int _Num_channels = 4;
static const unsigned int _Default_bits_per_channel = 32;
};
template<>
struct _Short_vector_type_traits<int>
{
typedef int _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Int_type;
static const unsigned int _Num_channels = 1;
static const unsigned int _Default_bits_per_channel = 32;
};
template<>
struct _Short_vector_type_traits<int_2>
{
typedef int_2::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Int_type;
static const unsigned int _Num_channels = 2;
static const unsigned int _Default_bits_per_channel = 32;
};
template<>
struct _Short_vector_type_traits<int_3>
{
typedef int_3::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Int_type;
static const unsigned int _Num_channels = 3;
static const unsigned int _Default_bits_per_channel = 32;
};
template<>
struct _Short_vector_type_traits<int_4>
{
typedef int_4::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Int_type;
static const unsigned int _Num_channels = 4;
static const unsigned int _Default_bits_per_channel = 32;
};
template<>
struct _Short_vector_type_traits<float>
{
typedef float _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Float_type;
static const unsigned int _Num_channels = 1;
static const unsigned int _Default_bits_per_channel = 32;
};
template<>
struct _Short_vector_type_traits<float_2>
{
typedef float_2::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Float_type;
static const unsigned int _Num_channels = 2;
static const unsigned int _Default_bits_per_channel = 32;
};
template<>
struct _Short_vector_type_traits<float_3>
{
typedef float_3::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Float_type;
static const unsigned int _Num_channels = 3;
static const unsigned int _Default_bits_per_channel = 32;
};
template<>
struct _Short_vector_type_traits<float_4>
{
typedef float_4::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Float_type;
static const unsigned int _Num_channels = 4;
static const unsigned int _Default_bits_per_channel = 32;
};
template<>
struct _Short_vector_type_traits<unorm>
{
typedef unorm _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Unorm_type;
static const unsigned int _Num_channels = 1;
static const unsigned int _Default_bits_per_channel = 16;
};
template<>
struct _Short_vector_type_traits<unorm_2>
{
typedef unorm_2::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Unorm_type;
static const unsigned int _Num_channels = 2;
static const unsigned int _Default_bits_per_channel = 16;
};
template<>
struct _Short_vector_type_traits<unorm_3>
{
typedef unorm_3::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = false;
static const _Short_vector_base_type_id _Format_base_type_id = _Invalid_type;
static const unsigned int _Num_channels = 0;
static const unsigned int _Default_bits_per_channel = 0;
};
template<>
struct _Short_vector_type_traits<unorm_4>
{
typedef unorm_4::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Unorm_type;
static const unsigned int _Num_channels = 4;
static const unsigned int _Default_bits_per_channel = 16;
};
template<>
struct _Short_vector_type_traits<norm>
{
typedef norm _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Norm_type;
static const unsigned int _Num_channels = 1;
static const unsigned int _Default_bits_per_channel = 16;
};
template<>
struct _Short_vector_type_traits<norm_2>
{
typedef norm_2::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Norm_type;
static const unsigned int _Num_channels = 2;
static const unsigned int _Default_bits_per_channel = 16;
};
template<>
struct _Short_vector_type_traits<norm_3>
{
typedef norm_3::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = false;
static const _Short_vector_base_type_id _Format_base_type_id = _Invalid_type;
static const unsigned int _Num_channels = 0;
static const unsigned int _Default_bits_per_channel = 0;
};
template<>
struct _Short_vector_type_traits<norm_4>
{
typedef norm_4::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Norm_type;
static const unsigned int _Num_channels = 4;
static const unsigned int _Default_bits_per_channel = 16;
};
template<>
struct _Short_vector_type_traits<double>
{
typedef double _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Double_type;
static const unsigned int _Num_channels = 2;
static const unsigned int _Default_bits_per_channel = 32;
};
template<>
struct _Short_vector_type_traits<double_2>
{
typedef double_2::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = true;
static const _Short_vector_base_type_id _Format_base_type_id = _Double_type;
static const unsigned int _Num_channels = 4;
static const unsigned int _Default_bits_per_channel = 32;
};
template<>
struct _Short_vector_type_traits<double_3>
{
typedef double_3::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = false;
static const _Short_vector_base_type_id _Format_base_type_id = _Invalid_type;
static const unsigned int _Num_channels = 0;
static const unsigned int _Default_bits_per_channel = 0;
};
template<>
struct _Short_vector_type_traits<double_4>
{
typedef double_4::value_type _Scalar_type;
static const bool _Is_valid_SVT_for_texture = false;
static const _Short_vector_base_type_id _Format_base_type_id = _Invalid_type;
static const unsigned int _Num_channels = 0;
static const unsigned int _Default_bits_per_channel = 0;
};
template<typename _Short_vector_type>
unsigned int _Get_default_bits_per_scalar_element()
{
return _Short_vector_type_traits<_Short_vector_type>::_Format_base_type_id == _Double_type ?
_Short_vector_type_traits<_Short_vector_type>::_Default_bits_per_channel * 2 :
_Short_vector_type_traits<_Short_vector_type>::_Default_bits_per_channel;
}
template<int _Rank>
std::array<size_t, 3> _Get_dimensions(const Concurrency::extent<_Rank> & _Ext, unsigned int _Mip_offset)
{
std::array<size_t, 3> _Arr;
// For un-used dimensions, use value 1.
switch((_Rank)) {
case 1:
_Arr[0] = static_cast<size_t>((_Ext[0] >> _Mip_offset) ? (_Ext[0] >> _Mip_offset) : 1U);
_Arr[1] = 1;
_Arr[2] = 1;
break;
case 2:
_Arr[0] = static_cast<size_t>((_Ext[1] >> _Mip_offset) ? (_Ext[1] >> _Mip_offset) : 1U);
_Arr[1] = static_cast<size_t>((_Ext[0] >> _Mip_offset) ? (_Ext[0] >> _Mip_offset) : 1U);
_Arr[2] = 1;
break;
case 3:
_Arr[0] = static_cast<size_t>((_Ext[2] >> _Mip_offset) ? (_Ext[2] >> _Mip_offset) : 1U);
_Arr[1] = static_cast<size_t>((_Ext[1] >> _Mip_offset) ? (_Ext[1] >> _Mip_offset) : 1U);
_Arr[2] = static_cast<size_t>((_Ext[0] >> _Mip_offset) ? (_Ext[0] >> _Mip_offset) : 1U);
break;
default:
_ASSERTE(false);
_Arr[0] = 1;
_Arr[1] = 1;
_Arr[2] = 1;
break;
}
return _Arr;
}
template <int _Rank>
std::array<size_t, 3> _Get_indices(const index<_Rank> &_Idx)
{
std::array<size_t, 3> _Arr;
// For un-used dimensions, use value 0.
switch((_Rank)) {
case 1:
_Arr[0] = static_cast<size_t>(_Idx[0]);
_Arr[1] = 0;
_Arr[2] = 0;
break;
case 2:
_Arr[0] = static_cast<size_t>(_Idx[1]);
_Arr[1] = static_cast<size_t>(_Idx[0]);
_Arr[2] = 0;
break;
case 3:
_Arr[0] = static_cast<size_t>(_Idx[2]);
_Arr[1] = static_cast<size_t>(_Idx[1]);
_Arr[2] = static_cast<size_t>(_Idx[0]);
break;
default:
_ASSERTE(false);
_Arr[0] = 0;
_Arr[1] = 0;
_Arr[2] = 0;
break;
}
return _Arr;
}
template<int _Rank>
Concurrency::extent<_Rank> _Create_extent(size_t _Width, size_t _Height, size_t _Depth)
{
extent<_Rank> _Ext;
switch((_Rank)) {
case 1:
_Ext[0] = static_cast<int>(_Width);
break;
case 2:
_Ext[0] = static_cast<int>(_Height);
_Ext[1] = static_cast<int>(_Width);
break;
case 3:
_Ext[0] = static_cast<int>(_Depth);
_Ext[1] = static_cast<int>(_Height);
_Ext[2] = static_cast<int>(_Width);
break;
default:
_ASSERTE(false);
break;
}
return _Ext;
}
// forward declaration
template <typename _Value_type, int _Rank> class _Texture_base;
template <typename _Value_type, int _Rank>
_Event _Copy_async_impl(const void * _Src, unsigned int _Src_byte_size, const _Texture_base<_Value_type, _Rank>& _Dst, const index<_Rank> &_Offset, const Concurrency::extent<_Rank> &_Copy_extent);
template <typename OutputIterator, typename _Value_type, int _Rank>
_Event _Copy_async_impl(const _Texture_base<_Value_type, _Rank> &_Src, OutputIterator _Dest_iter);
template<typename _Value_type, int _Rank>
_Event _Copy_async_impl(const _Texture_base<_Value_type, _Rank>& _Src, const index<_Rank> &_Src_offset,
const _Texture_base<_Value_type, _Rank>& _Dst, const index<_Rank> &_Dst_offset, const extent<_Rank> &_Copy_extent);
// The base class for texture, writeonly_texture_view
template <typename _Value_type, int _Rank>
class _Texture_base
{
static_assert(_Rank > 0 && _Rank <= 3, "texture is only supported for rank 1, 2, and 3.");
static_assert(_Short_vector_type_traits<typename std::remove_const<_Value_type>::type>::_Is_valid_SVT_for_texture, "invalid value_type for a texture.");
// Friends
template<typename _T>
friend const _Texture_descriptor& Concurrency::details::_Get_texture_descriptor(const _T& _Tex) __GPU;
template<typename _T>
friend _Ret_ _Texture* Concurrency::details::_Get_texture(const _T& _Tex) __CPU_ONLY;
template<typename _Value_type, int _Rank>
friend _Event _Copy_async_impl(const _Texture_base<_Value_type, _Rank>& _Src, const index<_Rank> &_Src_offset,
const _Texture_base<_Value_type, _Rank>& _Dst, const index<_Rank> &_Dst_offset, const extent<_Rank> &_Copy_extent) __CPU_ONLY;
public:
static const int rank = _Rank;
typedef _Value_type value_type;
typedef typename _Short_vector_type_traits<_Value_type>::_Scalar_type scalar_type;
public:
/// <summary>
/// Returns the extent that defines the shape of this texture or texture view.
/// </summary>
__declspec(property(get=get_extent)) Concurrency::extent<_Rank> extent;
Concurrency::extent<_Rank> get_extent() const __GPU
{
return _M_extent;
}
/// <summary>
/// Returns the extent for specific mipmap level of this texture or texture view.
/// </summary>
/// <param>
/// Mipmap level for which extent should be calculated.
/// </param>
Concurrency::extent<_Rank> get_mipmap_extent(unsigned int _Mipmap_level) const __CPU_ONLY
{
if (_Mipmap_level >= this->get_mipmap_levels())
{
std::stringstream _Err_msg;
_Err_msg << "Value for _Mipmap_level parameter (" << _Mipmap_level
<< ") cannot be greater than or equal to number of mipmap levels ("
<< this->get_mipmap_levels() << ") on the texture or texture view";
throw runtime_exception(_Err_msg.str().c_str(), E_INVALIDARG);
}
return Concurrency::details::_Get_extent_at_level(_M_extent, _Mipmap_level);
}
/// <summary>
/// Returns the extent for specific mipmap level of this texture or texture view.
/// </summary>
/// <param>
/// Mipmap level for which extent should be calculated.
/// </param>
Concurrency::extent<_Rank> get_mipmap_extent(unsigned int _Mipmap_level) const __GPU_ONLY
{
return Concurrency::details::_Get_extent_at_level_unsafe(_M_extent, _Mipmap_level);
}
/// <summary>
/// Returns the accelerator_view where this texture or texture view is located.
/// </summary>
__declspec(property(get=get_accelerator_view)) Concurrency::accelerator_view accelerator_view;
Concurrency::accelerator_view get_accelerator_view() const __CPU_ONLY
{
return _Get_texture()->_Get_access_on_accelerator_view();
}
/// <summary>
/// Returns the number of bits per scalar element
/// </summary>
__declspec(property(get=get_bits_per_scalar_element)) unsigned int bits_per_scalar_element;
unsigned int get_bits_per_scalar_element() const __CPU_ONLY
{
unsigned int _Bits_per_channel = _Get_texture()->_Get_bits_per_channel();
return _Short_vector_type_traits<_Value_type>::_Format_base_type_id == _Double_type ? _Bits_per_channel * (sizeof(double)/sizeof(int)) : _Bits_per_channel;
}
/// <summary>
/// Query how many mipmap levels are accessible by this texture (or texture view).
/// </summary>
/// <returns>
/// Returns number of mipmap levels accessible by this texture (or texture view).
/// </returns>
__declspec(property(get=get_mipmap_levels)) unsigned int mipmap_levels;
unsigned int get_mipmap_levels() const __GPU
{
return _M_texture_descriptor._Get_view_mipmap_levels();
}
/// <summary>
/// Returns the physical data length (in bytes) that is required in order to represent
/// the texture on the host side with its native format.
/// If the texture contains multiple mipmap levels the value represents the sum of physical data length for each accessible mipmap level by this texture (or texture view).
/// </summary>
__declspec(property(get=get_data_length)) unsigned int data_length;
unsigned int get_data_length() const __CPU_ONLY
{
return _Get_texture()->_Get_data_length(this->_Get_most_detailed_mipmap_level(), this->get_mipmap_levels());
}
protected:
// internal storage abstraction
typedef Concurrency::details::_Texture_descriptor _Texture_descriptor;
_Texture_base() __CPU_ONLY
{
// This default ctor is required to enable move ctor for a derived types,
// empty _Texture_base is later initialized by move assignment operator
}
_Texture_base(const Concurrency::extent<_Rank>& _Ext, unsigned int _Mipmap_levels = 1) __CPU_ONLY
: _M_extent(_Ext), _M_texture_descriptor(/*_Most_detailed_mipmap_level=*/0, _Mipmap_levels)
{
_Is_valid_extent(_M_extent);
_Are_valid_mipmap_parameters(/*_Most_detailed_mipmap_level=*/0, _Mipmap_levels);
// Validate if we can generate _Mipmap_levels number of mipmap levels given the dimensionality of the texture
unsigned int _Max_mipmap_levels = _Get_max_mipmap_levels(_M_extent);
if (_Mipmap_levels > _Max_mipmap_levels)
{
std::stringstream _Err_msg;
_Err_msg << "The texture extent is too small to generate (" << _Mipmap_levels << ") mipmap levels, the maximum allowed is (" << _Max_mipmap_levels << ")";
throw runtime_exception(_Err_msg.str().c_str(), E_INVALIDARG);
}
else if (_Mipmap_levels == 0)
{
// Generate full range of all mipmaps
// e.g. 2D 10x2 texture would have: 10x2, 5x1, 2x1, 1x1 (4 mipmap levels)
_Mipmap_levels = _Max_mipmap_levels;
}
_M_texture_descriptor._Set_view_mipmap_levels(_Mipmap_levels);
}
// shallow copy for texture_views
_Texture_base(const _Texture_base & _Src) __GPU
: _M_extent(_Src._M_extent), _M_texture_descriptor(_Src._M_texture_descriptor)
{
}
// shallow copy for texture_views that redefine range of mipmaps
_Texture_base(const _Texture_base & _Src, unsigned int _Most_detailed_mipmap_level, unsigned int _View_mipmap_levels) __CPU_ONLY
: _M_extent(_Get_extent_at_level(_Src.extent, _Most_detailed_mipmap_level)), _M_texture_descriptor(_Src._M_texture_descriptor, _Src._Get_most_detailed_mipmap_level() + _Most_detailed_mipmap_level, _View_mipmap_levels)
{
Concurrency::details::_Is_valid_mipmap_range(_Src.get_mipmap_levels(), _Most_detailed_mipmap_level, _View_mipmap_levels);
}
// shallow copy for texture_views that in restrict(amp) context, the texture views can no longer redefine mipmap range,
// but read-write texture view needs to flatten to single mipmap level when created over a texture with multiple mipmap levels.
_Texture_base(const _Texture_base & _Src, bool _Flatten_mipmap_levels) __GPU_ONLY
: _M_extent(_Src.extent), _M_texture_descriptor(_Src._M_texture_descriptor, /*_Most_detailed_mipmap_level=*/0, _Flatten_mipmap_levels ? /*_View_mipmap_levels=*/1 : _Src.get_mipmap_levels())
{
}
// interop
_Texture_base(const Concurrency::extent<_Rank>& _Ext, const _Texture_descriptor & _Desc) __CPU_ONLY
: _M_extent(_Ext), _M_texture_descriptor(_Desc)
{
Concurrency::details::_Is_valid_extent(_M_extent);
}
void _Copy_to(const _Texture_base & _Dest) const __CPU_ONLY
{
if (!(*this == _Dest))
{
_ASSERTE(this->extent == _Dest.extent);
details::_Copy_async_impl(*this, index<_Rank>(), _Dest, index<_Rank>(), _Dest.extent)._Get();
}
}
bool operator==(const _Texture_base & _Other) const __CPU_ONLY
{
return _Other._M_extent == _M_extent && _Other._M_texture_descriptor == _M_texture_descriptor;
}
~_Texture_base() __GPU
{
}
_Ret_ _Texture* _Get_texture() const __CPU_ONLY
{
return _M_texture_descriptor._Get_texture_ptr();
}
unsigned int _Get_most_detailed_mipmap_level() const __GPU
{
return _M_texture_descriptor._Get_most_detailed_mipmap_level();
}
bool _Are_mipmap_levels_overlapping(const _Texture_base &_Other) const __CPU_ONLY
{
return _M_texture_descriptor._Are_mipmap_levels_overlapping(&_Other._M_texture_descriptor);
}
protected:
Concurrency::extent<_Rank> _M_extent;
_Texture_descriptor _M_texture_descriptor;
};
inline void _Is_valid_data_length(unsigned int _Num_elems, unsigned int _Bits_per_elem)
{
unsigned long long _Bytes_per_elem = static_cast<unsigned long long>(_Bits_per_elem / 8U);
unsigned long long _Total_bytes = static_cast<unsigned long long>(_Num_elems) * _Bytes_per_elem;
if (_Total_bytes > static_cast<unsigned long long>(UINT_MAX))
{
throw runtime_exception("Invalid - texture data_length exceeds UINT_MAX", E_INVALIDARG);
}
}
template<typename _Iterator>
struct _Is_iterator
{
template<class _Uty> static auto _Fn(_Uty _Val, decltype(*_Val, ++_Val, 0)) -> std::true_type;
template<class _Uty> static auto _Fn(_Uty _Val, ...) -> std::false_type;
static constexpr bool value = decltype(_Fn(std::declval<_Iterator>(),0))::value;
};
} // namespace details
using Concurrency::graphics::details::_Short_vector_type_traits;
// forward declarations
template <typename _Value_type, int _Rank>
class texture;
template <typename _Value_type, int _Rank>
class writeonly_texture_view;
template <typename _Value_type, int _Rank>
class texture_view;
class sampler;
namespace direct3d
{
template<typename _Value_type, int _Rank>
texture<_Value_type, _Rank> make_texture(const Concurrency::accelerator_view &_Av, _In_ IUnknown *_D3D_texture, DXGI_FORMAT _View_format = DXGI_FORMAT_UNKNOWN) __CPU_ONLY;
sampler make_sampler(_In_ IUnknown *_D3D_sampler) __CPU_ONLY;
_Ret_ IUnknown * get_sampler(const Concurrency::accelerator_view &_Av, const sampler &_Sampler) __CPU_ONLY;
} // namespace direct3d
namespace details
{
template <typename T>
struct texture_traits
{
static const bool is_texture = false;
static const bool is_writable = false;
};
template <typename _Value_type, int _Rank>
struct texture_traits<texture<_Value_type, _Rank>>
{
static const bool is_texture = true;
static const bool is_writable = true;
};
template <typename _Value_type, int _Rank>
struct texture_traits<const texture<_Value_type, _Rank>>
{
static const bool is_texture = true;
static const bool is_writable = false;
};
template <typename _Value_type, int _Rank>
struct texture_traits<writeonly_texture_view<_Value_type, _Rank>>
{
static const bool is_texture = true;
static const bool is_writable = true;
};
template <typename _Value_type, int _Rank>
struct texture_traits<const writeonly_texture_view<_Value_type, _Rank>>
{
static const bool is_texture = true;
static const bool is_writable = true;
};
template <typename _Value_type, int _Rank>
struct texture_traits<texture_view<_Value_type, _Rank>>
{
static const bool is_texture = true;
static const bool is_writable = true;
};
template <typename _Value_type, int _Rank>
struct texture_traits<texture_view<const _Value_type, _Rank>>
{
static const bool is_texture = true;
static const bool is_writable = false;
};
template <typename _Value_type, int _Rank>
struct texture_traits<const texture_view<const _Value_type, _Rank>>
{
static const bool is_texture = true;
static const bool is_writable = false;
};
template <typename _Value_type, int _Rank>
struct texture_traits<const texture_view<_Value_type, _Rank>>
{
static const bool is_texture = true;
static const bool is_writable = true;
};
// The helper function used by ETW and copy functions to calculate number of bytes for the copy operation given input section
template <typename _Value_type, int _Rank>
unsigned int _Get_section_size(const _Texture_base<_Value_type, _Rank> &_Tex, const extent<_Rank> &_Extent)
{
_Texture* _Tex_ptr = _Get_texture(_Tex);
_Texture_descriptor _Tex_desc = _Get_texture_descriptor(_Tex);
return _Tex_ptr->_Get_data_length(_Tex_desc._Get_most_detailed_mipmap_level(), _Tex_desc._Get_view_mipmap_levels(), _Get_dimensions(_Extent, /*Mip_offset=*/0).data());
}
template <typename _Input_iterator, typename _Value_type>
_Event _Copy_async_impl(_Input_iterator _First, _Input_iterator _Last,
_In_ _Texture *_Dst, const size_t *_Dst_offset, unsigned int _Dst_mipmap_level,
const size_t *_Copy_extent, const size_t *_Preferred_copy_chunk_extent = NULL)
{
_ASSERTE(_Dst != nullptr);
_ASSERTE(_Dst_offset != nullptr);
_ASSERTE(_Copy_extent != nullptr);
_ASSERTE((unsigned int)std::distance(_First, _Last) >= (_Copy_extent[0] * _Copy_extent[1] * _Copy_extent[2]));
// The copy region should be within the bounds of the destination texture
_ASSERTE((_Dst_offset[0] + _Copy_extent[0]) <= _Dst->_Get_width(_Dst_mipmap_level));
_ASSERTE((_Dst_offset[1] + _Copy_extent[1]) <= _Dst->_Get_height(_Dst_mipmap_level));
_ASSERTE((_Dst_offset[2] + _Copy_extent[2]) <= _Dst->_Get_depth(_Dst_mipmap_level));
if ((sizeof(_Value_type) > sizeof(unsigned char)) && (_Dst->_Get_bits_per_element() != (8U * sizeof(_Value_type))))
{
throw runtime_exception("Iterator-based copy is not supported on textures where the size of the _Value_type is not equal to the texel size.", E_INVALIDARG);
}
// If the dest is accessible on the host we can perform the copy entirely on the host
if (_Dst->_Get_host_ptr() != NULL)
{
// We have made sure that the three multiplications below won't cause integer overflow when creating the texture
_ASSERTE(((_Dst->_Get_bits_per_element() * _Copy_extent[0]) % (8U * sizeof(_Value_type))) == 0);
size_t _Row_size = (_Dst->_Get_bits_per_element() * _Copy_extent[0]) >> 3; // in bytes
size_t _Depth_slice_size = _Row_size * _Copy_extent[1];
size_t _Row_pitch = _Dst->_Get_row_pitch();
size_t _Depth_pitch = _Dst->_Get_depth_pitch();
_ASSERTE(_Row_pitch >= _Row_size);
_ASSERTE(_Depth_pitch >= _Depth_slice_size);
size_t _Dst_offset_in_bytes = ((_Dst_offset[0] * _Dst->_Get_bits_per_element()) >> 3) +
(_Dst_offset[1] * _Row_pitch) + (_Dst_offset[2] * _Depth_pitch);
unsigned char *_PDest = reinterpret_cast<unsigned char*>(_Dst->_Get_host_ptr()) + _Dst_offset_in_bytes;
_Copy_data_on_host_src_iter(_Dst->_Get_rank(), _First, reinterpret_cast<_Value_type*>(_PDest),
_Row_size / sizeof(_Value_type), _Copy_extent[1], _Copy_extent[2],
_Row_pitch, _Depth_pitch, _Row_size / sizeof(_Value_type), _Depth_slice_size / sizeof(_Value_type));
return _Event();
}
// The dest is not accessible on the host; we need to copy src to
// a temporary staging texture and launch a copy from the staging texture
// to the dest texture.
_Event _Ev;
// Determine the copy chunk extent
std::array<size_t, 3> _Copy_chunk_extent;
if (_Preferred_copy_chunk_extent != NULL)
{
std::copy(&_Preferred_copy_chunk_extent[0], &_Preferred_copy_chunk_extent[3], _Copy_chunk_extent.begin());
}
else
{
_Get_preferred_copy_chunk_extent(_Dst->_Get_rank(), _Copy_extent[0], _Copy_extent[1], _Copy_extent[2], _Dst->_Get_bits_per_element(), _Copy_chunk_extent.data());
}
std::array<size_t, 3> _Curr_copy_offset;
std::copy(&_Dst_offset[0], &_Dst_offset[3], _Curr_copy_offset.begin());
std::array<size_t, 3> _Remaining_copy_extent;
std::copy(&_Copy_extent[0], &_Copy_extent[3], _Remaining_copy_extent.begin());
bool _Truncated_copy = false;
do
{
_Texture_ptr _Dst_staging_tex_ptr;
std::array<size_t, 3> _Curr_copy_extent;
_Truncated_copy = _Get_chunked_staging_texture(_Dst, _Copy_chunk_extent.data(), _Remaining_copy_extent.data(), _Curr_copy_extent.data(), &_Dst_staging_tex_ptr);
// Now copy from the src pointer to the temp staging texture
_Dst_staging_tex_ptr->_Map_buffer(_Write_access, true /* _Wait */);
std::array<size_t, 3> _Dst_staging_tex_offset;
_Dst_staging_tex_offset.fill(0);
_Event _Temp_ev = _Copy_async_impl<_Input_iterator, _Value_type>(_First, _Last, _Dst_staging_tex_ptr,
_Dst_staging_tex_offset.data(), /*_Dst_mipmap_level=*/0, _Curr_copy_extent.data(), _Copy_chunk_extent.data());
// Now chain a copy from the temporary staging texture to the _Dst texture
_Texture_ptr _Dst_tex_ptr = _Dst;
_Temp_ev = _Temp_ev._Add_continuation(std::function<_Event()>([_Dst_staging_tex_ptr, _Dst_tex_ptr, _Curr_copy_extent,
_Dst_staging_tex_offset, _Curr_copy_offset, _Dst_mipmap_level]() mutable -> _Event
{
return _Dst_staging_tex_ptr->_Copy_to_async(_Dst_tex_ptr, _Curr_copy_extent.data(), _Dst_staging_tex_offset.data(), _Curr_copy_offset.data(), /*_Src_mipmap_level=*/0, _Dst_mipmap_level);
}));
_Ev = _Ev._Add_event(_Temp_ev);
// Now adjust the _Src and _Dst offsets for the remaining part of the copy
if (_Truncated_copy)
{
// The offset only needs to be adjusted in the most significant dimension
_Curr_copy_offset[_Dst->_Get_rank() - 1] += _Curr_copy_extent[_Dst->_Get_rank() - 1];
std::advance(_First, (((_Curr_copy_extent[0] * _Dst->_Get_bits_per_element()) >> 3) / sizeof(_Value_type)) * _Curr_copy_extent[1] * _Curr_copy_extent[2]);
}
} while (_Truncated_copy);
return _Ev;
}
template <typename _Output_iterator, typename _Value_type>
_Event _Copy_async_impl(_Texture *_Tex, const size_t *_Tex_offset, unsigned int _Src_mipmap_level, const size_t *_Copy_extent, _Output_iterator _First, const size_t *_Preferred_copy_chunk_extent = NULL)
{
_ASSERTE(_Tex != nullptr);
_ASSERTE(_Tex_offset != nullptr);
_ASSERTE(_Copy_extent != nullptr);
// The copy region should be within the bounds of the source texture
_ASSERTE((_Tex_offset[0] + _Copy_extent[0]) <= _Tex->_Get_width(_Src_mipmap_level));
_ASSERTE((_Tex_offset[1] + _Copy_extent[1]) <= _Tex->_Get_height(_Src_mipmap_level));
_ASSERTE((_Tex_offset[2] + _Copy_extent[2]) <= _Tex->_Get_depth(_Src_mipmap_level));
if ((sizeof(_Value_type) > sizeof(unsigned char)) && (_Tex->_Get_bits_per_element() != (8U * sizeof(_Value_type))))
{
throw runtime_exception("Iterator-based copy is not supported on textures where the size of the _Value_type is not equal to the texel size.", E_INVALIDARG);
}
// If the texture is available on the host then we can perform the copy entirely on the host
if (_Tex->_Get_host_ptr() != nullptr)
{
// We have made sure that the three multiplications below won't cause integer overflow when creating the texture
_ASSERTE(((_Tex->_Get_bits_per_element() * _Copy_extent[0]) % 8U) == 0);
size_t _Row_size = (_Tex->_Get_bits_per_element() * _Copy_extent[0]) >> 3; // in bytes
size_t _Depth_slice_size = _Row_size * _Copy_extent[1];
size_t _Row_pitch = _Tex->_Get_row_pitch();
size_t _Depth_pitch = _Tex->_Get_depth_pitch();
_ASSERTE(_Row_pitch >= _Row_size);
_ASSERTE(_Depth_pitch >= _Depth_slice_size);
size_t _Tex_offset_in_bytes = ((_Tex_offset[0] * _Tex->_Get_bits_per_element()) >> 3) +
(_Tex_offset[1] * _Row_pitch) + (_Tex_offset[2] * _Depth_pitch);
unsigned char *_PTex = reinterpret_cast<unsigned char*>(_Tex->_Get_host_ptr()) + _Tex_offset_in_bytes;
_Copy_data_on_host_dst_iter(_Tex->_Get_rank(), reinterpret_cast<_Value_type*>(_PTex), _First,
_Row_size / sizeof(_Value_type), _Copy_extent[1], _Copy_extent[2],
_Row_pitch, _Depth_pitch, _Row_size / sizeof(_Value_type), _Depth_slice_size / sizeof(_Value_type));
return _Event();
}
// The texture is not accessible on the host; we need to copy to/from a staging
// texture before the copy to the destination. This is done in chunks, such that
// we can concurrently copy from the source texture to a staging texture while
// copying from a staging texture from a previous chunk to the destination.
_Event _Ev;
// Determine the copy chunk extent
std::array<size_t, 3> _Copy_chunk_extent;
if (_Preferred_copy_chunk_extent != nullptr)
{
std::copy(&_Preferred_copy_chunk_extent[0], &_Preferred_copy_chunk_extent[3], _Copy_chunk_extent.begin());
}
else
{
_Get_preferred_copy_chunk_extent(_Tex->_Get_rank(), _Copy_extent[0], _Copy_extent[1], _Copy_extent[2], _Tex->_Get_bits_per_element(), _Copy_chunk_extent.data());
}
std::array<size_t, 3> _Curr_copy_offset;
std::copy(&_Tex_offset[0], &_Tex_offset[3], _Curr_copy_offset.begin());
std::array<size_t, 3> _Remaining_copy_extent;
std::copy(&_Copy_extent[0], &_Copy_extent[3], _Remaining_copy_extent.begin());
bool _Truncated_copy = false;
_Texture_ptr _Staging_tex_ptr;
std::array<size_t, 3> _Curr_copy_extent;
_Truncated_copy = _Get_chunked_staging_texture(_Tex, _Copy_chunk_extent.data(), _Remaining_copy_extent.data(), _Curr_copy_extent.data(), &_Staging_tex_ptr);
// Now copy into the temp staging texture
std::array<size_t, 3> _Staging_tex_offset;
_Staging_tex_offset.fill(0);
_Event _Temp_ev = _Copy_async_impl(_Tex, _Curr_copy_offset.data(), _Src_mipmap_level,
_Staging_tex_ptr._Get_ptr(), _Staging_tex_offset.data(), /*_Dst_mipmap_level=*/0,
_Curr_copy_extent.data(), _Copy_chunk_extent.data());
_Ev = _Ev._Add_event(_Temp_ev);
// If we have finished our copy, we just need to add a continuation to copy
// from the temporary staging texture to the _Dst pointer
if (!_Truncated_copy)
{
return _Ev._Add_continuation(std::function<_Event()>([_Staging_tex_ptr,
_Curr_copy_extent, _Staging_tex_offset, _Copy_chunk_extent, _First]() mutable -> _Event
{
return _Copy_async_impl<_Output_iterator, _Value_type>(_Staging_tex_ptr, _Staging_tex_offset.data(), /*_Src_mipmap_level=*/0, _Curr_copy_extent.data(), _First, _Copy_chunk_extent.data());
}));
}
else
{
// The copy was truncated. We need to recursively perform the rest of the copy
_Texture_ptr _Tex_ptr = _Tex;
_Curr_copy_offset[_Tex->_Get_rank() - 1] += _Curr_copy_extent[_Tex->_Get_rank() - 1];
return _Ev._Add_continuation(std::function<_Event()>([_Staging_tex_ptr, _First, _Curr_copy_extent,
_Staging_tex_offset, _Tex_ptr, _Curr_copy_offset, _Remaining_copy_extent, _Copy_chunk_extent, _Src_mipmap_level]() mutable -> _Event
{
// Initiate copying of the remaining portion
_Output_iterator _New_dst_iter = _First;
_Advance_output_iterator<decltype(_New_dst_iter), size_t>(_New_dst_iter, (((_Curr_copy_extent[0] * _Tex_ptr->_Get_bits_per_element()) >> 3) / sizeof(_Value_type)) * _Curr_copy_extent[1] * _Curr_copy_extent[2]);
_Event _Ev1 = _Copy_async_impl<_Output_iterator, _Value_type>(_Tex_ptr, _Curr_copy_offset.data(), _Src_mipmap_level, _Remaining_copy_extent.data(), _New_dst_iter, _Copy_chunk_extent.data());
// Now copy the data from the temp staging buffer to the _Dst pointer
_Event _Ev2 = _Copy_async_impl<_Output_iterator, _Value_type>(_Staging_tex_ptr, _Staging_tex_offset.data(), /*_Src_mipmap_level=*/0, _Curr_copy_extent.data(), _First, _Copy_chunk_extent.data());
return _Ev2._Add_event(_Ev1);
}));
}
}
template <typename _Value_type, int _Rank>
_Event _Copy_async_impl(const void * _Src, unsigned int _Src_byte_size, const _Texture_base<_Value_type, _Rank>& _Dst, const index<_Rank> &_Dst_offset, const extent<_Rank> &_Copy_extent)
{
_Is_valid_section(_Dst.extent, _Dst_offset, _Copy_extent);
if (_Dst.get_mipmap_levels() > 1)
{
throw runtime_exception("Invalid destination - multiple mipmap levels cannot be copied from source", E_INVALIDARG);
}
if (_Src_byte_size < _Get_section_size(_Dst, _Copy_extent))
{
if (_Dst.extent == _Copy_extent)
{
throw runtime_exception("Invalid _Src_byte_size argument. _Src_byte_size is smaller than the total size of _Dst.", E_INVALIDARG);
}
else
{
throw runtime_exception("Invalid _Src_byte_size argument. _Src_byte_size is smaller than the provided section of _Dst.", E_INVALIDARG);
}
}
_Texture *_Dst_tex_ptr = _Get_texture(_Dst);
std::array<size_t, 3> _Copy_extent_arr = _Get_dimensions(_Copy_extent, /*_Mip_offset=*/0);
std::array<size_t, 3> _Dst_offset_arr = _Get_indices(_Dst_offset);
auto _First = stdext::make_unchecked_array_iterator(reinterpret_cast<const unsigned char*>(_Src));
auto _Last = stdext::make_unchecked_array_iterator(reinterpret_cast<const unsigned char*>(_Src) + _Src_byte_size);
return _Copy_async_impl<decltype(_First), unsigned char>(_First, _Last, _Dst_tex_ptr, _Dst_offset_arr.data(), _Get_texture_descriptor(_Dst)._Get_most_detailed_mipmap_level(), _Copy_extent_arr.data());
}
template<typename _Value_type, int _Rank>
_Event _Copy_async_impl(const _Texture_base<_Value_type, _Rank>& _Src, const index<_Rank> &_Src_offset, const extent<_Rank> &_Copy_extent, _Out_ void * _Dst, unsigned int _Dst_byte_size)
{
_Is_valid_section(_Src.extent, _Src_offset, _Copy_extent);
if (_Src.get_mipmap_levels() > 1)
{
throw runtime_exception("Invalid source - multiple mipmap levels cannot be copied to destination", E_INVALIDARG);
}
if (_Get_section_size(_Src, _Copy_extent) > _Dst_byte_size)
{
if (_Src.extent == _Copy_extent)
{
throw runtime_exception("Invalid _Dst_byte_size argument. _Dst_byte_size is smaller than the size of _Src.", E_INVALIDARG);
}
else
{
throw runtime_exception("Invalid _Dst_byte_size argument. _Dst_byte_size is smaller than the provided section of _Src.", E_INVALIDARG);
}
}
_Texture *_Src_tex_ptr = _Get_texture(_Src);
std::array<size_t, 3> _Copy_extent_arr = _Get_dimensions(_Copy_extent, /*_Mip_offset=*/0);
std::array<size_t, 3> _Src_offset_arr = _Get_indices(_Src_offset);
auto _First = stdext::make_unchecked_array_iterator(reinterpret_cast<unsigned char*>(_Dst));
return _Copy_async_impl<decltype(_First), unsigned char>(_Src_tex_ptr, _Src_offset_arr.data(), _Get_texture_descriptor(_Src)._Get_most_detailed_mipmap_level(), _Copy_extent_arr.data(), _First);