capi: finish basic gl capi

This commit is contained in:
chyyran 2022-12-03 18:56:57 -05:00
parent b569de1522
commit b928a8068d
8 changed files with 134 additions and 13 deletions

View file

@ -15,6 +15,9 @@
<sourceFolder url="file://$MODULE_DIR$/librashader-runtime-d3d11/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/librashader-runtime-d3d11/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/librashader-runtime-gl46/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/librashader-runtime-gl46/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/librashader-runtime/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/librashader-runtime/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/librashader-capi/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/librashader-capi-exports/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/librashader-capi/examples" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" /> <excludeFolder url="file://$MODULE_DIR$/target" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />

View file

@ -28,7 +28,7 @@ include = [
"PFN_lbr_preset_get_param", "PFN_lbr_preset_get_param",
"PFN_lbr_preset_print", "PFN_lbr_preset_print",
"PFN_lbr_preset_get_runtime_param_names", "PFN_lbr_preset_get_runtime_param_names",
"GLFilterChain" "FilterChain"
] ]
@ -37,5 +37,5 @@ include = [
[export.rename] [export.rename]
"LibrashaderError" = "_libra_error" "LibrashaderError" = "_libra_error"
"ShaderPreset" = "_shader_preset" "ShaderPreset" = "_shader_preset"
"GLFilterChain" = "_filter_chain_gl" "FilterChainGL" = "_filter_chain_gl"
"GLFilterChainOptions" = "filter_chain_gl_opt_t" "FilterChainOptionsGL" = "filter_chain_gl_opt_t"

View file

@ -9,7 +9,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
typedef struct FilterChain FilterChain; typedef struct _filter_chain_gl _filter_chain_gl;
typedef struct _libra_error _libra_error; typedef struct _libra_error _libra_error;
@ -27,12 +27,34 @@ typedef struct _shader_preset *libra_shader_preset_t;
typedef const void *(*gl_loader_t)(const char*); typedef const void *(*gl_loader_t)(const char*);
typedef struct FilterChainOptions { typedef struct filter_chain_gl_opt_t {
uint16_t gl_version; uint16_t gl_version;
bool use_dsa; bool use_dsa;
} FilterChainOptions; } filter_chain_gl_opt_t;
typedef struct FilterChain *libra_gl_filter_chain_t; typedef struct _filter_chain_gl *libra_gl_filter_chain_t;
typedef struct libra_source_image_gl_t {
uint32_t handle;
uint32_t format;
uint32_t width;
uint32_t height;
} libra_source_image_gl_t;
typedef struct libra_viewport_t {
float x;
float y;
uint32_t width;
uint32_t height;
} libra_viewport_t;
typedef struct libra_draw_framebuffer_gl_t {
uint32_t handle;
uint32_t texture;
uint32_t format;
uint32_t width;
uint32_t height;
} libra_draw_framebuffer_gl_t;
/** /**
* Load a preset. * Load a preset.
@ -106,9 +128,16 @@ libra_error_t libra_gl_init_context(gl_loader_t loader);
* - `out` may be either null or uninitialized, but must be aligned. * - `out` may be either null or uninitialized, but must be aligned.
*/ */
libra_error_t libra_gl_create_filter_chain(libra_shader_preset_t *preset, libra_error_t libra_gl_create_filter_chain(libra_shader_preset_t *preset,
const struct FilterChainOptions *options, const struct filter_chain_gl_opt_t *options,
libra_gl_filter_chain_t *out); libra_gl_filter_chain_t *out);
libra_error_t libra_gl_filter_chain_frame(libra_gl_filter_chain_t *chain,
size_t frame_count,
struct libra_source_image_gl_t image,
struct libra_viewport_t viewport,
struct libra_draw_framebuffer_gl_t out,
const float *mvp);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif // __cplusplus #endif // __cplusplus

View file

@ -7,3 +7,11 @@ pub type libra_error_t = *const LibrashaderError;
// #[cfg(feature = "runtime-opengl")] // #[cfg(feature = "runtime-opengl")]
pub type libra_gl_filter_chain_t = ManuallyDrop<Option<Box<librashader::runtime::gl::FilterChainGL>>>; pub type libra_gl_filter_chain_t = ManuallyDrop<Option<Box<librashader::runtime::gl::FilterChainGL>>>;
#[repr(C)]
pub struct libra_viewport_t {
pub x: f32,
pub y: f32,
pub width: u32,
pub height: u32,
}

View file

@ -1,11 +1,14 @@
use std::ffi::{c_char, c_void, CString}; use std::ffi::{c_char, c_void, CString};
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
use crate::ctypes::{libra_error_t, libra_gl_filter_chain_t, libra_shader_preset_t}; use crate::ctypes::{libra_error_t, libra_gl_filter_chain_t, libra_shader_preset_t, libra_viewport_t};
use crate::error::{assert_non_null, assert_some, LibrashaderError}; use crate::error::{assert_non_null, assert_some, LibrashaderError};
use crate::ffi::ffi_body; use crate::ffi::ffi_body;
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
use librashader::runtime::FilterChain;
use librashader::runtime::gl::{Framebuffer, GLImage, Viewport};
pub use librashader::runtime::gl::options::FilterChainOptionsGL; pub use librashader::runtime::gl::options::FilterChainOptionsGL;
use librashader::Size;
pub type gl_loader_t = unsafe extern "C" fn (*const c_char) -> *const c_void; pub type gl_loader_t = unsafe extern "C" fn (*const c_char) -> *const c_void;
/// Initialize the OpenGL Context for librashader. /// Initialize the OpenGL Context for librashader.
@ -58,4 +61,59 @@ pub unsafe extern "C" fn libra_gl_create_filter_chain(preset: *mut libra_shader_
out.write(MaybeUninit::new(ManuallyDrop::new(Some(Box::new(chain))))) out.write(MaybeUninit::new(ManuallyDrop::new(Some(Box::new(chain)))))
} }
}) })
} }
#[repr(C)]
pub struct libra_source_image_gl_t {
pub handle: u32,
pub format: u32,
pub width: u32,
pub height: u32
}
#[repr(C)]
pub struct libra_draw_framebuffer_gl_t {
pub handle: u32,
pub texture: u32,
pub format: u32,
pub width: u32,
pub height: u32
}
impl From<libra_source_image_gl_t> for GLImage {
fn from(value: libra_source_image_gl_t) -> Self {
GLImage {
handle: value.handle,
format: value.format,
size: Size::new(value.width, value.height),
padded_size: Size::default()
}
}
}
#[no_mangle]
pub unsafe extern "C" fn libra_gl_filter_chain_frame(chain: *mut libra_gl_filter_chain_t,
frame_count: usize,
image: libra_source_image_gl_t,
viewport: libra_viewport_t,
out: libra_draw_framebuffer_gl_t,
mvp: *const f32,
) -> libra_error_t {
ffi_body!(mut |chain| {
assert_some!(chain);
let chain = chain.as_mut().unwrap();
let image: GLImage = image.into();
let viewport = Viewport {
x: viewport.x,
y: viewport.y,
output: &chain.create_framebuffer_raw(out.texture, out.handle, out.format, Size::new(out.width, out.height), 1),
mvp: None,
};
chain.frame(&image, &viewport, frame_count, None)?;
})
}

