Skip to content

Commit

Permalink
Update api/gpu to use KJ_IF_SOME
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Sep 8, 2023
1 parent 5f0d006 commit a89edd2
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 65 deletions.
14 changes: 7 additions & 7 deletions src/workerd/api/gpu/gpu-adapter.c++
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,22 @@ GPUAdapter::requestDevice(jsg::Lock& js, jsg::Optional<GPUDeviceDescriptor> desc
wgpu::DeviceDescriptor desc{};
kj::Vector<wgpu::FeatureName> requiredFeatures;
wgpu::RequiredLimits limits;
KJ_IF_MAYBE (d, descriptor) {
KJ_IF_MAYBE (label, d->label) {
desc.label = label->cStr();
KJ_IF_SOME (d, descriptor) {
KJ_IF_SOME (label, d.label) {
desc.label = label.cStr();
}

KJ_IF_MAYBE (features, d->requiredFeatures) {
for (auto& required : *features) {
KJ_IF_SOME (features, d->requiredFeatures) {
for (auto& required : features) {
requiredFeatures.add(parseFeatureName(required));
}

desc.requiredFeaturesCount = requiredFeatures.size();
desc.requiredFeatures = requiredFeatures.begin();
}

KJ_IF_MAYBE (requiredLimits, d->requiredLimits) {
for (auto& f : requiredLimits->fields) {
KJ_IF_SOME (requiredLimits, d->requiredLimits) {
for (auto& f : requiredLimits.fields) {
setLimit(limits, f.name, f.value);
}
desc.requiredLimits = &limits;
Expand Down
16 changes: 8 additions & 8 deletions src/workerd/api/gpu/gpu-bindgroup-layout.c++
Original file line number Diff line number Diff line change
Expand Up @@ -524,20 +524,20 @@ wgpu::BindGroupLayoutEntry parseBindGroupLayoutEntry(GPUBindGroupLayoutEntry& en
e.binding = entry.binding;
e.visibility = static_cast<wgpu::ShaderStage>(entry.visibility);

KJ_IF_MAYBE (buffer, entry.buffer) {
e.buffer = parseBufferBindingLayout(*buffer);
KJ_IF_SOME (buffer, entry.buffer) {
e.buffer = parseBufferBindingLayout(buffer);
}

KJ_IF_MAYBE (sampler, entry.sampler) {
e.sampler = parseSamplerBindingLayout(*sampler);
KJ_IF_SOME (sampler, entry.sampler) {
e.sampler = parseSamplerBindingLayout(sampler);
}

KJ_IF_MAYBE (texture, entry.texture) {
e.texture = parseTextureBindingLayout(*texture);
KJ_IF_SOME (texture, entry.texture) {
e.texture = parseTextureBindingLayout(texture);
}

KJ_IF_MAYBE (storage, entry.storageTexture) {
e.storageTexture = parseStorageTextureBindingLayout(*storage);
KJ_IF_SOME (storage, entry.storageTexture) {
e.storageTexture = parseStorageTextureBindingLayout(storage);
}

return kj::mv(e);
Expand Down
8 changes: 4 additions & 4 deletions src/workerd/api/gpu/gpu-bindgroup.c++
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ wgpu::BindGroupEntry parseBindGroupEntry(GPUBindGroupEntry& entry) {
KJ_SWITCH_ONEOF(entry.resource) {
KJ_CASE_ONEOF(buffer, GPUBufferBinding) {
e.buffer = *buffer.buffer;
KJ_IF_MAYBE (offset, buffer.offset) {
e.offset = *offset;
KJ_IF_SOME (offset, buffer.offset) {
e.offset = offset;
}
KJ_IF_MAYBE (size, buffer.size) {
e.size = *size;
KJ_IF_SOME (size, buffer.size) {
e.size = size;
}
}
KJ_CASE_ONEOF(sampler, jsg::Ref<GPUSampler>) {
Expand Down
16 changes: 8 additions & 8 deletions src/workerd/api/gpu/gpu-command-encoder.c++
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ jsg::Ref<GPUCommandBuffer>
GPUCommandEncoder::finish(jsg::Optional<GPUCommandBufferDescriptor> descriptor) {
wgpu::CommandBufferDescriptor desc{};

KJ_IF_MAYBE (d, descriptor) {
KJ_IF_MAYBE (label, d->label) {
desc.label = label->cStr();
KJ_IF_SOME (d, descriptor) {
KJ_IF_SOME (label, d.label) {
desc.label = label.cStr();
}
}

Expand All @@ -34,13 +34,13 @@ GPUCommandEncoder::beginComputePass(jsg::Optional<GPUComputePassDescriptor> desc
wgpu::ComputePassDescriptor desc{};

kj::Vector<wgpu::ComputePassTimestampWrite> timestamps;
KJ_IF_MAYBE (d, descriptor) {
KJ_IF_MAYBE (label, d->label) {
desc.label = label->cStr();
KJ_IF_SOME (d, descriptor) {
KJ_IF_SOME (label, d.label) {
desc.label = label.cStr();
}

KJ_IF_MAYBE (timestampWrites, d->timestampWrites) {
for (auto& timestamp : *timestampWrites) {
KJ_IF_SOME (timestampWrites, d->timestampWrites) {
for (auto& timestamp : timestampWrites) {
wgpu::ComputePassTimestampWrite t{};
t.querySet = *timestamp.querySet;
t.queryIndex = timestamp.queryIndex;
Expand Down
10 changes: 5 additions & 5 deletions src/workerd/api/gpu/gpu-compute-pass-encoder.c++
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ void GPUComputePassEncoder::setBindGroup(
jsg::Optional<jsg::Sequence<GPUBufferDynamicOffset>> dynamicOffsets) {
wgpu::BindGroup bg = nullptr;

KJ_IF_MAYBE (bgroup, bindGroup) {
bg = **bgroup;
KJ_IF_SOME (bgroup, bindGroup) {
bg = *bgroup;
}

uint32_t* offsets = nullptr;
uint32_t num_offsets = 0;

KJ_IF_MAYBE (dos, dynamicOffsets) {
offsets = dos->begin();
num_offsets = dos->size();
KJ_IF_SOME (dos, dynamicOffsets) {
offsets = dos.begin();
num_offsets = dos.size();
}

encoder_.SetBindGroup(index, bg, num_offsets, offsets);
Expand Down
40 changes: 20 additions & 20 deletions src/workerd/api/gpu/gpu-device.c++
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ jsg::Ref<GPUSampler> GPUDevice::createSampler(GPUSamplerDescriptor descriptor) {
desc.compare = parseCompareFunction(descriptor.compare);
desc.maxAnisotropy = descriptor.maxAnisotropy.orDefault(1);

KJ_IF_MAYBE (label, descriptor.label) {
desc.label = label->cStr();
KJ_IF_SOME (label, descriptor.label) {
desc.label = label.cStr();
}

auto sampler = device_.CreateSampler(&desc);
Expand All @@ -137,8 +137,8 @@ jsg::Ref<GPUBindGroupLayout>
GPUDevice::createBindGroupLayout(GPUBindGroupLayoutDescriptor descriptor) {
wgpu::BindGroupLayoutDescriptor desc{};

KJ_IF_MAYBE (label, descriptor.label) {
desc.label = label->cStr();
KJ_IF_SOME (label, descriptor.label) {
desc.label = label.cStr();
}

kj::Vector<wgpu::BindGroupLayoutEntry> layoutEntries;
Expand All @@ -155,8 +155,8 @@ GPUDevice::createBindGroupLayout(GPUBindGroupLayoutDescriptor descriptor) {
jsg::Ref<GPUBindGroup> GPUDevice::createBindGroup(GPUBindGroupDescriptor descriptor) {
wgpu::BindGroupDescriptor desc{};

KJ_IF_MAYBE (label, descriptor.label) {
desc.label = label->cStr();
KJ_IF_SOME (label, descriptor.label) {
desc.label = label.cStr();
}

desc.layout = *descriptor.layout;
Expand All @@ -178,8 +178,8 @@ jsg::Ref<GPUShaderModule> GPUDevice::createShaderModule(GPUShaderModuleDescripto
wgpu::ShaderModuleWGSLDescriptor wgsl_desc{};
desc.nextInChain = &wgsl_desc;

KJ_IF_MAYBE (label, descriptor.label) {
desc.label = label->cStr();
KJ_IF_SOME (label, descriptor.label) {
desc.label = label.cStr();
}

wgsl_desc.code = descriptor.code.cStr();
Expand All @@ -192,8 +192,8 @@ jsg::Ref<GPUPipelineLayout>
GPUDevice::createPipelineLayout(GPUPipelineLayoutDescriptor descriptor) {
wgpu::PipelineLayoutDescriptor desc{};

KJ_IF_MAYBE (label, descriptor.label) {
desc.label = label->cStr();
KJ_IF_SOME (label, descriptor.label) {
desc.label = label.cStr();
}

kj::Vector<wgpu::BindGroupLayout> bindGroupLayouts;
Expand All @@ -213,9 +213,9 @@ GPUDevice::createCommandEncoder(jsg::Optional<GPUCommandEncoderDescriptor> descr
wgpu::CommandEncoderDescriptor desc{};

kj::String label = kj::str("");
KJ_IF_MAYBE (d, descriptor) {
KJ_IF_MAYBE (l, d->label) {
label = kj::mv(*l);
KJ_IF_SOME (d, descriptor) {
KJ_IF_SOME (l, d.label) {
label = kj::mv(l);
desc.label = label.cStr();
}
}
Expand All @@ -228,16 +228,16 @@ wgpu::ComputePipelineDescriptor
parseComputePipelineDescriptor(GPUComputePipelineDescriptor& descriptor) {
wgpu::ComputePipelineDescriptor desc{};

KJ_IF_MAYBE (label, descriptor.label) {
desc.label = label->cStr();
KJ_IF_SOME (label, descriptor.label) {
desc.label = label.cStr();
}

desc.compute.module = *descriptor.compute.module;
desc.compute.entryPoint = descriptor.compute.entryPoint.cStr();

kj::Vector<wgpu::ConstantEntry> constants;
KJ_IF_MAYBE (cDict, descriptor.compute.constants) {
for (auto& f : cDict->fields) {
KJ_IF_SOME (cDict, descriptor.compute.constants) {
for (auto& f : cDict.fields) {
wgpu::ConstantEntry e;
e.key = f.name.cStr();
e.value = f.value;
Expand Down Expand Up @@ -286,7 +286,7 @@ jsg::Promise<kj::Maybe<jsg::Ref<GPUError>>> GPUDevice::popErrorScope() {
auto c = std::unique_ptr<Context>(static_cast<Context*>(userdata));
switch (type) {
case WGPUErrorType::WGPUErrorType_NoError:
c->fulfiller->fulfill(nullptr);
c->fulfiller->fulfill(kj::none);
break;
case WGPUErrorType::WGPUErrorType_OutOfMemory: {
jsg::Ref<GPUError> err = jsg::alloc<GPUOutOfMemoryError>(kj::str(message));
Expand Down Expand Up @@ -449,8 +449,8 @@ GPUDevice::GPUDevice(jsg::Lock& js, wgpu::Device d)
jsg::Ref<GPUQuerySet> GPUDevice::createQuerySet(GPUQuerySetDescriptor descriptor) {
wgpu::QuerySetDescriptor desc{};

KJ_IF_MAYBE (label, descriptor.label) {
desc.label = label->cStr();
KJ_IF_SOME (label, descriptor.label) {
desc.label = label.cStr();
}

desc.count = descriptor.count;
Expand Down
10 changes: 5 additions & 5 deletions src/workerd/api/gpu/gpu-queue.c++
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ void GPUQueue::writeBuffer(jsg::Ref<GPUBuffer> buffer, GPUSize64 bufferOffset,
wgpu::Buffer buf = *buffer;

uint64_t dataOffset = 0;
KJ_IF_MAYBE (offset, dataOffsetElements) {
KJ_IF_SOME (offset, dataOffsetElements) {
// In the JS semantics of WebGPU, writeBuffer works in number of
// elements of the typed arrays.
dataOffset = *offset * data.getElementSize();
dataOffset = offset * data.getElementSize();
JSG_REQUIRE(dataOffset <= data.size(), TypeError, "dataOffset is larger than data's size.");
}

auto dataPtr = reinterpret_cast<uint8_t*>(data.asArrayPtr().begin()) + dataOffset;
size_t dataSize = data.size() - dataOffset;
KJ_IF_MAYBE (size, sizeElements) {
JSG_REQUIRE(*size <= std::numeric_limits<uint64_t>::max() / data.getElementSize(), TypeError,
KJ_IF_SOME (size, sizeElements) {
JSG_REQUIRE(size <= std::numeric_limits<uint64_t>::max() / data.getElementSize(), TypeError,
"size overflows.");
dataSize = *size * data.getElementSize();
dataSize = size * data.getElementSize();
JSG_REQUIRE(dataOffset + dataSize <= data.size(), TypeError,
"size + dataOffset is larger than data's size.");

Expand Down
4 changes: 2 additions & 2 deletions src/workerd/api/gpu/gpu-supported-features.c++
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace workerd::api::gpu {
GPUSupportedFeatures::GPUSupportedFeatures(kj::Array<wgpu::FeatureName> features) {
for (wgpu::FeatureName feature : features) {
// add only known features to the feature list
KJ_IF_MAYBE (knownF, getFeatureName(feature)) {
enabled_.insert(kj::mv(*knownF));
KJ_IF_SOME (knownF, getFeatureName(feature)) {
enabled_.insert(kj::mv(knownF));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/gpu/gpu-utils.c++
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ kj::Maybe<GPUFeatureName> getFeatureName(wgpu::FeatureName& feature) {
break;
}

return nullptr;
return kj::none;
}

} // namespace workerd::api::gpu
10 changes: 5 additions & 5 deletions src/workerd/api/gpu/gpu.c++
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ GPU::requestAdapter(jsg::Lock& js, jsg::Optional<GPURequestAdapterOptions> optio
auto adapters = instance_.GetAdapters();
if (adapters.empty()) {
KJ_LOG(WARNING, "no webgpu adapters found");
return js.resolvedPromise(kj::Maybe<jsg::Ref<GPUAdapter>>(nullptr));
return js.resolvedPromise(kj::Maybe<jsg::Ref<GPUAdapter>>(kj::none));
}

kj::Maybe<dawn::native::Adapter> adapter = nullptr;
kj::Maybe<dawn::native::Adapter> adapter;
for (auto& a : adapters) {
wgpu::AdapterProperties props;
a.GetProperties(&props);
Expand All @@ -72,13 +72,13 @@ GPU::requestAdapter(jsg::Lock& js, jsg::Optional<GPURequestAdapterOptions> optio
break;
}

KJ_IF_MAYBE (a, adapter) {
kj::Maybe<jsg::Ref<GPUAdapter>> gpuAdapter = jsg::alloc<GPUAdapter>(*a);
KJ_IF_SOME (a, adapter) {
kj::Maybe<jsg::Ref<GPUAdapter>> gpuAdapter = jsg::alloc<GPUAdapter>(a);
return js.resolvedPromise(kj::mv(gpuAdapter));
}

KJ_LOG(WARNING, "did not find an adapter that matched what we wanted");
return js.resolvedPromise(kj::Maybe<jsg::Ref<GPUAdapter>>(nullptr));
return js.resolvedPromise(kj::Maybe<jsg::Ref<GPUAdapter>>(kj::none));
}

} // namespace workerd::api::gpu

0 comments on commit a89edd2

Please sign in to comment.