Skip to content

Commit

Permalink
Implement GPURenderPassEncoder
Browse files Browse the repository at this point in the history
Add webidls for GPURenderPassEncoder and  GPURenderEncoderBase and
implement relevant methods.
  • Loading branch information
kunalmohan committed Jun 3, 2020
1 parent e452570 commit 1d4efb4
Show file tree
Hide file tree
Showing 11 changed files with 524 additions and 35 deletions.
121 changes: 110 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,92 @@ 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,
stencil_load_op,
stencil_store_op: match depth.stencilStoreOp {
GPUStoreOp::Store => wgt::StoreOp::Store,
GPUStoreOp::Clear => wgt::StoreOp::Clear,
},
clear_stencil,
}
});

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 +229,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
22 changes: 7 additions & 15 deletions components/script/dom/gpucomputepassencoder.rs
Expand Up @@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#![allow(unsafe_code)]

use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::GPUComputePassEncoderBinding::GPUComputePassEncoderMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
Expand All @@ -12,7 +14,6 @@ use crate::dom::gpubindgroup::GPUBindGroup;
use crate::dom::gpucommandencoder::{GPUCommandEncoder, GPUCommandEncoderState};
use crate::dom::gpucomputepipeline::GPUComputePipeline;
use dom_struct::dom_struct;
use std::cell::RefCell;
use webgpu::{
wgpu::command::{
compute_ffi::{
Expand All @@ -31,27 +32,22 @@ pub struct GPUComputePassEncoder {
channel: WebGPU,
label: DomRefCell<Option<DOMString>>,
#[ignore_malloc_size_of = "defined in wgpu-core"]
raw_pass: RefCell<Option<RawPass>>,
raw_pass: DomRefCell<Option<RawPass>>,
command_encoder: Dom<GPUCommandEncoder>,
}

impl GPUComputePassEncoder {
#[allow(unsafe_code)]
fn new_inherited(channel: WebGPU, parent: &GPUCommandEncoder) -> GPUComputePassEncoder {
GPUComputePassEncoder {
fn new_inherited(channel: WebGPU, parent: &GPUCommandEncoder) -> Self {
Self {
channel,
reflector_: Reflector::new(),
label: DomRefCell::new(None),
raw_pass: RefCell::new(Some(unsafe { RawPass::new_compute(parent.id().0) })),
raw_pass: DomRefCell::new(Some(unsafe { RawPass::new_compute(parent.id().0) })),
command_encoder: Dom::from_ref(parent),
}
}

pub fn new(
global: &GlobalScope,
channel: WebGPU,
parent: &GPUCommandEncoder,
) -> DomRoot<GPUComputePassEncoder> {
pub fn new(global: &GlobalScope, channel: WebGPU, parent: &GPUCommandEncoder) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUComputePassEncoder::new_inherited(channel, parent)),
global,
Expand All @@ -70,15 +66,13 @@ impl GPUComputePassEncoderMethods for GPUComputePassEncoder {
*self.label.borrow_mut() = value;
}

#[allow(unsafe_code)]
/// https://gpuweb.github.io/gpuweb/#dom-gpucomputepassencoder-dispatch
fn Dispatch(&self, x: u32, y: u32, z: u32) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
unsafe { wgpu_compute_pass_dispatch(raw_pass, x, y, z) };
}
}

#[allow(unsafe_code)]
/// https://gpuweb.github.io/gpuweb/#dom-gpurenderpassencoder-endpass
fn EndPass(&self) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().take() {
Expand All @@ -99,7 +93,6 @@ impl GPUComputePassEncoderMethods for GPUComputePassEncoder {
}
}

#[allow(unsafe_code)]
/// https://gpuweb.github.io/gpuweb/#dom-gpuprogrammablepassencoder-setbindgroup
fn SetBindGroup(&self, index: u32, bind_group: &GPUBindGroup, dynamic_offsets: Vec<u32>) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
Expand All @@ -115,7 +108,6 @@ impl GPUComputePassEncoderMethods for GPUComputePassEncoder {
}
}

#[allow(unsafe_code)]
/// https://gpuweb.github.io/gpuweb/#dom-gpucomputepassencoder-setpipeline
fn SetPipeline(&self, pipeline: &GPUComputePipeline) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
Expand Down
9 changes: 7 additions & 2 deletions components/script/dom/gpudevice.rs
Expand Up @@ -98,7 +98,6 @@ impl GPUDevice {
}
}

#[allow(unsafe_code)]
pub fn new(
global: &GlobalScope,
channel: WebGPU,
Expand Down Expand Up @@ -647,7 +646,13 @@ impl GPUDeviceMethods for GPUDevice {

let encoder = webgpu::WebGPUCommandEncoder(command_encoder_id);

GPUCommandEncoder::new(&self.global(), self.channel.clone(), encoder, true)
GPUCommandEncoder::new(
&self.global(),
self.channel.clone(),
self.device,
encoder,
true,
)
}

/// https://gpuweb.github.io/gpuweb/#dom-gpudevice-createtexture
Expand Down

0 comments on commit 1d4efb4

Please sign in to comment.