Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
DawnGraphiteUtils.cpp
Go to the documentation of this file.
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
10
17
18namespace skgpu::graphite {
19
20namespace ContextFactory {
21std::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
42bool DawnFormatIsDepthOrStencil(wgpu::TextureFormat format) {
43 switch (format) {
44 case wgpu::TextureFormat::Stencil8: // fallthrough
45 case wgpu::TextureFormat::Depth32Float:
46 case wgpu::TextureFormat::Depth24PlusStencil8:
47 case wgpu::TextureFormat::Depth32FloatStencil8:
48 return true;
49 default:
50 return false;
51 }
52}
53
54bool DawnFormatIsDepth(wgpu::TextureFormat format) {
55 switch (format) {
56 case wgpu::TextureFormat::Depth32Float:
57 case wgpu::TextureFormat::Depth24PlusStencil8:
58 case wgpu::TextureFormat::Depth32FloatStencil8:
59 return true;
60 default:
61 return false;
62 }
63}
64
65bool DawnFormatIsStencil(wgpu::TextureFormat format) {
66 switch (format) {
67 case wgpu::TextureFormat::Stencil8: // fallthrough
68 case wgpu::TextureFormat::Depth24PlusStencil8:
69 case wgpu::TextureFormat::Depth32FloatStencil8:
70 return true;
71 default:
72 return false;
73 }
74}
75
77 // TODO: Decide if we want to change this to always return a combined depth and stencil format
78 // to allow more sharing of depth stencil allocations.
79 if (mask == DepthStencilFlags::kDepth) {
80 // wgpu::TextureFormatDepth16Unorm is also a universally supported option here
81 return wgpu::TextureFormat::Depth32Float;
82 } else if (mask == DepthStencilFlags::kStencil) {
83 return wgpu::TextureFormat::Stencil8;
84 } else if (mask == DepthStencilFlags::kDepthStencil) {
85 return wgpu::TextureFormat::Depth24PlusStencil8;
86 }
87 SkASSERT(false);
88 return wgpu::TextureFormat::Undefined;
89}
90
91static bool check_shader_module(wgpu::ShaderModule* module,
92 const char* shaderText,
93 ShaderErrorHandler* errorHandler) {
94 // Prior to emsdk 3.1.51 wgpu::ShaderModule::GetCompilationInfo is unimplemented.
95#if defined(__EMSCRIPTEN__) && \
96 ((__EMSCRIPTEN_major__ < 3 || \
97 (__EMSCRIPTEN_major__ == 3 && __EMSCRIPTEN_minor__ < 1) || \
98 (__EMSCRIPTEN_major__ == 3 && __EMSCRIPTEN_minor__ == 1 && __EMSCRIPTEN_tiny__ < 51)))
99 return true;
100#endif
101 struct Handler {
102 static void Fn(WGPUCompilationInfoRequestStatus status,
103 const WGPUCompilationInfo* info,
104 void* userdata) {
105 Handler* self = reinterpret_cast<Handler*>(userdata);
106 SkASSERT(status == WGPUCompilationInfoRequestStatus_Success);
107
108 // Walk the message list and check for hard errors.
109 self->fSuccess = true;
110 for (size_t index = 0; index < info->messageCount; ++index) {
111 const WGPUCompilationMessage& entry = info->messages[index];
112 if (entry.type == WGPUCompilationMessageType_Error) {
113 self->fSuccess = false;
114 break;
115 }
116 }
117
118 // If we found a hard error, report the compilation messages to the error handler.
119 if (!self->fSuccess) {
120 std::string errors;
121 for (size_t index = 0; index < info->messageCount; ++index) {
122 const WGPUCompilationMessage& entry = info->messages[index];
123 errors += "line " +
124 std::to_string(entry.lineNum) + ':' +
125 std::to_string(entry.linePos) + ' ' +
126 entry.message + '\n';
127 }
128 self->fErrorHandler->compileError(
129 self->fShaderText, errors.c_str(), /*shaderWasCached=*/false);
130 }
131 }
132
133 const char* fShaderText;
134 ShaderErrorHandler* fErrorHandler;
135 bool fSuccess = false;
136 };
137
138 Handler handler;
139 handler.fShaderText = shaderText;
140 handler.fErrorHandler = errorHandler;
141 module->GetCompilationInfo(&Handler::Fn, &handler);
142
143 return handler.fSuccess;
144}
145
147 const char* label,
148 const std::string& wgsl,
149 wgpu::ShaderModule* module,
150 ShaderErrorHandler* errorHandler) {
151 wgpu::ShaderModuleWGSLDescriptor wgslDesc;
152 wgslDesc.code = wgsl.c_str();
153
154 wgpu::ShaderModuleDescriptor desc;
155 desc.nextInChain = &wgslDesc;
156 desc.label = label;
157
158 *module = sharedContext->device().CreateShaderModule(&desc);
159
160 return check_shader_module(module, wgsl.c_str(), errorHandler);
161}
162
163} // namespace skgpu::graphite
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
#define SkASSERT(cond)
Definition SkAssert.h:116
T * get() const
Definition SkRefCnt.h:303
static std::unique_ptr< Context > MakeContext(sk_sp< SharedContext >, std::unique_ptr< QueueManager >, const ContextOptions &)
Definition Context.cpp:926
static sk_sp< SharedContext > Make(const DawnBackendContext &, const ContextOptions &)
uint32_t uint32_t * format
SK_API std::unique_ptr< Context > MakeDawn(const DawnBackendContext &, const ContextOptions &)
static bool check_shader_module(wgpu::ShaderModule *module, const char *shaderText, ShaderErrorHandler *errorHandler)
wgpu::TextureFormat DawnDepthStencilFlagsToFormat(SkEnumBitMask< DepthStencilFlags > mask)
bool DawnFormatIsDepthOrStencil(wgpu::TextureFormat format)
bool DawnFormatIsDepth(wgpu::TextureFormat format)
bool DawnCompileWGSLShaderModule(const DawnSharedContext *sharedContext, const char *label, const std::string &wgsl, wgpu::ShaderModule *module, ShaderErrorHandler *errorHandler)
bool DawnFormatIsStencil(wgpu::TextureFormat format)