Skip to content

Commit dfe0e70

Browse files
[WebGPU, Dawn]: Upgrade Dawn version and added unorm texture type for WebGPU
1 parent 3474816 commit dfe0e70

18 files changed

Lines changed: 381 additions & 354 deletions

BuildTools/CMake/BuildUtils.cmake

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -466,24 +466,29 @@ function(add_format_validation_target MODULE_NAME MODULE_ROOT_PATH IDE_FOLDER)
466466

467467
endfunction()
468468

469-
# FetchContent's GIT_SHALLOW option is buggy and does not actually do a shallow
470-
# clone. This macro takes care of it.
471-
macro(FetchContent_DeclareShallowGit Name GIT_REPOSITORY GitRepository GIT_TAG GitTag)
469+
# The GIT_SHALLOW option in FetchContent is buggy and does not actually perform
470+
# a shallow clone. This macro fixes that. Also, we do not want to clone all
471+
# submodules to reduce download time, therefore we add an option GIT_SUBMODULES
472+
# to specify which submodules should be cloned (if any).
473+
macro(FetchContent_DeclareShallowGit Name GIT_REPOSITORY GitRepository GIT_TAG GitTag GIT_SUBMODULES GitSubmodules)
472474
include(FetchContent)
473-
FetchContent_Declare(
474-
"${Name}"
475475

476-
# This is what it'd look like if GIT_SHALLOW was indeed working:
477-
#GIT_REPOSITORY "${GitRepository}"
478-
#GIT_TAG "${GitTag}"
479-
#GIT_SHALLOW ON
476+
# This is what it'd look like if GIT_SHALLOW was indeed working:
477+
#GIT_REPOSITORY "${GitRepository}"
478+
#GIT_TAG "${GitTag}"
479+
#GIT_SHALLOW ON
480+
#GIT_SUBMODULES "${GitSubmodules}"
480481

481-
# Manual download mode instead:
482+
# Manual download mode instead:
483+
FetchContent_Declare(
484+
${Name}
482485
DOWNLOAD_COMMAND
483486
cd "${FETCHCONTENT_BASE_DIR}/${Name}-src" &&
484487
git init &&
485-
git fetch --depth=1 "${GitRepository}" "${GitTag}" &&
486-
git reset --hard FETCH_HEAD
488+
git remote add origin "${GitRepository}" &&
489+
git fetch --depth=1 origin "${GitTag}" &&
490+
git checkout FETCH_HEAD &&
491+
git submodule update --init --recursive --depth=1 ${GitSubmodules}
487492
)
488493
endmacro()
489494

Graphics/GraphicsEngineWebGPU/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ PRIVATE
179179
)
180180

181181
if (PLATFORM_WEB)
182-
target_link_options(Diligent-GraphicsEngineWebGPU-static PUBLIC "SHELL: -s USE_WEBGPU=1 -s USE_PTHREADS=1")
182+
target_link_options(Diligent-GraphicsEngineWebGPU-static PUBLIC "SHELL: --use-port=emdawnwebgpu -s USE_PTHREADS=1")
183+
target_compile_options(Diligent-GraphicsEngineWebGPU-static PUBLIC "--use-port=emdawnwebgpu")
184+
add_compile_options(
185+
-I${EMSCRIPTEN_ROOT_PATH}/cache/ports/emdawnwebgpu/emdawnwebgpu_pkg/webgpu/include
186+
)
183187
endif()
184188

185189
target_compile_definitions(Diligent-GraphicsEngineWebGPU-shared PUBLIC DILIGENT_WEBGPU_SHARED=1)

Graphics/GraphicsEngineWebGPU/include/WebGPUStubs.hpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,5 @@
2929
#if PLATFORM_WEB
3030

