rt(gl): use struct for draw_quad

This commit is contained in:
chyyran 2024-02-13 01:26:40 -05:00 committed by Ronny Chan
parent c121087348
commit aca5b5420c
7 changed files with 79 additions and 36 deletions

12
Cargo.lock generated
View file

@ -610,6 +610,17 @@ dependencies = [
"memchr",
]
[[package]]
name = "concat-arrays"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1df715824eb382e34b7afb7463b0247bf41538aeba731fba05241ecdb5dc3747"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "concurrent-queue"
version = "2.4.0"
@ -1666,6 +1677,7 @@ name = "librashader-runtime-gl"
version = "0.2.0-beta.9"
dependencies = [
"bytemuck",
"concat-arrays",
"gl",
"glfw 0.47.0",
"librashader-cache",

View file

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

View file

@ -22,9 +22,10 @@ spirv_cross = { package = "librashader-spirv-cross", version = "0.24" }
rustc-hash = "1.1.0"
gl = "0.14.0"
bytemuck = "1.12.3"
bytemuck = { version = "1.12.3", features = ["derive"] }
thiserror = "1.0.37"
rayon = "1.6.1"
concat-arrays = "0.1.2"
sptr = "0.3"

View file

@ -1,13 +1,7 @@
use crate::gl::DrawQuad;
use crate::gl::FINAL_VBO_DATA;
use crate::gl::{DrawQuad, OpenGLVertex};
use gl::types::{GLsizei, GLsizeiptr, GLuint};
#[rustfmt::skip]
static QUAD_VBO_DATA: &[f32; 16] = &[
0.0, 0.0, 0.0, 0.0,
1.0, 0.0, 1.0, 0.0,
0.0, 1.0, 0.0, 1.0,
1.0, 1.0, 1.0, 1.0,
];
use std::mem::offset_of;
pub struct Gl3DrawQuad {
vbo: GLuint,
@ -24,8 +18,8 @@ impl DrawQuad for Gl3DrawQuad {
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
gl::BufferData(
gl::ARRAY_BUFFER,
std::mem::size_of_val(QUAD_VBO_DATA) as GLsizeiptr,
QUAD_VBO_DATA.as_ptr().cast(),
4 * std::mem::size_of::<OpenGLVertex>() as GLsizeiptr,
FINAL_VBO_DATA.as_ptr().cast(),
gl::STATIC_DRAW,
);
gl::BindBuffer(gl::ARRAY_BUFFER, 0);
@ -51,16 +45,16 @@ impl DrawQuad for Gl3DrawQuad {
2,
gl::FLOAT,
gl::FALSE,
(4 * std::mem::size_of::<f32>()) as GLsizei,
sptr::invalid(0),
std::mem::size_of::<OpenGLVertex>() as GLsizei,
sptr::invalid(offset_of!(OpenGLVertex, position)),
);
gl::VertexAttribPointer(
1,
2,
gl::FLOAT,
gl::FALSE,
(4 * std::mem::size_of::<f32>()) as GLsizei,
sptr::invalid(2 * std::mem::size_of::<f32>()),
std::mem::size_of::<OpenGLVertex>() as GLsizei,
sptr::invalid(offset_of!(OpenGLVertex, position)),
);
}
}

View file

@ -1,14 +1,7 @@
use crate::gl::DrawQuad;
use crate::gl::FINAL_VBO_DATA;
use crate::gl::{DrawQuad, OpenGLVertex};
use gl::types::{GLint, GLsizeiptr, GLuint};
#[rustfmt::skip]
static QUAD_VBO_DATA: &[f32; 16] = &[
0.0, 0.0, 0.0, 0.0,
1.0, 0.0, 1.0, 0.0,
0.0, 1.0, 0.0, 1.0,
1.0, 1.0, 1.0, 1.0,
];
use std::mem::offset_of;
pub struct Gl46DrawQuad {
vbo: GLuint,
vao: GLuint,
@ -23,8 +16,8 @@ impl DrawQuad for Gl46DrawQuad {
gl::CreateBuffers(1, &mut vbo);
gl::NamedBufferData(
vbo,
std::mem::size_of_val(QUAD_VBO_DATA) as GLsizeiptr,
QUAD_VBO_DATA.as_ptr().cast(),
4 * std::mem::size_of::<OpenGLVertex>() as GLsizeiptr,
FINAL_VBO_DATA.as_ptr().cast(),
gl::STATIC_DRAW,
);
gl::CreateVertexArrays(1, &mut vao);
@ -32,16 +25,29 @@ impl DrawQuad for Gl46DrawQuad {
gl::EnableVertexArrayAttrib(vao, 0);
gl::EnableVertexArrayAttrib(vao, 1);
gl::VertexArrayVertexBuffer(vao, 0, vbo, 0, 4 * std::mem::size_of::<f32>() as GLint);
gl::VertexArrayVertexBuffer(
vao,
0,
vbo,
0,
std::mem::size_of::<OpenGLVertex>() as GLint,
);
gl::VertexArrayAttribFormat(vao, 0, 2, gl::FLOAT, gl::FALSE, 0);
gl::VertexArrayAttribFormat(
vao,
0,
2,
gl::FLOAT,
gl::FALSE,
offset_of!(OpenGLVertex, position) as GLuint,
);
gl::VertexArrayAttribFormat(
vao,
1,
2,
gl::FLOAT,
gl::FALSE,
2 * std::mem::size_of::<f32>() as GLuint,
offset_of!(OpenGLVertex, texcoord) as GLuint,
);
gl::VertexArrayAttribBinding(vao, 0, 0);

View file

@ -7,6 +7,7 @@ use crate::error::Result;
use crate::framebuffer::GLImage;
use crate::samplers::SamplerSet;
use crate::texture::InputTexture;
use bytemuck::{Pod, Zeroable};
pub use framebuffer::GLFramebuffer;
use gl::types::{GLenum, GLuint};
use librashader_common::{ImageFormat, Size};
@ -17,6 +18,33 @@ use librashader_reflect::reflect::semantics::{BufferReflection, TextureBinding};
use librashader_runtime::uniforms::UniformStorageAccess;
use rustc_hash::FxHashMap;
#[repr(C)]
#[derive(Debug, Copy, Clone, Default, Zeroable, Pod)]
pub(crate) struct OpenGLVertex {
pub position: [f32; 2],
pub texcoord: [f32; 2],
}
pub(crate) static FINAL_VBO_DATA: &[OpenGLVertex; 4] = &[
OpenGLVertex {
position: [0.0, 0.0],
texcoord: [0.0, 0.0],
},
OpenGLVertex {
position: [1.0, 0.0],
texcoord: [1.0, 0.0],
},
OpenGLVertex {
position: [0.0, 1.0],
texcoord: [0.0, 1.0],
},
OpenGLVertex {
position: [1.0, 1.0],
texcoord: [1.0, 1.0],
},
];
pub(crate) trait LoadLut {
fn load_luts(textures: &[TextureConfig]) -> Result<FxHashMap<usize, InputTexture>>;
}

View file

@ -1,12 +1,12 @@
use crate::error;
use crate::memory::VulkanBuffer;
use array_concat::concat_arrays;
use ash::vk;
use bytemuck::{Pod, Zeroable};
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;
// Vulkan does vertex expansion
#[repr(C)]
@ -75,8 +75,7 @@ impl DrawQuad {
{
let slice = buffer.as_mut_slice()?;
slice
.copy_from_slice(bytemuck::cast_slice(VBO_DATA));
slice.copy_from_slice(bytemuck::cast_slice(VBO_DATA));
}
Ok(DrawQuad {