Skip to content

Commit

Permalink
Merge pull request #620 from phaazon/shader-f64
Browse files Browse the repository at this point in the history
Re-add support for f64 (feature gated).
  • Loading branch information
hadronized committed Jan 21, 2023
2 parents 4963cf5 + 1603c80 commit f01df4b
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 4 deletions.
3 changes: 1 addition & 2 deletions luminance-gl2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ maintenance = { status = "actively-developed" }
[features]
default = ["GL33"]
GL33 = []
# OpenGL extensions
GL_ARB_gpu_shader_fp64 = []
shader-f64 = []

[dependencies]
gl = "0.14"
Expand Down
104 changes: 102 additions & 2 deletions luminance-gl2/src/gl33.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,12 +1278,12 @@ impl StageHandle {
}
}

#[cfg(feature = "GL_ARB_gpu_shader_fp64")]
#[cfg(feature = "shader-f64")]
const GLSL_PRAGMA: &str = "#version 330 core\n\
#extension GL_ARB_separate_shader_objects : require\n
#extension GL_ARB_gpu_shader_fp64 : require\n\
layout(std140) uniform;\n";
#[cfg(not(feature = "GL_ARB_gpu_shader_fp64"))]
#[cfg(not(feature = "shader-f64"))]
const GLSL_PRAGMA: &str = "#version 330 core\n\
#extension GL_ARB_separate_shader_objects : require\n\
layout(std140) uniform;\n";
Expand Down Expand Up @@ -2678,6 +2678,15 @@ unsafe impl ShaderBackend for GL33 {
Ok(())
}

#[cfg(feature = "shader-f64")]
fn visit_f64(&mut self, uni: &Uni<f64>, value: &f64) -> Result<(), ShaderError> {
unsafe {
gl::Uniform1d(uni.handle() as GLint, *value);
}

Ok(())
}