View file

@ -7,8 +7,8 @@ use librashader_reflect::back::cross::{CrossGlslContext, GlslVersion};
use librashader_reflect::back::targets::GLSL; use librashader_reflect::back::targets::GLSL;
use librashader_reflect::front::shaderc::GlslangCompilation; use librashader_reflect::front::shaderc::GlslangCompilation;
use spirv_cross::spirv::Decoration; use spirv_cross::spirv::Decoration;
use gl::types::{GLint, GLuint}; use gl::types::{GLenum, GLint, GLuint};
use librashader_common::{FilterMode, WrapMode}; use librashader_common::{FilterMode, Size, WrapMode};
use std::collections::VecDeque; use std::collections::VecDeque;
use librashader_reflect::reflect::ReflectShader; use librashader_reflect::reflect::ReflectShader;
use crate::{error, GLImage, util, Viewport}; use crate::{error, GLImage, util, Viewport};
@ -84,6 +84,13 @@ type ShaderPassMeta = (
); );
impl<T: GLInterface> FilterChainImpl<T> { impl<T: GLInterface> FilterChainImpl<T> {
pub(crate) fn create_framebuffer_raw(&self, texture: GLuint,
handle: GLuint,
format: GLenum,
size: Size<u32>,
miplevels: u32,) -> Framebuffer {
T::FramebufferInterface::new_from_raw(texture, handle, format, size, miplevels)
}
/// Load a filter chain from a pre-parsed `ShaderPreset`. /// Load a filter chain from a pre-parsed `ShaderPreset`.
pub(crate) fn load_from_preset( pub(crate) fn load_from_preset(
preset: ShaderPreset, preset: ShaderPreset,

View file

@ -1,9 +1,10 @@
use std::path::Path; use std::path::Path;
use gl::types::{GLenum, GLuint};
use librashader_presets::ShaderPreset; use librashader_presets::ShaderPreset;
use crate::filter_chain::filter_impl::FilterChainImpl; use crate::filter_chain::filter_impl::FilterChainImpl;
use crate::filter_chain::inner::FilterChainDispatch; use crate::filter_chain::inner::FilterChainDispatch;
use crate::{GLImage, Viewport}; use crate::{Framebuffer, GLImage, Viewport};
use crate::error::{Result, FilterChainError}; use crate::error::{Result, FilterChainError};
use crate::options::{FilterChainOptionsGL, FrameOptionsGL}; use crate::options::{FilterChainOptionsGL, FrameOptionsGL};
@ -12,12 +13,26 @@ mod inner;
mod parameters; mod parameters;
pub(crate) use filter_impl::FilterCommon; pub(crate) use filter_impl::FilterCommon;
use librashader_common::Size;
pub struct FilterChainGL { pub struct FilterChainGL {
pub(in crate::filter_chain) filter: FilterChainDispatch, pub(in crate::filter_chain) filter: FilterChainDispatch,
} }
impl FilterChainGL { impl FilterChainGL {
pub fn create_framebuffer_raw(&self, texture: GLuint,
handle: GLuint,
format: GLenum,
size: Size<u32>,
miplevels: u32,) -> Framebuffer {
match &self.filter {
FilterChainDispatch::DirectStateAccess(p) => {
p.create_framebuffer_raw(texture, handle, format, size, miplevels)
}
FilterChainDispatch::Compatibility(p) => p.create_framebuffer_raw(texture, handle, format, size, miplevels),
}
}
pub fn load_from_preset( pub fn load_from_preset(
preset: ShaderPreset, preset: ShaderPreset,
options: Option<&FilterChainOptionsGL>, options: Option<&FilterChainOptionsGL>,

View file

@ -20,6 +20,7 @@ mod viewport;
pub use filter_chain::FilterChainGL; pub use filter_chain::FilterChainGL;
pub use framebuffer::GLImage; pub use framebuffer::GLImage;
pub use viewport::Viewport; pub use viewport::Viewport;
pub use crate::gl::Framebuffer;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {