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

feat(op_crates): webgpu #7977

Merged
merged 184 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from 182 commits
Commits
Show all changes
184 commits
Select commit Hold shift + click to select a range
22b7a0b
initial work
crowlKats Oct 8, 2020
3112ee4
up to pipeline
crowlKats Oct 9, 2020
097f7bc
up to renderbundleencoder
crowlKats Oct 9, 2020
708007f
cleanup
crowlKats Oct 9, 2020
1c2434c
switch to wgpu-core
crowlKats Oct 11, 2020
ea292e3
Better TODOs
crowlKats Oct 11, 2020
cc61da9
command encoder debug ops
crowlKats Oct 11, 2020
512ae29
add render pass
crowlKats Oct 12, 2020
c426657
cleanup
crowlKats Oct 12, 2020
11032e7
compute pass
crowlKats Oct 13, 2020
49c82d0
render_bundle_encoder
crowlKats Oct 13, 2020
477e15f
fmt
crowlKats Oct 13, 2020
0ca3900
queue
crowlKats Oct 13, 2020
fbd21ab
buffer
crowlKats Oct 13, 2020
39cb0ea
setBindGroup
crowlKats Oct 13, 2020
5aeca94
split up init
crowlKats Oct 13, 2020
28d1f96
cleanup
crowlKats Oct 14, 2020
3a2ef8b
fmt
crowlKats Oct 14, 2020
40322fa
Merge branch 'master' into webgpu
crowlKats Oct 14, 2020
1b881c7
cleanup
crowlKats Oct 14, 2020
1275b03
use gfx_select and cleanup
crowlKats Oct 14, 2020
12ccbf4
Merge branch 'master' into webgpu
crowlKats Oct 14, 2020
ef57dcf
cleanup
crowlKats Oct 14, 2020
4f270c9
fix most of compiler errors
bartlomieju Oct 14, 2020
f4ae3ff
fix more compile errors
bartlomieju Oct 14, 2020
7cf4a35
fix more compile errors, part2
bartlomieju Oct 14, 2020
5c4dd3f
fix more compile errors, part3
bartlomieju Oct 14, 2020
c0b038f
bogus data
crowlKats Oct 14, 2020
cc321ea
real data
crowlKats Oct 14, 2020
ffbb6b7
fix more compile errors, part4
bartlomieju Oct 14, 2020
6d00506
fix more compile errors, part5
bartlomieju Oct 14, 2020
ca2578b
fix NonZeroU8
crowlKats Oct 15, 2020
679b461
create webgpu instance and add to navigator & workerNavigator
crowlKats Oct 15, 2020
fbb5a5b
bump wgpu-core and fix VertexBufferDescriptor
crowlKats Oct 15, 2020
704e4db
fix gpubuffer mapping
crowlKats Oct 15, 2020
877119f
fix more compiler errors
bartlomieju Oct 16, 2020
7e9ee9a
fix remaining compile errors
crowlKats Oct 16, 2020
f13f86e
fix some lint errors
crowlKats Oct 16, 2020
bec5d74
lint rust
bartlomieju Oct 16, 2020
9b1e83f
fix buffer mapping
crowlKats Oct 16, 2020
8107a61
fixes & temporary get_mapped_range solution
crowlKats Oct 16, 2020
c6f5355
fix createBindGroup resource
crowlKats Oct 16, 2020
9f7ce58
Merge branch 'master' into webgpu
crowlKats Nov 23, 2020
f08f003
Merge branch 'master' into webgpu
crowlKats Dec 7, 2020
0903f68
move to own op crate
crowlKats Dec 9, 2020
7b86e73
Merge branch 'master' into webgpu
crowlKats Dec 9, 2020
05816a8
Merge branch 'master' into webgpu
crowlKats Dec 14, 2020
322b03c
Merge branch 'master' into webgpu
crowlKats Feb 5, 2021
8bbccbc
change to new resource table
crowlKats Feb 6, 2021
d875d85
fix lint
crowlKats Feb 8, 2021
0f5a904
stuff
crowlKats Feb 8, 2021
12b2870
dont hold refcell over await point
lucacasonato Feb 8, 2021
536fe2b
fmt
lucacasonato Feb 8, 2021
c34bf29
poll device during buffer_map_async
lucacasonato Feb 8, 2021
713f553
Merge remote-tracking branch 'origin/master' into webgpu
lucacasonato Feb 8, 2021
e7e149d
fix
lucacasonato Feb 8, 2021
4f46979
remove AsyncRefCell
lucacasonato Feb 8, 2021
aae3eaf
update wgpu
crowlKats Feb 9, 2021
b99cfbd
remove crate aliases
crowlKats Feb 9, 2021
2c37fd8
clean up
crowlKats Feb 9, 2021
00993bf
continue update
crowlKats Feb 9, 2021
737e0b3
use symbol instead of weakmaps
crowlKats Feb 9, 2021
c77be71
add missing labels
crowlKats Feb 9, 2021
75e60f2
clean up comments
crowlKats Feb 9, 2021
4d064ff
clear some todos
crowlKats Feb 9, 2021
d9887e7
Merge branch 'webgpu' of github.com:crowlKats/deno into webgpu
lucacasonato Feb 9, 2021
463d2f7
fix bugs
crowlKats Feb 9, 2021
a8e05b8
Merge remote-tracking branch 'origin/webgpu' into webgpu
crowlKats Feb 9, 2021
2cc9d3c
fix build
lucacasonato Feb 9, 2021
c4567e7
Merge remote-tracking branch 'origin/master' into webgpu
lucacasonato Feb 9, 2021
bb275eb
get rid of segfault via experimental means on macos x86
crowlKats Feb 10, 2021
12bbab9
add tracing
crowlKats Feb 10, 2021
4aa05db
don't copy mapped buffer
lucacasonato Feb 10, 2021
bbf88a3
Merge remote-tracking branch 'origin/master' into webgpu
lucacasonato Feb 10, 2021
b5fc318
add unit test
lucacasonato Feb 10, 2021
e40dbb2
fmt lint
lucacasonato Feb 10, 2021
2d03404
remove resource sanitize
lucacasonato Feb 10, 2021
69bacfd
lint
lucacasonato Feb 10, 2021
675db5a
add typings
crowlKats Feb 10, 2021
8b4b882
fix typings
crowlKats Feb 10, 2021
61d214b
Install mesa-vulkan
lucacasonato Feb 10, 2021
b3138d1
delete unused structs
crowlKats Feb 10, 2021
6551011
change dts
lucacasonato Feb 10, 2021
90dfd29
Merge remote-tracking branch 'origin/webgpu' into webgpu
crowlKats Feb 10, 2021
03c4faa
webgpu_test.ts
lucacasonato Feb 10, 2021
68f0f58
fix workerNavigator dts
crowlKats Feb 10, 2021
844bb3a
Merge remote-tracking branch 'origin/webgpu' into webgpu
crowlKats Feb 10, 2021
927d5db
only run on windows
lucacasonato Feb 10, 2021
8575291
Merge branch 'webgpu' of github.com:crowlKats/deno into webgpu
lucacasonato Feb 10, 2021
24daeb6
don't install mesa-vulkan
lucacasonato Feb 10, 2021
6bab6b9
whoops
lucacasonato Feb 10, 2021
6f69ae5
return null on no adapter
crowlKats Feb 10, 2021
94ba01e
Merge remote-tracking branch 'origin/webgpu' into webgpu
crowlKats Feb 10, 2021
52df4bb
move instance to OpState
crowlKats Feb 10, 2021
2d06ee6
rename js file
crowlKats Feb 10, 2021
8ba9549
add unstable check
crowlKats Feb 10, 2021
e9236b9
fix stuff
crowlKats Feb 11, 2021
c7d0e0d
Merge remote-tracking branch 'origin/master' into webgpu
lucacasonato Feb 11, 2021
7c5b8a0
fix stuff
crowlKats Feb 11, 2021
da2b53d
expose classes and make constructors private
crowlKats Feb 12, 2021
efb63bc
fix bindgroup entry
crowlKats Feb 13, 2021
be09f84
Merge branch 'master' into webgpu
crowlKats Feb 14, 2021
c739529
support more features
crowlKats Feb 14, 2021
e6b7fac
add setters
crowlKats Feb 14, 2021
5fd635d
add typings for extended features
crowlKats Feb 14, 2021
59d99d4
Revert "add setters"
crowlKats Feb 15, 2021
249aed6
Merge remote-tracking branch 'origin/master' into webgpu
lucacasonato Feb 15, 2021
e5916a0
fix writeBuffer
crowlKats Feb 15, 2021
35d24fb
Merge branch 'webgpu' of github.com:crowlKats/deno into webgpu
lucacasonato Feb 15, 2021
401c787
fix depthStencilAttachment
crowlKats Feb 15, 2021
1aa856d
Merge remote-tracking branch 'origin/master' into webgpu
lucacasonato Feb 15, 2021
821490a
start on webidl
lucacasonato Feb 16, 2021
2a3cf49
add DENO_WEBGPU_TRACE env var
crowlKats Feb 17, 2021
ee1af0a
fix free before use in dynamic_offsets_data
lucacasonato Feb 17, 2021
0c32f51
start on webidl
lucacasonato Feb 17, 2021
da31780
Merge branch 'webgpu' of github.com:crowlKats/deno into webgpu
lucacasonato Feb 17, 2021
9384999
Merge remote-tracking branch 'origin/master' into webgpu
lucacasonato Feb 17, 2021
5d1c431
fix another free before use
lucacasonato Feb 17, 2021
0262c81
fmt
lucacasonato Feb 17, 2021
9aa59ac
fix env var
crowlKats Feb 17, 2021
acf458a
More IDL
lucacasonato Feb 17, 2021
1bd9cb1
fmt + lint
lucacasonato Feb 17, 2021
3c14678
Merge branch 'webgpu' of github.com:crowlKats/deno into webgpu
lucacasonato Feb 17, 2021
e56dfe7
nearly done with webidl
lucacasonato Feb 17, 2021
f97fba7
GPUDevice webidl
lucacasonato Feb 17, 2021
bea8d68
add flags classes
crowlKats Feb 17, 2021
635169b
adapter features
crowlKats Feb 17, 2021
84df17f
adapter limits
crowlKats Feb 17, 2021
277b156
webidl for GPUBuffer
lucacasonato Feb 17, 2021
e6b8319
Merge branch 'webgpu' of github.com:crowlKats/deno into webgpu
lucacasonato Feb 17, 2021
52d8cbb
add GPUShaderStage
crowlKats Feb 18, 2021
b7fc535
add GPUShaderStage
crowlKats Feb 18, 2021
f6e8367
Merge remote-tracking branch 'origin/webgpu' into webgpu
crowlKats Feb 18, 2021
539e9ba
fix
lucacasonato Feb 18, 2021
6ed1e68
createSequenceConverter
lucacasonato Feb 18, 2021
c22374e
Merge branch 'webgpu' of github.com:crowlKats/deno into webgpu
lucacasonato Feb 18, 2021
a3f3a82
Merge branch 'master' into webgpu
crowlKats Feb 18, 2021
12cd525
change args to use structs
crowlKats Feb 18, 2021
72ef821
add typings for flag classes
crowlKats Feb 18, 2021
4071ce9
fix sequence converter and idl_types
lucacasonato Feb 18, 2021
02b2f3e
Merge branch 'webgpu' of github.com:crowlKats/deno into webgpu
lucacasonato Feb 18, 2021
55f7d1f
fmt
lucacasonato Feb 18, 2021
e73396b
more validation
lucacasonato Feb 18, 2021
ff0616a
add descriptor slot to queryset
crowlKats Feb 18, 2021
c2b0cb6
add consumable logic
crowlKats Feb 18, 2021
b2694bb
initial inspect stuff
crowlKats Feb 18, 2021
8cdd367
first cleanup
lucacasonato Feb 18, 2021
4696b99
Merge branch 'webgpu' of github.com:crowlKats/deno into webgpu
lucacasonato Feb 18, 2021
c5d3b1b
add inspect for all classes
crowlKats Feb 18, 2021
60ad162
add createPipelineAsync polyfills
crowlKats Feb 18, 2021
aaa382c
fix async get
crowlKats Feb 18, 2021
4224a2a
resource cleanup
lucacasonato Feb 19, 2021
3623d63
Merge branch 'webgpu' of github.com:crowlKats/deno into webgpu
lucacasonato Feb 19, 2021
c70f029
fmt
lucacasonato Feb 19, 2021
13a73e9
fmt + lint
lucacasonato Feb 19, 2021
1b1f9be
unit test passes
lucacasonato Feb 19, 2021
08745f8
add custominspect for GPU
crowlKats Feb 19, 2021
cab9e74
fix tests
lucacasonato Feb 19, 2021
6212dd1
change adapter in tests
lucacasonato Feb 19, 2021
01e3b55
Merge remote-tracking branch 'origin/master' into webgpu
lucacasonato Feb 19, 2021
2f040df
Merge remote-tracking branch 'origin/master' into webgpu
lucacasonato Feb 19, 2021
3d47780
Merge remote-tracking branch 'origin/main' into webgpu
lucacasonato Feb 19, 2021
4015f16
more jsdoc + webidl
lucacasonato Feb 19, 2021
e835bfd
Merge branch 'webgpu' of github.com:crowlKats/deno into webgpu
lucacasonato Feb 19, 2021
3ab1a79
move consumed chheck after webidl checks
crowlKats Feb 20, 2021
952e816
assertDevice + assertDeviceMatch + assertResource
lucacasonato Feb 21, 2021
6c9d960
Merge branch 'webgpu' of github.com:crowlKats/deno into webgpu
lucacasonato Feb 21, 2021
be59908
fix
lucacasonato Feb 21, 2021
43d4839
Merge remote-tracking branch 'origin/main' into webgpu
lucacasonato Feb 22, 2021
0c37c29
assert device for GPURenderPassEncoder
lucacasonato Feb 22, 2021
55c39ed
assert device for GPUComputePassEncoder
lucacasonato Feb 22, 2021
21a5e82
assert device for GPURenderBundleEncoder
lucacasonato Feb 22, 2021
2e5f2b3
chore(core): optional args for registerErrorClass
lucacasonato Feb 25, 2021
cf076ec
error handling
lucacasonato Feb 25, 2021
daa48a9
fmt + lint
lucacasonato Feb 25, 2021
e7fc4ee
Merge remote-tracking branch 'origin/main' into webgpu
lucacasonato Feb 25, 2021
8b2eddc
add readme
lucacasonato Feb 26, 2021
0524f71
add DENO_WEBGPU_TRACE env var & CI information
crowlKats Feb 28, 2021
465a832
add hello-triangle example as test
crowlKats Feb 28, 2021
5d758db
trigger ci
lucacasonato Mar 1, 2021
a3b17b9
Merge remote-tracking branch 'origin/main' into webgpu
lucacasonato Mar 1, 2021
be27fae
lint
lucacasonato Mar 1, 2021
115585b
change interfaces to classes
crowlKats Mar 1, 2021
dcf513c
Merge remote-tracking branch 'origin/webgpu' into webgpu
crowlKats Mar 1, 2021
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
557 changes: 557 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use deno_core::RuntimeOptions;
use deno_runtime::deno_crypto;
use deno_runtime::deno_fetch;
use deno_runtime::deno_web;
use deno_runtime::deno_webgpu;
use deno_runtime::deno_websocket;
use regex::Regex;
use std::collections::HashMap;
Expand Down Expand Up @@ -62,6 +63,7 @@ fn create_compiler_snapshot(
let mut op_crate_libs = HashMap::new();
op_crate_libs.insert("deno.web", deno_web::get_declaration());
op_crate_libs.insert("deno.fetch", deno_fetch::get_declaration());
op_crate_libs.insert("deno.webgpu", deno_webgpu::get_declaration());
op_crate_libs.insert("deno.websocket", deno_websocket::get_declaration());
op_crate_libs.insert("deno.crypto", deno_crypto::get_declaration());

Expand Down Expand Up @@ -260,6 +262,10 @@ fn main() {
"cargo:rustc-env=DENO_FETCH_LIB_PATH={}",
deno_fetch::get_declaration().display()
);
println!(
"cargo:rustc-env=DENO_WEBGPU_LIB_PATH={}",
deno_webgpu::get_declaration().display()
);
println!(
"cargo:rustc-env=DENO_WEBSOCKET_LIB_PATH={}",
deno_websocket::get_declaration().display()
Expand Down
7 changes: 7 additions & 0 deletions cli/dts/lib.deno.window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/// <reference no-default-lib="true" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.shared_globals" />
/// <reference lib="deno.webgpu" />
/// <reference lib="esnext" />

declare class Window extends EventTarget {
Expand All @@ -17,12 +18,18 @@ declare class Window extends EventTarget {
confirm: (message?: string) => boolean;
prompt: (message?: string, defaultValue?: string) => string | null;
Deno: typeof Deno;
navigator: Navigator;
}

declare var window: Window & typeof globalThis;
declare var self: Window & typeof globalThis;
declare var onload: ((this: Window, ev: Event) => any) | null;
declare var onunload: ((this: Window, ev: Event) => any) | null;
declare var navigator: Navigator;

declare interface Navigator {
readonly gpu: GPU;
}

/**
* Shows the given message and waits for the enter key pressed.
Expand Down
8 changes: 8 additions & 0 deletions cli/dts/lib.deno.worker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/// <reference no-default-lib="true" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.shared_globals" />
/// <reference lib="deno.webgpu" />
/// <reference lib="esnext" />

declare class WorkerGlobalScope {
Expand All @@ -29,6 +30,13 @@ declare class WorkerGlobalScope {
close: () => void;
postMessage: (message: any) => void;
Deno: typeof Deno;
navigator: WorkerNavigator;
}

declare var navigator: WorkerNavigator;

declare interface WorkerNavigator {
readonly gpu: GPU;
}

declare class DedicatedWorkerGlobalScope extends WorkerGlobalScope {
Expand Down
1 change: 1 addition & 0 deletions cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ static ENV_VARIABLES_HELP: &str = r#"ENVIRONMENT VARIABLES:
DENO_DIR Set the cache directory
DENO_INSTALL_ROOT Set deno install's output directory
(defaults to $HOME/.deno/bin)
DENO_WEBGPU_TRACE Directory to use for wgpu traces
HTTP_PROXY Proxy address for HTTP requests
(module downloads, fetch)
HTTPS_PROXY Proxy address for HTTPS requests
Expand Down
5 changes: 4 additions & 1 deletion cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,11 @@ fn print_cache_info(

pub fn get_types(unstable: bool) -> String {
let mut types = format!(
"{}\n{}\n{}\n{}\n{}\n{}",
"{}\n{}\n{}\n{}\n{}\n{}\n{}",
crate::tsc::DENO_NS_LIB,
crate::tsc::DENO_WEB_LIB,
crate::tsc::DENO_FETCH_LIB,
crate::tsc::DENO_WEBGPU_LIB,
crate::tsc::DENO_WEBSOCKET_LIB,
crate::tsc::SHARED_GLOBALS_LIB,
crate::tsc::WINDOW_LIB,
Expand Down Expand Up @@ -1022,6 +1023,8 @@ fn init_logger(maybe_level: Option<Level>) {
)
// https://github.com/denoland/deno/issues/6641
.filter_module("rustyline", LevelFilter::Off)
// wgpu backend crates (gfx_backend), have a lot of useless INFO and WARN logs
.filter_module("gfx", LevelFilter::Error)
.format(|buf, record| {
let mut target = record.target().to_string();
if let Some(line_no) = record.line() {
Expand Down
1 change: 1 addition & 0 deletions cli/tests/unit/unit_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ import "./write_text_file_test.ts";
import "./performance_test.ts";
import "./version_test.ts";
import "./websocket_test.ts";
import "./webgpu_test.ts";
225 changes: 225 additions & 0 deletions cli/tests/unit/webgpu_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
// TODO(lucacasonato): remove when GPUBufferUsage and friends are added to dlint
// deno-lint-ignore-file no-undef

import { assert, assertEquals, unitTest } from "./test_util.ts";

let isCI: boolean;
try {
isCI = (Deno.env.get("CI")?.length ?? 0) > 0;
} catch {
isCI = true;
}

// Skip this test on linux CI, because the vulkan emulator is not good enough
// yet, and skip on macOS because these do not have virtual GPUs.
unitTest({
perms: { read: true, env: true },
ignore: (Deno.build.os === "linux" || Deno.build.os === "darwin") && isCI,
}, async function webgpuComputePass() {
const adapter = await navigator.gpu.requestAdapter();
assert(adapter);

const numbers = [1, 4, 3, 295];

const device = await adapter.requestDevice();
assert(device);

const shaderCode = await Deno.readTextFile(
"cli/tests/webgpu_computepass_shader.wgsl",
);

const shaderModule = device.createShaderModule({
code: shaderCode,
});

const size = new Uint32Array(numbers).byteLength;

const stagingBuffer = device.createBuffer({
size: size,
usage: 1 | 8,
});

const storageBuffer = device.createBuffer({
label: "Storage Buffer",
size: size,
usage: 0x80 | 8 | 4,
mappedAtCreation: true,
});

const buf = new Uint32Array(storageBuffer.getMappedRange());

buf.set(numbers);

storageBuffer.unmap();

const bindGroupLayout = device.createBindGroupLayout({
entries: [
{
binding: 0,
visibility: 4,
buffer: {
type: "storage",
minBindingSize: 4,
},
},
],
});

const bindGroup = device.createBindGroup({
layout: bindGroupLayout,
entries: [
{
binding: 0,
resource: {
buffer: storageBuffer,
},
},
],
});

const pipelineLayout = device.createPipelineLayout({
bindGroupLayouts: [bindGroupLayout],
});

const computePipeline = device.createComputePipeline({
layout: pipelineLayout,
compute: {
module: shaderModule,
entryPoint: "main",
},
});

const encoder = device.createCommandEncoder();

const computePass = encoder.beginComputePass();
computePass.setPipeline(computePipeline);
computePass.setBindGroup(0, bindGroup);
computePass.insertDebugMarker("compute collatz iterations");
computePass.dispatch(numbers.length);
computePass.endPass();

encoder.copyBufferToBuffer(storageBuffer, 0, stagingBuffer, 0, size);

device.queue.submit([encoder.finish()]);

await stagingBuffer.mapAsync(1);

const data = stagingBuffer.getMappedRange();

assertEquals(new Uint32Array(data), new Uint32Array([0, 2, 7, 55]));

stagingBuffer.unmap();

device.destroy();

// TODO(lucacasonato): webgpu spec should add a explicit destroy method for
// adapters.
const resources = Object.keys(Deno.resources());
Deno.close(Number(resources[resources.length - 1]));
});

// Skip this test on linux CI, because the vulkan emulator is not good enough
// yet, and skip on macOS because these do not have virtual GPUs.
unitTest({
perms: { read: true, env: true },
ignore: (Deno.build.os === "linux" || Deno.build.os === "darwin") && isCI,
}, async function webgpuHelloTriangle() {
const adapter = await navigator.gpu.requestAdapter();
assert(adapter);

const device = await adapter.requestDevice();
assert(device);

const shaderCode = await Deno.readTextFile(
"cli/tests/webgpu_hellotriangle_shader.wgsl",
);

const shaderModule = device.createShaderModule({
code: shaderCode,
});

const pipelineLayout = device.createPipelineLayout({
bindGroupLayouts: [],
});

const renderPipeline = device.createRenderPipeline({
layout: pipelineLayout,
vertex: {
module: shaderModule,
entryPoint: "vs_main",
},
fragment: {
module: shaderModule,
entryPoint: "fs_main",
targets: [
{
format: "rgba8unorm-srgb",
},
],
},
});

const dimensions = {
width: 200,
height: 200,
};
const unpaddedBytesPerRow = dimensions.width * 4;
const align = 256;
const paddedBytesPerRowPadding = (align - unpaddedBytesPerRow % align) %
align;
const paddedBytesPerRow = unpaddedBytesPerRow + paddedBytesPerRowPadding;

const outputBuffer = device.createBuffer({
label: "Capture",
size: paddedBytesPerRow * dimensions.height,
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
});
const texture = device.createTexture({
label: "Capture",
size: dimensions,
format: "rgba8unorm-srgb",
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
});

const encoder = device.createCommandEncoder();
const renderPass = encoder.beginRenderPass({
colorAttachments: [
{
view: texture.createView(),
storeOp: "store",
loadValue: [0, 1, 0, 1],
},
],
});
renderPass.setPipeline(renderPipeline);
renderPass.draw(3, 1);
renderPass.endPass();

encoder.copyTextureToBuffer(
{
texture,
},
{
buffer: outputBuffer,
bytesPerRow: paddedBytesPerRow,
rowsPerImage: 0,
},
dimensions,
);

device.queue.submit([encoder.finish()]);

await outputBuffer.mapAsync(1);
const data = new Uint8Array(outputBuffer.getMappedRange());

assertEquals(data, await Deno.readFile("cli/tests/webgpu_hellotriangle.out"));

outputBuffer.unmap();

device.destroy();

// TODO(lucacasonato): webgpu spec should add a explicit destroy method for
// adapters.
const resources = Object.keys(Deno.resources());
Deno.close(Number(resources[resources.length - 1]));
});
39 changes: 39 additions & 0 deletions cli/tests/webgpu_computepass_shader.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[[builtin(global_invocation_id)]]
var global_id: vec3<u32>;

[[block]]
struct PrimeIndices {
data: [[stride(4)]] array<u32>;
}; // this is used as both input and output for convenience

[[group(0), binding(0)]]
var<storage> v_indices: [[access(read_write)]] PrimeIndices;

// The Collatz Conjecture states that for any integer n:
// If n is even, n = n/2
// If n is odd, n = 3n+1
// And repeat this process for each new n, you will always eventually reach 1.
// Though the conjecture has not been proven, no counterexample has ever been found.
// This function returns how many times this recurrence needs to be applied to reach 1.
fn collatz_iterations(n_base: u32) -> u32{
var n: u32 = n_base;
var i: u32 = 0u;
loop {
if (n <= 1u) {
break;
}
if (n % 2u == 0u) {
n = n / 2u;
}
else {
n = 3u * n + 1u;
}
i = i + 1u;
}
return i;
}

[[stage(compute), workgroup_size(1)]]
fn main() {
v_indices.data[global_id.x] = collatz_iterations(v_indices.data[global_id.x]);
}
Binary file added cli/tests/webgpu_hellotriangle.out
Binary file not shown.
19 changes: 19 additions & 0 deletions cli/tests/webgpu_hellotriangle_shader.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[[builtin(vertex_index)]]
var<in> in_vertex_index: u32;
[[builtin(position)]]
var<out> out_pos: vec4<f32>;

[[stage(vertex)]]
fn vs_main() {
var x: f32 = f32(i32(in_vertex_index) - 1);
var y: f32 = f32(i32(in_vertex_index & 1) * 2 - 1);
out_pos = vec4<f32>(x, y, 0.0, 1.0);
}

[[location(0)]]
var<out> out_color: vec4<f32>;

[[stage(fragment)]]
fn fs_main() {
out_color = vec4<f32>(1.0, 0.0, 0.0, 1.0);
}
Loading