Skip to content

Commit

Permalink
update webgpu api (apache#6547)
Browse files Browse the repository at this point in the history
  • Loading branch information
WenheLI authored Sep 25, 2020
1 parent 56d8a99 commit 9f6a33a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@types/node": "^12.12.37",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"@typescript-eslint/parser": "^2.29.0",
"@webgpu/types": "^0.0.24",
"@webgpu/types": "^0.0.31",
"eslint": "^6.8.0",
"jest": "^26.0.1",
"rollup": "^2.7.6",
Expand Down
4 changes: 2 additions & 2 deletions web/src/rpc_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ export class RPCServer {
this.logger
);
try {
const gpuDevice: GPUDevice | undefined = await detectGPUDevice();
if (gpuDevice !== undefined) {
const gpuDevice: GPUDevice | undefined | null = await detectGPUDevice();
if (gpuDevice !== undefined && gpuDevice !== null) {
const label = gpuDevice.label?.toString() || "WebGPU";
this.log("Initialize GPU device: " + label);
inst.initWebGPU(gpuDevice);
Expand Down
16 changes: 10 additions & 6 deletions web/src/webgpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export type GPUPointer = number;
/**
* DetectGPU device in the environment.
*/
export async function detectGPUDevice(): Promise<GPUDevice | undefined> {
export async function detectGPUDevice(): Promise<GPUDevice | undefined | null> {
if (typeof navigator !== "undefined" && navigator.gpu !== undefined) {
const adapter = await navigator.gpu.requestAdapter();
return await adapter.requestDevice();
return await adapter?.requestDevice();
} else {
return undefined;
}
Expand Down Expand Up @@ -235,11 +235,14 @@ export class WebGPUContext {
nbytes: number
): void {
// Perhaps it would be more useful to use a staging buffer?
const [gpuTemp, cpuTemp] = this.device.createBufferMapped({
const gpuTemp = this.device.createBuffer({
mappedAtCreation: true,
size: nbytes,
usage: GPUBufferUsage.MAP_WRITE | GPUBufferUsage.COPY_SRC,
usage: GPUBufferUsage.MAP_WRITE | GPUBufferUsage.COPY_SRC
});

const cpuTemp = gpuTemp.getMappedRange();

const viewU8 = new Uint8Array(cpuTemp);
viewU8.set(this.memory.loadRawBytes(from, nbytes));
gpuTemp.unmap();
Expand Down Expand Up @@ -281,8 +284,9 @@ export class WebGPUContext {
this.device.defaultQueue.submit([copyCommands]);

this.numPendingReads += 1;
const readEvent = gpuTemp.mapReadAsync().then((data: ArrayBuffer) => {
this.memory.storeRawBytes(to, new Uint8Array(data));

const readEvent = gpuTemp.mapAsync(GPUMapMode.READ).then((data: unknown) => {
this.memory.storeRawBytes(to, new Uint8Array(data as ArrayBuffer));
this.numPendingReads -= 1;
gpuTemp.destroy();
});
Expand Down

0 comments on commit 9f6a33a

Please sign in to comment.