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

Implement GPURenderPassEncoder #26769

Merged
merged 2 commits into from Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions components/script/dom/gpuadapter.rs
Expand Up @@ -39,8 +39,8 @@ impl GPUAdapter {
name: DOMString,
extensions: Heap<*mut JSObject>,
adapter: WebGPUAdapter,
) -> GPUAdapter {
GPUAdapter {
) -> Self {
Self {
reflector_: Reflector::new(),
channel,
name,
Expand All @@ -55,7 +55,7 @@ impl GPUAdapter {
name: DOMString,
extensions: Heap<*mut JSObject>,
adapter: WebGPUAdapter,
) -> DomRoot<GPUAdapter> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUAdapter::new_inherited(
channel, name, extensions, adapter,
Expand All @@ -81,9 +81,7 @@ impl GPUAdapterMethods for GPUAdapter {
let promise = Promise::new_in_current_realm(&self.global(), comp);
let sender = response_async(&promise, self);
let desc = wgt::DeviceDescriptor {
extensions: wgt::Extensions {
anisotropic_filtering: descriptor.extensions.anisotropicFiltering,
},
extensions: wgt::Extensions::empty(),
limits: wgt::Limits {
max_bind_groups: descriptor.limits.maxBindGroups,
},
Expand Down
8 changes: 2 additions & 6 deletions components/script/dom/gpubindgroup.rs
Expand Up @@ -21,7 +21,7 @@ pub struct GPUBindGroup {
}

impl GPUBindGroup {
fn new_inherited(bind_group: WebGPUBindGroup, valid: bool) -> GPUBindGroup {
fn new_inherited(bind_group: WebGPUBindGroup, valid: bool) -> Self {
Self {
reflector_: Reflector::new(),
label: DomRefCell::new(None),
Expand All @@ -30,11 +30,7 @@ impl GPUBindGroup {
}
}

pub fn new(
global: &GlobalScope,
bind_group: WebGPUBindGroup,
valid: bool,
) -> DomRoot<GPUBindGroup> {
pub fn new(global: &GlobalScope, bind_group: WebGPUBindGroup, valid: bool) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUBindGroup::new_inherited(bind_group, valid)),
global,
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/gpubindgrouplayout.rs
Expand Up @@ -32,7 +32,7 @@ impl GPUBindGroupLayout {
bind_group_layout: WebGPUBindGroupLayout,
bindings: Vec<GPUBindGroupLayoutEntry>,
valid: bool,
) -> GPUBindGroupLayout {
) -> Self {
Self {
reflector_: Reflector::new(),
channel,
Expand All @@ -49,7 +49,7 @@ impl GPUBindGroupLayout {
bind_group_layout: WebGPUBindGroupLayout,
bindings: Vec<GPUBindGroupLayoutEntry>,
valid: bool,
) -> DomRoot<GPUBindGroupLayout> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUBindGroupLayout::new_inherited(
channel,
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/gpubuffer.rs
Expand Up @@ -57,7 +57,7 @@ impl GPUBuffer {
usage: u32,
valid: bool,
mapping: RootedTraceableBox<Heap<*mut JSObject>>,
) -> GPUBuffer {
) -> Self {
Self {
reflector_: Reflector::new(),
channel,
Expand All @@ -83,7 +83,7 @@ impl GPUBuffer {
usage: u32,
valid: bool,
mapping: RootedTraceableBox<Heap<*mut JSObject>>,
) -> DomRoot<GPUBuffer> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUBuffer::new_inherited(
channel, buffer, device, state, size, usage, valid, mapping,
Expand Down
6 changes: 3 additions & 3 deletions components/script/dom/gpucommandbuffer.rs
Expand Up @@ -37,8 +37,8 @@ impl GPUCommandBuffer {
channel: WebGPU,
command_buffer: WebGPUCommandBuffer,
buffers: HashSet<DomRoot<GPUBuffer>>,
) -> GPUCommandBuffer {
GPUCommandBuffer {
) -> Self {
Self {
channel,
reflector_: Reflector::new(),
label: DomRefCell::new(None),
Expand All @@ -52,7 +52,7 @@ impl GPUCommandBuffer {
channel: WebGPU,
command_buffer: WebGPUCommandBuffer,
buffers: HashSet<DomRoot<GPUBuffer>>,
) -> DomRoot<GPUCommandBuffer> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUCommandBuffer::new_inherited(
channel,
Expand Down
123 changes: 112 additions & 11 deletions components/script/dom/gpucommandencoder.rs
Expand Up @@ -6,6 +6,10 @@ use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::GPUBufferBinding::GPUSize64;
use crate::dom::bindings::codegen::Bindings::GPUCommandEncoderBinding::{
GPUCommandBufferDescriptor, GPUCommandEncoderMethods, GPUComputePassDescriptor,
GPURenderPassDescriptor, GPUStencilLoadValue, GPUStoreOp,
};
use crate::dom::bindings::codegen::UnionTypes::{
GPULoadOpOrDoubleSequenceOrGPUColorDict as GPUColorLoad, GPULoadOpOrFloat,
};
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
Expand All @@ -15,17 +19,20 @@ use crate::dom::globalscope::GlobalScope;
use crate::dom::gpubuffer::GPUBuffer;
use crate::dom::gpucommandbuffer::GPUCommandBuffer;
use crate::dom::gpucomputepassencoder::GPUComputePassEncoder;
use crate::dom::gpurenderpassencoder::GPURenderPassEncoder;
use dom_struct::dom_struct;
use std::cell::Cell;
use std::collections::HashSet;
use webgpu::wgt::BufferUsage;
use webgpu::{self, WebGPU, WebGPURequest};
use webgpu::wgpu::command::{
RawPass, RenderPassColorAttachmentDescriptor, RenderPassDepthStencilAttachmentDescriptor,
RenderPassDescriptor,
};
use webgpu::{self, wgt, WebGPU, WebGPUDevice, WebGPURequest};

const BUFFER_COPY_ALIGN_MASK: u64 = 3;

// https://gpuweb.github.io/gpuweb/#enumdef-encoder-state
#[derive(MallocSizeOf, PartialEq)]
#[allow(dead_code)]
pub enum GPUCommandEncoderState {
Open,
EncodingRenderPass,
Expand All @@ -42,19 +49,22 @@ pub struct GPUCommandEncoder {
encoder: webgpu::WebGPUCommandEncoder,
buffers: DomRefCell<HashSet<DomRoot<GPUBuffer>>>,
state: DomRefCell<GPUCommandEncoderState>,
device: WebGPUDevice,
valid: Cell<bool>,
}

impl GPUCommandEncoder {
pub fn new_inherited(
channel: WebGPU,
device: WebGPUDevice,
encoder: webgpu::WebGPUCommandEncoder,
valid: bool,
) -> GPUCommandEncoder {
GPUCommandEncoder {
) -> Self {
Self {
channel,
reflector_: Reflector::new(),
label: DomRefCell::new(None),
device,
encoder,
buffers: DomRefCell::new(HashSet::new()),
state: DomRefCell::new(GPUCommandEncoderState::Open),
Expand All @@ -65,11 +75,14 @@ impl GPUCommandEncoder {
pub fn new(
global: &GlobalScope,
channel: WebGPU,
device: WebGPUDevice,
encoder: webgpu::WebGPUCommandEncoder,
valid: bool,
) -> DomRoot<GPUCommandEncoder> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUCommandEncoder::new_inherited(channel, encoder, valid)),
Box::new(GPUCommandEncoder::new_inherited(
channel, device, encoder, valid,
)),
global,
)
}
Expand Down Expand Up @@ -113,6 +126,94 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
GPUComputePassEncoder::new(&self.global(), self.channel.clone(), &self)
}

#[allow(unsafe_code)]
/// https://gpuweb.github.io/gpuweb/#dom-gpucommandencoder-beginrenderpass
fn BeginRenderPass(
&self,
descriptor: &GPURenderPassDescriptor,
) -> DomRoot<GPURenderPassEncoder> {
self.set_state(
GPUCommandEncoderState::EncodingRenderPass,
GPUCommandEncoderState::Open,
);

let colors = descriptor
.colorAttachments
.iter()
.map(|color| {
let (load_op, clear_color) = match color.loadValue {
GPUColorLoad::GPULoadOp(_) => (wgt::LoadOp::Load, wgt::Color::TRANSPARENT),
GPUColorLoad::DoubleSequence(ref s) => (
wgt::LoadOp::Clear,
wgt::Color {
r: *s[0],
g: *s[1],
b: *s[2],
a: *s[3],
},
),
GPUColorLoad::GPUColorDict(ref d) => (
wgt::LoadOp::Clear,
wgt::Color {
r: *d.r,
g: *d.g,
b: *d.b,
a: *d.a,
},
),
};
RenderPassColorAttachmentDescriptor {
attachment: color.attachment.id().0,
resolve_target: color.resolveTarget.as_ref().map(|t| t.id().0),
load_op,
store_op: match color.storeOp {
GPUStoreOp::Store => wgt::StoreOp::Store,
GPUStoreOp::Clear => wgt::StoreOp::Clear,
},
clear_color,
}
})
.collect::<Vec<_>>();

let depth_stencil = descriptor.depthStencilAttachment.as_ref().map(|depth| {
let (depth_load_op, clear_depth) = match depth.depthLoadValue {
GPULoadOpOrFloat::GPULoadOp(_) => (wgt::LoadOp::Load, 0.0f32),
GPULoadOpOrFloat::Float(f) => (wgt::LoadOp::Clear, *f),
};
let (stencil_load_op, clear_stencil) = match depth.stencilLoadValue {
GPUStencilLoadValue::GPULoadOp(_) => (wgt::LoadOp::Load, 0u32),
GPUStencilLoadValue::RangeEnforcedUnsignedLong(l) => (wgt::LoadOp::Clear, l),
};
RenderPassDepthStencilAttachmentDescriptor {
attachment: depth.attachment.id().0,
depth_load_op,
depth_store_op: match depth.depthStoreOp {
GPUStoreOp::Store => wgt::StoreOp::Store,
GPUStoreOp::Clear => wgt::StoreOp::Clear,
},
clear_depth,
depth_read_only: depth.depthReadOnly,
stencil_load_op,
stencil_store_op: match depth.stencilStoreOp {
GPUStoreOp::Store => wgt::StoreOp::Store,
GPUStoreOp::Clear => wgt::StoreOp::Clear,
},
clear_stencil,
stencil_read_only: depth.stencilReadOnly,
}
});

let desc = RenderPassDescriptor {
color_attachments: colors.as_ptr(),
color_attachments_length: colors.len(),
depth_stencil_attachment: depth_stencil.as_ref(),
};

let raw_pass = unsafe { RawPass::new_render(self.id().0, &desc) };

GPURenderPassEncoder::new(&self.global(), self.channel.clone(), raw_pass, &self)
}

/// https://gpuweb.github.io/gpuweb/#dom-gpucommandencoder-copybuffertobuffer
fn CopyBufferToBuffer(
&self,
Expand All @@ -130,12 +231,12 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
Some(_) => true,
None => false,
};
valid &= match BufferUsage::from_bits(source.usage()) {
Some(usage) => usage.contains(BufferUsage::COPY_SRC),
valid &= match wgt::BufferUsage::from_bits(source.usage()) {
Some(usage) => usage.contains(wgt::BufferUsage::COPY_SRC),
None => false,
};
valid &= match BufferUsage::from_bits(destination.usage()) {
Some(usage) => usage.contains(BufferUsage::COPY_DST),
valid &= match wgt::BufferUsage::from_bits(destination.usage()) {
Some(usage) => usage.contains(wgt::BufferUsage::COPY_DST),
None => false,
};
valid &= (*self.state.borrow() == GPUCommandEncoderState::Open) &&
Expand Down