Skip to content

Commit

Permalink
Add GPUSampler and GPUTextureView to BindingResource
Browse files Browse the repository at this point in the history
Add validation for BindGroups
  • Loading branch information
kunalmohan committed Jun 17, 2020
1 parent abc3ed4 commit 00b3f78
Show file tree
Hide file tree
Showing 12 changed files with 591 additions and 123 deletions.
57 changes: 51 additions & 6 deletions components/script/dom/gpubindgroup.rs
Expand Up @@ -3,36 +3,81 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::GPUBindGroupBinding::GPUBindGroupMethods;
use crate::dom::bindings::codegen::Bindings::GPUBindGroupBinding::{
GPUBindGroupEntry, GPUBindGroupMethods,
};
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
use crate::dom::gpubindgrouplayout::GPUBindGroupLayout;
use crate::dom::gpubuffer::GPUBuffer;
use crate::dom::gputextureview::TextureSubresource;
use dom_struct::dom_struct;
use std::cell::Cell;
use webgpu::WebGPUBindGroup;
use std::collections::HashMap;
use webgpu::{WebGPUBindGroup, WebGPUDevice};

#[dom_struct]
pub struct GPUBindGroup {
reflector_: Reflector,
label: DomRefCell<Option<DOMString>>,
bind_group: WebGPUBindGroup,
device: WebGPUDevice,
layout: Dom<GPUBindGroupLayout>,
#[ignore_malloc_size_of = "defined in webgpu"]
entries: Vec<GPUBindGroupEntry>,
used_buffers: HashMap<Dom<GPUBuffer>, u32>,
used_textures: HashMap<TextureSubresource, u32>,
valid: Cell<bool>,
}

impl GPUBindGroup {
fn new_inherited(bind_group: WebGPUBindGroup, valid: bool) -> Self {
fn new_inherited(
bind_group: WebGPUBindGroup,
device: WebGPUDevice,
valid: bool,
entries: Vec<GPUBindGroupEntry>,
layout: &GPUBindGroupLayout,
used_buffers: HashMap<DomRoot<GPUBuffer>, u32>,
used_textures: HashMap<TextureSubresource, u32>,
) -> Self {
Self {
reflector_: Reflector::new(),
label: DomRefCell::new(None),
bind_group,
device,
valid: Cell::new(valid),
layout: Dom::from_ref(layout),
entries,
used_buffers: used_buffers
.into_iter()
.map(|(key, value)| (Dom::from_ref(&*key), value))
.collect(),
used_textures,
}
}

pub fn new(global: &GlobalScope, bind_group: WebGPUBindGroup, valid: bool) -> DomRoot<Self> {
pub fn new(
global: &GlobalScope,
bind_group: WebGPUBindGroup,
device: WebGPUDevice,
valid: bool,
entries: Vec<GPUBindGroupEntry>,
layout: &GPUBindGroupLayout,
used_buffers: HashMap<DomRoot<GPUBuffer>, u32>,
used_textures: HashMap<TextureSubresource, u32>,
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUBindGroup::new_inherited(bind_group, valid)),
Box::new(GPUBindGroup::new_inherited(
bind_group,
device,
valid,
entries,
layout,
used_buffers,
used_textures,
)),
global,
)
}
Expand Down
15 changes: 8 additions & 7 deletions components/script/dom/gpubindgrouplayout.rs
Expand Up @@ -12,6 +12,7 @@ use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
use dom_struct::dom_struct;
use std::cell::Cell;
use std::collections::HashMap;
use webgpu::{WebGPU, WebGPUBindGroupLayout};

#[dom_struct]
Expand All @@ -20,7 +21,7 @@ pub struct GPUBindGroupLayout {
label: DomRefCell<Option<DOMString>>,
bind_group_layout: WebGPUBindGroupLayout,
#[ignore_malloc_size_of = "defined in webgpu"]
bindings: Vec<GPUBindGroupLayoutEntry>,
entry_map: HashMap<u32, GPUBindGroupLayoutEntry>,
#[ignore_malloc_size_of = "defined in webgpu"]
channel: WebGPU,
valid: Cell<bool>,
Expand All @@ -30,15 +31,15 @@ impl GPUBindGroupLayout {
fn new_inherited(
channel: WebGPU,
bind_group_layout: WebGPUBindGroupLayout,
bindings: Vec<GPUBindGroupLayoutEntry>,
entry_map: HashMap<u32, GPUBindGroupLayoutEntry>,
valid: bool,
) -> Self {
Self {
reflector_: Reflector::new(),
channel,
label: DomRefCell::new(None),
bind_group_layout,
bindings,
entry_map,
valid: Cell::new(valid),
}
}
Expand All @@ -47,14 +48,14 @@ impl GPUBindGroupLayout {
global: &GlobalScope,
channel: WebGPU,
bind_group_layout: WebGPUBindGroupLayout,
bindings: Vec<GPUBindGroupLayoutEntry>,
entry_map: HashMap<u32, GPUBindGroupLayoutEntry>,
valid: bool,
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUBindGroupLayout::new_inherited(
channel,
bind_group_layout,
bindings,
entry_map,
valid,
)),
global,
Expand All @@ -71,8 +72,8 @@ impl GPUBindGroupLayout {
self.bind_group_layout
}

pub fn bindings(&self) -> &[GPUBindGroupLayoutEntry] {
&self.bindings
pub fn entries(&self) -> &HashMap<u32, GPUBindGroupLayoutEntry> {
&self.entry_map
}
}

Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/gpubuffer.rs
Expand Up @@ -110,7 +110,7 @@ impl GPUBuffer {
self.state.borrow()
}

pub fn valid(&self) -> bool {
pub fn is_valid(&self) -> bool {
self.valid.get()
}
}
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/gpucommandencoder.rs
Expand Up @@ -240,8 +240,8 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
None => false,
};
valid &= (*self.state.borrow() == GPUCommandEncoderState::Open) &&
source.valid() &&
destination.valid() &
source.is_valid() &&
destination.is_valid() &
!(size & BUFFER_COPY_ALIGN_MASK == 0) &
!(source_offset & BUFFER_COPY_ALIGN_MASK == 0) &
!(destination_offset & BUFFER_COPY_ALIGN_MASK == 0) &
Expand Down

0 comments on commit 00b3f78

Please sign in to comment.