1 /*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef GrD3DUtil_DEFINED
9 #define GrD3DUtil_DEFINED
10
11 #include "include/core/SkImage.h"
12 #include "include/gpu/GrTypes.h"
13 #include "include/gpu/d3d/GrD3DTypes.h"
14 #include "include/private/GrTypesPriv.h"
15
16 #define GR_D3D_CALL_ERRCHECK(X) \
17 do { \
18 HRESULT result = X; \
19 SkASSERT(SUCCEEDED(result)); \
20 if (!SUCCEEDED(result)) { \
21 SkDebugf("Failed Direct3D call. Error: 0x%08lx\n", result); \
22 } \
23 } while (false)
24
25 static constexpr bool operator==(const D3D12_CPU_DESCRIPTOR_HANDLE & first,
26 const D3D12_CPU_DESCRIPTOR_HANDLE & second) {
27 return first.ptr == second.ptr;
28 }
29
30 /**
31 * Returns true if the format is compressed.
32 */
33 bool GrDxgiFormatIsCompressed(DXGI_FORMAT);
34
GrDxgiFormatChannels(DXGI_FORMAT format)35 static constexpr uint32_t GrDxgiFormatChannels(DXGI_FORMAT format) {
36 switch (format) {
37 case DXGI_FORMAT_R8G8B8A8_UNORM: return kRGBA_SkColorChannelFlags;
38 case DXGI_FORMAT_R8_UNORM: return kRed_SkColorChannelFlag;
39 case DXGI_FORMAT_B8G8R8A8_UNORM: return kRGBA_SkColorChannelFlags;
40 case DXGI_FORMAT_B5G6R5_UNORM: return kRGB_SkColorChannelFlags;
41 case DXGI_FORMAT_R16G16B16A16_FLOAT: return kRGBA_SkColorChannelFlags;
42 case DXGI_FORMAT_R16_FLOAT: return kRed_SkColorChannelFlag;
43 case DXGI_FORMAT_R8G8_UNORM: return kRG_SkColorChannelFlags;
44 case DXGI_FORMAT_R10G10B10A2_UNORM: return kRGBA_SkColorChannelFlags;
45 case DXGI_FORMAT_B4G4R4A4_UNORM: return kRGBA_SkColorChannelFlags;
46 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: return kRGBA_SkColorChannelFlags;
47 case DXGI_FORMAT_BC1_UNORM: return kRGBA_SkColorChannelFlags;
48 case DXGI_FORMAT_R16_UNORM: return kRed_SkColorChannelFlag;
49 case DXGI_FORMAT_R16G16_UNORM: return kRG_SkColorChannelFlags;
50 case DXGI_FORMAT_R16G16B16A16_UNORM: return kRGBA_SkColorChannelFlags;
51 case DXGI_FORMAT_R16G16_FLOAT: return kRG_SkColorChannelFlags;
52 case DXGI_FORMAT_D24_UNORM_S8_UINT: return 0;
53 case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: return 0;
54
55 default: return 0;
56 }
57 }
58
GrDxgiFormatDesc(DXGI_FORMAT format)59 static constexpr GrColorFormatDesc GrDxgiFormatDesc(DXGI_FORMAT format) {
60 switch (format) {
61 case DXGI_FORMAT_R8G8B8A8_UNORM:
62 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm);
63 case DXGI_FORMAT_R8_UNORM:
64 return GrColorFormatDesc::MakeR(8, GrColorTypeEncoding::kUnorm);
65 case DXGI_FORMAT_B8G8R8A8_UNORM:
66 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm);
67 case DXGI_FORMAT_B5G6R5_UNORM:
68 return GrColorFormatDesc::MakeRGB(5, 6, 5, GrColorTypeEncoding::kUnorm);
69 case DXGI_FORMAT_R16G16B16A16_FLOAT:
70 return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kFloat);
71 case DXGI_FORMAT_R16_FLOAT:
72 return GrColorFormatDesc::MakeR(16, GrColorTypeEncoding::kFloat);
73 case DXGI_FORMAT_R8G8_UNORM:
74 return GrColorFormatDesc::MakeRG(8, GrColorTypeEncoding::kUnorm);
75 case DXGI_FORMAT_R10G10B10A2_UNORM:
76 return GrColorFormatDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm);
77 case DXGI_FORMAT_B4G4R4A4_UNORM:
78 return GrColorFormatDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm);
79 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
80 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kSRGBUnorm);
81 case DXGI_FORMAT_R16_UNORM:
82 return GrColorFormatDesc::MakeR(16, GrColorTypeEncoding::kUnorm);
83 case DXGI_FORMAT_R16G16_UNORM:
84 return GrColorFormatDesc::MakeRG(16, GrColorTypeEncoding::kUnorm);
85 case DXGI_FORMAT_R16G16B16A16_UNORM:
86 return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kUnorm);
87 case DXGI_FORMAT_R16G16_FLOAT:
88 return GrColorFormatDesc::MakeRG(16, GrColorTypeEncoding::kFloat);
89
90 // Compressed texture formats are not expected to have a description.
91 case DXGI_FORMAT_BC1_UNORM: return GrColorFormatDesc::MakeInvalid();
92
93 // This type only describes color channels.
94 case DXGI_FORMAT_D24_UNORM_S8_UINT: return GrColorFormatDesc::MakeInvalid();
95 case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: return GrColorFormatDesc::MakeInvalid();
96
97 default: return GrColorFormatDesc::MakeInvalid();
98 }
99 }
100
GrDxgiFormatBytesPerBlock(DXGI_FORMAT format)101 static constexpr size_t GrDxgiFormatBytesPerBlock(DXGI_FORMAT format) {
102 switch (format) {
103 case DXGI_FORMAT_R8G8B8A8_UNORM: return 4;
104 case DXGI_FORMAT_R8_UNORM: return 1;
105 case DXGI_FORMAT_B8G8R8A8_UNORM: return 4;
106 case DXGI_FORMAT_B5G6R5_UNORM: return 2;
107 case DXGI_FORMAT_R16G16B16A16_FLOAT: return 8;
108 case DXGI_FORMAT_R16_FLOAT: return 2;
109 case DXGI_FORMAT_R8G8_UNORM: return 2;
110 case DXGI_FORMAT_R10G10B10A2_UNORM: return 4;
111 case DXGI_FORMAT_B4G4R4A4_UNORM: return 2;
112 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: return 4;
113 case DXGI_FORMAT_BC1_UNORM: return 8;
114 case DXGI_FORMAT_R16_UNORM: return 2;
115 case DXGI_FORMAT_R16G16_UNORM: return 4;
116 case DXGI_FORMAT_R16G16B16A16_UNORM: return 8;
117 case DXGI_FORMAT_R16G16_FLOAT: return 4;
118 case DXGI_FORMAT_D24_UNORM_S8_UINT: return 4;
119 case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: return 8;
120
121 default: return 0;
122 }
123 }
124
GrDxgiFormatStencilBits(DXGI_FORMAT format)125 static constexpr int GrDxgiFormatStencilBits(DXGI_FORMAT format) {
126 switch (format) {
127 case DXGI_FORMAT_D24_UNORM_S8_UINT:
128 return 8;
129 case DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
130 return 8;
131 default:
132 return 0;
133 }
134 }
135
136 #if defined(SK_DEBUG) || GR_TEST_UTILS
GrDxgiFormatToStr(DXGI_FORMAT dxgiFormat)137 static constexpr const char* GrDxgiFormatToStr(DXGI_FORMAT dxgiFormat) {
138 switch (dxgiFormat) {
139 case DXGI_FORMAT_R8G8B8A8_UNORM: return "R8G8B8A8_UNORM";
140 case DXGI_FORMAT_R8_UNORM: return "R8_UNORM";
141 case DXGI_FORMAT_B8G8R8A8_UNORM: return "B8G8R8A8_UNORM";
142 case DXGI_FORMAT_B5G6R5_UNORM: return "R5G6B5_UNORM";
143 case DXGI_FORMAT_R16G16B16A16_FLOAT: return "R16G16B16A16_FLOAT";
144 case DXGI_FORMAT_R16_FLOAT: return "R16_FLOAT";
145 case DXGI_FORMAT_R8G8_UNORM: return "R8G8_UNORM";
146 case DXGI_FORMAT_R10G10B10A2_UNORM: return "R10G10B10A2_UNORM";
147 case DXGI_FORMAT_B4G4R4A4_UNORM: return "B4G4R4A4_UNORM";
148 case DXGI_FORMAT_R32G32B32A32_FLOAT: return "R32G32B32A32_FLOAT";
149 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: return "R8G8B8A8_UNORM_SRGB";
150 case DXGI_FORMAT_BC1_UNORM: return "BC1_UNORM";
151 case DXGI_FORMAT_R16_UNORM: return "R16_UNORM";
152 case DXGI_FORMAT_R16G16_UNORM: return "R16G16_UNORM";
153 case DXGI_FORMAT_R16G16B16A16_UNORM: return "R16G16B16A16_UNORM";
154 case DXGI_FORMAT_R16G16_FLOAT: return "R16G16_FLOAT";
155 case DXGI_FORMAT_D24_UNORM_S8_UINT: return "D24_UNORM_S8_UINT";
156 case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: return "D32_FLOAT_S8X24_UINT";
157
158 default: return "Unknown";
159 }
160 }
161 #endif
162
163 #endif
164