feat(deno): accept GPUTexture as GPUBindingResource

This commit is contained in:
Erich Gubler committed 2025-09-26 17:51:35 -04:00
1 parent 375095f789
commit 2b71b1f99d
7 files changed
+110 -13

No files matched your search

+5 -7
View File
@@ -110,14 +110,12 @@ webgpu:api,validation,image_copy,layout_related:copy_end_overflows_u64:*
fails-if(dx12) webgpu:api,validation,image_copy,layout_related:offset_alignment:*
webgpu:api,validation,image_copy,texture_related:format:dimension="1d";*
webgpu:api,validation,queue,submit:command_buffer,*
// `GPUTexture` as `GPUTextureView` not supported by Deno. https://github.com/gfx-rs/wgpu/issues/8200
//FAIL: webgpu:api,validation,render_pass,render_pass_descriptor:attachments,one_color_attachment:
//FAIL: webgpu:api,validation,render_pass,render_pass_descriptor:attachments,one_depth_stencil_attachment:
webgpu:api,validation,render_pass,render_pass_descriptor:attachments,same_size:
webgpu:api,validation,render_pass,render_pass_descriptor:attachments,color_depth_mismatch:
webgpu:api,validation,render_pass,render_pass_descriptor:attachments,layer_count:*
webgpu:api,validation,render_pass,render_pass_descriptor:attachments,mip_level_count:*
webgpu:api,validation,render_pass,render_pass_descriptor:attachments,*
webgpu:api,validation,render_pass,render_pass_descriptor:color_attachments,depthSlice,bound_check:*
// Fails due to missing validation.
// FAIL: webgpu:api,validation,render_pass,render_pass_descriptor:color_attachments,depthSlice,overlaps,diff_miplevel:*
webgpu:api,validation,render_pass,render_pass_descriptor:resolveTarget,*
webgpu:api,validation,render_pass,resolve:resolve_attachment:*
webgpu:api,validation,texture,rg11b10ufloat_renderable:*
webgpu:api,operation,render_pipeline,overrides:*
webgpu:api,operation,rendering,basic:clear:*
+12
View File
@@ -17,6 +17,7 @@ use deno_core::WebIDL;
use crate::buffer::GPUBuffer;
use crate::error::GPUGenericError;
use crate::sampler::GPUSampler;
use crate::texture::GPUTexture;
use crate::texture::GPUTextureView;
use crate::Instance;
@@ -93,6 +94,7 @@ pub(crate) struct GPUBufferBinding {
pub(crate) enum GPUBindingResource {
Sampler(Ptr<GPUSampler>),
Texture(Ptr<GPUTexture>),
TextureView(Ptr<GPUTextureView>),
Buffer(Ptr<GPUBuffer>),
BufferBinding(GPUBufferBinding),
@@ -116,6 +118,16 @@ impl<'a> WebIdlConverter<'a> for GPUBindingResource {
options,
)
.map(Self::Sampler)
.or_else(|_| {
<Ptr<GPUTexture>>::convert(
scope,
value,
prefix.clone(),
context.borrowed(),
options,
)
.map(Self::Texture)
})
.or_else(|_| {
<Ptr<GPUTextureView>>::convert(
scope,
+5 -3
View File
@@ -75,9 +75,11 @@ impl GPUCommandEncoder {
.map(|attachment| {
attachment.into_option().map(|attachment| {
wgpu_core::command::RenderPassColorAttachment {
view: attachment.view.id,
view: attachment.view.to_view_id(),
depth_slice: attachment.depth_slice,
resolve_target: attachment.resolve_target.map(|target| target.id),
resolve_target: attachment
.resolve_target
.map(|target| target.to_view_id()),
load_op: attachment
.load_op
.with_default_value(attachment.clear_value.map(Into::into)),
@@ -103,7 +105,7 @@ impl GPUCommandEncoder {
}
Ok(wgpu_core::command::RenderPassDepthStencilAttachment {
view: attachment.view.id,
view: attachment.view.to_view_id(),
depth: PassChannel {
load_op: attachment
.depth_load_op
+4
View File
@@ -230,6 +230,7 @@ impl GPUDevice {
instance: self.instance.clone(),
error_handler: self.error_handler.clone(),
id,
default_view_id: Default::default(),
label: descriptor.label,
size: wgpu_descriptor.size,
mip_level_count: wgpu_descriptor.mip_level_count,
@@ -403,6 +404,9 @@ impl GPUDevice {
GPUBindingResource::Sampler(sampler) => {
BindingResource::Sampler(sampler.id)
}
GPUBindingResource::Texture(texture) => {
BindingResource::TextureView(texture.default_view_id())
}
GPUBindingResource::TextureView(texture_view) => {
BindingResource::TextureView(texture_view.id)
}
+53 -3
View File
@@ -7,6 +7,10 @@ use std::num::NonZeroU64;
use deno_core::cppgc::Ptr;
use deno_core::op2;
use deno_core::v8;
use deno_core::v8::HandleScope;
use deno_core::v8::Local;
use deno_core::v8::Value;
use deno_core::webidl::ContextFn;
use deno_core::webidl::IntOptions;
use deno_core::webidl::Nullable;
use deno_core::webidl::WebIdlConverter;
@@ -17,6 +21,7 @@ use deno_core::WebIDL;
use crate::buffer::GPUBuffer;
use crate::error::GPUGenericError;
use crate::render_bundle::GPURenderBundle;
use crate::texture::GPUTexture;
use crate::texture::GPUTextureView;
use crate::webidl::GPUColor;
use crate::Instance;
@@ -441,10 +446,10 @@ pub(crate) struct GPURenderPassDescriptor {
#[derive(WebIDL)]
#[webidl(dictionary)]
pub(crate) struct GPURenderPassColorAttachment {
pub view: Ptr<GPUTextureView>,
pub view: GPUTextureOrView,
#[options(enforce_range = true)]
pub depth_slice: Option<u32>,
pub resolve_target: Option<Ptr<GPUTextureView>>,
pub resolve_target: Option<GPUTextureOrView>,
pub clear_value: Option<GPUColor>,
pub load_op: GPULoadOp,
pub store_op: GPUStoreOp,
@@ -495,7 +500,7 @@ impl From<GPUStoreOp> for wgpu_core::command::StoreOp {
#[derive(WebIDL)]
#[webidl(dictionary)]
pub(crate) struct GPURenderPassDepthStencilAttachment {
pub view: Ptr<GPUTextureView>,
pub view: GPUTextureOrView,
pub depth_clear_value: Option<f32>,
pub depth_load_op: Option<GPULoadOp>,
pub depth_store_op: Option<GPUStoreOp>,
@@ -519,3 +524,48 @@ pub(crate) struct GPURenderPassTimestampWrites {
#[options(enforce_range = true)]
pub end_of_pass_write_index: Option<u32>,
}
pub(crate) enum GPUTextureOrView {
Texture(Ptr<GPUTexture>),
TextureView(Ptr<GPUTextureView>),
}
impl GPUTextureOrView {
pub(crate) fn to_view_id(&self) -> wgpu_core::id::TextureViewId {
match self {
Self::Texture(texture) => texture.default_view_id(),
Self::TextureView(texture_view) => texture_view.id,
}
}
}
impl<'a> WebIdlConverter<'a> for GPUTextureOrView {
type Options = ();
fn convert<'b>(
scope: &mut HandleScope<'a>,
value: Local<'a, Value>,
prefix: Cow<'static, str>,
context: ContextFn<'b>,
options: &Self::Options,
) -> Result<Self, WebIdlError> {
<Ptr<GPUTexture>>::convert(
scope,
value,
prefix.clone(),
context.borrowed(),
options,
)
.map(Self::Texture)
.or_else(|_| {
<Ptr<GPUTextureView>>::convert(
scope,
value,
prefix.clone(),
context.borrowed(),
options,
)
.map(Self::TextureView)
})
}
}
+1
View File
@@ -146,6 +146,7 @@ impl GPUCanvasContext {
instance: config.device.instance.clone(),
error_handler: config.device.error_handler.clone(),
id,
default_view_id: Default::default(),
label: "".to_string(),
size: wgpu_types::Extent3d {
width: *self.width.borrow(),
+30
View File
@@ -1,5 +1,7 @@
// Copyright 2018-2025 the Deno authors. MIT license.
use std::sync::OnceLock;
use deno_core::op2;
use deno_core::webidl::WebIdlInterfaceConverter;
use deno_core::GarbageCollected;
@@ -43,6 +45,7 @@ pub struct GPUTexture {
pub error_handler: super::error::ErrorHandler,
pub id: wgpu_core::id::TextureId,
pub default_view_id: OnceLock<wgpu_core::id::TextureViewId>,
pub label: String,
@@ -54,8 +57,35 @@ pub struct GPUTexture {
pub usage: u32,
}
impl GPUTexture {
pub(crate) fn default_view_id(&self) -> wgpu_core::id::TextureViewId {
*self.default_view_id.get_or_init(|| {
let (id, err) =
self
.instance
.texture_create_view(self.id, &Default::default(), None);
if let Some(err) = err {
use wgpu_types::error::WebGpuError;
assert_ne!(
err.webgpu_error_type(),
wgpu_types::error::ErrorType::Validation,
concat!(
"getting default view for a texture ",
"caused a validation error (!?)"
)
);
self.error_handler.push_error(Some(err));
}
id
})
}
}
impl Drop for GPUTexture {
fn drop(&mut self) {
if let Some(id) = self.default_view_id.take() {
self.instance.texture_view_drop(id).unwrap();
}
self.instance.texture_drop(self.id);
}
}