3131
inline constexpr WGPUFeatureName WGPUFeatureName_ChromiumExperimentalTimestampQueryInsidePasses = static_cast<WGPUFeatureName>(0x000003EE);
32-
inline constexpr WGPUFeatureName WGPUFeatureName_Unorm16TextureFormats = static_cast<WGPUFeatureName>(0x000003FB);
33-
inline constexpr WGPUFeatureName WGPUFeatureName_Snorm16TextureFormats = static_cast<WGPUFeatureName>(0x000003FC);
34-
35-
inline constexpr WGPUTextureFormat WGPUTextureFormat_R16Unorm = static_cast<WGPUTextureFormat>(0x00000060);
36-
inline constexpr WGPUTextureFormat WGPUTextureFormat_R16Snorm = static_cast<WGPUTextureFormat>(0x00000063);
37-
inline constexpr WGPUTextureFormat WGPUTextureFormat_RG16Unorm = static_cast<WGPUTextureFormat>(0x00000061);
38-
inline constexpr WGPUTextureFormat WGPUTextureFormat_RG16Snorm = static_cast<WGPUTextureFormat>(0x00000064);
39-
inline constexpr WGPUTextureFormat WGPUTextureFormat_RGBA16Unorm = static_cast<WGPUTextureFormat>(0x00000062);
40-
inline constexpr WGPUTextureFormat WGPUTextureFormat_RGBA16Snorm = static_cast<WGPUTextureFormat>(0x00000065);
41-
42-
inline constexpr WGPUSurfaceGetCurrentTextureStatus WGPUSurfaceGetCurrentTextureStatus_Error = static_cast<WGPUSurfaceGetCurrentTextureStatus>(0x00000007);
4332

4433
#endif

Graphics/GraphicsEngineWebGPU/include/pch.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,8 @@
3939

4040
using WGPUOptionalBool = bool;
4141
using WGPUShaderSourceWGSL = WGPUShaderModuleWGSLDescriptor;
42-
using WGPUStringView = const char*;
4342
using WGPUSurfaceSourceCanvasHTMLSelector_Emscripten = WGPUSurfaceDescriptorFromCanvasHTMLSelector;
4443

45-
constexpr bool WGPUOptionalBool_True = true;
46-
constexpr bool WGPUOptionalBool_False = false;
4744

4845
constexpr WGPUSType WGPUSType_ShaderSourceWGSL = WGPUSType_ShaderModuleWGSLDescriptor;
4946
constexpr WGPUSType WGPUSType_SurfaceSourceCanvasHTMLSelector_Emscripten = WGPUSType_SurfaceDescriptorFromCanvasHTMLSelector;

Graphics/GraphicsEngineWebGPU/src/DeviceContextWebGPUImpl.cpp

Lines changed: 90 additions & 86 deletions
Large diffs are not rendered by default.

Graphics/GraphicsEngineWebGPU/src/EngineFactoryWebGPU.cpp

