-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy path9on12Util.h
More file actions
2599 lines (2367 loc) · 94.5 KB
/
Copy path9on12Util.h
File metadata and controls
2599 lines (2367 loc) · 94.5 KB
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.
// Licensed under the MIT license.
#pragma once
#include <intrin.h>
#if _M_AMD64
#define SSE4_CRC_INPUT_TYPE UINT64
#define SSE4_CRC_FUNCTION _mm_crc32_u64
#elif _M_IX86
#define SSE4_CRC_INPUT_TYPE UINT32
#define SSE4_CRC_FUNCTION _mm_crc32_u32
#endif
#pragma warning(disable: 4063) // Cases in a switch with values that don't belong to the enum
namespace D3D9on12
{
static volatile long g_CommandID = 0;
template <typename T> using unique_comptr = D3D12TranslationLayer::unique_comptr<T>;
struct CleanupResourceBindingsAndRelease
{
void operator()(D3D12TranslationLayer::Resource*);
};
typedef D3D12TranslationLayer::unique_comptr<D3D12TranslationLayer::Resource,
CleanupResourceBindingsAndRelease> unique_unbind_resourceptr;
template<typename T>
struct inline_destructor
{
void operator()(T *pT) const
{
pT->~T();
}
};
class Result
{
public:
enum Value : uint8_t
{
S_SUCCESS = 0x00,
S_CHANGE = 0x01,
E_FAILURE = 0x10,
E_INVALID_ARG = 0x20
};
Result() = default;
constexpr Result(Value result) : value(result) {}
constexpr Result operator||(const Result result) { return result.value > value ? result : *this; }
constexpr bool Succeeded() const { return value < Result::E_FAILURE; }
constexpr bool Failed() const { return value >= Result::E_FAILURE; }
constexpr bool operator==(Result other) const noexcept { return value == other.value; }
constexpr bool operator!=(Result other) const noexcept { return value != other.value; }
//Get the underlying enum value. This allows nice switch syntax without enabling implicit casting of Result to HRESULT
constexpr Value AsEnum() const { return value; }
//Explicit way of translating to HRESULT for communicating with parts of the codebase that haven't been migrated from HRESULT.
constexpr HRESULT AsHresult() const
{
switch (value)
{
case S_SUCCESS:
case S_CHANGE:
return S_OK;
case E_FAILURE: return E_FAIL;
case E_INVALID_ARG: return E_INVALIDARG;
default:
Check9on12(false);
return E_ILLEGAL_STATE_CHANGE;
}
}
/* Updates the result such that the higher valued result takes precedence */
void Update(Result newResult)
{
if (newResult.value > value)
{
value = newResult.value;
}
}
private:
Value value;
};
//
// Translates DXGI error codes to their D3D9 equivalents so that the
// correct HRESULTs propagate to the D3D9 runtime. Non-DXGI codes pass
// through unchanged.
//
// Not an exhuastive list, but the ones most likely to be hit
//
inline HRESULT TranslateDxgiHrToD3D9(HRESULT hr)
{
switch (hr)
{
case DXGI_ERROR_DEVICE_REMOVED: return D3DERR_DEVICEREMOVED;
case DXGI_ERROR_DEVICE_HUNG: return D3DERR_DEVICEHUNG;
case DXGI_ERROR_DEVICE_RESET: return D3DERR_DEVICELOST;
case DXGI_ERROR_DRIVER_INTERNAL_ERROR: return D3DERR_DRIVERINTERNALERROR;
case DXGI_ERROR_INVALID_CALL: return D3DERR_DRIVERINTERNALERROR; // This is a driver error from the app's perspective as it means that 9on12 made an invalid call
default: return hr;
}
}
#define D3D9on12_DDI_ENTRYPOINT_START(implemented) \
__pragma(warning(suppress:4127)) /* conditional is constant due to constant macro parameter(s) */ \
if((implemented) == false && RegistryConstants::g_cBreakOnMissingDDI) \
{ \
DebugBreak();\
} \
if(RegistryConstants::g_cEnableDDISpew)\
{\
PrintDebugMessage(std::string(std::string("Command ID: ") + std::to_string(InterlockedIncrement(&g_CommandID))));\
PrintDebugMessage(WarningStrings::g_cDDIEntryHeader + std::string(__FUNCTION__));\
}\
\
HRESULT EntryPointHr = S_OK; \
try \
{ \
#define CLOSE_TRYCATCH_AND_STORE_HRESULT(hr) \
EntryPointHr = hr; \
} \
catch (_com_error& hrEx) \
{ \
EntryPointHr = hrEx.Error(); \
} \
catch (std::bad_alloc&) \
{ \
EntryPointHr = E_OUTOFMEMORY; \
} \
EntryPointHr = TranslateDxgiHrToD3D9(EntryPointHr); \
#define D3D9on12_DDI_ENTRYPOINT_END_AND_RETURN_HR(hr) \
CLOSE_TRYCATCH_AND_STORE_HRESULT(hr) \
return EntryPointHr; \
#define D3D9on12_DDI_ENTRYPOINT_END_AND_REPORT_HR(hDevice, hr) \
CLOSE_TRYCATCH_AND_STORE_HRESULT(hr) \
__pragma(warning(suppress:4127)) /* conditional is constant due to constant macro parameter(s) */ \
if(FAILED(hr)) \
{ \
Device* pDevice = Device::GetDeviceFromHandle(hDevice); \
if (pDevice) \
pDevice->ReportError(EntryPointHr); \
}
#define D3D9on12_DDI_ENTRYPOINT_END_REPORT_HR_AND_RETURN(hDevice, hr, value) \
CLOSE_TRYCATCH_AND_STORE_HRESULT(hr) \
__pragma(warning(suppress:4127)) /* conditional is constant due to constant macro parameter(s) */ \
if(FAILED(hr)) \
{ \
Device* pDevice = Device::GetDeviceFromHandle(hDevice); \
if (pDevice) \
pDevice->ReportError(EntryPointHr); \
} \
return value;
#define RETURN_E_INVALIDARG_AND_CHECK() \
{ \
if (RegistryConstants::g_cCheckOnInvalidArg) \
{ \
DebugBreak(); \
} \
return E_INVALIDARG; \
}
//
// Converts an HRESULT to an exception. This matches ThrowFailure used
// elsewhere in dxg.
//
inline void ThrowFailure(HRESULT hr)
{
if (FAILED(hr))
{
throw _com_error(hr);
}
}
static void PrintErrorText()
{
DWORD retSize;
LPTSTR pString = NULL;
retSize = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ARGUMENT_ARRAY,
NULL,
GetLastError(),
LANG_NEUTRAL,
(LPTSTR)&pString,
0,
NULL);
PrintDebugMessage(std::string(CStringA(pString)));
}
const BYTE g_redOutputPS[] =
{
0, 2, 255, 255, 254, 255,
22, 0, 67, 84, 65, 66,
28, 0, 0, 0, 35, 0,
0, 0, 0, 2, 255, 255,
0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0,
28, 0, 0, 0, 112, 115,
95, 50, 95, 48, 0, 77,
105, 99, 114, 111, 115, 111,
102, 116, 32, 40, 82, 41,
32, 72, 76, 83, 76, 32,
83, 104, 97, 100, 101, 114,
32, 67, 111, 109, 112, 105,
108, 101, 114, 32, 57, 46,
50, 55, 46, 57, 53, 50,
46, 51, 48, 50, 50, 0,
81, 0, 0, 5, 0, 0,
15, 160, 0, 0, 128, 63,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 128, 63,
1, 0, 0, 2, 0, 8,
15, 128, 0, 0, 228, 160,
255, 255, 0, 0
};
const BYTE g_passThroughVS[] =
{
0, 2, 254, 255, 254, 255,
22, 0, 67, 84, 65, 66,
28, 0, 0, 0, 35, 0,
0, 0, 0, 2, 254, 255,
0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0,
28, 0, 0, 0, 118, 115,
95, 50, 95, 48, 0, 77,
105, 99, 114, 111, 115, 111,
102, 116, 32, 40, 82, 41,
32, 72, 76, 83, 76, 32,
83, 104, 97, 100, 101, 114,
32, 67, 111, 109, 112, 105,
108, 101, 114, 32, 49, 48,
46, 48, 46, 49, 48, 48,
49, 49, 46, 48, 0, 171,
31, 0, 0, 2, 0, 0,
0, 128, 0, 0, 15, 144,
1, 0, 0, 2, 0, 0,
15, 192, 0, 0, 228, 144,
255, 255, 0, 0
};
// Can't use the std libraries implementation because the internal build tools don't have the latest std library
static inline BOOL isnan(const float& a)
{
static const UINT exponentMask = 0x7f800000;
static const UINT mantissaMask = 0x007fffff;
UINT u = *(const UINT*)&a;
return (((u & exponentMask) == exponentMask) && (u & mantissaMask)); // NaN
}
static bool isinf(const float &f)
{
static const INT32 Max32 = 0x7f7FFFFF;
UINT n = *(UINT*)&f;
UINT abs = n & 0x7FFFFFFF;
return abs > Max32;
}
static void AssertFloatNotInfOrNan(float f)
{
Check9on12(!isnan(f));
Check9on12(!isinf(f));
}
template<typename DataType>
static FORCEINLINE DataType Align(DataType location, size_t alignment)
{
return (DataType)((location + (alignment - 1)) & ~(alignment - 1));
}
template<typename DataType>
static FORCEINLINE bool IsAligned(DataType location, size_t alignment)
{
return location % alignment == 0;
}
static FORCEINLINE UINT CeilToClosestPowerOfTwo(UINT32 n)
{
// Accomplished using bit magic, only works on numbers
// won't overflow a 32 bit UINT
assert(n <= BIT(31));
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
static RECT RectThatCoversEntireResource(const D3D12_PLACED_SUBRESOURCE_FOOTPRINT &footprint)
{
RECT rect = {};
rect.right = (LONG)footprint.Footprint.Width;
rect.bottom = (LONG)footprint.Footprint.Height;
return rect;
}
static RECT RectThatCoversEntireResource(const D3D12_RESOURCE_DESC& desc)
{
RECT rect = {};
rect.right = (LONG)desc.Width;
rect.bottom = (LONG)desc.Height;
return rect;
}
static bool RectsAreEqual(const RECT& a, const RECT& b)
{
return a.bottom == b.bottom && a.left == b.left && a.right == b.right && a.top == b.top;
}
static UINT BoxWidth(const D3D12_BOX &b)
{
Check9on12(b.right >= b.left);
return b.right - b.left;
}
static UINT BoxHeight(const D3D12_BOX &b)
{
Check9on12(b.bottom >= b.top);
return b.bottom - b.top;
}
static UINT BoxDepth(const D3D12_BOX &b)
{
Check9on12(b.back >= b.front);
return b.back - b.front;
}
static UINT RectHeight(const RECT& r)
{
Check9on12(r.bottom >= r.top);
return r.bottom - r.top;
}
static UINT RectWidth(const RECT& r)
{
Check9on12(r.right >= r.left);
return r.right - r.left;
}
static bool IsRectEmpty(const RECT& r)
{
return r.left == 0 && r.right == 0 && r.top == 0 && r.bottom == 0;
}
static bool IsBoxValid(const D3D12_BOX& box)
{
return box.right > box.left && box.bottom > box.top && box.back > box.front;
}
static D3D12_BOX BoxFromDesc(const D3D12_RESOURCE_DESC &desc)
{
return CD3DX12_BOX(0, 0, 0, static_cast<LONG>(desc.Width), static_cast<LONG>(desc.Height), static_cast<LONG>(desc.DepthOrArraySize));
}
// We round down on for left and top; and we round up for
// right and bottom inline void CBaseTexture::ScaleRectDown(RECT *pRect, UINT PowersOfTwo)
static void ScaleRectDown(RECT& rect, UINT PowersOfTwo)
{
Check9on12(PowersOfTwo > 0);
Check9on12(PowersOfTwo < 32);
Check9on12(rect.right > 0);
Check9on12(rect.bottom > 0);
Check9on12(rect.left < rect.right);
Check9on12(rect.top < rect.bottom);
Check9on12(rect.left >= 0);
Check9on12(rect.top >= 0);
// Rounding down is automatic with the shift operator
rect.left >>= PowersOfTwo;
rect.top >>= PowersOfTwo;
if (rect.right & ((1 << PowersOfTwo) - 1))
{
rect.right >>= PowersOfTwo;
rect.right++;
}
else
{
rect.right >>= PowersOfTwo;
}
if (rect.bottom & ((1 << PowersOfTwo) - 1))
{
rect.bottom >>= PowersOfTwo;
rect.bottom++;
}
else
{
rect.bottom >>= PowersOfTwo;
}
}
static void ClipBox(D3D12_BOX& box, const D3D12_PLACED_SUBRESOURCE_FOOTPRINT &footprint)
{
if (BoxWidth(box) > footprint.Footprint.Width)
{
box.right = box.left + footprint.Footprint.Width;
}
if (BoxHeight(box) > footprint.Footprint.Height)
{
box.bottom = box.left + footprint.Footprint.Height;
}
if (BoxDepth(box) > footprint.Footprint.Depth)
{
box.back = box.front + footprint.Footprint.Depth;
}
}
static void ScaleBoxDown(D3D12_BOX& box, UINT PowersOfTwo)
{
Check9on12(box.front < box.back);
Check9on12(box.back > 0);
RECT tempRect = {};
tempRect.left = box.left;
tempRect.right = box.right;
tempRect.top = box.top;
tempRect.bottom = box.bottom;
ScaleRectDown(tempRect, PowersOfTwo);
box.left= tempRect.left;
box.right = tempRect.right;
box.top = tempRect.top;
box.bottom = tempRect.bottom;
// Rounding down is automatic with the shift operator
box.front >>= PowersOfTwo;
if (box.back & ((1 << PowersOfTwo) - 1))
{
box.back >>= PowersOfTwo;
box.back++;
}
else
{
box.back >>= PowersOfTwo;
}
}
static bool IsLumaPlane(UINT PlaneIndex)
{
return PlaneIndex == 0;
}
static bool IsChromaPlane(UINT PlaneIndex)
{
return !IsLumaPlane(PlaneIndex);
}
static RECT ConvertLumaRectToChromaRect(const RECT& lumaRect, UINT subsampleX, UINT subsampleY)
{
RECT chromaRect;
chromaRect.left = (lumaRect.left + subsampleX - 1) / subsampleX;
chromaRect.right = (lumaRect.right + subsampleX - 1) / subsampleX;
chromaRect.top = (lumaRect.top + subsampleY - 1) / subsampleY;
chromaRect.bottom = (lumaRect.bottom + subsampleY - 1) / subsampleY;
return chromaRect;
}
static D3DDDI_UNLOCKFLAGS ConvertUnlockAsyncFlag(const D3DDDI_UNLOCKASYNCFLAGS &asyncFlag)
{
D3DDDI_UNLOCKFLAGS unlockFlag = {};
unlockFlag.NotifyOnly = asyncFlag.NotifyOnly;
return unlockFlag;
}
static D3DDDI_LOCKFLAGS ConvertLockAsyncFlags(const D3DDDI_LOCKASYNCFLAGS &asyncFlag)
{
D3DDDI_LOCKFLAGS lockFlag = {};
// TODO: Can we do anything with this info?
// NoExistingReferences : 1; // 0x00000020
lockFlag.NoOverwrite = asyncFlag.NoOverwrite;
lockFlag.Discard = asyncFlag.Discard;
lockFlag.RangeValid = asyncFlag.RangeValid;
lockFlag.AreaValid = asyncFlag.AreaValid;
lockFlag.BoxValid = asyncFlag.BoxValid;
lockFlag.NotifyOnly = asyncFlag.NotifyOnly;
lockFlag.WriteOnly = TRUE;
return lockFlag;
}
static SECURITY_ATTRIBUTES ConvertSecurityDescriptorToSecurityAttributes(const SECURITY_DESCRIPTOR *pDesc)
{
SECURITY_ATTRIBUTES attributes = {};
attributes.lpSecurityDescriptor = (LPVOID)pDesc;
return attributes;
}
// Clip the src/dst rects for a copy based on a boundingRect (usually either a dirty rect or resource boundaries)
static void ClipCopyRectsWithBoudingRect(RECT &sourceRect, RECT &destRect, RECT &boundingRect)
{
const FLOAT widthScalingFactor = (FLOAT)RectWidth(destRect) / (FLOAT)RectWidth(sourceRect);
const FLOAT heightScalingFactor = (FLOAT)RectHeight(destRect) / (FLOAT)RectHeight(sourceRect);
// Clamp any negative values
boundingRect.left = max(0l, boundingRect.left);
boundingRect.top = max(0l, boundingRect.top);
// Using ceil for all floating point operation here is what allows us to be "conformant" for the HLK Present2.exe
if (sourceRect.left < boundingRect.left)
{
const FLOAT clippedAmount = (FLOAT)(boundingRect.left - sourceRect.left);
sourceRect.left = boundingRect.left;
destRect.left += (LONG)ceil(clippedAmount * widthScalingFactor);
}
if (sourceRect.right > boundingRect.right)
{
const FLOAT clippedAmount = (FLOAT)(sourceRect.right - boundingRect.right);
sourceRect.right = boundingRect.right;
destRect.right -= (LONG)ceil(clippedAmount * widthScalingFactor);
}
if (sourceRect.top < boundingRect.top)
{
const FLOAT clippedAmount = (FLOAT)(boundingRect.top - sourceRect.top);
sourceRect.top = boundingRect.top;
destRect.top += (LONG)ceil(clippedAmount * heightScalingFactor);
}
if (sourceRect.bottom > boundingRect.bottom)
{
const FLOAT clippedAmount = (FLOAT)(sourceRect.bottom - boundingRect.bottom);
sourceRect.bottom = boundingRect.bottom;
destRect.bottom -= (LONG)ceil(clippedAmount * heightScalingFactor);
}
}
// Clips rects being used for a copy so that they fit DX12 requirements. PairedRect is the other source/destination
// rect corresponding to rectToClip
static void ClipCopyRect(RECT &rectToClip, RECT &pairedRect, INT resourceWidth, INT resourceHeight)
{
RECT resourceBounds;
resourceBounds.left = 0;
resourceBounds.top = 0;
resourceBounds.right = resourceWidth;
resourceBounds.bottom = resourceHeight;
ClipCopyRectsWithBoudingRect(rectToClip, pairedRect, resourceBounds);
}
static const D3D12TranslationLayer::EQueryType g_cNoD3D12EquivalentQuery = (D3D12TranslationLayer::EQueryType) - 1;
static D3D12TranslationLayer::EQueryType ConvertQueryType(D3DDDIQUERYTYPE type)
{
switch (type)
{
case D3DDDIQUERYTYPE_OCCLUSION:
return D3D12TranslationLayer::e_QUERY_OCCLUSION;
case D3DDDIQUERYTYPE_TIMESTAMP:
return D3D12TranslationLayer::e_QUERY_TIMESTAMP;
case D3DDDIQUERYTYPE_TIMESTAMPDISJOINT:
return D3D12TranslationLayer::e_QUERY_TIMESTAMPDISJOINT;
case D3DDDIQUERYTYPE_EVENT:
return D3D12TranslationLayer::e_QUERY_EVENT;
case D3DDDIQUERYTYPE_TIMESTAMPFREQ:
case D3DDDIQUERYTYPE_VCACHE:
case D3DDDIQUERYTYPE_RESOURCEMANAGER:
case D3DDDIQUERYTYPE_VERTEXSTATS:
case D3DDDIQUERYTYPE_DDISTATS:
case D3DDDIQUERYTYPE_PIPELINETIMINGS:
case D3DDDIQUERYTYPE_INTERFACETIMINGS:
case D3DDDIQUERYTYPE_VERTEXTIMINGS:
case D3DDDIQUERYTYPE_PIXELTIMINGS:
case D3DDDIQUERYTYPE_BANDWIDTHTIMINGS:
case D3DDDIQUERYTYPE_CACHEUTILIZATION:
case D3DDDIQUERYTYPE_COUNTER_DEVICE_DEPENDENT:
default:
Check9on12(false);
break;
}
return g_cNoD3D12EquivalentQuery;
}
static D3D12TranslationLayer::EShaderStage ConvertFrom9on12ShaderType(ShaderType shaderType)
{
switch (shaderType )
{
case D3D9on12::VERTEX_SHADER:
return D3D12TranslationLayer::e_VS;
case D3D9on12::GEOMETRY_SHADER:
return D3D12TranslationLayer::e_GS;
case D3D9on12::PIXEL_SHADER:
return D3D12TranslationLayer::e_PS;
case D3D9on12::NUM_SHADER_TYPES:
default:
Check9on12(false);
return D3D12TranslationLayer::e_VS;
}
}
static D3D12_TEXTURE_ADDRESS_MODE ConvertToD3D12TextureAddress(D3DTEXTUREADDRESS d3dTextureAddress)
{
switch (d3dTextureAddress)
{
case D3DTADDRESS_WRAP:
return D3D12_TEXTURE_ADDRESS_MODE_WRAP;
case D3DTADDRESS_MIRROR:
return D3D12_TEXTURE_ADDRESS_MODE_MIRROR;
case D3DTADDRESS_CLAMP:
return D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
case D3DTADDRESS_BORDER:
return D3D12_TEXTURE_ADDRESS_MODE_BORDER;
case D3DTADDRESS_MIRRORONCE:
return D3D12_TEXTURE_ADDRESS_MODE_MIRROR_ONCE;
default:
Check9on12(false);
return (D3D12_TEXTURE_ADDRESS_MODE)-1;
}
}
static D3D12_FILTER ConvertToD3D12ComparisonFilter(D3D12_FILTER filter)
{
switch (filter)
{
case D3D12_FILTER_MIN_MAG_MIP_POINT :
return D3D12_FILTER_COMPARISON_MIN_MAG_MIP_POINT;
case D3D12_FILTER_MIN_MAG_POINT_MIP_LINEAR :
return D3D12_FILTER_COMPARISON_MIN_MAG_POINT_MIP_LINEAR;
case D3D12_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT :
return D3D12_FILTER_COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT;
case D3D12_FILTER_MIN_POINT_MAG_MIP_LINEAR :
return D3D12_FILTER_COMPARISON_MIN_POINT_MAG_MIP_LINEAR;
case D3D12_FILTER_MIN_LINEAR_MAG_MIP_POINT :
return D3D12_FILTER_COMPARISON_MIN_LINEAR_MAG_MIP_POINT;
case D3D12_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
return D3D12_FILTER_COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR;
case D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT :
return D3D12_FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT;
case D3D12_FILTER_MIN_MAG_MIP_LINEAR :
return D3D12_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR;
case D3D12_FILTER_ANISOTROPIC :
return D3D12_FILTER_COMPARISON_ANISOTROPIC;
case D3D12_FILTER_MIN_MAG_ANISOTROPIC_MIP_POINT :
return D3D12_FILTER_COMPARISON_MIN_MAG_ANISOTROPIC_MIP_POINT;
default:
Check9on12(false);
return (D3D12_FILTER)-1;
}
}
static D3D12_FILTER ConvertToD3D12Filter(
D3DTEXTUREFILTERTYPE magFilter,
D3DTEXTUREFILTERTYPE minFilter,
D3DTEXTUREFILTERTYPE mipFilter,
bool supportAnisoPointMip)
{
if ((D3DTEXF_ANISOTROPIC == minFilter)
|| (D3DTEXF_ANISOTROPIC == magFilter))
{
return (mipFilter == D3DTEXF_POINT && supportAnisoPointMip) ?
D3D12_FILTER_MIN_MAG_ANISOTROPIC_MIP_POINT :
D3D12_FILTER_ANISOTROPIC;
}
switch (mipFilter)
{
case D3DTEXF_POINT:
if ((D3DTEXF_POINT == minFilter)
&& (D3DTEXF_POINT == magFilter))
{
return D3D12_FILTER_MIN_MAG_MIP_POINT;
}
if ((D3DTEXF_POINT == minFilter)
&& (D3DTEXF_LINEAR == magFilter))
{
return D3D12_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT;
}
if ((D3DTEXF_LINEAR == minFilter)
&& (D3DTEXF_POINT == magFilter))
{
return D3D12_FILTER_MIN_LINEAR_MAG_MIP_POINT;
}
if ((D3DTEXF_LINEAR == minFilter)
&& (D3DTEXF_LINEAR == magFilter))
{
return D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT;
}
break;
default:
// D3D12 doesn't support a "none", filter. Instead we fake this by disabling mips outside of mip 0 via SRVs.
// It's important that we pick LINEAR for the mip filter instead of POINT because hardware that does LOD
// clamping BEFORE determining whether to minify vs magnify (QC), will have a 0.5 rounding factor to the LOD
// when doing POINT. This is enough to change the decision on whether to minify/magnify. This causes multiple
// issues in the DX9 HCK tests.
//
// Note DX9 tests assumed clamping happened after the minify/magnify decision, with contradicts the DX11 HW spec
// that says it's undefined when clamping happens. This means that 9on12 can't properly support tests that try to
// verify mip:point + mismatching min/mag filters match exactly with the ref rast. These tests have been modified to
// no longer have this requirement
case D3DTEXF_NONE:
case D3DTEXF_LINEAR:
if ((D3DTEXF_POINT == minFilter)
&& (D3DTEXF_POINT == magFilter))
{
return D3D12_FILTER_MIN_MAG_POINT_MIP_LINEAR;
}
if ((D3DTEXF_POINT == minFilter)
&& (D3DTEXF_LINEAR == magFilter))
{
return D3D12_FILTER_MIN_POINT_MAG_MIP_LINEAR;
}
if ((D3DTEXF_LINEAR == minFilter)
&& (D3DTEXF_POINT == magFilter))
{
return D3D12_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR;
}
if ((D3DTEXF_LINEAR == minFilter)
&& (D3DTEXF_LINEAR == magFilter))
{
return D3D12_FILTER_MIN_MAG_MIP_LINEAR;
}
break;
}
return D3D12_FILTER_MIN_MAG_MIP_POINT;
}
static D3D12_FILTER ConvertToD3D12SamplerFilter(
D3DTEXTUREFILTERTYPE magFilter,
D3DTEXTUREFILTERTYPE minFilter,
D3DTEXTUREFILTERTYPE mipFilter,
bool comparisonEnabled,
bool supportAnisoPointMip)
{
D3D12_FILTER filter = ConvertToD3D12Filter(magFilter, minFilter, mipFilter, supportAnisoPointMip);
if (comparisonEnabled)
{
filter = ConvertToD3D12ComparisonFilter(filter);
}
return filter;
}
static D3D12_STENCIL_OP ConvertToD3D12StencilOp(D3DSTENCILOP d3dStencilOp)
{
switch (d3dStencilOp)
{
default:
Check9on12(false);
return (D3D12_STENCIL_OP)-1;
case D3DSTENCILOP_KEEP:
return D3D12_STENCIL_OP_KEEP;
case D3DSTENCILOP_ZERO:
return D3D12_STENCIL_OP_ZERO;
case D3DSTENCILOP_REPLACE:
return D3D12_STENCIL_OP_REPLACE;
case D3DSTENCILOP_INCRSAT:
return D3D12_STENCIL_OP_INCR_SAT;
case D3DSTENCILOP_DECRSAT:
return D3D12_STENCIL_OP_DECR_SAT;
case D3DSTENCILOP_INVERT:
return D3D12_STENCIL_OP_INVERT;
case D3DSTENCILOP_INCR:
return D3D12_STENCIL_OP_INCR;
case D3DSTENCILOP_DECR:
return D3D12_STENCIL_OP_DECR;
}
}
static D3D12_COMPARISON_FUNC ConvertToD3D12ComparisonFunc(D3DCMPFUNC d3dFunc)
{
switch (d3dFunc)
{
default:
Check9on12(false);
return (D3D12_COMPARISON_FUNC)-1;
case D3DCMP_NEVER:
return D3D12_COMPARISON_FUNC_NEVER;
case D3DCMP_LESS:
return D3D12_COMPARISON_FUNC_LESS;
case D3DCMP_EQUAL:
return D3D12_COMPARISON_FUNC_EQUAL;
case D3DCMP_LESSEQUAL:
return D3D12_COMPARISON_FUNC_LESS_EQUAL;
case D3DCMP_GREATER:
return D3D12_COMPARISON_FUNC_GREATER;
case D3DCMP_NOTEQUAL:
return D3D12_COMPARISON_FUNC_NOT_EQUAL;
case D3DCMP_GREATEREQUAL:
return D3D12_COMPARISON_FUNC_GREATER_EQUAL;
case D3DCMP_ALWAYS:
return D3D12_COMPARISON_FUNC_ALWAYS;
}
}
static D3D12_BLEND_OP ConvertToD3D12BlendOp(D3DBLENDOP d3dBlendOp)
{
switch (d3dBlendOp)
{
default:
Check9on12(false);
// This is still an unexpected scenario, but we've encountered at least
// one app that hits this (World of Warplanes). To avoid failing PSO creation,
// we're falling through to a 'reasonable' fallback.
case D3DBLENDOP_ADD:
return D3D12_BLEND_OP_ADD;
case D3DBLENDOP_SUBTRACT:
return D3D12_BLEND_OP_SUBTRACT;
case D3DBLENDOP_REVSUBTRACT:
return D3D12_BLEND_OP_REV_SUBTRACT;
case D3DBLENDOP_MIN:
return D3D12_BLEND_OP_MIN;
case D3DBLENDOP_MAX:
return D3D12_BLEND_OP_MAX;
}
}
static D3D12_BLEND ConvertToD3D12Blend(D3DBLEND d3dBlend)
{
switch (d3dBlend)
{
default:
case D3DBLEND_ZERO:
return D3D12_BLEND_ZERO;
case D3DBLEND_ONE:
return D3D12_BLEND_ONE;
case D3DBLEND_SRCCOLOR:
return D3D12_BLEND_SRC_COLOR;
case D3DBLEND_INVSRCCOLOR:
return D3D12_BLEND_INV_SRC_COLOR;
case D3DBLEND_SRCALPHA:
return D3D12_BLEND_SRC_ALPHA;
case D3DBLEND_INVSRCALPHA:
return D3D12_BLEND_INV_SRC_ALPHA;
case D3DBLEND_DESTALPHA:
return D3D12_BLEND_DEST_ALPHA;
case D3DBLEND_INVDESTALPHA:
return D3D12_BLEND_INV_DEST_ALPHA;
case D3DBLEND_DESTCOLOR:
return D3D12_BLEND_DEST_COLOR;
case D3DBLEND_INVDESTCOLOR:
return D3D12_BLEND_INV_DEST_COLOR;
case D3DBLEND_SRCALPHASAT:
return D3D12_BLEND_SRC_ALPHA_SAT;
case D3DBLEND_BLENDFACTOR:
return D3D12_BLEND_BLEND_FACTOR;
case D3DBLEND_INVBLENDFACTOR:
return D3D12_BLEND_INV_BLEND_FACTOR;
case D3DBLEND_SRCCOLOR2:
return D3D12_BLEND_SRC1_COLOR;
case D3DBLEND_INVSRCCOLOR2:
return D3D12_BLEND_INV_SRC1_COLOR;
}
}
// In DX9 "COLOR" referred to both COLOR and ALPHA, but in DX12 COLOR specifically
// means just the color and not the alpha. Convert anything that says "COLOR"
// to just it's "ALPHA" variant
static D3D12_BLEND ConvertToD3D12AlphaBlend(D3DBLEND d3dBlend)
{
switch (d3dBlend)
{
default:
case D3DBLEND_ZERO:
return D3D12_BLEND_ZERO;
case D3DBLEND_ONE:
return D3D12_BLEND_ONE;
case D3DBLEND_SRCCOLOR:
case D3DBLEND_SRCALPHA:
return D3D12_BLEND_SRC_ALPHA;
case D3DBLEND_INVSRCCOLOR:
case D3DBLEND_INVSRCALPHA:
return D3D12_BLEND_INV_SRC_ALPHA;
case D3DBLEND_DESTALPHA:
case D3DBLEND_DESTCOLOR:
return D3D12_BLEND_DEST_ALPHA;
case D3DBLEND_INVDESTALPHA:
case D3DBLEND_INVDESTCOLOR:
return D3D12_BLEND_INV_DEST_ALPHA;
case D3DBLEND_SRCALPHASAT:
return D3D12_BLEND_SRC_ALPHA_SAT;
case D3DBLEND_BLENDFACTOR:
return D3D12_BLEND_BLEND_FACTOR;
case D3DBLEND_INVBLENDFACTOR:
return D3D12_BLEND_INV_BLEND_FACTOR;
case D3DBLEND_SRCCOLOR2:
return D3D12_BLEND_SRC1_ALPHA;
case D3DBLEND_INVSRCCOLOR2:
return D3D12_BLEND_INV_SRC1_ALPHA;
}
}
static bool D3D12BlendAppliesToAlpha(D3D12_BLEND d3dBlend)
{
switch (d3dBlend)
{
case D3D12_BLEND_SRC_COLOR:
case D3D12_BLEND_INV_SRC_COLOR:
case D3D12_BLEND_DEST_COLOR:
case D3D12_BLEND_INV_DEST_COLOR:
case D3D12_BLEND_SRC1_COLOR:
case D3D12_BLEND_INV_SRC1_COLOR:
return false;
case D3D12_BLEND_SRC_ALPHA:
case D3D12_BLEND_INV_SRC_ALPHA:
case D3D12_BLEND_DEST_ALPHA:
case D3D12_BLEND_INV_DEST_ALPHA:
case D3D12_BLEND_SRC_ALPHA_SAT:
case D3D12_BLEND_BLEND_FACTOR:
case D3D12_BLEND_INV_BLEND_FACTOR:
case D3D12_BLEND_ZERO:
case D3D12_BLEND_ONE:
case D3D12_BLEND_SRC1_ALPHA:
case D3D12_BLEND_INV_SRC1_ALPHA:
return true;
default:
Check9on12(FALSE);
return false;
}
}
static DirectX::XMFLOAT4 ARGBToUNORMFloat(D3DCOLOR color)
{
DirectX::XMFLOAT4 output;
output.x = float((color >> 16) & 0xff) / 255.0f;
output.y = float((color >> 8) & 0xff) / 255.0f;
output.z = float(color & 0xff) / 255.0f;
output.w = float((color >> 24) & 0xff) / 255.0f;
return output;
}
static D3DFORMAT ConvertDDIFormatToAPI(D3DDDIFORMAT format)
{
// DDI format is equivalent to the API format
return (D3DFORMAT)format;
}
static DXGI_FORMAT ConvertToSRGB(DXGI_FORMAT Format)
{
const DXGI_FORMAT *pFormatSet = CD3D11FormatHelper::GetFormatCastSet(Format);
for (; *pFormatSet != DXGI_FORMAT_UNKNOWN; pFormatSet++)
{
if (CD3D11FormatHelper::IsSRGBFormat(*pFormatSet))
{
return *pFormatSet;
}
}
return DXGI_FORMAT_UNKNOWN;
}
static bool IsSRGBCompatible(DXGI_FORMAT Format)
{
return ConvertToSRGB(Format) != DXGI_FORMAT_UNKNOWN;
}
static DXGI_FORMAT ConvertToTypeless(DXGI_FORMAT format)
{
return CD3D11FormatHelper::GetParentFormat(format);
}
static bool IsBlockCompressedFormat(DXGI_FORMAT format)
{
return CD3D11FormatHelper::IsBlockCompressFormat(format);
}
static bool IsYUVFormat(DXGI_FORMAT format)
{
return CD3D11FormatHelper::YUV(format);
}
static bool IsDepthStencilFormat(DXGI_FORMAT format)
{
return CD3D11FormatHelper::GetComponentName(format, 0) == D3D11FCN_D;
}
static DXGI_FORMAT ConvertDepthFormatToCompanionSRVFormat(DXGI_FORMAT format)
{
Check9on12(IsDepthStencilFormat(format));
D3D11_FORMAT_COMPONENT_INTERPRETATION componentInterpretation = CD3D11FormatHelper::GetFormatComponentInterpretation(format, 0);
const DXGI_FORMAT *pFormatSet = CD3D11FormatHelper::GetFormatCastSet(format);
for (; *pFormatSet != DXGI_FORMAT_UNKNOWN; pFormatSet++)
{
if (CD3D11FormatHelper::GetTypeLevel(*pFormatSet) == D3D11_FORMAT_TYPE_LEVEL::D3D11FTL_FULL_TYPE &&
CD3D11FormatHelper::GetComponentName(*pFormatSet, 0) == D3D11FCN_R &&