Skip to content

Commit

Permalink
Auto merge of #26769 - kunalmohan:gpu-render-pass, r=<try>
Browse files Browse the repository at this point in the history
Implement GPURenderPassEncoder

Add webidls for GPURenderPassEncoder and  GPURenderEncoderBase and
implement relevant methods.

<!-- Please describe your changes on the following line: -->
r?@kvark

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [ ] These changes fix #___ (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
  • Loading branch information
bors-servo committed Jun 3, 2020
2 parents 948dd73 + 4b6316c commit 387495a
Show file tree
Hide file tree
Showing 11 changed files with 542 additions and 23 deletions.
124 changes: 115 additions & 9 deletions components/script/dom/gpucommandencoder.rs
Expand Up @@ -6,6 +6,11 @@ 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, GPUStoreOp,
};
use crate::dom::bindings::codegen::UnionTypes::{
GPULoadOpOrDoubleSequenceOrGPUColorDict as GPUColorLoad, GPULoadOpOrFloat,
GPULoadOpOrRangeEnforcedUnsignedLong as GPULongLoad,
};
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
Expand All @@ -15,17 +20,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 +50,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 {
channel,
reflector_: Reflector::new(),
label: DomRefCell::new(None),
device,
encoder,
buffers: DomRefCell::new(HashSet::new()),
state: DomRefCell::new(GPUCommandEncoderState::Open),
Expand All @@ -65,11 +76,14 @@ impl GPUCommandEncoder {
pub fn new(
global: &GlobalScope,
channel: WebGPU,
device: WebGPUDevice,
encoder: webgpu::WebGPUCommandEncoder,
valid: bool,
) -> DomRoot<GPUCommandEncoder> {
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 @@ -110,7 +124,99 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
GPUCommandEncoderState::EncodingComputePass,
GPUCommandEncoderState::Open,
);
GPUComputePassEncoder::new(&self.global(), self.channel.clone(), &self)
GPUComputePassEncoder::new(&self.global(), self.channel.clone(), &self, self.device)
}

#[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 {
GPULongLoad::GPULoadOp(_) => (wgt::LoadOp::Load, 0u32),
GPULongLoad::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,
self.device,
)
}

/// https://gpuweb.github.io/gpuweb/#dom-gpucommandencoder-copybuffertobuffer
Expand All @@ -130,12 +236,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
27 changes: 16 additions & 11 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 @@ -21,28 +22,33 @@ use webgpu::{
},
RawPass,
},
WebGPU, WebGPURequest,
WebGPU, WebGPUDevice, WebGPURequest,
};

#[dom_struct]
pub struct GPUComputePassEncoder {
reflector_: Reflector,
#[ignore_malloc_size_of = "defined in webgpu"]
channel: WebGPU,
device: WebGPUDevice,
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 {
fn new_inherited(
channel: WebGPU,
parent: &GPUCommandEncoder,
device: WebGPUDevice,
) -> GPUComputePassEncoder {
GPUComputePassEncoder {
channel,
reflector_: Reflector::new(),
label: DomRefCell::new(None),
raw_pass: RefCell::new(Some(unsafe { RawPass::new_compute(parent.id().0) })),
device,
raw_pass: DomRefCell::new(Some(unsafe { RawPass::new_compute(parent.id().0) })),
command_encoder: Dom::from_ref(parent),
}
}
Expand All @@ -51,9 +57,12 @@ impl GPUComputePassEncoder {
global: &GlobalScope,
channel: WebGPU,
parent: &GPUCommandEncoder,
device: WebGPUDevice,
) -> DomRoot<GPUComputePassEncoder> {
reflect_dom_object(
Box::new(GPUComputePassEncoder::new_inherited(channel, parent)),
Box::new(GPUComputePassEncoder::new_inherited(
channel, parent, device,
)),
global,
)
}
Expand All @@ -70,15 +79,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 +106,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 +121,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 387495a

Please sign in to comment.