Skip to content

Commit 3158d63

Browse files
vontureAngle LUCI CQ
authored andcommitted
WebGPU: Store the WebGPU proc table in wrappers
Instead of relying on the global WebGPU functions, pass the proc table to the object wrappers. Bug: angleproject:342213844 Change-Id: I79a5e819ffac5b366fed0a159a6cef116b5e82b3 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6514676 Reviewed-by: Liza Burakova <liza@chromium.org> Commit-Queue: Geoff Lang <geofflang@chromium.org> Reviewed-by: Matthew Denton <mpdenton@chromium.org>
1 parent 71b58cd commit 3158d63

19 files changed

Lines changed: 240 additions & 137 deletions

src/libANGLE/renderer/wgpu/BufferWgpu.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ angle::Result BufferWgpu::setData(const gl::Context *context,
7272
BufferFeedback *feedback)
7373
{
7474
ContextWgpu *contextWgpu = webgpu::GetImpl(context);
75+
const DawnProcTable *wgpu = webgpu::GetProcs(contextWgpu);
7576
webgpu::DeviceHandle device = webgpu::GetDevice(context);
7677

7778
bool hasData = data && size > 0;
@@ -82,7 +83,8 @@ angle::Result BufferWgpu::setData(const gl::Context *context,
8283
(hasData && !mBuffer.canMapForWrite()))
8384
{
8485
// Allocate a new buffer
85-
ANGLE_TRY(mBuffer.initBuffer(device, size, GetDefaultWGPUBufferUsageForBinding(target),
86+
ANGLE_TRY(mBuffer.initBuffer(wgpu, device, size,
87+
GetDefaultWGPUBufferUsageForBinding(target),
8688
webgpu::MapAtCreation::Yes));
8789
}
8890

src/libANGLE/renderer/wgpu/ContextWgpu.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ void ContextWgpu::onDestroy(const gl::Context *context)
9191

9292
angle::Result ContextWgpu::initialize(const angle::ImageLoadContext &imageLoadContext)
9393
{
94+
const DawnProcTable *wgpu = webgpu::GetProcs(this);
95+
9496
mImageLoadContext = imageLoadContext;
9597

9698
// Create the driver uniform bind group layout, which won't ever change.
@@ -108,7 +110,8 @@ angle::Result ContextWgpu::initialize(const angle::ImageLoadContext &imageLoadCo
108110
driverUniformsBindGroupLayoutDesc.entryCount = 1;
109111
driverUniformsBindGroupLayoutDesc.entries = &driverUniformBindGroupEntry;
110112
mDriverUniformsBindGroupLayout = webgpu::BindGroupLayoutHandle::Acquire(
111-
wgpuDeviceCreateBindGroupLayout(getDevice().get(), &driverUniformsBindGroupLayoutDesc));
113+
wgpu,
114+
wgpu->deviceCreateBindGroupLayout(getDevice().get(), &driverUniformsBindGroupLayoutDesc));
112115

113116
// Driver uniforms should be set to 0 for later memcmp.
114117
memset(&mDriverUniforms, 0, sizeof(mDriverUniforms));
@@ -140,8 +143,9 @@ angle::Result ContextWgpu::flush(webgpu::RenderPassClosureReason closureReason)
140143

141144
if (mCurrentCommandEncoder)
142145
{
146+
const DawnProcTable *wgpu = webgpu::GetProcs(this);
143147
webgpu::CommandBufferHandle commandBuffer = webgpu::CommandBufferHandle::Acquire(
144-
wgpuCommandEncoderFinish(mCurrentCommandEncoder.get(), nullptr));
148+
wgpu, wgpu->commandEncoderFinish(mCurrentCommandEncoder.get(), nullptr));
145149
mCurrentCommandEncoder = nullptr;
146150

147151
wgpuQueueSubmit(getQueue().get(), 1, &commandBuffer.get());
@@ -218,8 +222,9 @@ void ContextWgpu::ensureCommandEncoderCreated()
218222
{
219223
if (!mCurrentCommandEncoder)
220224
{
221-
mCurrentCommandEncoder = webgpu::CommandEncoderHandle::Acquire(
222-
wgpuDeviceCreateCommandEncoder(getDevice().get(), nullptr));
225+
const DawnProcTable *wgpu = webgpu::GetProcs(this);
226+
mCurrentCommandEncoder = webgpu::CommandEncoderHandle::Acquire(
227+
wgpu, wgpu->deviceCreateCommandEncoder(getDevice().get(), nullptr));
223228
}
224229
}
225230

@@ -1096,9 +1101,11 @@ void ContextWgpu::handleError(GLenum errorCode,
10961101

10971102
angle::Result ContextWgpu::startRenderPass(const webgpu::PackedRenderPassDescriptor &desc)
10981103
{
1104+
const DawnProcTable *wgpu = webgpu::GetProcs(this);
1105+
10991106
ensureCommandEncoderCreated();
11001107

1101-
mCurrentRenderPass = webgpu::CreateRenderPass(mCurrentCommandEncoder, desc);
1108+
mCurrentRenderPass = webgpu::CreateRenderPass(wgpu, mCurrentCommandEncoder, desc);
11021109
mDirtyBits |= mNewRenderPassDirtyBits;
11031110

11041111
return angle::Result::Continue;
@@ -1456,6 +1463,8 @@ angle::Result ContextWgpu::handleDirtyBindGroups(DirtyBits::Iterator *dirtyBitsI
14561463

14571464
angle::Result ContextWgpu::handleDirtyDriverUniforms(DirtyBits::Iterator *dirtyBitsIterator)
14581465
{
1466+
const DawnProcTable *wgpu = webgpu::GetProcs(this);
1467+
14591468
DriverUniforms newDriverUniforms;
14601469
memset(&newDriverUniforms, 0, sizeof(newDriverUniforms));
14611470

@@ -1501,7 +1510,7 @@ angle::Result ContextWgpu::handleDirtyDriverUniforms(DirtyBits::Iterator *dirtyB
15011510
// Upload the new driver uniforms to a new GPU buffer.
15021511
webgpu::BufferHelper driverUniformBuffer;
15031512

1504-
ANGLE_TRY(driverUniformBuffer.initBuffer(getDevice(), sizeof(DriverUniforms),
1513+
ANGLE_TRY(driverUniformBuffer.initBuffer(wgpu, getDevice(), sizeof(DriverUniforms),
15051514
WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst,
15061515
webgpu::MapAtCreation::Yes));
15071516

@@ -1524,7 +1533,7 @@ angle::Result ContextWgpu::handleDirtyDriverUniforms(DirtyBits::Iterator *dirtyB
15241533
bindGroupDesc.entryCount = 1;
15251534
bindGroupDesc.entries = &bindGroupEntry;
15261535
mDriverUniformsBindGroup = webgpu::BindGroupHandle::Acquire(
1527-
wgpuDeviceCreateBindGroup(getDevice().get(), &bindGroupDesc));
1536+
wgpu, wgpu->deviceCreateBindGroup(getDevice().get(), &bindGroupDesc));
15281537

15291538
// This bind group needs to be updated on the same draw call as the driver uniforms are updated.
15301539
dirtyBitsIterator->setLaterBit(DIRTY_BIT_BIND_GROUPS);

src/libANGLE/renderer/wgpu/ContextWgpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ class ContextWgpu : public ContextImpl
297297

298298
const angle::ImageLoadContext &getImageLoadContext() const { return mImageLoadContext; }
299299

300-
DisplayWgpu *getDisplay() { return mDisplay; }
300+
DisplayWgpu *getDisplay() const { return mDisplay; }
301301
webgpu::DeviceHandle getDevice() const { return mDisplay->getDevice(); }
302302
webgpu::QueueHandle getQueue() const { return mDisplay->getQueue(); }
303303
webgpu::InstanceHandle getInstance() const { return mDisplay->getInstance(); }

src/libANGLE/renderer/wgpu/DisplayWgpu.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ DisplayWgpu::~DisplayWgpu() {}
3131

3232
egl::Error DisplayWgpu::initialize(egl::Display *display)
3333
{
34+
mProcTable = dawn::native::GetProcs();
35+
dawnProcSetProcs(&mProcTable);
36+
3437
ANGLE_TRY(createWgpuDevice());
3538

36-
mQueue = webgpu::QueueHandle::Acquire(wgpuDeviceGetQueue(mDevice.get()));
39+
mQueue = webgpu::QueueHandle::Acquire(&mProcTable, mProcTable.deviceGetQueue(mDevice.get()));
3740

3841
mFormatTable.initialize();
3942

@@ -249,11 +252,10 @@ void DisplayWgpu::generateCaps(egl::Caps *outCaps) const
249252

250253
egl::Error DisplayWgpu::createWgpuDevice()
251254
{
252-
dawnProcSetProcs(&dawn::native::GetProcs());
253-
254255
WGPUInstanceDescriptor instanceDescriptor = WGPU_INSTANCE_DESCRIPTOR_INIT;
255256
instanceDescriptor.capabilities.timedWaitAnyEnable = true;
256-
mInstance = webgpu::InstanceHandle::Acquire(wgpuCreateInstance(&instanceDescriptor));
257+
mInstance = webgpu::InstanceHandle::Acquire(&mProcTable,
258+
mProcTable.createInstance(&instanceDescriptor));
257259

258260
struct RequestAdapterResult
259261
{
@@ -271,13 +273,14 @@ egl::Error DisplayWgpu::createWgpuDevice()
271273
struct WGPUStringView message, void *userdata1,
272274
void *userdata2) {
273275
RequestAdapterResult *result = reinterpret_cast<RequestAdapterResult *>(userdata1);
274-
ASSERT(userdata2 == nullptr);
276+
const DawnProcTable *wgpu = reinterpret_cast<const DawnProcTable *>(userdata2);
275277

276278
result->status = status;
277-
result->adapter = webgpu::AdapterHandle::Acquire(adapter);
279+
result->adapter = webgpu::AdapterHandle::Acquire(wgpu, adapter);
278280
result->message = std::string(message.data, message.length);
279281
};
280282
requestAdapterCallback.userdata1 = &adapterResult;
283+
requestAdapterCallback.userdata2 = &mProcTable;
281284

282285
WGPUFutureWaitInfo futureWaitInfo;
283286
futureWaitInfo.future =
@@ -307,7 +310,8 @@ egl::Error DisplayWgpu::createWgpuDevice()
307310
<< " - message: " << std::string(message.data, message.length);
308311
};
309312

310-
mDevice = webgpu::DeviceHandle::Acquire(wgpuAdapterCreateDevice(mAdapter.get(), &deviceDesc));
313+
mDevice = webgpu::DeviceHandle::Acquire(
314+
&mProcTable, mProcTable.adapterCreateDevice(mAdapter.get(), &deviceDesc));
311315
return egl::NoError();
312316
}
313317

src/libANGLE/renderer/wgpu/DisplayWgpu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class DisplayWgpu : public DisplayImpl
9292

9393
angle::NativeWindowSystem getWindowSystem() const override;
9494

95+
const DawnProcTable *getProcs() const { return &mProcTable; }
9596
webgpu::AdapterHandle getAdapter() { return mAdapter; }
9697
webgpu::DeviceHandle getDevice() { return mDevice; }
9798
webgpu::QueueHandle getQueue() { return mQueue; }
@@ -116,6 +117,8 @@ class DisplayWgpu : public DisplayImpl
116117

117118
egl::Error createWgpuDevice();
118119

120+
DawnProcTable mProcTable;
121+
119122
webgpu::AdapterHandle mAdapter;
120123
webgpu::InstanceHandle mInstance;
121124
webgpu::DeviceHandle mDevice;

src/libANGLE/renderer/wgpu/ProgramExecutableWgpu.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ angle::Result ProgramExecutableWgpu::updateUniformsAndGetBindGroup(
4848
{
4949
if (mDefaultUniformBlocksDirty.any())
5050
{
51+
const DawnProcTable *wgpu = webgpu::GetProcs(contextWgpu);
52+
5153
// TODO(anglebug.com/376553328): this creates an entire new buffer every time a single
5254
// uniform changes, and the old ones are just garbage collected. This should be optimized.
5355
webgpu::BufferHelper defaultUniformBuffer;
@@ -63,7 +65,7 @@ angle::Result ProgramExecutableWgpu::updateUniformsAndGetBindGroup(
6365
return angle::Result::Stop;
6466
}
6567

66-
ANGLE_TRY(defaultUniformBuffer.initBuffer(contextWgpu->getDevice(), requiredSpace,
68+
ANGLE_TRY(defaultUniformBuffer.initBuffer(wgpu, contextWgpu->getDevice(), requiredSpace,
6769
WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst,
6870
webgpu::MapAtCreation::Yes));
6971

@@ -106,7 +108,7 @@ angle::Result ProgramExecutableWgpu::updateUniformsAndGetBindGroup(
106108
bindGroupDesc.entryCount = bindings.size();
107109
bindGroupDesc.entries = bindings.data();
108110
mDefaultBindGroup = webgpu::BindGroupHandle::Acquire(
109-
wgpuDeviceCreateBindGroup(contextWgpu->getDevice().get(), &bindGroupDesc));
111+
wgpu, wgpu->deviceCreateBindGroup(contextWgpu->getDevice().get(), &bindGroupDesc));
110112
}
111113

112114
ASSERT(mDefaultBindGroup);
@@ -121,6 +123,8 @@ angle::Result ProgramExecutableWgpu::getSamplerAndTextureBindGroup(
121123
{
122124
if (mSamplerBindingsDirty)
123125
{
126+
const DawnProcTable *wgpu = webgpu::GetProcs(contextWgpu);
127+
124128
const gl::ActiveTexturesCache &completeTextures =
125129
contextWgpu->getState().getActiveTexturesCache();
126130

@@ -175,7 +179,7 @@ angle::Result ProgramExecutableWgpu::getSamplerAndTextureBindGroup(
175179
// TODO(anglebug.com/389145696): potentially cache sampler.
176180
WGPUSamplerDescriptor sampleDesc = gl_wgpu::GetWgpuSamplerDesc(samplerState);
177181
webgpu::SamplerHandle wgpuSampler = webgpu::SamplerHandle::Acquire(
178-
wgpuDeviceCreateSampler(contextWgpu->getDevice().get(), &sampleDesc));
182+
wgpu, wgpu->deviceCreateSampler(contextWgpu->getDevice().get(), &sampleDesc));
179183
samplers.push_back(wgpuSampler);
180184

181185
WGPUBindGroupEntry samplerBindGroupEntry = WGPU_BIND_GROUP_ENTRY_INIT;
@@ -207,7 +211,7 @@ angle::Result ProgramExecutableWgpu::getSamplerAndTextureBindGroup(
207211
bindGroupDesc.entryCount = bindings.size();
208212
bindGroupDesc.entries = bindings.data();
209213
mSamplersAndTexturesBindGroup = webgpu::BindGroupHandle::Acquire(
210-
wgpuDeviceCreateBindGroup(contextWgpu->getDevice().get(), &bindGroupDesc));
214+
wgpu, wgpu->deviceCreateBindGroup(contextWgpu->getDevice().get(), &bindGroupDesc));
211215

212216
mSamplerBindingsDirty = false;
213217
}
@@ -480,6 +484,9 @@ void ProgramExecutableWgpu::genBindingLayoutIfNecessary(ContextWgpu *context)
480484
{
481485
return;
482486
}
487+
488+
const DawnProcTable *wgpu = webgpu::GetProcs(context);
489+
483490
// TODO(anglebug.com/42267100): for now, only create a wgpu::PipelineLayout with the default
484491
// uniform block, driver uniform block, and textures/samplers. Will need to be extended for
485492
// UBOs. Also, possibly provide this layout as a compilation hint to createShaderModule().
@@ -516,7 +523,8 @@ void ProgramExecutableWgpu::genBindingLayoutIfNecessary(ContextWgpu *context)
516523
defaultBindGroupLayoutDesc.entryCount = defaultBindGroupLayoutEntries.size();
517524
defaultBindGroupLayoutDesc.entries = defaultBindGroupLayoutEntries.data();
518525
mDefaultBindGroupLayout = webgpu::BindGroupLayoutHandle::Acquire(
519-
wgpuDeviceCreateBindGroupLayout(context->getDevice().get(), &defaultBindGroupLayoutDesc));
526+
wgpu,
527+
wgpu->deviceCreateBindGroupLayout(context->getDevice().get(), &defaultBindGroupLayoutDesc));
520528

521529
// Add the textures/samplers to the second bind group.
522530
std::vector<WGPUBindGroupLayoutEntry> samplersAndTexturesBindGroupLayoutEntries;
@@ -570,9 +578,9 @@ void ProgramExecutableWgpu::genBindingLayoutIfNecessary(ContextWgpu *context)
570578
samplersAndTexturesBindGroupLayoutEntries.size();
571579
texturesAndSamplersBindGroupLayoutDesc.entries =
572580
samplersAndTexturesBindGroupLayoutEntries.data();
573-
mSamplersAndTexturesBindGroupLayout =
574-
webgpu::BindGroupLayoutHandle::Acquire(wgpuDeviceCreateBindGroupLayout(
575-
context->getDevice().get(), &texturesAndSamplersBindGroupLayoutDesc));
581+
mSamplersAndTexturesBindGroupLayout = webgpu::BindGroupLayoutHandle::Acquire(
582+
wgpu, wgpu->deviceCreateBindGroupLayout(context->getDevice().get(),
583+
&texturesAndSamplersBindGroupLayoutDesc));
576584

577585
// Driver uniforms bind groups are handled by ContextWgpu.
578586

@@ -592,7 +600,7 @@ void ProgramExecutableWgpu::genBindingLayoutIfNecessary(ContextWgpu *context)
592600
layoutDesc.bindGroupLayoutCount = groupLayouts.size();
593601
layoutDesc.bindGroupLayouts = groupLayouts.data();
594602
mPipelineLayout = webgpu::PipelineLayoutHandle::Acquire(
595-
wgpuDeviceCreatePipelineLayout(context->getDevice().get(), &layoutDesc));
603+
wgpu, wgpu->deviceCreatePipelineLayout(context->getDevice().get(), &layoutDesc));
596604
}
597605

598606
} // namespace rx

src/libANGLE/renderer/wgpu/ProgramWgpu.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@ angle::Result InitDefaultUniformBlock(const std::vector<sh::ShaderVariable> &uni
8080
class CreateWGPUShaderModuleTask : public LinkSubTask
8181
{
8282
public:
83-
CreateWGPUShaderModuleTask(webgpu::InstanceHandle instance,
83+
CreateWGPUShaderModuleTask(const DawnProcTable *wgpu,
84+
webgpu::InstanceHandle instance,
8485
webgpu::DeviceHandle device,
8586
const gl::SharedCompiledShaderState &compiledShaderState,
8687
const gl::ProgramExecutable &executable,
8788
gl::ProgramMergedVaryings mergedVaryings,
8889
TranslatedWGPUShaderModule &resultShaderModule)
89-
: mInstance(instance),
90+
: mProcTable(wgpu),
91+
mInstance(instance),
9092
mDevice(device),
9193
mCompiledShaderState(compiledShaderState),
9294
mExecutable(executable),
@@ -138,7 +140,8 @@ class CreateWGPUShaderModuleTask : public LinkSubTask
138140
shaderModuleDescriptor.nextInChain = &shaderModuleWGSLDescriptor.chain;
139141

140142
mShaderModule.module = webgpu::ShaderModuleHandle::Acquire(
141-
wgpuDeviceCreateShaderModule(mDevice.get(), &shaderModuleDescriptor));
143+
mProcTable,
144+
mProcTable->deviceCreateShaderModule(mDevice.get(), &shaderModuleDescriptor));
142145

143146
WGPUCompilationInfoCallbackInfo getCompilationInfoCallback =
144147
WGPU_COMPILATION_INFO_CALLBACK_INFO_INIT;
@@ -190,6 +193,7 @@ class CreateWGPUShaderModuleTask : public LinkSubTask
190193
}
191194

192195
private:
196+
const DawnProcTable *mProcTable = nullptr;
193197
webgpu::InstanceHandle mInstance;
194198
webgpu::DeviceHandle mDevice;
195199
gl::SharedCompiledShaderState mCompiledShaderState;
@@ -205,8 +209,12 @@ class CreateWGPUShaderModuleTask : public LinkSubTask
205209
class LinkTaskWgpu : public LinkTask
206210
{
207211
public:
208-
LinkTaskWgpu(webgpu::InstanceHandle instance, webgpu::DeviceHandle device, ProgramWgpu *program)
209-
: mInstance(instance),
212+
LinkTaskWgpu(const DawnProcTable *wgpu,
213+
webgpu::InstanceHandle instance,
214+
webgpu::DeviceHandle device,
215+
ProgramWgpu *program)
216+
: mProcTable(wgpu),
217+
mInstance(instance),
210218
mDevice(device),
211219
mProgram(program),
212220
mExecutable(&mProgram->getState().getExecutable())
@@ -231,8 +239,9 @@ class LinkTaskWgpu : public LinkTask
231239
if (shaders[shaderType])
232240
{
233241
auto task = std::make_shared<CreateWGPUShaderModuleTask>(
234-
mInstance, mDevice, shaders[shaderType], *executable->getExecutable(),
235-
mergedVaryings, executable->getShaderModule(shaderType));
242+
mProcTable, mInstance, mDevice, shaders[shaderType],
243+
*executable->getExecutable(), mergedVaryings,
244+
executable->getShaderModule(shaderType));
236245
linkSubTasksOut->push_back(task);
237246
}
238247
}
@@ -343,6 +352,7 @@ class LinkTaskWgpu : public LinkTask
343352
}
344353
}
345354

355+
const DawnProcTable *mProcTable = nullptr;
346356
webgpu::InstanceHandle mInstance;
347357
webgpu::DeviceHandle mDevice;
348358
ProgramWgpu *mProgram = nullptr;
@@ -373,10 +383,11 @@ void ProgramWgpu::setSeparable(bool separable) {}
373383

374384
angle::Result ProgramWgpu::link(const gl::Context *context, std::shared_ptr<LinkTask> *linkTaskOut)
375385
{
386+
const DawnProcTable *wgpu = webgpu::GetProcs(context);
376387
webgpu::DeviceHandle device = webgpu::GetDevice(context);
377388
webgpu::InstanceHandle instance = webgpu::GetInstance(context);
378389

379-
*linkTaskOut = std::shared_ptr<LinkTask>(new LinkTaskWgpu(instance, device, this));
390+
*linkTaskOut = std::shared_ptr<LinkTask>(new LinkTaskWgpu(wgpu, instance, device, this));
380391
return angle::Result::Continue;
381392
}
382393

0 commit comments

Comments
 (0)