Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement more webgpu objects #925

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/workerd/api/gpu/gpu-command-encoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2017-2022 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

#pragma once

#include <webgpu/webgpu_cpp.h>
#include <workerd/jsg/jsg.h>

namespace workerd::api::gpu {

class GPUCommandEncoder : public jsg::Object {
public:
explicit GPUCommandEncoder(wgpu::CommandEncoder e) : encoder_(kj::mv(e)){};
JSG_RESOURCE_TYPE(GPUCommandEncoder) {}

private:
wgpu::CommandEncoder encoder_;
};

struct GPUCommandEncoderDescriptor {
jsg::Optional<kj::String> label;

JSG_STRUCT(label);
};

} // namespace workerd::api::gpu
43 changes: 43 additions & 0 deletions src/workerd/api/gpu/gpu-compute-pipeline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2017-2022 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

#pragma once

#include "gpu-pipeline-layout.h"
#include "gpu-shader-module.h"
#include <webgpu/webgpu_cpp.h>
#include <workerd/jsg/jsg.h>

namespace workerd::api::gpu {

class GPUComputePipeline : public jsg::Object {
public:
explicit GPUComputePipeline(wgpu::ComputePipeline p) : pipeline_(kj::mv(p)){};
JSG_RESOURCE_TYPE(GPUComputePipeline) {}

private:
wgpu::ComputePipeline pipeline_;
};

using GPUPipelineConstantValue = double;

struct GPUProgrammableStage {
jsg::Ref<GPUShaderModule> module;
kj::String entryPoint;
jsg::Optional<jsg::Dict<GPUPipelineConstantValue>> constants;

JSG_STRUCT(module, entryPoint, constants);
};

using GPUComputePipelineLayout = kj::OneOf<kj::String, jsg::Ref<GPUPipelineLayout>>;

struct GPUComputePipelineDescriptor {
jsg::Optional<kj::String> label;
GPUProgrammableStage compute;
GPUComputePipelineLayout layout;

JSG_STRUCT(label, compute, layout);
};

} // namespace workerd::api::gpu
57 changes: 56 additions & 1 deletion src/workerd/api/gpu/gpu-device.c++
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#include "gpu-bindgroup-layout.h"
#include "gpu-bindgroup.h"
#include "gpu-buffer.h"
#include "gpu-command-encoder.h"
#include "gpu-sampler.h"
#include "gpu-utils.h"
#include "workerd/jsg/exception.h"

namespace workerd::api::gpu {

Expand Down Expand Up @@ -225,7 +227,7 @@ GPUDevice::createPipelineLayout(GPUPipelineLayoutDescriptor descriptor) {
}

kj::Vector<wgpu::BindGroupLayout> bindGroupLayouts;
for (auto &l: descriptor.bindGroupLayouts) {
for (auto &l : descriptor.bindGroupLayouts) {
bindGroupLayouts.add(*l);
}

Expand All @@ -236,4 +238,57 @@ GPUDevice::createPipelineLayout(GPUPipelineLayoutDescriptor descriptor) {
return jsg::alloc<GPUPipelineLayout>(kj::mv(layout));
}

jsg::Ref<GPUCommandEncoder> GPUDevice::createCommandEncoder(
jsg::Optional<GPUCommandEncoderDescriptor> descriptor) {
wgpu::CommandEncoderDescriptor desc{};

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

auto encoder = device_.CreateCommandEncoder(&desc);
return jsg::alloc<GPUCommandEncoder>(kj::mv(encoder));
}

jsg::Ref<GPUComputePipeline>
GPUDevice::createComputePipeline(GPUComputePipelineDescriptor descriptor) {
wgpu::ComputePipelineDescriptor desc{};

KJ_IF_MAYBE (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) {
wgpu::ConstantEntry e;
e.key = f.name.cStr();
e.value = f.value;
constants.add(kj::mv(e));
}
}

desc.compute.constants = constants.begin();
desc.compute.constantCount = constants.size();

KJ_SWITCH_ONEOF(descriptor.layout) {
KJ_CASE_ONEOF(autoLayoutMode, kj::String) {
JSG_REQUIRE(autoLayoutMode == "auto", TypeError,
"unknown auto layout mode", autoLayoutMode);
desc.layout = nullptr;
}
KJ_CASE_ONEOF(layout, jsg::Ref<GPUPipelineLayout>) {
desc.layout = *layout;
}
}

auto pipeline = device_.CreateComputePipeline(&desc);
return jsg::alloc<GPUComputePipeline>(kj::mv(pipeline));
}

} // namespace workerd::api::gpu
8 changes: 8 additions & 0 deletions src/workerd/api/gpu/gpu-device.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "gpu-bindgroup-layout.h"
#include "gpu-bindgroup.h"
#include "gpu-buffer.h"
#include "gpu-command-encoder.h"
#include "gpu-compute-pipeline.h"
#include "gpu-pipeline-layout.h"
#include "gpu-sampler.h"
#include "gpu-shader-module.h"
Expand All @@ -26,6 +28,8 @@ class GPUDevice : public jsg::Object {
JSG_METHOD(createSampler);
JSG_METHOD(createShaderModule);
JSG_METHOD(createPipelineLayout);
JSG_METHOD(createComputePipeline);
JSG_METHOD(createCommandEncoder);
}