fn visit_bool(&mut self, uni: &Uni<bool>, value: &bool) -> Result<(), ShaderError> {
unsafe {
gl::Uniform1ui(uni.handle() as GLint, *value as u32);
Expand Down Expand Up @@ -2722,6 +2731,19 @@ unsafe impl ShaderBackend for GL33 {
Ok(())
}

#[cfg(feature = "shader-f64")]
fn visit_f64_array<const N: usize>(
&mut self,
uni: &Uni<[f64; N]>,
value: &[f64; N],
) -> Result<(), ShaderError> {
unsafe {
gl::Uniform1dv(uni.handle() as GLint, N as GLsizei, value.as_ptr());
}

Ok(())
}

fn visit_bool_array<const N: usize>(
&mut self,
uni: &Uni<[bool; N]>,
Expand Down Expand Up @@ -2770,6 +2792,18 @@ unsafe impl ShaderBackend for GL33 {
Ok(())
}

#[cfg(feature = "shader-f64")]
fn visit_dvec2<T>(&mut self, uni: &Uni<T>, value: &[f64; 2]) -> Result<(), ShaderError>
where
T: AsRef<[f64; 2]>,
{
unsafe {
gl::Uniform2d(uni.handle() as GLint, value[0], value[1]);
}

Ok(())
}

fn visit_bvec2<T>(&mut self, uni: &Uni<T>, value: &[bool; 2]) -> Result<(), ShaderError>
where
T: AsRef<[bool; 2]>,
Expand Down Expand Up @@ -2814,6 +2848,18 @@ unsafe impl ShaderBackend for GL33 {
Ok(())
}

#[cfg(feature = "shader-f64")]
fn visit_dvec3<T>(&mut self, uni: &Uni<T>, value: &[f64; 3]) -> Result<(), ShaderError>
where
T: AsRef<[f64; 3]>,
{
unsafe {
gl::Uniform3d(uni.handle() as GLint, value[0], value[1], value[2]);
}

Ok(())
}

fn visit_bvec3<T>(&mut self, uni: &Uni<T>, value: &[bool; 3]) -> Result<(), ShaderError>
where
T: AsRef<[bool; 3]>,
Expand Down Expand Up @@ -2881,6 +2927,24 @@ unsafe impl ShaderBackend for GL33 {
Ok(())
}

#[cfg(feature = "shader-f64")]
fn visit_dvec4<T>(&mut self, uni: &Uni<T>, value: &[f64; 4]) -> Result<(), ShaderError>
where
T: AsRef<[f64; 4]>,
{
unsafe {
gl::Uniform4d(
uni.handle() as GLint,
value[0],
value[1],
value[2],
value[3],
);
}

Ok(())
}

fn visit_bvec4<T>(&mut self, uni: &Uni<T>, value: &[bool; 4]) -> Result<(), ShaderError>
where
T: AsRef<[bool; 4]>,
Expand Down Expand Up @@ -2931,6 +2995,42 @@ unsafe impl ShaderBackend for GL33 {
Ok(())
}

#[cfg(feature = "shader-f64")]
fn visit_dmat22<T>(&mut self, uni: &Uni<T>, value: &[[f64; 2]; 2]) -> Result<(), ShaderError>
where
T: AsRef<[[f64; 2]; 2]>,
{
unsafe {
gl::UniformMatrix2dv(uni.handle() as GLint, 1, gl::FALSE, value.as_ptr() as _);
}

Ok(())
}

#[cfg(feature = "shader-f64")]
fn visit_dmat33<T>(&mut self, uni: &Uni<T>, value: &[[f64; 3]; 3]) -> Result<(), ShaderError>
where
T: AsRef<[[f64; 3]; 3]>,
{
unsafe {
gl::UniformMatrix3dv(uni.handle() as GLint, 1, gl::FALSE, value.as_ptr() as _);
}

Ok(())
}

#[cfg(feature = "shader-f64")]
fn visit_dmat44<T>(&mut self, uni: &Uni<T>, value: &[[f64; 4]; 4]) -> Result<(), ShaderError>
where
T: AsRef<[[f64; 4]; 4]>,
{
unsafe {
gl::UniformMatrix4dv(uni.handle() as GLint, 1, gl::FALSE, value.as_ptr() as _);
}

Ok(())
}

fn visit_texture<D, P>(
&mut self,
uni: &Uni<InUseTexture<D, P>>,
Expand Down
1 change: 1 addition & 0 deletions luminance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ maintenance = { status = "actively-developed" }

[features]
default = ["luminance-derive"]
shader-f64 = []

[dependencies]
luminance-derive = { version = "0.11.0-dev", path = "../luminance-derive", optional = true }
Expand Down
21 changes: 21 additions & 0 deletions luminance/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,27 @@ pub unsafe trait ShaderBackend {
as_ref visit_mat44, [[f32; 4]; 4],
}

#[cfg(feature = "shader-f64")]
mk_uniform_visit! {
visit_f64, f64,
}

#[cfg(feature = "shader-f64")]
mk_uniform_visit! {
array visit_f64_array, f64,
}

#[cfg(feature = "shader-f64")]
mk_uniform_visit! {
as_ref visit_dvec2, [f64; 2],
as_ref visit_dvec3, [f64; 3],
as_ref visit_dvec4, [f64; 4],

as_ref visit_dmat22, [[f64; 2]; 2],
as_ref visit_dmat33, [[f64; 3]; 3],
as_ref visit_dmat44, [[f64; 4]; 4],
}

fn visit_texture<D, P>(
&mut self,
uni: &Uni<InUseTexture<D, P>>, // FIXME: probably wrong too?
Expand Down
67 changes: 67 additions & 0 deletions luminance/src/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,16 @@ pub enum UniType {

Floating(UniDim),

#[cfg(feature = "shader-f64")]
Floating64(UniDim),

Boolean(UniDim),

Matrix(UniMatDim),

#[cfg(feature = "shader-f64")]
Matrix64(UniMatDim),

Sampler(pixel::Type, Dim),

Buffer,
Expand Down Expand Up @@ -260,6 +266,12 @@ impl_Uniform!(array u32, visit_u32_array);
impl_Uniform!(array f32, visit_f32_array);
impl_Uniform!(array bool, visit_bool_array);

#[cfg(feature = "shader-f64")]
impl_Uniform!(f64, Floating64, visit_f64, UniDim::Dim1);

#[cfg(feature = "shader-f64")]
impl_Uniform!(array f64, visit_f64_array);

#[cfg(feature = "mint")]
impl_Uniform!(
as_ref
Expand Down Expand Up @@ -287,6 +299,15 @@ impl_Uniform!(
visit_vec2,
UniDim::Dim2
);
#[cfg(all(feature = "mint", feature = "shader-f64"))]
impl_Uniform!(
as_ref
mint::Vector2<f64>,
[f64; 2],
Floating64,
visit_dvec2,
UniDim::Dim2
);
#[cfg(feature = "mint")]
impl_Uniform!(
as_ref
Expand Down Expand Up @@ -323,6 +344,15 @@ impl_Uniform!(
visit_vec3,
UniDim::Dim3
);
#[cfg(all(feature = "mint", feature = "shader-f64"))]
impl_Uniform!(
as_ref
mint::Vector3<f64>,
[f64; 3],
Floating64,
visit_dvec3,
UniDim::Dim3
);
#[cfg(feature = "mint")]
impl_Uniform!(
as_ref
Expand Down Expand Up @@ -359,6 +389,15 @@ impl_Uniform!(
visit_vec4,
UniDim::Dim4
);
#[cfg(all(feature = "mint", feature = "shader-f64"))]
impl_Uniform!(
as_ref
mint::Vector4<f64>,
[f64; 4],
Floating64,
visit_dvec4,
UniDim::Dim4
);
#[cfg(feature = "mint")]
impl_Uniform!(
as_ref
Expand Down Expand Up @@ -397,6 +436,34 @@ impl_Uniform!(
UniMatDim::Mat44
);

#[cfg(all(feature = "mint", feature = "shader-f64"))]
impl_Uniform!(
as_ref
mint::ColumnMatrix2<f64>,
[[f64; 2]; 2],
Matrix64,
visit_dmat22,
UniMatDim::Mat22
);
#[cfg(all(feature = "mint", feature = "shader-f64"))]
impl_Uniform!(
as_ref
mint::ColumnMatrix3<f64>,
[[f64; 3]; 3],
Matrix64,
visit_dmat33,
UniMatDim::Mat33
);
#[cfg(all(feature = "mint", feature = "shader-f64"))]
impl_Uniform!(
as_ref
mint::ColumnMatrix4<f64>,
[[f64; 4]; 4],
Matrix64,
visit_dmat44,
UniMatDim::Mat44
);

// FIXME: I think we should be using Texture<D, P> and use InUseTexture<D, P::Type> as Value?!
impl<D, P> Uniform for InUseTexture<D, P>
where
Expand Down

0 comments on commit f01df4b

Please sign in to comment.