rt: unify drawquad type

This commit is contained in:
chyyran 2024-02-22 00:41:29 -05:00 committed by Ronny Chan
parent 95ac8adc20
commit c7d1d347a4
15 changed files with 147 additions and 181 deletions

1
Cargo.lock generated
View file

@ -1591,6 +1591,7 @@ dependencies = [
name = "librashader-runtime" name = "librashader-runtime"
version = "0.2.2" version = "0.2.2"
dependencies = [ dependencies = [
"array-concat",
"bytemuck", "bytemuck",
"image", "image",
"librashader-common", "librashader-common",

View file

@ -2,7 +2,7 @@ use crate::error;
use crate::error::assume_d3d11_init; use crate::error::assume_d3d11_init;
use array_concat::concat_arrays; use array_concat::concat_arrays;
use bytemuck::offset_of; use bytemuck::offset_of;
use librashader_runtime::quad::QuadType; use librashader_runtime::quad::{QuadType, VertexInput};
use windows::core::PCSTR; use windows::core::PCSTR;
use windows::Win32::Graphics::Direct3D::D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; use windows::Win32::Graphics::Direct3D::D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
use windows::Win32::Graphics::Direct3D11::{ use windows::Win32::Graphics::Direct3D11::{
@ -12,52 +12,45 @@ use windows::Win32::Graphics::Direct3D11::{
}; };
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32_FLOAT; use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32_FLOAT;
#[repr(C)] const OFFSCREEN_VBO_DATA: [VertexInput; 4] = [
#[derive(Debug, Copy, Clone, Default)] VertexInput {
struct D3D11Vertex { position: [-1.0, -1.0, 0.0, 1.0],
position: [f32; 2],
texcoord: [f32; 2],
}
const OFFSCREEN_VBO_DATA: [D3D11Vertex; 4] = [
D3D11Vertex {
position: [-1.0, -1.0],
texcoord: [0.0, 1.0], texcoord: [0.0, 1.0],
}, },
D3D11Vertex { VertexInput {
position: [-1.0, 1.0], position: [-1.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 0.0], texcoord: [0.0, 0.0],
}, },
D3D11Vertex { VertexInput {
position: [1.0, -1.0], position: [1.0, -1.0, 0.0, 1.0],
texcoord: [1.0, 1.0], texcoord: [1.0, 1.0],
}, },
D3D11Vertex { VertexInput {
position: [1.0, 1.0], position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 0.0], texcoord: [1.0, 0.0],
}, },
]; ];
const FINAL_VBO_DATA: [D3D11Vertex; 4] = [ const FINAL_VBO_DATA: [VertexInput; 4] = [
D3D11Vertex { VertexInput {
position: [0.0, 0.0], position: [0.0, 0.0, 0.0, 1.0],
texcoord: [0.0, 1.0], texcoord: [0.0, 1.0],
}, },
D3D11Vertex { VertexInput {
position: [0.0, 1.0], position: [0.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 0.0], texcoord: [0.0, 0.0],
}, },
D3D11Vertex { VertexInput {
position: [1.0, 0.0], position: [1.0, 0.0, 0.0, 1.0],
texcoord: [1.0, 1.0], texcoord: [1.0, 1.0],
}, },
D3D11Vertex { VertexInput {
position: [1.0, 1.0], position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 0.0], texcoord: [1.0, 0.0],
}, },
]; ];
static VBO_DATA: &[D3D11Vertex; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA); static VBO_DATA: &[VertexInput; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA);
pub(crate) struct DrawQuad { pub(crate) struct DrawQuad {
stride: u32, stride: u32,
@ -70,7 +63,7 @@ impl DrawQuad {
let mut vbo = None; let mut vbo = None;
device.CreateBuffer( device.CreateBuffer(
&D3D11_BUFFER_DESC { &D3D11_BUFFER_DESC {
ByteWidth: 2 * std::mem::size_of::<[D3D11Vertex; 4]>() as u32, ByteWidth: 2 * std::mem::size_of::<[VertexInput; 4]>() as u32,
Usage: D3D11_USAGE_IMMUTABLE, Usage: D3D11_USAGE_IMMUTABLE,
BindFlags: D3D11_BIND_VERTEX_BUFFER, BindFlags: D3D11_BIND_VERTEX_BUFFER,
CPUAccessFlags: Default::default(), CPUAccessFlags: Default::default(),
@ -88,7 +81,7 @@ impl DrawQuad {
Ok(DrawQuad { Ok(DrawQuad {
vbo, vbo,
stride: std::mem::size_of::<D3D11Vertex>() as u32, stride: std::mem::size_of::<VertexInput>() as u32,
}) })
} }
} }
@ -125,7 +118,7 @@ impl DrawQuad {
SemanticIndex: 0, SemanticIndex: 0,
Format: DXGI_FORMAT_R32G32_FLOAT, Format: DXGI_FORMAT_R32G32_FLOAT,
InputSlot: 0, InputSlot: 0,
AlignedByteOffset: offset_of!(D3D11Vertex, position) as u32, AlignedByteOffset: offset_of!(VertexInput, position) as u32,
InputSlotClass: D3D11_INPUT_PER_VERTEX_DATA, InputSlotClass: D3D11_INPUT_PER_VERTEX_DATA,
InstanceDataStepRate: 0, InstanceDataStepRate: 0,
}, },
@ -134,7 +127,7 @@ impl DrawQuad {
SemanticIndex: 1, SemanticIndex: 1,
Format: DXGI_FORMAT_R32G32_FLOAT, Format: DXGI_FORMAT_R32G32_FLOAT,
InputSlot: 0, InputSlot: 0,
AlignedByteOffset: offset_of!(D3D11Vertex, texcoord) as u32, AlignedByteOffset: offset_of!(VertexInput, texcoord) as u32,
InputSlotClass: D3D11_INPUT_PER_VERTEX_DATA, InputSlotClass: D3D11_INPUT_PER_VERTEX_DATA,
InstanceDataStepRate: 0, InstanceDataStepRate: 0,
}, },

View file

@ -12,13 +12,13 @@ use librashader_runtime_d3d11::options::FilterChainOptionsD3D11;
// const FILTER_PATH: &str = // const FILTER_PATH: &str =
// "../test/Mega_Bezel_Packs/Duimon-Mega-Bezel/Presets/Advanced/Nintendo_GBA_SP/GBA_SP-[ADV]-[LCD-GRID].slangp"; // "../test/Mega_Bezel_Packs/Duimon-Mega-Bezel/Presets/Advanced/Nintendo_GBA_SP/GBA_SP-[ADV]-[LCD-GRID].slangp";
// const FILTER_PATH: &str = const FILTER_PATH: &str =
// "../test/shaders_slang/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp"; "../test/shaders_slang/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp";
// const FILTER_PATH: &str = "../test/slang-shaders/test/history.slangp"; // const FILTER_PATH: &str = "../test/slang-shaders/test/history.slangp";
// const FILTER_PATH: &str = "../test/slang-shaders/test/feedback.slangp"; // const FILTER_PATH: &str = "../test/slang-shaders/test/feedback.slangp";
const FILTER_PATH: &str = "../test/shaders_slang/crt/crt-royale.slangp"; // const FILTER_PATH: &str = "../test/shaders_slang/crt/crt-royale.slangp";
const IMAGE_PATH: &str = "../triangle.png"; const IMAGE_PATH: &str = "../triangle.png";
#[test] #[test]
fn triangle_d3d11_args() { fn triangle_d3d11_args() {

View file

@ -1,8 +1,8 @@
use crate::buffer::D3D12Buffer; use crate::buffer::D3D12Buffer;
use crate::error; use crate::error;
use array_concat::concat_arrays; use array_concat::concat_arrays;
use bytemuck::{offset_of, Pod, Zeroable}; use bytemuck::offset_of;
use librashader_runtime::quad::QuadType; use librashader_runtime::quad::{QuadType, VertexInput};
use windows::core::PCSTR; use windows::core::PCSTR;
use windows::Win32::Graphics::Direct3D::D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; use windows::Win32::Graphics::Direct3D::D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
use windows::Win32::Graphics::Direct3D12::{ use windows::Win32::Graphics::Direct3D12::{
@ -11,52 +11,45 @@ use windows::Win32::Graphics::Direct3D12::{
}; };
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32_FLOAT; use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32_FLOAT;
#[repr(C)] const OFFSCREEN_VBO_DATA: [VertexInput; 4] = [
#[derive(Debug, Copy, Clone, Default, Zeroable, Pod)] VertexInput {
struct D3D12Vertex { position: [-1.0, -1.0, 0.0, 1.0],
position: [f32; 2],
texcoord: [f32; 2],
}
const OFFSCREEN_VBO_DATA: [D3D12Vertex; 4] = [
D3D12Vertex {
position: [-1.0, -1.0],
texcoord: [0.0, 1.0], texcoord: [0.0, 1.0],
}, },
D3D12Vertex { VertexInput {
position: [-1.0, 1.0], position: [-1.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 0.0], texcoord: [0.0, 0.0],
}, },
D3D12Vertex { VertexInput {
position: [1.0, -1.0], position: [1.0, -1.0, 0.0, 1.0],
texcoord: [1.0, 1.0], texcoord: [1.0, 1.0],
}, },
D3D12Vertex { VertexInput {
position: [1.0, 1.0], position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 0.0], texcoord: [1.0, 0.0],
}, },
]; ];
const FINAL_VBO_DATA: [D3D12Vertex; 4] = [ const FINAL_VBO_DATA: [VertexInput; 4] = [
D3D12Vertex { VertexInput {
position: [0.0, 0.0], position: [0.0, 0.0, 0.0, 1.0],
texcoord: [0.0, 1.0], texcoord: [0.0, 1.0],
}, },
D3D12Vertex { VertexInput {
position: [0.0, 1.0], position: [0.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 0.0], texcoord: [0.0, 0.0],
}, },
D3D12Vertex { VertexInput {
position: [1.0, 0.0], position: [1.0, 0.0, 0.0, 1.0],
texcoord: [1.0, 1.0], texcoord: [1.0, 1.0],
}, },
D3D12Vertex { VertexInput {
position: [1.0, 1.0], position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 0.0], texcoord: [1.0, 0.0],
}, },
]; ];
static VBO_DATA: &[D3D12Vertex; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA); static VBO_DATA: &[VertexInput; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA);
pub(crate) struct DrawQuad { pub(crate) struct DrawQuad {
_buffer: ID3D12Resource, _buffer: ID3D12Resource,
@ -65,8 +58,8 @@ pub(crate) struct DrawQuad {
impl DrawQuad { impl DrawQuad {
pub fn new(device: &ID3D12Device) -> error::Result<DrawQuad> { pub fn new(device: &ID3D12Device) -> error::Result<DrawQuad> {
let stride = std::mem::size_of::<D3D12Vertex>() as u32; let stride = std::mem::size_of::<VertexInput>() as u32;
let size = 2 * std::mem::size_of::<[D3D12Vertex; 4]>() as u32; let size = 2 * std::mem::size_of::<[VertexInput; 4]>() as u32;
let mut buffer = D3D12Buffer::new(device, size as usize)?; let mut buffer = D3D12Buffer::new(device, size as usize)?;
buffer buffer
.map(None)? .map(None)?
@ -110,7 +103,7 @@ impl DrawQuad {
SemanticIndex: 0, SemanticIndex: 0,
Format: DXGI_FORMAT_R32G32_FLOAT, Format: DXGI_FORMAT_R32G32_FLOAT,
InputSlot: 0, InputSlot: 0,
AlignedByteOffset: offset_of!(D3D12Vertex, position) as u32, AlignedByteOffset: offset_of!(VertexInput, position) as u32,
InputSlotClass: D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, InputSlotClass: D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
InstanceDataStepRate: 0, InstanceDataStepRate: 0,
}, },
@ -119,7 +112,7 @@ impl DrawQuad {
SemanticIndex: 1, SemanticIndex: 1,
Format: DXGI_FORMAT_R32G32_FLOAT, Format: DXGI_FORMAT_R32G32_FLOAT,
InputSlot: 0, InputSlot: 0,
AlignedByteOffset: offset_of!(D3D12Vertex, texcoord) as u32, AlignedByteOffset: offset_of!(VertexInput, texcoord) as u32,
InputSlotClass: D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, InputSlotClass: D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
InstanceDataStepRate: 0, InstanceDataStepRate: 0,
}, },

View file

@ -1,8 +1,8 @@
use crate::gl::{DrawQuad, OpenGLVertex}; use crate::gl::DrawQuad;
use crate::gl::{FINAL_VBO_DATA, OFFSCREEN_VBO_DATA}; use crate::gl::{FINAL_VBO_DATA, OFFSCREEN_VBO_DATA};
use bytemuck::offset_of; use bytemuck::offset_of;
use gl::types::{GLsizei, GLsizeiptr, GLuint}; use gl::types::{GLsizei, GLsizeiptr, GLuint};
use librashader_runtime::quad::QuadType; use librashader_runtime::quad::{QuadType, VertexInput};
pub struct Gl3DrawQuad { pub struct Gl3DrawQuad {
vbo: [GLuint; 2], vbo: [GLuint; 2],
vao: GLuint, vao: GLuint,
@ -59,16 +59,16 @@ impl DrawQuad for Gl3DrawQuad {
4, 4,
gl::FLOAT, gl::FLOAT,
gl::FALSE, gl::FALSE,
std::mem::size_of::<OpenGLVertex>() as GLsizei, std::mem::size_of::<VertexInput>() as GLsizei,
sptr::invalid(offset_of!(OpenGLVertex, position)), sptr::invalid(offset_of!(VertexInput, position)),
); );
gl::VertexAttribPointer( gl::VertexAttribPointer(
1, 1,
2, 2,
gl::FLOAT, gl::FLOAT,
gl::FALSE, gl::FALSE,
std::mem::size_of::<OpenGLVertex>() as GLsizei, std::mem::size_of::<VertexInput>() as GLsizei,
sptr::invalid(offset_of!(OpenGLVertex, texcoord)), sptr::invalid(offset_of!(VertexInput, texcoord)),
); );
} }
} }

View file

@ -1,8 +1,8 @@
use crate::gl::{DrawQuad, OpenGLVertex}; use crate::gl::DrawQuad;
use crate::gl::{FINAL_VBO_DATA, OFFSCREEN_VBO_DATA}; use crate::gl::{FINAL_VBO_DATA, OFFSCREEN_VBO_DATA};
use bytemuck::offset_of; use bytemuck::offset_of;
use gl::types::{GLint, GLsizeiptr, GLuint}; use gl::types::{GLint, GLsizeiptr, GLuint};
use librashader_runtime::quad::QuadType; use librashader_runtime::quad::{QuadType, VertexInput};
pub struct Gl46DrawQuad { pub struct Gl46DrawQuad {
vbo: [GLuint; 2], vbo: [GLuint; 2],
@ -41,7 +41,7 @@ impl DrawQuad for Gl46DrawQuad {
4, 4,
gl::FLOAT, gl::FLOAT,
gl::FALSE, gl::FALSE,
offset_of!(OpenGLVertex, position) as GLuint, offset_of!(VertexInput, position) as GLuint,
); );
gl::VertexArrayAttribFormat( gl::VertexArrayAttribFormat(
vao, vao,
@ -49,7 +49,7 @@ impl DrawQuad for Gl46DrawQuad {
2, 2,
gl::FLOAT, gl::FLOAT,
gl::FALSE, gl::FALSE,
offset_of!(OpenGLVertex, texcoord) as GLuint, offset_of!(VertexInput, texcoord) as GLuint,
); );
gl::VertexArrayAttribBinding(vao, 0, 0); gl::VertexArrayAttribBinding(vao, 0, 0);
@ -71,7 +71,7 @@ impl DrawQuad for Gl46DrawQuad {
0, 0,
self.vbo[buffer_index], self.vbo[buffer_index],
0, 0,
std::mem::size_of::<OpenGLVertex>() as GLint, std::mem::size_of::<VertexInput>() as GLint,
); );
gl::BindVertexArray(self.vao); gl::BindVertexArray(self.vao);

View file

@ -7,7 +7,6 @@ use crate::error::Result;
use crate::framebuffer::GLImage; use crate::framebuffer::GLImage;
use crate::samplers::SamplerSet; use crate::samplers::SamplerSet;
use crate::texture::InputTexture; use crate::texture::InputTexture;
use bytemuck::{Pod, Zeroable};
pub use framebuffer::GLFramebuffer; pub use framebuffer::GLFramebuffer;
use gl::types::{GLenum, GLuint}; use gl::types::{GLenum, GLuint};
use librashader_common::map::FastHashMap; use librashader_common::map::FastHashMap;
@ -16,50 +15,42 @@ use librashader_presets::{Scale2D, TextureConfig};
use librashader_reflect::back::glsl::CrossGlslContext; use librashader_reflect::back::glsl::CrossGlslContext;
use librashader_reflect::back::ShaderCompilerOutput; use librashader_reflect::back::ShaderCompilerOutput;
use librashader_reflect::reflect::semantics::{BufferReflection, TextureBinding}; use librashader_reflect::reflect::semantics::{BufferReflection, TextureBinding};
use librashader_runtime::quad::QuadType; use librashader_runtime::quad::{QuadType, VertexInput};
use librashader_runtime::uniforms::UniformStorageAccess; use librashader_runtime::uniforms::UniformStorageAccess;
#[repr(C)] static OFFSCREEN_VBO_DATA: &[VertexInput; 4] = &[
#[derive(Debug, Copy, Clone, Default, Zeroable, Pod)] VertexInput {
pub(crate) struct OpenGLVertex {
pub position: [f32; 4],
pub texcoord: [f32; 2],
}
static OFFSCREEN_VBO_DATA: &[OpenGLVertex; 4] = &[
OpenGLVertex {
position: [-1.0, -1.0, 0.0, 1.0], position: [-1.0, -1.0, 0.0, 1.0],
texcoord: [0.0, 0.0], texcoord: [0.0, 0.0],
}, },
OpenGLVertex { VertexInput {
position: [1.0, -1.0, 0.0, 1.0], position: [1.0, -1.0, 0.0, 1.0],
texcoord: [1.0, 0.0], texcoord: [1.0, 0.0],
}, },
OpenGLVertex { VertexInput {
position: [-1.0, 1.0, 0.0, 1.0], position: [-1.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 1.0], texcoord: [0.0, 1.0],
}, },
OpenGLVertex { VertexInput {
position: [1.0, 1.0, 0.0, 1.0], position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 1.0], texcoord: [1.0, 1.0],
}, },
]; ];
static FINAL_VBO_DATA: &[OpenGLVertex; 4] = &[ static FINAL_VBO_DATA: &[VertexInput; 4] = &[
OpenGLVertex { VertexInput {
position: [0.0, 0.0, 0.0, 1.0], position: [0.0, 0.0, 0.0, 1.0],
texcoord: [0.0, 0.0], texcoord: [0.0, 0.0],
}, },
OpenGLVertex { VertexInput {
position: [1.0, 0.0, 0.0, 1.0], position: [1.0, 0.0, 0.0, 1.0],
texcoord: [1.0, 0.0], texcoord: [1.0, 0.0],
}, },
OpenGLVertex { VertexInput {
position: [0.0, 1.0, 0.0, 1.0], position: [0.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 1.0], texcoord: [0.0, 1.0],
}, },
OpenGLVertex { VertexInput {
position: [1.0, 1.0, 0.0, 1.0], position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 1.0], texcoord: [1.0, 1.0],
}, },

View file

@ -1,10 +1,9 @@
use array_concat::concat_arrays; use array_concat::concat_arrays;
use bytemuck::{Pod, Zeroable};
use icrate::Metal::{ use icrate::Metal::{
MTLBuffer, MTLDevice, MTLPrimitiveTypeTriangleStrip, MTLRenderCommandEncoder, MTLBuffer, MTLDevice, MTLPrimitiveTypeTriangleStrip, MTLRenderCommandEncoder,
MTLResourceStorageModeManaged, MTLResourceStorageModeShared, MTLResourceStorageModeManaged, MTLResourceStorageModeShared,
}; };
use librashader_runtime::quad::QuadType; use librashader_runtime::quad::{QuadType, VertexInput};
use objc2::rc::Id; use objc2::rc::Id;
use objc2::runtime::ProtocolObject; use objc2::runtime::ProtocolObject;
use std::ffi::c_void; use std::ffi::c_void;
@ -13,52 +12,45 @@ use std::ptr::NonNull;
use crate::error::{FilterChainError, Result}; use crate::error::{FilterChainError, Result};
use crate::graphics_pipeline::VERTEX_BUFFER_INDEX; use crate::graphics_pipeline::VERTEX_BUFFER_INDEX;
#[repr(C)] const OFFSCREEN_VBO_DATA: [VertexInput; 4] = [
#[derive(Debug, Copy, Clone, Default, Zeroable, Pod)] VertexInput {
pub(crate) struct MetalVertex {
pub position: [f32; 4],
pub texcoord: [f32; 2],
}
const OFFSCREEN_VBO_DATA: [MetalVertex; 4] = [
MetalVertex {
position: [-1.0, -1.0, 0.0, 1.0], position: [-1.0, -1.0, 0.0, 1.0],
texcoord: [0.0, 1.0], texcoord: [0.0, 1.0],
}, },
MetalVertex { VertexInput {
position: [-1.0, 1.0, 0.0, 1.0], position: [-1.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 0.0], texcoord: [0.0, 0.0],
}, },
MetalVertex { VertexInput {
position: [1.0, -1.0, 0.0, 1.0], position: [1.0, -1.0, 0.0, 1.0],
texcoord: [1.0, 1.0], texcoord: [1.0, 1.0],
}, },
MetalVertex { VertexInput {
position: [1.0, 1.0, 0.0, 1.0], position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 0.0], texcoord: [1.0, 0.0],
}, },
]; ];
const FINAL_VBO_DATA: [MetalVertex; 4] = [ const FINAL_VBO_DATA: [VertexInput; 4] = [
MetalVertex { VertexInput {
position: [0.0, 0.0, 0.0, 1.0], position: [0.0, 0.0, 0.0, 1.0],
texcoord: [0.0, 1.0], texcoord: [0.0, 1.0],
}, },
MetalVertex { VertexInput {
position: [0.0, 1.0, 0.0, 1.0], position: [0.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 0.0], texcoord: [0.0, 0.0],
}, },
MetalVertex { VertexInput {
position: [1.0, 0.0, 0.0, 1.0], position: [1.0, 0.0, 0.0, 1.0],
texcoord: [1.0, 1.0], texcoord: [1.0, 1.0],
}, },
MetalVertex { VertexInput {
position: [1.0, 1.0, 0.0, 1.0], position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 0.0], texcoord: [1.0, 0.0],
}, },
]; ];
const VBO_DATA: [MetalVertex; 8] = concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA); const VBO_DATA: [VertexInput; 8] = concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA);
pub struct DrawQuad { pub struct DrawQuad {
buffer: Id<ProtocolObject<dyn MTLBuffer>>, buffer: Id<ProtocolObject<dyn MTLBuffer>>,

View file

@ -1,4 +1,3 @@
use crate::draw_quad::MetalVertex;
use crate::error::{FilterChainError, Result}; use crate::error::{FilterChainError, Result};
use crate::select_optimal_pixel_format; use crate::select_optimal_pixel_format;
use bytemuck::offset_of; use bytemuck::offset_of;
@ -14,7 +13,9 @@ use icrate::Metal::{
}; };
use librashader_reflect::back::msl::{CrossMslContext, NagaMslContext}; use librashader_reflect::back::msl::{CrossMslContext, NagaMslContext};
use librashader_reflect::back::ShaderCompilerOutput; use librashader_reflect::back::ShaderCompilerOutput;
use librashader_runtime::quad::VertexInput;
use librashader_runtime::render_target::RenderTarget; use librashader_runtime::render_target::RenderTarget;
use objc2::rc::Id; use objc2::rc::Id;
use objc2::runtime::ProtocolObject; use objc2::runtime::ProtocolObject;
@ -90,18 +91,18 @@ impl PipelineLayoutObjects {
// hopefully metal fills in vertices otherwise we'll need to use the vec4 stuff. // hopefully metal fills in vertices otherwise we'll need to use the vec4 stuff.
position.setFormat(MTLVertexFormatFloat4); position.setFormat(MTLVertexFormatFloat4);
position.setBufferIndex(VERTEX_BUFFER_INDEX); position.setBufferIndex(VERTEX_BUFFER_INDEX);
position.setOffset(offset_of!(MetalVertex, position)); position.setOffset(offset_of!(VertexInput, position));
texcoord.setFormat(MTLVertexFormatFloat2); texcoord.setFormat(MTLVertexFormatFloat2);
texcoord.setBufferIndex(VERTEX_BUFFER_INDEX); texcoord.setBufferIndex(VERTEX_BUFFER_INDEX);
texcoord.setOffset(offset_of!(MetalVertex, texcoord)); texcoord.setOffset(offset_of!(VertexInput, texcoord));
attributes.setObject_atIndexedSubscript(Some(&position), 0); attributes.setObject_atIndexedSubscript(Some(&position), 0);
attributes.setObject_atIndexedSubscript(Some(&texcoord), 1); attributes.setObject_atIndexedSubscript(Some(&texcoord), 1);
binding.setStepFunction(MTLVertexStepFunctionPerVertex); binding.setStepFunction(MTLVertexStepFunctionPerVertex);
binding.setStride(std::mem::size_of::<MetalVertex>()); binding.setStride(std::mem::size_of::<VertexInput>());
layouts.setObject_atIndexedSubscript(Some(&binding), VERTEX_BUFFER_INDEX); layouts.setObject_atIndexedSubscript(Some(&binding), VERTEX_BUFFER_INDEX);
descriptor descriptor

View file

@ -2,59 +2,50 @@ use crate::error;
use crate::memory::VulkanBuffer; use crate::memory::VulkanBuffer;
use array_concat::concat_arrays; use array_concat::concat_arrays;
use ash::vk; use ash::vk;
use bytemuck::{Pod, Zeroable};
use gpu_allocator::vulkan::Allocator; use gpu_allocator::vulkan::Allocator;
use librashader_runtime::quad::QuadType; use librashader_runtime::quad::{QuadType, VertexInput};
use parking_lot::RwLock; use parking_lot::RwLock;
use std::sync::Arc; use std::sync::Arc;
// Vulkan does vertex expansion const OFFSCREEN_VBO_DATA: [VertexInput; 4] = [
#[repr(C)] VertexInput {
#[derive(Debug, Copy, Clone, Default, Zeroable, Pod)] position: [-1.0, -1.0, 0.0, 1.0],
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], texcoord: [0.0, 0.0],
}, },
VulkanVertex { VertexInput {
position: [-1.0, 1.0], position: [-1.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 1.0], texcoord: [0.0, 1.0],
}, },
VulkanVertex { VertexInput {
position: [1.0, -1.0], position: [1.0, -1.0, 0.0, 1.0],
texcoord: [1.0, 0.0], texcoord: [1.0, 0.0],
}, },
VulkanVertex { VertexInput {
position: [1.0, 1.0], position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 1.0], texcoord: [1.0, 1.0],
}, },
]; ];
const FINAL_VBO_DATA: [VulkanVertex; 4] = [ const FINAL_VBO_DATA: [VertexInput; 4] = [
VulkanVertex { VertexInput {
position: [0.0, 0.0], position: [0.0, 0.0, 0.0, 1.0],
texcoord: [0.0, 0.0], texcoord: [0.0, 0.0],
}, },
VulkanVertex { VertexInput {
position: [0.0, 1.0], position: [0.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 1.0], texcoord: [0.0, 1.0],
}, },
VulkanVertex { VertexInput {
position: [1.0, 0.0], position: [1.0, 0.0, 0.0, 1.0],
texcoord: [1.0, 0.0], texcoord: [1.0, 0.0],
}, },
VulkanVertex { VertexInput {
position: [1.0, 1.0], position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 1.0], texcoord: [1.0, 1.0],
}, },
]; ];
static VBO_DATA: &[VulkanVertex; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA); static VBO_DATA: &[VertexInput; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA);
pub struct DrawQuad { pub struct DrawQuad {
buffer: VulkanBuffer, buffer: VulkanBuffer,
@ -70,7 +61,7 @@ impl DrawQuad {
device, device,
allocator, allocator,
vk::BufferUsageFlags::VERTEX_BUFFER, vk::BufferUsageFlags::VERTEX_BUFFER,
std::mem::size_of::<[VulkanVertex; 8]>(), std::mem::size_of::<[VertexInput; 8]>(),
)?; )?;
{ {

View file

@ -5,14 +5,15 @@ use crate::error::FilterChainError;
use crate::framebuffer::OutputImage; use crate::framebuffer::OutputImage;
use crate::render_pass::VulkanRenderPass; use crate::render_pass::VulkanRenderPass;
use ash::vk::PushConstantRange; use ash::vk::PushConstantRange;
use bytemuck::offset_of;
use librashader_cache::cache_pipeline; use librashader_cache::cache_pipeline;
use librashader_reflect::back::ShaderCompilerOutput; use librashader_reflect::back::ShaderCompilerOutput;
use librashader_reflect::reflect::semantics::{BufferReflection, TextureBinding}; use librashader_reflect::reflect::semantics::{BufferReflection, TextureBinding};
use librashader_reflect::reflect::ShaderReflection; use librashader_reflect::reflect::ShaderReflection;
use librashader_runtime::quad::VertexInput;
use librashader_runtime::render_target::RenderTarget; use librashader_runtime::render_target::RenderTarget;
use std::ffi::CStr; use std::ffi::CStr;
use std::sync::Arc; use std::sync::Arc;
const ENTRY_POINT: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"main\0") }; const ENTRY_POINT: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"main\0") };
pub struct PipelineDescriptors { pub struct PipelineDescriptors {
@ -201,19 +202,19 @@ impl VulkanGraphicsPipeline {
location: 0, location: 0,
binding: 0, binding: 0,
format: vk::Format::R32G32_SFLOAT, format: vk::Format::R32G32_SFLOAT,
offset: 0, offset: offset_of!(VertexInput, position) as u32,
}, },
vk::VertexInputAttributeDescription { vk::VertexInputAttributeDescription {
location: 1, location: 1,
binding: 0, binding: 0,
format: vk::Format::R32G32_SFLOAT, format: vk::Format::R32G32_SFLOAT,
offset: (2 * std::mem::size_of::<f32>()) as u32, offset: offset_of!(VertexInput, texcoord) as u32,
}, },
]; ];
let input_binding = vk::VertexInputBindingDescription::builder() let input_binding = vk::VertexInputBindingDescription::builder()
.binding(0) .binding(0)
.stride(4 * std::mem::size_of::<f32>() as u32) .stride(std::mem::size_of::<VertexInput>() as u32)
.input_rate(vk::VertexInputRate::VERTEX); .input_rate(vk::VertexInputRate::VERTEX);
let input_binding = [*input_binding]; let input_binding = [*input_binding];

