Skip to content

Commit

Permalink
deno_webgpu: Option-al color attachments support
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Jun 25, 2022
1 parent 00a5912 commit 823bd0a
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 61 deletions.
2 changes: 1 addition & 1 deletion deno_webgpu/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Resource for WebGpuRenderBundle {
pub struct CreateRenderBundleEncoderArgs {
device_rid: ResourceId,
label: Option<String>,
color_formats: Vec<wgpu_types::TextureFormat>,
color_formats: Vec<Option<wgpu_types::TextureFormat>>,
depth_stencil_format: Option<wgpu_types::TextureFormat>,
sample_count: u32,
depth_read_only: bool,
Expand Down
53 changes: 29 additions & 24 deletions deno_webgpu/src/command_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn op_webgpu_command_encoder_begin_render_pass(
state: &mut OpState,
command_encoder_rid: ResourceId,
label: Option<String>,
color_attachments: Vec<GpuRenderPassColorAttachment>,
color_attachments: Vec<Option<GpuRenderPassColorAttachment>>,
depth_stencil_attachment: Option<GpuRenderPassDepthStencilAttachment>,
_occlusion_query_set: Option<u32>, // not yet implemented
) -> Result<WebGpuResult, AnyError> {
Expand All @@ -89,30 +89,35 @@ pub fn op_webgpu_command_encoder_begin_render_pass(
let color_attachments = color_attachments
.into_iter()
.map(|color_attachment| {
let texture_view_resource = state
.resource_table
.get::<super::texture::WebGpuTextureView>(color_attachment.view)?;

let resolve_target = color_attachment
.resolve_target
.map(|rid| {
state
.resource_table
.get::<super::texture::WebGpuTextureView>(rid)
let rp_at = if let Some(at) = color_attachment.as_ref() {
let texture_view_resource = state
.resource_table
.get::<super::texture::WebGpuTextureView>(at.view)?;

let resolve_target = at
.resolve_target
.map(|rid| {
state
.resource_table
.get::<super::texture::WebGpuTextureView>(rid)
})
.transpose()?
.map(|texture| texture.0);

Some(wgpu_core::command::RenderPassColorAttachment {
view: texture_view_resource.0,
resolve_target,
channel: wgpu_core::command::PassChannel {
load_op: at.load_op,
store_op: at.store_op,
clear_value: at.clear_value.unwrap_or_default(),
read_only: false,
},
})
.transpose()?
.map(|texture| texture.0);

Ok(wgpu_core::command::RenderPassColorAttachment {
view: texture_view_resource.0,
resolve_target,
channel: wgpu_core::command::PassChannel {
load_op: color_attachment.load_op,
store_op: color_attachment.store_op,
clear_value: color_attachment.clear_value.unwrap_or_default(),
read_only: false,
},
})
} else {
None
};
Ok(rp_at)
})
.collect::<Result<Vec<_>, AnyError>>()?;

Expand Down
2 changes: 1 addition & 1 deletion deno_webgpu/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl From<GpuMultisampleState> for wgpu_types::MultisampleState {
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct GpuFragmentState {
targets: Vec<wgpu_types::ColorTargetState>,
targets: Vec<Option<wgpu_types::ColorTargetState>>,
module: u32,
entry_point: String,
// TODO(lucacasonato): constants
Expand Down
8 changes: 4 additions & 4 deletions player/tests/data/quad.ron
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
entry_point: "fs_main",
),
targets: [
(
Some((
format: rgba8unorm,
),
)),
],
)),
),
Expand All @@ -90,7 +90,7 @@
push_constant_data: [],
),
target_colors: [
(
Some((
view: Id(0, 1, Empty),
resolve_target: None,
channel: (
Expand All @@ -104,7 +104,7 @@
),
read_only: false,
),
),
)),
],
target_depth_stencil: None,
),
Expand Down
4 changes: 2 additions & 2 deletions player/tests/data/zero-init-texture-rendertarget.ron
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
push_constant_data: [],
),
target_colors: [
(
Some((
view: Id(0, 1, Empty),
resolve_target: None,
channel: (
Expand All @@ -57,7 +57,7 @@
),
read_only: false,
),
),
)),
],
target_depth_stencil: None,
),
Expand Down
39 changes: 16 additions & 23 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2760,30 +2760,23 @@ impl<A: HalApi> Device<A> {

if validated_stages.contains(wgt::ShaderStages::FRAGMENT) {
for (i, output) in io.iter() {
match color_targets.get(*i as usize) {
Some(state) => {
let format = if let Some(st) = state.as_ref() {
st.format
} else {
return Err(
pipeline::CreateRenderPipelineError::InvalidFragmentOutputLocation(
*i,
),
);
};
validation::check_texture_format(format, &output.ty).map_err(
|pipeline| {
pipeline::CreateRenderPipelineError::ColorState(
*i as u8,
pipeline::ColorStateError::IncompatibleFormat {
pipeline,
shader: output.ty,
},
)
if let Some(state) = color_targets.get(*i as usize) {
let format = if let Some(st) = state.as_ref() {
st.format
} else {
return Err(
pipeline::CreateRenderPipelineError::InvalidFragmentOutputLocation(*i),
);
};
validation::check_texture_format(format, &output.ty).map_err(|pipeline| {
pipeline::CreateRenderPipelineError::ColorState(
*i as u8,
pipeline::ColorStateError::IncompatibleFormat {
pipeline,
shader: output.ty,
},
)?;
}
None => (),
)
})?;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions wgpu-hal/examples/raw-gles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@ fn fill_screen(exposed: &hal::ExposedAdapter<hal::api::Gles>, width: u32, height
depth_or_array_layers: 1,
},
sample_count: 1,
color_attachments: &[hal::ColorAttachment {
color_attachments: &[Some(hal::ColorAttachment {
target: hal::Attachment {
view: &view,
usage: hal::TextureUses::COLOR_TARGET,
},
resolve_target: None,
ops: hal::AttachmentOps::STORE,
clear_value: wgt::Color::BLUE,
}],
})],
depth_stencil_attachment: None,
multiview: None,
};
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/metal/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
//TODO: set visibility results buffer

for (i, at) in desc.color_attachments.iter().enumerate() {
if let Some(at) = at {
if let Some(at) = at.as_ref() {
let at_descriptor = descriptor.color_attachments().object_at(i as u64).unwrap();
at_descriptor.set_texture(Some(&at.target.view.raw));
if let Some(ref resolve) = at.resolve_target {
Expand Down
6 changes: 3 additions & 3 deletions wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ impl super::DeviceShared {
.build();
vk_attachments.push(vk_attachment);
} else {
resolve_refs.push(unused.clone());
resolve_refs.push(unused);
};
} else {
color_refs.push(unused.clone());
resolve_refs.push(unused.clone());
color_refs.push(unused);
resolve_refs.push(unused);
}
}

Expand Down

0 comments on commit 823bd0a

Please sign in to comment.