Skip to content

Commit

Permalink
rt(wgpu): add structure to wgpu backend
Browse files Browse the repository at this point in the history
  • Loading branch information
chyyran committed Dec 16, 2023
1 parent 650b512 commit a2f0d3a
Show file tree
Hide file tree
Showing 19 changed files with 566 additions and 282 deletions.
93 changes: 55 additions & 38 deletions .idea/workspace.xml

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

1 change: 1 addition & 0 deletions Cargo.lock

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

10 changes: 10 additions & 0 deletions librashader-common/src/wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,13 @@ impl From<WrapMode> for wgpu_types::AddressMode {
}
}
}

impl From<Size<u32>> for wgpu_types::Extent3d {
fn from(value: Size<u32>) -> Self {
wgpu_types::Extent3d {
width: value.width,
height: value.height,
depth_or_array_layers: 1,
}
}
}
4 changes: 2 additions & 2 deletions librashader-runtime-vk/src/filter_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl FilterPass {

let residual = self
.graphics_pipeline
.begin_rendering(&parent.device, output, cmd)?;
.begin_rendering(output, cmd)?;

unsafe {
parent.device.cmd_bind_pipeline(
Expand Down Expand Up @@ -178,7 +178,7 @@ impl FilterPass {
.device
.cmd_set_viewport(cmd, 0, &[output.output.size.into()]);
parent.draw_quad.draw_quad(cmd, vbo_type);
self.graphics_pipeline.end_rendering(&parent.device, cmd);
self.graphics_pipeline.end_rendering(cmd);
}
Ok(residual)
}
Expand Down
13 changes: 6 additions & 7 deletions librashader-runtime-vk/src/graphics_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,13 @@ impl VulkanGraphicsPipeline {
#[inline(always)]
pub(crate) fn begin_rendering(
&self,
device: &ash::Device,
output: &RenderTarget<OutputImage>,
cmd: vk::CommandBuffer,
) -> error::Result<Option<vk::Framebuffer>> {
if let Some(render_pass) = &self.render_pass {
let attachments = [output.output.image_view];
let framebuffer = unsafe {
device.create_framebuffer(
self.device.create_framebuffer(
&vk::FramebufferCreateInfo::builder()
.render_pass(render_pass.handle)
.attachments(&attachments)
Expand All @@ -418,7 +417,7 @@ impl VulkanGraphicsPipeline {
extent: output.output.size.into(),
});
unsafe {
device.cmd_begin_render_pass(cmd, &render_pass_info, vk::SubpassContents::INLINE);
self.device.cmd_begin_render_pass(cmd, &render_pass_info, vk::SubpassContents::INLINE);
}
Ok(Some(framebuffer))
} else {
Expand All @@ -438,18 +437,18 @@ impl VulkanGraphicsPipeline {
.color_attachments(&attachments);

unsafe {
device.cmd_begin_rendering(cmd, &rendering_info);
self.device.cmd_begin_rendering(cmd, &rendering_info);
}
Ok(None)
}
}

pub(crate) fn end_rendering(&self, device: &ash::Device, cmd: vk::CommandBuffer) {
pub(crate) fn end_rendering(&self, cmd: vk::CommandBuffer) {
unsafe {
if self.render_pass.is_none() {
device.cmd_end_rendering(cmd);
self.device.cmd_end_rendering(cmd);
} else {
device.cmd_end_render_pass(cmd)
self.device.cmd_end_render_pass(cmd)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion librashader-runtime-wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ parking_lot = "0.12.1"
rayon = "1.8.0"
bytemuck = { version = "1.14.0", features = ["derive"] }


array-concat = "0.5.2"

[dev-dependencies]
config = { version = "0.13.4", features = [] }
Expand Down
52 changes: 52 additions & 0 deletions librashader-runtime-wgpu/src/draw_quad.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use array_concat::concat_arrays;
use librashader_runtime::quad::QuadType;
use wgpu::util::{BufferInitDescriptor, DeviceExt};
use wgpu::{Buffer, BufferDescriptor, Device, RenderPass};

#[rustfmt::skip]
const VBO_OFFSCREEN: [f32; 16] = [
// Offscreen
-1.0f32, -1.0, 0.0, 0.0,
-1.0, 1.0, 0.0, 1.0,
1.0, -1.0, 1.0, 0.0,
1.0, 1.0, 1.0, 1.0,
];

#[rustfmt::skip]
const VBO_DEFAULT_FINAL: [f32; 16] = [
// Final
0.0f32, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 1.0,
1.0, 0.0, 1.0, 0.0,
1.0, 1.0, 1.0, 1.0,
];

static VBO_DATA: &[f32; 32] = &concat_arrays!(VBO_OFFSCREEN, VBO_DEFAULT_FINAL);
pub struct DrawQuad {
buffer: Buffer,
}

impl DrawQuad {
pub fn new(device: &Device) -> DrawQuad {
let buffer = device.create_buffer_init(&BufferInitDescriptor {
label: Some("librashader vbo"),
contents: bytemuck::cast_slice(VBO_DATA),
usage: wgpu::BufferUsages::VERTEX,
});

DrawQuad { buffer }
}

pub fn bind_vbo_for_frame<'a, 'b: 'a>(&'b self, cmd: &mut RenderPass<'a>) {
cmd.set_vertex_buffer(0, self.buffer.slice(0..));
}

pub fn draw_quad<'a, 'b: 'a>(&'b self, cmd: &mut RenderPass<'a>, vbo: QuadType) {
let offset = match vbo {
QuadType::Offscreen => 0..3,
QuadType::Final => 1..4,
};

cmd.draw(offset, 0..1)
}
}
2 changes: 1 addition & 1 deletion librashader-runtime-wgpu/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use librashader_runtime::image::ImageError;
use std::convert::Infallible;
use thiserror::Error;

/// Cumulative error type for Vulkan filter chains.
/// Cumulative error type for WGPU filter chains.
#[derive(Error, Debug)]
pub enum FilterChainError {
#[error("SPIRV reflection error")]
Expand Down
Loading

0 comments on commit a2f0d3a

Please sign in to comment.