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"
version = "0.2.0-beta.9"
dependencies = [
"array-concat",
"ash",
"ash-window",
"bytemuck",

View file

@ -149,6 +149,6 @@ mod test {
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();
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" }
rustc-hash = "1.1.0"
bytemuck = "1.12.3"
bytemuck = { version = "1.12.3", features = ["derive"] }
thiserror = "1.0.37"
ash = { version = "0.37", features = ["debug"] }
gpu-allocator = { version = "0.22.0", default-features = false, features = ["vulkan"] }
parking_lot = "0.12.1"
rayon = "1.6.1"
array-concat = "0.5.2"
[dev-dependencies]
num = "0.4.0"

View file

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

View file

@ -1,27 +1,57 @@
use array_concat::concat_arrays;
use bytemuck::{Pod, Zeroable};
use librashader_runtime::quad::QuadType;
use wgpu::util::{BufferInitDescriptor, DeviceExt};
use wgpu::{Buffer, 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,
// As per https://www.w3.org/TR/webgpu/#vertex-processing,
// WGPU does vertex expansion
#[repr(C)]
#[derive(Debug, Copy, Clone, Default, Zeroable, Pod)]
struct WgpuVertex {
position: [f32; 2],
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 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,
const FINAL_VBO_DATA: [WgpuVertex; 4] = [
WgpuVertex {
position: [0.0, 0.0],
texcoord: [0.0, 0.0],
},
WgpuVertex {
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 {
buffer: Buffer,
@ -31,7 +61,7 @@ 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),
contents: bytemuck::cast_slice(VBO_DATA),
usage: wgpu::BufferUsages::VERTEX,
});