Lines changed: 77 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,19 @@ std::vector<WebGPUAdapterWrapper> FindCompatibleAdapters(WGPUInstance wgpuInstan
147147

148148
struct CallbackUserData
149149
{
150-
WGPUAdapter Adapter = nullptr;
151-
WGPURequestAdapterStatus RequestStatus = {};
152-
String Message = {};
153-
bool IsReady = {};
150+
WGPUAdapter wgpuAdapter = nullptr;
151+
WGPURequestAdapterStatus wgpuRequestStatus = {};
152+
String Message = {};
153+
bool IsReady = {};
154154
};
155155

156-
auto OnAdapterRequestEnded = [](WGPURequestAdapterStatus Status, WGPUAdapter Adapter, WGPUStringView Message, void* pCallbackUserData) {
157-
if (pCallbackUserData != nullptr)
156+
auto OnAdapterRequestEnded = [](WGPURequestAdapterStatus wgpuStatus, WGPUAdapter wgpuAdapter, WGPUStringView Message, void* pUserData0, void* pUserData1) {
157+
if (pUserData0 != nullptr)
158158
{
159-
CallbackUserData* pUserData = static_cast<CallbackUserData*>(pCallbackUserData);
160-
pUserData->Adapter = Adapter;
161-
pUserData->RequestStatus = Status;
162-
pUserData->IsReady = true;
159+
CallbackUserData* pUserData = static_cast<CallbackUserData*>(pUserData0);
160+
pUserData->wgpuAdapter = wgpuAdapter;
161+
pUserData->wgpuRequestStatus = wgpuStatus;
162+
pUserData->IsReady = true;
163163
if (WGPUStringViewValid(Message))
164164
pUserData->Message = WGPUStringViewToString(Message);
165165
}
@@ -169,27 +169,31 @@ std::vector<WebGPUAdapterWrapper> FindCompatibleAdapters(WGPUInstance wgpuInstan
169169
WGPUPowerPreference_HighPerformance,
170170
WGPUPowerPreference_LowPower};
171171

172-
for (const WGPUPowerPreference& powerPreference : PowerPreferences)
172+
for (const WGPUPowerPreference& PowerPreference : PowerPreferences)
173173
{
174174
CallbackUserData UserData{};
175175

176-
WGPURequestAdapterOptions Options{};
177-
Options.powerPreference = powerPreference;
178-
Options.backendType = WGPUBackendType_Undefined;
179-
Options.forceFallbackAdapter = false;
180-
Options.compatibilityMode = false;
181-
wgpuInstanceRequestAdapter(wgpuInstance, &Options, OnAdapterRequestEnded, &UserData);
176+
WGPURequestAdapterOptions wgpuAdapterRequestOptions{};
177+
wgpuAdapterRequestOptions.powerPreference = PowerPreference;
178+
wgpuAdapterRequestOptions.backendType = WGPUBackendType_Undefined;
179+
wgpuAdapterRequestOptions.forceFallbackAdapter = false;
180+
181+
WGPURequestAdapterCallbackInfo wgpuAdapterRequestCallbackInfo{};
182+
wgpuAdapterRequestCallbackInfo.callback = OnAdapterRequestEnded;
183+
wgpuAdapterRequestCallbackInfo.mode = WGPUCallbackMode_AllowSpontaneous;
184+
wgpuAdapterRequestCallbackInfo.userdata1 = &UserData;
185+
wgpuInstanceRequestAdapter(wgpuInstance, &wgpuAdapterRequestOptions, wgpuAdapterRequestCallbackInfo);
182186

183187
while (!UserData.IsReady)
184188
InstancePoolEvents(wgpuInstance);
185189

186-
if (UserData.RequestStatus == WGPURequestAdapterStatus_Success)
190+
if (UserData.wgpuRequestStatus == WGPURequestAdapterStatus_Success)
187191
{
188-
auto adapter_it = std::find_if(wgpuAdapters.begin(), wgpuAdapters.end(),
189-
[&](const auto& wgpuAdapter) { return wgpuAdapter.Get() == UserData.Adapter; });
192+
auto AdapterIter = std::find_if(wgpuAdapters.begin(), wgpuAdapters.end(),
193+
[&](const auto& wgpuAdapter) { return wgpuAdapter.Get() == UserData.wgpuAdapter; });
190194

191-
if (adapter_it == wgpuAdapters.end())
192-
wgpuAdapters.emplace_back(UserData.Adapter);
195+
if (AdapterIter == wgpuAdapters.end())
196+
wgpuAdapters.emplace_back(UserData.wgpuAdapter);
193197
}
194198
else
195199
{
@@ -200,32 +204,23 @@ std::vector<WebGPUAdapterWrapper> FindCompatibleAdapters(WGPUInstance wgpuInstan
200204
return wgpuAdapters;
201205
}
202206

203-
static void DeviceLostCallback(WGPUDeviceLostReason Reason,
207+
static void DeviceLostCallback(WGPUDevice const* wgpuDevice,
208+
WGPUDeviceLostReason wgpuReason,
204209
WGPUStringView Message,
205-
void* userdata)
210+
void* userdata,
211+
void* userdata2)
206212
{
207-
bool Expression = Reason != WGPUDeviceLostReason_Destroyed;
208-
#if !PLATFORM_WEB
209-
Expression &= (Reason != WGPUDeviceLostReason_InstanceDropped);
210-
#endif
213+
bool Expression = wgpuReason != WGPUDeviceLostReason_Destroyed;
214+
Expression &= (wgpuReason != WGPUDeviceLostReason_CallbackCancelled);
215+
211216
if (Expression && WGPUStringViewValid(Message))
212217
{
213218
LOG_DEBUG_MESSAGE(DEBUG_MESSAGE_SEVERITY_ERROR, "WebGPU: ", WGPUStringViewToString(Message));
214219
}
215220
}
216221

217-
#if !PLATFORM_WEB
218-
static void DeviceLostCallback2(WGPUDevice const* device,
219-
WGPUDeviceLostReason Reason,
220-
WGPUStringView Message,
221-
void* userdata1,
222-
void* userdata2)
223-
{
224-
DeviceLostCallback(Reason, Message, userdata1);
225-
}
226-
227-
static void UncapturedErrorCallback2(WGPUDevice const* device,
228-
WGPUErrorType MessageType,
222+
static void UncapturedErrorCallback2(WGPUDevice const* wgpuDevice,
223+
WGPUErrorType wgpuMessageType,
229224
WGPUStringView Message,
230225
void* userdata1,
231226
void* userdata2)
@@ -235,12 +230,12 @@ static void UncapturedErrorCallback2(WGPUDevice const* device,
235230
LOG_DEBUG_MESSAGE(DEBUG_MESSAGE_SEVERITY_ERROR, "WebGPU: ", WGPUStringViewToString(Message));
236231
}
237232
}
238-
#endif
233+
239234

240235
WebGPUDeviceWrapper CreateDeviceForAdapter(const DeviceFeatures& Features, WGPUInstance wgpuInstance, WGPUAdapter wgpuAdapter)
241236
{
242-
WGPUSupportedLimits SupportedLimits{};
243-
wgpuAdapterGetLimits(wgpuAdapter, &SupportedLimits);
237+
WGPULimits wgpuSupportedLimits{};
238+
wgpuAdapterGetLimits(wgpuAdapter, &wgpuSupportedLimits);
244239

245240
std::vector<WGPUFeatureName> wgpuFeatures{};
246241
{
@@ -280,19 +275,19 @@ WebGPUDeviceWrapper CreateDeviceForAdapter(const DeviceFeatures& Features, WGPUI
280275

281276
struct CallbackUserData
282277
{
283-
WGPUDevice Device = nullptr;
284-
WGPURequestDeviceStatus RequestStatus = {};
285-
String Message = {};
286-
bool IsReady = {};
278+
WGPUDevice wgpuDevice = nullptr;
279+
WGPURequestDeviceStatus wgpuRequestStatus = {};
280+
String Message = {};
281+
bool IsReady = {};
287282
} UserData;
288283

289-
auto OnDeviceRequestEnded = [](WGPURequestDeviceStatus Status, WGPUDevice Device, WGPUStringView Message, void* pCallbackUserData) {
290-
if (pCallbackUserData != nullptr)
284+
WGPURequestDeviceCallback OnDeviceRequestEnded = [](WGPURequestDeviceStatus wgpuStatus, WGPUDevice wgpuDevice, WGPUStringView Message, void* pUserData1, void* pUserData2) {
285+
if (pUserData1 != nullptr)
291286
{
292-
CallbackUserData* pUserData = static_cast<CallbackUserData*>(pCallbackUserData);
293-
pUserData->Device = Device;
294-
pUserData->RequestStatus = Status;
295-
pUserData->IsReady = true;
287+
CallbackUserData* pUserData = static_cast<CallbackUserData*>(pUserData1);
288+
pUserData->wgpuDevice = wgpuDevice;
289+
pUserData->wgpuRequestStatus = wgpuStatus;
290+
pUserData->IsReady = true;
296291
if (WGPUStringViewValid(Message))
297292
pUserData->Message = WGPUStringViewToString(Message);
298293
}
@@ -310,28 +305,27 @@ WebGPUDeviceWrapper CreateDeviceForAdapter(const DeviceFeatures& Features, WGPUI
310305
wgpuDawnTogglesDesc.enabledToggles = ToggleNames;
311306
#endif
312307

313-
WGPURequiredLimits RequiredLimits{nullptr, SupportedLimits.limits};
308+
WGPUDeviceDescriptor wgpuDeviceDesc{};
309+
wgpuDeviceDesc.requiredLimits = &wgpuSupportedLimits;
310+
wgpuDeviceDesc.requiredFeatureCount = wgpuFeatures.size();
311+
wgpuDeviceDesc.requiredFeatures = wgpuFeatures.data();
312+
wgpuDeviceDesc.deviceLostCallbackInfo = {nullptr, WGPUCallbackMode_AllowSpontaneous, DeviceLostCallback};
313+
wgpuDeviceDesc.nextInChain = reinterpret_cast<WGPUChainedStruct*>(&wgpuDawnTogglesDesc);
314314

315-
WGPUDeviceDescriptor DeviceDesc{};
316-
DeviceDesc.requiredLimits = &RequiredLimits;
317-
DeviceDesc.requiredFeatureCount = wgpuFeatures.size();
318-
DeviceDesc.requiredFeatures = wgpuFeatures.data();
319-
#if PLATFORM_WEB
320-
DeviceDesc.deviceLostCallback = DeviceLostCallback;
321-
#else
322-
DeviceDesc.deviceLostCallbackInfo2 = {nullptr, WGPUCallbackMode_AllowSpontaneous, DeviceLostCallback2};
323-
DeviceDesc.uncapturedErrorCallbackInfo2 = {nullptr, UncapturedErrorCallback2};
324-
DeviceDesc.nextInChain = reinterpret_cast<WGPUChainedStruct*>(&wgpuDawnTogglesDesc);
325-
#endif
326-
wgpuAdapterRequestDevice(wgpuAdapter, &DeviceDesc, OnDeviceRequestEnded, &UserData);
315+
WGPURequestDeviceCallbackInfo wgpuDeviceRequestCallbackInfo{};
316+
wgpuDeviceRequestCallbackInfo.nextInChain = nullptr;
317+
wgpuDeviceRequestCallbackInfo.mode = WGPUCallbackMode_AllowSpontaneous;
318+
wgpuDeviceRequestCallbackInfo.callback = OnDeviceRequestEnded;
319+
wgpuDeviceRequestCallbackInfo.userdata1 = &UserData;
320+
wgpuAdapterRequestDevice(wgpuAdapter, &wgpuDeviceDesc, wgpuDeviceRequestCallbackInfo);
327321

328322
while (!UserData.IsReady)
329323
InstancePoolEvents(wgpuInstance);
330324

331-
if (UserData.RequestStatus != WGPURequestDeviceStatus_Success)
325+
if (UserData.wgpuRequestStatus != WGPURequestDeviceStatus_Success)
332326
LOG_ERROR_AND_THROW(UserData.Message);
333327

334-
return WebGPUDeviceWrapper{UserData.Device};
328+
return WebGPUDeviceWrapper{UserData.wgpuDevice};
335329
}
336330

337331
bool FeatureSupported(WGPUAdapter wgpuAdapter, WGPUDevice wgpuDevice, WGPUFeatureName Feature)
@@ -454,7 +448,7 @@ GraphicsAdapterInfo GetGraphicsAdapterInfo(WGPUAdapter wgpuAdapter, WGPUDevice w
454448

455449
AdapterInfo.Features = GetSupportedFeatures(wgpuAdapter, wgpuDevice);
456450

457-
WGPUSupportedLimits wgpuSupportedLimits{};
451+
WGPULimits wgpuSupportedLimits{};
458452
if (wgpuAdapter)
459453
wgpuAdapterGetLimits(wgpuAdapter, &wgpuSupportedLimits);
460454
else
@@ -491,28 +485,28 @@ GraphicsAdapterInfo GetGraphicsAdapterInfo(WGPUAdapter wgpuAdapter, WGPUDevice w
491485
{
492486
ComputeShaderProperties& ComputeShaderInfo{AdapterInfo.ComputeShader};
493487

494-
ComputeShaderInfo.MaxThreadGroupSizeX = wgpuSupportedLimits.limits.maxComputeWorkgroupSizeX;
495-
ComputeShaderInfo.MaxThreadGroupSizeY = wgpuSupportedLimits.limits.maxComputeWorkgroupSizeY;
496-
ComputeShaderInfo.MaxThreadGroupSizeZ = wgpuSupportedLimits.limits.maxComputeWorkgroupSizeZ;
488+
ComputeShaderInfo.MaxThreadGroupSizeX = wgpuSupportedLimits.maxComputeWorkgroupSizeX;
489+
ComputeShaderInfo.MaxThreadGroupSizeY = wgpuSupportedLimits.maxComputeWorkgroupSizeY;
490+
ComputeShaderInfo.MaxThreadGroupSizeZ = wgpuSupportedLimits.maxComputeWorkgroupSizeZ;
497491

498-
ComputeShaderInfo.MaxThreadGroupCountX = wgpuSupportedLimits.limits.maxComputeWorkgroupsPerDimension;
499-
ComputeShaderInfo.MaxThreadGroupCountY = wgpuSupportedLimits.limits.maxComputeWorkgroupsPerDimension;
500-
ComputeShaderInfo.MaxThreadGroupCountZ = wgpuSupportedLimits.limits.maxComputeWorkgroupsPerDimension;
492+
ComputeShaderInfo.MaxThreadGroupCountX = wgpuSupportedLimits.maxComputeWorkgroupsPerDimension;
493+
ComputeShaderInfo.MaxThreadGroupCountY = wgpuSupportedLimits.maxComputeWorkgroupsPerDimension;
494+
ComputeShaderInfo.MaxThreadGroupCountZ = wgpuSupportedLimits.maxComputeWorkgroupsPerDimension;
501495

502-
ComputeShaderInfo.SharedMemorySize = wgpuSupportedLimits.limits.maxComputeWorkgroupStorageSize;
503-
ComputeShaderInfo.MaxThreadGroupInvocations = wgpuSupportedLimits.limits.maxComputeInvocationsPerWorkgroup;
496+
ComputeShaderInfo.SharedMemorySize = wgpuSupportedLimits.maxComputeWorkgroupStorageSize;
497+
ComputeShaderInfo.MaxThreadGroupInvocations = wgpuSupportedLimits.maxComputeInvocationsPerWorkgroup;
504498
}
505499

506500
// Set texture info
507501
{
508502
TextureProperties& TextureInfo{AdapterInfo.Texture};
509503

510504
TextureInfo.MaxTexture1DArraySlices = 0; // Not supported in WebGPU
511-
TextureInfo.MaxTexture2DArraySlices = wgpuSupportedLimits.limits.maxTextureArrayLayers;
505+
TextureInfo.MaxTexture2DArraySlices = wgpuSupportedLimits.maxTextureArrayLayers;
512506

513-
TextureInfo.MaxTexture1DDimension = wgpuSupportedLimits.limits.maxTextureDimension1D;
514-
TextureInfo.MaxTexture2DDimension = wgpuSupportedLimits.limits.maxTextureDimension2D;
515-
TextureInfo.MaxTexture3DDimension = wgpuSupportedLimits.limits.maxTextureDimension3D;
507+
TextureInfo.MaxTexture1DDimension = wgpuSupportedLimits.maxTextureDimension1D;
508+
TextureInfo.MaxTexture2DDimension = wgpuSupportedLimits.maxTextureDimension2D;
509+
TextureInfo.MaxTexture3DDimension = wgpuSupportedLimits.maxTextureDimension3D;
516510

517511
TextureInfo.Texture2DMSSupported = True;
518512
TextureInfo.Texture2DMSArraySupported = False;
@@ -524,8 +518,8 @@ GraphicsAdapterInfo GetGraphicsAdapterInfo(WGPUAdapter wgpuAdapter, WGPUDevice w
524518
// Set buffer info
525519
{
526520
BufferProperties& BufferInfo{AdapterInfo.Buffer};
527-
BufferInfo.ConstantBufferOffsetAlignment = wgpuSupportedLimits.limits.minUniformBufferOffsetAlignment;
528-
BufferInfo.StructuredBufferOffsetAlignment = wgpuSupportedLimits.limits.minStorageBufferOffsetAlignment;
521+
BufferInfo.ConstantBufferOffsetAlignment = wgpuSupportedLimits.minUniformBufferOffsetAlignment;
522+
BufferInfo.StructuredBufferOffsetAlignment = wgpuSupportedLimits.minStorageBufferOffsetAlignment;
529523
}
530524

531525
// Set sampler info

0 commit comments

Comments
 (0)