private:
Expand All @@ -39,6 +43,10 @@ class GPUDevice : public jsg::Object {
createShaderModule(GPUShaderModuleDescriptor descriptor);
jsg::Ref<GPUPipelineLayout>
createPipelineLayout(GPUPipelineLayoutDescriptor descriptor);
jsg::Ref<GPUComputePipeline>
createComputePipeline(GPUComputePipelineDescriptor descriptor);
jsg::Ref<GPUCommandEncoder>
createCommandEncoder(jsg::Optional<GPUCommandEncoderDescriptor> descriptor);
};

struct GPUQueueDescriptor {
Expand Down
4 changes: 3 additions & 1 deletion src/workerd/api/gpu/gpu-pipeline-layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

#pragma once

#include "workerd/api/gpu/gpu-bindgroup-layout.h"
#include "gpu-bindgroup-layout.h"
#include <webgpu/webgpu_cpp.h>
#include <workerd/jsg/jsg.h>

namespace workerd::api::gpu {

class GPUPipelineLayout : public jsg::Object {
public:
// Implicit cast operator to Dawn GPU object
inline operator const wgpu::PipelineLayout &() const { return layout_; }
explicit GPUPipelineLayout(wgpu::PipelineLayout l) : layout_(kj::mv(l)){};
JSG_RESOURCE_TYPE(GPUPipelineLayout) {}

Expand Down
2 changes: 2 additions & 0 deletions src/workerd/api/gpu/gpu-shader-module.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace workerd::api::gpu {

class GPUShaderModule : public jsg::Object {
public:
// Implicit cast operator to Dawn GPU object
inline operator const wgpu::ShaderModule &() const { return shader_; }
explicit GPUShaderModule(wgpu::ShaderModule s) : shader_(kj::mv(s)){};
JSG_RESOURCE_TYPE(GPUShaderModule) {}

Expand Down
7 changes: 6 additions & 1 deletion src/workerd/api/gpu/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "gpu-adapter.h"
#include "gpu-bindgroup-layout.h"
#include "gpu-bindgroup.h"
#include "gpu-command-encoder.h"
#include "gpu-compute-pipeline.h"
#include "gpu-device.h"
#include "gpu-pipeline-layout.h"
#include "gpu-sampler.h"
Expand Down Expand Up @@ -44,6 +46,9 @@ class GPU : public jsg::Object {
api::gpu::GPUBindGroupEntry, api::gpu::GPUBufferBinding, \
api::gpu::GPUSampler, api::gpu::GPUSamplerDescriptor, \
api::gpu::GPUShaderModule, api::gpu::GPUShaderModuleDescriptor, \
api::gpu::GPUPipelineLayout, api::gpu::GPUPipelineLayoutDescriptor
api::gpu::GPUPipelineLayout, api::gpu::GPUPipelineLayoutDescriptor, \
api::gpu::GPUComputePipeline, api::gpu::GPUComputePipelineDescriptor, \
api::gpu::GPUProgrammableStage, api::gpu::GPUCommandEncoder, \
api::gpu::GPUCommandEncoderDescriptor

}; // namespace workerd::api::gpu
4 changes: 4 additions & 0 deletions src/workerd/api/gpu/webgpu-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,9 @@ export const read_sync_stack = {
},
});
ok(computePipeline);

// Commands submission
const commandEncoder = device.createCommandEncoder();
ok(commandEncoder);
},
};