diff --git a/Cargo.lock b/Cargo.lock index 9d330ea..906f8fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1685,6 +1685,7 @@ dependencies = [ name = "librashader-runtime-vk" version = "0.2.0-beta.9" dependencies = [ + "array-concat", "ash", "ash-window", "bytemuck", diff --git a/librashader-reflect/src/reflect/cross/msl.rs b/librashader-reflect/src/reflect/cross/msl.rs index 9201e5c..4d99793 100644 --- a/librashader-reflect/src/reflect/cross/msl.rs +++ b/librashader-reflect/src/reflect/cross/msl.rs @@ -149,6 +149,6 @@ mod test { let compiled = msl.compile(Some(msl::Version::V2_0)).unwrap(); - println!("{}", compiled.fragment); + println!("{}", compiled.vertex); } } diff --git a/librashader-reflect/src/reflect/naga/msl.rs b/librashader-reflect/src/reflect/naga/msl.rs index 3731f7d..997a8c0 100644 --- a/librashader-reflect/src/reflect/naga/msl.rs +++ b/librashader-reflect/src/reflect/naga/msl.rs @@ -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); } } diff --git a/librashader-runtime-vk/Cargo.toml b/librashader-runtime-vk/Cargo.toml index 514b099..dbf81b7 100644 --- a/librashader-runtime-vk/Cargo.toml +++ b/librashader-runtime-vk/Cargo.toml @@ -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" diff --git a/librashader-runtime-vk/src/draw_quad.rs b/librashader-runtime-vk/src/draw_quad.rs index 0658306..38d79f0 100644 --- a/librashader-runtime-vk/src/draw_quad.rs +++ b/librashader-runtime-vk/src/draw_quad.rs @@ -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, @@ -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 { diff --git a/librashader-runtime-wgpu/src/draw_quad.rs b/librashader-runtime-wgpu/src/draw_quad.rs index 5134835..db2d20e 100644 --- a/librashader-runtime-wgpu/src/draw_quad.rs +++ b/librashader-runtime-wgpu/src/draw_quad.rs @@ -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, });