Skip to content

Commit

Permalink
hal/gles: render passes
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jun 24, 2021
1 parent 549edaf commit 8ba2771
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 98 deletions.
2 changes: 2 additions & 0 deletions wgpu-core/src/command/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,8 @@ impl<'a, A: HalApi> RenderPassInfo<'a, A> {

let hal_desc = hal::RenderPassDescriptor {
label,
extent,
sample_count,
color_attachments: &colors,
depth_stencil_attachment: depth_stencil,
};
Expand Down
6 changes: 6 additions & 0 deletions wgpu-hal/examples/halmark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,12 @@ impl<A: hal::Api> Example<A> {
};
let pass_desc = hal::RenderPassDescriptor {
label: None,
extent: wgt::Extent3d {
width: self.extent[0],
height: self.extent[1],
depth_or_array_layers: 1,
},
sample_count: 1,
color_attachments: &[hal::ColorAttachment {
target: hal::Attachment {
view: &surface_tex_view,
Expand Down
11 changes: 9 additions & 2 deletions wgpu-hal/src/gles/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ impl crate::Adapter<super::Api> for super::Adapter {
let gl = &self.shared.context;
gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, 1);
gl.pixel_store_i32(glow::PACK_ALIGNMENT, 1);
let main_vao = gl.create_vertex_array().unwrap();
let main_vao = gl
.create_vertex_array()
.map_err(|_| crate::DeviceError::OutOfMemory)?;
gl.bind_vertex_array(Some(main_vao));

Ok(crate::OpenDevice {
Expand All @@ -294,7 +296,12 @@ impl crate::Adapter<super::Api> for super::Adapter {
},
queue: super::Queue {
shared: Arc::clone(&self.shared),
copy_fbo: gl.create_framebuffer().unwrap(),
draw_fbo: gl
.create_framebuffer()
.map_err(|_| crate::DeviceError::OutOfMemory)?,
copy_fbo: gl
.create_framebuffer()
.map_err(|_| crate::DeviceError::OutOfMemory)?,
temp_query_results: Vec::new(),
},
})
Expand Down
104 changes: 96 additions & 8 deletions wgpu-hal/src/gles/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl super::CommandBuffer {

impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
unsafe fn begin_encoding(&mut self, label: crate::Label) -> Result<(), crate::DeviceError> {
self.state = super::CommandState::default();
self.cmd_buffer.label = label.map(str::to_string);
Ok(())
}
Expand Down Expand Up @@ -100,8 +101,9 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
) where
T: Iterator<Item = crate::BufferTextureCopy>,
{
let format_info = dst.format.describe();
assert_eq!(
dst.format_info.block_dimensions,
format_info.block_dimensions,
(1, 1),
"Compressed texture copies are TODO"
);
Expand All @@ -115,7 +117,7 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
dst_info: super::TextureCopyInfo {
external_format: dst.format_desc.external,
data_type: dst.format_desc.data_type,
texel_size: dst.format_info.block_size,
texel_size: format_info.block_size,
},
copy,
})
Expand All @@ -131,8 +133,9 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
) where
T: Iterator<Item = crate::BufferTextureCopy>,
{
let format_info = src.format.describe();
assert_eq!(
src.format_info.block_dimensions,
format_info.block_dimensions,
(1, 1),
"Compressed texture copies are TODO"
);
Expand All @@ -144,7 +147,7 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
src_info: super::TextureCopyInfo {
external_format: src.format_desc.external,
data_type: src.format_desc.data_type,
texel_size: src.format_info.block_size,
texel_size: format_info.block_size,
},
dst: dst.raw,
dst_target: dst.target,
Expand Down Expand Up @@ -190,8 +193,82 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {

// render

unsafe fn begin_render_pass(&mut self, desc: &crate::RenderPassDescriptor<super::Api>) {}
unsafe fn end_render_pass(&mut self) {}
unsafe fn begin_render_pass(&mut self, desc: &crate::RenderPassDescriptor<super::Api>) {
if let Some(label) = desc.label {
let range = self.cmd_buffer.add_marker(label);
self.cmd_buffer.commands.push(C::PushDebugGroup(range));
self.state.has_pass_label = true;
}

// set the framebuffer
self.cmd_buffer
.commands
.push(C::ResetFramebuffer(desc.extent));
for (i, cat) in desc.color_attachments.iter().enumerate() {
let attachment = glow::COLOR_ATTACHMENT0 + i as u32;
self.cmd_buffer.commands.push(C::SetFramebufferAttachment {
attachment,
view: cat.target.view.clone(),
});
}
if let Some(ref dsat) = desc.depth_stencil_attachment {
let attachment = match dsat.target.view.aspects {
crate::FormatAspect::DEPTH => glow::DEPTH_ATTACHMENT,
crate::FormatAspect::STENCIL => glow::STENCIL_ATTACHMENT,
_ => glow::DEPTH_STENCIL_ATTACHMENT,
};
self.cmd_buffer.commands.push(C::SetFramebufferAttachment {
attachment,
view: dsat.target.view.clone(),
});
}

self.cmd_buffer
.commands
.push(C::SetDrawColorBuffers(desc.color_attachments.len() as u8));
// issue the clears
for (i, cat) in desc.color_attachments.iter().enumerate() {
if !cat.ops.contains(crate::AttachmentOp::LOAD) {
let draw_buffer = glow::DRAW_BUFFER0 + i as u32;
let c = &cat.clear_value;
self.cmd_buffer
.commands
.push(match cat.target.view.sample_type {
wgt::TextureSampleType::Float { .. } => C::ClearColorF(
draw_buffer,
[c.r as f32, c.g as f32, c.r as f32, c.a as f32],
),
wgt::TextureSampleType::Depth => unimplemented!(),
wgt::TextureSampleType::Uint => C::ClearColorU(
draw_buffer,
[c.r as u32, c.g as u32, c.r as u32, c.a as u32],
),
wgt::TextureSampleType::Sint => C::ClearColorI(
draw_buffer,
[c.r as i32, c.g as i32, c.r as i32, c.a as i32],
),
});
}
}
if let Some(ref dsat) = desc.depth_stencil_attachment {
if !dsat.depth_ops.contains(crate::AttachmentOp::LOAD) {
self.cmd_buffer
.commands
.push(C::ClearDepth(dsat.clear_value.0));
}
if !dsat.stencil_ops.contains(crate::AttachmentOp::LOAD) {
self.cmd_buffer
.commands
.push(C::ClearStencil(dsat.clear_value.1));
}
}
}
unsafe fn end_render_pass(&mut self) {
if self.state.has_pass_label {
self.cmd_buffer.commands.push(C::PopDebugGroup);
self.state.has_pass_label = false;
}
}

unsafe fn set_bind_group(
&mut self,
Expand Down Expand Up @@ -346,8 +423,19 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {

// compute

unsafe fn begin_compute_pass(&mut self, desc: &crate::ComputePassDescriptor) {}
unsafe fn end_compute_pass(&mut self) {}
unsafe fn begin_compute_pass(&mut self, desc: &crate::ComputePassDescriptor) {
if let Some(label) = desc.label {
let range = self.cmd_buffer.add_marker(label);
self.cmd_buffer.commands.push(C::PushDebugGroup(range));
self.state.has_pass_label = true;
}
}
unsafe fn end_compute_pass(&mut self) {
if self.state.has_pass_label {
self.cmd_buffer.commands.push(C::PopDebugGroup);
self.state.has_pass_label = false;
}
}

unsafe fn set_compute_pipeline(&mut self, pipeline: &super::ComputePipeline) {}

Expand Down
11 changes: 11 additions & 0 deletions wgpu-hal/src/gles/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,14 @@ pub fn map_primitive_topology(topology: wgt::PrimitiveTopology) -> u32 {
Pt::TriangleStrip => glow::TRIANGLE_STRIP,
}
}

pub fn map_view_dimension(dim: wgt::TextureViewDimension) -> u32 {
use wgt::TextureViewDimension as Tvd;
match dim {
Tvd::D1 | Tvd::D2 => glow::TEXTURE_2D,
Tvd::D2Array => glow::TEXTURE_2D_ARRAY,
Tvd::Cube => glow::TEXTURE_CUBE_MAP,
Tvd::CubeArray => glow::TEXTURE_CUBE_MAP_ARRAY,
Tvd::D3 => glow::TEXTURE_3D,
}
}
54 changes: 25 additions & 29 deletions wgpu-hal/src/gles/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ impl super::Device {
};

let shader = &stage.module.naga;
let entry_point_index = (&shader.module.entry_points)
.into_iter()
let entry_point_index = shader
.module
.entry_points
.iter()
.position(|ep| ep.name.as_str() == stage.entry_point)
.ok_or(crate::PipelineError::EntryPoint(naga_stage))?;

Expand Down Expand Up @@ -397,10 +399,7 @@ impl crate::Device<super::Api> for super::Device {
desc.size.height as i32,
);
}
super::TextureInner::Renderbuffer {
raw,
aspects: desc.format.into(),
}
super::TextureInner::Renderbuffer { raw }
} else {
let raw = gl.create_texture().unwrap();
let target = match desc.dimension {
Expand Down Expand Up @@ -456,8 +455,8 @@ impl crate::Device<super::Api> for super::Device {

Ok(super::Texture {
inner,
format: desc.format,
format_desc,
format_info: desc.format.describe(),
})
}
unsafe fn destroy_texture(&self, texture: super::Texture) {
Expand All @@ -477,18 +476,21 @@ impl crate::Device<super::Api> for super::Device {
texture: &super::Texture,
desc: &crate::TextureViewDescriptor,
) -> Result<super::TextureView, crate::DeviceError> {
Ok(match texture.inner {
super::TextureInner::Renderbuffer { raw, aspects } => {
super::TextureView::Renderbuffer {
raw,
aspects: aspects & crate::FormatAspect::from(desc.range.aspect),
Ok(super::TextureView {
inner: match texture.inner {
super::TextureInner::Renderbuffer { raw } => {
super::TextureInner::Renderbuffer { raw }
}
}
super::TextureInner::Texture { raw, target } => super::TextureView::Texture {
raw,
target,
range: desc.range.clone(),
super::TextureInner::Texture { raw, target: _ } => super::TextureInner::Texture {
raw,
target: conv::map_view_dimension(desc.dimension),
},
},
sample_type: texture.format.describe().sample_type,
aspects: crate::FormatAspect::from(texture.format)
& crate::FormatAspect::from(desc.range.aspect),
base_mip_level: desc.range.base_mip_level,
base_array_layer: desc.range.base_array_layer,
})
}
unsafe fn destroy_texture_view(&self, _view: super::TextureView) {}
Expand Down Expand Up @@ -615,19 +617,13 @@ impl crate::Device<super::Api> for super::Device {
super::RawBinding::Sampler(sampler.raw)
}
wgt::BindingType::Texture { .. } | wgt::BindingType::StorageTexture { .. } => {
match *desc.textures[entry.resource_index as usize].view {
super::TextureView::Renderbuffer { .. } => {
match desc.textures[entry.resource_index as usize].view.inner {
super::TextureInner::Renderbuffer { .. } => {
panic!("Unable to use a renderbuffer in a group")
}
super::TextureView::Texture {
raw,
target,
ref range,
} => super::RawBinding::Texture {
raw,
target,
range: range.clone(),
},
super::TextureInner::Texture { raw, target } => {
super::RawBinding::Texture { raw, target }
}
}
}
};
Expand Down Expand Up @@ -687,7 +683,7 @@ impl crate::Device<super::Api> for super::Device {

Ok(super::RenderPipeline {
inner,
primitive: desc.primitive.clone(),
primitive: desc.primitive,
attributes,
depth: desc.depth_stencil.clone(),
})
Expand Down
Loading

0 comments on commit 8ba2771

Please sign in to comment.