rt(vk, wgpu): use structs for quad data

This commit is contained in:
chyyran 2024-02-11 16:46:43 -05:00 committed by Ronny Chan
parent 2d98ebec1b
commit c121087348
6 changed files with 100 additions and 38 deletions

1
Cargo.lock generated
View file

@ -1685,6 +1685,7 @@ dependencies = [
name = "librashader-runtime-vk" name = "librashader-runtime-vk"
version = "0.2.0-beta.9" version = "0.2.0-beta.9"
dependencies = [ dependencies = [
"array-concat",
"ash", "ash",
"ash-window", "ash-window",
"bytemuck", "bytemuck",

View file

@ -149,6 +149,6 @@ mod test {
let compiled = msl.compile(Some(msl::Version::V2_0)).unwrap(); let compiled = msl.compile(Some(msl::Version::V2_0)).unwrap();
println!("{}", compiled.fragment); println!("{}", compiled.vertex);
} }
} }

View file

@ -196,6 +196,6 @@ mod test {
let compiled = msl.compile(Some(msl::Version::V2_0)).unwrap(); let compiled = msl.compile(Some(msl::Version::V2_0)).unwrap();
println!("{}", compiled.fragment); println!("{:?}", compiled.context.fragment.translation_info.entry_point_names);
} }
} }

View file

