summaryrefslogtreecommitdiff
path: root/ext/webgpu/shader.rs
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2021-08-11 12:27:05 +0200
committerGitHub <noreply@github.com>2021-08-11 12:27:05 +0200
commita0285e2eb88f6254f6494b0ecd1878db3a3b2a58 (patch)
tree90671b004537e20f9493fd3277ffd21d30b39a0e /ext/webgpu/shader.rs
parent3a6994115176781b3a93d70794b1b81bc95e42b4 (diff)
Rename extensions/ directory to ext/ (#11643)
Diffstat (limited to 'ext/webgpu/shader.rs')
-rw-r--r--ext/webgpu/shader.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/ext/webgpu/shader.rs b/ext/webgpu/shader.rs
new file mode 100644
index 000000000..f48411969
--- /dev/null
+++ b/ext/webgpu/shader.rs
@@ -0,0 +1,70 @@
+// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
+
+use deno_core::error::bad_resource_id;
+use deno_core::error::null_opbuf;
+use deno_core::error::AnyError;
+use deno_core::ResourceId;
+use deno_core::ZeroCopyBuf;
+use deno_core::{OpState, Resource};
+use serde::Deserialize;
+use std::borrow::Cow;
+
+use super::error::WebGpuResult;
+
+pub(crate) struct WebGpuShaderModule(pub(crate) wgpu_core::id::ShaderModuleId);
+impl Resource for WebGpuShaderModule {
+ fn name(&self) -> Cow<str> {
+ "webGPUShaderModule".into()
+ }
+}
+
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct CreateShaderModuleArgs {
+ device_rid: ResourceId,
+ label: Option<String>,
+ code: Option<String>,
+ _source_map: Option<()>, // not yet implemented
+}
+
+pub fn op_webgpu_create_shader_module(
+ state: &mut OpState,
+ args: CreateShaderModuleArgs,
+ zero_copy: Option<ZeroCopyBuf>,
+) -> Result<WebGpuResult, AnyError> {
+ let instance = state.borrow::<super::Instance>();
+ let device_resource = state
+ .resource_table
+ .get::<super::WebGpuDevice>(args.device_rid)
+ .ok_or_else(bad_resource_id)?;
+ let device = device_resource.0;
+
+ let source = match args.code {
+ Some(code) => {
+ wgpu_core::pipeline::ShaderModuleSource::Wgsl(Cow::from(code))
+ }
+ None => wgpu_core::pipeline::ShaderModuleSource::SpirV(Cow::from(unsafe {
+ match &zero_copy {
+ Some(zero_copy) => {
+ let (prefix, data, suffix) = zero_copy.align_to::<u32>();
+ assert!(prefix.is_empty());
+ assert!(suffix.is_empty());
+ data
+ }
+ None => return Err(null_opbuf()),
+ }
+ })),
+ };
+
+ let descriptor = wgpu_core::pipeline::ShaderModuleDescriptor {
+ label: args.label.map(Cow::from),
+ flags: wgpu_types::ShaderFlags::all(),
+ };
+
+ gfx_put!(device => instance.device_create_shader_module(
+ device,
+ &descriptor,
+ source,
+ std::marker::PhantomData
+ ) => state, WebGpuShaderModule)
+}