Debian Codegraph @

src/gpu/graphite/dawn/DawnGraphiteUtils.cpp

raw
1 /*
2 * Copyright 2022 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 #include "src/gpu/graphite/dawn/DawnGraphiteUtils.h"
9
10 #include "include/gpu/ShaderErrorHandler.h"
11 #include "include/gpu/graphite/Context.h"
12 #include "include/gpu/graphite/dawn/DawnBackendContext.h"
13 #include "src/gpu/graphite/ContextPriv.h"
14 #include "src/gpu/graphite/TextureFormat.h"
15 #include "src/gpu/graphite/dawn/DawnQueueManager.h"
16 #include "src/gpu/graphite/dawn/DawnSharedContext.h"
17
18 namespace skgpu::graphite {
19
20 namespace ContextFactory {
21 std::unique_ptr<Context> MakeDawn(const DawnBackendContext& backendContext,
22 const ContextOptions& options) {
23 sk_sp<SharedContext> sharedContext = DawnSharedContext::Make(backendContext, options);
24 if (!sharedContext) {
25 return nullptr;
26 }
27
28 auto queueManager =
29 std::make_unique<DawnQueueManager>(backendContext.fQueue, sharedContext.get());
30 if (!queueManager) {
31 return nullptr;
32 }
33
34 auto context = ContextCtorAccessor::MakeContext(std::move(sharedContext),
35 std::move(queueManager),
36 options);
37 SkASSERT(context);
38 return context;
39 }
40 } // namespace ContextFactory
41
42 SkTextureCompressionType DawnFormatToCompressionType(wgpu::TextureFormat format) {
43 switch (format) {
44 case wgpu::TextureFormat::ETC2RGB8Unorm: return SkTextureCompressionType::kETC2_RGB8_UNORM;
45 case wgpu::TextureFormat::BC1RGBAUnorm: return SkTextureCompressionType::kBC1_RGBA8_UNORM;
46 default: return SkTextureCompressionType::kNone;
47 }
48 }
49
50 #define DAWN_FORMAT_MAPPING(M) \
51 M(TextureFormat::kR8, wgpu::TextureFormat::R8Unorm) \
52 /*TextureFormat::kR16 Native-only */ \
53 M(TextureFormat::kR16F, wgpu::TextureFormat::R16Float) \
54 M(TextureFormat::kR32F, wgpu::TextureFormat::R32Float) \
55 /*TextureFormat::kA8 unsupported */ \
56 M(TextureFormat::kRG8, wgpu::TextureFormat::RG8Unorm) \
57 /*TextureFormat::kR16 Native-only */ \
58 M(TextureFormat::kRG16F, wgpu::TextureFormat::RG16Float) \
59 M(TextureFormat::kRG32F, wgpu::TextureFormat::RG32Float) \
60 /*TextureFormat::kRGB8 unsupported */ \
61 /*TextureFormat::kBGR8 unsupported */ \
62 /*TextureFormat::kB5_G6_R5, unsupported */ \
63 /*TextureFormat::kR5_G6_B5 unsupported */ \
64 /*TextureFormat::kRGB16 unsupported */ \
65 /*TextureFormat::kRGB16F unsupported */ \
66 /*TextureFormat::kRGB32F unsupported */ \
67 /*TextureFormat::kRGB8_sRGB unsupported */ \
68 /*TextureFormat::kBGR10_XR unsupported */ \
69 M(TextureFormat::kRGBA8, wgpu::TextureFormat::RGBA8Unorm) \
70 /*TextureFormat::kRGBA16 Native-only */ \
71 M(TextureFormat::kRGBA16F, wgpu::TextureFormat::RGBA16Float) \
72 M(TextureFormat::kRGBA32F, wgpu::TextureFormat::RGBA32Float) \
73 M(TextureFormat::kRGB10_A2, wgpu::TextureFormat::RGB10A2Unorm) \
74 /*TextureFormat::kRGBA10x6 unsupported */ \
75 M(TextureFormat::kRGBA8_sRGB, wgpu::TextureFormat::RGBA8UnormSrgb) \
76 M(TextureFormat::kBGRA8, wgpu::TextureFormat::BGRA8Unorm) \
77 /*TextureFormat::kBGR10_A2 unsupported */ \
78 M(TextureFormat::kBGRA8_sRGB, wgpu::TextureFormat::BGRA8UnormSrgb) \
79 /*TextureFormat::kABGR4, unsupported */ \
80 /*TextureFormat::kARGB4 unsupported */ \
81 /*TextureFormat::kBGRA10x6_XR unsupported */ \
82 M(TextureFormat::kRGB8_ETC2, wgpu::TextureFormat::ETC2RGB8Unorm) \
83 M(TextureFormat::kRGB8_ETC2_sRGB, wgpu::TextureFormat::ETC2RGB8UnormSrgb) \
84 /*TextureFormat::kRGB_BC1 unsupported */ \
85 M(TextureFormat::kRGBA8_BC1, wgpu::TextureFormat::BC1RGBAUnorm) \
86 M(TextureFormat::kRGBA8_BC1_sRGB, wgpu::TextureFormat::BC1RGBAUnormSrgb) \
87 /*TextureFormat::kYUV8_P2_420 Native-only */ \
88 /*TextureFormat::kYUV8_P3_420 unsupported */ \
89 /*TextureFormat::kYUV10x6_P2_420 Native-only */ \
90 /*TextureFormat::kExternal Native-only */ \
91 M(TextureFormat::kS8, wgpu::TextureFormat::Stencil8) \
92 M(TextureFormat::kD16, wgpu::TextureFormat::Depth16Unorm) \
93 M(TextureFormat::kD32F, wgpu::TextureFormat::Depth32Float) \
94 M(TextureFormat::kD24_S8, wgpu::TextureFormat::Depth24PlusStencil8) \
95 M(TextureFormat::kD32F_S8, wgpu::TextureFormat::Depth32FloatStencil8) \
96
97 #if !defined(__EMSCRIPTEN__)
98 #define DAWN_FORMAT_MAPPING_NATIVE_ONLY(M) \
99 M(TextureFormat::kR16, wgpu::TextureFormat::R16Unorm) \
100 M(TextureFormat::kRG16, wgpu::TextureFormat::RG16Unorm) \
101 M(TextureFormat::kRGBA16, wgpu::TextureFormat::RGBA16Unorm) \
102 M(TextureFormat::kYUV8_P2_420, wgpu::TextureFormat::R8BG8Biplanar420Unorm) \
103 M(TextureFormat::kYUV10x6_P2_420, wgpu::TextureFormat::R10X6BG10X6Biplanar420Unorm) \
104 M(TextureFormat::kExternal, wgpu::TextureFormat::External)
105 #else
106 #define DAWN_FORMAT_MAPPING_NATIVE_ONLY(M)
107 #endif
108
109 TextureFormat DawnFormatToTextureFormat(wgpu::TextureFormat format) {
110 #define M(TF, WGPU) case WGPU: return TF;
111 switch(format) {
112 DAWN_FORMAT_MAPPING(M)
113 DAWN_FORMAT_MAPPING_NATIVE_ONLY(M)
114 default: return TextureFormat::kUnsupported;
115 }
116 #undef M
117 }
118 wgpu::TextureFormat TextureFormatToDawnFormat(TextureFormat format) {
119 #define M(TF, WGPU) case TF: return WGPU;
120 switch(format) {
121 DAWN_FORMAT_MAPPING(M)
122 DAWN_FORMAT_MAPPING_NATIVE_ONLY(M)
123 default: return wgpu::TextureFormat::Undefined;
124 }
125 #undef M
126 }
127
128 static bool check_shader_module([[maybe_unused]] const DawnSharedContext* sharedContext,
129 wgpu::ShaderModule* module,
130 const char* shaderText,
131 ShaderErrorHandler* errorHandler) {
132 // Prior to emsdk 3.1.51 wgpu::ShaderModule::GetCompilationInfo is unimplemented.
133 #if defined(__EMSCRIPTEN__) && \
134 ((__EMSCRIPTEN_major__ < 3 || \
135 (__EMSCRIPTEN_major__ == 3 && __EMSCRIPTEN_minor__ < 1) || \
136 (__EMSCRIPTEN_major__ == 3 && __EMSCRIPTEN_minor__ == 1 && __EMSCRIPTEN_tiny__ < 51)))
137 return true;
138 #else
139 struct Handler {
140 static void Fn(WGPUCompilationInfoRequestStatus status,
141 const WGPUCompilationInfo* info,
142 void* userdata) {
143 Handler* self = reinterpret_cast<Handler*>(userdata);
144 SkASSERT(status == WGPUCompilationInfoRequestStatus_Success);
145
146 // Walk the message list and check for hard errors.
147 self->fSuccess = true;
148 for (size_t index = 0; index < info->messageCount; ++index) {
149 const WGPUCompilationMessage& entry = info->messages[index];
150 if (entry.type == WGPUCompilationMessageType_Error) {
151 self->fSuccess = false;
152 break;
153 }
154 }
155
156 // If we found a hard error, report the compilation messages to the error handler.
157 if (!self->fSuccess) {
158 std::string errors;
159 for (size_t index = 0; index < info->messageCount; ++index) {
160 const WGPUCompilationMessage& entry = info->messages[index];
161 std::string messageString(entry.message.data, entry.message.length);
162 errors += "line " + std::to_string(entry.lineNum) + ':' +
163 std::to_string(entry.linePos) + ' ' + messageString + '\n';
164 }
165 self->fErrorHandler->compileError(
166 self->fShaderText, errors.c_str(), /*shaderWasCached=*/false);
167 }
168 }
169
170 const char* fShaderText;
171 ShaderErrorHandler* fErrorHandler;
172 bool fSuccess = false;
173 };
174
175 Handler handler;
176 handler.fShaderText = shaderText;
177 handler.fErrorHandler = errorHandler;
178 #if defined(__EMSCRIPTEN__)
179 // Deprecated function.
180 module->GetCompilationInfo(&Handler::Fn, &handler);
181 #else
182 // New API.
183 wgpu::FutureWaitInfo waitInfo{};
184 waitInfo.future = module->GetCompilationInfo(
185 wgpu::CallbackMode::WaitAnyOnly,
186 [handlerPtr = &handler](wgpu::CompilationInfoRequestStatus status,
187 const wgpu::CompilationInfo* info) {
188 Handler::Fn(static_cast<WGPUCompilationInfoRequestStatus>(status),
189 reinterpret_cast<const WGPUCompilationInfo*>(info),
190 handlerPtr);
191 });
192
193 const auto& instance = static_cast<const DawnSharedContext*>(sharedContext)->instance();
194 [[maybe_unused]] auto status =
195 instance.WaitAny(1, &waitInfo, /*timeoutNS=*/std::numeric_limits<uint64_t>::max());
196 SkASSERT(status == wgpu::WaitStatus::Success);
197 #endif // defined(__EMSCRIPTEN__)
198
199 return handler.fSuccess;
200 #endif
201 }
202
203 bool DawnCompileWGSLShaderModule(const DawnSharedContext* sharedContext,
204 const char* label,
205 const SkSL::NativeShader& wgsl,
206 wgpu::ShaderModule* module,
207 ShaderErrorHandler* errorHandler) {
208 #if defined(__EMSCRIPTEN__)
209 wgpu::ShaderModuleWGSLDescriptor wgslDesc;
210 #else
211 wgpu::ShaderSourceWGSL wgslDesc;
212 #endif
213 wgslDesc.code = wgsl.fText.c_str();
214
215 wgpu::ShaderModuleDescriptor desc;
216 desc.nextInChain = &wgslDesc;
217 if (sharedContext->caps()->setBackendLabels()) {
218 desc.label = label;
219 }
220
221 *module = sharedContext->device().CreateShaderModule(&desc);
222
223 return check_shader_module(sharedContext, module, wgsl.fText.c_str(), errorHandler);
224 }
225
226 #if !defined(__EMSCRIPTEN__)
227
228 namespace {
229
230 static constexpr int kUsesExternalFormatBits = 1;
231 static constexpr int kYcbcrModelBits = 3;
232 static constexpr int kYcbcrRangeBits = 1;
233 static constexpr int kXChromaOffsetBits = 1;
234 static constexpr int kYChromaOffsetBits = 1;
235 // wgpu::FilterMode contains Undefined/Nearest/Linear entries (Linear is 2).
236 static constexpr int kChromaFilterBits = 2;
237 static constexpr int kForceExplicitReconBits = 1;
238 static constexpr int kComponentBits = 3;
239
240 static constexpr int kUsesExternalFormatShift = 0;
241 static constexpr int kYcbcrModelShift = kUsesExternalFormatShift + kUsesExternalFormatBits;
242 static constexpr int kYcbcrRangeShift = kYcbcrModelShift + kYcbcrModelBits;
243 static constexpr int kXChromaOffsetShift = kYcbcrRangeShift + kYcbcrRangeBits;
244 static constexpr int kYChromaOffsetShift = kXChromaOffsetShift + kXChromaOffsetBits;
245 static constexpr int kChromaFilterShift = kYChromaOffsetShift + kYChromaOffsetBits;
246 static constexpr int kForceExplicitReconShift = kChromaFilterShift + kChromaFilterBits;
247 static constexpr int kComponentRShift = kForceExplicitReconShift + kForceExplicitReconBits;
248 static constexpr int kComponentGShift = kComponentRShift + kComponentBits;
249 static constexpr int kComponentBShift = kComponentGShift + kComponentBits;
250 static constexpr int kComponentAShift = kComponentBShift + kComponentBits;
251
252 static constexpr uint32_t kUseExternalFormatMask =
253 ((1 << kUsesExternalFormatBits) - 1) << kUsesExternalFormatShift;
254 static constexpr uint32_t kYcbcrModelMask =
255 ((1 << kYcbcrModelBits) - 1) << kYcbcrModelShift;
256 static constexpr uint32_t kYcbcrRangeMask =
257 ((1 << kYcbcrRangeBits) - 1) << kYcbcrRangeShift;
258 static constexpr uint32_t kXChromaOffsetMask =
259 ((1 << kXChromaOffsetBits) - 1) << kXChromaOffsetShift;
260 static constexpr uint32_t kYChromaOffsetMask =
261 ((1 << kYChromaOffsetBits) - 1) << kYChromaOffsetShift;
262 static constexpr uint32_t kChromaFilterMask =
263 ((1 << kChromaFilterBits) - 1) << kChromaFilterShift;
264 static constexpr uint32_t kForceExplicitReconMask =
265 ((1 << kForceExplicitReconBits) - 1) << kForceExplicitReconShift;
266 static constexpr uint32_t kComponentRMask = ((1 << kComponentBits) - 1) << kComponentRShift;
267 static constexpr uint32_t kComponentBMask = ((1 << kComponentBits) - 1) << kComponentBShift;
268 static constexpr uint32_t kComponentGMask = ((1 << kComponentBits) - 1) << kComponentGShift;
269 static constexpr uint32_t kComponentAMask = ((1 << kComponentBits) - 1) << kComponentAShift;
270
271 } // anonymous namespace
272
273 bool DawnDescriptorsAreEquivalent(const wgpu::YCbCrVkDescriptor& desc1,
274 const wgpu::YCbCrVkDescriptor& desc2) {
275 return desc1.vkFormat == desc2.vkFormat &&
276 desc1.vkYCbCrModel == desc2.vkYCbCrModel &&
277 desc1.vkYCbCrRange == desc2.vkYCbCrRange &&
278 desc1.vkComponentSwizzleRed == desc2.vkComponentSwizzleRed &&
279 desc1.vkComponentSwizzleGreen == desc2.vkComponentSwizzleGreen &&
280 desc1.vkComponentSwizzleBlue == desc2.vkComponentSwizzleBlue &&
281 desc1.vkComponentSwizzleAlpha == desc2.vkComponentSwizzleAlpha &&
282 desc1.vkXChromaOffset == desc2.vkXChromaOffset &&
283 desc1.vkYChromaOffset == desc2.vkYChromaOffset &&
284 desc1.vkChromaFilter == desc2.vkChromaFilter &&
285 desc1.forceExplicitReconstruction == desc2.forceExplicitReconstruction &&
286 desc1.externalFormat == desc2.externalFormat;
287 }
288
289 bool DawnDescriptorIsValid(const wgpu::YCbCrVkDescriptor& desc) {
290 static const wgpu::YCbCrVkDescriptor kDefaultYcbcrDescriptor = {};
291 return !DawnDescriptorsAreEquivalent(desc, kDefaultYcbcrDescriptor);
292 }
293
294 bool DawnDescriptorUsesExternalFormat(const wgpu::YCbCrVkDescriptor& desc) {
295 SkASSERT(desc.externalFormat != 0 || desc.vkFormat != 0);
296 return desc.externalFormat != 0;
297 }
298
299 ImmutableSamplerInfo DawnDescriptorToImmutableSamplerInfo(const wgpu::YCbCrVkDescriptor& desc) {
300 static_assert(kComponentAShift + kComponentBits <= 32);
301 SkASSERT(desc.vkYCbCrModel < (1u << kYcbcrModelBits ));
302 SkASSERT(desc.vkYCbCrRange < (1u << kYcbcrRangeBits ));
303 SkASSERT(desc.vkXChromaOffset < (1u << kXChromaOffsetBits ));
304 SkASSERT(desc.vkYChromaOffset < (1u << kYChromaOffsetBits ));
305 SkASSERT(static_cast<uint32_t>(desc.vkChromaFilter) < (1u << kChromaFilterBits ));
306 SkASSERT(desc.vkComponentSwizzleRed < (1u << kComponentBits ));
307 SkASSERT(desc.vkComponentSwizzleGreen < (1u << kComponentBits ));
308 SkASSERT(desc.vkComponentSwizzleBlue < (1u << kComponentBits ));
309 SkASSERT(desc.vkComponentSwizzleAlpha < (1u << kComponentBits ));
310 SkASSERT(static_cast<uint32_t>(desc.forceExplicitReconstruction)
311 < (1u << kForceExplicitReconBits));
312
313 const bool usesExternalFormat = DawnDescriptorUsesExternalFormat(desc);
314
315 ImmutableSamplerInfo info;
316 info.fNonFormatYcbcrConversionInfo =
317 (((uint32_t)(usesExternalFormat ) << kUsesExternalFormatShift) |
318 ((uint32_t)(desc.vkYCbCrModel ) << kYcbcrModelShift ) |
319 ((uint32_t)(desc.vkYCbCrRange ) << kYcbcrRangeShift ) |
320 ((uint32_t)(desc.vkXChromaOffset ) << kXChromaOffsetShift ) |
321 ((uint32_t)(desc.vkYChromaOffset ) << kYChromaOffsetShift ) |
322 ((uint32_t)(desc.vkChromaFilter ) << kChromaFilterShift ) |
323 ((uint32_t)(desc.forceExplicitReconstruction ) << kForceExplicitReconShift) |
324 ((uint32_t)(desc.vkComponentSwizzleRed ) << kComponentRShift ) |
325 ((uint32_t)(desc.vkComponentSwizzleGreen ) << kComponentGShift ) |
326 ((uint32_t)(desc.vkComponentSwizzleBlue ) << kComponentBShift ) |
327 ((uint32_t)(desc.vkComponentSwizzleAlpha ) << kComponentAShift ));
328 info.fFormat = usesExternalFormat ? desc.externalFormat : desc.vkFormat;
329 return info;
330 }
331
332 wgpu::YCbCrVkDescriptor DawnDescriptorFromImmutableSamplerInfo(ImmutableSamplerInfo info) {
333 const uint32_t nonFormatInfo = info.fNonFormatYcbcrConversionInfo;
334
335 wgpu::YCbCrVkDescriptor desc;
336 const bool usesExternalFormat =
337 (nonFormatInfo >> kUsesExternalFormatShift) & kUseExternalFormatMask;
338 if (usesExternalFormat) {
339 desc.vkFormat = 0;
340 desc.externalFormat = info.fFormat;
341 } else {
342 desc.vkFormat = (uint32_t) info.fFormat;
343 desc.externalFormat = 0;
344 }
345
346 desc.vkYCbCrModel = (nonFormatInfo & kYcbcrModelMask) >> kYcbcrModelShift;
347 desc.vkYCbCrRange = (nonFormatInfo & kYcbcrRangeMask) >> kYcbcrRangeShift;
348 desc.vkComponentSwizzleRed = (nonFormatInfo & kComponentRMask) >> kComponentRShift;
349 desc.vkComponentSwizzleGreen = (nonFormatInfo & kComponentGMask) >> kComponentGShift;
350 desc.vkComponentSwizzleBlue = (nonFormatInfo & kComponentBMask) >> kComponentBShift;
351 desc.vkComponentSwizzleAlpha = (nonFormatInfo & kComponentAMask) >> kComponentAShift;
352 desc.vkXChromaOffset = (nonFormatInfo & kXChromaOffsetMask) >> kXChromaOffsetShift;
353 desc.vkYChromaOffset = (nonFormatInfo & kYChromaOffsetMask) >> kYChromaOffsetShift;
354 desc.vkChromaFilter = static_cast<wgpu::FilterMode>(
355 (nonFormatInfo & kChromaFilterMask) >> kChromaFilterShift);
356 desc.forceExplicitReconstruction =
357 (nonFormatInfo & kForceExplicitReconMask) >> kForceExplicitReconShift;
358 return desc;
359 }
360
361 #endif // !defined(__EMSCRIPTEN__)
362
363 } // namespace skgpu::graphite