@ -22,12 +22,13 @@ librashader-runtime = { path = "../librashader-runtime" , version = "0.2.0-beta.
librashader-cache = { path = "../librashader-cache", version = "0.2.0-beta.9" } librashader-cache = { path = "../librashader-cache", version = "0.2.0-beta.9" }
rustc-hash = "1.1.0" rustc-hash = "1.1.0"
bytemuck = "1.12.3" bytemuck = { version = "1.12.3", features = ["derive"] }
thiserror = "1.0.37" thiserror = "1.0.37"
ash = { version = "0.37", features = ["debug"] } ash = { version = "0.37", features = ["debug"] }
gpu-allocator = { version = "0.22.0", default-features = false, features = ["vulkan"] } gpu-allocator = { version = "0.22.0", default-features = false, features = ["vulkan"] }
parking_lot = "0.12.1" parking_lot = "0.12.1"
rayon = "1.6.1" rayon = "1.6.1"
array-concat = "0.5.2"
[dev-dependencies] [dev-dependencies]
num = "0.4.0" num = "0.4.0"

View file

@ -5,25 +5,57 @@ use gpu_allocator::vulkan::Allocator;
use librashader_runtime::quad::QuadType; use librashader_runtime::quad::QuadType;
use parking_lot::RwLock; use parking_lot::RwLock;
use std::sync::Arc; use std::sync::Arc;
use bytemuck::{Pod, Zeroable};
use array_concat::concat_arrays;
#[rustfmt::skip] // Vulkan does vertex expansion
static VBO_OFFSCREEN: &[f32; 16] = &[ #[repr(C)]
// Offscreen #[derive(Debug, Copy, Clone, Default, Zeroable, Pod)]
-1.0, -1.0, 0.0, 0.0, struct VulkanVertex {
-1.0, 1.0, 0.0, 1.0, position: [f32; 2],
1.0, -1.0, 1.0, 0.0, texcoord: [f32; 2],
1.0, 1.0, 1.0, 1.0, }
const OFFSCREEN_VBO_DATA: [VulkanVertex; 4] = [
VulkanVertex {
position: [-1.0, -1.0],
texcoord: [0.0, 0.0],
},
VulkanVertex {
position: [-1.0, 1.0],
texcoord: [0.0, 1.0],
},
VulkanVertex {
position: [1.0, -1.0],
texcoord: [1.0, 0.0],
},
VulkanVertex {
position: [1.0, 1.0],
texcoord: [1.0, 1.0],
},
]; ];
#[rustfmt::skip] const FINAL_VBO_DATA: [VulkanVertex; 4] = [
static VBO_DEFAULT_FINAL: &[f32; 16] = &[ VulkanVertex {
// Final position: [0.0, 0.0],
0.0, 0.0, 0.0, 0.0, texcoord: [0.0, 0.0],
0.0, 1.0, 0.0, 1.0, },
1.0, 0.0, 1.0, 0.0, VulkanVertex {
1.0, 1.0, 1.0, 1.0, position: [0.0, 1.0],
texcoord: [0.0, 1.0],
},
VulkanVertex {
position: [1.0, 0.0],
texcoord: [1.0, 0.0],
},
VulkanVertex {
position: [1.0, 1.0],
texcoord: [1.0, 1.0],
},
]; ];
static VBO_DATA: &[VulkanVertex; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA);
pub struct DrawQuad { pub struct DrawQuad {
buffer: VulkanBuffer, buffer: VulkanBuffer,
device: Arc<ash::Device>, device: Arc<ash::Device>,
@ -38,15 +70,13 @@ impl DrawQuad {
device, device,
allocator, allocator,
vk::BufferUsageFlags::VERTEX_BUFFER, vk::BufferUsageFlags::VERTEX_BUFFER,
2 * std::mem::size_of::<[f32; 16]>(), std::mem::size_of::<[VulkanVertex; 8]>(),
)?; )?;
{ {
let slice = buffer.as_mut_slice()?; let slice = buffer.as_mut_slice()?;
slice[0..std::mem::size_of::<[f32; 16]>()] slice
.copy_from_slice(bytemuck::cast_slice(VBO_OFFSCREEN)); .copy_from_slice(bytemuck::cast_slice(VBO_DATA));
slice[std::mem::size_of::<[f32; 16]>()..]
.copy_from_slice(bytemuck::cast_slice(VBO_DEFAULT_FINAL));
} }
Ok(DrawQuad { Ok(DrawQuad {

View file

@ -1,27 +1,57 @@
use array_concat::concat_arrays; use array_concat::concat_arrays;
use bytemuck::{Pod, Zeroable};
use librashader_runtime::quad::QuadType; use librashader_runtime::quad::QuadType;
use wgpu::util::{BufferInitDescriptor, DeviceExt}; use wgpu::util::{BufferInitDescriptor, DeviceExt};
use wgpu::{Buffer, Device, RenderPass}; use wgpu::{Buffer, Device, RenderPass};
#[rustfmt::skip] // As per https://www.w3.org/TR/webgpu/#vertex-processing,
const VBO_OFFSCREEN: [f32; 16] = [ // WGPU does vertex expansion
// Offscreen #[repr(C)]
-1.0f32, -1.0, 0.0, 0.0, #[derive(Debug, Copy, Clone, Default, Zeroable, Pod)]
-1.0, 1.0, 0.0, 1.0, struct WgpuVertex {
1.0, -1.0, 1.0, 0.0, position: [f32; 2],
1.0, 1.0, 1.0, 1.0, texcoord: [f32; 2],
}
const OFFSCREEN_VBO_DATA: [WgpuVertex; 4] = [
WgpuVertex {
position: [-1.0, -1.0],
texcoord: [0.0, 0.0],
},
WgpuVertex {
position: [-1.0, 1.0],
texcoord: [0.0, 1.0],
},
WgpuVertex {
position: [1.0, -1.0],
texcoord: [1.0, 0.0],
},
WgpuVertex {
position: [1.0, 1.0],
texcoord: [1.0, 1.0],
},
]; ];
#[rustfmt::skip] const FINAL_VBO_DATA: [WgpuVertex; 4] = [
const VBO_DEFAULT_FINAL: [f32; 16] = [ WgpuVertex {
// Final position: [0.0, 0.0],
0.0f32, 0.0, 0.0, 0.0, texcoord: [0.0, 0.0],
0.0, 1.0, 0.0, 1.0, },
1.0, 0.0, 1.0, 0.0, WgpuVertex {
1.0, 1.0, 1.0, 1.0, position: [0.0, 1.0],
texcoord: [0.0, 1.0],
},
WgpuVertex {
position: [1.0, 0.0],
texcoord: [1.0, 0.0],
},
WgpuVertex {
position: [1.0, 1.0],
texcoord: [1.0, 1.0],
},
]; ];
const VBO_DATA: [f32; 32] = concat_arrays!(VBO_OFFSCREEN, VBO_DEFAULT_FINAL); static VBO_DATA: &[WgpuVertex; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA);
pub struct DrawQuad { pub struct DrawQuad {
buffer: Buffer, buffer: Buffer,
@ -31,7 +61,7 @@ impl DrawQuad {
pub fn new(device: &Device) -> DrawQuad { pub fn new(device: &Device) -> DrawQuad {
let buffer = device.create_buffer_init(&BufferInitDescriptor { let buffer = device.create_buffer_init(&BufferInitDescriptor {
label: Some("librashader vbo"), label: Some("librashader vbo"),
contents: bytemuck::cast_slice(&VBO_DATA), contents: bytemuck::cast_slice(VBO_DATA),
usage: wgpu::BufferUsages::VERTEX, usage: wgpu::BufferUsages::VERTEX,
}); });