View file

@ -1,57 +1,47 @@
use array_concat::concat_arrays; use array_concat::concat_arrays;
use bytemuck::{Pod, Zeroable}; use librashader_runtime::quad::{QuadType, VertexInput};
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};
// As per https://www.w3.org/TR/webgpu/#vertex-processing, const OFFSCREEN_VBO_DATA: [VertexInput; 4] = [
// WGPU does vertex expansion VertexInput {
#[repr(C)]
#[derive(Debug, Copy, Clone, Default, Zeroable, Pod)]
pub struct WgpuVertex {
pub position: [f32; 4],
pub texcoord: [f32; 2],
}
const OFFSCREEN_VBO_DATA: [WgpuVertex; 4] = [
WgpuVertex {
position: [-1.0, -1.0, 0.0, 1.0], position: [-1.0, -1.0, 0.0, 1.0],
texcoord: [0.0, 0.0], texcoord: [0.0, 0.0],
}, },
WgpuVertex { VertexInput {
position: [-1.0, 1.0, 0.0, 1.0], position: [-1.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 1.0], texcoord: [0.0, 1.0],
}, },
WgpuVertex { VertexInput {
position: [1.0, -1.0, 0.0, 1.0], position: [1.0, -1.0, 0.0, 1.0],
texcoord: [1.0, 0.0], texcoord: [1.0, 0.0],
}, },
WgpuVertex { VertexInput {
position: [1.0, 1.0, 0.0, 1.0], position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 1.0], texcoord: [1.0, 1.0],
}, },
]; ];
const FINAL_VBO_DATA: [WgpuVertex; 4] = [ const FINAL_VBO_DATA: [VertexInput; 4] = [
WgpuVertex { VertexInput {
position: [0.0, 0.0, 0.0, 1.0], position: [0.0, 0.0, 0.0, 1.0],
texcoord: [0.0, 0.0], texcoord: [0.0, 0.0],
}, },
WgpuVertex { VertexInput {
position: [0.0, 1.0, 0.0, 1.0], position: [0.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 1.0], texcoord: [0.0, 1.0],
}, },
WgpuVertex { VertexInput {
position: [1.0, 0.0, 0.0, 1.0], position: [1.0, 0.0, 0.0, 1.0],
texcoord: [1.0, 0.0], texcoord: [1.0, 0.0],
}, },
WgpuVertex { VertexInput {
position: [1.0, 1.0, 0.0, 1.0], position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 1.0], texcoord: [1.0, 1.0],
}, },
]; ];
static VBO_DATA: &[WgpuVertex; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA); static VBO_DATA: &[VertexInput; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA);
pub struct DrawQuad { pub struct DrawQuad {
buffer: Buffer, buffer: Buffer,

View file

@ -1,10 +1,10 @@
use crate::draw_quad::WgpuVertex;
use crate::framebuffer::WgpuOutputView; use crate::framebuffer::WgpuOutputView;
use crate::util; use crate::util;
use librashader_reflect::back::wgsl::NagaWgslContext; use librashader_reflect::back::wgsl::NagaWgslContext;
use librashader_reflect::back::ShaderCompilerOutput; use librashader_reflect::back::ShaderCompilerOutput;
use librashader_reflect::reflect::ShaderReflection; use librashader_reflect::reflect::ShaderReflection;
use librashader_runtime::render_target::RenderTarget; use librashader_runtime::render_target::RenderTarget;
use librashader_runtime::quad::VertexInput;
use std::borrow::Cow; use std::borrow::Cow;
use std::sync::Arc; use std::sync::Arc;
use wgpu::{ use wgpu::{
@ -153,18 +153,18 @@ impl PipelineLayoutObjects {
module: &self.vertex, module: &self.vertex,
entry_point: &self.vertex_entry_name, entry_point: &self.vertex_entry_name,
buffers: &[VertexBufferLayout { buffers: &[VertexBufferLayout {
array_stride: std::mem::size_of::<WgpuVertex>() as wgpu::BufferAddress, array_stride: std::mem::size_of::<VertexInput>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex, step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[ attributes: &[
wgpu::VertexAttribute { wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x4, format: wgpu::VertexFormat::Float32x4,
offset: bytemuck::offset_of!(WgpuVertex, position) offset: bytemuck::offset_of!(VertexInput, position)
as wgpu::BufferAddress, as wgpu::BufferAddress,
shader_location: 0, shader_location: 0,
}, },
wgpu::VertexAttribute { wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2, format: wgpu::VertexFormat::Float32x2,
offset: bytemuck::offset_of!(WgpuVertex, texcoord) offset: bytemuck::offset_of!(VertexInput, texcoord)
as wgpu::BufferAddress, as wgpu::BufferAddress,
shader_location: 1, shader_location: 1,
}, },

View file

@ -18,6 +18,7 @@ librashader-preprocess = { path = "../librashader-preprocess", version = "0.2.2"
librashader-reflect = { path = "../librashader-reflect", version = "0.2.2" } librashader-reflect = { path = "../librashader-reflect", version = "0.2.2" }
bytemuck = "1.12.3" bytemuck = "1.12.3"
num-traits = "0.2.15" num-traits = "0.2.15"
array-concat = "0.5.2"
tinymap = "0.4.0" tinymap = "0.4.0"

View file

@ -1,3 +1,5 @@
use bytemuck::{Pod, Zeroable};
/// Different type of quad to render to depending on pass type /// Different type of quad to render to depending on pass type
pub enum QuadType { pub enum QuadType {
/// Offscreen, intermediate passes. /// Offscreen, intermediate passes.
@ -23,3 +25,13 @@ pub static DEFAULT_MVP: &[f32; 16] = &[
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
-1.0, -1.0, 0.0, 1.0, -1.0, -1.0, 0.0, 1.0,
]; ];
/// The vertex inputs to a slang shader
///
/// See [IO interface variables](https://github.com/libretro/slang-shaders?tab=readme-ov-file#io-interface-variables)
#[repr(C)]
#[derive(Debug, Copy, Clone, Default, Zeroable, Pod)]
pub struct VertexInput {
pub position: [f32; 4], // vec4 position
pub texcoord: [f32; 2], // vec2 texcoord;
}